1 /*
2  * e-mail-config-import-page.c
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17 
18 #include "evolution-config.h"
19 
20 #include <glib/gi18n-lib.h>
21 
22 #include "e-mail-config-import-page.h"
23 
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))
27 
28 typedef struct _AsyncContext AsyncContext;
29 
30 struct _EMailConfigImportPagePrivate {
31 	EImport *import;
32 	EImportTarget *import_target;
33 	GSList *available_importers;
34 };
35 
36 struct _AsyncContext {
37 	EMailConfigImportPage *page;
38 	GQueue pending_importers;
39 	EActivity *activity;
40 	GCancellable *cancellable;
41 	gulong cancel_id;
42 };
43 
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);
48 
49 G_DEFINE_DYNAMIC_TYPE_EXTENDED (
50 	EMailConfigImportPage,
51 	e_mail_config_import_page,
52 	GTK_TYPE_SCROLLED_WINDOW,
53 	0,
54 	G_IMPLEMENT_INTERFACE_DYNAMIC (
55 		E_TYPE_MAIL_CONFIG_PAGE,
56 		e_mail_config_import_page_interface_init))
57 
58 static void
async_context_free(AsyncContext * async_context)59 async_context_free (AsyncContext *async_context)
60 {
61 	if (async_context->page != NULL)
62 		g_object_unref (async_context->page);
63 
64 	if (async_context->activity != NULL)
65 		g_object_unref (async_context->activity);
66 
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);
72 	}
73 
74 	g_queue_clear (&async_context->pending_importers);
75 
76 	g_slice_free (AsyncContext, async_context);
77 }
78 
79 static void
mail_config_import_page_status(EImport * import,const gchar * what,gint percent,gpointer user_data)80 mail_config_import_page_status (EImport *import,
81 				const gchar *what,
82 				gint percent,
83 				gpointer user_data)
84 {
85 	GSimpleAsyncResult *simple = user_data;
86 	AsyncContext *async_context;
87 
88 	async_context = g_simple_async_result_get_op_res_gpointer (simple);
89 
90 	e_activity_set_text (async_context->activity, what);
91 	e_activity_set_percent (async_context->activity, (gdouble) percent);
92 }
93 
94 static void
mail_config_import_page_complete(EImport * import,const GError * error,gpointer user_data)95 mail_config_import_page_complete (EImport *import,
96 				  const GError *error,
97                                   gpointer user_data)
98 {
99 	GSimpleAsyncResult *simple = user_data;
100 
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);
108 	}
109 }
110 
111 static gboolean
mail_config_import_page_next(gpointer user_data)112 mail_config_import_page_next (gpointer user_data)
113 {
114 	GSimpleAsyncResult *simple;
115 	AsyncContext *async_context;
116 	GCancellable *cancellable;
117 	EImportImporter *next_importer;
118 	GError *error = NULL;
119 
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;
123 
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);
127 
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);
132 
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);
141 
142 	} else {
143 		g_simple_async_result_complete (simple);
144 		g_object_unref (simple);
145 	}
146 
147 	return FALSE;
148 }
149 
150 static void
mail_config_import_page_cancelled(GCancellable * cancellable,AsyncContext * async_context)151 mail_config_import_page_cancelled (GCancellable *cancellable,
152                                    AsyncContext *async_context)
153 {
154 	GQueue *pending_importers;
155 	EImportImporter *current_importer;
156 
157 	pending_importers = &async_context->pending_importers;
158 	current_importer = g_queue_peek_head (pending_importers);
159 	g_return_if_fail (current_importer != NULL);
160 
161 	e_import_cancel (
162 		async_context->page->priv->import,
163 		async_context->page->priv->import_target,
164 		current_importer);
165 }
166 
167 static void
mail_config_import_page_dispose(GObject * object)168 mail_config_import_page_dispose (GObject *object)
169 {
170 	EMailConfigImportPagePrivate *priv;
171 
172 	priv = E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (object);
173 
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;
181 	}
182 
183 	g_slist_free (priv->available_importers);
184 	priv->available_importers = NULL;
185 
186 	/* Chain up to parent's dispose() method. */
187 	G_OBJECT_CLASS (e_mail_config_import_page_parent_class)->
188 		dispose (object);
189 }
190 
191 static void
mail_config_import_page_constructed(GObject * object)192 mail_config_import_page_constructed (GObject *object)
193 {
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;
201 
202 	page = E_MAIL_CONFIG_IMPORT_PAGE (object);
203 
204 	/* Chain up to parent's constructed() method. */
205 	G_OBJECT_CLASS (e_mail_config_import_page_parent_class)->constructed (object);
206 
207 	main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 24);
208 
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);
215 
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);
221 
222 	container = widget;
223 
224 	list = page->priv->available_importers;
225 
226 	for (link = list; link != NULL; link = link->next) {
227 		EImportImporter *importer = link->data;
228 		gchar *from_text;
229 
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);
237 
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);
243 
244 		row++;
245 	}
246 
247 	e_mail_config_page_set_content (E_MAIL_CONFIG_PAGE (page), main_box);
248 }
249 
250 static void
e_mail_config_import_page_class_init(EMailConfigImportPageClass * class)251 e_mail_config_import_page_class_init (EMailConfigImportPageClass *class)
252 {
253 	GObjectClass *object_class;
254 
255 	g_type_class_add_private (
256 		class, sizeof (EMailConfigImportPagePrivate));
257 
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;
261 }
262 
263 static void
e_mail_config_import_page_class_finalize(EMailConfigImportPageClass * class)264 e_mail_config_import_page_class_finalize (EMailConfigImportPageClass *class)
265 {
266 }
267 
268 static void
e_mail_config_import_page_interface_init(EMailConfigPageInterface * iface)269 e_mail_config_import_page_interface_init (EMailConfigPageInterface *iface)
270 {
271 	iface->title = _("Importing Files");
272 	iface->sort_order = E_MAIL_CONFIG_IMPORT_PAGE_SORT_ORDER;
273 }
274 
275 static void
e_mail_config_import_page_init(EMailConfigImportPage * page)276 e_mail_config_import_page_init (EMailConfigImportPage *page)
277 {
278 	page->priv = E_MAIL_CONFIG_IMPORT_PAGE_GET_PRIVATE (page);
279 
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);
286 }
287 
288 void
e_mail_config_import_page_type_register(GTypeModule * type_module)289 e_mail_config_import_page_type_register (GTypeModule *type_module)
290 {
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);
295 }
296 
297 EMailConfigPage *
e_mail_config_import_page_new(void)298 e_mail_config_import_page_new (void)
299 {
300 	return g_object_new (E_TYPE_MAIL_CONFIG_IMPORT_PAGE, NULL);
301 }
302 
303 guint
e_mail_config_import_page_get_n_importers(EMailConfigImportPage * page)304 e_mail_config_import_page_get_n_importers (EMailConfigImportPage *page)
305 {
306 	g_return_val_if_fail (E_IS_MAIL_CONFIG_IMPORT_PAGE (page), 0);
307 
308 	return g_slist_length (page->priv->available_importers);
309 }
310 
311 void
e_mail_config_import_page_import(EMailConfigImportPage * page,EActivity * activity,GAsyncReadyCallback callback,gpointer user_data)312 e_mail_config_import_page_import (EMailConfigImportPage *page,
313                                   EActivity *activity,
314                                   GAsyncReadyCallback callback,
315                                   gpointer user_data)
316 {
317 	GSimpleAsyncResult *simple;
318 	AsyncContext *async_context;
319 	GCancellable *cancellable;
320 	EImportImporter *first_importer;
321 	GSList *list, *link;
322 
323 	g_return_if_fail (E_IS_MAIL_CONFIG_IMPORT_PAGE (page));
324 	g_return_if_fail (E_IS_ACTIVITY (activity));
325 
326 	cancellable = e_activity_get_cancellable (activity);
327 
328 	async_context = g_slice_new0 (AsyncContext);
329 	async_context->page = g_object_ref (page);
330 	async_context->activity = g_object_ref (activity);
331 
332 	list = page->priv->available_importers;
333 
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);
337 	}
338 
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);
345 	}
346 
347 	simple = g_simple_async_result_new (
348 		G_OBJECT (page), callback, user_data,
349 		e_mail_config_import_page_import);
350 
351 	g_simple_async_result_set_op_res_gpointer (
352 		simple, async_context, (GDestroyNotify) async_context_free);
353 
354 	/* Start the first importer. */
355 
356 	first_importer = g_queue_peek_head (&async_context->pending_importers);
357 
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);
368 }
369 
370 gboolean
e_mail_config_import_page_import_finish(EMailConfigImportPage * page,GAsyncResult * result,GError ** error)371 e_mail_config_import_page_import_finish (EMailConfigImportPage *page,
372                                          GAsyncResult *result,
373                                          GError **error)
374 {
375 	GSimpleAsyncResult *simple;
376 
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);
381 
382 	simple = G_SIMPLE_ASYNC_RESULT (result);
383 
384 	/* Assume success unless a GError is set. */
385 	return !g_simple_async_result_propagate_error (simple, error);
386 }
387 
388