[gnome-control-center] network: display "device state reason", and remove duplicate code



commit 15baea472088f1284705bbec9d3e1afdece8665d
Author: Dan Winship <danw gnome org>
Date:   Mon Feb 4 16:50:53 2013 -0500

    network: display "device state reason", and remove duplicate code
    
    For disconnected/unavailable devices, show the localized
    NMDeviceStateReason next to the localized NMDeviceState (and not in
    the tooltip). Also, move the code to do this into panel-common rather
    to save some duplication.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=676117

 panels/network/net-device-ethernet.c |   11 +---
 panels/network/net-device-mobile.c   |   16 +----
 panels/network/net-device-simple.c   |   16 +----
 panels/network/net-device-wifi.c     |    5 +-
 panels/network/net-virtual-device.c  |   11 +---
 panels/network/panel-common.c        |  110 ++++++++++++++++++++++++----------
 panels/network/panel-common.h        |    6 +-
 7 files changed, 90 insertions(+), 85 deletions(-)
---
diff --git a/panels/network/net-device-ethernet.c b/panels/network/net-device-ethernet.c
index 0cd3b14..186b057 100644
--- a/panels/network/net-device-ethernet.c
+++ b/panels/network/net-device-ethernet.c
@@ -208,7 +208,6 @@ device_ethernet_refresh_ui (NetDeviceEthernet *device)
         NMDevice *nm_device;
         NMDeviceState state;
         GtkWidget *widget;
-        GString *status;
         gchar *speed = NULL;
 
         nm_device = net_device_get_nm_device (NET_DEVICE (device));
@@ -230,17 +229,9 @@ device_ethernet_refresh_ui (NetDeviceEthernet *device)
         gtk_switch_set_active (GTK_SWITCH (widget), device_state_to_off_switch (state));
         device->updating_device = FALSE;
 
-        widget = GTK_WIDGET (gtk_builder_get_object (device->builder, "label_status"));
-        status = g_string_new (panel_device_state_to_localized_string (nm_device));
         if (state != NM_DEVICE_STATE_UNAVAILABLE)
                 speed = net_device_simple_get_speed (NET_DEVICE_SIMPLE (device));
-        if (speed) {
-                if (status->len)
-                        g_string_append (status, " - ");
-                g_string_append (status, speed);
-        }
-        gtk_label_set_label (GTK_LABEL (widget), status->str);
-        g_string_free (status, TRUE);
+        panel_set_device_status (device->builder, "label_status", nm_device, speed);
 
         populate_ui (device);
 }
diff --git a/panels/network/net-device-mobile.c b/panels/network/net-device-mobile.c
index 4b68783..9afbdb5 100644
--- a/panels/network/net-device-mobile.c
+++ b/panels/network/net-device-mobile.c
@@ -247,10 +247,8 @@ static void
 nm_device_mobile_refresh_ui (NetDeviceMobile *device_mobile)
 {
         gboolean is_connected;
-        GString *status;
         GtkListStore *liststore;
         GtkWidget *widget;
-        guint speed = 0;
         NetDeviceMobilePrivate *priv = device_mobile->priv;
         NMClient *client;
         NMDeviceModemCapabilities caps;
@@ -268,18 +266,8 @@ nm_device_mobile_refresh_ui (NetDeviceMobile *device_mobile)
         client = net_object_get_client (NET_OBJECT (device_mobile));
         mobilebb_enabled_toggled (client, NULL, device_mobile);
 
-        /* set device state, with status and optionally speed */
-        widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "label_status"));
-        status = g_string_new (panel_device_state_to_localized_string (nm_device));
-        if (speed  > 0) {
-                if (status->len)
-                        g_string_append (status, " - ");
-                /* Translators: network device speed */
-                g_string_append_printf (status, _("%d Mb/s"), speed);
-        }
-        gtk_label_set_label (GTK_LABEL (widget), status->str);
-        g_string_free (status, TRUE);
-        gtk_widget_set_tooltip_text (widget, panel_device_state_reason_to_localized_string (nm_device));
+        /* set device state, with status */
+        panel_set_device_status (device_mobile->priv->builder, "label_status", nm_device, NULL);
 
         /* sensitive for other connection types if the device is currently connected */
         widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder,
