aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/migration.c
blob: be93309f1f3bf5a3f6c9970eb0f779e162b95561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* calendar-component.c
 *
 * Copyright (C) 2003  Ximian, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Rodrigo Moya <rodrigo@ximian.com>
 */

#include <bonobo/bonobo-i18n.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <libgnomevfs/gnome-vfs-xfer.h>
#include <gal/util/e-util.h>
#include "migration.h"

static gboolean
process_calendar_dir (ESourceGroup *source_group, const char *path,
              const char *name, const char *base_uri)
{
    char *s;
    GnomeVFSURI *from, *to;
    GnomeVFSResult vres;
    ESource *source;
    GDir *dir;
    gboolean retval = TRUE;

    s = g_build_filename (path, "calendar.ics", NULL);
    if (!g_file_test (s, G_FILE_TEST_EXISTS)) {
        g_free (s);
        return FALSE;
    }

    /* transfer the old file to its new location */
    from = gnome_vfs_uri_new (s);
    g_free (s);
    if (!from)
        return FALSE;

    s = g_build_filename (e_source_group_peek_base_uri (source_group), base_uri,
                  "calendar.ics", NULL);
    if (e_mkdir_hier (s, 0700) != 0) {
        gnome_vfs_uri_unref (from);
        g_free (s);
        return FALSE;
    }
    to = gnome_vfs_uri_new (s);
    g_free (s);
    if (!to) {
        gnome_vfs_uri_unref (from);
        return FALSE;
    }

    vres = gnome_vfs_xfer_uri ((const GnomeVFSURI *) from,
                   (const GnomeVFSURI *) to,
                   GNOME_VFS_XFER_DEFAULT,
                   GNOME_VFS_XFER_ERROR_MODE_ABORT,
                   GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
                   NULL, NULL);
    gnome_vfs_uri_unref (from);
    gnome_vfs_uri_unref (to);

    if (vres != GNOME_VFS_OK)
        return FALSE;

    /* create the new source */
    source = e_source_new (name, base_uri);
    e_source_group_add_source (source_group, source, -1);

    /* process subfolders */
    s = g_build_filename (path, "subfolders", NULL);
    dir = g_dir_open (s, 0, NULL);
    if (dir) {
        const char *name, *tmp_s;

        while ((name = g_dir_read_name (dir))) {
            tmp_s = g_build_filename (s, name, NULL);
            if (g_file_test (tmp_s, G_FILE_TEST_IS_DIR)) {
                retval = process_calendar_dir (source_group, tmp_s, name, name);
            }

            g_free (tmp_s);
        }

        g_dir_close (dir);
    }

    g_free (s);

    return retval;
}

gboolean
migrate_old_calendars (ESourceGroup *source_group)
{
    char *path;
    gboolean retval;

    g_return_val_if_fail (E_IS_SOURCE_GROUP (source_group), FALSE);

    path = g_build_filename (g_get_home_dir (), "evolution", NULL);
        if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
        g_free (path);
                return FALSE;
    }
    g_free (path);

    /* look for the top-level calendar */
    path = g_build_filename (g_get_home_dir (), "evolution/local/Calendar", NULL);
    retval = process_calendar_dir (source_group, path, _("Personal"), "Personal");
    g_free (path);
    
        return retval;
}