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

« back to all changes in this revision

Viewing changes to libtinymail-maemo/tny-maemo-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-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., 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-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
        return;
 
143
}
 
144
 
 
145
 
 
146
/**
 
147
 * tny_maemo_device_new:
 
148
 *
 
149
 * Return value: A new #TnyDevice implemented for MAEMO
 
150
 **/
 
151
TnyDevice*
 
152
tny_maemo_device_new (void)
 
153
{
 
154
        TnyMaemoDevice *self = g_object_new (TNY_TYPE_MAEMO_DEVICE, NULL);
 
155
 
 
156
        return TNY_DEVICE (self);
 
157
}
 
158
 
 
159
 
 
160
static void
 
161
tny_maemo_device_finalize (GObject *object)
 
162
{
 
163
        (*parent_class->finalize) (object);
 
164
 
 
165
        return;
 
166
}
 
167
 
 
168
 
 
169
static void
 
170
tny_device_init (gpointer g, gpointer iface_data)
 
171
{
 
172
        TnyDeviceIface *klass = (TnyDeviceIface *)g;
 
173
 
 
174
        klass->is_online_func = tny_maemo_device_is_online;
 
175
        klass->reset_func = tny_maemo_device_reset;
 
176
        klass->force_offline_func = tny_maemo_device_force_offline;
 
177
        klass->force_online_func = tny_maemo_device_force_online;
 
178
 
 
179
        return;
 
180
}
 
181
 
 
182
 
 
183
 
 
184
static void 
 
185
tny_maemo_device_class_init (TnyMaemoDeviceClass *class)
 
186
{
 
187
        GObjectClass *object_class;
 
188
 
 
189
        parent_class = g_type_class_peek_parent (class);
 
190
        object_class = (GObjectClass*) class;
 
191
 
 
192
        object_class->finalize = tny_maemo_device_finalize;
 
193
 
 
194
        g_type_class_add_private (object_class, sizeof (TnyMaemoDevicePriv));
 
195
 
 
196
        return;
 
197
}
 
198
 
 
199
GType 
 
200
tny_maemo_device_get_type (void)
 
201
{
 
202
        static GType type = 0;
 
203
 
 
204
        if (G_UNLIKELY(type == 0))
 
205
        {
 
206
                static const GTypeInfo info = 
 
207
                {
 
208
                  sizeof (TnyMaemoDeviceClass),
 
209
                  NULL,   /* base_init */
 
210
                  NULL,   /* base_finalize */
 
211
                  (GClassInitFunc) tny_maemo_device_class_init,   /* class_init */
 
212
                  NULL,   /* class_finalize */
 
213
                  NULL,   /* class_data */
 
214
                  sizeof (TnyMaemoDevice),
 
215
                  0,      /* n_preallocs */
 
216
                  tny_maemo_device_instance_init    /* instance_init */
 
217
                };
 
218
 
 
219
                static const GInterfaceInfo tny_device_info = 
 
220
                {
 
221
                  (GInterfaceInitFunc) tny_device_init, /* interface_init */
 
222
                  NULL,         /* interface_finalize */
 
223
                  NULL          /* interface_data */
 
224
                };
 
225
 
 
226
                type = g_type_register_static (G_TYPE_OBJECT,
 
227
                        "TnyMaemoDevice",
 
228
                        &info, 0);
 
229
 
 
230
                g_type_add_interface_static (type, TNY_TYPE_DEVICE, 
 
231
                        &tny_device_info);
 
232
 
 
233
        }
 
234
 
 
235
        return type;
 
236
}
 
237