diff --git a/panels/network/net-device-simple.c b/panels/network/net-device-simple.c
index f7a1dfc..0b936ed 100644
--- a/panels/network/net-device-simple.c
+++ b/panels/network/net-device-simple.c
@@ -93,13 +93,12 @@ update_off_switch_from_device_state (GtkSwitch *sw,
 static void
 nm_device_simple_refresh_ui (NetDeviceSimple *device_simple)
 {
+        NetDeviceSimplePrivate *priv = device_simple->priv;
         char *hwaddr;
-        GString *status;
         GtkWidget *widget;
         char *speed = NULL;
         NMDevice *nm_device;
         NMDeviceState state;
-        NetDeviceSimplePrivate *priv = device_simple->priv;
 
         nm_device = net_device_get_nm_device (NET_DEVICE (device_simple));
 
@@ -124,20 +123,9 @@ nm_device_simple_refresh_ui (NetDeviceSimple *device_simple)
         gtk_widget_set_visible (widget, state != NM_DEVICE_STATE_UNMANAGED);
 
         /* set device state, with status and optionally speed */
-        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_status"));
-        status = g_string_new (panel_device_state_to_localized_string (nm_device));
         if (state != NM_DEVICE_STATE_UNAVAILABLE)
                 speed = net_device_simple_get_speed (device_simple);
-        if (speed) {
-                if (status->len)
-                        g_string_append (status, " - ");
-                g_string_append (status, speed);
-                g_free (speed);
-        }
-        gtk_label_set_label (GTK_LABEL (widget), status->str);
-        g_string_free (status, TRUE);
-        gtk_widget_set_tooltip_text (widget,
-                                     panel_device_state_reason_to_localized_string (nm_device));
+        panel_set_device_status (priv->builder, "label_status", nm_device, speed);
 
         /* device MAC */
         g_object_get (G_OBJECT (nm_device),
diff --git a/panels/network/net-device-wifi.c b/panels/network/net-device-wifi.c
index aa8b73d..13093ba 100644
--- a/panels/network/net-device-wifi.c
+++ b/panels/network/net-device-wifi.c
@@ -494,7 +494,6 @@ nm_device_wifi_refresh_ui (NetDeviceWifi *device_wifi)
         const gchar *str;
         gboolean is_hotspot;
         gchar *str_tmp = NULL;
-        GtkWidget *widget;
         gint strength = 0;
         guint speed = 0;
         NMAccessPoint *active_ap;
@@ -595,9 +594,7 @@ nm_device_wifi_refresh_ui (NetDeviceWifi *device_wifi)
         else
                 panel_set_device_widget_details (priv->builder, "last_used", NULL);
 
-        widget = GTK_WIDGET (gtk_builder_get_object (device_wifi->priv->builder, "heading_status"));
-        gtk_label_set_label (GTK_LABEL (widget),
-                             panel_device_state_to_localized_string (nm_device));
+        panel_set_device_status (priv->builder, "heading_status", nm_device, NULL);
 
         /* update list of APs */
         populate_ap_list (device_wifi);
diff --git a/panels/network/net-virtual-device.c b/panels/network/net-virtual-device.c
index f8567f9..b1c2378 100644
--- a/panels/network/net-virtual-device.c
+++ b/panels/network/net-virtual-device.c
@@ -153,7 +153,6 @@ net_virtual_device_refresh (NetObject *object)
         NetVirtualDevice *virtual_device = NET_VIRTUAL_DEVICE (object);
         NetVirtualDevicePrivate *priv = virtual_device->priv;
         char *hwaddr;
-        const char *status, *tooltip;
         GtkWidget *widget;
         NMDevice *nm_device;
         NMDeviceState state;
@@ -174,16 +173,12 @@ net_virtual_device_refresh (NetObject *object)
         update_off_switch_from_device_state (GTK_SWITCH (widget), state, virtual_device);
 
         /* set device state, with status and optionally speed */
-        widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_status"));
         if (nm_device) {
-                status = panel_device_state_to_localized_string (nm_device);
-                tooltip = panel_device_state_reason_to_localized_string (nm_device);
+                panel_set_device_status (priv->builder, "label_status", nm_device, NULL);
         } else {
-                status = "";
-                tooltip = NULL;
+                widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_status"));
+                gtk_label_set_label (GTK_LABEL (widget), "");
         }
-        gtk_label_set_label (GTK_LABEL (widget), status);
-        gtk_widget_set_tooltip_text (widget, tooltip);
 
         /* device MAC */
         if (nm_device) {
diff --git a/panels/network/panel-common.c b/panels/network/panel-common.c
index b71eee2..452f02d 100644
--- a/panels/network/panel-common.c
+++ b/panels/network/panel-common.c
@@ -27,6 +27,7 @@
 #include <gtk/gtk.h>
 
 #include <nm-device-ethernet.h>
+#include <nm-device-infiniband.h>
 #include <nm-device-modem.h>
 #include <nm-utils.h>
 
@@ -180,19 +181,11 @@ panel_ap_mode_to_localized_string (NM80211Mode mode)
         return value;
 }
 
-/**
- * panel_device_state_to_localized_string:
- **/
-const gchar *
-panel_device_state_to_localized_string (NMDevice *device)
+static const gchar *
+device_state_to_localized_string (NMDeviceState state)
 {
-        NMDeviceType type;
-        NMDeviceState state;
-
-        type = nm_device_get_device_type (device);
-        state = nm_device_get_state (device);
-
         const gchar *value = NULL;
+
         switch (state) {
         case NM_DEVICE_STATE_UNKNOWN:
                 /* TRANSLATORS: device status */
@@ -203,20 +196,11 @@ panel_device_state_to_localized_string (NMDevice *device)
                 value = _("Unmanaged");
                 break;
         case NM_DEVICE_STATE_UNAVAILABLE:
-                if (nm_device_get_firmware_missing (device)) {
-                        /* TRANSLATORS: device status */
-                        value = _("Firmware missing");
-                } else if (type == NM_DEVICE_TYPE_ETHERNET &&
-                           !nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (device))) {
-                        /* TRANSLATORS: device status */
-                        value = _("Cable unplugged");
-                } else {
-                        /* TRANSLATORS: device status */
-                        value = _("Unavailable");
-                }
+                /* TRANSLATORS: device status */
+                value = _("Unavailable");
                 break;
         case NM_DEVICE_STATE_DISCONNECTED:
-                value = "";
+                value = NULL;
                 break;
         case NM_DEVICE_STATE_PREPARE:
         case NM_DEVICE_STATE_CONFIG:
@@ -291,16 +275,17 @@ panel_vpn_state_to_localized_string (NMVPNConnectionState type)
         return value;
 }
 
-/**
- * panel_device_state_reason_to_localized_string:
- **/
-const gchar *
-panel_device_state_reason_to_localized_string (NMDevice *device)
+static const gchar *
+device_state_reason_to_localized_string (NMDevice *device)
 {
         const gchar *value = NULL;
         NMDeviceStateReason state_reason;
 
-        /* we only want the StateReason's we care about */
+        /* This only covers NMDeviceStateReasons that explain why a connection
+         * failed / can't be attempted, and aren't redundant with the state
+         * (eg, NM_DEVICE_STATE_REASON_CARRIER).
+         */
+
         nm_device_get_state_reason (device, &state_reason);
         switch (state_reason) {
         case NM_DEVICE_STATE_REASON_CONFIG_FAILED:
@@ -435,10 +420,6 @@ panel_device_state_reason_to_localized_string (NMDevice *device)
                 /* TRANSLATORS: device status reason */
                 value = _("Connection disappeared");
                 break;
-        case NM_DEVICE_STATE_REASON_CARRIER:
-                /* TRANSLATORS: device status reason */
-                value = _("Carrier/link changed");
-                break;
         case NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED:
                 /* TRANSLATORS: device status reason */
                 value = _("Existing connection was assumed");
@@ -483,6 +464,69 @@ panel_device_state_reason_to_localized_string (NMDevice *device)
         return value;
 }
 
+static gchar *
+device_status_to_localized_string (NMDevice *nm_device,
+                                   const gchar *speed)
+{
+        NMDeviceState state;
+        GString *string;
+        const gchar *state_str = NULL, *reason_str = NULL;
+
+        string = g_string_new (NULL);
+
+        state = nm_device_get_state (nm_device);
+        if (state == NM_DEVICE_STATE_UNAVAILABLE) {
+                if (nm_device_get_firmware_missing (nm_device)) {
+                        /* TRANSLATORS: device status */
+                        state_str = _("Firmware missing");
+                } else if (NM_IS_DEVICE_ETHERNET (nm_device) &&
+                           !nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (nm_device))) {
+                        /* TRANSLATORS: device status */
+                        state_str = _("Cable unplugged");
+                } else if (NM_IS_DEVICE_INFINIBAND (nm_device) &&
+                           !nm_device_infiniband_get_carrier (NM_DEVICE_INFINIBAND (nm_device))) {
+                        state_str = _("Cable unplugged");
+                }
+        }
+        if (!state_str)
+                state_str = device_state_to_localized_string (state);
+        if (state_str)
+                g_string_append (string, state_str);
+
+        if (state > NM_DEVICE_STATE_UNAVAILABLE && speed) {
+                if (string->len)
+                        g_string_append (string, " - ");
+                g_string_append (string, speed);
+        } else if (state == NM_DEVICE_STATE_UNAVAILABLE ||
+                   state == NM_DEVICE_STATE_DISCONNECTED ||
+                   state == NM_DEVICE_STATE_DEACTIVATING ||
+                   state == NM_DEVICE_STATE_FAILED) {
+                reason_str = device_state_reason_to_localized_string (nm_device);
+                if (*reason_str) {
+                        if (string->len)
+                                g_string_append (string, " - ");
+                        g_string_append (string, reason_str);
+                }
+        }
+
+        return g_string_free (string, FALSE);
+}
+
+void
+panel_set_device_status (GtkBuilder *builder,
+                         const gchar *label_name,
+                         NMDevice *nm_device,
+                         const gchar *speed)
+{
+        GtkLabel *label;
+        gchar *status;
+
+        label = GTK_LABEL (gtk_builder_get_object (builder, label_name));
+        status = device_status_to_localized_string (nm_device, speed);
+        gtk_label_set_label (label, status);
+        g_free (status);
+}
+
 gboolean
 panel_set_device_widget_details (GtkBuilder *builder,
                                  const gchar *widget_suffix,
diff --git a/panels/network/panel-common.h b/panels/network/panel-common.h
index 89e75e8..279abe2 100644
--- a/panels/network/panel-common.h
+++ b/panels/network/panel-common.h
@@ -35,9 +35,11 @@ const gchar     *panel_device_to_icon_name                     (NMDevice *device
 const gchar     *panel_device_to_localized_string              (NMDevice *device);
 const gchar     *panel_device_to_sortable_string               (NMDevice *device);
 const gchar     *panel_ap_mode_to_localized_string             (NM80211Mode mode);
-const gchar     *panel_device_state_to_localized_string        (NMDevice *device);
 const gchar     *panel_vpn_state_to_localized_string           (NMVPNConnectionState type);
-const gchar     *panel_device_state_reason_to_localized_string (NMDevice *device);
+void             panel_set_device_status                       (GtkBuilder *builder,
+                                                                const gchar *label_name,
+                                                                NMDevice *nm_device,
+                                                                const gchar *speed);
 gboolean         panel_set_device_widget_details               (GtkBuilder *builder,
                                                                 const gchar *widget_suffix,
                                                                 const gchar *value);


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