~ubuntu-branches/ubuntu/karmic/libtinymail/karmic

« back to all changes in this revision

Viewing changes to libtinymail-gnome-desktop/tny-gnome-device.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-10-12 11:21:12 UTC
  • Revision ID: james.westby@ubuntu.com-20071012112112-fod9fs7yrooxjr7i
Tags: upstream-0.0.2
ImportĀ upstreamĀ versionĀ 0.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* libtinymail-camel - The Tiny Mail base library for Camel
 
2
 * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with self library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <glib/gi18n-lib.h>
 
23
 
 
24
#include <tny-gnome-device.h>
 
25
 
 
26
/*
 
27
#ifdef GNOME
 
28
#undef GNOME
 
29
#endif
 
30
*/
 
31
 
 
32
#ifdef GNOME
 
33
#include <libnm_glib.h>
 
34
#endif
 
35
 
 
36
static GObjectClass *parent_class = NULL;
 
37
 
 
38
#include "tny-gnome-device-priv.h"
 
39
 
 
40
static void tny_gnome_device_on_online (TnyDevice *self);
 
41
static void tny_gnome_device_on_offline (TnyDevice *self);
 
42
static gboolean tny_gnome_device_is_online (TnyDevice *self);
 
43
 
 
44
static gboolean
 
45
emit_status (TnyDevice *self)
 
46
{
 
47
        if (tny_gnome_device_is_online (self))
 
48
                tny_gnome_device_on_online (self);
 
49
        else
 
50
                tny_gnome_device_on_offline (self);
 
51
 
 
52
        return FALSE;
 
53
}
 
54
 
 
55
 
 
56
#ifdef GNOME
 
57
static void 
 
58
nm_callback (libnm_glib_ctx *nm_ctx, gpointer user_data)
 
59
{
 
60
        TnyDevice *self = (TnyDevice *)user_data;
 
61
 
 
62
        emit_status (self);
 
63
 
 
64
        return;
 
65
}
 
66
#endif
 
67
 
 
68
static void 
 
69
tny_gnome_device_reset (TnyDevice *self)
 
70
{
 
71
        TnyGnomeDevicePriv *priv = TNY_GNOME_DEVICE_GET_PRIVATE (self);
 
72
 
 
73
        const gboolean status_before = tny_gnome_device_is_online (self);
 
74
 
 
75
        priv->fset = FALSE;
 
76
        priv->forced = FALSE;
 
77
 
 
78
        /* Signal if it changed: */
 
79
        if (status_before != tny_gnome_device_is_online (self))
 
80
                g_idle_add_full (G_PRIORITY_DEFAULT, 
 
81
                        (GSourceFunc) emit_status, 
 
82
                        g_object_ref (self), 
 
83
                        (GDestroyNotify) g_object_unref);
 
84
 
 
85
        return;
 
86
}
 
87
 
 
88
static void 
 
89
tny_gnome_device_force_online (TnyDevice *self)
 
90
{
 
91
        TnyGnomeDevicePriv *priv = TNY_GNOME_DEVICE_GET_PRIVATE (self);
 
92
 
 
93
        const gboolean already_online = tny_gnome_device_is_online (self);
 
94
 
 
95
        priv->fset = TRUE;
 
96
        priv->forced = TRUE;
 
97
 
 
98
        /* Signal if it changed: */
 
99
        if (!already_online)
 
100
                g_idle_add_full (G_PRIORITY_DEFAULT, 
 
101
                        (GSourceFunc) emit_status, 
 
102
                        g_object_ref (self), 
 
103
                        (GDestroyNotify) g_object_unref);
 
104
 
 
105
        return;
 
106
}
 
107
 
 
108
 
 
109
static void
 
110
tny_gnome_device_force_offline (TnyDevice *self)
 
