aboutsummaryrefslogtreecommitdiffstats
path: root/modules/startup-wizard/e-mail-config-import-page.c
blob: e45963608a97e84583c87564c31269057ffa4ed8 (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
/*
 * e-mail-config-import-page.c
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "e-mail-config-import-page.h"

#include <config.h>
#include <glib/gi18n-lib.h>

#define E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_CONFIG_IMPORT_PAGE, EMailConfigImportPagePrivate))

typedef struct _AsyncContext AsyncContext;

struct _EMailConfigImportPagePrivate {
    EImport *import;
    EImportTarget *import_target;
    GSList *available_importers;
};

struct _AsyncContext {
    EMailConfigImportPage *page;
    GQueue pending_importers;
    EActivity *activity;
    GCancellable *cancellable;
    gulong cancel_id;
};

/* Forward Declarations */
static void e_mail_config_import_page_interface_init
                    (EMailConfigPageInterface *iface);
static gboolean mail_config_import_page_next    (gpointer user_data);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (
    EMailConfigImportPage,
    e_mail_config_import_page,
    GTK_TYPE_BOX,
    0,
    G_IMPLEMENT_INTERFACE_DYNAMIC (
        E_TYPE_MAIL_CONFIG_PAGE,
        e_mail_config_import_page_interface_init))

static void
async_context_free (AsyncContext *async_context)
{
    if (async_context->page != NULL)
        g_object_unref (async_context->page);

    if (async_context->activity != NULL)
        g_object_unref (async_context->activity);

    if (async_context->cancellable != NULL) {
        g_cancellable_disconnect (
            async_context->cancellable,
            async_context->cancel_id);
        g_object_unref (async_context->cancellable);
    }

    g_queue_clear (&async_context->pending_importers);

    g_slice_free (AsyncContext, async_context);
}

static void
mail_config_import_page_status (EImport *import,
                                const gchar *what,
                                gint percent,
                                GSimpleAsyncResult *simple)
{
    AsyncContext *async_context;

    async_context = g_simple_async_result_get_op_res_gpointer (simple);

    e_activity_set_text (async_context->activity, what);
    e_activity_set_percent (async_context->activity, (gdouble) percent);
}

static void
mail_config_import_page_complete (EImport *import,
                                  GSimpleAsyncResult *simple)
{
    /* Schedule the next importer to start. */
    g_idle_add (mail_config_import_page_next, simple);
}

static gboolean
mail_config_import_page_next (gpointer user_data)
{
    GSimpleAsyncResult *simple;
    AsyncContext *async_context;
    GCancellable *cancellable;
    EImportImporter *next_importer;
    GError *error = NULL;

    simple = G_SIMPLE_ASYNC_RESULT (user_data);
    async_context = g_simple_async_result_get_op_res_gpointer (simple);
    cancellable = async_context->cancellable;

    /* Pop the completed importer and peek at the next one. */
    g_queue_pop_head (&async_context->pending_importers);
    next_importer = g_queue_peek_head (&async_context->pending_importers);

    if (g_cancellable_set_error_if_cancelled (cancellable, &error)) {
        g_simple_async_result_take_error (simple, error);
        g_simple_async_result_complete (simple);
        g_object_unref (simple);

    } else if (next_importer != NULL) {
        e_import_import (
            async_context->page->priv->import,
            async_context->page->priv->import_target,
            next_importer,
            (EImportStatusFunc) mail_config_import_page_status,
            (EImportCompleteFunc) mail_config_import_page_complete,
            simple);

    } else {
        g_simple_async_result_complete (simple);
        g_object_unref (simple);
    }

    return FALSE;
}

static void
mail_config_import_page_cancelled (GCancellable *cancellable,
                                   AsyncContext *async_context)
{
    GQueue *pending_importers;
    EImportImporter *current_importer;

    pending_importers = &async_context->pending_importers;
    current_importer = g_queue_peek_head (pending_importers);
    g_return_if_fail (current_importer != NULL);

    e_import_cancel (
        async_context->page->priv->import,
        async_context->page->priv->import_target,
        current_importer);
}

static void
mail_config_import_page_dispose (GObject *object)
{
    EMailConfigImportPagePrivate *priv;

    priv = E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (object);

    if (priv->import != NULL) {
        e_import_target_free (
            priv->import,
            priv->import_target);
        g_object_unref (priv->import);
        priv->import_target = NULL;
        priv->import = NULL;
    }

    g_slist_free (priv->available_importers);
    priv->available_importers = NULL;

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_config_import_page_parent_class)->
        dispose (object);
}

