network: disambiguate devices of the same type

If there are multiple devices that would end up with the same name in
the device list, disambiguate their names via new libnm-gtk API.

https://bugzilla.gnome.org/show_bug.cgi?id=677143
This commit is contained in:
Dan Winship 2012-09-27 16:15:19 -04:00 committed by Bastien Nocera
parent 71c0325f28
commit 63756458b2
7 changed files with 88 additions and 64 deletions

View file

@ -29,6 +29,7 @@
#include "nm-client.h" #include "nm-client.h"
#include "nm-device.h" #include "nm-device.h"
#include "nm-device-modem.h" #include "nm-device-modem.h"
#include <libnm-gtk/nm-ui-utils.h>
#include "net-device.h" #include "net-device.h"
#include "net-device-mobile.h" #include "net-device-mobile.h"
@ -381,6 +382,54 @@ panel_refresh_killswitch_visibility (CcNetworkPanel *panel)
show_flight_toggle); show_flight_toggle);
} }
static void
panel_refresh_device_titles (CcNetworkPanel *panel)
{
GPtrArray *ndarray, *nmdarray;
NetDevice **devices;
NMDevice **nm_devices;
gchar **titles;
gint i, num_devices;
NetObject *object;
GtkTreeIter iter;
GtkTreeModel *model;
model = GTK_TREE_MODEL (gtk_builder_get_object (panel->priv->builder,
"liststore_devices"));
if (!gtk_tree_model_get_iter_first (model, &iter))
return;
ndarray = g_ptr_array_new_with_free_func (g_object_unref);
nmdarray = g_ptr_array_new ();
do {
gtk_tree_model_get (model, &iter,
PANEL_DEVICES_COLUMN_OBJECT, &object,
-1);
if (NET_IS_DEVICE (object)) {
g_ptr_array_add (ndarray, object);
g_ptr_array_add (nmdarray, net_device_get_nm_device (NET_DEVICE (object)));
}
} while (gtk_tree_model_iter_next (model, &iter));
if (!ndarray->len) {
g_ptr_array_free (ndarray, TRUE);
g_ptr_array_free (nmdarray, TRUE);
return;
}
devices = (NetDevice **)ndarray->pdata;
nm_devices = (NMDevice **)nmdarray->pdata;
num_devices = ndarray->len;
titles = nma_utils_disambiguate_device_names (nm_devices, num_devices);
for (i = 0; i < num_devices; i++) {
net_object_set_title (NET_OBJECT (devices[i]), titles[i]);
g_free (titles[i]);
}
g_free (titles);
g_ptr_array_free (ndarray, TRUE);
g_ptr_array_free (nmdarray, TRUE);
}
static gboolean static gboolean
handle_argv_for_device (CcNetworkPanel *panel, handle_argv_for_device (CcNetworkPanel *panel,
NMDevice *device, NMDevice *device,
@ -471,7 +520,6 @@ handle_argv (CcNetworkPanel *panel)
static gboolean static gboolean
panel_add_device (CcNetworkPanel *panel, NMDevice *device) panel_add_device (CcNetworkPanel *panel, NMDevice *device)
{ {
const gchar *title;
GtkListStore *liststore_devices; GtkListStore *liststore_devices;
GtkTreeIter iter; GtkTreeIter iter;
NMDeviceType type; NMDeviceType type;
@ -506,7 +554,6 @@ panel_add_device (CcNetworkPanel *panel, NMDevice *device)
} }
/* create device */ /* create device */
title = panel_device_to_localized_string (device);
net_device = g_object_new (device_g_type, net_device = g_object_new (device_g_type,
"panel", panel, "panel", panel,
"removable", FALSE, "removable", FALSE,
@ -515,7 +562,6 @@ panel_add_device (CcNetworkPanel *panel, NMDevice *device)
"remote-settings", panel->priv->remote_settings, "remote-settings", panel->priv->remote_settings,
"nm-device", device, "nm-device", device,
"id", nm_device_get_udi (device), "id", nm_device_get_udi (device),
"title", title,
NULL); NULL);
/* add as a panel */ /* add as a panel */
@ -538,7 +584,6 @@ panel_add_device (CcNetworkPanel *panel, NMDevice *device)
&iter, &iter,
PANEL_DEVICES_COLUMN_ICON, panel_device_to_icon_name (device), PANEL_DEVICES_COLUMN_ICON, panel_device_to_icon_name (device),
PANEL_DEVICES_COLUMN_SORT, panel_device_to_sortable_string (device), PANEL_DEVICES_COLUMN_SORT, panel_device_to_sortable_string (device),
PANEL_DEVICES_COLUMN_TITLE, title,
PANEL_DEVICES_COLUMN_OBJECT, net_device, PANEL_DEVICES_COLUMN_OBJECT, net_device,
-1); -1);
@ -576,6 +621,25 @@ panel_remove_device (CcNetworkPanel *panel, NMDevice *device)
} while (gtk_tree_model_iter_next (model, &iter)); } while (gtk_tree_model_iter_next (model, &iter));
} }
static void
get_object_title (GtkTreeViewColumn *column,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
NetObject *object;
gtk_tree_model_get (model, iter,
PANEL_DEVICES_COLUMN_OBJECT, &object,
-1);
if (!object)
return;
g_object_set (cell, "text", net_object_get_title (object), NULL);
g_object_unref (object);
}
static void static void
panel_add_devices_columns (CcNetworkPanel *panel, GtkTreeView *treeview) panel_add_devices_columns (CcNetworkPanel *panel, GtkTreeView *treeview)
{ {
@ -600,9 +664,11 @@ panel_add_devices_columns (CcNetworkPanel *panel, GtkTreeView *treeview)
"wrap-mode", PANGO_WRAP_WORD, "wrap-mode", PANGO_WRAP_WORD,
"ellipsize", PANGO_ELLIPSIZE_END, "ellipsize", PANGO_ELLIPSIZE_END,
NULL); NULL);
column = gtk_tree_view_column_new_with_attributes ("title", renderer, column = gtk_tree_view_column_new_with_attributes ("title", renderer, NULL);
"markup", PANEL_DEVICES_COLUMN_TITLE, gtk_tree_view_column_set_cell_data_func (GTK_TREE_VIEW_COLUMN (column),
NULL); renderer,
get_object_title,
NULL, NULL);
gtk_tree_view_column_set_sort_column_id (column, PANEL_DEVICES_COLUMN_SORT); gtk_tree_view_column_set_sort_column_id (column, PANEL_DEVICES_COLUMN_SORT);
liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (priv->builder, liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (priv->builder,
"liststore_devices")); "liststore_devices"));
@ -682,11 +748,11 @@ panel_add_proxy_device (CcNetworkPanel *panel)
liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (panel->priv->builder, liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (panel->priv->builder,
"liststore_devices")); "liststore_devices"));
title = g_strdup_printf ("%s", _("Network proxy")); title = g_strdup_printf ("%s", _("Network proxy"));
net_object_set_title (NET_OBJECT (proxy), title);
gtk_list_store_append (liststore_devices, &iter); gtk_list_store_append (liststore_devices, &iter);
gtk_list_store_set (liststore_devices, gtk_list_store_set (liststore_devices,
&iter, &iter,
PANEL_DEVICES_COLUMN_ICON, "preferences-system-network", PANEL_DEVICES_COLUMN_ICON, "preferences-system-network",
PANEL_DEVICES_COLUMN_TITLE, title,
PANEL_DEVICES_COLUMN_SORT, "9", PANEL_DEVICES_COLUMN_SORT, "9",
PANEL_DEVICES_COLUMN_OBJECT, proxy, PANEL_DEVICES_COLUMN_OBJECT, proxy,
-1); -1);
@ -746,6 +812,7 @@ device_added_cb (NMClient *client, NMDevice *device, CcNetworkPanel *panel)
g_debug ("New device added"); g_debug ("New device added");
panel_add_device (panel, device); panel_add_device (panel, device);
panel_refresh_killswitch_visibility (panel); panel_refresh_killswitch_visibility (panel);
panel_refresh_device_titles (panel);
} }
static void static void
@ -754,6 +821,7 @@ device_removed_cb (NMClient *client, NMDevice *device, CcNetworkPanel *panel)
g_debug ("Device removed"); g_debug ("Device removed");
panel_remove_device (panel, device); panel_remove_device (panel, device);
panel_refresh_killswitch_visibility (panel); panel_refresh_killswitch_visibility (panel);
panel_refresh_device_titles (panel);
} }
static void static void
@ -792,6 +860,8 @@ out:
select_first_device (panel); select_first_device (panel);
} }
panel_refresh_device_titles (panel);
g_debug ("Calling handle_argv() after cold-plugging devices"); g_debug ("Calling handle_argv() after cold-plugging devices");
handle_argv (panel); handle_argv (panel);
} }
@ -833,7 +903,6 @@ static void
panel_add_vpn_device (CcNetworkPanel *panel, NMConnection *connection) panel_add_vpn_device (CcNetworkPanel *panel, NMConnection *connection)
{ {
gchar *title; gchar *title;
gchar *title_markup;
GtkListStore *liststore_devices; GtkListStore *liststore_devices;
GtkTreeIter iter; GtkTreeIter iter;
NetVpn *net_vpn; NetVpn *net_vpn;
@ -869,19 +938,16 @@ panel_add_vpn_device (CcNetworkPanel *panel, NMConnection *connection)
liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (panel->priv->builder, liststore_devices = GTK_LIST_STORE (gtk_builder_get_object (panel->priv->builder,
"liststore_devices")); "liststore_devices"));
title = g_strdup_printf (_("%s VPN"), nm_connection_get_id (connection)); title = g_strdup_printf (_("%s VPN"), nm_connection_get_id (connection));
title_markup = g_strdup (title);
net_object_set_title (NET_OBJECT (net_vpn), title); net_object_set_title (NET_OBJECT (net_vpn), title);
gtk_list_store_append (liststore_devices, &iter); gtk_list_store_append (liststore_devices, &iter);
gtk_list_store_set (liststore_devices, gtk_list_store_set (liststore_devices,
&iter, &iter,
PANEL_DEVICES_COLUMN_ICON, "network-vpn", PANEL_DEVICES_COLUMN_ICON, "network-vpn",
PANEL_DEVICES_COLUMN_TITLE, title_markup,
PANEL_DEVICES_COLUMN_SORT, "5", PANEL_DEVICES_COLUMN_SORT, "5",
PANEL_DEVICES_COLUMN_OBJECT, net_vpn, PANEL_DEVICES_COLUMN_OBJECT, net_vpn,
-1); -1);
g_free (title); g_free (title);
g_free (title_markup);
} }
static void static void

