aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-offline-sync.c
blob: dd10c58db21f3a47f6ceb5f6989d6f6df75ee568 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-shell-offline-sync.c - Sync folders before going into Offline mode.
 *
 * Copyright (C) 2002 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: Ettore Perazzoli <ettore@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-shell-offline-sync.h"

#include "e-shell.h"
#include "e-shell-constants.h"

#include "Evolution.h"

#include <gconf/gconf-client.h>

#include <gal/widgets/e-gui-utils.h>

#include <gtk/gtklabel.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkprogressbar.h>
#include <gtk/gtksignal.h>

#include <libgnomeui/gnome-dialog.h>
#include <libgnome/gnome-i18n.h>

#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-exception.h>


typedef struct _SyncData SyncData;
typedef struct _SyncFolderProgressListenerServant SyncFolderProgressListenerServant;

struct _SyncFolderProgressListenerServant {
    POA_GNOME_Evolution_SyncFolderProgressListener servant;
    SyncData *sync_data;
};

struct _SyncData {
    /* The shell.  */
    EShell *shell;

    /* The progress dialog.  */
    GtkWidget *dialog;

    /* Label in the progress dialog.  */
    GtkWidget *label;

    /* Progress bar in the progress dialog.  */
    GtkWidget *progress_bar;

    /* Path of the folder currently being synced.  */
    char *current_folder_path;

    /* Whether to cancel the current folder's syncing.  */
    gboolean cancel;

    /* Whether the current folder is finished syncing; used for async
       notification of completion.  */
    gboolean current_folder_finished;

    /* CORBA cruft.  */
    SyncFolderProgressListenerServant *progress_listener_servant;
    GNOME_Evolution_SyncFolderProgressListener progress_listener_objref;
};


/* The progress listener interface.  */

static PortableServer_ServantBase__epv SyncFolderProgressListener_base_epv;
static POA_GNOME_Evolution_SyncFolderProgressListener__epv SyncFolderProgressListener_epv;
static POA_GNOME_Evolution_SyncFolderProgressListener__vepv SyncFolderProgressListener_vepv;

static SyncFolderProgressListenerServant *
progress_listener_servant_new (SyncData *sync_data)
{
    SyncFolderProgressListenerServant *servant;

    servant = g_new0 (SyncFolderProgressListenerServant, 1);

    servant->servant.vepv = &SyncFolderProgressListener_vepv;
    servant->sync_data    = sync_data;

    return servant;
}

static void
progress_listener_servant_free (SyncFolderProgressListenerServant *servant)
{
    CORBA_Environment ev;
    PortableServer_ObjectId *oid;

    CORBA_exception_init (&ev);

    oid = PortableServer_POA_servant_to_id (bonobo_poa (), servant, &ev);
    PortableServer_POA_deactivate_object (bonobo_poa (), oid, &ev);
    CORBA_free (oid);

    CORBA_exception_free (&ev);

    g_free (servant);
}

static void
impl_SyncFolderProgressListener_updateProgress (PortableServer_Servant servant,
                        const CORBA_float percent,
                        CORBA_Environment *ev)
{
    SyncData *sync_data;

    sync_data = ((SyncFolderProgressListenerServant *) servant)->sync_data;
    gtk_progress_set_percentage (GTK_PROGRESS (sync_data->progress_bar), percent);
}

static void
impl_SyncFolderProgressListener_reportSuccess (PortableServer_Servant servant,
                           CORBA_Environment *ev)
{
    SyncData *sync_data;

    sync_data = ((SyncFolderProgressListenerServant *) servant)->sync_data;
    sync_data->current_folder_finished = TRUE;
}

static void
impl_SyncFolderProgressListener_reportFailure (PortableServer_Servant servant,
                           const CORBA_char *message,
                           CORBA_Environment *ev)
{
    EFolder *folder;
    SyncData *sync_data;

    sync_data = ((SyncFolderProgressListenerServant *) servant)->sync_data;

    folder = e_storage_set_get_folder (e_shell_get_storage_set (sync_data->shell),
                       sync_data->current_folder_path);

    /* FIXME -- We probably should give the user more of a chance to do
       something about it.  */
    e_notice (GTK_WINDOW (sync_data->dialog),
          _("Error synchronizing \"%s\":\n%s"), e_folder_get_name (folder), message);

    sync_data->current_folder_finished = TRUE;
}

