1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-disco-folder.c: abstract class for a disconnectable folder */
5 * Authors: Dan Winship <danw@ximian.com>
7 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU Lesser General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
28 #include <glib/gi18n-lib.h>
30 #include "camel-debug.h"
31 #include "camel-disco-folder.h"
32 #include "camel-disco-store.h"
33 #include "camel-offline-settings.h"
34 #include "camel-session.h"
36 #define CAMEL_DISCO_FOLDER_GET_PRIVATE(obj) \
37 (G_TYPE_INSTANCE_GET_PRIVATE \
38 ((obj), CAMEL_TYPE_DISCO_FOLDER, CamelDiscoFolderPrivate))
40 struct _CamelDiscoFolderPrivate {
41 gboolean offline_sync;
44 struct _cdf_sync_data {
46 CamelFolderChangeInfo *changes;
49 /* The custom property ID is a CamelArg artifact.
50 * It still identifies the property in state files. */
53 PROP_OFFLINE_SYNC = 0x2400
56 G_DEFINE_TYPE (CamelDiscoFolder, camel_disco_folder, CAMEL_TYPE_FOLDER)
59 cdf_sync_free (struct _cdf_sync_data *data)
61 if (data->changes != NULL)
62 camel_folder_change_info_free (data->changes);
63 g_object_unref (data->folder);
65 g_slice_free (struct _cdf_sync_data, data);
69 cdf_sync_offline (CamelSession *session,
70 GCancellable *cancellable,
71 struct _cdf_sync_data *data,
74 camel_operation_push_message (
76 _("Downloading new messages for offline mode"));
78 if (data->changes != NULL) {
82 uid_added = data->changes->uid_added;
84 for (ii = 0; ii < uid_added->len; ii++) {
85 gint pc = ii * 100 / uid_added->len;
87 camel_operation_progress (cancellable, pc);
88 camel_disco_folder_cache_message (
89 CAMEL_DISCO_FOLDER (data->folder),
90 uid_added->pdata[ii], NULL, error);
93 camel_disco_folder_prepare_for_offline (
94 CAMEL_DISCO_FOLDER (data->folder),
95 "(match-all)", NULL, error);
98 camel_operation_pop_message (cancellable);
102 cdf_folder_changed (CamelFolder *folder,
103 CamelFolderChangeInfo *changes)
105 CamelService *service;
106 CamelSession *session;
107 CamelSettings *settings;
108 CamelStore *parent_store;
109 gboolean sync_folder;
112 parent_store = camel_folder_get_parent_store (folder);
114 service = CAMEL_SERVICE (parent_store);
115 session = camel_service_get_session (service);
117 sync_folder = camel_disco_folder_get_offline_sync (
118 CAMEL_DISCO_FOLDER (folder));
120 settings = camel_service_ref_settings (service);
122 sync_store = camel_offline_settings_get_stay_synchronized (
123 CAMEL_OFFLINE_SETTINGS (settings));
125 g_object_unref (settings);
127 if (changes->uid_added->len > 0 && (sync_folder || sync_store)) {
128 struct _cdf_sync_data *data;
130 data = g_slice_new0 (struct _cdf_sync_data);
131 data->changes = camel_folder_change_info_new ();
132 camel_folder_change_info_cat (data->changes, changes);
133 data->folder = g_object_ref (folder);
135 camel_session_submit_job (
137 (CamelSessionCallback) cdf_sync_offline,
138 data, (GDestroyNotify) cdf_sync_free);
143 disco_folder_set_property (GObject *object,
148 switch (property_id) {
149 case PROP_OFFLINE_SYNC:
150 camel_disco_folder_set_offline_sync (
151 CAMEL_DISCO_FOLDER (object),
152 g_value_get_boolean (value));
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
160 disco_folder_get_property (GObject *object,
165 switch (property_id) {
166 case PROP_OFFLINE_SYNC:
167 g_value_set_boolean (
168 value, camel_disco_folder_get_offline_sync (
169 CAMEL_DISCO_FOLDER (object)));
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
177 disco_expunge_uids (CamelFolder *folder,
179 GCancellable *cancellable,
182 CamelDiscoFolderClass *disco_folder_class;
183 CamelStore *parent_store;
189 parent_store = camel_folder_get_parent_store (folder);
190 disco_folder_class = CAMEL_DISCO_FOLDER_GET_CLASS (folder);
192 switch (camel_disco_store_status (CAMEL_DISCO_STORE (parent_store))) {
193 case CAMEL_DISCO_STORE_ONLINE:
194 g_return_val_if_fail (disco_folder_class->expunge_uids_online != NULL, FALSE);
195 success = disco_folder_class->expunge_uids_online (
196 folder, uids, error);
198 folder, expunge_uids_online, success, error);
201 case CAMEL_DISCO_STORE_OFFLINE:
202 g_return_val_if_fail (disco_folder_class->expunge_uids_offline != NULL, FALSE);
203 success = disco_folder_class->expunge_uids_offline (
204 folder, uids, error);
206 folder, expunge_uids_offline, success, error);
209 case CAMEL_DISCO_STORE_RESYNCING:
210 g_return_val_if_fail (disco_folder_class->expunge_uids_resyncing != NULL, FALSE);
211 success = disco_folder_class->expunge_uids_resyncing (
212 folder, uids, error);
214 folder, expunge_uids_resyncing, success, error);
218 g_return_val_if_reached (FALSE);
222 disco_append_message_sync (CamelFolder *folder,
223 CamelMimeMessage *message,
224 CamelMessageInfo *info,
225 gchar **appended_uid,
226 GCancellable *cancellable,
229 CamelDiscoFolderClass *disco_folder_class;
230 CamelStore *parent_store;
233 parent_store = camel_folder_get_parent_store (folder);
234 disco_folder_class = CAMEL_DISCO_FOLDER_GET_CLASS (folder);
236 switch (camel_disco_store_status (CAMEL_DISCO_STORE (parent_store))) {
237 case CAMEL_DISCO_STORE_ONLINE:
238 g_return_val_if_fail (disco_folder_class->append_online != NULL, FALSE);
239 success = disco_folder_class->append_online (
240 folder, message, info,
241 appended_uid, cancellable, error);
242 CAMEL_CHECK_GERROR (folder, append_online, success, error);
245 case CAMEL_DISCO_STORE_OFFLINE:
246 g_return_val_if_fail (disco_folder_class->append_offline != NULL, FALSE);
247 success = disco_folder_class->append_offline (
248 folder, message, info,
249 appended_uid, cancellable, error);
250 CAMEL_CHECK_GERROR (folder, append_offline, success, error);
253 case CAMEL_DISCO_STORE_RESYNCING:
254 g_return_val_if_fail (disco_folder_class->append_resyncing != NULL, FALSE);
255 success = disco_folder_class->append_resyncing (
256 folder, message, info,
257 appended_uid, cancellable, error);
258 CAMEL_CHECK_GERROR (folder, append_resyncing, success, error);
262 g_return_val_if_reached (FALSE);
266 disco_expunge_sync (CamelFolder *folder,
267 GCancellable *cancellable,
272 CamelMessageInfo *info;
274 GPtrArray *known_uids;
276 uids = g_ptr_array_new ();
277 camel_folder_summary_prepare_fetch_all (folder->summary, NULL);
278 known_uids = camel_folder_summary_get_array (folder->summary);
279 for (i = 0; known_uids && i < known_uids->len; i++) {
280 const gchar *uid = g_ptr_array_index (known_uids, i);
282 info = camel_folder_summary_get (folder->summary, uid);
283 if (camel_message_info_flags (info) & CAMEL_MESSAGE_DELETED)
284 g_ptr_array_add (uids, (gpointer) uid);
285 camel_message_info_free (info);
288 success = disco_expunge_uids (folder, uids, cancellable, error);
290 g_ptr_array_free (uids, TRUE);
292 camel_folder_summary_free_array (known_uids);
298 disco_refresh_info_sync (CamelFolder *folder,
299 GCancellable *cancellable,
302 CamelDiscoFolderClass *disco_folder_class;
303 CamelStore *parent_store;
306 parent_store = camel_folder_get_parent_store (folder);
308 if (camel_disco_store_status (CAMEL_DISCO_STORE (parent_store)) != CAMEL_DISCO_STORE_ONLINE)
311 disco_folder_class = CAMEL_DISCO_FOLDER_GET_CLASS (folder);
312 g_return_val_if_fail (disco_folder_class->refresh_info_online != NULL, FALSE);
314 success = disco_folder_class->refresh_info_online (
315 folder, cancellable, error);
316 CAMEL_CHECK_GERROR (folder, refresh_info_online, success, error);
322 disco_synchronize_sync (CamelFolder *folder,
324 GCancellable *cancellable,
327 CamelDiscoFolderClass *disco_folder_class;
328 CamelStore *parent_store;
331 if (expunge && !disco_expunge_sync (folder, cancellable, error))
334 camel_object_state_write (CAMEL_OBJECT (folder));
336 parent_store = camel_folder_get_parent_store (folder);
337 disco_folder_class = CAMEL_DISCO_FOLDER_GET_CLASS (folder);
339 switch (camel_disco_store_status (CAMEL_DISCO_STORE (parent_store))) {
340 case CAMEL_DISCO_STORE_ONLINE:
341 g_return_val_if_fail (disco_folder_class->sync_online != NULL, FALSE);
342 success = disco_folder_class->sync_online (folder, error);
343 CAMEL_CHECK_GERROR (folder, sync_online, success, error);
346 case CAMEL_DISCO_STORE_OFFLINE:
347 g_return_val_if_fail (disco_folder_class->sync_offline != NULL, FALSE);
348 success = disco_folder_class->sync_offline (folder, error);
349 CAMEL_CHECK_GERROR (folder, sync_offline, success, error);
352 case CAMEL_DISCO_STORE_RESYNCING:
353 g_return_val_if_fail (disco_folder_class->sync_resyncing != NULL, FALSE);
354 success = disco_folder_class->sync_resyncing (folder, error);
355 CAMEL_CHECK_GERROR (folder, sync_resyncing, success, error);
359 g_return_val_if_reached (FALSE);
363 disco_transfer_messages_to_sync (CamelFolder *source,
366 gboolean delete_originals,
367 GPtrArray **transferred_uids,
368 GCancellable *cancellable,
371 CamelDiscoFolderClass *disco_folder_class;
372 CamelStore *parent_store;
375 parent_store = camel_folder_get_parent_store (source);
376 disco_folder_class = CAMEL_DISCO_FOLDER_GET_CLASS (source);
378 switch (camel_disco_store_status (CAMEL_DISCO_STORE (parent_store))) {
379 case CAMEL_DISCO_STORE_ONLINE:
380 g_return_val_if_fail (disco_folder_class->transfer_online != NULL, FALSE);
381 success = disco_folder_class->transfer_online (
382 source, uids, dest, transferred_uids,
383 delete_originals, cancellable, error);
384 CAMEL_CHECK_GERROR (source, transfer_online, success, error);
387 case CAMEL_DISCO_STORE_OFFLINE:
388 g_return_val_if_fail (disco_folder_class->transfer_offline != NULL, FALSE);
389 success = disco_folder_class->transfer_offline (
390 source, uids, dest, transferred_uids,
391 delete_originals, cancellable, error);
392 CAMEL_CHECK_GERROR (source, transfer_offline, success, error);
395 case CAMEL_DISCO_STORE_RESYNCING:
396 g_return_val_if_fail (disco_folder_class->transfer_resyncing != NULL, FALSE);
397 success = disco_folder_class->transfer_resyncing (
398 source, uids, dest, transferred_uids,
399 delete_originals, cancellable, error);
400 CAMEL_CHECK_GERROR (source, transfer_resyncing, success, error);
404 g_return_val_if_reached (FALSE);
408 disco_prepare_for_offline (CamelDiscoFolder *disco_folder,
409 const gchar *expression,
410 GCancellable *cancellable,
413 CamelFolder *folder = CAMEL_FOLDER (disco_folder);
415 const gchar *display_name;
416 const gchar *message;
418 gboolean success = TRUE;
420 message = _("Preparing folder '%s' for offline");
421 display_name = camel_folder_get_display_name (folder);
422 camel_operation_push_message (cancellable, message, display_name);
425 uids = camel_folder_search_by_expression (folder, expression, cancellable, error);
427 uids = camel_folder_get_uids (folder);
430 camel_operation_pop_message (cancellable);
434 for (i = 0; i < uids->len && success; i++) {
435 camel_operation_progress (
436 cancellable, (i * 100) / uids->len);
437 success = camel_disco_folder_cache_message (
438 disco_folder, uids->pdata[i], cancellable, error);
442 camel_folder_search_free (folder, uids);
444 camel_folder_free_uids (folder, uids);
446 camel_operation_pop_message (cancellable);
452 disco_refresh_info_online (CamelFolder *folder,
453 GCancellable *cancellable,
460 camel_disco_folder_class_init (CamelDiscoFolderClass *class)
462 GObjectClass *object_class;
463 CamelFolderClass *folder_class;
465 g_type_class_add_private (class, sizeof (CamelDiscoFolderPrivate));
467 object_class = G_OBJECT_CLASS (class);
468 object_class->set_property = disco_folder_set_property;
469 object_class->get_property = disco_folder_get_property;
471 folder_class = CAMEL_FOLDER_CLASS (class);
472 folder_class->append_message_sync = disco_append_message_sync;
473 folder_class->expunge_sync = disco_expunge_sync;
474 folder_class->refresh_info_sync = disco_refresh_info_sync;
475 folder_class->synchronize_sync = disco_synchronize_sync;
476 folder_class->transfer_messages_to_sync = disco_transfer_messages_to_sync;
478 class->prepare_for_offline = disco_prepare_for_offline;
479 class->refresh_info_online = disco_refresh_info_online;
481 g_object_class_install_property (
484 g_param_spec_boolean (
487 _("Copy folder content locally for _offline operation"),
490 CAMEL_PARAM_PERSISTENT));
494 camel_disco_folder_init (CamelDiscoFolder *disco_folder)
496 disco_folder->priv = CAMEL_DISCO_FOLDER_GET_PRIVATE (disco_folder);
499 disco_folder, "changed",
500 G_CALLBACK (cdf_folder_changed), NULL);
504 * camel_disco_folder_get_offline_sync:
505 * @disco_folder: a #CamelDiscoFolder
510 camel_disco_folder_get_offline_sync (CamelDiscoFolder *disco_folder)
512 g_return_val_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder), FALSE);
514 return disco_folder->priv->offline_sync;
518 * camel_disco_folder_set_offline_sync:
519 * @disco_folder: a #CamelDiscoFolder
520 * @offline_sync: whether to synchronize for offline use
525 camel_disco_folder_set_offline_sync (CamelDiscoFolder *disco_folder,
526 gboolean offline_sync)
528 g_return_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder));
530 if (disco_folder->priv->offline_sync == offline_sync)
533 disco_folder->priv->offline_sync = offline_sync;
535 g_object_notify (G_OBJECT (disco_folder), "offline-sync");
539 * camel_disco_folder_expunge_uids:
540 * @folder: a (disconnectable) folder
541 * @uids: array of UIDs to expunge
542 * @cancellable: optional #GCancellable object, or %NULL
543 * @error: return location for a #GError, or %NULL
545 * This expunges the messages in @uids from @folder. It should take
546 * whatever steps are needed to avoid expunging any other messages,
547 * although in some cases it may not be possible to avoid expunging
548 * messages that are marked deleted by another client at the same time
549 * as the expunge_uids call is running.
551 * Returns: %TRUE on success, %FALSE on failure
554 camel_disco_folder_expunge_uids (CamelFolder *folder,
556 GCancellable *cancellable,
559 g_return_val_if_fail (CAMEL_IS_DISCO_FOLDER (folder), FALSE);
560 g_return_val_if_fail (uids != NULL, FALSE);
562 return disco_expunge_uids (folder, uids, cancellable, error);
566 * camel_disco_folder_cache_message:
567 * @disco_folder: the folder
568 * @uid: the UID of the message to cache
569 * @cancellable: optional #GCancellable object, or %NULL
570 * @error: return location for a #GError, or %NULL
572 * Requests that @disco_folder cache message @uid to disk.
574 * Returns: %TRUE on success, %FALSE on failure
577 camel_disco_folder_cache_message (CamelDiscoFolder *disco_folder,
579 GCancellable *cancellable,
582 CamelDiscoFolderClass *class;
585 g_return_val_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder), FALSE);
586 g_return_val_if_fail (uid != NULL, FALSE);
588 class = CAMEL_DISCO_FOLDER_GET_CLASS (disco_folder);
589 g_return_val_if_fail (class->cache_message != NULL, FALSE);
591 success = class->cache_message (
592 disco_folder, uid, cancellable, error);
593 CAMEL_CHECK_GERROR (disco_folder, cache_message, success, error);
599 * camel_disco_folder_prepare_for_offline:
600 * @disco_folder: the folder
601 * @expression: an expression describing messages to synchronize, or %NULL
602 * if all messages should be sync'ed.
603 * @cancellable: optional #GCancellable object, or %NULL
604 * @error: return location for a #GError, or %NULL
606 * This prepares @disco_folder for offline operation, by downloading
607 * the bodies of all messages described by @expression (using the
608 * same syntax as camel_folder_search_by_expression() ).
610 * Returns: %TRUE on success, %FALSE on failure
613 camel_disco_folder_prepare_for_offline (CamelDiscoFolder *disco_folder,
614 const gchar *expression,
615 GCancellable *cancellable,
618 CamelDiscoFolderClass *class;
621 g_return_val_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder), FALSE);
623 class = CAMEL_DISCO_FOLDER_GET_CLASS (disco_folder);
624 g_return_val_if_fail (class->prepare_for_offline != NULL, FALSE);
626 success = class->prepare_for_offline (
627 disco_folder, expression, cancellable, error);
628 CAMEL_CHECK_GERROR (disco_folder, prepare_for_offline, success, error);