1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@novell.com>
5 * Copyright 2005 Novell, Inc. (www.novell.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <glib/gi18n-lib.h>
31 #include "camel-folder.h"
32 #include "camel-offline-folder.h"
33 #include "camel-offline-store.h"
34 #include "camel-session.h"
37 static void camel_offline_store_class_init (CamelOfflineStoreClass *klass);
38 static void camel_offline_store_init (CamelOfflineStore *store, CamelOfflineStoreClass *klass);
39 static void camel_offline_store_finalize (CamelObject *object);
41 static void offline_store_construct (CamelService *service, CamelSession *session,
42 CamelProvider *provider, CamelURL *url,
46 static CamelStoreClass *parent_class = NULL;
50 camel_offline_store_get_type (void)
52 static CamelType type = 0;
55 type = camel_type_register (CAMEL_STORE_TYPE,
57 sizeof (CamelOfflineStore),
58 sizeof (CamelOfflineStoreClass),
59 (CamelObjectClassInitFunc) camel_offline_store_class_init,
61 (CamelObjectInitFunc) camel_offline_store_init,
62 (CamelObjectFinalizeFunc) camel_offline_store_finalize);
70 camel_offline_store_class_init (CamelOfflineStoreClass *klass)
72 parent_class = (CamelStoreClass *) camel_type_get_global_classfuncs (CAMEL_STORE_TYPE);
74 ((CamelServiceClass *) klass)->construct = offline_store_construct;
78 camel_offline_store_init (CamelOfflineStore *store, CamelOfflineStoreClass *klass)
80 store->state = CAMEL_OFFLINE_STORE_NETWORK_AVAIL;
84 camel_offline_store_finalize (CamelObject *object)
91 offline_store_construct (CamelService *service, CamelSession *session,
92 CamelProvider *provider, CamelURL *url,
95 CamelOfflineStore *store = CAMEL_OFFLINE_STORE (service);
97 CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
98 if (camel_exception_is_set (ex))
101 store->state = camel_session_is_online (session) ?
102 CAMEL_OFFLINE_STORE_NETWORK_AVAIL : CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL;
107 * camel_offline_store_set_network_state:
108 * @store: a #CamelOfflineStore object
109 * @state: the network state
110 * @ex: a #CamelException
112 * Set the network state to either #CAMEL_OFFLINE_STORE_NETWORK_AVAIL
113 * or #CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL.
116 camel_offline_store_set_network_state (CamelOfflineStore *store, int state, CamelException *ex)
119 CamelService *service = CAMEL_SERVICE (store);
120 gboolean network_state = camel_session_get_network_state (service->session);
122 if (store->state == state)
125 camel_exception_init (&lex);
126 if (store->state == CAMEL_OFFLINE_STORE_NETWORK_AVAIL) {
127 /* network available -> network unavailable */
129 if (((CamelStore *) store)->folders) {
134 sync = camel_url_get_param (((CamelService *) store)->url, "sync_offline") != NULL;
136 folders = camel_object_bag_list (((CamelStore *) store)->folders);
137 for (i = 0; i < folders->len; i++) {
138 folder = folders->pdata[i];
140 if (CAMEL_CHECK_TYPE (folder, CAMEL_OFFLINE_FOLDER_TYPE)
141 && (sync || ((CamelOfflineFolder *) folder)->sync_offline)) {
142 camel_offline_folder_downsync ((CamelOfflineFolder *) folder, NULL, &lex);
143 camel_exception_clear (&lex);
146 camel_object_unref (folder);
149 g_ptr_array_free (folders, TRUE);
152 camel_store_sync (CAMEL_STORE (store), FALSE, &lex);
153 camel_exception_clear (&lex);
156 if (!camel_service_disconnect (CAMEL_SERVICE (store), network_state, ex))
159 /* network unavailable -> network available */
160 if (!camel_service_connect (CAMEL_SERVICE (store), ex))
164 store->state = state;