static gboolean
setup_progress_listener (SyncData *sync_data)
{
    SyncFolderProgressListenerServant *servant;
    CORBA_Environment ev;

    SyncFolderProgressListener_base_epv._private    = NULL;
    SyncFolderProgressListener_base_epv.finalize    = NULL;
    SyncFolderProgressListener_base_epv.default_POA = NULL;

    SyncFolderProgressListener_epv.updateProgress = impl_SyncFolderProgressListener_updateProgress;
    SyncFolderProgressListener_epv.reportSuccess  = impl_SyncFolderProgressListener_reportSuccess;
    SyncFolderProgressListener_epv.reportFailure  = impl_SyncFolderProgressListener_reportFailure;

    SyncFolderProgressListener_vepv._base_epv = &SyncFolderProgressListener_base_epv;
    SyncFolderProgressListener_vepv.GNOME_Evolution_SyncFolderProgressListener_epv = &SyncFolderProgressListener_epv;

    servant = progress_listener_servant_new (sync_data);

    CORBA_exception_init (&ev);

    POA_GNOME_Evolution_SyncFolderProgressListener__init ((PortableServer_Servant) servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Cannot initialize GNOME::Evolution::Offline::ProgressListener");
        progress_listener_servant_free (servant);
        CORBA_exception_free (&ev);
        return FALSE;
    }

    CORBA_free (PortableServer_POA_activate_object (bonobo_poa (), servant, &ev));

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Cannot activate GNOME::Evolution::Offline::ProgressListener");
        progress_listener_servant_free (servant);
        CORBA_exception_free (&ev);
        return FALSE;
    }

    sync_data->progress_listener_servant = servant;
    sync_data->progress_listener_objref = PortableServer_POA_servant_to_reference (bonobo_poa (),
                                               servant, &ev);

    CORBA_exception_free (&ev);

    return TRUE;
}


/* Setting up the progress dialog.  */

static int
progress_dialog_close_callback (GnomeDialog *dialog,
                void *data)
{
    /* Don't allow the dialog to be closed through the window manager close
       command.  */
    return TRUE;
}

static void
progress_dialog_clicked_callback (GnomeDialog *dialog,
                  int button_num,
                  void *data)
{
    SyncData *sync_data;

    sync_data = (SyncData *) data;
    sync_data->cancel = TRUE;
}

static void
setup_dialog (SyncData *sync_data)
{
    sync_data->dialog = gnome_dialog_new (_("Syncing Folder"), GNOME_STOCK_BUTTON_CANCEL, NULL);
    gtk_widget_set_usize (sync_data->dialog, 300, -1);
    gtk_window_set_policy (GTK_WINDOW (sync_data->dialog), FALSE, FALSE, FALSE);

    g_signal_connect (sync_data->dialog, "close",
              G_CALLBACK (progress_dialog_close_callback), sync_data);
    g_signal_connect (sync_data->dialog, "clicked",
              G_CALLBACK (progress_dialog_clicked_callback), sync_data);

    sync_data->label = gtk_label_new ("");
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (sync_data->dialog)->vbox),
                sync_data->label, FALSE, TRUE, 0);

    sync_data->progress_bar = gtk_progress_bar_new ();
    gtk_progress_set_activity_mode (GTK_PROGRESS (sync_data->progress_bar), FALSE);
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (sync_data->dialog)->vbox),
                sync_data->progress_bar, FALSE, TRUE, 0);

    gtk_widget_show_all (sync_data->dialog);
}


