Code for logging and replaying offline operations.
[platform/upstream/evolution-data-server.git] / camel / camel-disco-folder.c
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 */
3
4 /* 
5  * Authors: Dan Winship <danw@ximian.com>
6  *
7  * Copyright (C) 2001 Ximian, Inc.
8  *
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.
13  *
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.
18  *
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
22  * USA
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include "camel-disco-folder.h"
30 #include "camel-disco-store.h"
31 #include "camel-exception.h"
32
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)))
35
36 static CamelFolderClass *parent_class = NULL;
37
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);
41
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);
48
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,
53                                        CamelException *ex);
54
55 static void
56 camel_disco_folder_class_init (CamelDiscoFolderClass *camel_disco_folder_class)
57 {
58         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_disco_folder_class);
59
60         parent_class = CAMEL_FOLDER_CLASS (camel_type_get_global_classfuncs (camel_folder_get_type ()));
61
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;
65
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;
70
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;
74 }
75
76 CamelType
77 camel_disco_folder_get_type (void)
78 {
79         static CamelType camel_disco_folder_type = CAMEL_INVALID_TYPE;
80
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,
87                         NULL, NULL, NULL);
88         }
89
90         return camel_disco_folder_type;
91 }
92
93
94 static void
95 disco_refresh_info (CamelFolder *folder, CamelException *ex)
96 {
97         if (camel_disco_store_status (CAMEL_DISCO_STORE (folder->parent_store)) != CAMEL_DISCO_STORE_ONLINE)
98                 return;
99         CDF_CLASS (folder)->refresh_info_online (folder, ex);
100 }
101
102 static void
103 disco_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
104 {
105         if (expunge) {
106                 disco_expunge (folder, ex);
107                 if (camel_exception_is_set (ex))
108                         return;
109         }
110
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);
114                 break;
115
116         case CAMEL_DISCO_STORE_OFFLINE:
117                 CDF_CLASS (folder)->sync_offline (folder, ex);
118                 break;
119
120         case CAMEL_DISCO_STORE_RESYNCING:
121                 CDF_CLASS (folder)->sync_resyncing (folder, ex);
122                 break;
123         }
124 }
125
126 static void
127 disco_expunge_uids (CamelFolder *folder, GPtrArray *uids, CamelException *ex)
128 {
129         CamelDiscoStore *disco = CAMEL_DISCO_STORE (folder->parent_store);
130
131         if (uids->len == 0)
132                 return;
133
134         switch (camel_disco_store_status (disco)) {
135         case CAMEL_DISCO_STORE_ONLINE:
136                 CDF_CLASS (folder)->expunge_uids_online (folder, uids, ex);
137                 break;
138
139         case CAMEL_DISCO_STORE_OFFLINE:
140                 CDF_CLASS (folder)->expunge_uids_offline (folder, uids, ex);
141                 break;
142
143         case CAMEL_DISCO_STORE_RESYNCING:
144                 CDF_CLASS (folder)->expunge_uids_resyncing (folder, uids, ex);
145                 break;
146         }
147 }
148
149 static void
150 disco_expunge (CamelFolder *folder, CamelException *ex)
151 {
152         GPtrArray *uids;
153         int i, count;
154         CamelMessageInfo *info;
155
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);
163         }
164
165         disco_expunge_uids (folder, uids, ex);
166
167         for (i = 0; i < uids->len; i++)
168                 g_free (uids->pdata[i]);
169         g_ptr_array_free (uids, TRUE);
170 }
171
172 static void
173 disco_append_message (CamelFolder *folder, CamelMimeMessage *message,
174                       const CamelMessageInfo *info, CamelException *ex)
175 {
176         CamelDiscoStore *disco = CAMEL_DISCO_STORE (folder->parent_store);
177
178         switch (camel_disco_store_status (disco)) {
179         case CAMEL_DISCO_STORE_ONLINE:
180                 CDF_CLASS (folder)->append_online (folder, message, info, ex);
181                 break;
182
183         case CAMEL_DISCO_STORE_OFFLINE:
184                 CDF_CLASS (folder)->append_offline (folder, message, info, ex);
185                 break;
186
187         case CAMEL_DISCO_STORE_RESYNCING:
188                 CDF_CLASS (folder)->append_resyncing (folder, message, info, ex);
189                 break;
190         }
191 }
192
193 static void
194 disco_copy_messages_to (CamelFolder *source, GPtrArray *uids,
195                         CamelFolder *destination, CamelException *ex)
196 {
197         CamelDiscoStore *disco = CAMEL_DISCO_STORE (source->parent_store);
198
199         switch (camel_disco_store_status (disco)) {
200         case CAMEL_DISCO_STORE_ONLINE:
201                 CDF_CLASS (source)->copy_online (source, uids, destination, ex);
202                 break;
203
204         case CAMEL_DISCO_STORE_OFFLINE:
205                 CDF_CLASS (source)->copy_offline (source, uids, destination, ex);
206                 break;
207
208         case CAMEL_DISCO_STORE_RESYNCING:
209                 CDF_CLASS (source)->copy_resyncing (source, uids, destination, ex);
210                 break;
211         }
212 }
213
214 static void
215 disco_move_messages_to (CamelFolder *source, GPtrArray *uids,
216                         CamelFolder *destination, CamelException *ex)
217 {
218         CamelDiscoStore *disco = CAMEL_DISCO_STORE (source->parent_store);
219
220         switch (camel_disco_store_status (disco)) {
221         case CAMEL_DISCO_STORE_ONLINE:
222                 CDF_CLASS (source)->move_online (source, uids, destination, ex);
223                 break;
224
225         case CAMEL_DISCO_STORE_OFFLINE:
226                 CDF_CLASS (source)->move_offline (source, uids, destination, ex);
227                 break;
228
229         case CAMEL_DISCO_STORE_RESYNCING:
230                 CDF_CLASS (source)->move_resyncing (source, uids, destination, ex);
231                 break;
232         }
233 }
234
235
236 /**
237  * camel_disco_folder_expunge_uids:
238  * @folder: a (disconnectable) folder
239  * @uids: array of UIDs to expunge
240  * @ex: a CamelException
241  *
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.
247  **/
248 void
249 camel_disco_folder_expunge_uids (CamelFolder *folder, GPtrArray *uids,
250                                  CamelException *ex)
251 {
252         disco_expunge_uids (folder, uids, ex);
253 }
254
255
256 static void
257 disco_cache_message (CamelDiscoFolder *disco_folder, const char *uid,
258                      CamelException *ex)
259 {
260         g_warning ("CamelDiscoFolder::cache_message not implemented for `%s'",
261                    camel_type_to_name (CAMEL_OBJECT_GET_TYPE (disco_folder)));
262 }
263
264 /**
265  * camel_disco_folder_cache_message:
266  * @disco_folder: the folder
267  * @uid: the UID of the message to cache
268  * @ex: a CamelException
269  *
270  * Requests that @disco_folder cache message @uid to disk.
271  **/
272 void
273 camel_disco_folder_cache_message (CamelDiscoFolder *disco_folder,
274                                   const char *uid, CamelException *ex)
275 {
276         CDF_CLASS (disco_folder)->cache_message (disco_folder, uid, ex);
277 }
278
279
280 static void
281 disco_prepare_for_offline (CamelDiscoFolder *disco_folder,
282                            const char *expression,
283                            CamelException *ex)
284 {
285         CamelFolder *folder = CAMEL_FOLDER (disco_folder);
286         GPtrArray *uids;
287         int i;
288
289         if (expression)
290                 uids = camel_folder_search_by_expression (folder, expression, ex);
291         else
292                 uids = camel_folder_get_uids (folder);
293         if (!uids)
294                 return;
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))
298                         break;
299         }
300         if (expression)
301                 camel_folder_search_free (folder, uids);
302         else
303                 camel_folder_free_uids (folder, uids);
304 }
305
306 /**
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
312  *
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() ).
316  **/
317 void 
318 camel_disco_folder_prepare_for_offline (CamelDiscoFolder *disco_folder,
319                                         const char *expression,
320                                         CamelException *ex)
321 {
322         g_return_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder));
323
324         CDF_CLASS (disco_folder)->prepare_for_offline (disco_folder, expression, ex);
325 }