aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-nm.c
blob: 58923831e4da8711c7c032295818121ce1f07ee7 (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
/*
 * 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/>  
 *
 *
 * Authors:
 *      Shreyas Srinivasan <sshreyas@novell.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#define DBUS_API_SUBJECT_TO_CHANGE 1

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

#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <e-shell-window.h>
#include <Evolution.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>
#include <NetworkManager/NetworkManager.h>

typedef enum _ShellLineStatus {
    E_SHELL_LINE_DOWN,
    E_SHELL_LINE_UP
} ShellLineStatus;

static gboolean init_dbus (EShellWindow *window);

static DBusConnection *dbus_connection = NULL;

static gboolean
reinit_dbus (gpointer user_data)
{
    if (init_dbus (user_data))
        return FALSE;

    /* keep trying to re-establish dbus connection */

    return TRUE;
}

static DBusHandlerResult
e_shell_network_monitor (DBusConnection *connection G_GNUC_UNUSED,
                         DBusMessage *message, void *user_data)
{
    DBusError error;
    const char *object;
    ShellLineStatus status;
    EShellWindow *window = NULL;
    EShell *shell = NULL;
    GNOME_Evolution_ShellState shell_state;
    EShellLineStatus line_status;

    if (!user_data || !E_IS_SHELL_WINDOW (user_data))
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
    window = E_SHELL_WINDOW (user_data);
    shell = e_shell_window_peek_shell (window);

    dbus_error_init (&error);
    object = dbus_message_get_path (message);

    if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
        object && !strcmp (object, DBUS_PATH_LOCAL)) {
        dbus_connection_unref (dbus_connection);
        dbus_connection = NULL;

        g_timeout_add (3000, reinit_dbus, window);

        return DBUS_HANDLER_RESULT_HANDLED;
    }

    if (dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DeviceNoLongerActive"))
        status = E_SHELL_LINE_DOWN;
    else if (dbus_message_is_signal (message, NM_DBUS_INTERFACE, "DeviceNowActive"))
        status = E_SHELL_LINE_UP;
    else
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    if (!dbus_message_get_args (message, &error, DBUS_TYPE_OBJECT_PATH,
                    &object, DBUS_TYPE_INVALID))
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    line_status = e_shell_get_line_status (shell);

    if (line_status == E_SHELL_LINE_STATUS_ONLINE && status == E_SHELL_LINE_DOWN) {
        shell_state = GNOME_Evolution_FORCED_OFFLINE;
        e_shell_go_offline (shell, window, shell_state);
    } else if (line_status == E_SHELL_LINE_STATUS_FORCED_OFFLINE && status == E_SHELL_LINE_UP) {
        shell_state = GNOME_Evolution_USER_ONLINE;
        e_shell_go_online (shell, window, shell_state);
    }

    return DBUS_HANDLER_RESULT_HANDLED;
}

static gboolean
init_dbus (EShellWindow *window)
{
    DBusError error;

    if (dbus_connection != NULL)
        return TRUE;

    dbus_error_init (&error);
    if (!(dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
        g_warning ("could not get system bus: %s\n", error.message);
        dbus_error_free (&error);
        return FALSE;
    }

    dbus_connection_setup_with_g_main (dbus_connection, NULL);
    dbus_connection_set_exit_on_disconnect (dbus_connection, FALSE);

    if (!dbus_connection_add_filter (dbus_connection, e_shell_network_monitor, window, NULL))
        goto exception;

    dbus_bus_add_match (dbus_connection,
                "type='signal',"
                "interface='" NM_DBUS_INTERFACE "',"
                "sender='" NM_DBUS_SERVICE "',"
                "path='" NM_DBUS_PATH "'", &error);
    if (dbus_error_is_set (&error)) {
        dbus_error_free (&error);
        goto exception;
    }

    return TRUE;

exception:

    dbus_connection_unref (dbus_connection);
    dbus_connection = NULL;

    return FALSE;
}

gboolean
e_shell_dbus_initialise (EShellWindow *window)
{
    g_type_init ();

    return init_dbus (window);
}

void
e_shell_dbus_dispose (EShellWindow *window)
{
    //FIXME
    return;
}