aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-net-monitor.c
blob: a6fb56fd9ba3b817fa81eb5021d04021f1761bf9 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 *  Copyright © 2005, 2006 Jean-François Rameau
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include "ephy-net-monitor.h"

#include "eel-gconf-extensions.h"
#include "ephy-dbus.h"
#include "ephy-debug.h"
#include "ephy-prefs.h"

#include <NetworkManager.h>

#include <gmodule.h>

typedef enum 
{
    NETWORK_UP,
    NETWORK_DOWN
} NetworkStatus;

#define EPHY_NET_MONITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NET_MONITOR, EphyNetMonitorPrivate))

struct _EphyNetMonitorPrivate
{
    DBusConnection *bus;
    guint notify_id;
    guint active : 1;
    NetworkStatus status;
};

enum
{
    PROP_0,
    PROP_NETWORK_STATUS
};

G_DEFINE_TYPE (EphyNetMonitor, ephy_net_monitor, G_TYPE_OBJECT)

static void
ephy_net_monitor_set_net_status (EphyNetMonitor *monitor,
                 NetworkStatus status)
{
    EphyNetMonitorPrivate *priv = monitor->priv;

    LOG ("EphyNetMonitor turning Epiphany to %s mode",
         status != NETWORK_DOWN ? "online" : "offline");

    priv->status = status;

    g_object_notify (G_OBJECT (monitor), "network-status");
}

static void 
ephy_net_monitor_dbus_notify (DBusPendingCall *pending,
                              EphyNetMonitor *monitor) 
{
    DBusMessage* msg = dbus_pending_call_steal_reply (pending);
    
    LOG ("EphyNetMonitor getting response from dbus");

    if (!msg) return;

    if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_RETURN) 
    {
        dbus_uint32_t result;

        if (dbus_message_get_args (msg, NULL, DBUS_TYPE_UINT32, &result,
                      DBUS_TYPE_INVALID)) 
        {
            NetworkStatus net_status;

            net_status = result == NM_STATE_CONNECTED ? NETWORK_UP : NETWORK_DOWN;

            LOG ("EphyNetMonitor guesses the network is %s", 
                    net_status != NETWORK_DOWN ? "up" : "down");

            ephy_net_monitor_set_net_status (monitor, net_status);
        }
        dbus_message_unref (msg);
    }
}

/* This is the heart of Net Monitor monitor */
/* ATM, if there is an active device, we say that network is up: that's all ! */
static void
ephy_net_monitor_check_network (EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv = monitor->priv;
    DBusMessage *message;
    DBusPendingCall* reply;

    g_return_if_fail (priv != NULL && priv->bus != NULL);

    LOG ("EphyNetMonitor checking network");

    /* ask to Network Manager if there is at least one active device */
    message = dbus_message_new_method_call (NM_DBUS_SERVICE, 
                        NM_DBUS_PATH, 
                        NM_DBUS_INTERFACE, 
                        "state");

    if (message == NULL)
    {
        g_warning ("Couldn't allocate the dbus message");
        /* fallback: let's Epiphany roll */
        return;
    }

    if (dbus_connection_send_with_reply (priv->bus, message, &reply, -1)) 
    {
        dbus_pending_call_set_notify (reply,
                          (DBusPendingCallNotifyFunction)ephy_net_monitor_dbus_notify,
                          monitor, NULL);
        dbus_pending_call_unref (reply);
    }
    
    dbus_message_unref (message);
}

