Set the disconnected status. (camel_disco_store_can_work_offline): Return
[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 }
121
122 static void
123 disco_expunge_uids (CamelFolder *folder, GPtrArray *uids, CamelException *ex)
124 {
125         CamelDiscoStore *disco = CAMEL_DISCO_STORE (folder->parent_store);
126
127         if (uids->len == 0)
128                 return;
129
130         switch (camel_disco_store_status (disco)) {
131         case CAMEL_DISCO_STORE_ONLINE:
132                 CDF_CLASS (folder)->expunge_uids_online (folder, uids, ex);
133                 break;
134
135         case CAMEL_DISCO_STORE_OFFLINE:
136                 CDF_CLASS (folder)->expunge_uids_offline (folder, uids, ex);
137 #ifdef NOTYET
138                 if (!camel_exception_is_set (ex)) {
139                         camel_disco_diary_log (disco->diary,
140                                                CAMEL_DISCO_DIARY_FOLDER_EXPUNGE,
141                                                folder, uids);
142                 }
143 #endif
144                 break;
145         }
146 }
147
148 static void
149 disco_expunge (CamelFolder *folder, CamelException *ex)
150 {
151         GPtrArray *uids;
152         int i, count;
153         CamelMessageInfo *info;
154
155         uids = g_ptr_array_new ();
156         count = camel_folder_summary_count (folder->summary);
157         for (i = 0; i < count; i++) {
158                 info = camel_folder_summary_index (folder->summary, i);
159                 if (info->flags & CAMEL_MESSAGE_DELETED)
160                         g_ptr_array_add (uids, g_strdup (camel_message_info_uid (info)));
161                 camel_folder_summary_info_free (folder->summary, info);
162         }
163
164         disco_expunge_uids (folder, uids, ex);
165
166         for (i = 0; i < uids->len; i++)
167                 g_free (uids->pdata[i]);
168         g_ptr_array_free (uids, TRUE);
169 }
170
171 static void
172 disco_append_message (CamelFolder *folder, CamelMimeMessage *message,
173                       const CamelMessageInfo *info, CamelException *ex)
174 {
175         CamelDiscoStore *disco = CAMEL_DISCO_STORE (folder->parent_store);
176         char *uid;
177
178         switch (camel_disco_store_status (disco)) {
179         case CAMEL_DISCO_STORE_ONLINE:
180                 uid = CDF_CLASS (folder)->append_online (folder, message, info, ex);
181                 break;
182
183         case CAMEL_DISCO_STORE_OFFLINE:
184                 uid = CDF_CLASS (folder)->append_offline (folder, message, info, ex);
185 #ifdef NOTYET
186                 if (uid) {
187                         camel_disco_diary_log (disco->diary,
188                                                CAMEL_DISCO_DIARY_FOLDER_APPEND,
189                                                folder, uid);
190                 }
191 #endif
192                 break;
193         }
194         g_free (uid);
195 }
196
197 static void
198 disco_copy_messages_to (CamelFolder *source, GPtrArray *uids,
199                         CamelFolder *destination, CamelException *ex)
200 {
201         CamelDiscoStore *disco = CAMEL_DISCO_STORE (source->parent_store);
202
203         switch (camel_disco_store_status (disco)) {
204         case CAMEL_DISCO_STORE_ONLINE:
205                 CDF_CLASS (source)->copy_online (source, uids, destination, ex);
206                 break;
207
208         case CAMEL_DISCO_STORE_OFFLINE:
209                 CDF_CLASS (source)->copy_offline (source, uids, destination, ex);
210 #ifdef NOTYET
211                 if (!camel_exception_is_set (ex)) {
212                         camel_disco_diary_log (disco->diary,
213                                                CAMEL_DISCO_DIARY_FOLDER_COPY,
214                                                source, destination, uids);
215                 }
216 #endif
217                 break;
218         }
219 }
220
221 static void
222 disco_move_messages_to (CamelFolder *source, GPtrArray *uids,
223                         CamelFolder *destination, CamelException *ex)
224 {
225         CamelDiscoStore *disco = CAMEL_DISCO_STORE (source->parent_store);
226
227         switch (camel_disco_store_status (disco)) {
228         case CAMEL_DISCO_STORE_ONLINE:
229                 CDF_CLASS (source)->move_online (source, uids, destination, ex);
230                 break;
231
232         case CAMEL_DISCO_STORE_OFFLINE:
233                 CDF_CLASS (source)->move_offline (source, uids, destination, ex);
234 #ifdef NOTYET
235                 if (!camel_exception_is_set (ex)) {
236                         camel_disco_diary_log (disco->diary,
237                                                CAMEL_DISCO_DIARY_FOLDER_MOVE,
238                                                source, destination, uids);
239                 }
240 #endif
241                 break;
242         }
243 }
244
245
246 /**
247  * camel_disco_folder_expunge_uids:
248  * @folder: a (disconnectable) folder
249  * @uids: array of UIDs to expunge
250  * @ex: a CamelException
251  *
252  * This expunges the messages in @uids from @folder. It should take
253  * whatever steps are needed to avoid expunging any other messages,
254  * although in some cases it may not be possible to avoid expunging
255  * messages that are marked deleted by another client at the same time
256  * as the expunge_uids call is running.
257  **/
258 void
259 camel_disco_folder_expunge_uids (CamelFolder *folder, GPtrArray *uids,
260                                  CamelException *ex)
261 {
262         disco_expunge_uids (folder, uids, ex);
263 }
264
265
266 static void
267 disco_cache_message (CamelDiscoFolder *disco_folder, const char *uid,
268                      CamelException *ex)
269 {
270         g_warning ("CamelDiscoFolder::cache_message not implemented for `%s'",
271                    camel_type_to_name (CAMEL_OBJECT_GET_TYPE (disco_folder)));
272 }
273
274 /**
275  * camel_disco_folder_cache_message:
276  * @disco_folder: the folder
277  * @uid: the UID of the message to cache
278  * @ex: a CamelException
279  *
280  * Requests that @disco_folder cache message @uid to disk.
281  **/
282 void
283 camel_disco_folder_cache_message (CamelDiscoFolder *disco_folder,
284                                   const char *uid, CamelException *ex)
285 {
286         CDF_CLASS (disco_folder)->cache_message (disco_folder, uid, ex);
287 }
288
289
290 static void
291 disco_prepare_for_offline (CamelDiscoFolder *disco_folder,
292                            const char *expression,
293                            CamelException *ex)
294 {
295         CamelFolder *folder = CAMEL_FOLDER (disco_folder);
296         GPtrArray *uids;
297         int i;
298
299         if (expression)
300                 uids = camel_folder_search_by_expression (folder, expression, ex);
301         else
302                 uids = camel_folder_get_uids (folder);
303         if (!uids)
304                 return;
305         for (i = 0; i < uids->len; i++) {
306                 camel_disco_folder_cache_message (disco_folder, uids->pdata[i], ex);
307                 if (camel_exception_is_set (ex))
308                         break;
309         }
310         if (expression)
311                 camel_folder_search_free (folder, uids);
312         else
313                 camel_folder_free_uids (folder, uids);
314 }
315
316 /**
317  * camel_disco_folder_prepare_for_offline:
318  * @disco_folder: the folder
319  * @expression: an expression describing messages to synchronize, or %NULL
320  * if all messages should be sync'ed.
321  * @ex: a CamelException
322  *
323  * This prepares @disco_folder for offline operation, by downloading
324  * the bodies of all messages described by @expression (using the
325  * same syntax as camel_folder_search_by_expression() ).
326  **/
327 void 
328 camel_disco_folder_prepare_for_offline (CamelDiscoFolder *disco_folder,
329                                         const char *expression,
330                                         CamelException *ex)
331 {
332         g_return_if_fail (CAMEL_IS_DISCO_FOLDER (disco_folder));
333
334         CDF_CLASS (disco_folder)->prepare_for_offline (disco_folder, expression, ex);
335 }