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) 2001 Ximian, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include "camel-disco-folder.h"
30 #include "camel-disco-store.h"
31 #include "camel-exception.h"
33 #define CF_CLASS(o) (CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS (o)))
34 #define CDF_CLASS(o) (CAMEL_DISCO_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS (o)))
36 static CamelFolderClass *parent_class = NULL;
38 static void disco_refresh_info (CamelFolder *folder, CamelException *ex);
39 static void disco_sync (CamelFolder *folder, gboolean expunge, CamelException *ex);
40 static void disco_expunge (CamelFolder *folder, CamelException *ex);
42 static void disco_append_message (CamelFolder *folder, CamelMimeMessage *message,
43 const CamelMessageInfo *info, CamelException *ex);
44 static void disco_copy_messages_to (CamelFolder *source, GPtrArray *uids,
45 CamelFolder *destination, CamelException *ex);
46 static void disco_move_messages_to (CamelFolder *source, GPtrArray *uids,
47 CamelFolder *destination, CamelException *ex);
49 static void disco_cache_message (CamelDiscoFolder *disco_folder,
50 const char *uid, CamelException *ex);
51 static void disco_prepare_for_offline (CamelDiscoFolder *disco_folder,
52 const char *expression,
56 camel_disco_folder_class_init (CamelDiscoFolderClass *camel_disco_folder_class)
58 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_disco_folder_class);
60 parent_class = CAMEL_FOLDER_CLASS (camel_type_get_global_classfuncs (camel_folder_get_type ()));
62 /* virtual method definition */
63 camel_disco_folder_class->cache_message = disco_cache_message;
64 camel_disco_folder_class->prepare_for_offline = disco_prepare_for_offline;
66 /* virtual method overload */
67 camel_folder_class->refresh_info = disco_refresh_info;
68 camel_folder_class->sync = disco_sync;
69 camel_folder_class->expunge = disco_expunge;
71 camel_folder_class->append_message = disco_append_message;
72 camel_folder_class->copy_messages_to = disco_copy_messages_to;
73 camel_folder_class->move_messages_to = disco_move_messages_to;
77 camel_disco_folder_get_type (void)
79 static CamelType camel_disco_folder_type = CAMEL_INVALID_TYPE;
81 if (camel_disco_folder_type == CAMEL_INVALID_TYPE) {
82 camel_disco_folder_type = camel_type_register (
83 CAMEL_FOLDER_TYPE, "CamelDiscoFolder",
84 sizeof (CamelDiscoFolder),
85 sizeof (CamelDiscoFolderClass),
86 (CamelObjectClassInitFunc) camel_disco_folder_class_init,
90 return camel_disco_folder_type;
95 disco_refresh_info (CamelFolder *folder, CamelException *ex)
97 if (camel_disco_store_status (CAMEL_DISCO_STORE (folder->parent_store)) != CAMEL_DISCO_STORE_ONLINE)
99 CDF_CLASS (folder)->refresh_info_online (folder, ex);
103 disco_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
106 disco_expunge (folder, ex);
107 if (camel_exception_is_set (ex))
111 switch (camel_disco_store_status (CAMEL_DISCO_STORE (folder->parent_store))) {
112 case CAMEL_DISCO_STORE_ONLINE:
113 CDF_CLASS (folder)->sync_online (folder, ex);
116 case CAMEL_DISCO_STORE_OFFLINE:
117 CDF_CLASS (folder)->sync_offline (folder, ex);
120 case CAMEL_DISCO_STORE_RESYNCING:
121 CDF_CLASS (folder)->sync_resyncing (folder, ex);
127 disco_expunge_uids (CamelFolder *folder, GPtrArray *uids, CamelException *ex)
129 CamelDiscoStore *disco = CAMEL_DISCO_STORE (folder->parent_store);
134 switch (camel_disco_store_status (disco)) {
135 case CAMEL_DISCO_STORE_ONLINE:
136 CDF_CLASS (folder)->expunge_uids_online (folder, uids, ex);
139 case CAMEL_DISCO_STORE_OFFLINE:
140 CDF_CLASS (folder)->expunge_uids_offline (folder, uids, ex);
143 case CAMEL_DISCO_STORE_RESYNCING:
144 CDF_CLASS (folder)->expunge_uids_resyncing (folder, uids, ex);
150 disco_expunge (CamelFolder *folder, CamelException *ex)
154 CamelMessageInfo *info;
156 uids = g_ptr_array_new ();
157 count = camel_folder_summary_count (folder->summary);
158 for (i = 0; i < count; i++) {
159 info = camel_folder_summary_index (folder->summary, i);
160 if (info->flags & CAMEL_MESSAGE_DELETED)
161 g_ptr_array_add (uids, g_strdup (camel_message_info_uid (info)));
162 camel_folder_summary_info_free (folder->summary, info);
165 disco_expunge_uids (folder, uids, ex);
167 for (i = 0; i < uids->len; i++)
168 g_free (uids->pdata[i]);
169 g_ptr_array_free (uids, TRUE);
173 disco_append_message (CamelFolder *folder, CamelMimeMessage *message,
174 const CamelMessageInfo *info, CamelException *ex)
176 CamelDiscoStore *disco = CAMEL_DISCO_STORE (folder->parent_store);
178 switch (camel_disco_store_status (disco)) {
179 case CAMEL_DISCO_STORE_ONLINE:
180 CDF_CLASS (folder)->append_online (folder, message, info, ex);
183 case CAMEL_DISCO_STORE_OFFLINE:
184 CDF_CLASS (folder)->append_offline (folder, message, info, ex);
187 case CAMEL_DISCO_STORE_RESYNCING:
188 CDF_CLASS (folder)->append_resyncing (folder, message, info, ex);
194 disco_copy_messages_to (CamelFolder *source, GPtrArray *uids,
195 CamelFolder *destination, CamelException *ex)
197 CamelDiscoStore *disco = CAMEL_DISCO_STORE (source->parent_store);
199 switch (camel_disco_store_status (disco)) {
200 case CAMEL_DISCO_STORE_ONLINE:
201 CDF_CLASS (source)->copy_online (source, uids, destination, ex);
204 case CAMEL_DISCO_STORE_OFFLINE:
205 CDF_CLASS (source)->copy_offline (source, uids, destination, ex);
208 case CAMEL_DISCO_STORE_RESYNCING:
209 CDF_CLASS (source)->copy_resyncing (source, uids, destination, ex);
215 disco_move_messages_to (CamelFolder *source, GPtrArray *uids,
216 CamelFolder *destination, CamelException *ex)
218 CamelDiscoStore *disco = CAMEL_DISCO_STORE (source->parent_store);
220 switch (camel_disco_store_status (disco)) {
221 case CAMEL_DISCO_STORE_ONLINE:
222 CDF_CLASS (source)->move_online (source, uids, destination, ex);
225 case CAMEL_DISCO_STORE_OFFLINE:
226 CDF_CLASS (source)->move_offline (source, uids, destination, ex);
229 case CAMEL_DISCO_STORE_RESYNCING:
230 CDF_CLASS (source)->move_resyncing (source, uids, destination, ex);
237 * camel_disco_folder_expunge_uids:
238 * @folder: a (disconnectable) folder
239 * @uids: array of UIDs to expunge
240 * @ex: a CamelException
242 * This expunges the messages in @uids from @folder. It should take
243 * whatever steps are needed to avoid expunging any other messages,
244 * although in some cases it may not be possible to avoid expunging
245 * messages that are marked deleted by another client at the same time
246 * as the expunge_uids call is running.
249 camel_disco_folder_expunge_uids (CamelFolder *folder, GPtrArray *uids,
252 disco_expunge_uids (folder, uids, ex);
257 disco_cache_message (CamelDiscoFolder *disco_folder, const char *uid,
260 g_warning ("CamelDiscoFolder::cache_message not implemented for `%s'",
261 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (disco_folder)));
265 * camel_disco_folder_cache_message:
266 * @disco_folder: the folder
267 * @uid: the UID of the message to cache
268 * @ex: a CamelException
270 * Requests that @disco_folder cache message @uid to disk.
273 camel_disco_folder_cache_message (CamelDiscoFolder *disco_folder,
274 const char *uid, CamelException *ex)
276 CDF_CLASS (disco_folder)->cache_message (disco_folder, uid, ex);
281 disco_prepare_for_offline (CamelDiscoFolder *disco_folder,
282 const char *expression,
285 CamelFolder *folder = CAMEL_FOLDER (disco_folder);
290 uids = camel_folder_search_by_expression (folder, expression, ex);
292 uids = camel_folder_get_uids (folder);
295 for (i = 0; i < uids->len; i++) {
296 camel_disco_folder_cache_message (disco_folder, uids->pdata[i], ex);
297 if (camel_exception_is_set (ex))
301 camel_folder_search_free (folder, uids);
303 camel_folder_free_uids (folder, uids);
307 * camel_disco_folder_prepare_for_offline:
308 * @disco_folder: the folder
309 * @expression: an expression describing messages to synchronize, or %NULL
310 * if all messages should be sync'ed.
311 * @ex: a CamelException
313 * This prepares @disco_folder for offline operation, by downloading
314 * the bodies of all messages described by @expression (using the
315 * same syntax as camel_folder_search_by_expression() ).
318 camel_disco_folder_prepare_for_offline (CamelDiscoFolder *disco_folder,
319 const char *expression,
322 g_return_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder));
324 CDF_CLASS (disco_folder)->prepare_for_offline (disco_folder, expression, ex);