[gnome-initial-setup/82-support-wi-fi-devices-appearing-dynamically: 1/3] network: factor out finding Wi-Fi device



commit 8fec2f65250808c854e5e73d4ecbecaac5f3c482
Author: Will Thompson <will willthompson co uk>
Date:   Mon Jan 20 07:17:58 2020 +0000

    network: factor out finding Wi-Fi device
    
    This code currently runs only once, when the page is being constructed.
    We would like to be able to react to new network devices being attached.
    Factor it out so that it can be reused, and take the opportunity to
    clean it up a bit. No functional change.

 .../pages/network/gis-network-page.c               | 100 +++++++++++----------
 1 file changed, 55 insertions(+), 45 deletions(-)
---
diff --git a/gnome-initial-setup/pages/network/gis-network-page.c 
b/gnome-initial-setup/pages/network/gis-network-page.c
index 387420d..cf0a5dd 100644
--- a/gnome-initial-setup/pages/network/gis-network-page.c
+++ b/gnome-initial-setup/pages/network/gis-network-page.c
@@ -618,20 +618,63 @@ device_state_changed (GObject *object, GParamSpec *param, GisNetworkPage *page)
 }
 
 static void
-gis_network_page_constructed (GObject *object)
+find_best_device (GisNetworkPage *page)
 {
-  GisNetworkPage *page = GIS_NETWORK_PAGE (object);
   GisNetworkPagePrivate *priv = gis_network_page_get_instance_private (page);
   const GPtrArray *devices;
-  NMDevice *device;
   guint i;
+
+  /* FIXME: deal with multiple devices and devices being removed */
+  if (priv->nm_device != NULL) {
+    g_debug ("Already displaying %s", nm_device_get_description (priv->nm_device));
+    return;
+  }
+
+  devices = nm_client_get_devices (priv->nm_client);
+  g_return_if_fail (devices != NULL);
+  for (i = 0; i < devices->len; i++) {
+    NMDevice *device = g_ptr_array_index (devices, i);
+
+    if (!nm_device_get_managed (device))
+      continue;
+
+    if (nm_device_get_device_type (device) == NM_DEVICE_TYPE_WIFI) {
+      /* FIXME deal with multiple, dynamic devices */
+      priv->nm_device = g_object_ref (device);
+      g_debug ("Displaying %s", nm_device_get_description (priv->nm_device));
+
+      g_signal_connect (priv->nm_device, "notify::state",
+                        G_CALLBACK (device_state_changed), page);
+      g_signal_connect (priv->nm_client, "notify::active-connections",
+                        G_CALLBACK (active_connections_changed), page);
+
+      sync_complete (page);
+
+      break;
+    }
+  }
+}
+
+static void
+gis_network_page_constructed (GObject *object)
+{
+  GisNetworkPage *page = GIS_NETWORK_PAGE (object);
+  GisNetworkPagePrivate *priv = gis_network_page_get_instance_private (page);
   gboolean visible = FALSE;
   GError *error = NULL;
 
   G_OBJECT_CLASS (gis_network_page_parent_class)->constructed (object);
 
+  gis_page_set_skippable (GIS_PAGE (page), TRUE);
+
   priv->icons = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
 
+  gtk_list_box_set_selection_mode (GTK_LIST_BOX (priv->network_list), GTK_SELECTION_NONE);
+  gtk_list_box_set_header_func (GTK_LIST_BOX (priv->network_list), update_header_func, NULL, NULL);
+  gtk_list_box_set_sort_func (GTK_LIST_BOX (priv->network_list), ap_sort, NULL, NULL);
+  g_signal_connect (priv->network_list, "row-activated",
+                    G_CALLBACK (row_activated), page);
+
   priv->nm_client = nm_client_new (NULL, &error);
   if (!priv->nm_client) {
     g_warning ("Can't create NetworkManager client, hiding network page: %s",
@@ -644,53 +687,20 @@ gis_network_page_constructed (GObject *object)
                           priv->turn_on_switch, "active",
                           G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
-  devices = nm_client_get_devices (priv->nm_client);
-  if (devices) {
-    for (i = 0; i < devices->len; i++) {
-      device = g_ptr_array_index (devices, i);
-
-      if (!nm_device_get_managed (device))
-        continue;
-
-      if (nm_device_get_device_type (device) == NM_DEVICE_TYPE_WIFI) {
-        /* FIXME deal with multiple, dynamic devices */
-        priv->nm_device = g_object_ref (device);
-        break;
-      }
-    }
-  }
+  find_best_device (page);
 
-  if (priv->nm_device == NULL) {
+  if (g_getenv ("GIS_ALWAYS_SHOW_NETWORK_PAGE") != NULL) {
+    /* Allow to always show the network page, for debugging purposes */
+    visible = TRUE;
+  } else if (priv->nm_device == NULL) {
     g_debug ("No network device found, hiding network page");
-    goto out;
-  }
-
-  /* Allow to always show the network, even if there's an active connection, for
-   * debugging purposes */
-  if (g_getenv ("GIS_ALWAYS_SHOW_NETWORK_PAGE") == NULL &&
-      nm_device_get_state (priv->nm_device) == NM_DEVICE_STATE_ACTIVATED) {
+  } else if (nm_device_get_state (priv->nm_device) == NM_DEVICE_STATE_ACTIVATED) {
     g_debug ("Activated network device found, hiding network page");
-    goto out;
+  } else {
+    visible = TRUE;
   }
 
-  visible = TRUE;
-
-  g_signal_connect (priv->nm_device, "notify::state",
-                    G_CALLBACK (device_state_changed), page);
-  g_signal_connect (priv->nm_client, "notify::active-connections",
-                    G_CALLBACK (active_connections_changed), page);
-
-  gtk_list_box_set_selection_mode (GTK_LIST_BOX (priv->network_list), GTK_SELECTION_NONE);
-  gtk_list_box_set_header_func (GTK_LIST_BOX (priv->network_list), update_header_func, NULL, NULL);
-  gtk_list_box_set_sort_func (GTK_LIST_BOX (priv->network_list), ap_sort, NULL, NULL);
-  g_signal_connect (priv->network_list, "row-activated",
-                    G_CALLBACK (row_activated), page);
-
-  sync_complete (page);
-
-  gis_page_set_skippable (GIS_PAGE (page), TRUE);
-
- out:
+out:
   gtk_widget_set_visible (GTK_WIDGET (page), visible);
 }
 


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