summaryrefslogtreecommitdiff
path: root/gio/gapplication.c
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2018-08-18 15:35:33 -0400
committerMatthias Clasen <mclasen@redhat.com>2018-11-26 11:45:29 -0500
commit3a4b18f9038f316c617e229123b54ea9885ba1dc (patch)
treee9f8d5ceea655fff5276f226f52bb799de20397e /gio/gapplication.c
parent051c9ada8e8a5af59b3e98c55a7f6c43612b9efa (diff)
downloadglib-3a4b18f9038f316c617e229123b54ea9885ba1dc.tar.gz
GApplication: Add a way to replace a unique instance
While uniqueness is great, sometimes you want to restart a newer version of the same app. These two flags make that possible. We also add a ::name-lost signal, that is emitted when it happens. The default handler for this signal just calls g_application_quit(), but applications may want to connect and do cleanup or state-saving here.
Diffstat (limited to 'gio/gapplication.c')
-rw-r--r--gio/gapplication.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/gio/gapplication.c b/gio/gapplication.c
index 5a2f5adf7..2d2ab48e3 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -212,6 +212,7 @@
* the @dbus_register vfunc. Since: 2.34
* @handle_local_options: invoked locally after the parsing of the commandline
* options has occurred. Since: 2.40
+ * @name_lost: invoked when another instance is taking over the name. Since: 2.60
*
* Virtual function table for #GApplication.
*
@@ -277,6 +278,7 @@ enum
SIGNAL_ACTION,
SIGNAL_COMMAND_LINE,
SIGNAL_HANDLE_LOCAL_OPTIONS,
+ SIGNAL_NAME_LOST,
NR_SIGNALS
};
@@ -476,6 +478,7 @@ g_application_parse_command_line (GApplication *application,
{
gboolean become_service = FALSE;
gchar *app_id = NULL;
+ gboolean replace = FALSE;
GVariantDict *dict = NULL;
GOptionContext *context;
GOptionGroup *gapplication_group;
@@ -557,6 +560,18 @@ g_application_parse_command_line (GApplication *application,
g_option_group_add_entries (gapplication_group, entries);
}
+ /* Allow replacing if the application allows it */
+ if (application->priv->flags & G_APPLICATION_ALLOW_REPLACEMENT)
+ {
+ GOptionEntry entries[] = {
+ { "gapplication-replace", '\0', 0, G_OPTION_ARG_NONE, &replace,
+ N_("Replace the running instance") },
+ { NULL }
+ };
+
+ g_option_group_add_entries (gapplication_group, entries);
+ }
+
/* Now we parse... */
if (!g_option_context_parse_strv (context, arguments, error))
goto out;
@@ -569,6 +584,10 @@ g_application_parse_command_line (GApplication *application,
if (app_id)
g_application_set_application_id (application, app_id);
+ /* Check for --gapplication-replace */
+ if (replace)
+ application->priv->flags |= G_APPLICATION_REPLACE;
+
dict = g_variant_dict_new (NULL);
if (application->priv->packed_options)
{
@@ -1177,6 +1196,13 @@ g_application_real_dbus_unregister (GApplication *application,
{
}
+static gboolean
+g_application_real_name_lost (GApplication *application)
+{
+ g_application_quit (application);
+ return TRUE;
+}
+
/* GObject implementation stuff {{{1 */
static void
g_application_set_property (GObject *object,
@@ -1434,6 +1460,7 @@ g_application_class_init (GApplicationClass *class)
class->add_platform_data = g_application_real_add_platform_data;
class->dbus_register = g_application_real_dbus_register;
class->dbus_unregister = g_application_real_dbus_unregister;
+ class->name_lost = g_application_real_name_lost;
g_object_class_install_property (object_class, PROP_APPLICATION_ID,
g_param_spec_string ("application-id",
@@ -1628,6 +1655,25 @@ g_application_class_init (GApplicationClass *class)
g_application_handle_local_options_accumulator, NULL, NULL,
G_TYPE_INT, 1, G_TYPE_VARIANT_DICT);
+ /**
+ * GApplication::name-lost:
+ * @application: the application
+ *
+ * The ::name-lost signal is emitted only on the registered primary instance
+ * when a new instance has taken over. This can only happen if the application
+ * is using the %G_APPLICATION_ALLOW_REPLACEMENT flag.
+ *
+ * The default handler for this signal calls g_application_quit().
+ *
+ * Returns: %TRUE if the signal has been handled
+ *
+ * Since: 2.60
+ */
+ g_application_signals[SIGNAL_NAME_LOST] =
+ g_signal_new (I_("name-lost"), G_TYPE_APPLICATION, G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GApplicationClass, name_lost),
+ g_signal_accumulator_true_handled, NULL, NULL,
+ G_TYPE_BOOLEAN, 0);
}
/* Application ID validity {{{1 */