111
{
 
112
        TnyGnomeDevicePriv *priv = TNY_GNOME_DEVICE_GET_PRIVATE (self);
 
113
 
 
114
        const gboolean already_offline = !tny_gnome_device_is_online (self);
 
115
 
 
116
        priv->fset = TRUE;
 
117
        priv->forced = FALSE;
 
118
 
 
119
        /* Signal if it changed: */
 
120
        if (!already_offline)
 
121
                g_idle_add_full (G_PRIORITY_DEFAULT, 
 
122
                        (GSourceFunc) emit_status, 
 
123
                        g_object_ref (self), 
 
124
                        (GDestroyNotify) g_object_unref);
 
125
 
 
126
        return;
 
127
}
 
128
 
 
129
static void
 
130
tny_gnome_device_on_online (TnyDevice *self)
 
131
{
 
132
        gdk_threads_enter ();
 
133
        g_signal_emit (self, tny_device_signals [TNY_DEVICE_CONNECTION_CHANGED], 0, TRUE);
 
134
        gdk_threads_leave ();
 
135
 
 
136
        return;
 
137
}
 
138
 
 
139
static void
 
140
tny_gnome_device_on_offline (TnyDevice *self)
 
141
{
 
142
        gdk_threads_enter ();
 
143
        g_signal_emit (self, tny_device_signals [TNY_DEVICE_CONNECTION_CHANGED], 0, FALSE);
 
144
        gdk_threads_leave ();
 
145
 
 
146
        return;
 
147
}
 
148
 
 
149
static gboolean
 
150
tny_gnome_device_is_online (TnyDevice *self)
 
151
{
 
152
        TnyGnomeDevicePriv *priv = TNY_GNOME_DEVICE_GET_PRIVATE (self);
 
153
        gboolean retval = priv->forced;
 
154
 
 
155
#ifdef GNOME
 
156
        if (!priv->fset && !priv->invnm)
 
157
        {
 
158
                libnm_glib_state state = libnm_glib_get_network_state (priv->nm_ctx);
 
159
                
 
160
                switch (state)
 
161
                {
 
162
                        case LIBNM_ACTIVE_NETWORK_CONNECTION:
 
163
                        retval = TRUE;
 
164
                        break;
 
165
 
 
166
                        case LIBNM_NO_DBUS:
 
167
                        case LIBNM_NO_NETWORKMANAGER:
 
168
                        case LIBNM_INVALID_CONTEXT:
 
169
                        g_print (_("Invalid network manager installation. Going to assume Offline status\n"));
 
170
                        priv->invnm = TRUE;
 
171
 
 
172
                        libnm_glib_unregister_callback (priv->nm_ctx, priv->callback_id);
 
173
                        libnm_glib_shutdown (priv->nm_ctx);
 
174
 
 
175
                        case LIBNM_NO_NETWORK_CONNECTION:
 
176
                        default:
 
177
                        retval = FALSE;
 
178
                        break;
 
179
                }
 
180
        }
 
181
#endif
 
182
 
 
183
        return retval;
 
184
}
 
185
 
 
186
/* #define IMMEDIATE_ONLINE_TEST */
 
187
 
 
188
static void
 
189
tny_gnome_device_instance_init (GTypeInstance *instance, gpointer g_class)
 
190
{
 
191
        TnyGnomeDevice *self = (TnyGnomeDevice *)instance;
 
192
        TnyGnomeDevicePriv *priv = TNY_GNOME_DEVICE_GET_PRIVATE (self);
 
193
 
 
194
        priv->fset = FALSE;
 
195
        priv->forced = FALSE;
 
196
 
 
197
#ifdef IMMEDIATE_ONLINE_TEST
 
198
        priv->fset = TRUE;
 
199
        priv->forced = TRUE;
 
200
#endif
 
201
 
 
202
#ifdef GNOME
 
203
        priv->invnm = FALSE;
 
204
        priv->nm_ctx = libnm_glib_init ();
 
205
#ifndef IMMEDIATE_ONLINE_TEST
 
206
        priv->callback_id = libnm_glib_register_callback 
 
207
                (priv->nm_ctx, nm_callback, self, NULL);
 
208
#endif
 
209
#endif
 
210
 
 
211
        return;
 
212
}
 
