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

« back to all changes in this revision

Viewing changes to libtinymail-olpc/tny-olpc-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-olpc - The Tiny Mail base library for olpc
 
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-olpc-device.h>
 
25
 
 
26
static GObjectClass *parent_class = NULL;
 
27
 
 
28
#include "tny-olpc-device-priv.h"
 
29
 
 
30
static void tny_olpc_device_on_online (TnyDevice *self);
 
31
static void tny_olpc_device_on_offline (TnyDevice *self);
 
32
static gboolean tny_olpc_device_is_online (TnyDevice *self);
 
33
 
 
34
 
 
35
static gboolean
 
36
emit_status (TnyDevice *self)
 
37
{
 
38
        if (tny_olpc_device_is_online (self))
 
39
                tny_olpc_device_on_online (self);
 
40
        else
 
41
                tny_olpc_device_on_offline (self);
 
42
 
 
43
        return FALSE;
 
44
}
 
45
 
 
46
static void 
 
47
tny_olpc_device_reset (TnyDevice *self)
 
48
{
 
49
        TnyOlpcDevicePriv *priv = TNY_OLPC_DEVICE_GET_PRIVATE (self);
 
50
 
 
51
        const gboolean status_before = tny_olpc_device_is_online (self);
 
52
 
 
53
        priv->fset = FALSE;
 
54
        priv->forced = FALSE;
 
55
 
 
56
        /* Signal if it changed: */
 
57
        if (status_before != tny_olpc_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_olpc_device_force_online (TnyDevice *self)
 
66
{
 
67
 
 
68
        TnyOlpcDevicePriv *priv = TNY_OLPC_DEVICE_GET_PRIVATE (self);
 
69
 
 
70
        const gboolean already_online = tny_olpc_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_olpc_device_force_offline (TnyDevice *self)
 
88
{
 
89
        TnyOlpcDevicePriv *priv = TNY_OLPC_DEVICE_GET_PRIVATE (self);
 
90
 
 
91
        const gboolean already_offline = !tny_olpc_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_olpc_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_olpc_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_olpc_device_is_online (TnyDevice *self)
 
128
{
 
129
        TnyOlpcDevicePriv *priv = TNY_OLPC_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_olpc_device_instance_init (GTypeInstance *instance, gpointer g_class)
 
140
{
 
141
        TnyOlpcDevice *self = (TnyOlpcDevice *) instance;
 
142
        TnyOlpcDevicePriv *priv = TNY_OLPC_DEVICE_GET_PRIVATE (self);
 
143
 
 
144
        return;
 
145
}
 
146
 
 
147
 
 
148
/**
 
149
 * tny_olpc_device_new:
 
150
 *
 
151
 * Return value: A new #TnyDevice implemented for OLPC
 
152
 **/
 
153
TnyDevice*
 
154
tny_olpc_device_new (void)
 
155
{
 
156
        TnyOlpcDevice *self = g_object_new (TNY_TYPE_OLPC_DEVICE, NULL);
 
157
 
 
158
        return TNY_DEVICE (self);
 
159
}
 
160
 
 
161
 
 
162
static void
 
163
tny_olpc_device_finalize (GObject *object)
 
164
{
 
165
        TnyOlpcDevice *self = (TnyOlpcDevice *) object;
 
166
        TnyOlpcDevicePriv *priv = TNY_OLPC_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_func = tny_olpc_device_is_online;
 
180
        klass->reset_func = tny_olpc_device_reset;
 
181
        klass->force_offline_func = tny_olpc_device_force_offline;
 
182
        klass->force_online_func = tny_olpc_device_force_online;
 
183
 
 
184
        return;
 
185
}
 
186
 
 
187
 
 
188
 
 
189
static void 
 
190
tny_olpc_device_class_init (TnyOlpcDeviceClass *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_olpc_device_finalize;
 
198
 
 
199
        g_type_class_add_private (object_class, sizeof (TnyOlpcDevicePriv));
 
200
 
 
201
        return;
 
202
}
 
203
 
 
204
GType 
 
205
tny_olpc_device_get_type (void)
 
206
{
 
207
        static GType type = 0;
 
208
 
 
209
        if (G_UNLIKELY(type == 0))
 
210
        {
 
211
                static const GTypeInfo info = 
 
212
                {
 
213
                  sizeof (TnyOlpcDeviceClass),
 
214
                  NULL,   /* base_init */
 
215
                  NULL,   /* base_finalize */
 
216
                  (GClassInitFunc) tny_olpc_device_class_init,   /* class_init */
 
217
                  NULL,   /* class_finalize */
 
218
                  NULL,   /* class_data */
 
219
                  sizeof (TnyOlpcDevice),
 
220
                  0,      /* n_preallocs */
 
221
                  tny_olpc_device_instance_init    /* instance_init */
 
222
                };
 
223
 
 
224
                static const GInterfaceInfo tny_device_info = 
 
225
                {
 
226
                  (GInterfaceInitFunc) tny_device_init, /* interface_init */
 
227
                  NULL,         /* interface_finalize */
 
228
                  NULL          /* interface_data */
 
229
                };
 
230
 
 
231
                type = g_type_register_static (G_TYPE_OBJECT,
 
232
                        "TnyOlpcDevice",
 
233
                        &info, 0);
 
234
 
 
235
                g_type_add_interface_static (type, TNY_TYPE_DEVICE, 
 
236
                        &tny_device_info);
 
237
 
 
238
        }
 
239
 
 
240
        return type;
 
241
}
 
242