[PATCH f20-branch] (v2) network gui spoke: use GDBus to obtain list of settings (#1018467)

Radek Vykydal rvykydal at redhat.com
Wed Oct 16 11:29:57 UTC 2013


Version 2 addresses Brian's review remarks and also replaces one more
use of list_connections() with GDBus in chunk -474,11 +479,12

Another step in moving to GDBus.
libnm-glib (NMRemoteSettings.list_connections()) returns empty list
when called for the first time.
---
 pyanaconda/installclasses/fedora.py |   2 +-
 pyanaconda/network.py               |   6 +-
 pyanaconda/nm.py                    |  52 +++++++++++----
 pyanaconda/ui/gui/spokes/network.py | 129 +++++++++++++-----------------------
 4 files changed, 90 insertions(+), 99 deletions(-)

diff --git a/pyanaconda/installclasses/fedora.py b/pyanaconda/installclasses/fedora.py
index c433d49..79c1a9b 100644
--- a/pyanaconda/installclasses/fedora.py
+++ b/pyanaconda/installclasses/fedora.py
@@ -49,7 +49,7 @@ class InstallClass(BaseInstallClass):
                 continue
             try:
                 onboot = nm.nm_device_setting_value(devName, "connection", "autoconnect")
-            except nm.DeviceSettingsNotFoundError:
+            except nm.SettingsNotFoundError:
                 continue
             if not onboot == False:
                 return
diff --git a/pyanaconda/network.py b/pyanaconda/network.py
index 563a680..b7ca246 100644
--- a/pyanaconda/network.py
+++ b/pyanaconda/network.py
@@ -283,7 +283,7 @@ def dumpMissingDefaultIfcfgs():
         # check that device has connection without ifcfg file
         try:
             con_uuid = nm.nm_device_setting_value(devname, "connection", "uuid")
-        except nm.DeviceSettingsNotFoundError:
+        except nm.SettingsNotFoundError:
             continue
         if find_ifcfg_file([("UUID", con_uuid)], root_path=""):
             continue
@@ -293,7 +293,7 @@ def dumpMissingDefaultIfcfgs():
             log.debug("network: dumping ifcfg file for default autoconnection on %s", devname)
             nm.nm_update_settings_of_device(devname, [['connection', 'autoconnect', False, None]])
             log.debug("network: setting autoconnect of %s to False" , devname)
-        except nm.DeviceSettingsNotFoundError:
+        except nm.SettingsNotFoundError:
             log.debug("network: no ifcfg file for %s", devname)
         rv = True
 
@@ -939,7 +939,7 @@ def setOnboot(ksdata):
         try:
             nm.nm_update_settings_of_device(devname, [['connection', 'autoconnect', network_data.onboot, None]])
             ifcfglog.debug("setting autoconnect (ONBOOT) of %s to %s" , devname, network_data.onboot)
-        except nm.DeviceSettingsNotFoundError as e:
+        except nm.SettingsNotFoundError as e:
             log.debug("setOnboot: %s", e)
 
 def networkInitialize(ksdata):
diff --git a/pyanaconda/nm.py b/pyanaconda/nm.py
index cd8512d..6755883 100644
--- a/pyanaconda/nm.py
+++ b/pyanaconda/nm.py
@@ -52,12 +52,7 @@ class PropertyNotFoundError(ValueError):
     def __str__(self):
         return self.__repr__()
 
-class SettingNotFoundError(ValueError):
-    """Setting of NMRemoteConnection was not found"""
-    def __str__(self):
-        return self.__repr__()
-
-class DeviceSettingsNotFoundError(ValueError):
+class SettingsNotFoundError(ValueError):
     """Settings NMRemoteConnection object was not found"""
     def __str__(self):
         return self.__repr__()
@@ -620,12 +615,12 @@ def nm_device_setting_value(name, key1, key2):
                 by NM
        :rtype: unpacked GDBus variant or None
        :raise UnknownDeviceError: if device is not found
-       :raise DeviceSettingsNotFoundError: if settings were not found
+       :raise SettingsNotFoundError: if settings were not found
                                            (eg for "wlan0")
     """
     settings_path = _device_settings(name)
     if not settings_path:
-        raise DeviceSettingsNotFoundError(name)
+        raise SettingsNotFoundError(name)
     proxy = _get_proxy(object_path=settings_path, interface_name="org.freedesktop.NetworkManager.Settings.Connection")
     args = None
     settings = proxy.call_sync("GetSettings",
@@ -637,10 +632,43 @@ def nm_device_setting_value(name, key1, key2):
     try:
         value = settings[key1][key2]
     except KeyError:
-        #raise SettingNotFoundError(key1, key2)
         value = None
     return value
 
+def nm_ap_setting_value(ssid, key1, key2):
+    """Return value of ap's setting specified by key1 and key2.
+
+       :param ssid: name of ap (ssid)
+       :type ssid: str
+       :param key1: first-level key of setting (eg "connection")
+       :type key1: str
+       :param key2: second-level key of setting (eg "uuid")
+       :type key2: str
+       :return: value of setting or None if the setting was not found
+                which means it does not exist or default value is used
+                by NM
+       :rtype: unpacked GDBus variant or None
+       :raise SettingsNotFoundError: if settings were not found
+                                           (eg for "wlan0")
+    """
+    settings_path = _settings_for_ap(ssid)
+    if not settings_path:
+        raise SettingsNotFoundError(ssid)
+    proxy = _get_proxy(object_path=settings_path, interface_name="org.freedesktop.NetworkManager.Settings.Connection")
+    args = None
+    settings = proxy.call_sync("GetSettings",
+                               args,
+                               Gio.DBusCallFlags.NONE,
+                               DEFAULT_DBUS_TIMEOUT,
+                               None)
+    settings = settings.unpack()[0]
+    try:
+        value = settings[key1][key2]
+    except KeyError:
+        value = None
+    return value
+
+
 def nm_activate_device_connection(dev_name, con_uuid):
     """Activate device with specified connection.
 
@@ -707,12 +735,12 @@ def nm_update_settings_of_device(name, new_values):
                          value:
                          default_type_str: str
        :raise UnknownDeviceError: if device is not found
-       :raise DeviceSettingsNotFoundError: if settings were not found
+       :raise SettingsNotFoundError: if settings were not found
                                            (eg for "wlan0")
     """
     settings_path = _device_settings(name)
     if not settings_path:
-        raise DeviceSettingsNotFoundError(name)
+        raise SettingsNotFoundError(name)
     return _update_settings(settings_path, new_values)
 
 def _update_settings(settings_path, new_values):
@@ -958,7 +986,7 @@ def test():
     if wireless_device:
         try:
             nm_update_settings_of_device(wireless_device, [[key1, key2, new_value, None]])
-        except DeviceSettingsNotFoundError as e:
+        except SettingsNotFoundError as e:
             print "%s" % e
 
     #nm_update_settings_of_device(devname, [["connection", "id", "test", None]])
diff --git a/pyanaconda/ui/gui/spokes/network.py b/pyanaconda/ui/gui/spokes/network.py
index a2dbdcb..1d8eaeb 100644
--- a/pyanaconda/ui/gui/spokes/network.py
+++ b/pyanaconda/ui/gui/spokes/network.py
@@ -43,7 +43,7 @@ from pyanaconda.ui.gui.utils import gtk_call_once, enlightbox
 from pyanaconda.ui.common import FirstbootSpokeMixIn
 
 from pyanaconda import network
-from pyanaconda.nm import nm_device_setting_value, nm_device_ip_config, nm_activated_devices, nm_device_active_ssid
+from pyanaconda import nm
 
 from gi.repository import GLib, GObject, Pango, Gio, NetworkManager, NMClient
 import dbus
@@ -117,11 +117,6 @@ def localized_string_of_device_state(device, state):
 
     return s
 
-configuration_of_disconnected_devices_allowed = True
-# it is not in gnome-control-center but it makes sense
-# for installer
-# https://bugzilla.redhat.com/show_bug.cgi?id=704119
-
 __all__ = ["NetworkSpoke", "NetworkStandaloneSpoke"]
 
 class CellRendererSignal(Gtk.CellRendererPixbuf):
@@ -263,7 +258,6 @@ class NetworkControlBox(object):
         self._updating_device = False
 
         self.client = NMClient.Client.new()
-        self.remote_settings = NMClient.RemoteSettings()
 
         # devices list
         # limited to wired and wireless
@@ -386,11 +380,13 @@ class NetworkControlBox(object):
 
         log.info("network: access point changed: %s", ssid_target)
 
-        con = self.find_connection_for_device(device, ssid_target)
-        if con:
-            self.client.activate_connection(con, device,
-                                            None, None, None)
-        else:
+        try:
+            uuid = nm.nm_ap_setting_value(ssid_target, "connection", "uuid")
+            nm.nm_activate_device_connection(device.get_iface(), uuid)
+        except nm.UnmanagedDeviceError as e:
+            log.debug("network: on_wireless_ap_changed: %s", e)
+        except nm.SettingsNotFoundError as e:
+            log.debug("network: on_wireless_ap_changed: %s", e)
             self.client.add_and_activate_connection(None, device, ap_obj_path,
                                                     None, None)
 
@@ -405,26 +401,36 @@ class NetworkControlBox(object):
         if not device:
             return
 
-        con = self.find_active_connection_for_device(device)
-        ssid = None
-        if not con and configuration_of_disconnected_devices_allowed:
+        devname = device.get_iface()
+        uuid = None
+        try:
+            uuid = nm.nm_device_active_con_uuid(devname)
+        except nm.UnknownDeviceError as e:
+            log.debug("network: on_edit_connection: %s", e)
+        if not uuid:
             if device.get_device_type() == NetworkManager.DeviceType.WIFI:
-                ssid = self.selected_ssid
-            con = self.find_connection_for_device(device, ssid)
+                if self.selected_ssid:
+                    try:
+                        uuid = nm.nm_ap_setting_value(self.selected_ssid, "connection", "uuid")
+                    except nm.SettingsNotFoundError as e:
+                        log.debug("network: on_edit_connection: %s", e)
+            else:
+                uuid = nm.nm_device_setting_value(devname, "connection", "uuid")
 
-        if con:
-            uuid = con.get_uuid()
-        else:
+        if not uuid:
+            log.debug("network: on_edit_connection: can't find connection for device %s", devname)
             return
 
         # 871132 auto activate wireless connection after editing if it is not
         # already activated (assume entering secrets)
         activate = None
-        if (device.get_device_type() == NetworkManager.DeviceType.WIFI and ssid
-            and (nm_device_active_ssid(device.get_iface()) == ssid)):
-            activate = (con, device)
+        if (device.get_device_type() == NetworkManager.DeviceType.WIFI
+            and self.selected_ssid
+            and self.selected_ssid != nm.nm_device_active_ssid(devname)):
+            activate = (uuid, devname)
 
-        log.info("network: configuring connection %s device %s ssid %s", uuid, device.get_iface(), ssid)
+        log.info("network: configuring connection %s device %s ssid %s",
+                 uuid, devname, self.selected_ssid)
         self.kill_nmce(msg="Configure button clicked")
         proc = subprocess.Popen(["nm-connection-editor", "--edit", "%s" % uuid])
         self._running_nmce = proc
@@ -446,13 +452,12 @@ class NetworkControlBox(object):
             if self._running_nmce and self._running_nmce.pid == pid:
                 self._running_nmce = None
             if activate:
-                con, device = activate
-                gtk_call_once(self._activate_connection_cb, con, device)
+                uuid, devname = activate
+                gtk_call_once(self._activate_connection_cb, uuid, devname)
             network.logIfcfgFiles("nm-c-e run")
 
-    def _activate_connection_cb(self, con, device):
-        self.client.activate_connection(con, device,
-                                        None, None, None)
+    def _activate_connection_cb(self, uuid, devname):
+        nm.nm_activate_device_connection(devname, uuid)
 
     def on_wireless_enabled(self, *args):
         switch = self.builder.get_object("device_wireless_off_switch")
@@ -474,11 +479,12 @@ class NetworkControlBox(object):
                         NetworkManager.DeviceType.BOND,
                         NetworkManager.DeviceType.VLAN):
             if active:
-                cons = self.remote_settings.list_connections()
-                dev_cons = device.filter_connections(cons)
-                if dev_cons:
-                    self.client.activate_connection(dev_cons[0], device,
-                                                    None, None, None)
+                uuid = nm.nm_device_setting_value(device.get_iface(), "connection", "uuid")
+                if uuid:
+                    try:
+                        nm.nm_activate_device_connection(device.get_iface(), uuid)
+                    except nm.UnmanagedDeviceError as e:
+                        log.debug("network: on_device_off_toggled: %s", e)
                 else:
                     self.client.add_and_activate_connection(None, device, None,
                                                             None, None)
@@ -521,47 +527,6 @@ class NetworkControlBox(object):
             return None
         return model.get(itr, DEVICES_COLUMN_OBJECT)[0]
 
-    def find_connection_for_device(self, device, ssid=None):
-        dev_type = device.get_device_type()
-        cons = self.remote_settings.list_connections()
-        for con in cons:
-            con_type = con.get_setting_connection().get_connection_type()
-            if dev_type == NetworkManager.DeviceType.ETHERNET:
-                if con_type != NetworkManager.SETTING_WIRED_SETTING_NAME:
-                    continue
-                settings = con.get_setting_wired()
-                con_hwaddr = ":".join("%02X" % ord(bytechar)
-                                      for bytechar in settings.get_mac_address())
-                if con_hwaddr == device.get_hw_address():
-                    return con
-            elif dev_type == NetworkManager.DeviceType.WIFI:
-                if con_type != NetworkManager.SETTING_WIRELESS_SETTING_NAME:
-                    continue
-                settings = con.get_setting_wireless()
-                if ssid == settings.get_ssid():
-                    return con
-            elif dev_type == NetworkManager.DeviceType.BOND:
-                if con_type != NetworkManager.SETTING_BOND_SETTING_NAME:
-                    continue
-                settings = con.get_setting_bond()
-                if device.get_iface() == settings.get_virtual_iface_name():
-                    return con
-            elif dev_type == NetworkManager.DeviceType.VLAN:
-                if con_type != NetworkManager.SETTING_VLAN_SETTING_NAME:
-                    continue
-                settings = con.get_setting_vlan()
-                if device.get_iface() == settings.get_interface_name():
-                    return con
-            else:
-                return None
-
-    def find_active_connection_for_device(self, device):
-        cons = self.client.get_active_connections()
-        for con in cons:
-            if con.get_devices()[0] is device:
-                return self.remote_settings.get_connection_by_path(con.get_connection())
-        return None
-
     def _device_is_stored(self, nm_device):
         """Check that device with Udi of nm_device is already in liststore"""
         udi = nm_device.get_udi()
@@ -689,8 +654,8 @@ class NetworkControlBox(object):
         elif dev_type == NetworkManager.DeviceType.VLAN:
             dt = "wired"
 
-        ipv4cfg = nm_device_ip_config(device.get_iface(), version=4)
-        ipv6cfg = nm_device_ip_config(device.get_iface(), version=6)
+        ipv4cfg = nm.nm_device_ip_config(device.get_iface(), version=4)
+        ipv6cfg = nm.nm_device_ip_config(device.get_iface(), version=6)
 
         if ipv4cfg and ipv4cfg[0]:
             addr_str, prefix, gateway_str = ipv4cfg[0][0]
@@ -782,7 +747,7 @@ class NetworkControlBox(object):
         dev_type = device.get_device_type()
         if dev_type == NetworkManager.DeviceType.VLAN:
             self._set_device_info_value("wired", "vlanid", str(device.get_vlan_id()))
-            parent = nm_device_setting_value(device.get_iface(), "vlan", "parent")
+            parent = nm.nm_device_setting_value(device.get_iface(), "vlan", "parent")
             self._set_device_info_value("wired", "parent", parent)
 
     def _refresh_speed_hwaddr(self, device, state=None):
@@ -881,8 +846,6 @@ class NetworkControlBox(object):
                                             NetworkManager.DeviceState.DEACTIVATING,
                                             NetworkManager.DeviceState.FAILED))
             self._updating_device = False
-            if not configuration_of_disconnected_devices_allowed:
-                self.builder.get_object("button_%s_options" % dev_type_str).set_sensitive(state == NetworkManager.DeviceState.ACTIVATED)
         elif dev_type_str == "wireless":
             self.on_wireless_enabled()
 
@@ -1254,7 +1217,7 @@ class NetworkSpoke(FirstbootSpokeMixIn, NormalSpoke):
     def completed(self):
         # TODO: check also if source requires updates when implemented
         return (not can_touch_runtime_system("require network connection")
-                or nm_activated_devices())
+                or nm.nm_activated_devices())
 
     @property
     def mandatory(self):
@@ -1365,7 +1328,7 @@ class NetworkStandaloneSpoke(StandaloneSpoke):
     @property
     def completed(self):
         return (not can_touch_runtime_system("require network connection")
-                or nm_activated_devices())
+                or nm.nm_activated_devices())
 
     def initialize(self):
         register_secret_agent(self)
@@ -1406,7 +1369,7 @@ def _update_network_data(data, ncb):
         nd = network.ksdata_from_ifcfg(devname)
         if not nd:
             continue
-        if devname in nm_activated_devices():
+        if devname in nm.nm_activated_devices():
             nd.activate = True
         data.network.network.append(nd)
     hostname = ncb.hostname
-- 
1.7.11.7



More information about the anaconda-patches mailing list