Commit 0e671286 authored by Allison Karlitskaya's avatar Allison Karlitskaya

GApplication: parse command line options

Add support for parsing command line options with GApplication.

You can add GOptionGroup and GOptionEntry using two new APIs:
g_application_add_option_group() and
g_application_add_main_option_entries().

Also add a "handle-local-options" signal that allows handling of
commandline arguments in the local process without having to override
local_command_line.

As a special feature, you can have a %NULL @arg_data in a GOptionEntry
which will cause the argument to be stored in a GVariantDict.  This
dictionary is available for inspection and modification by the
"handle-local-options" signal and can be forwarded to the primary
instance in cases of command line invocation (where it can be fetched
using g_application_command_line_get_options()).

https://bugzilla.gnome.org/show_bug.cgi?id=721977
parent 7f362330
......@@ -3020,6 +3020,9 @@ g_application_send_notification
g_application_withdraw_notification
<SUBSECTION>
g_application_run
g_application_add_main_option_entries
g_application_add_option_group
<SUBSECTION>
g_application_set_default
g_application_get_default
<SUBSECTION>
......
This diff is collapsed.
......@@ -115,9 +115,11 @@ struct _GApplicationClass
void (* dbus_unregister) (GApplication *application,
GDBusConnection *connection,
const gchar *object_path);
gint (* handle_local_options)(GApplication *application,
GVariantDict *options);
/*< private >*/
gpointer padding[9];
gpointer padding[8];
};
GLIB_AVAILABLE_IN_ALL
......@@ -157,6 +159,13 @@ GLIB_DEPRECATED
void g_application_set_action_group (GApplication *application,
GActionGroup *action_group);
GLIB_AVAILABLE_IN_2_40
void g_application_add_main_option_entries (GApplication *application,
const GOptionEntry *entries);
GLIB_AVAILABLE_IN_2_40
void g_application_add_option_group (GApplication *application,
GOptionGroup *group);
GLIB_AVAILABLE_IN_ALL
gboolean g_application_get_is_registered (GApplication *application);
GLIB_AVAILABLE_IN_ALL
......
......@@ -211,6 +211,7 @@ enum
{
PROP_NONE,
PROP_ARGUMENTS,
PROP_OPTIONS,
PROP_PLATFORM_DATA,
PROP_IS_REMOTE
};
......@@ -219,6 +220,8 @@ struct _GApplicationCommandLinePrivate
{
GVariant *platform_data;
GVariant *arguments;
GVariant *options;
GVariantDict *options_dict;
gchar *cwd;
gchar **environ;
......@@ -253,6 +256,12 @@ grok_platform_data (GApplicationCommandLine *cmdline)
cmdline->priv->environ =
g_variant_dup_bytestring_array (value, NULL);
}
else if (strcmp (key, "options") == 0)
{
if (!cmdline->priv->options)
cmdline->priv->options = g_variant_ref (value);
}
}
static void
......@@ -321,6 +330,11 @@ g_application_command_line_set_property (GObject *object,
cmdline->priv->arguments = g_value_dup_variant (value);
break;
case PROP_OPTIONS:
g_assert (cmdline->priv->options == NULL);
cmdline->priv->options = g_value_dup_variant (value);
break;
case PROP_PLATFORM_DATA:
g_assert (cmdline->priv->platform_data == NULL);
cmdline->priv->platform_data = g_value_dup_variant (value);
......@@ -338,6 +352,12 @@ g_application_command_line_finalize (GObject *object)
{
GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
if (cmdline->priv->options_dict)
g_variant_dict_unref (cmdline->priv->options_dict);
if (cmdline->priv->options)
g_variant_unref (cmdline->priv->options);
if (cmdline->priv->platform_data)
g_variant_unref (cmdline->priv->platform_data);
if (cmdline->priv->arguments)
......@@ -394,6 +414,13 @@ g_application_command_line_class_init (GApplicationCommandLineClass *class)
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_OPTIONS,
g_param_spec_variant ("options",
P_("Options"),
P_("The options sent along with the commandline"),
G_VARIANT_TYPE_VARDICT, NULL, G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_PLATFORM_DATA,
g_param_spec_variant ("platform-data",
P_("Platform data"),
......@@ -450,6 +477,35 @@ g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
return argv;
}
/**
* g_application_command_line_get_options_dict:
* @cmdline: a #GApplicationCommandLine
*
* Gets the options there were passed to g_application_command_line().
*
* If you did not override local_command_line() then these are the same
* options that were parsed according to the #GOptionEntrys added to the
* application with g_application_add_main_option_entries() and possibly
* modified from your GApplication::handle-local-options handler.
*
* If no options were sent then an empty dictionary is returned so that
* you don't need to check for %NULL.
*
* Returns: (transfer none): a #GVariantDict with the options
*
* Since: 2.40
**/
GVariantDict *
g_application_command_line_get_options_dict (GApplicationCommandLine *cmdline)
{
g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (cmdline), NULL);
if (!cmdline->priv->options_dict)
cmdline->priv->options_dict = g_variant_dict_new (cmdline->priv->options);
return cmdline->priv->options_dict;
}
/**
* g_application_command_line_get_stdin:
* @cmdline: a #GApplicationCommandLine
......
......@@ -76,6 +76,9 @@ GLIB_AVAILABLE_IN_ALL
gchar ** g_application_command_line_get_arguments (GApplicationCommandLine *cmdline,
int *argc);
GLIB_AVAILABLE_IN_2_40
GVariantDict * g_application_command_line_get_options_dict (GApplicationCommandLine *cmdline);
GLIB_AVAILABLE_IN_2_36
GInputStream * g_application_command_line_get_stdin (GApplicationCommandLine *cmdline);
......
......@@ -701,7 +701,7 @@ g_application_impl_cmdline_done (GObject *source,
int
g_application_impl_command_line (GApplicationImpl *impl,
gchar **arguments,
const gchar * const *arguments,
GVariant *platform_data)
{
const static GDBusInterfaceVTable vtable = {
......
......@@ -31,7 +31,7 @@ void g_application_impl_open (GApplic
GVariant *platform_data);
int g_application_impl_command_line (GApplicationImpl *impl,
gchar **arguments,
const gchar *const *arguments,
GVariant *platform_data);
void g_application_impl_flush (GApplicationImpl *impl);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment