shell: Let panels have their own commandline flags

Add a class method to CcPanel to get a GOptionGroup which will be added
to the main commandline parser. This gives panels the chance to have
commandline "--flags" in addition to the already available parameters.

This changes changes the way parameters are passed to panels: the first
entry in the GVariant array is always the a{sv} dictionary of
commandline flags, followed by the remaining free-form arguments.

https://bugzilla.gnome.org/show_bug.cgi?id=696054
This commit is contained in:
Emanuele Aina 2013-03-15 17:34:43 +01:00
parent 9977bb200e
commit 31a8a99440
9 changed files with 55 additions and 10 deletions

View file

@ -91,17 +91,19 @@ cc_keyboard_panel_set_property (GObject *object,
page = section = NULL;
switch (g_variant_n_children (parameters))
{
case 2:
g_variant_get_child (parameters, 1, "v", &v);
case 3:
g_variant_get_child (parameters, 2, "v", &v);
section = g_variant_get_string (v, NULL);
g_variant_unref (v);
/* fall-through */
case 1:
g_variant_get_child (parameters, 0, "v", &v);
case 2:
g_variant_get_child (parameters, 1, "v", &v);
page = g_variant_get_string (v, NULL);
g_variant_unref (v);
cc_keyboard_panel_set_page (panel, page, section);
/* fall-through */
case 1:
/* No flags expected, fall-through */
case 0:
break;
default:

View file

@ -173,7 +173,8 @@ variant_av_to_string_array (GVariant *array)
count = g_variant_iter_init (&iter, array);
strv = g_ptr_array_sized_new (count + 1);
while (g_variant_iter_next (&iter, "v", &v)) {
g_ptr_array_add (strv, (gpointer *)g_variant_get_string (v, NULL));
if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
g_ptr_array_add (strv, (gpointer *)g_variant_get_string (v, NULL));
g_variant_unref (v);
}
g_ptr_array_add (strv, NULL); /* NULL-terminate the strv data array */

View file

@ -108,12 +108,12 @@ cc_goa_panel_set_property (GObject *object,
const gchar *first_arg = NULL;
parameters = g_value_get_variant (value);
if (parameters == NULL)
if (parameters == NULL || g_variant_n_children (parameters) == 0)
return;
if (g_variant_n_children (parameters) > 0)
if (g_variant_n_children (parameters) > 1)
{
g_variant_get_child (parameters, 0, "v", &v);
g_variant_get_child (parameters, 1, "v", &v);
if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING))
first_arg = g_variant_get_string (v, NULL);
else

View file

@ -56,9 +56,10 @@ cc_sound_panel_set_property (GObject *object,
GVariant *parameters;
parameters = g_value_get_variant (value);
if (parameters && g_variant_n_children (parameters) > 0) {
if (parameters && g_variant_n_children (parameters) > 1) {
GVariant *v;
g_variant_get_child (parameters, 0, "v", &v);
/* Skip the first child, we don't expect any flag */
g_variant_get_child (parameters, 1, "v", &v);
gvc_mixer_dialog_set_page (self->dialog, g_variant_get_string (v, NULL));
g_variant_unref (v);
}

View file

@ -105,18 +105,22 @@ cc_application_command_line (GApplication *application,
int retval = 0;
GOptionContext *context;
GError *error = NULL;
GVariantBuilder *flags_builder;
verbose = FALSE;
show_overview = FALSE;
show_help = FALSE;
start_panels = NULL;
flags_builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
argv = g_application_command_line_get_arguments (command_line, &argc);
context = g_option_context_new (N_("- Settings"));
g_option_context_add_main_entries (context, all_options, GETTEXT_PACKAGE);
g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
g_option_context_add_group (context, gtk_get_option_group (TRUE));
cc_panel_loader_add_option_groups (context, flags_builder);
g_option_context_set_help_enabled (context, FALSE);
if (g_option_context_parse (context, &argc, &argv, &error) == FALSE)
@ -192,6 +196,8 @@ cc_application_command_line (GApplication *application,
g_debug ("No extra argument");
builder = g_variant_builder_new (G_VARIANT_TYPE ("av"));
g_variant_builder_add (builder, "v", g_variant_builder_end (flags_builder));
for (i = 1; start_panels[i] != NULL; i++)
g_variant_builder_add (builder, "v", g_variant_new_string (start_panels[i]));
parameters = g_variant_builder_end (builder);

View file

@ -215,4 +215,21 @@ cc_panel_loader_load_by_name (CcShell *shell,
NULL);
}
void
cc_panel_loader_add_option_groups (GOptionContext *context,
GVariantBuilder *builder)
{
int i;
for (i = 0; i < G_N_ELEMENTS (all_panels); i++)
{
GType (*get_type) (void);
get_type = all_panels[i].get_type;
GOptionGroup *group = cc_panel_get_option_group (get_type(), builder);
if (group == NULL)
continue;
g_option_context_add_group (context, group);
}
}
#endif /* CC_PANEL_LOADER_NO_GTYPES */

View file

@ -30,6 +30,8 @@ G_BEGIN_DECLS
void cc_panel_loader_fill_model (CcShellModel *model);
GList *cc_panel_loader_get_panels (void);
void cc_panel_loader_add_option_groups (GOptionContext *context,
GVariantBuilder *builder);
CcPanel *cc_panel_loader_load_by_name (CcShell *shell,
const char *name,
GVariant *parameters);

View file

@ -259,3 +259,15 @@ cc_panel_get_help_uri (CcPanel *panel)
return NULL;
}
GOptionGroup *
cc_panel_get_option_group (GType panel_type,
GVariantBuilder *builder)
{
GOptionGroup *options = NULL;
CcPanelClass *class = CC_PANEL_CLASS (g_type_class_ref (panel_type));
if (class->get_option_group != NULL)
options = class->get_option_group (builder);
g_type_class_unref (class);
return options;
}

View file

@ -75,12 +75,16 @@ struct _CcPanelClass
/*< private >*/
GtkBinClass parent_class;
GOptionGroup * (* get_option_group) (GVariantBuilder *builder);
GPermission * (* get_permission) (CcPanel *panel);
const char * (* get_help_uri) (CcPanel *panel);
};
GType cc_panel_get_type (void);
GOptionGroup *cc_panel_get_option_group (GType panel_type,
GVariantBuilder *builder);
CcShell* cc_panel_get_shell (CcPanel *panel);
GPermission *cc_panel_get_permission (CcPanel *panel);