summaryrefslogtreecommitdiffstats
path: root/gweather-network.patch
blob: 8e9725968745e0bac5da6e4270c82596190ae65c (plain)
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
diff -up gnome-applets-2.23.3/gweather/gweather-applet.c.gweather-network gnome-applets-2.23.3/gweather/gweather-applet.c
--- gnome-applets-2.23.3/gweather/gweather-applet.c.gweather-network	2008-06-11 02:34:08.000000000 -0400
+++ gnome-applets-2.23.3/gweather/gweather-applet.c	2008-07-31 21:58:21.000000000 -0400
@@ -29,6 +29,10 @@
 #include <libnotify/notification.h>
 #endif
 
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <NetworkManager/NetworkManager.h>
+
 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
 
 #include "gweather.h"
@@ -289,6 +293,8 @@ applet_destroy (GtkWidget *widget, GWeat
     weather_info_abort (gw_applet->gweather_info);
 }
 
+static void setup_network_monitor (GWeatherApplet *gw_applet);
+
 void gweather_applet_create (GWeatherApplet *gw_applet)
 {
     AtkObject *atk_obj;
@@ -324,7 +330,7 @@ void gweather_applet_create (GWeatherApp
     g_signal_connect (GTK_OBJECT(gw_applet->applet), "button_press_event",
                        GTK_SIGNAL_FUNC(clicked_cb), gw_applet);
     g_signal_connect (G_OBJECT(gw_applet->applet), "key_press_event",           
-			G_CALLBACK(key_press_cb), gw_applet);                    
+			G_CALLBACK(key_press_cb), gw_applet);
                      
     gtk_widget_set_tooltip_text (GTK_WIDGET(gw_applet->applet), _("GNOME Weather"));
 
@@ -354,9 +360,9 @@ void gweather_applet_create (GWeatherApp
 					  NULL);
     }
 	
-    place_widgets(gw_applet);
-  
-	return;
+    place_widgets(gw_applet);        
+
+    setup_network_monitor (gw_applet);     
 }
 
 gint timeout_cb (gpointer data)
@@ -521,3 +527,88 @@ void gweather_update (GWeatherApplet *gw
 						    update_finish, gw_applet);
     }
 }
+
+static void
+state_notify (DBusPendingCall *pending, gpointer data)
+{
+	GWeatherApplet *gw_applet = data;
+
+        DBusMessage *msg = dbus_pending_call_steal_reply (pending);
+
+        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)) {
+                        if (result == NM_STATE_CONNECTED) {
+				gweather_update (gw_applet);
+                        }
+                }
+        }
+
+        dbus_message_unref (msg);
+}
+
+static void
+check_network (DBusConnection *connection, gpointer user_data)
+{
+        DBusMessage *message;
+        DBusPendingCall *reply;
+
+        message = dbus_message_new_method_call (NM_DBUS_SERVICE,
+                                                NM_DBUS_PATH,
+                                                NM_DBUS_INTERFACE,
+                                                "state");
+        if (dbus_connection_send_with_reply (connection, message, &reply, -1)) {
+                dbus_pending_call_set_notify (reply, state_notify, user_data, NULL);
+                dbus_pending_call_unref (reply);
+        }
+
+        dbus_message_unref (message);
+}
+
+static DBusHandlerResult
+filter_func (DBusConnection *connection, DBusMessage *message, void *user_data)
+{
+    if (dbus_message_is_signal (message, 
+				NM_DBUS_INTERFACE, 
+				"StateChanged")) {
+	check_network (connection, user_data);
+
+        return DBUS_HANDLER_RESULT_HANDLED;
+    }
+
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static void
+setup_network_monitor (GWeatherApplet *gw_applet)
+{
+    GError *error;
+    static DBusGConnection *bus = NULL;
+    DBusConnection *dbus;
+
+    if (bus == NULL) {
+        error = NULL;
+        bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+        if (bus == NULL) {
+            g_warning ("Couldn't connect to system bus: %s",
+                       error->message);
+            g_error_free (error);
+
+            return;
+        }
+
+        dbus = dbus_g_connection_get_connection (bus);	
+        dbus_connection_add_filter (dbus, filter_func, gw_applet, NULL);
+        dbus_bus_add_match (dbus,
+                            "type='signal',"
+                            "interface='" NM_DBUS_INTERFACE_DEVICE "'",
+                            NULL);
+    }
+}	
+