From 3f46f5fe76c4e48dde9d1bb6f8953bbce9e09702 Mon Sep 17 00:00:00 2001 From: infirit Date: Wed, 26 Nov 2014 19:02:40 +0100 Subject: mateweather, invest: use GNetworkMonitor Rather than talking to NetworkManager over D-Bus, use GNetworkMonitor to monitor the network state. Taken from gnome-applets commit: 9b53c7156b61d51777dbe2f60c55125c86b3409c From: Dan Winship --- configure.ac | 23 ------- invest-applet/invest/defs.py.in | 1 - invest-applet/invest/networkmanager.py | 74 +++----------------- mateweather/Makefile.am | 2 - mateweather/mateweather-applet.c | 122 ++++++--------------------------- 5 files changed, 30 insertions(+), 192 deletions(-) diff --git a/configure.ac b/configure.ac index ca4c97f1..30ed4583 100644 --- a/configure.ac +++ b/configure.ac @@ -25,7 +25,6 @@ DBUS_GLIB_REQUIRED=0.74 MATE_ICON_THEME_REQUIRED=1.1.0 LIBXML_REQUIRED=2.5.0 POLKIT_REQUIRED=0.92 -NETWORKMANAGER_REQUIRED=0.7 dnl *************************************************************************** AM_MAINTAINER_MODE @@ -292,27 +291,6 @@ AC_SUBST(LIBMATEWEATHER_CFLAGS) AC_SUBST(LIBMATEWEATHER_LIBS) AM_CONDITIONAL(BUILD_LIBMATEWEATHER_APPLETS, $build_libmateweather_applets) -dnl -- check for NetworkManager ----------------------------------------------- -AC_ARG_ENABLE([networkmanager], - AC_HELP_STRING([--enable-networkmanager], - [Enable NetworkManager support @<:@default=yes@:>@]), - ENABLE_NETWORKMANAGER=$enableval, ENABLE_NETWORKMANAGER=yes) - -HAVE_NETWORKMANAGER=no -NETWORKMANAGER_VERSION=unknown -AC_SUBST(NETWORKMANAGER_VERSION) -if test "x$ENABLE_NETWORKMANAGER" = "xyes"; then - PKG_CHECK_MODULES(NETWORKMANAGER, NetworkManager >= $NETWORKMANAGER_REQUIRED, - HAVE_NETWORKMANAGER=yes, HAVE_NETWORKMANAGER=no) - - if test "x$HAVE_NETWORKMANAGER" = "xyes"; then - AC_DEFINE(HAVE_NETWORKMANAGER, 1, [NetworkManager available]) - NETWORKMANAGER_VERSION=`$PKG_CONFIG --modversion NetworkManager` - AC_SUBST(NETWORKMANAGER_CFLAGS) - AC_SUBST(NETWORKMANAGER_LIBS) - AC_SUBST(NETWORKMANAGER_VERSION) - fi -fi dnl *************************************************************************** dnl *** find paths to installed binaries *** @@ -719,7 +697,6 @@ mate-applets-$VERSION configure summary: - trashapplet always Using DBUS: $HAVE_DBUS - Using NetworkManager: $HAVE_NETWORKMANAGER Using HAL: $HAVE_HAL Using UPOWER: $HAVE_UPOWER Using libnotify: $HAVE_LIBNOTIFY diff --git a/invest-applet/invest/defs.py.in b/invest-applet/invest/defs.py.in index a503005e..41efe69a 100644 --- a/invest-applet/invest/defs.py.in +++ b/invest-applet/invest/defs.py.in @@ -6,6 +6,5 @@ PYTHONDIR = "@PYTHONDIR@" GETTEXT_PACKAGE = "@GETTEXT_PACKAGE@" MATELOCALEDIR = "@MATELOCALEDIR@" BUILDERDIR = "@BUILDERDIR@" -NETWORKMANAGER_VERSION = "@NETWORKMANAGER_VERSION@" PKGDATADIR = "@PKGDATADIR@" GTK_API_VERSION = "@GTK_API_VERSION@" diff --git a/invest-applet/invest/networkmanager.py b/invest-applet/invest/networkmanager.py index f1156115..446e0429 100644 --- a/invest-applet/invest/networkmanager.py +++ b/invest-applet/invest/networkmanager.py @@ -1,80 +1,26 @@ import mate_invest -from mate_invest.defs import NETWORKMANAGER_VERSION -from dbus.mainloop.glib import DBusGMainLoop -import dbus - -# possible states, see http://projects.gnome.org/NetworkManager/developers/ -> spec 0.8 -> NM_STATE -STATE_UNKNOWN = dbus.UInt32(0) -STATE_ASLEEP = dbus.UInt32(1) -STATE_CONNECTING = dbus.UInt32(2) -STATE_CONNECTED = dbus.UInt32(3) -STATE_DISCONNEDTED = dbus.UInt32(4) - -# numerical values of these states depend on the network manager version, they changed with 0.8.995 -fields = NETWORKMANAGER_VERSION.split('.') -if len(fields) >= 2: - major = int(fields[0]) - minor = int(fields[1]) - if len(fields) > 2: - micro = int(fields[2]) - - if major > 0 or major == 0 and (minor >= 9 or len(fields) > 2 and minor == 8 and micro >= 995): - # see http://projects.gnome.org/NetworkManager/developers/ -> spec 0.9 -> NM_STATE - print("Found NetworkManager spec 0.9 (%s)" % NETWORKMANAGER_VERSION) - STATE_UNKNOWN = dbus.UInt32(0) - STATE_ASLEEP = dbus.UInt32(10) - STATE_DISCONNECTED = dbus.UInt32(20) - STATE_DISCONNECTING = dbus.UInt32(30) - STATE_CONNECTING = dbus.UInt32(40) - STATE_CONNECTED_LOCAL = dbus.UInt32(50) - STATE_CONNECTED_SITE = dbus.UInt32(60) - STATE_CONNECTED_GLOBAL = dbus.UInt32(70) - STATE_CONNECTED = STATE_CONNECTED_GLOBAL # backward compatibility with < 0.9 +from gi.repository import Gio class NetworkManager: def __init__(self): - self.state = STATE_UNKNOWN + self.network_available = True self.statechange_callback = None - try: - # get an event loop - loop = DBusGMainLoop() - - # get the NetworkManager object from D-Bus - mate_invest.debug("Connecting to Network Manager via D-Bus") - bus = dbus.SystemBus(mainloop=loop) - nmobj = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager') - nm = dbus.Interface(nmobj, 'org.freedesktop.NetworkManager') - - # connect the signal handler to the bus - bus.add_signal_receiver(self.handler, None, - 'org.freedesktop.NetworkManager', - 'org.freedesktop.NetworkManager', - '/org/freedesktop/NetworkManager') - - # get the current status of the network manager - self.state = nm.state() - mate_invest.debug("Current Network Manager status is %d" % self.state) - except Exception, msg: - mate_invest.error("Could not connect to the Network Manager: %s" % msg ) + self.monitor = Gio.NetworkMonitor.get_default() + self.monitor.connect('network-changed', self.on_network_changed) def online(self): - return self.state == STATE_UNKNOWN or self.state == STATE_CONNECTED + return self.network_available def offline(self): return not self.online() # the signal handler for signals from the network manager - def handler(self,signal=None): - if isinstance(signal, dict): - state = signal.get('State') - if state != None: - mate_invest.debug("Network Manager change state %d => %d" % (self.state, state) ); - self.state = state - - # notify about state change - if self.statechange_callback != None: - self.statechange_callback() + def on_network_changed(self, monitor, available): + self.network_available = available + # notify about state change + if self.statechange_callback != None: + self.statechange_callback() def set_statechange_callback(self,handler): self.statechange_callback = handler diff --git a/mateweather/Makefile.am b/mateweather/Makefile.am index db2f004c..c8ec98e5 100644 --- a/mateweather/Makefile.am +++ b/mateweather/Makefile.am @@ -10,7 +10,6 @@ AM_CPPFLAGS = \ $(MATEDESKTOP_CFLAGS) \ $(LIBNOTIFY_CFLAGS) \ $(LIBMATEWEATHER_CFLAGS) \ - $(NETWORKMANAGER_CFLAGS) \ $(DBUS_CFLAGS) libexec_PROGRAMS = mateweather-applet @@ -29,7 +28,6 @@ mateweather_applet_LDADD = \ $(MATEDESKTOP_LIBS) \ $(MATE_LIBS2_LIBS) \ $(LIBMATEWEATHER_LIBS) \ - $(NETWORKMANAGER_LIBS) \ $(DBUS_LIBS) uidir = $(datadir)/mate/ui diff --git a/mateweather/mateweather-applet.c b/mateweather/mateweather-applet.c index fcaa7295..4dc26a16 100644 --- a/mateweather/mateweather-applet.c +++ b/mateweather/mateweather-applet.c @@ -33,12 +33,6 @@ #define MATEWEATHER_I_KNOW_THIS_IS_UNSTABLE -#ifdef HAVE_NETWORKMANAGER -#include -#include -#include -#endif - #include "mateweather.h" #include "mateweather-about.h" #include "mateweather-pref.h" @@ -279,9 +273,20 @@ key_press_cb (GtkWidget *widget, GdkEventKey *event, MateWeatherApplet *gw_apple } +static void +network_changed (GNetworkMonitor *monitor, gboolean available, MateWeatherApplet *gw_applet) +{ + if (available) { + mateweather_update (gw_applet); + } +} + + static void applet_destroy (GtkWidget *widget, MateWeatherApplet *gw_applet) { + GNetworkMonitor *monitor; + if (gw_applet->pref_dialog) gtk_widget_destroy (gw_applet->pref_dialog); @@ -303,18 +308,20 @@ applet_destroy (GtkWidget *widget, MateWeatherApplet *gw_applet) gw_applet->settings = NULL; } + monitor = g_network_monitor_get_default (); + g_signal_handlers_disconnect_by_func (monitor, + G_CALLBACK (network_changed), + gw_applet); + weather_info_abort (gw_applet->mateweather_info); } -#ifdef HAVE_NETWORKMANAGER -static void setup_network_monitor (MateWeatherApplet *gw_applet); -#endif - void mateweather_applet_create (MateWeatherApplet *gw_applet) { GtkActionGroup *action_group; gchar *ui_path; AtkObject *atk_obj; + GNetworkMonitor*monitor; gw_applet->mateweather_pref.location = NULL; gw_applet->mateweather_pref.show_notifications = FALSE; @@ -381,10 +388,9 @@ void mateweather_applet_create (MateWeatherApplet *gw_applet) place_widgets(gw_applet); -#ifdef HAVE_NETWORKMANAGER - setup_network_monitor (gw_applet); -#endif -} + monitor = g_network_monitor_get_default(); + g_signal_connect (monitor, "network-changed", + G_CALLBACK (network_changed), gw_applet);} gint timeout_cb (gpointer data) { @@ -545,91 +551,3 @@ void mateweather_update (MateWeatherApplet *gw_applet) update_finish, gw_applet); } } - -#ifdef HAVE_NETWORKMANAGER -static void -state_notify (DBusPendingCall *pending, gpointer data) -{ - MateWeatherApplet *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) { - /* thank you, glibc */ - res_init (); - mateweather_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 (MateWeatherApplet *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 "'", - NULL); - } -} -#endif /* HAVE_NETWORKMANAGER */ -- cgit v1.2.1