/* Sync the folder at the specified @folder_path.  */
static void
sync_folder (SyncData *sync_data,
         const char *folder_path,
         int num,
         int total)
{
    EvolutionShellComponentClient *component_client;
    EStorageSet *storage_set;
    GNOME_Evolution_Folder *corba_folder;
    GNOME_Evolution_Offline offline_interface;
    CORBA_Environment ev;
    EFolder *folder;
    char *evolution_uri;
    char *msg;

    CORBA_exception_init (&ev);

    /* Retrieve the folder that needs to be synced from the storage set, as
       well as the component that should perform the syncing.  */

    storage_set = e_shell_get_storage_set (sync_data->shell);

    folder = e_storage_set_get_folder (storage_set, folder_path);
    if (folder == NULL) {
        /* This might be a remote folder that is not visible right now,
           or is otherwise hidden from the tree somehow.  So we just
           ignore it, and keep going without signalling any error.  */
        return;
    }

    /* Don't attempt to sync folders that don't have the can_sync_offline
       property set.  */
    if (! e_folder_get_can_sync_offline (folder))
        return;

    component_client = e_folder_type_registry_get_handler_for_type (e_shell_get_folder_type_registry (sync_data->shell),
                                    e_folder_get_type_string (folder));

    offline_interface = evolution_shell_component_client_get_offline_interface (component_client);
    if (offline_interface == CORBA_OBJECT_NIL) {
        /* The component doesn't support going off-line, just ignore
           this as it's probably a programming error in the
           implementation of the component.  */
        return;
    }

    /* Prepare the CORBA folder to be passed to the component.  */

    corba_folder = GNOME_Evolution_Folder__alloc ();
    evolution_uri = g_strconcat (E_SHELL_URI_PREFIX, "/", folder_path, NULL);
    e_folder_to_corba (folder, evolution_uri, corba_folder);
    g_free (evolution_uri);

    /* Prepare the dialog.  */

    msg = g_strdup_printf (_("Synchronizing \"%s\" (%d of %d) ..."),
                   e_folder_get_name (folder), num, total);
    gtk_label_set (GTK_LABEL (sync_data->label), msg);
    g_free (msg);

    gtk_progress_set_value (GTK_PROGRESS (sync_data->progress_bar), 0.0);

    /* Get the data ready.  */

    g_free (sync_data->current_folder_path);
    sync_data->current_folder_path = g_strdup (folder_path);
    sync_data->current_folder_finished = FALSE;
    sync_data->cancel = FALSE;

    /* Tell the component to start syncing.  */

    GNOME_Evolution_Offline_syncFolder (offline_interface,
                        corba_folder,
                        sync_data->progress_listener_objref,
                        &ev);
    if (BONOBO_EX (&ev)) {
        g_warning ("Error invoking ::syncFolder -- %s", BONOBO_EX_REPOID (&ev));
        CORBA_free (corba_folder);
        CORBA_exception_free (&ev);
        return;
    }

    /* Wait for the component to signal completion...  */

    while (! sync_data->current_folder_finished && ! sync_data->cancel) {
        gtk_main_iteration ();

        /* Check if the user clicked the Cancel button.  */
        if (sync_data->cancel) {
            gnome_dialog_set_sensitive (GNOME_DIALOG (sync_data->dialog), 0, FALSE);

            GNOME_Evolution_Offline_cancelSyncFolder (offline_interface, corba_folder, &ev);

            while (! sync_data->current_folder_finished)
                gtk_main_iteration ();

            break;
        }
    }

    /* All done.  */

    CORBA_free (corba_folder);
    CORBA_exception_free (&ev);
}

/* Free up the data needed for syncing.  */
static void
cleanup (SyncData *sync_data)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    if (sync_data->dialog != NULL)
        gtk_widget_destroy (sync_data->dialog);

    if (sync_data->progress_listener_servant != NULL)
        progress_listener_servant_free (sync_data->progress_listener_servant);

    if (sync_data->progress_listener_objref != CORBA_OBJECT_NIL)
        CORBA_Object_release (sync_data->progress_listener_objref, &ev);

    g_free (sync_data);

    CORBA_exception_free (&ev);
}


void
e_shell_offline_sync_all_folders (EShell *shell,
                  GtkWindow *parent_window)
{
    GConfClient *gconf_client;
    SyncData *sync_data;
    GSList *path_list;
    GSList *p;
    int i;

    gconf_client = gconf_client_get_default ();

    path_list = gconf_client_get_list (gconf_client, "/apps/evolution/shell/offline/folder_paths",
                       GCONF_VALUE_STRING, NULL);

    sync_data = g_new0 (SyncData, 1);
    sync_data->shell = shell;

    /* Initialize everything, then go ahead and sync.  */

    if (! setup_progress_listener (sync_data))
        goto done;

    setup_dialog (sync_data);

    for (p = path_list, i = 1; p != NULL; p = p->next, i ++) {
        const char *path;

        path = (const char *) p->data;

        sync_folder (sync_data, path, i, g_slist_length (path_list));

        /* If the operation has been cancelled, stop syncing and
           return.  */
        if (sync_data->cancel) {
            /* FIXME: Do we want to pop up a dialog asking for
               confirmation?  */
            break;
        }
    }

 done:
    cleanup (sync_data);

    g_slist_foreach (path_list, (GFunc) g_free, NULL);
    g_slist_free (path_list);
}