View file

@ -240,11 +240,11 @@ nm_device_mobile_refresh_ui (NetDeviceMobile *device_mobile)
NMDeviceModemCapabilities caps; NMDeviceModemCapabilities caps;
NMDevice *nm_device; NMDevice *nm_device;
/* set device kind */
nm_device = net_device_get_nm_device (NET_DEVICE (device_mobile)); nm_device = net_device_get_nm_device (NET_DEVICE (device_mobile));
/* set device kind */
widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "label_device")); widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "label_device"));
gtk_label_set_label (GTK_LABEL (widget), g_object_bind_property (device_mobile, "title", widget, "label", 0);
panel_device_to_localized_string (nm_device));
/* set up the device on/off switch */ /* set up the device on/off switch */
widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "device_off_switch")); widget = GTK_WIDGET (gtk_builder_get_object (device_mobile->priv->builder, "device_off_switch"));

View file

@ -101,11 +101,11 @@ nm_device_simple_refresh_ui (NetDeviceSimple *device_simple)
NMDeviceState state; NMDeviceState state;
NetDeviceSimplePrivate *priv = device_simple->priv; NetDeviceSimplePrivate *priv = device_simple->priv;
/* set device kind */
nm_device = net_device_get_nm_device (NET_DEVICE (device_simple)); nm_device = net_device_get_nm_device (NET_DEVICE (device_simple));
/* set device kind */
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_device")); widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "label_device"));
gtk_label_set_label (GTK_LABEL (widget), g_object_bind_property (device_simple, "title", widget, "label", 0);
panel_device_to_localized_string (nm_device));
widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "image_device")); widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "image_device"));
gtk_image_set_from_icon_name (GTK_IMAGE (widget), gtk_image_set_from_icon_name (GTK_IMAGE (widget),
panel_device_to_icon_name (nm_device), panel_device_to_icon_name (nm_device),

View file

@ -780,7 +780,7 @@ nm_device_wifi_refresh_ui (NetDeviceWifi *device_wifi)
widget = GTK_WIDGET (gtk_builder_get_object (device_wifi->priv->builder, "label_device")); widget = GTK_WIDGET (gtk_builder_get_object (device_wifi->priv->builder, "label_device"));
gtk_label_set_label (GTK_LABEL (widget), gtk_label_set_label (GTK_LABEL (widget),
priv->selected_ssid_title ? priv->selected_ssid_title : panel_device_to_localized_string (nm_device)); priv->selected_ssid_title ? priv->selected_ssid_title : net_object_get_title (NET_OBJECT (device_wifi)));
/* only disconnect when connection active */ /* only disconnect when connection active */
if (ap == active_ap) { if (ap == active_ap) {

View file

@ -88,6 +88,7 @@ net_object_set_id (NetObject *object, const gchar *id)
{ {
g_return_if_fail (NET_IS_OBJECT (object)); g_return_if_fail (NET_IS_OBJECT (object));
object->priv->id = g_strdup (id); object->priv->id = g_strdup (id);
g_object_notify (G_OBJECT (object), "id");
} }
gboolean gboolean
@ -109,6 +110,7 @@ net_object_set_title (NetObject *object, const gchar *title)
{ {
g_return_if_fail (NET_IS_OBJECT (object)); g_return_if_fail (NET_IS_OBJECT (object));
object->priv->title = g_strdup (title); object->priv->title = g_strdup (title);
g_object_notify (G_OBJECT (object), "title");
} }
NMClient * NMClient *
@ -301,7 +303,7 @@ net_object_class_init (NetObjectClass *klass)
pspec = g_param_spec_string ("title", NULL, NULL, pspec = g_param_spec_string ("title", NULL, NULL,
NULL, NULL,
G_PARAM_READWRITE); G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
g_object_class_install_property (object_class, PROP_TITLE, pspec); g_object_class_install_property (object_class, PROP_TITLE, pspec);
pspec = g_param_spec_boolean ("removable", NULL, NULL, pspec = g_param_spec_boolean ("removable", NULL, NULL,

View file

@ -68,49 +68,6 @@ panel_device_to_icon_name (NMDevice *device)
return value; return value;
} }
/**
* panel_device_to_localized_string:
**/
const gchar *
panel_device_to_localized_string (NMDevice *device)
{
const gchar *value = NULL;
NMDeviceModemCapabilities caps;
switch (nm_device_get_device_type (device)) {
case NM_DEVICE_TYPE_UNKNOWN:
/* TRANSLATORS: device type */
value = _("Unknown");
break;
case NM_DEVICE_TYPE_ETHERNET:
/* TRANSLATORS: device type */
value = _("Wired");
break;
case NM_DEVICE_TYPE_WIFI:
/* TRANSLATORS: device type */
value = _("Wi-Fi");
break;
case NM_DEVICE_TYPE_MODEM:
caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
if ((caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) ||
(caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)) {
/* TRANSLATORS: device type */
value = _("Mobile broadband");
}
break;
case NM_DEVICE_TYPE_BT:
/* TRANSLATORS: device type */
value = _("Bluetooth");
break;
case NM_DEVICE_TYPE_OLPC_MESH:
/* TRANSLATORS: device type */
value = _("Mesh");
break;
default:
break;
}
return value;
}
/** /**
* panel_device_to_sortable_string: * panel_device_to_sortable_string:
* *

View file

@ -31,7 +31,6 @@
G_BEGIN_DECLS G_BEGIN_DECLS
const gchar *panel_device_to_icon_name (NMDevice *device); 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_device_to_sortable_string (NMDevice *device);
const gchar *panel_ap_mode_to_localized_string (NM80211Mode mode); const gchar *panel_ap_mode_to_localized_string (NM80211Mode mode);
const gchar *panel_device_state_to_localized_string (NMDevice *device); const gchar *panel_device_state_to_localized_string (NMDevice *device);