Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / src / offline-listener.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* server-interface-check.h
3  *
4  * Copyright (C) 2004  Novell, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * Author: Sivaiah Nallagatla <snallagatla@novell.com>
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "offline-listener.h"
28 #include <libedata-book/e-data-book-factory.h>
29 #if ENABLE_CALENDAR
30 #include <libedata-cal/e-data-cal-factory.h>
31 #endif
32 #include <gconf/gconf-client.h>
33
34 enum {
35
36         OFFLINE_MODE=1,
37         ONLINE_MODE
38 };
39
40 static GObjectClass *parent_class = NULL;
41
42 struct _OfflineListenerPrivate 
43 {
44         GConfClient *default_client;
45
46 #if ENABLE_CALENDAR
47         EDataCalFactory *cal_factory;
48 #endif
49         EDataBookFactory *book_factory;
50         gboolean is_offline_now;
51 };
52
53
54 static void 
55 set_online_status (OfflineListener *offline_listener, gboolean is_offline)
56 {
57         OfflineListenerPrivate *priv;
58         
59         priv = offline_listener->priv;
60
61 #if ENABLE_CALENDAR
62         e_data_cal_factory_set_backend_mode
63                 (priv->cal_factory, is_offline ? OFFLINE_MODE : ONLINE_MODE);
64 #endif
65         e_data_book_factory_set_backend_mode
66                 (priv->book_factory, is_offline ? OFFLINE_MODE : ONLINE_MODE);
67 }
68
69 static void 
70 online_status_changed (GConfClient *client, int cnxn_id, GConfEntry *entry, gpointer data)
71 {
72         GConfValue *value;
73         gboolean offline;
74         OfflineListener *offline_listener;
75         OfflineListenerPrivate *priv;
76
77         offline_listener = OFFLINE_LISTENER(data);
78         priv = offline_listener->priv;
79         offline = FALSE;
80         value = gconf_entry_get_value (entry);
81         if (value)
82                 offline = gconf_value_get_bool (value);
83         if (priv->is_offline_now != offline) {
84                 priv->is_offline_now = offline;
85                 set_online_status (offline_listener ,offline);
86         }
87         
88 }
89
90
91 static void 
92 setup_offline_listener (OfflineListener *offline_listener)
93 {
94         OfflineListenerPrivate *priv = offline_listener->priv;
95         
96         priv->default_client = gconf_client_get_default ();
97         gconf_client_add_dir (priv->default_client, "/apps/evolution/shell", GCONF_CLIENT_PRELOAD_RECURSIVE,NULL);
98         gconf_client_notify_add (priv->default_client, "/apps/evolution/shell/start_offline", (GConfClientNotifyFunc)online_status_changed, offline_listener, NULL, NULL);
99         priv->is_offline_now = gconf_client_get_bool (priv->default_client, "/apps/evolution/shell/start_offline", NULL);
100         set_online_status (offline_listener, priv->is_offline_now); 
101 }
102
103 #if ENABLE_CALENDAR
104 OfflineListener*
105 offline_listener_new (EDataBookFactory *book_factory, EDataCalFactory *cal_factory)
106 #else
107 OfflineListener*
108 offline_listener_new (EDataBookFactory *book_factory)
109 #endif
110 {
111         OfflineListener *offline_listener = g_object_new (OFFLINE_TYPE_LISTENER, NULL);
112         OfflineListenerPrivate *priv = offline_listener->priv;
113         
114         priv->book_factory = book_factory;
115 #if ENABLE_CALENDAR
116         priv->cal_factory = cal_factory;
117 #endif
118         setup_offline_listener (offline_listener);
119         return offline_listener;
120
121 }
122
123
124 static void
125 offline_listener_dispose (GObject *object)
126 {
127         OfflineListener *offline_listener = OFFLINE_LISTENER (object);
128         if (offline_listener->priv->default_client) {
129                 g_object_unref (offline_listener->priv->default_client);
130                 offline_listener->priv->default_client = NULL;
131         }
132         (* G_OBJECT_CLASS (parent_class)->dispose) (object);
133 }
134
135 static void
136 offline_listener_finalize (GObject *object)
137 {
138         OfflineListener *offline_listener;
139         OfflineListenerPrivate *priv;
140
141         offline_listener = OFFLINE_LISTENER (object);
142         priv = offline_listener->priv;
143         
144         g_free (priv);
145         offline_listener->priv = NULL;
146         
147         parent_class->finalize (object);
148 }
149
150 static void
151 offline_listener_init (OfflineListener *listener)
152 {
153         OfflineListenerPrivate *priv;
154         
155         priv =g_new0 (OfflineListenerPrivate, 1);
156         listener->priv = priv;
157         
158 }
159
160
161
162 static void
163 offline_listener_class_init (OfflineListener *klass)
164 {
165         GObjectClass *object_class;
166
167         parent_class = g_type_class_peek_parent (klass);
168
169         object_class = G_OBJECT_CLASS (klass);
170         object_class->dispose = offline_listener_dispose;
171         object_class->finalize = offline_listener_finalize;
172         
173         
174
175 }
176
177
178 GType
179 offline_listener_get_type (void)
180 {
181         static GType type = 0;
182
183         if (!type) {
184                 static GTypeInfo info = {
185                         sizeof (OfflineListenerClass),
186                         (GBaseInitFunc) NULL,
187                         (GBaseFinalizeFunc) NULL,
188                         (GClassInitFunc) offline_listener_class_init,
189                         NULL, NULL,
190                         sizeof (OfflineListener),
191                         0,
192                         (GInstanceInitFunc) offline_listener_init,
193                 };
194                 type = g_type_register_static (G_TYPE_OBJECT, "OfflineListener", &info, 0);
195         }
196
197         return type;
198 }