From bf9c8625048d2adc5cbc5893fa27e7e9a3a9ba3e Mon Sep 17 00:00:00 2001 From: Jonas Danielsson Date: Wed, 13 Aug 2014 07:06:36 -0400 Subject: [PATCH] GApplication: Add g_application_add_main_option This function adds a single main option entry to be handeled by GApplication. The option entry has it arg_data field set to NULL and will be added to the applications packed_options. The rationale for this is that bindings will be able to add command line options even when they can't use the un-boxed struct GOptionEntry. https://bugzilla.gnome.org/show_bug.cgi?id=727455 --- docs/reference/gio/gio-sections.txt | 1 + gio/gapplication.c | 67 +++++++++++++++++++++++++++++++++++-- gio/gapplication.h | 9 +++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt index e89c8c2..80980fd 100644 --- a/docs/reference/gio/gio-sections.txt +++ b/docs/reference/gio/gio-sections.txt @@ -3040,6 +3040,7 @@ g_application_withdraw_notification g_application_run g_application_add_main_option_entries +g_application_add_main_option g_application_add_option_group g_application_set_default diff --git a/gio/gapplication.c b/gio/gapplication.c index 6c33f73..9546ae8 100644 --- a/gio/gapplication.c +++ b/gio/gapplication.c @@ -239,6 +239,9 @@ struct _GApplicationPrivate GSList *option_groups; GHashTable *packed_options; gboolean options_parsed; + + /* Allocated option strings, from g_application_add_main_option() */ + GSList *option_strings; }; enum @@ -665,6 +668,64 @@ g_application_add_main_option_entries (GApplication *application, } /** + * g_application_add_main_option: + * @application: the #GApplication + * @long_name: the long name of an option used to specify it in a commandline + * @short_name: the short name of an option + * @flags: flags from #GOptionFlags + * @arg: the type of the option, as a #GOptionArg + * @description: the description for the option in `--help` output + * @arg_description: (nullable): the placeholder to use for the extra argument + * parsed by the option in `--help` output + * + * Add an option to be handled by @application. + * + * Calling this function is the equivalent of calling + * g_application_add_main_option_entries() with a single #GOptionEntry + * that has its arg_data member set to %NULL. + * + * The parsed arguments will be packed into a #GVariantDict which + * is passed to #GApplication::handle-local-options. If + * %G_APPLICATION_HANDLES_COMMAND_LINE is set, then it will also + * be sent to the primary instance. See + * g_application_add_main_option_entries() for more details. + * + * See #GOptionEntry for more documentation of the arguments. + * + * Since: 2.42 + **/ +void +g_application_add_main_option (GApplication *application, + const char *long_name, + char short_name, + gint flags, + GOptionArg arg, + const char *description, + const char *arg_description) +{ + gchar *dup_string; + GOptionEntry my_entry[2] = { + { NULL, short_name, flags, arg, NULL, NULL, NULL }, + { NULL } + }; + + g_return_if_fail (G_IS_APPLICATION (application)); + g_return_if_fail (long_name != NULL); + g_return_if_fail (description != NULL); + + my_entry[0].long_name = dup_string = g_strdup (long_name); + application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string); + + my_entry[0].description = dup_string = g_strdup (description); + application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string); + + my_entry[0].arg_description = dup_string = g_strdup (arg_description); + application->priv->option_strings = g_slist_prepend (application->priv->option_strings, dup_string); + + g_application_add_main_option_entries (application, my_entry); +} + +/** * g_application_add_option_group: * @application: the #GApplication * @group: a #GOptionGroup @@ -1140,8 +1201,10 @@ g_application_finalize (GObject *object) if (application->priv->main_options) g_option_group_free (application->priv->main_options); if (application->priv->packed_options) - g_hash_table_unref (application->priv->packed_options); - + { + g_slist_free_full (application->priv->option_strings, g_free); + g_hash_table_unref (application->priv->packed_options); + } if (application->priv->impl) g_application_impl_destroy (application->priv->impl); g_free (application->priv->id); diff --git a/gio/gapplication.h b/gio/gapplication.h index b2b6a8a..30995e2 100644 --- a/gio/gapplication.h +++ b/gio/gapplication.h @@ -168,6 +168,15 @@ void g_application_set_action_group (GApplic GLIB_AVAILABLE_IN_2_40 void g_application_add_main_option_entries (GApplication *application, const GOptionEntry *entries); + +GLIB_AVAILABLE_IN_2_42 +void g_application_add_main_option (GApplication *application, + const char *long_name, + char short_name, + gint flags, + GOptionArg arg, + const char *description, + const char *arg_description); GLIB_AVAILABLE_IN_2_40 void g_application_add_option_group (GApplication *application, GOptionGroup *group); -- 2.7.4