1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@novell.com>
5 * Copyright (C) 1999-2008 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.
27 #include <glib/gi18n-lib.h>
29 #include "camel-folder.h"
30 #include "camel-offline-folder.h"
31 #include "camel-offline-settings.h"
32 #include "camel-offline-store.h"
33 #include "camel-session.h"
35 #define CAMEL_OFFLINE_STORE_GET_PRIVATE(obj) \
36 (G_TYPE_INSTANCE_GET_PRIVATE \
37 ((obj), CAMEL_TYPE_OFFLINE_STORE, CamelOfflineStorePrivate))
39 struct _CamelOfflineStorePrivate {
43 G_DEFINE_TYPE (CamelOfflineStore, camel_offline_store, CAMEL_TYPE_STORE)
46 offline_store_constructed (GObject *object)
48 CamelOfflineStorePrivate *priv;
49 CamelSession *session;
51 priv = CAMEL_OFFLINE_STORE_GET_PRIVATE (object);
53 /* Chain up to parent's constructed() method. */
54 G_OBJECT_CLASS (camel_offline_store_parent_class)->
57 session = camel_service_get_session (CAMEL_SERVICE (object));
58 priv->online = camel_session_get_online (session);
62 camel_offline_store_class_init (CamelOfflineStoreClass *class)
64 GObjectClass *object_class;
65 CamelServiceClass *service_class;
67 g_type_class_add_private (class, sizeof (CamelOfflineStorePrivate));
69 object_class = G_OBJECT_CLASS (class);
70 object_class->constructed = offline_store_constructed;
72 service_class = CAMEL_SERVICE_CLASS (class);
73 service_class->settings_type = CAMEL_TYPE_OFFLINE_SETTINGS;
77 camel_offline_store_init (CamelOfflineStore *store)
79 store->priv = CAMEL_OFFLINE_STORE_GET_PRIVATE (store);
83 * camel_offline_store_get_online:
84 * @store: a #CamelOfflineStore
86 * Returns %TRUE if @store is online.
91 camel_offline_store_get_online (CamelOfflineStore *store)
93 g_return_val_if_fail (CAMEL_IS_OFFLINE_STORE (store), 0);
95 return store->priv->online;
99 * camel_offline_store_set_online_sync:
100 * @store: a #CamelOfflineStore
101 * @online: %TRUE for online, %FALSE for offline
102 * @cancellable: optional #GCancellable object, or %NULL
103 * @error: return location for a #GError, or %NULL
105 * Sets the online/offline state of @store according to @online.
108 camel_offline_store_set_online_sync (CamelOfflineStore *store,
110 GCancellable *cancellable,
113 CamelService *service;
114 CamelSession *session;
115 CamelSettings *settings;
116 gboolean network_available;
117 gboolean store_is_online;
121 g_return_val_if_fail (CAMEL_IS_OFFLINE_STORE (store), FALSE);
123 if (store->priv->online == online)
126 service = CAMEL_SERVICE (store);
127 session = camel_service_get_session (service);
129 network_available = camel_session_get_network_available (session);
130 store_is_online = camel_offline_store_get_online (store);
132 settings = camel_service_ref_settings (service);
134 sync_store = camel_offline_settings_get_stay_synchronized (
135 CAMEL_OFFLINE_SETTINGS (settings));
137 g_object_unref (settings);
139 /* Returning to online mode is the simpler case. */
140 if (!store_is_online) {
141 store->priv->online = online;
142 return camel_service_connect_sync (
143 service, cancellable, error);
146 /* network available -> network unavailable */
147 if (network_available) {
151 folders = camel_object_bag_list (
152 CAMEL_STORE (store)->folders);
154 for (ii = 0; ii < folders->len; ii++) {
155 CamelFolder *folder = folders->pdata[ii];
156 gboolean sync_folder;
158 if (!CAMEL_IS_OFFLINE_FOLDER (folder))
162 camel_offline_folder_get_offline_sync (
163 CAMEL_OFFLINE_FOLDER (folder));
165 if (sync_store || sync_folder)
166 camel_offline_folder_downsync_sync (
167 CAMEL_OFFLINE_FOLDER (folder),
168 NULL, cancellable, NULL);
171 g_ptr_array_foreach (folders, (GFunc) g_object_unref, NULL);
172 g_ptr_array_free (folders, TRUE);
174 camel_store_synchronize_sync (
175 CAMEL_STORE (store), FALSE, cancellable, NULL);
178 success = camel_service_disconnect_sync (
179 service, network_available, cancellable, error);
181 store->priv->online = online;
187 * camel_offline_store_prepare_for_offline_sync:
192 camel_offline_store_prepare_for_offline_sync (CamelOfflineStore *store,
193 GCancellable *cancellable,
196 CamelService *service;
197 CamelSession *session;
198 CamelSettings *settings;
199 gboolean network_available;
200 gboolean store_is_online;
203 g_return_val_if_fail (CAMEL_IS_OFFLINE_STORE (store), FALSE);
205 service = CAMEL_SERVICE (store);
206 session = camel_service_get_session (service);
208 network_available = camel_session_get_network_available (session);
209 store_is_online = camel_offline_store_get_online (store);
211 settings = camel_service_ref_settings (service);
213 sync_store = camel_offline_settings_get_stay_synchronized (
214 CAMEL_OFFLINE_SETTINGS (settings));
216 g_object_unref (settings);
218 if (network_available && store_is_online) {
222 folders = camel_object_bag_list (
223 CAMEL_STORE (store)->folders);
225 for (ii = 0; ii < folders->len; ii++) {
226 CamelFolder *folder = folders->pdata[ii];
227 gboolean sync_folder;
229 if (!CAMEL_IS_OFFLINE_FOLDER (folder))
233 camel_offline_folder_get_offline_sync (
234 CAMEL_OFFLINE_FOLDER (folder));
236 if (sync_store || sync_folder) {
237 camel_offline_folder_downsync_sync (
238 CAMEL_OFFLINE_FOLDER (folder),
239 NULL, cancellable, NULL);
243 g_ptr_array_foreach (folders, (GFunc) g_object_unref, NULL);
244 g_ptr_array_free (folders, TRUE);
247 if (network_available)
248 camel_store_synchronize_sync (
249 CAMEL_STORE (store), FALSE, cancellable, NULL);