Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-offline-store.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@novell.com>
4  *
5  *  Copyright 2005 Novell, Inc. (www.novell.com)
6  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <glib.h>
29 #include <glib/gi18n-lib.h>
30
31 #include "camel-folder.h"
32 #include "camel-offline-folder.h"
33 #include "camel-offline-store.h"
34 #include "camel-session.h"
35
36
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);
40
41 static void offline_store_construct (CamelService *service, CamelSession *session,
42                                      CamelProvider *provider, CamelURL *url,
43                                      CamelException *ex);
44
45
46 static CamelStoreClass *parent_class = NULL;
47
48
49 CamelType
50 camel_offline_store_get_type (void)
51 {
52         static CamelType type = 0;
53         
54         if (!type) {
55                 type = camel_type_register (CAMEL_STORE_TYPE,
56                                             "CamelOfflineStore",
57                                             sizeof (CamelOfflineStore),
58                                             sizeof (CamelOfflineStoreClass),
59                                             (CamelObjectClassInitFunc) camel_offline_store_class_init,
60                                             NULL,
61                                             (CamelObjectInitFunc) camel_offline_store_init,
62                                             (CamelObjectFinalizeFunc) camel_offline_store_finalize);
63         }
64         
65         return type;
66 }
67
68
69 static void
70 camel_offline_store_class_init (CamelOfflineStoreClass *klass)
71 {
72         parent_class = (CamelStoreClass *) camel_type_get_global_classfuncs (CAMEL_STORE_TYPE);
73         
74         ((CamelServiceClass *) klass)->construct = offline_store_construct;
75 }
76
77 static void
78 camel_offline_store_init (CamelOfflineStore *store, CamelOfflineStoreClass *klass)
79 {
80         store->state = CAMEL_OFFLINE_STORE_NETWORK_AVAIL;
81 }
82
83 static void
84 camel_offline_store_finalize (CamelObject *object)
85 {
86         ;
87 }
88
89
90 static void
91 offline_store_construct (CamelService *service, CamelSession *session,
92                           CamelProvider *provider, CamelURL *url,
93                           CamelException *ex)
94 {
95         CamelOfflineStore *store = CAMEL_OFFLINE_STORE (service);
96         
97         CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
98         if (camel_exception_is_set (ex))
99                 return;
100         
101         store->state = camel_session_is_online (session) ?
102                 CAMEL_OFFLINE_STORE_NETWORK_AVAIL : CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL;
103 }
104
105
106 /**
107  * camel_offline_store_set_network_state:
108  * @store: a #CamelOfflineStore object
109  * @state: the network state
110  * @ex: a #CamelException
111  *
112  * Set the network state to either #CAMEL_OFFLINE_STORE_NETWORK_AVAIL
113  * or #CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL.
114  **/
115 void
116 camel_offline_store_set_network_state (CamelOfflineStore *store, int state, CamelException *ex)
117 {
118         CamelException lex;
119         CamelService *service = CAMEL_SERVICE (store);
120         gboolean network_state = camel_session_get_network_state (service->session);
121
122         if (store->state == state)
123                 return;
124         
125         camel_exception_init (&lex);
126         if (store->state == CAMEL_OFFLINE_STORE_NETWORK_AVAIL) {
127                 /* network available -> network unavailable */
128                 if (network_state) {
129                         if (((CamelStore *) store)->folders) {
130                                 GPtrArray *folders;
131                                 CamelFolder *folder;
132                                 int i, sync;
133                                 
134                                 sync = camel_url_get_param (((CamelService *) store)->url, "sync_offline") != NULL;
135                                 
136                                 folders = camel_object_bag_list (((CamelStore *) store)->folders);
137                                 for (i = 0; i < folders->len; i++) {
138                                         folder = folders->pdata[i];
139                                         
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);
144                                         }
145                                         
146                                         camel_object_unref (folder);
147                                 }
148                                 
149                                 g_ptr_array_free (folders, TRUE);
150                         }
151                         
152                         camel_store_sync (CAMEL_STORE (store), FALSE, &lex);
153                         camel_exception_clear (&lex);
154                 }
155
156                 if (!camel_service_disconnect (CAMEL_SERVICE (store), network_state, ex))
157                         return;
158         } else {
159                 /* network unavailable -> network available */
160                 if (!camel_service_connect (CAMEL_SERVICE (store), ex))
161                         return;
162         }
163         
164         store->state = state;
165 }