2 * e-mail-config-import-page.c
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "evolution-config.h"
20 #include <glib/gi18n-lib.h>
22 #include "e-mail-config-import-page.h"
24 #define E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE(obj) \
25 (G_TYPE_INSTANCE_GET_PRIVATE \
26 ((obj), E_TYPE_MAIL_CONFIG_IMPORT_PAGE, EMailConfigImportPagePrivate))
28 typedef struct _AsyncContext AsyncContext
;
30 struct _EMailConfigImportPagePrivate
{
32 EImportTarget
*import_target
;
33 GSList
*available_importers
;
36 struct _AsyncContext
{
37 EMailConfigImportPage
*page
;
38 GQueue pending_importers
;
40 GCancellable
*cancellable
;
44 /* Forward Declarations */
45 static void e_mail_config_import_page_interface_init
46 (EMailConfigPageInterface
*iface
);
47 static gboolean
mail_config_import_page_next (gpointer user_data
);
49 G_DEFINE_DYNAMIC_TYPE_EXTENDED (
50 EMailConfigImportPage
,
51 e_mail_config_import_page
,
52 GTK_TYPE_SCROLLED_WINDOW
,
54 G_IMPLEMENT_INTERFACE_DYNAMIC (
55 E_TYPE_MAIL_CONFIG_PAGE
,
56 e_mail_config_import_page_interface_init
))
59 async_context_free (AsyncContext
*async_context
)
61 if (async_context
->page
!= NULL
)
62 g_object_unref (async_context
->page
);
64 if (async_context
->activity
!= NULL
)
65 g_object_unref (async_context
->activity
);
67 if (async_context
->cancellable
!= NULL
) {
68 g_cancellable_disconnect (
69 async_context
->cancellable
,
70 async_context
->cancel_id
);
71 g_object_unref (async_context
->cancellable
);
74 g_queue_clear (&async_context
->pending_importers
);
76 g_slice_free (AsyncContext
, async_context
);
80 mail_config_import_page_status (EImport
*import
,
85 GSimpleAsyncResult
*simple
= user_data
;
86 AsyncContext
*async_context
;
88 async_context
= g_simple_async_result_get_op_res_gpointer (simple
);
90 e_activity_set_text (async_context
->activity
, what
);
91 e_activity_set_percent (async_context
->activity
, (gdouble
) percent
);
95 mail_config_import_page_complete (EImport
*import
,
99 GSimpleAsyncResult
*simple
= user_data
;
102 g_simple_async_result_set_from_error (simple
, error
);
103 g_simple_async_result_complete (simple
);
104 g_object_unref (simple
);
106 /* Schedule the next importer to start. */
107 g_idle_add (mail_config_import_page_next
, simple
);
112 mail_config_import_page_next (gpointer user_data
)
114 GSimpleAsyncResult
*simple
;
115 AsyncContext
*async_context
;
116 GCancellable
*cancellable
;
117 EImportImporter
*next_importer
;
118 GError
*error
= NULL
;
120 simple
= G_SIMPLE_ASYNC_RESULT (user_data
);
121 async_context
= g_simple_async_result_get_op_res_gpointer (simple
);
122 cancellable
= async_context
->cancellable
;
124 /* Pop the completed importer and peek at the next one. */
125 g_queue_pop_head (&async_context
->pending_importers
);
126 next_importer
= g_queue_peek_head (&async_context
->pending_importers
);
128 if (g_cancellable_set_error_if_cancelled (cancellable
, &error
)) {
129 g_simple_async_result_take_error (simple
, error
);
130 g_simple_async_result_complete (simple
);
131 g_object_unref (simple
);
133 } else if (next_importer
!= NULL
) {
135 async_context
->page
->priv
->import
,
136 async_context
->page
->priv
->import_target
,
138 mail_config_import_page_status
,
139 mail_config_import_page_complete
,
143 g_simple_async_result_complete (simple
);
144 g_object_unref (simple
);
151 mail_config_import_page_cancelled (GCancellable
*cancellable
,
152 AsyncContext
*async_context
)
154 GQueue
*pending_importers
;
155 EImportImporter
*current_importer
;
157 pending_importers
= &async_context
->pending_importers
;
158 current_importer
= g_queue_peek_head (pending_importers
);
159 g_return_if_fail (current_importer
!= NULL
);
162 async_context
->page
->priv
->import
,
163 async_context
->page
->priv
->import_target
,
168 mail_config_import_page_dispose (GObject
*object
)
170 EMailConfigImportPagePrivate
*priv
;
172 priv
= E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (object
);
174 if (priv
->import
!= NULL
) {
175 e_import_target_free (
177 priv
->import_target
);
178 g_object_unref (priv
->import
);
179 priv
->import_target
= NULL
;
183 g_slist_free (priv
->available_importers
);
184 priv
->available_importers
= NULL
;
186 /* Chain up to parent's dispose() method. */
187 G_OBJECT_CLASS (e_mail_config_import_page_parent_class
)->
192 mail_config_import_page_constructed (GObject
*object
)
194 EMailConfigImportPage
*page
;
196 GtkWidget
*container
;
202 page
= E_MAIL_CONFIG_IMPORT_PAGE (object
);
204 /* Chain up to parent's constructed() method. */
205 G_OBJECT_CLASS (e_mail_config_import_page_parent_class
)->constructed (object
);
207 main_box
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 24);
209 text
= _("Please select the information "
210 "that you would like to import:");
211 widget
= gtk_label_new (text
);
212 gtk_misc_set_alignment (GTK_MISC (widget
), 0.0, 0.5);
213 gtk_box_pack_start (GTK_BOX (main_box
), widget
, FALSE
, FALSE
, 0);
214 gtk_widget_show (widget
);
216 widget
= gtk_grid_new ();
217 gtk_grid_set_row_spacing (GTK_GRID (widget
), 12);
218 gtk_grid_set_column_spacing (GTK_GRID (widget
), 12);
219 gtk_box_pack_start (GTK_BOX (main_box
), widget
, FALSE
, FALSE
, 0);
220 gtk_widget_show (widget
);
224 list
= page
->priv
->available_importers
;
226 for (link
= list
; link
!= NULL
; link
= link
->next
) {
227 EImportImporter
*importer
= link
->data
;
230 widget
= e_import_get_widget (
232 page
->priv
->import_target
, importer
);
235 gtk_grid_attach (GTK_GRID (container
), widget
, 1, row
, 1, 1);
236 gtk_widget_show (widget
);
238 from_text
= g_strdup_printf (_("From %s:"), importer
->name
);
239 widget
= gtk_label_new (from_text
);
240 gtk_misc_set_alignment (GTK_MISC (widget
), 0.0, 0.0);
241 gtk_grid_attach (GTK_GRID (container
), widget
, 0, row
, 1, 1);
242 gtk_widget_show (widget
);
247 e_mail_config_page_set_content (E_MAIL_CONFIG_PAGE (page
), main_box
);
251 e_mail_config_import_page_class_init (EMailConfigImportPageClass
*class)
253 GObjectClass
*object_class
;
255 g_type_class_add_private (
256 class, sizeof (EMailConfigImportPagePrivate
));
258 object_class
= G_OBJECT_CLASS (class);
259 object_class
->dispose
= mail_config_import_page_dispose
;
260 object_class
->constructed
= mail_config_import_page_constructed
;
264 e_mail_config_import_page_class_finalize (EMailConfigImportPageClass
*class)
269 e_mail_config_import_page_interface_init (EMailConfigPageInterface
*iface
)
271 iface
->title
= _("Importing Files");
272 iface
->sort_order
= E_MAIL_CONFIG_IMPORT_PAGE_SORT_ORDER
;
276 e_mail_config_import_page_init (EMailConfigImportPage
*page
)
278 page
->priv
= E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (page
);
281 e_import_new ("org.gnome.evolution.shell.importer");
282 page
->priv
->import_target
= (EImportTarget
*)
283 e_import_target_new_home (page
->priv
->import
);
284 page
->priv
->available_importers
= e_import_get_importers (
285 page
->priv
->import
, page
->priv
->import_target
);
289 e_mail_config_import_page_type_register (GTypeModule
*type_module
)
291 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
292 * function, so we have to wrap it with a public function in
293 * order to register types from a separate compilation unit. */
294 e_mail_config_import_page_register_type (type_module
);
298 e_mail_config_import_page_new (void)
300 return g_object_new (E_TYPE_MAIL_CONFIG_IMPORT_PAGE
, NULL
);
304 e_mail_config_import_page_get_n_importers (EMailConfigImportPage
*page
)
306 g_return_val_if_fail (E_IS_MAIL_CONFIG_IMPORT_PAGE (page
), 0);
308 return g_slist_length (page
->priv
->available_importers
);
312 e_mail_config_import_page_import (EMailConfigImportPage
*page
,
314 GAsyncReadyCallback callback
,
317 GSimpleAsyncResult
*simple
;
318 AsyncContext
*async_context
;
319 GCancellable
*cancellable
;
320 EImportImporter
*first_importer
;
323 g_return_if_fail (E_IS_MAIL_CONFIG_IMPORT_PAGE (page
));
324 g_return_if_fail (E_IS_ACTIVITY (activity
));
326 cancellable
= e_activity_get_cancellable (activity
);
328 async_context
= g_slice_new0 (AsyncContext
);
329 async_context
->page
= g_object_ref (page
);
330 async_context
->activity
= g_object_ref (activity
);
332 list
= page
->priv
->available_importers
;
334 for (link
= list
; link
!= NULL
; link
= g_slist_next (link
)) {
335 EImportImporter
*importer
= link
->data
;
336 g_queue_push_tail (&async_context
->pending_importers
, importer
);
339 if (G_IS_CANCELLABLE (cancellable
)) {
340 async_context
->cancellable
= g_object_ref (cancellable
);
341 async_context
->cancel_id
= g_cancellable_connect (
343 G_CALLBACK (mail_config_import_page_cancelled
),
344 async_context
, (GDestroyNotify
) NULL
);
347 simple
= g_simple_async_result_new (
348 G_OBJECT (page
), callback
, user_data
,
349 e_mail_config_import_page_import
);
351 g_simple_async_result_set_op_res_gpointer (
352 simple
, async_context
, (GDestroyNotify
) async_context_free
);
354 /* Start the first importer. */
356 first_importer
= g_queue_peek_head (&async_context
->pending_importers
);
358 if (first_importer
!= NULL
)
360 async_context
->page
->priv
->import
,
361 async_context
->page
->priv
->import_target
,
363 mail_config_import_page_status
,
364 mail_config_import_page_complete
,
367 g_simple_async_result_complete_in_idle (simple
);
371 e_mail_config_import_page_import_finish (EMailConfigImportPage
*page
,
372 GAsyncResult
*result
,
375 GSimpleAsyncResult
*simple
;
377 g_return_val_if_fail (
378 g_simple_async_result_is_valid (
379 result
, G_OBJECT (page
),
380 e_mail_config_import_page_import
), FALSE
);
382 simple
= G_SIMPLE_ASYNC_RESULT (result
);
384 /* Assume success unless a GError is set. */
385 return !g_simple_async_result_propagate_error (simple
, error
);