aboutsummaryrefslogtreecommitdiffstats
path: root/modules/connman/evolution-connman.c
blob: f6225df46c608639e247b61b6bf524afd946b53e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * evolution-connman.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gio/gio.h>

#include <shell/e-shell.h>
#include <e-util/e-extension.h>

#define CM_DBUS_SERVICE   "net.connman"
#define CM_DBUS_INTERFACE "net.connman.Manager"
#define CM_DBUS_PATH      "/"

/* Standard GObject macros */
#define E_TYPE_CONNMAN \
    (e_connman_get_type ())
#define E_CONNMAN(obj) \
    (G_TYPE_CHECK_INSTANCE_CAST \
    ((obj), E_TYPE_CONNMAN, EConnMan))

typedef struct {
    EExtension parent;
    GDBusConnection *connection;
} EConnMan;
typedef EExtensionClass EConnManClass;

/* Module Entry Points */
void e_module_load (GTypeModule *type_module);
void e_module_unload (GTypeModule *type_module);

/* Forward Declarations */
GType e_connman_get_type (void);
static gboolean connman_connect (EConnMan *extension);

G_DEFINE_DYNAMIC_TYPE (EConnMan, e_connman, E_TYPE_EXTENSION)

static void
extension_set_state (EConnMan *extension,
                     const gchar *state)
{
    EExtensible *extensible;
    gboolean network_available;

    extensible = e_extension_get_extensible (E_EXTENSION (extension));
    g_return_if_fail (E_IS_SHELL (extensible));

    network_available = (g_strcmp0 (state, "online") == 0);
    e_shell_set_network_available (E_SHELL (extensible), network_available);
}

static void
connman_connection_closed_cb (GDBusConnection *connection,
                              gboolean remote_peer_vanished,
                              GError *error,
                              EConnMan *extension)
{
    gboolean try_again;

    g_object_unref (extension->connection);
    extension->connection = NULL;

    /* Try connecting to the session bus immediately, and then
     * keep trying at 3 second intervals until we're back on. */

    try_again = connman_connect (extension);

    if (try_again)
        g_timeout_add_seconds (
            3, (GSourceFunc) connman_connect, extension);
}

static void
conn_manager_signal_cb (GDBusConnection *connection,
                        const gchar *sender_name,
                        const gchar *object_path,
                        const gchar *interface_name,
                        const gchar *signal_name,
                        GVariant *parameters,
                        gpointer user_data)
{
    EConnMan *extension = user_data;
    gchar *state = NULL;

    if (g_strcmp0 (interface_name, CM_DBUS_INTERFACE) != 0
        || g_strcmp0 (object_path, CM_DBUS_PATH) != 0
        || g_strcmp0 (signal_name, "StateChanged") != 0)
        return;

    g_variant_get (parameters, "(s)", &state);
    extension_set_state (extension, state);
    g_free (state);
}

static void
connman_check_initial_state (EConnMan *extension)
{
    GDBusMessage *message = NULL;
    GDBusMessage *response = NULL;
    GVariant *body;
    gchar *state = NULL;
    GError *error = NULL;

    message = g_dbus_message_new_method_call (
        CM_DBUS_SERVICE, CM_DBUS_PATH, CM_DBUS_INTERFACE, "GetState");

    /* XXX Assuming this should be safe to call synchronously. */
    response = g_dbus_connection_send_message_with_reply_sync (
        extension->connection, message,
        G_DBUS_SEND_MESSAGE_FLAGS_NONE, 100, NULL, NULL, &error);

    if (response != NULL) {
        if (g_dbus_message_to_gerror (response, &error)) {
            g_object_unref (response);
            response = NULL;
        }
    }

    g_object_unref (message);

    if (error != NULL) {
        g_warning ("%s: %s", G_STRFUNC, error->message);
        g_error_free (error);
        return;
    }

    g_return_if_fail (G_IS_DBUS_MESSAGE (response));

    body = g_dbus_message_get_body (response);
    g_variant_get (body, "(s)", &state);
    extension_set_state (extension, state);
    g_free (state);

    g_object_unref (response);
}

static gboolean
connman_connect (EConnMan *extension)
{
    GError *error = NULL;

    /* This is a timeout callback, so the return value denotes
     * whether to reschedule, not whether we're successful. */

    if (extension->connection != NULL)
        return FALSE;

    extension->connection =
        g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);

    if (error != NULL) {
        g_warning ("%s: %s", G_STRFUNC, error->message);
        g_error_free (error);
        return TRUE;
    }

    g_return_val_if_fail (
        G_IS_DBUS_CONNECTION (extension->connection), FALSE);

    g_dbus_connection_set_exit_on_close (extension->connection, FALSE);

    if (!g_dbus_connection_signal_subscribe (
        extension->connection,
        CM_DBUS_SERVICE,
        CM_DBUS_INTERFACE,
        NULL,
        CM_DBUS_PATH,
        NULL,
        G_DBUS_SIGNAL_FLAGS_NONE,
        conn_manager_signal_cb,
        extension,
        NULL)) {
        g_warning ("%s: Failed to subscribe for a signal", G_STRFUNC);
        goto fail;
    }

    g_signal_connect (
        extension->connection, "closed",
        G_CALLBACK (connman_connection_closed_cb), extension);

    connman_check_initial_state (extension);

    return FALSE;

fail:
    g_object_unref (extension->connection);
    extension->connection = NULL;

    return TRUE;
}

static void
connman_constructed (GObject *object)
{
    connman_connect (E_CONNMAN (object));

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_connman_parent_class)->constructed (object);
}

static void
e_connman_class_init (EConnManClass *class)
{
    GObjectClass *object_class;
    EExtensionClass *extension_class;

    object_class = G_OBJECT_CLASS (class);
    object_class->constructed = connman_constructed;

    extension_class = E_EXTENSION_CLASS (class);
    extension_class->extensible_type = E_TYPE_SHELL;
}

static void
e_connman_class_finalize (EConnManClass *class)
{
}

static void
e_connman_init (EConnMan *extension)
{
}

G_MODULE_EXPORT void
e_module_load (GTypeModule *type_module)
{
    e_connman_register_type (type_module);
}

G_MODULE_EXPORT void
e_module_unload (GTypeModule *type_module)
{
}