mixer-card: Improve GObject properties gunk a bit

Keep track of the `GParamSpec`s of the properties. This allows us to use
`g_object_notify_by_pspec()`, which is a bit more performant than
`g_object_notify()`(as it doesn't need to take a global lock to lookup
the property name). It also prevents accidental typos in the property
name at compile time.

Also always add `G_PARAM_STATIC_STRINGS`, to prevent some unnecessary
string duplications of property name, blurb and description.
This commit is contained in:
Niels De Graef 2022-05-29 13:44:56 +02:00 committed by Bastien Nocera
parent 9fa985ca25
commit 87a567eece

View File

@ -61,7 +61,9 @@ enum
PROP_ICON_NAME, PROP_ICON_NAME,
PROP_PROFILE, PROP_PROFILE,
PROP_HUMAN_PROFILE, PROP_HUMAN_PROFILE,
N_PROPS
}; };
static GParamSpec *obj_props[N_PROPS] = { NULL, };
static void gvc_mixer_card_finalize (GObject *object); static void gvc_mixer_card_finalize (GObject *object);
@ -117,7 +119,7 @@ gvc_mixer_card_set_name (GvcMixerCard *card,
g_free (card->priv->name); g_free (card->priv->name);
card->priv->name = g_strdup (name); card->priv->name = g_strdup (name);
g_object_notify (G_OBJECT (card), "name"); g_object_notify_by_pspec (G_OBJECT (card), obj_props[PROP_NAME]);
return TRUE; return TRUE;
} }
@ -137,7 +139,7 @@ gvc_mixer_card_set_icon_name (GvcMixerCard *card,
g_free (card->priv->icon_name); g_free (card->priv->icon_name);
card->priv->icon_name = g_strdup (icon_name); card->priv->icon_name = g_strdup (icon_name);
g_object_notify (G_OBJECT (card), "icon-name"); g_object_notify_by_pspec (G_OBJECT (card), obj_props[PROP_ICON_NAME]);
return TRUE; return TRUE;
} }
@ -191,7 +193,7 @@ gvc_mixer_card_set_profile (GvcMixerCard *card,
} }
} }
g_object_notify (G_OBJECT (card), "profile"); g_object_notify_by_pspec (G_OBJECT (card), obj_props[PROP_PROFILE]);
return TRUE; return TRUE;
} }
@ -468,54 +470,42 @@ gvc_mixer_card_class_init (GvcMixerCardClass *klass)
gobject_class->set_property = gvc_mixer_card_set_property; gobject_class->set_property = gvc_mixer_card_set_property;
gobject_class->get_property = gvc_mixer_card_get_property; gobject_class->get_property = gvc_mixer_card_get_property;
g_object_class_install_property (gobject_class, obj_props[PROP_INDEX] = g_param_spec_ulong ("index",
PROP_INDEX, "Index",
g_param_spec_ulong ("index", "The index for this card",
"Index", 0, G_MAXULONG, 0,
"The index for this card", G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
0, G_MAXULONG, 0, obj_props[PROP_ID] = g_param_spec_ulong ("id",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); "id",
g_object_class_install_property (gobject_class, "The id for this card",
PROP_ID, 0, G_MAXULONG, 0,
g_param_spec_ulong ("id", G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
"id", obj_props[PROP_PA_CONTEXT] = g_param_spec_pointer ("pa-context",
"The id for this card", "PulseAudio context",
0, G_MAXULONG, 0, "The PulseAudio context for this card",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, obj_props[PROP_NAME] = g_param_spec_string ("name",
PROP_PA_CONTEXT, "Name",
g_param_spec_pointer ("pa-context", "Name to display for this card",
"PulseAudio context", NULL,
"The PulseAudio context for this card", G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)); obj_props[PROP_ICON_NAME] = g_param_spec_string ("icon-name",
g_object_class_install_property (gobject_class, "Icon Name",
PROP_NAME, "Name of icon to display for this card",
g_param_spec_string ("name", NULL,
"Name", G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_STATIC_STRINGS);
"Name to display for this card", obj_props[PROP_PROFILE] = g_param_spec_string ("profile",
NULL, "Profile",
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); "Name of current profile for this card",
g_object_class_install_property (gobject_class, NULL,
PROP_ICON_NAME, G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
g_param_spec_string ("icon-name", obj_props[PROP_HUMAN_PROFILE] = g_param_spec_string ("human-profile",
"Icon Name", "Profile (Human readable)",
"Name of icon to display for this card", "Name of current profile for this card in human readable form",
NULL, NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT)); G_PARAM_READABLE|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class,
PROP_PROFILE, g_object_class_install_properties (gobject_class, N_PROPS, obj_props);
g_param_spec_string ("profile",
"Profile",
"Name of current profile for this card",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_HUMAN_PROFILE,
g_param_spec_string ("human-profile",
"Profile (Human readable)",
"Name of current profile for this card in human readable form",
NULL,
G_PARAM_READABLE));
} }
static void static void