root/trunk/libtinymail-maemo/tny-maemo-device.c

Revision 3823 (checked in by svillar, 2 weeks ago)

Some misc fixes

Line 
1 /* libtinymail-maemo - The Tiny Mail base library for maemo
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., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <config.h>
21
22 #include <glib/gi18n-lib.h>
23
24 #include <tny-maemo-device.h>
25 #include <gdk/gdk.h>
26
27 static GObjectClass *parent_class = NULL;
28
29 #include "tny-maemo-device-priv.h"
30
31 static void tny_maemo_device_on_online (TnyDevice *self);
32 static void tny_maemo_device_on_offline (TnyDevice *self);
33 static gboolean tny_maemo_device_is_online (TnyDevice *self);
34
35
36 static gboolean
37 emit_status (TnyDevice *self)
38 {
39         if (tny_maemo_device_is_online (self))
40                 tny_maemo_device_on_online (self);
41         else
42                 tny_maemo_device_on_offline (self);
43
44         return FALSE;
45 }
46
47 static void
48 tny_maemo_device_reset (TnyDevice *self)
49 {
50         TnyMaemoDevicePriv *priv = TNY_MAEMO_DEVICE_GET_PRIVATE (self);
51
52         const gboolean status_before = tny_maemo_device_is_online (self);
53
54         priv->fset = FALSE;
55         priv->forced = FALSE;
56
57         /* Signal if it changed: */
58         if (status_before != tny_maemo_device_is_online (self))
59                 g_idle_add_full (G_PRIORITY_DEFAULT,
60                         (GSourceFunc) emit_status,
61                         g_object_ref (self),
62                         (GDestroyNotify) g_object_unref);
63 }
64
65 static void
66 tny_maemo_device_force_online (TnyDevice *self)
67 {
68
69         TnyMaemoDevicePriv *priv = TNY_MAEMO_DEVICE_GET_PRIVATE (self);
70
71         const gboolean already_online = tny_maemo_device_is_online (self);
72
73         priv->fset = TRUE;
74         priv->forced = TRUE;
75
76         /* Signal if it changed: */
77         if (!already_online)
78                 g_idle_add_full (G_PRIORITY_DEFAULT,
79                         (GSourceFunc) emit_status,
80                         g_object_ref (self),
81                         (GDestroyNotify) g_object_unref);
82
83         return;
84 }
85
86
87 static void
88 tny_maemo_device_force_offline (TnyDevice *self)
89 {
90         TnyMaemoDevicePriv *priv = TNY_MAEMO_DEVICE_GET_PRIVATE (self);
91
92         const gboolean already_offline = !tny_maemo_device_is_online (self);
93
94         priv->fset = TRUE;
95         priv->forced = FALSE;
96
97         /* Signal if it changed: */
98         if (!already_offline)
99                 g_idle_add_full (G_PRIORITY_DEFAULT,
100                         (GSourceFunc) emit_status,
101                         g_object_ref (self),
102                         (GDestroyNotify) g_object_unref);
103
104         return;
105 }
106
107 static void
108 tny_maemo_device_on_online (TnyDevice *self)
109 {
110         gdk_threads_enter ();
111         g_signal_emit (self, tny_device_signals [TNY_DEVICE_CONNECTION_CHANGED], 0, TRUE);
112         gdk_threads_leave ();
113
114         return;
115 }
116
117 static void
118 tny_maemo_device_on_offline (TnyDevice *self)
119 {
120         gdk_threads_enter ();
121         g_signal_emit (self, tny_device_signals [TNY_DEVICE_CONNECTION_CHANGED], 0, FALSE);
122         gdk_threads_leave ();
123
124         return;
125 }
126
127 static gboolean
128 tny_maemo_device_is_online (TnyDevice *self)
129 {
130         TnyMaemoDevicePriv *priv = TNY_MAEMO_DEVICE_GET_PRIVATE (self);
131         gboolean retval = FALSE;
132
133         if (priv->fset)
134                 retval = priv->forced;
135
136         return retval;
137 }
138
139 static void
140 tny_maemo_device_instance_init (GTypeInstance *instance, gpointer g_class)
141 {
142         TnyMaemoDevicePriv *priv = TNY_MAEMO_DEVICE_GET_PRIVATE (instance);
143
144         priv->forced = FALSE;
145         priv->fset = FALSE;
146 }
147
148
149 /**
150  * tny_maemo_device_new:
151  *
152  * Return value: A new #TnyDevice implemented for MAEMO
153  **/
154 TnyDevice*
155 tny_maemo_device_new (void)
156 {
157         TnyMaemoDevice *self = g_object_new (TNY_TYPE_MAEMO_DEVICE, NULL);
158
159         return TNY_DEVICE (self);
160 }
161
162
163 static void
164 tny_maemo_device_finalize (GObject *object)
165 {
166         (*parent_class->finalize) (object);
167
168         return;
169 }
170
171
172 static void
173 tny_device_init (gpointer g, gpointer iface_data)
174 {
175         TnyDeviceIface *klass = (TnyDeviceIface *)g;
176
177         klass->is_online= tny_maemo_device_is_online;
178         klass->reset= tny_maemo_device_reset;
179         klass->force_offline= tny_maemo_device_force_offline;
180         klass->force_online= tny_maemo_device_force_online;
181
182         return;
183 }
184
185
186
187 static void
188 tny_maemo_device_class_init (TnyMaemoDeviceClass *class)
189 {
190         GObjectClass *object_class;
191
192         parent_class = g_type_class_peek_parent (class);
193         object_class = (GObjectClass*) class;
194
195         object_class->finalize = tny_maemo_device_finalize;
196
197         g_type_class_add_private (object_class, sizeof (TnyMaemoDevicePriv));
198
199         return;
200 }
201
202 static gpointer
203 tny_maemo_device_register_type (gpointer notused)
204 {
205         GType type = 0;
206
207         static const GTypeInfo info =
208                 {
209                         sizeof (TnyMaemoDeviceClass),
210                         NULL,   /* base_init */
211                         NULL,   /* base_finalize */
212                         (GClassInitFunc) tny_maemo_device_class_init,   /* class_init */
213                         NULL,   /* class_finalize */
214                         NULL,   /* class_data */
215                         sizeof (TnyMaemoDevice),
216                         0,      /* n_preallocs */
217                         tny_maemo_device_instance_init    /* instance_init */
218                 };
219        
220         static const GInterfaceInfo tny_device_info =
221                 {
222                         (GInterfaceInitFunc) tny_device_init, /* interface_init */
223                         NULL,         /* interface_finalize */
224                         NULL          /* interface_data */
225                 };
226        
227         type = g_type_register_static (G_TYPE_OBJECT,
228                                        "TnyMaemoDevice",
229                                        &info, 0);
230        
231         g_type_add_interface_static (type, TNY_TYPE_DEVICE,
232                                      &tny_device_info);
233        
234         return GUINT_TO_POINTER (type);
235 }
236
237 GType
238 tny_maemo_device_get_type (void)
239 {
240         static GOnce once = G_ONCE_INIT;
241         g_once (&once, tny_maemo_device_register_type, NULL);
242         return GPOINTER_TO_UINT (once.retval);
243 }
Note: See TracBrowser for help on using the browser.