Bug 783987 - Mail account assistant/editor too tall for small resolution
[evolution.git] / src / modules / startup-wizard / e-mail-config-import-page.c
blobef5a5e126f14c4bf730924e045164efce68dac02
1 /*
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
11 * for more details.
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 {
31 EImport *import;
32 EImportTarget *import_target;
33 GSList *available_importers;
36 struct _AsyncContext {
37 EMailConfigImportPage *page;
38 GQueue pending_importers;
39 EActivity *activity;
40 GCancellable *cancellable;
41 gulong cancel_id;
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))
58 static void
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);
79 static void
80 mail_config_import_page_status (EImport *import,
81 const gchar *what,
82 gint percent,
83 gpointer user_data)
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);
94 static void
95 mail_config_import_page_complete (EImport *import,
96 const GError *error,
97 gpointer user_data)
99 GSimpleAsyncResult *simple = user_data;
101 if (error) {
102 g_simple_async_result_set_from_error (simple, error);
103 g_simple_async_result_complete (simple);
104 g_object_unref (simple);
105 } else {
106 /* Schedule the next importer to start. */
107 g_idle_add (mail_config_import_page_next, simple);
111 static gboolean
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) {
134 e_import_import (
135 async_context->page->priv->import,
136 async_context->page->priv->import_target,
137 next_importer,
138 mail_config_import_page_status,
139 mail_config_import_page_complete,
140 simple);
142 } else {
143 g_simple_async_result_complete (simple);
144 g_object_unref (simple);
147 return FALSE;
150 static void
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);
161 e_import_cancel (
162 async_context->page->priv->import,
163 async_context->page->priv->import_target,
164 current_importer);
167 static void
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 (
176 priv->import,
177 priv->import_target);
178 g_object_unref (priv->import);
179 priv->import_target = NULL;
180 priv->import = 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)->
188 dispose (object);
191 static void
192 mail_config_import_page_constructed (GObject *object)
194 EMailConfigImportPage *page;
195 GtkWidget *widget;
196 GtkWidget *container;
197 GtkWidget *main_box;
198 GSList *list, *link;
199 const gchar *text;
200 gint row = 0;
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);
222 container = widget;
224 list = page->priv->available_importers;
226 for (link = list; link != NULL; link = link->next) {
227 EImportImporter *importer = link->data;
228 gchar *from_text;
230 widget = e_import_get_widget (
231 page->priv->import,
232 page->priv->import_target, importer);
233 if (widget == NULL)
234 continue;
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);
244 row++;
247 e_mail_config_page_set_content (E_MAIL_CONFIG_PAGE (page), main_box);
250 static void
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;
263 static void
264 e_mail_config_import_page_class_finalize (EMailConfigImportPageClass *class)
268 static void
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;
275 static void
276 e_mail_config_import_page_init (EMailConfigImportPage *page)
278 page->priv = E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (page);
280 page->priv->import =
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);
288 void
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);
297 EMailConfigPage *
298 e_mail_config_import_page_new (void)
300 return g_object_new (E_TYPE_MAIL_CONFIG_IMPORT_PAGE, NULL);
303 guint
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);
311 void
312 e_mail_config_import_page_import (EMailConfigImportPage *page,
313 EActivity *activity,
314 GAsyncReadyCallback callback,
315 gpointer user_data)
317 GSimpleAsyncResult *simple;
318 AsyncContext *async_context;
319 GCancellable *cancellable;
320 EImportImporter *first_importer;
321 GSList *list, *link;
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 (
342 cancellable,
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)
359 e_import_import (
360 async_context->page->priv->import,
361 async_context->page->priv->import_target,
362 first_importer,
363 mail_config_import_page_status,
364 mail_config_import_page_complete,
365 simple);
366 else
367 g_simple_async_result_complete_in_idle (simple);
370 gboolean
371 e_mail_config_import_page_import_finish (EMailConfigImportPage *page,
372 GAsyncResult *result,
373 GError **error)
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);