aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/tasks-migrate.c
blob: 885c782a0ce03292c644a1ac45ea7cf65dc6a036 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Evolution calendar - Migrate tasks from the calendar folder to the tasks folder
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Authors: Federico Mena-Quintero <federico@ximian.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnome.h>
#include <cal-client/cal-client.h>
#include "component-factory.h"
#include "tasks-migrate.h"



/* Client for the calendar folder */
static CalClient *calendar_client = NULL;

/* Client for the tasks folder */
static CalClient *tasks_client = NULL;

/* Whether we have done the migration yet */
static gboolean migrated = FALSE;



/* Performs the actual migration process */
static void
migrate (void)
{
    GList *uids;
    GList *l;
    gboolean success;
    gboolean at_least_one;

    g_assert (calendar_client != NULL);
    g_assert (tasks_client != NULL);
    g_assert (cal_client_get_load_state (calendar_client) == CAL_CLIENT_LOAD_LOADED);
    g_assert (cal_client_get_load_state (tasks_client) == CAL_CLIENT_LOAD_LOADED);

    uids = cal_client_get_uids (calendar_client, CALOBJ_TYPE_TODO);

    success = TRUE;
    at_least_one = FALSE;

    for (l = uids; l; l = l->next) {
        const char *uid;
        CalComponent *comp;
        CalClientGetStatus status;

        at_least_one = TRUE;

        uid = l->data;
        status = cal_client_get_object (calendar_client, uid, &comp);

        switch (status) {
        case CAL_CLIENT_GET_SUCCESS:
            if (cal_client_update_object (tasks_client, comp))
                cal_client_remove_object (calendar_client, uid);
            else
                success = FALSE;

            gtk_object_unref (GTK_OBJECT (comp));
            break;

        case CAL_CLIENT_GET_NOT_FOUND:
            /* This is OK; the object may have disappeared from the server */
            break;

        case CAL_CLIENT_GET_SYNTAX_ERROR:
            success = FALSE;
            break;

        default:
            g_assert_not_reached ();
        }
    }

    cal_obj_uid_list_free (uids);

    if (!at_least_one)
        return;

    if (success)
        gnome_ok_dialog (_("Evolution has taken the tasks that were in your calendar folder "
                   "and automatically migrated them to the new tasks folder."));
    else
        gnome_ok_dialog (_("Evolution has tried to take the tasks that were in your "
                   "calendar folder and migrate them to the new tasks folder.\n"
                   "Some of the tasks could not be migrated, so "
                   "this process may be attempted again in the future."));
}

/* Displays an error to indicate that a calendar could not be opened */
static void
open_error (const char *uri)
{
    char *msg;

    msg = g_strdup_printf (_("Could not open `%s'; no items from the calendar folder "
                 "will be migrated to the tasks folder."),
                 uri);
    gnome_error_dialog (msg);
    g_free (msg);
}

/* Displays an error to indicate that a URI method is not supported */
static void
method_error (const char *uri)
{
    char *msg;

    msg = g_strdup_printf (_("The method required to load `%s' is not supported; "
                 "no items from the calendar folder will be migrated "
                 "to the tasks folder."),
                   uri);
    gnome_error_dialog (msg);
    g_free (msg);
}

/* Callback used when the tasks client is finished loading */
static void
tasks_opened_cb (CalClient *client, CalClientOpenStatus status, gpointer data)
{
    g_assert (calendar_client != NULL);
    g_assert (cal_client_get_load_state (calendar_client) == CAL_CLIENT_LOAD_LOADED);

    switch (status) {
    case CAL_CLIENT_OPEN_SUCCESS:
        migrate ();
        break;

    case CAL_CLIENT_OPEN_ERROR:
        open_error (cal_client_get_uri (client));
        migrated = FALSE;
        break;

    case CAL_CLIENT_OPEN_NOT_FOUND:
        /* This can't happen because we did not specify only_if_exists when
         * issuing the open request.
         */
        g_assert_not_reached ();
        break;

    case CAL_CLIENT_OPEN_METHOD_NOT_SUPPORTED:
        method_error (cal_client_get_uri (client));
        migrated = FALSE;
        break;

    default:
        g_assert_not_reached ();
    }

    gtk_object_unref (GTK_OBJECT (calendar_client));
    calendar_client = NULL;

    gtk_object_unref (GTK_OBJECT (tasks_client));
    tasks_client = NULL;
}