/* Filters all the messages from Network Manager */
static DBusHandlerResult
filter_func (DBusConnection *connection,
         DBusMessage *message,
         void *user_data)
{
    EphyNetMonitor *monitor;

    g_return_val_if_fail (user_data != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);

    monitor = EPHY_NET_MONITOR (user_data);

    if (dbus_message_is_signal (message,
                    NM_DBUS_INTERFACE,
                    "StateChange"))
    {
        LOG ("EphyNetMonitor catches StateChange signal");

        ephy_net_monitor_check_network (monitor);

        return DBUS_HANDLER_RESULT_HANDLED;
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static void
ephy_net_monitor_attach_to_dbus (EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv = monitor->priv;
    DBusError error;
    EphyDbus *dbus;
    DBusGConnection *g_connection;
    
    LOG ("EphyNetMonitor is trying to attach to SYSTEM bus");

    dbus = ephy_dbus_get_default ();

    g_connection = ephy_dbus_get_bus (dbus, EPHY_DBUS_SYSTEM);
    if (g_connection == NULL) return;
    
    priv->bus = dbus_g_connection_get_connection (g_connection);

    if (priv->bus != NULL)
    {
        dbus_connection_add_filter (priv->bus, 
                        filter_func, 
                        monitor,
                        NULL);
        dbus_error_init (&error);
        dbus_bus_add_match (priv->bus, 
                    "type='signal',interface='" NM_DBUS_INTERFACE "'", 
                    &error);
        if (dbus_error_is_set(&error)) 
        {
            g_warning("EphyNetMonitor cannot register signal handler: %s: %s", 
                  error.name, error.message);
            dbus_error_free(&error);
        }
        LOG ("EphyNetMonitor attached to SYSTEM bus");
    }
}

static void
ephy_net_monitor_detach_from_dbus (EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv = monitor->priv;
    
    LOG ("EphyNetMonitor is trying to detach from SYSTEM bus");

    if (priv->bus != NULL)
    {
        dbus_connection_remove_filter (priv->bus, 
                           filter_func, 
                           monitor);
        priv->bus = NULL;
    }
}


static void
connect_to_system_bus_cb (EphyDbus *dbus,
              EphyDbusBus kind,
              EphyNetMonitor *monitor)
{
    if (kind == EPHY_DBUS_SYSTEM)
    {
        LOG ("EphyNetMonitor connecting to SYSTEM bus");

        ephy_net_monitor_attach_to_dbus (monitor);
    }
}

static void
disconnect_from_system_bus_cb (EphyDbus *dbus,
                   EphyDbusBus kind,
                   EphyNetMonitor *monitor)
{
    if (kind == EPHY_DBUS_SYSTEM)
    {
        LOG ("EphyNetMonitor disconnected from SYSTEM bus");

        /* no bus anymore */
        ephy_net_monitor_detach_from_dbus (monitor);
    }
}

static void
ephy_net_monitor_startup (EphyNetMonitor *monitor)
{
    EphyDbus *dbus;

    dbus = ephy_dbus_get_default ();

    LOG ("EphyNetMonitor starting up");

    ephy_net_monitor_attach_to_dbus (monitor);

    if (monitor->priv->bus != NULL)
        {
        /* DBUS may disconnect us at any time. So listen carefully to it */
        g_signal_connect (dbus, "connected",  
                  G_CALLBACK (connect_to_system_bus_cb), monitor);
        g_signal_connect (dbus, "disconnected",  
                  G_CALLBACK (disconnect_from_system_bus_cb), monitor);

        ephy_net_monitor_check_network (monitor);
    }
}

static void
ephy_net_monitor_shutdown (EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv = monitor->priv;
    EphyDbus *dbus;

    dbus = ephy_dbus_get_default ();
            
    ephy_net_monitor_detach_from_dbus (monitor);
    g_signal_handlers_disconnect_by_func
        (dbus, G_CALLBACK (connect_to_system_bus_cb), monitor);
    g_signal_handlers_disconnect_by_func
        (dbus,G_CALLBACK (disconnect_from_system_bus_cb), monitor);
        
    priv->bus = NULL;

    LOG ("EphyNetMonitor shutdown");
}

static void
notify_network_managed_cb (GConfClient *client,
               guint cnxn_id,
               GConfEntry *entry,
               EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv = monitor->priv;
    GConfValue *value;
    gboolean active = TRUE;

    LOG (CONF_NETWORK_MANAGED " key changed");

    g_assert (entry != NULL);

    value = gconf_entry_get_value (entry);
    if (value != NULL && value->type == GCONF_VALUE_BOOL)
    {
        active = gconf_value_get_bool (value);
    }

    priv->active = active;

    g_object_notify (G_OBJECT (monitor), "network-status");
}

static void
ephy_net_monitor_init (EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv;

    priv = monitor->priv = EPHY_NET_MONITOR_GET_PRIVATE (monitor);

    LOG ("EphyNetMonitor initialising");

    priv->status = NETWORK_UP;

    priv->notify_id = eel_gconf_notification_add
        (CONF_NETWORK_MANAGED,
         (GConfClientNotifyFunc) notify_network_managed_cb,
         monitor);
    eel_gconf_notify (CONF_NETWORK_MANAGED);

    ephy_net_monitor_startup (monitor);
}

static void
ephy_net_monitor_dispose (GObject *object)
{
    EphyNetMonitor *monitor = EPHY_NET_MONITOR (object);
    EphyNetMonitorPrivate *priv = monitor->priv;

    LOG ("EphyNetMonitor finalising");

    ephy_net_monitor_shutdown (monitor);

    if (priv->notify_id != 0)
    {
        eel_gconf_notification_remove (priv->notify_id);
        priv->notify_id = 0;
    }

    G_OBJECT_CLASS (ephy_net_monitor_parent_class)->dispose (object);
}

static void
ephy_net_monitor_get_property (GObject *object,
                   guint prop_id,
                   GValue *value,
                   GParamSpec *pspec)
{
    EphyNetMonitor *monitor = EPHY_NET_MONITOR (object);

    switch (prop_id)
    {
        case PROP_NETWORK_STATUS:
            g_value_set_boolean (value, ephy_net_monitor_get_net_status (monitor));
            break;
    }
}

static void
ephy_net_monitor_class_init (EphyNetMonitorClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->dispose = ephy_net_monitor_dispose;
    object_class->get_property = ephy_net_monitor_get_property;

/**
 * EphyNetMonitor::network-status:
 * 
 * Whether the network is on-line.
 */
    g_object_class_install_property
        (object_class,
         PROP_NETWORK_STATUS,
         g_param_spec_boolean ("network-status",
                       "network-status",
                       "network-status",
                       FALSE,
                       G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

    g_type_class_add_private (object_class, sizeof (EphyNetMonitorPrivate));
}

/* public API */

EphyNetMonitor *
ephy_net_monitor_new (void)
{
    return g_object_new (EPHY_TYPE_NET_MONITOR, NULL);
}

gboolean
ephy_net_monitor_get_net_status (EphyNetMonitor *monitor)
{
    EphyNetMonitorPrivate *priv;

    g_return_val_if_fail (EPHY_IS_NET_MONITOR (monitor), FALSE);

    priv = monitor->priv;

    return !priv->active || priv->status != NETWORK_DOWN;
}