device: add nm_device_get_type_description() function

Add a function to get a concise representation of the
device type.

libnm already has nm_device_get_type_description() for that
and it is shown by

  nmcli -f GENERAL.TYPE device show

Reimplement that function for nm-core. Just take care that the
two implementations don't diverge.
This commit is contained in:
Thomas Haller 2015-05-15 15:00:07 +02:00
parent ec972ad305
commit e9e9d44468
5 changed files with 60 additions and 1 deletions

View file

@ -1000,6 +1000,9 @@ nm_device_get_type_description (NMDevice *device)
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
const char *desc, *typename;
/* BEWARE: this function should return the same value
* as nm_device_get_type_description() in nm-core. */
g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
if (priv->type_description)

View file

@ -54,6 +54,14 @@ get_generic_capabilities (NMDevice *dev)
return NM_DEVICE_CAP_NONE;
}
static const char *
get_type_description (NMDevice *device)
{
if (NM_DEVICE_GENERIC_GET_PRIVATE (device)->type_description)
return NM_DEVICE_GENERIC_GET_PRIVATE (device)->type_description;
return NM_DEVICE_CLASS (nm_device_generic_parent_class)->get_type_description (device);
}
static gboolean
check_connection_compatible (NMDevice *device, NMConnection *connection)
{
@ -184,6 +192,7 @@ nm_device_generic_class_init (NMDeviceGenericClass *klass)
object_class->set_property = set_property;
parent_class->get_generic_capabilities = get_generic_capabilities;
parent_class->get_type_description = get_type_description;
parent_class->check_connection_compatible = check_connection_compatible;
parent_class->update_connection = update_connection;

View file

@ -191,6 +191,7 @@ typedef struct {
int ip_ifindex;
NMDeviceType type;
char * type_desc;
char * type_description;
NMDeviceCapabilities capabilities;
char * driver;
char * driver_version;
@ -824,6 +825,34 @@ nm_device_get_type_desc (NMDevice *self)
return NM_DEVICE_GET_PRIVATE (self)->type_desc;
}
const char *
nm_device_get_type_description (NMDevice *self)
{
g_return_val_if_fail (self != NULL, NULL);
/* Beware: this function should return the same
* value as nm_device_get_type_description() in libnm. */
return NM_DEVICE_GET_CLASS (self)->get_type_description (self);
}
static const char *
get_type_description (NMDevice *self)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
if (!priv->type_description) {
const char *typename;
typename = G_OBJECT_TYPE_NAME (self);
if (g_str_has_prefix (typename, "NMDevice"))
typename += 8;
priv->type_description = g_ascii_strdown (typename, -1);
}
return priv->type_description;
}
gboolean
nm_device_has_carrier (NMDevice *self)
{
@ -8788,6 +8817,7 @@ finalize (GObject *object)
g_free (priv->driver_version);
g_free (priv->firmware_version);
g_free (priv->type_desc);
g_free (priv->type_description);
g_free (priv->dhcp_anycast_address);
g_hash_table_unref (priv->ip6_saved_properties);
@ -8805,7 +8835,7 @@ set_property (GObject *object, guint prop_id,
NMPlatformLink *platform_device;
const char *hw_addr, *p;
guint count;
switch (prop_id) {
case PROP_PLATFORM_DEVICE:
platform_device = g_value_get_pointer (value);
@ -9073,6 +9103,7 @@ nm_device_class_init (NMDeviceClass *klass)
klass->act_stage4_ip6_config_timeout = act_stage4_ip6_config_timeout;
klass->have_any_ready_slaves = have_any_ready_slaves;
klass->get_type_description = get_type_description;
klass->spec_match_list = spec_match_list;
klass->can_auto_connect = can_auto_connect;
klass->check_connection_compatible = check_connection_compatible;

View file

@ -212,6 +212,8 @@ typedef struct {
/* Sync deactivating (in the DISCONNECTED phase) */
void (* deactivate) (NMDevice *self);
const char *(*get_type_description) (NMDevice *self);
NMMatchSpecMatchType (* spec_match_list) (NMDevice *self, const GSList *specs);
/* Update the connection with currently configured L2 settings */
@ -278,6 +280,7 @@ int nm_device_get_ip_ifindex(NMDevice *dev);
const char * nm_device_get_driver (NMDevice *dev);
const char * nm_device_get_driver_version (NMDevice *dev);
const char * nm_device_get_type_desc (NMDevice *dev);
const char * nm_device_get_type_description (NMDevice *dev);
NMDeviceType nm_device_get_device_type (NMDevice *dev);
int nm_device_get_priority (NMDevice *dev);

View file

@ -372,6 +372,18 @@ get_generic_capabilities (NMDevice *device)
return NM_DEVICE_CAP_IS_NON_KERNEL;
}
static const char *
get_type_description (NMDevice *device)
{
NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (device);
if (NM_FLAGS_HAS (priv->current_caps, NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS))
return "gsm";
if (NM_FLAGS_HAS (priv->current_caps, NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO))
return "cdma";
return NM_DEVICE_CLASS (nm_device_modem_parent_class)->get_type_description (device);
}
static gboolean
check_connection_compatible (NMDevice *device, NMConnection *connection)
{
@ -741,6 +753,7 @@ nm_device_modem_class_init (NMDeviceModemClass *mclass)
object_class->constructed = constructed;
device_class->get_generic_capabilities = get_generic_capabilities;
device_class->get_type_description = get_type_description;
device_class->check_connection_compatible = check_connection_compatible;
device_class->check_connection_available = check_connection_available;
device_class->complete_connection = complete_connection;