From ae9a389f83db79310991d24c85d766281ee01d19 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 26 Apr 2013 11:49:57 -0400 Subject: [PATCH] libnm-glib, cli: add nm_device_get_type_description Add a new libnm-glib method to get the type description for a device, and use it in nmcli. For most types, the type description is based on the class name, but for NMDeviceGeneric, it comes from the :type-description property. --- cli/src/devices.c | 47 ++-------------------------------- libnm-glib/libnm-glib.ver | 1 + libnm-glib/nm-device-generic.c | 10 ++++++++ libnm-glib/nm-device-modem.c | 16 ++++++++++++ libnm-glib/nm-device.c | 38 +++++++++++++++++++++++++++ libnm-glib/nm-device.h | 4 ++- src/platform/tests/dump.c | 23 +---------------- 7 files changed, 71 insertions(+), 68 deletions(-) diff --git a/cli/src/devices.c b/cli/src/devices.c index 43f8bd0675..ca38b1ede6 100644 --- a/cli/src/devices.c +++ b/cli/src/devices.c @@ -286,49 +286,6 @@ quit (void) g_main_loop_quit (loop); /* quit main loop */ } -/* Convert device type to string. Use setting names strings to match with - * connection type names. - */ -static const char * -device_type_to_string (NMDevice *device) -{ - NMDeviceModemCapabilities caps = NM_DEVICE_MODEM_CAPABILITY_NONE; - - switch (nm_device_get_device_type (device)) { - case NM_DEVICE_TYPE_ETHERNET: - return NM_SETTING_WIRED_SETTING_NAME; - case NM_DEVICE_TYPE_ADSL: - return NM_SETTING_ADSL_SETTING_NAME; - case NM_DEVICE_TYPE_WIFI: - return NM_SETTING_WIRELESS_SETTING_NAME; - case NM_DEVICE_TYPE_MODEM: - caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device)); - if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) - return NM_SETTING_GSM_SETTING_NAME; - else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) - return NM_SETTING_CDMA_SETTING_NAME; - return _("Unknown"); - case NM_DEVICE_TYPE_BT: - return NM_SETTING_BLUETOOTH_SETTING_NAME; - case NM_DEVICE_TYPE_OLPC_MESH: - return NM_SETTING_OLPC_MESH_SETTING_NAME; -#if WITH_WIMAX - case NM_DEVICE_TYPE_WIMAX: - return NM_SETTING_WIMAX_SETTING_NAME; -#endif - case NM_DEVICE_TYPE_INFINIBAND: - return NM_SETTING_INFINIBAND_SETTING_NAME; - case NM_DEVICE_TYPE_BOND: - return NM_SETTING_BOND_SETTING_NAME; - case NM_DEVICE_TYPE_VLAN: - return NM_SETTING_VLAN_SETTING_NAME; - case NM_DEVICE_TYPE_BRIDGE: - return NM_SETTING_BRIDGE_SETTING_NAME; - default: - return _("Unknown"); - } -} - static char * ap_wpa_rsn_flags_to_string (NM80211ApSecurityFlags flags) { @@ -619,7 +576,7 @@ show_device_info (gpointer data, gpointer user_data) nmc->allowed_fields[0].value = (char *) nmc_fields_dev_show_sections[0].name; /* "GENERAL"*/ nmc->allowed_fields[1].value = (char *) nm_device_get_iface (device); - nmc->allowed_fields[2].value = (char *) device_type_to_string (device); + nmc->allowed_fields[2].value = (char *) nm_device_get_type_description (device); nmc->allowed_fields[3].value = (char *) nm_device_get_vendor (device); nmc->allowed_fields[4].value = (char *) nm_device_get_product (device); nmc->allowed_fields[5].value = (char *) (nm_device_get_driver (device) ? nm_device_get_driver (device) : _("(unknown)")); @@ -953,7 +910,7 @@ static void show_device_status (NMDevice *device, NmCli *nmc) { nmc->allowed_fields[0].value = (char *) nm_device_get_iface (device); - nmc->allowed_fields[1].value = (char *) device_type_to_string (device); + nmc->allowed_fields[1].value = (char *) nm_device_get_type_description (device); nmc->allowed_fields[2].value = (char *) nmc_device_state_to_string (nm_device_get_state (device)); nmc->allowed_fields[3].value = (char *) nm_object_get_path (NM_OBJECT (device)); diff --git a/libnm-glib/libnm-glib.ver b/libnm-glib/libnm-glib.ver index d066067cfa..b4342f4b22 100644 --- a/libnm-glib/libnm-glib.ver +++ b/libnm-glib/libnm-glib.ver @@ -126,6 +126,7 @@ global: nm_device_get_state; nm_device_get_state_reason; nm_device_get_type; + nm_device_get_type_description; nm_device_get_udi; nm_device_get_vendor; nm_device_infiniband_error_get_type; diff --git a/libnm-glib/nm-device-generic.c b/libnm-glib/nm-device-generic.c index b69070fdd2..fb0b693420 100644 --- a/libnm-glib/nm-device-generic.c +++ b/libnm-glib/nm-device-generic.c @@ -119,6 +119,15 @@ nm_device_generic_get_hw_address (NMDeviceGeneric *device) /***********************************************************/ +static const char * +get_type_description (NMDevice *device) +{ + NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (device); + + _nm_object_ensure_inited (NM_OBJECT (device)); + return priv->type_description; +} + static gboolean connection_compatible (NMDevice *device, NMConnection *connection, GError **error) { @@ -235,6 +244,7 @@ nm_device_generic_class_init (NMDeviceGenericClass *klass) object_class->finalize = finalize; object_class->get_property = get_property; + device_class->get_type_description = get_type_description; device_class->connection_compatible = connection_compatible; /** diff --git a/libnm-glib/nm-device-modem.c b/libnm-glib/nm-device-modem.c index 35c757cb6a..d7f48ce616 100644 --- a/libnm-glib/nm-device-modem.c +++ b/libnm-glib/nm-device-modem.c @@ -114,6 +114,20 @@ nm_device_modem_get_current_capabilities (NMDeviceModem *self) return NM_DEVICE_MODEM_GET_PRIVATE (self)->current_caps; } +static const char * +get_type_description (NMDevice *device) +{ + NMDeviceModemCapabilities caps; + + caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device)); + if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) + return "gsm"; + else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) + return "cdma"; + else + return NULL; +} + static gboolean connection_compatible (NMDevice *device, NMConnection *connection, GError **error) { @@ -232,6 +246,8 @@ nm_device_modem_class_init (NMDeviceModemClass *modem_class) object_class->constructed = constructed; object_class->get_property = get_property; object_class->dispose = dispose; + + device_class->get_type_description = get_type_description; device_class->connection_compatible = connection_compatible; /** diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c index cda3a26381..049b591933 100644 --- a/libnm-glib/nm-device.c +++ b/libnm-glib/nm-device.c @@ -77,6 +77,7 @@ typedef struct { char *driver; char *driver_version; char *firmware_version; + char *type_description; NMDeviceCapabilities capabilities; gboolean managed; gboolean firmware_missing; @@ -1083,6 +1084,43 @@ nm_device_get_firmware_version (NMDevice *device) return NM_DEVICE_GET_PRIVATE (device)->firmware_version; } +/** + * nm_device_get_type_description: + * @device: a #NMDevice + * + * Gets a (non-localized) description of the type of device that + * @device is. + * + * Returns: the type description of the device. This is the internal + * string used by the device, and must not be modified. + * + * Since: 0.9.10 + **/ +const char * +nm_device_get_type_description (NMDevice *device) +{ + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device); + const char *desc, *typename; + + g_return_val_if_fail (NM_IS_DEVICE (device), NULL); + + if (priv->type_description) + return priv->type_description; + + if (NM_DEVICE_GET_CLASS (device)->get_type_description) { + desc = NM_DEVICE_GET_CLASS (device)->get_type_description (device); + if (desc) + return desc; + } + + typename = G_OBJECT_TYPE_NAME (device); + if (g_str_has_prefix (typename, "NMDevice")) + typename += 8; + priv->type_description = g_ascii_strdown (typename, -1); + + return priv->type_description; +} + /** * nm_device_get_capabilities: * @device: a #NMDevice diff --git a/libnm-glib/nm-device.h b/libnm-glib/nm-device.h index 0f0e277629..ed274caa11 100644 --- a/libnm-glib/nm-device.h +++ b/libnm-glib/nm-device.h @@ -98,13 +98,14 @@ typedef struct { NMConnection *connection, GError **error); + const char * (*get_type_description) (NMDevice *device); + /* Padding for future expansion */ void (*_reserved1) (void); void (*_reserved2) (void); void (*_reserved3) (void); void (*_reserved4) (void); void (*_reserved5) (void); - void (*_reserved6) (void); } NMDeviceClass; GType nm_device_get_type (void); @@ -118,6 +119,7 @@ const char * nm_device_get_udi (NMDevice *device); const char * nm_device_get_driver (NMDevice *device); const char * nm_device_get_driver_version (NMDevice *device); const char * nm_device_get_firmware_version (NMDevice *device); +const char * nm_device_get_type_description (NMDevice *device); NMDeviceCapabilities nm_device_get_capabilities (NMDevice *device); gboolean nm_device_get_managed (NMDevice *device); gboolean nm_device_get_autoconnect (NMDevice *device); diff --git a/src/platform/tests/dump.c b/src/platform/tests/dump.c index 80f2aa0139..d5397032ea 100644 --- a/src/platform/tests/dump.c +++ b/src/platform/tests/dump.c @@ -6,27 +6,6 @@ #include "nm-linux-platform.h" #include "nm-fake-platform.h" -static const char * -type_to_string (NMLinkType type) -{ - switch (type) { - case NM_LINK_TYPE_LOOPBACK: - return "loopback"; - case NM_LINK_TYPE_ETHERNET: - return "ethernet"; - case NM_LINK_TYPE_DUMMY: - return "dummy"; - case NM_LINK_TYPE_BRIDGE: - return "bridge"; - case NM_LINK_TYPE_BOND: - return "bond"; - case NM_LINK_TYPE_TEAM: - return "team"; - default: - return "unknown-type"; - } -} - static void dump_interface (NMPlatformLink *link) { @@ -45,7 +24,7 @@ dump_interface (NMPlatformLink *link) g_assert (link->up || !link->connected); - printf ("%d: %s: %s", link->ifindex, link->name, type_to_string (link->type)); + printf ("%d: %s: %s", link->ifindex, link->name, link->type_name); if (link->up) printf (" %s", link->connected ? "CONNECTED" : "DISCONNECTED"); else