static void
mail_config_import_page_constructed (GObject *object)
{
    EMailConfigImportPage *page;
    GtkWidget *widget;
    GtkWidget *container;
    GSList *list, *link;
    const gchar *text;
    gint row = 0;

    page = E_MAIL_CONFIG_IMPORT_PAGE (object);

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_config_import_page_parent_class)->constructed (object);

    gtk_orientable_set_orientation (
        GTK_ORIENTABLE (page), GTK_ORIENTATION_VERTICAL);

    gtk_box_set_spacing (GTK_BOX (page), 24);

    text = _("Please select the information "
         "that you would like to import:");
    widget = gtk_label_new (text);
    gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
    gtk_box_pack_start (GTK_BOX (page), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    widget = gtk_grid_new ();
    gtk_grid_set_row_spacing (GTK_GRID (widget), 12);
    gtk_grid_set_column_spacing (GTK_GRID (widget), 12);
    gtk_box_pack_start (GTK_BOX (page), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    container = widget;

    list = page->priv->available_importers;

    for (link = list; link != NULL; link = link->next) {
        EImportImporter *importer = link->data;
        gchar *from_text;

        widget = e_import_get_widget (
            page->priv->import,
            page->priv->import_target, importer);
        if (widget == NULL)
            continue;
        gtk_grid_attach (GTK_GRID (container), widget, 1, row, 1, 1);
        gtk_widget_show (widget);

        from_text = g_strdup_printf (_("From %s:"), importer->name);
        widget = gtk_label_new (from_text);
        gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
        gtk_grid_attach (GTK_GRID (container), widget, 0, row, 1, 1);
        gtk_widget_show (widget);

        row++;
    }
}

static void
e_mail_config_import_page_class_init (EMailConfigImportPageClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (
        class, sizeof (EMailConfigImportPagePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = mail_config_import_page_dispose;
    object_class->constructed = mail_config_import_page_constructed;
}

static void
e_mail_config_import_page_class_finalize (EMailConfigImportPageClass *class)
{
}

static void
e_mail_config_import_page_interface_init (EMailConfigPageInterface *iface)
{
    iface->title = _("Importing Files");
    iface->sort_order = E_MAIL_CONFIG_IMPORT_PAGE_SORT_ORDER;
}

static void
e_mail_config_import_page_init (EMailConfigImportPage *page)
{
    page->priv = E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (page);

    page->priv->import =
        e_import_new ("org.gnome.evolution.shell.importer");
    page->priv->import_target = (EImportTarget *)
        e_import_target_new_home (page->priv->import);
    page->priv->available_importers = e_import_get_importers (
        page->priv->import, page->priv->import_target);
}

void
e_mail_config_import_page_type_register (GTypeModule *type_module)
{
    /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
     *     function, so we have to wrap it with a public function in
     *     order to register types from a separate compilation unit. */
    e_mail_config_import_page_register_type (type_module);
}

EMailConfigPage *
e_mail_config_import_page_new (void)
{
    return g_object_new (E_TYPE_MAIL_CONFIG_IMPORT_PAGE, NULL);
}

guint
e_mail_config_import_page_get_n_importers (EMailConfigImportPage *page)
{
    g_return_val_if_fail (E_IS_MAIL_CONFIG_IMPORT_PAGE (page), 0);

    return g_slist_length (page->priv->available_importers);
}

void
e_mail_config_import_page_import (EMailConfigImportPage *page,
                                  EActivity *activity,
                                  GAsyncReadyCallback callback,
                                  gpointer user_data)
{
    GSimpleAsyncResult *simple;
    AsyncContext *async_context;
    GCancellable *cancellable;
    EImportImporter *first_importer;
    GSList *list, *link;

    g_return_if_fail (E_IS_MAIL_CONFIG_IMPORT_PAGE (page));
    g_return_if_fail (E_IS_ACTIVITY (activity));

    cancellable = e_activity_get_cancellable (activity);

    async_context = g_slice_new0 (AsyncContext);
    async_context->page = g_object_ref (page);
    async_context->activity = g_object_ref (activity);

    list = page->priv->available_importers;

    for (link = list; link != NULL; link = g_slist_next (link)) {
        EImportImporter *importer = link->data;
        g_queue_push_tail (&async_context->pending_importers, importer);
    }

    if (G_IS_CANCELLABLE (cancellable)) {
        async_context->cancellable = g_object_ref (cancellable);
        async_context->cancel_id = g_cancellable_connect (
            cancellable,
            G_CALLBACK (mail_config_import_page_cancelled),
            async_context, (GDestroyNotify) NULL);
    }

    simple = g_simple_async_result_new (
        G_OBJECT (page), callback, user_data,
        e_mail_config_import_page_import);

    g_simple_async_result_set_op_res_gpointer (
        simple, async_context, (GDestroyNotify) async_context_free);

    /* Start the first importer. */

    first_importer = g_queue_peek_head (&async_context->pending_importers);

    if (first_importer != NULL)
        e_import_import (
            async_context->page->priv->import,
            async_context->page->priv->import_target,
            first_importer,
            (EImportStatusFunc) mail_config_import_page_status,
            (EImportCompleteFunc) mail_config_import_page_complete,
            simple);
    else
        g_simple_async_result_complete_in_idle (simple);
}

gboolean
e_mail_config_import_page_import_finish (EMailConfigImportPage *page,
                                         GAsyncResult *result,
                                         GError **error)
{
    GSimpleAsyncResult *simple;

    g_return_val_if_fail (
        g_simple_async_result_is_valid (
        result, G_OBJECT (page),
        e_mail_config_import_page_import), FALSE);

    simple = G_SIMPLE_ASYNC_RESULT (result);

    /* Assume success unless a GError is set. */
    return !g_simple_async_result_propagate_error (simple, error);
}