/* Initiates the loading process for the tasks client */
static gboolean
load_tasks_client (void)
{
    char *uri;
    gboolean success;

    g_assert (calendar_client != NULL);
    g_assert (cal_client_get_load_state (calendar_client) == CAL_CLIENT_LOAD_LOADED);

    tasks_client = cal_client_new ();
    if (!tasks_client)
        goto error;

    gtk_signal_connect (GTK_OBJECT (tasks_client), "cal_opened",
                GTK_SIGNAL_FUNC (tasks_opened_cb),
                NULL);

    uri = g_strdup_printf ("%s/local/Tasks/tasks.ics", evolution_dir);
    success = cal_client_open_calendar (tasks_client, uri, FALSE);
    g_free (uri);

    if (success)
        return TRUE;

 error:
    g_message ("load_tasks_client(): could not issue open request for the tasks client");

    if (tasks_client) {
        gtk_object_unref (GTK_OBJECT (tasks_client));
        tasks_client = NULL;
    }

    return FALSE;
}

/* Callback used when the calendar client finishes loading */
static void
calendar_opened_cb (CalClient *client, CalClientOpenStatus status, gpointer data)
{
    switch (status) {
    case CAL_CLIENT_OPEN_SUCCESS:
        if (!load_tasks_client ()) {
            migrated = FALSE;
            break;
        }

        return;

    case CAL_CLIENT_OPEN_ERROR:
        open_error (cal_client_get_uri (client));
        migrated = FALSE;
        break;

    case CAL_CLIENT_OPEN_NOT_FOUND:
        /* This is OK; the calendar folder did not exist in the first
         * place so there is nothing to migrate.
         */
        break;

    case CAL_CLIENT_OPEN_METHOD_NOT_SUPPORTED:
        method_error (cal_client_get_uri (client));
        migrated = FALSE;
        break;

    default:
        g_assert_not_reached ();
    }

    gtk_object_unref (GTK_OBJECT (calendar_client));
    calendar_client = NULL;
}

/* Initiates the loading process for the calendar client */
static gboolean
load_calendar_client (void)
{
    char *uri;
    gboolean success;

    /* First we load the calendar client; the tasks client will be loaded
     * later only if the former one succeeds.
     */

    calendar_client = cal_client_new ();
    if (!calendar_client)
        goto error;

    gtk_signal_connect (GTK_OBJECT (calendar_client), "cal_opened",
                GTK_SIGNAL_FUNC (calendar_opened_cb),
                NULL);

    uri = g_strdup_printf ("%s/local/Calendar/calendar.ics", evolution_dir);
    success = cal_client_open_calendar (calendar_client, uri, TRUE);
    g_free (uri);

    if (success)
        return TRUE;

 error:
    g_message ("load_calendar_client(): could not issue open request for the calendar client");

    if (calendar_client) {
        gtk_object_unref (GTK_OBJECT (calendar_client));
        calendar_client = NULL;
    }

    return FALSE;
}

/**
 * tasks_migrate:
 *
 * Initiates the asynchronous process that migrates the tasks from the default
 * user calendar folder to the default tasks folder.  This is because Evolution
 * used to store tasks in the same folder as the calendar by default, but they
 * are separate folders now.
 **/
void
tasks_migrate (void)
{
    g_assert (!migrated);
    migrated = TRUE;

    if (!load_calendar_client ())
        migrated = FALSE;
}