[gnome-applets/wip/network-manager-deprecation: 1/2] gweather, invest: use GNetworkMonitor



commit ef7e4dcd70ffce0dcafe35cfc6a81f58ee236503
Author: Dan Winship <danw gnome org>
Date:   Mon Sep 8 00:33:27 2014 +0200

    gweather, invest: use GNetworkMonitor
    
     * Rather than talking to NetworkManager over D-Bus, use GNetworkMonitor
       to monitor the network state.
    
    Signed-off-by: Sebastian Geiger <lanoxx gmx net>

 configure.ac                           |   25 +------
 gweather/Makefile.am                   |    4 +-
 gweather/gweather-applet.c             |  120 +++++--------------------------
 invest-applet/invest/defs.py.in        |    1 -
 invest-applet/invest/networkmanager.py |   79 +++------------------
 5 files changed, 32 insertions(+), 197 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 4319f3b..3dfd64a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -13,7 +13,7 @@ dnl ***************************************************************************
 dnl *** Minimum library versions for GNOME-APPLETS                          ***
 dnl ***************************************************************************
 GTK_REQUIRED=3.2
-GLIB_REQUIRED=2.26.0
+GLIB_REQUIRED=2.30.0
 GIO_REQUIRED=2.26.0
 GCONF_REQUIRED=2.8.0
 LIBPANEL_REQUIRED=2.91.90
@@ -29,7 +29,6 @@ LIBXML_REQUIRED=2.5.0
 GWEATHER_REQUIRED=3.7.3
 GUCHARMAP3_REQUIRED=2.33.0
 POLKIT_REQUIRED=0.92
-NETWORKMANAGER_REQUIRED=0.7
 GST10_REQUIRED=0.10.2
 dnl ***************************************************************************
 
@@ -262,27 +261,6 @@ AM_CONDITIONAL(BUILD_LIBGWEATHER_APPLETS, $build_libgweather_applets)
 dnl -- check for GSettings (required for gweather applet)
 GLIB_GSETTINGS
 
-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=unkown
-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)
-   fi
-fi
-
 dnl -- check for the Mixer ----------------------------------------------------
 AC_ARG_ENABLE([mixer-applet],
     AC_HELP_STRING([--enable-mixer-applet], [Enable the gstreamer-based mixer applet.]),
@@ -732,7 +710,6 @@ gnome-applets-$VERSION configure summary:
         - trashapplet                  always
 
        Using DBUS:                     $HAVE_DBUS
-       Using NetworkManager:           $HAVE_NETWORKMANAGER
        Using UPOWER:                   $HAVE_UPOWER
        Enabling IPv6:                  $have_ipv6
 " >&2
diff --git a/gweather/Makefile.am b/gweather/Makefile.am
index 6fab2a6..8908524 100644
--- a/gweather/Makefile.am
+++ b/gweather/Makefile.am
@@ -11,7 +11,6 @@ AM_CPPFLAGS =                                 \
        $(DBUS_CFLAGS)                          \
        $(LIBNOTIFY_CFLAGS)                     \
        $(LIBGWEATHER_CFLAGS)                   \
-       $(NETWORKMANAGER_CFLAGS)                \
        $(WARN_CFLAGS)
 
 libexec_PROGRAMS = gweather-applet-2
@@ -31,8 +30,7 @@ gweather_applet_2_LDADD =     \
        $(GNOME_APPLETS_LIBS)   \
        $(DBUS_LIBS)            \
        $(GNOME_LIBS2_LIBS)     \
-       $(LIBGWEATHER_LIBS)     \
-       $(NETWORKMANAGER_LIBS)
+       $(LIBGWEATHER_LIBS)
 
 uidir = $(pkgdatadir)/ui
 ui_DATA = gweather-applet-menu.xml
diff --git a/gweather/gweather-applet.c b/gweather/gweather-applet.c
index 739d558..eb3abcf 100644
--- a/gweather/gweather-applet.c
+++ b/gweather/gweather-applet.c
@@ -32,12 +32,6 @@
 
 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
 
-#ifdef HAVE_NETWORKMANAGER
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#include <NetworkManager/NetworkManager.h>
-#endif
-
 #include "gweather.h"
 #include "gweather-about.h"
 #include "gweather-pref.h"
@@ -282,9 +276,20 @@ key_press_cb (GtkWidget *widget, GdkEventKey *event, GWeatherApplet *gw_applet)
 
 }
 
+
+static void
+network_changed (GNetworkMonitor *monitor, gboolean available, GWeatherApplet *gw_applet)
+{
+    if (available) {
+        gweather_update (gw_applet);
+    }
+}
+
 static void
 applet_destroy (GtkWidget *widget, GWeatherApplet *gw_applet)
 {
+    GNetworkMonitor *monitor;
+
     if (gw_applet->pref_dialog)
        gtk_widget_destroy (gw_applet->pref_dialog);
 
@@ -304,19 +309,21 @@ applet_destroy (GtkWidget *widget, GWeatherApplet *gw_applet)
     g_clear_object (&gw_applet->lib_settings);
     g_clear_object (&gw_applet->applet_settings);
 
+    monitor = g_network_monitor_get_default ();
+    g_signal_handlers_disconnect_by_func (monitor,
+                                          G_CALLBACK (network_changed),
+                                          gw_applet);
+
     gweather_info_abort (gw_applet->gweather_info);
 }
 
-#ifdef HAVE_NETWORKMANAGER
-static void setup_network_monitor (GWeatherApplet *gw_applet);
-#endif
-
 void gweather_applet_create (GWeatherApplet *gw_applet)
 {
     GtkActionGroup *action_group;
     gchar          *ui_path;
     AtkObject      *atk_obj;
     GWeatherForecastType type;
+    GNetworkMonitor*monitor;
 
     panel_applet_set_flags (gw_applet->applet, PANEL_APPLET_EXPAND_MINOR);
 
@@ -379,9 +386,9 @@ void gweather_applet_create (GWeatherApplet *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)
@@ -523,90 +530,3 @@ void gweather_update (GWeatherApplet *gw_applet)
     gweather_info_update (gw_applet->gweather_info);
 }
 
-#ifdef HAVE_NETWORKMANAGER
-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) {
-                               /* thank you, glibc */
-                               res_init ();
-                               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 "'",
-                            NULL);
-    }
-}      
-#endif /* HAVE_NETWORKMANAGER */
diff --git a/invest-applet/invest/defs.py.in b/invest-applet/invest/defs.py.in
index 5a2e732..97fa171 100644
--- a/invest-applet/invest/defs.py.in
+++ b/invest-applet/invest/defs.py.in
@@ -7,4 +7,3 @@ PYTHONDIR = "@PYTHONDIR@"
 GETTEXT_PACKAGE = "@GETTEXT_PACKAGE@"
 GNOMELOCALEDIR = "@GNOMELOCALEDIR@"
 BUILDERDIR = "@BUILDERDIR@"
-NETWORKMANAGER_VERSION = "@NETWORKMANAGER_VERSION@"
diff --git a/invest-applet/invest/networkmanager.py b/invest-applet/invest/networkmanager.py
index 34700ec..fef939b 100644
--- a/invest-applet/invest/networkmanager.py
+++ b/invest-applet/invest/networkmanager.py
@@ -1,84 +1,25 @@
 import invest
-from 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)
-
-# attributes of the network manager
-version = NETWORKMANAGER_VERSION
-spec = "0.8"
-
-# 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
-               spec = "0.9"
-               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 comparitibility 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
-                       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()
-                       invest.debug("Current Network Manager status is %d" % self.state)
-               except Exception, msg:
-                       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:
-                               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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]