1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-disco-store.c: abstract class for a disconnectable store */
5 * Authors: Dan Winship <danw@ximian.com>
7 * Copyright 2001 Ximian, Inc.
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 GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
31 #include <glib/gi18n-lib.h>
33 #include "camel-disco-diary.h"
34 #include "camel-disco-folder.h"
35 #include "camel-disco-store.h"
36 #include "camel-exception.h"
37 #include "camel-session.h"
41 #define CDS_CLASS(o) (CAMEL_DISCO_STORE_CLASS (CAMEL_OBJECT_GET_CLASS (o)))
43 static CamelStoreClass *parent_class = NULL;
45 static void disco_construct (CamelService *service, CamelSession *session,
46 CamelProvider *provider, CamelURL *url,
48 static gboolean disco_connect (CamelService *service, CamelException *ex);
49 static void disco_cancel_connect (CamelService *service);
50 static gboolean disco_disconnect (CamelService *service, gboolean clean, CamelException *ex);
51 static CamelFolder *disco_get_folder (CamelStore *store, const char *name,
52 guint32 flags, CamelException *ex);
53 static CamelFolderInfo *disco_get_folder_info (CamelStore *store,
54 const char *top, guint32 flags,
56 static void set_status (CamelDiscoStore *disco_store,
57 CamelDiscoStoreStatus status,
59 static gboolean can_work_offline (CamelDiscoStore *disco_store);
61 static int disco_setv (CamelObject *object, CamelException *ex, CamelArgV *args);
62 static int disco_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args);
65 camel_disco_store_class_init (CamelDiscoStoreClass *camel_disco_store_class)
67 CamelObjectClass *camel_object_class =
68 CAMEL_OBJECT_CLASS (camel_disco_store_class);
69 CamelServiceClass *camel_service_class =
70 CAMEL_SERVICE_CLASS (camel_disco_store_class);
71 CamelStoreClass *camel_store_class =
72 CAMEL_STORE_CLASS (camel_disco_store_class);
74 parent_class = CAMEL_STORE_CLASS (camel_type_get_global_classfuncs (camel_store_get_type ()));
76 /* virtual method definition */
77 camel_disco_store_class->set_status = set_status;
78 camel_disco_store_class->can_work_offline = can_work_offline;
80 /* virtual method overload */
81 camel_object_class->setv = disco_setv;
82 camel_object_class->getv = disco_getv;
84 camel_service_class->construct = disco_construct;
85 camel_service_class->connect = disco_connect;
86 camel_service_class->disconnect = disco_disconnect;
87 camel_service_class->cancel_connect = disco_cancel_connect;
89 camel_store_class->get_folder = disco_get_folder;
90 camel_store_class->get_folder_info = disco_get_folder_info;
94 camel_disco_store_get_type (void)
96 static CamelType camel_disco_store_type = CAMEL_INVALID_TYPE;
98 if (camel_disco_store_type == CAMEL_INVALID_TYPE) {
99 camel_disco_store_type = camel_type_register (
102 sizeof (CamelDiscoStore),
103 sizeof (CamelDiscoStoreClass),
104 (CamelObjectClassInitFunc) camel_disco_store_class_init,
110 return camel_disco_store_type;
114 disco_setv (CamelObject *object, CamelException *ex, CamelArgV *args)
116 /* CamelDiscoStore doesn't currently have anything to set */
117 return CAMEL_OBJECT_CLASS (parent_class)->setv (object, ex, args);
121 disco_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args)
123 /* CamelDiscoStore doesn't currently have anything to get */
124 return CAMEL_OBJECT_CLASS (parent_class)->getv (object, ex, args);
128 disco_construct (CamelService *service, CamelSession *session,
129 CamelProvider *provider, CamelURL *url,
132 CamelDiscoStore *disco = CAMEL_DISCO_STORE (service);
134 CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
135 if (camel_exception_is_set (ex))
138 disco->status = camel_session_is_online (session) ?
139 CAMEL_DISCO_STORE_ONLINE : CAMEL_DISCO_STORE_OFFLINE;
143 disco_connect (CamelService *service, CamelException *ex)
145 CamelDiscoStore *store = CAMEL_DISCO_STORE (service);
146 CamelDiscoStoreStatus status;
147 struct _CamelDiscoDiary *diary;
149 status = camel_disco_store_status (store);
150 if (status != CAMEL_DISCO_STORE_OFFLINE) {
151 if (!CAMEL_SERVICE_CLASS (parent_class)->connect (service, ex)) {
152 status = camel_disco_store_status (store);
153 if (status != CAMEL_DISCO_STORE_OFFLINE)
155 camel_exception_clear (ex);
160 case CAMEL_DISCO_STORE_ONLINE:
161 case CAMEL_DISCO_STORE_RESYNCING:
162 if (!CDS_CLASS (service)->connect_online (service, ex))
168 d(printf(" diary is %s\n", camel_disco_diary_empty(store->diary)?"empty":"not empty"));
169 if (camel_disco_diary_empty (store->diary))
172 /* Need to resync. Note we do the ref thing since during the replay
173 disconnect could be called, which will remove store->diary and unref it */
174 store->status = CAMEL_DISCO_STORE_RESYNCING;
175 diary = store->diary;
176 camel_object_ref(diary);
177 camel_disco_diary_replay(diary, ex);
178 camel_object_unref(diary);
179 store->status = CAMEL_DISCO_STORE_ONLINE;
180 if (camel_exception_is_set (ex))
183 if (!camel_service_disconnect (service, TRUE, ex))
185 return camel_service_connect (service, ex);
187 case CAMEL_DISCO_STORE_OFFLINE:
188 return CDS_CLASS (service)->connect_offline (service, ex);
191 g_assert_not_reached ();
196 disco_cancel_connect (CamelService *service)
198 CamelDiscoStore *store = CAMEL_DISCO_STORE (service);
201 store->status = CAMEL_DISCO_STORE_OFFLINE;
202 CAMEL_SERVICE_CLASS (parent_class)->cancel_connect (service);
206 disco_disconnect (CamelService *service, gboolean clean, CamelException *ex)
208 CamelDiscoStore *store = CAMEL_DISCO_STORE (service);
210 switch (camel_disco_store_status (store)) {
211 case CAMEL_DISCO_STORE_ONLINE:
212 case CAMEL_DISCO_STORE_RESYNCING:
213 if (!CDS_CLASS (service)->disconnect_online (service, clean, ex))
217 case CAMEL_DISCO_STORE_OFFLINE:
218 if (!CDS_CLASS (service)->disconnect_offline (service, clean, ex))
224 return CAMEL_SERVICE_CLASS (parent_class)->disconnect (service, clean, ex);
228 disco_get_folder (CamelStore *store, const char *name,
229 guint32 flags, CamelException *ex)
231 CamelDiscoStore *disco_store = CAMEL_DISCO_STORE (store);
233 switch (camel_disco_store_status (disco_store)) {
234 case CAMEL_DISCO_STORE_ONLINE:
235 return CDS_CLASS (store)->get_folder_online (store, name, flags, ex);
237 case CAMEL_DISCO_STORE_OFFLINE:
238 return CDS_CLASS (store)->get_folder_offline (store, name, flags, ex);
240 case CAMEL_DISCO_STORE_RESYNCING:
241 return CDS_CLASS (store)->get_folder_resyncing (store, name, flags, ex);
244 g_assert_not_reached ();
248 static CamelFolderInfo *
249 disco_get_folder_info (CamelStore *store, const char *top,
250 guint32 flags, CamelException *ex)
252 CamelDiscoStore *disco_store = CAMEL_DISCO_STORE (store);
254 switch (camel_disco_store_status (disco_store)) {
255 case CAMEL_DISCO_STORE_ONLINE:
256 return CDS_CLASS (store)->get_folder_info_online (store, top, flags, ex);
258 case CAMEL_DISCO_STORE_OFFLINE:
259 /* Can't edit subscriptions while offline */
260 if ((store->flags & CAMEL_STORE_SUBSCRIPTIONS) &&
261 !(flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED)) {
262 camel_disco_store_check_online (disco_store, ex);
266 return CDS_CLASS (store)->get_folder_info_offline (store, top, flags, ex);
268 case CAMEL_DISCO_STORE_RESYNCING:
269 return CDS_CLASS (store)->get_folder_info_resyncing (store, top, flags, ex);
272 g_assert_not_reached ();
278 * camel_disco_store_status:
279 * @store: a disconnectable store
281 * Return value: the current online/offline status of @store.
283 CamelDiscoStoreStatus
284 camel_disco_store_status (CamelDiscoStore *store)
286 CamelService *service = CAMEL_SERVICE (store);
288 g_return_val_if_fail (CAMEL_IS_DISCO_STORE (store), CAMEL_DISCO_STORE_ONLINE);
290 if (store->status != CAMEL_DISCO_STORE_OFFLINE
291 && !camel_session_is_online (service->session))
292 store->status = CAMEL_DISCO_STORE_OFFLINE;
294 return store->status;
298 set_status(CamelDiscoStore *disco_store, CamelDiscoStoreStatus status, CamelException *ex)
301 CamelService *service = CAMEL_SERVICE (disco_store);
302 gboolean network_state = camel_session_get_network_state (service->session);
304 if (disco_store->status == status)
307 camel_exception_init(&x);
308 /* Sync the folder fully if we've been told to sync online for this store or this folder
309 and we're going offline */
312 if (disco_store->status == CAMEL_DISCO_STORE_ONLINE
313 && status == CAMEL_DISCO_STORE_OFFLINE) {
314 if (((CamelStore *)disco_store)->folders) {
319 sync = camel_url_get_param(((CamelService *)disco_store)->url, "offline_sync") != NULL;
321 folders = camel_object_bag_list(((CamelStore *)disco_store)->folders);
322 for (i=0;i<folders->len;i++) {
323 folder = folders->pdata[i];
324 if (CAMEL_CHECK_TYPE(folder, CAMEL_DISCO_FOLDER_TYPE)
325 && (sync || ((CamelDiscoFolder *)folder)->offline_sync)) {
326 camel_disco_folder_prepare_for_offline((CamelDiscoFolder *)folder, "(match-all)", &x);
327 camel_exception_clear(&x);
329 camel_object_unref(folder);
331 g_ptr_array_free(folders, TRUE);
335 camel_store_sync(CAMEL_STORE (disco_store), FALSE, &x);
336 camel_exception_clear(&x);
339 if (!camel_service_disconnect (CAMEL_SERVICE (disco_store), network_state, ex))
342 disco_store->status = status;
343 camel_service_connect (CAMEL_SERVICE (disco_store), ex);
347 * camel_disco_store_set_status:
348 * @store: a disconnectable store
349 * @status: the new status
350 * @ex: a CamelException
352 * Sets @store to @status. If an error occurrs and the status cannot
353 * be set to @status, @ex will be set.
356 camel_disco_store_set_status (CamelDiscoStore *store,
357 CamelDiscoStoreStatus status,
360 d(printf("disco store set status: %s\n", status == CAMEL_DISCO_STORE_ONLINE?"online":"offline"));
362 CDS_CLASS (store)->set_status (store, status, ex);
366 can_work_offline (CamelDiscoStore *disco_store)
368 g_warning ("CamelDiscoStore::can_work_offline not implemented for `%s'",
369 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (disco_store)));
374 * camel_disco_store_can_work_offline:
375 * @store: a disconnectable store
377 * Return value: whether or not @store can be used offline. (Will be
378 * %FALSE if the store is not caching data to local disk, for example.)
381 camel_disco_store_can_work_offline (CamelDiscoStore *store)
383 return CDS_CLASS (store)->can_work_offline (store);
388 * camel_disco_store_check_online:
389 * @store: a disconnectable store
390 * @ex: a CamelException
392 * This checks that @store is online, and sets @ex if it is not. This
393 * can be used as a simple way to set a generic error message in @ex
394 * for operations that won't work offline.
396 * Return value: whether or not @store is online.
399 camel_disco_store_check_online (CamelDiscoStore *store, CamelException *ex)
401 if (camel_disco_store_status (store) != CAMEL_DISCO_STORE_ONLINE) {
402 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
403 _("You must be working online to "
404 "complete this operation"));
412 camel_disco_store_prepare_for_offline(CamelDiscoStore *disco_store, CamelException *ex)
415 CamelService *service = CAMEL_SERVICE (disco_store);
416 gboolean network_state = camel_session_get_network_state (service->session);
418 camel_exception_init(&x);
419 /* Sync the folder fully if we've been told to sync online for this store or this folder */
422 if (disco_store->status == CAMEL_DISCO_STORE_ONLINE) {
423 if (((CamelStore *)disco_store)->folders) {
428 sync = camel_url_get_param(((CamelService *)disco_store)->url, "offline_sync") != NULL;
430 folders = camel_object_bag_list(((CamelStore *)disco_store)->folders);
431 for (i=0;i<folders->len;i++) {
432 folder = folders->pdata[i];
433 if (CAMEL_CHECK_TYPE(folder, CAMEL_DISCO_FOLDER_TYPE)
434 && (sync || ((CamelDiscoFolder *)folder)->offline_sync)) {
435 camel_disco_folder_prepare_for_offline((CamelDiscoFolder *)folder, "(match-all)", &x);
436 camel_exception_clear(&x);
438 camel_object_unref(folder);
440 g_ptr_array_free(folders, TRUE);
444 camel_store_sync(CAMEL_STORE (disco_store), FALSE, &x);
445 camel_exception_clear(&x);