aboutsummaryrefslogtreecommitdiffstats
path: root/modules/network-manager/evolution-network-manager.c
blob: 7dc110f411aa455fa18b5ff81cc60d3b93aee5ff (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
/*
 * evolution-network-manager.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/>
 *
 */

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <NetworkManager/NetworkManager.h>

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

/* Standard GObject macros */
#define E_TYPE_NETWORK_MANAGER \
    (e_network_manager_get_type ())
#define E_NETWORK_MANAGER(obj) \
    (G_TYPE_CHECK_INSTANCE_CAST \
    ((obj), E_TYPE_NETWORK_MANAGER, ENetworkManager))

typedef struct _ENetworkManager ENetworkManager;
typedef struct _ENetworkManagerClass ENetworkManagerClass;

struct _ENetworkManager {
    EExtension parent;
    DBusConnection *connection;
};

struct _ENetworkManagerClass {
    EExtensionClass parent_class;
};

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

/* Forward Declarations */
GType e_network_manager_get_type (void);
static gboolean network_manager_connect (ENetworkManager *extension);

G_DEFINE_DYNAMIC_TYPE (ENetworkManager, e_network_manager, E_TYPE_EXTENSION)

static EShell *
network_manager_get_shell (ENetworkManager *extension)
{
    EExtensible *extensible;

    extensible = e_extension_get_extensible (E_EXTENSION (extension));

    return E_SHELL (extensible);
}

static DBusHandlerResult
network_manager_monitor (DBusConnection *connection G_GNUC_UNUSED,
                         DBusMessage *message,
                         gpointer user_data)
{
    ENetworkManager *extension = user_data;
    EShell *shell;
    const gchar *path;
    guint32 state;
    DBusError error = DBUS_ERROR_INIT;

    shell = network_manager_get_shell (extension);

    path = dbus_message_get_path (message);

    if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
        g_strcmp0 (path, DBUS_PATH_LOCAL) == 0) {
        dbus_connection_unref (extension->connection);
        extension->connection = NULL;

        g_timeout_add_seconds (
            3, (GSourceFunc) network_manager_connect, extension);

        return DBUS_HANDLER_RESULT_HANDLED;
    }

    if (!dbus_message_is_signal (message, NM_DBUS_INTERFACE, "StateChanged"))
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    dbus_message_get_args (
        message, &error,
        DBUS_TYPE_UINT32, &state,
        DBUS_TYPE_INVALID);

    if (dbus_error_is_set (&error)) {
        g_warning ("%s", error.message);
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    switch (state) {
        case NM_STATE_CONNECTED:
            e_shell_set_network_available (shell, TRUE);
            break;
        case NM_STATE_ASLEEP:
        case NM_STATE_DISCONNECTED:
            e_shell_set_network_available (shell, FALSE);
            break;
        default:
            break;
    }

    return DBUS_HANDLER_RESULT_HANDLED;
}

static void
network_manager_check_initial_state (ENetworkManager *extension)
{
    EShell *shell;
    DBusMessage *message = NULL;
    DBusMessage *response = NULL;
    guint32 state = -1;
    DBusError error = DBUS_ERROR_INIT;

    shell = network_manager_get_shell (extension);

    message = dbus_message_new_method_call (
        NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_INTERFACE, "state");

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

    if (response != NULL) {
        dbus_message_get_args (
            response, &error, DBUS_TYPE_UINT32,
            &state, DBUS_TYPE_INVALID);
    } else {
        g_warning ("%s", error.message);
        dbus_error_free (&error);
        return;
    }

    /* Update the state only in the absence of a network connection,
     * otherwise let the old state prevail. */
    if (state == NM_STATE_ASLEEP || state == NM_STATE_DISCONNECTED)
        e_shell_set_network_available (shell, FALSE);

    dbus_message_unref (message);
    dbus_message_unref (response);
}

static gboolean
network_manager_connect (ENetworkManager *extension)
{
    DBusError error = DBUS_ERROR_INIT;

    /* 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 = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
    if (extension->connection == NULL) {
        g_warning ("%s", error.message);
        dbus_error_free (&error);
        return TRUE;
    }

    dbus_connection_setup_with_g_main (extension->connection, NULL);
    dbus_connection_set_exit_on_disconnect (extension->connection, FALSE);

    if (!dbus_connection_add_filter (
        extension->connection,
        network_manager_monitor, extension, NULL))
        goto fail;

    network_manager_check_initial_state (extension);

    dbus_bus_add_match (
        extension->connection,
        "type='signal',"
        "interface='" NM_DBUS_INTERFACE "',"
        "sender='" NM_DBUS_SERVICE "',"
        "path='" NM_DBUS_PATH "'",
        &error);
    if (dbus_error_is_set (&error)) {
        g_warning ("%s", error.message);
        dbus_error_free (&error);
        goto fail;
    }

    return FALSE;

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

    return TRUE;
}

static void
network_manager_constructed (GObject *object)
{
    network_manager_connect (E_NETWORK_MANAGER (object));
}

static void
e_network_manager_class_init (ENetworkManagerClass *class)
{
    GObjectClass *object_class;
    EExtensionClass *extension_class;

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

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

static void
e_network_manager_class_finalize (ENetworkManagerClass *class)
{
}

static void
e_network_manager_init (ENetworkManager *extension)
{
}

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

G_MODULE_EXPORT void
e_module_unload (GTypeModule *type_module)
{
}