gapplication: add a way to override the app-id

Some applications support running in a mode where they present
themselves as a different application to the user (for example web
browsers or terminals).

To facilitate this, add an option --gapplication-app-id which allows
users to override an application's id from desktop files or similar.

Applications need to opt-in to this by setting the
G_APPLICATION_CAN_OVERRIDE_APP_ID flag.

https://bugzilla.gnome.org/show_bug.cgi?id=743933
This commit is contained in:
Lars Uebernickel 2015-02-03 14:09:44 +01:00 committed by Matthias Clasen
parent 15dea440c4
commit b32f8ba19b
2 changed files with 31 additions and 8 deletions

View File

@ -474,8 +474,10 @@ g_application_parse_command_line (GApplication *application,
GError **error)
{
gboolean become_service = FALSE;
gchar *app_id = NULL;
GVariantDict *dict = NULL;
GOptionContext *context;
GOptionGroup *gapplication_group;
/* Due to the memory management of GOptionGroup we can only parse
* options once. That's because once you add a group to the
@ -487,6 +489,12 @@ g_application_parse_command_line (GApplication *application,
context = g_option_context_new (NULL);
gapplication_group = g_option_group_new ("gapplication",
_("GApplication options"), _("Show GApplication options"),
NULL, NULL);
g_option_group_set_translation_domain (gapplication_group, GETTEXT_PACKAGE);
g_option_context_add_group (context, gapplication_group);
/* If the application has not registered local options and it has
* G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
* their primary instance commandline handler may want to deal with
@ -525,20 +533,25 @@ g_application_parse_command_line (GApplication *application,
*/
if ((application->priv->flags & (G_APPLICATION_IS_SERVICE | G_APPLICATION_IS_LAUNCHER)) == 0)
{
GOptionGroup *option_group;
GOptionEntry entries[] = {
{ "gapplication-service", '\0', 0, G_OPTION_ARG_NONE, &become_service,
N_("Enter GApplication service mode (use from D-Bus service files)") },
{ NULL }
};
option_group = g_option_group_new ("gapplication",
_("GApplication options"), _("Show GApplication options"),
NULL, NULL);
g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
g_option_group_add_entries (option_group, entries);
g_option_group_add_entries (gapplication_group, entries);
}
g_option_context_add_group (context, option_group);
/* Allow overriding the ID if the application allows it */
if (application->priv->flags & G_APPLICATION_CAN_OVERRIDE_APP_ID)
{
GOptionEntry entries[] = {
{ "gapplication-app-id", '\0', 0, G_OPTION_ARG_STRING, &app_id,
N_("Override the application's ID") },
{ NULL }
};
g_option_group_add_entries (gapplication_group, entries);
}
/* Now we parse... */
@ -549,6 +562,10 @@ g_application_parse_command_line (GApplication *application,
if (become_service)
application->priv->flags |= G_APPLICATION_IS_SERVICE;
/* Check for --gapplication-app-id */
if (app_id)
g_application_set_application_id (application, app_id);
dict = g_variant_dict_new (NULL);
if (application->priv->packed_options)
{
@ -562,6 +579,7 @@ out:
application->priv->options_parsed = TRUE;
g_option_context_free (context);
g_free (app_id);
return dict;
}

View File

@ -1464,6 +1464,9 @@ typedef enum
* owner of the application ID nor does it check if an existing
* owner already exists. Everything occurs in the local process.
* Since: 2.30.
* @G_APPLICATION_CAN_OVERRIDE_APP_ID: Allow users to override the
* application ID from the command line with `--gapplication-app-id`.
* Since: 2.48
*
* Flags used to define the behaviour of a #GApplication.
*
@ -1479,7 +1482,9 @@ typedef enum
G_APPLICATION_HANDLES_COMMAND_LINE = (1 << 3),
G_APPLICATION_SEND_ENVIRONMENT = (1 << 4),
G_APPLICATION_NON_UNIQUE = (1 << 5)
G_APPLICATION_NON_UNIQUE = (1 << 5),
G_APPLICATION_CAN_OVERRIDE_APP_ID = (1 << 6)
} GApplicationFlags;
/**