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

Revision 3666 (checked in by jdapena, 6 months ago)

* Use GOnce registering all types in tinymail to make

registering thread-safe.

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