Updated Spanish translation
[evolution.git] / libemail-engine / e-mail-store-utils.c
blob2f5b044426dc52d95f3578cbe092b589807135db
1 /*
2 * e-mail-store-utils.c
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include "e-mail-utils.h"
25 #include "e-mail-store-utils.h"
27 #include <glib/gi18n-lib.h>
29 typedef struct _AsyncContext AsyncContext;
31 struct _AsyncContext {
32 gchar *full_name;
35 static void
36 async_context_free (AsyncContext *context)
38 g_free (context->full_name);
40 g_slice_free (AsyncContext, context);
43 static void
44 mail_store_create_folder_thread (GSimpleAsyncResult *simple,
45 GObject *object,
46 GCancellable *cancellable)
48 AsyncContext *context;
49 GError *error = NULL;
51 context = g_simple_async_result_get_op_res_gpointer (simple);
53 e_mail_store_create_folder_sync (
54 CAMEL_STORE (object), context->full_name,
55 cancellable, &error);
57 if (error != NULL)
58 g_simple_async_result_take_error (simple, error);
61 gboolean
62 e_mail_store_create_folder_sync (CamelStore *store,
63 const gchar *full_name,
64 GCancellable *cancellable,
65 GError **error)
67 CamelFolderInfo *folder_info;
68 gchar *copied_full_name;
69 gchar *display_name;
70 const gchar *parent;
71 gboolean success = TRUE;
73 g_return_val_if_fail (CAMEL_IS_STORE (store), FALSE);
74 g_return_val_if_fail (full_name != NULL, FALSE);
76 copied_full_name = g_strdup (full_name);
77 display_name = strrchr (copied_full_name, '/');
78 if (display_name == NULL) {
79 display_name = copied_full_name;
80 parent = "";
81 } else {
82 *display_name++ = '\0';
83 parent = copied_full_name;
86 folder_info = camel_store_create_folder_sync (
87 store, parent, display_name, cancellable, error);
89 g_free (copied_full_name);
91 if (folder_info == NULL)
92 return FALSE;
94 if (CAMEL_IS_SUBSCRIBABLE (store))
95 success = camel_subscribable_subscribe_folder_sync (
96 CAMEL_SUBSCRIBABLE (store),
97 full_name, cancellable, error);
99 camel_store_free_folder_info (store, folder_info);
101 return success;
104 void
105 e_mail_store_create_folder (CamelStore *store,
106 const gchar *full_name,
107 gint io_priority,
108 GCancellable *cancellable,
109 GAsyncReadyCallback callback,
110 gpointer user_data)
112 GSimpleAsyncResult *simple;
113 AsyncContext *context;
115 g_return_if_fail (CAMEL_IS_STORE (store));
116 g_return_if_fail (full_name != NULL);
118 context = g_slice_new0 (AsyncContext);
119 context->full_name = g_strdup (full_name);
121 simple = g_simple_async_result_new (
122 G_OBJECT (store), callback, user_data,
123 e_mail_store_create_folder);
125 g_simple_async_result_set_op_res_gpointer (
126 simple, context, (GDestroyNotify) async_context_free);
128 g_simple_async_result_run_in_thread (
129 simple, mail_store_create_folder_thread,
130 io_priority, cancellable);
132 g_object_unref (simple);
135 gboolean
136 e_mail_store_create_folder_finish (CamelStore *store,
137 GAsyncResult *result,
138 GError **error)
140 GSimpleAsyncResult *simple;
142 g_return_val_if_fail (
143 g_simple_async_result_is_valid (
144 result, G_OBJECT (store),
145 e_mail_store_create_folder), FALSE);
147 simple = G_SIMPLE_ASYNC_RESULT (result);
149 /* Assume success unless a GError is set. */
150 return !g_simple_async_result_propagate_error (simple, error);
153 static void
154 mail_store_go_offline_thread (GSimpleAsyncResult *simple,
155 CamelStore *store,
156 GCancellable *cancellable)
158 CamelService *service;
159 const gchar *display_name;
160 GError *error = NULL;
162 service = CAMEL_SERVICE (store);
164 display_name = camel_service_get_display_name (service);
165 if (display_name == NULL || *display_name == '\0')
166 display_name = G_OBJECT_TYPE_NAME (service);
168 camel_operation_push_message (
169 cancellable, _("Disconnecting from '%s'"), display_name);
171 if (CAMEL_IS_DISCO_STORE (store)) {
172 CamelDiscoStore *disco_store;
174 disco_store = CAMEL_DISCO_STORE (store);
176 if (camel_disco_store_can_work_offline (disco_store))
177 camel_disco_store_set_status (
178 disco_store, CAMEL_DISCO_STORE_OFFLINE,
179 cancellable, &error);
180 else
181 em_utils_disconnect_service_sync (service, TRUE, cancellable, &error);
183 } else if (CAMEL_IS_OFFLINE_STORE (store)) {
184 CamelOfflineStore *offline_store;
186 offline_store = CAMEL_OFFLINE_STORE (store);
188 camel_offline_store_set_online_sync (
189 offline_store, FALSE, cancellable, &error);
191 } else
192 em_utils_disconnect_service_sync (service, TRUE, cancellable, &error);
194 if (error != NULL)
195 g_simple_async_result_take_error (simple, error);
197 camel_operation_pop_message (cancellable);
200 void
201 e_mail_store_go_offline (CamelStore *store,
202 gint io_priority,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
205 gpointer user_data)
207 GSimpleAsyncResult *simple;
209 g_return_if_fail (CAMEL_IS_STORE (store));
211 /* Cancel any pending connect first so the set_offline_op
212 * thread won't get queued behind a hung connect op. */
213 camel_service_cancel_connect (CAMEL_SERVICE (store));
215 simple = g_simple_async_result_new (
216 G_OBJECT (store), callback,
217 user_data, e_mail_store_go_offline);
219 g_simple_async_result_run_in_thread (
220 simple, (GSimpleAsyncThreadFunc)
221 mail_store_go_offline_thread,
222 io_priority, cancellable);
224 g_object_unref (simple);
227 gboolean
228 e_mail_store_go_offline_finish (CamelStore *store,
229 GAsyncResult *result,
230 GError **error)
232 GSimpleAsyncResult *simple;
234 g_return_val_if_fail (
235 g_simple_async_result_is_valid (
236 result, G_OBJECT (store), e_mail_store_go_offline), FALSE);
238 simple = G_SIMPLE_ASYNC_RESULT (result);
240 /* Assume success unless a GError is set. */
241 return !g_simple_async_result_propagate_error (simple, error);
244 static void
245 mail_store_go_online_thread (GSimpleAsyncResult *simple,
246 CamelStore *store,
247 GCancellable *cancellable)
249 CamelService *service;
250 const gchar *display_name;
251 GError *error = NULL;
253 service = CAMEL_SERVICE (store);
255 display_name = camel_service_get_display_name (service);
256 if (display_name == NULL || *display_name == '\0')
257 display_name = G_OBJECT_TYPE_NAME (service);
259 camel_operation_push_message (
260 cancellable, _("Reconnecting to '%s'"), display_name);
262 if (CAMEL_IS_DISCO_STORE (store))
263 camel_disco_store_set_status (
264 CAMEL_DISCO_STORE (store),
265 CAMEL_DISCO_STORE_ONLINE,
266 cancellable, &error);
268 else if (CAMEL_IS_OFFLINE_STORE (store))
269 camel_offline_store_set_online_sync (
270 CAMEL_OFFLINE_STORE (store),
271 TRUE, cancellable, &error);
273 if (error != NULL)
274 g_simple_async_result_take_error (simple, error);
276 camel_operation_pop_message (cancellable);
279 void
280 e_mail_store_go_online (CamelStore *store,
281 gint io_priority,
282 GCancellable *cancellable,
283 GAsyncReadyCallback callback,
284 gpointer user_data)
286 GSimpleAsyncResult *simple;
288 g_return_if_fail (CAMEL_IS_STORE (store));
290 simple = g_simple_async_result_new (
291 G_OBJECT (store), callback,
292 user_data, e_mail_store_go_online);
294 g_simple_async_result_run_in_thread (
295 simple, (GSimpleAsyncThreadFunc)
296 mail_store_go_online_thread,
297 io_priority, cancellable);
299 g_object_unref (simple);
302 gboolean
303 e_mail_store_go_online_finish (CamelStore *store,
304 GAsyncResult *result,
305 GError **error)
307 GSimpleAsyncResult *simple;
309 g_return_val_if_fail (
310 g_simple_async_result_is_valid (
311 result, G_OBJECT (store), e_mail_store_go_online), FALSE);
313 simple = G_SIMPLE_ASYNC_RESULT (result);
315 /* Assume success unless a GError is set. */
316 return !g_simple_async_result_propagate_error (simple, error);
319 static void
320 mail_store_prepare_for_offline_thread (GSimpleAsyncResult *simple,
321 CamelStore *store,
322 GCancellable *cancellable)
324 CamelService *service;
325 const gchar *display_name;
326 GError *error = NULL;
328 service = CAMEL_SERVICE (store);
330 display_name = camel_service_get_display_name (service);
331 if (display_name == NULL || *display_name == '\0')
332 display_name = G_OBJECT_TYPE_NAME (service);
334 camel_operation_push_message (
335 cancellable, _("Preparing account '%s' for offline"),
336 display_name);
338 if (CAMEL_IS_DISCO_STORE (store))
339 camel_disco_store_prepare_for_offline (
340 CAMEL_DISCO_STORE (store), cancellable, &error);
342 else if (CAMEL_IS_OFFLINE_STORE (store))
343 camel_offline_store_prepare_for_offline_sync (
344 CAMEL_OFFLINE_STORE (store), cancellable, &error);
346 if (error != NULL)
347 g_simple_async_result_take_error (simple, error);
349 camel_operation_pop_message (cancellable);
352 void
353 e_mail_store_prepare_for_offline (CamelStore *store,
354 gint io_priority,
355 GCancellable *cancellable,
356 GAsyncReadyCallback callback,
357 gpointer user_data)
359 GSimpleAsyncResult *simple;
361 g_return_if_fail (CAMEL_IS_STORE (store));
363 simple = g_simple_async_result_new (
364 G_OBJECT (store), callback, user_data,
365 e_mail_store_prepare_for_offline);
367 g_simple_async_result_run_in_thread (
368 simple, (GSimpleAsyncThreadFunc)
369 mail_store_prepare_for_offline_thread,
370 io_priority, cancellable);
372 g_object_unref (simple);
375 gboolean
376 e_mail_store_prepare_for_offline_finish (CamelStore *store,
377 GAsyncResult *result,
378 GError **error)
380 GSimpleAsyncResult *simple;
382 g_return_val_if_fail (
383 g_simple_async_result_is_valid (
384 result, G_OBJECT (store),
385 e_mail_store_prepare_for_offline), FALSE);
387 simple = G_SIMPLE_ASYNC_RESULT (result);
389 /* Assume success unless a GError is set. */
390 return !g_simple_async_result_propagate_error (simple, error);