213
 
 
214
 
 
215
 
 
216
/**
 
217
 * tny_gnome_device_new:
 
218
 *
 
219
 * Create a #TnyDevice for GNOME desktops. If available, it uses NetworkManager
 
220
 * to know about the network status and changes of your computer.
 
221
 * 
 
222
 * Return value: A new #TnyDevice instance
 
223
 **/
 
224
TnyDevice*
 
225
tny_gnome_device_new (void)
 
226
{
 
227
        TnyGnomeDevice *self = g_object_new (TNY_TYPE_GNOME_DEVICE, NULL);
 
228
 
 
229
        return TNY_DEVICE (self);
 
230
}
 
231
 
 
232
 
 
233
static void
 
234
tny_gnome_device_finalize (GObject *object)
 
235
{
 
236
        TnyGnomeDevice *self = (TnyGnomeDevice *)object;        
 
237
        TnyGnomeDevicePriv *priv = TNY_GNOME_DEVICE_GET_PRIVATE (self);
 
238
 
 
239
#ifdef GNOME
 
240
        if (!priv->invnm)
 
241
        {
 
242
                libnm_glib_unregister_callback (priv->nm_ctx, priv->callback_id);
 
243
                libnm_glib_shutdown (priv->nm_ctx);
 
244
        }
 
245
#endif
 
246
 
 
247
        (*parent_class->finalize) (object);
 
248
 
 
249
        return;
 
250
}
 
251
 
 
252
 
 
253
static void
 
254
tny_device_init (gpointer g, gpointer iface_data)
 
255
{
 
256
        TnyDeviceIface *klass = (TnyDeviceIface *)g;
 
257
 
 
258
        klass->is_online_func = tny_gnome_device_is_online;
 
259
        klass->reset_func = tny_gnome_device_reset;
 
260
        klass->force_offline_func = tny_gnome_device_force_offline;
 
261
        klass->force_online_func = tny_gnome_device_force_online;
 
262
 
 
263
        return;
 
264
}
 
265
 
 
266
 
 
267
 
 
268
static void 
 
269
tny_gnome_device_class_init (TnyGnomeDeviceClass *class)
 
270
{
 
271
        GObjectClass *object_class;
 
272
 
 
273
        parent_class = g_type_class_peek_parent (class);
 
274
        object_class = (GObjectClass*) class;
 
275
 
 
276
        object_class->finalize = tny_gnome_device_finalize;
 
277
 
 
278
        g_type_class_add_private (object_class, sizeof (TnyGnomeDevicePriv));
 
279
 
 
280
        return;
 
281
}
 
282
 
 
283
GType 
 
284
tny_gnome_device_get_type (void)
 
285
{
 
286
        static GType type = 0;
 
287
 
 
288
        if (G_UNLIKELY(type == 0))
 
289
        {
 
290
                static const GTypeInfo info = 
 
291
                {
 
292
                  sizeof (TnyGnomeDeviceClass),
 
293
                  NULL,   /* base_init */
 
294
                  NULL,   /* base_finalize */
 
295
                  (GClassInitFunc) tny_gnome_device_class_init,   /* class_init */
 
296
                  NULL,   /* class_finalize */
 
297
                  NULL,   /* class_data */
 
298
                  sizeof (TnyGnomeDevice),
 
299
                  0,      /* n_preallocs */
 
300
                  tny_gnome_device_instance_init    /* instance_init */
 
301
                };
 
302
 
 
303
                static const GInterfaceInfo tny_device_info = 
 
304
                {
 
305
                  (GInterfaceInitFunc) tny_device_init, /* interface_init */
 
306
                  NULL,         /* interface_finalize */
 
307
                  NULL          /* interface_data */
 
308
                };
 
309
 
 
310
                type = g_type_register_static (G_TYPE_OBJECT,
 
311
                        "TnyGnomeDevice",
 
312
                        &info, 0);
 
313
 
 
314
                g_type_add_interface_static (type, TNY_TYPE_DEVICE, 
 
315
                        &tny_device_info);
 
316
 
 
317
        }
 
318
 
 
319
        return type;
 
320
}
 
321