2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
5 <chapter id="gtk-migrating-unique-GtkApplication">
7 <title>Migrating from libunique to GApplication or GtkApplication</title>
10 libunique offers 'unique application' support as well as ways to
11 communicate with a running application instance. This is implemented
12 in various ways, either using D-Bus, or socket-based communication.
16 Starting with GLib 2.26, D-Bus support has been integrated into GIO
17 in the form of GDBus, and #GApplication has been added to provide
18 the same level of application support as libunique.
21 <example><title>A unique application</title>
22 <para>Here is a simple application using libunique:
25 main (int argc, char *argv[])
30 gtk_init (&argc, &argv);
32 app = unique_app_new ("org.gtk.TestApplication", NULL);
34 if (unique_app_is_running (app))
36 UniqueResponse response;
38 response = unique_app_send_message (app, UNIQUE_ACTIVATE, NULL);
41 return response == UNIQUE_RESPONSE_OK ? 0 : 1;
44 window = create_my_window ();
46 unique_app_watch_window (app, GTK_WINDOW (window));
48 gtk_widget_show (window);
57 The same application using GtkApplication:
60 activate (GtkApplication *app)
65 list = gtk_application_get_windows (app);
69 gtk_window_present (GTK_WINDOW (list->data));
73 window = create_my_window ();
74 gtk_window_set_application (GTK_WINDOW (window), app);
75 gtk_widget_show (window);
80 main (int argc, char *argv[])
85 app = gtk_application_new ("org.gtk.TestApplication", 0);
86 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
88 status = g_application_run (app);
97 <section><title>Uniqueness</title>
99 Instead of creating a #UniqueApp with unique_app_new(), create
100 a #GApplication with g_application_new() or a #GtkApplication
101 with gtk_application_new(). The @name that was used with
102 unique_app_new() is very likely usable as the @application_id for
103 g_application_new() without any changes, and GtkApplication passes
104 the <envar>DESKTOP_STARTUP_ID</envar> environment variable
108 While libunique expects you to check for an already running instance
109 yourself and activate it manually, GApplication handles all this on
110 its own in g_application_run(). If you still need to find out if there
111 is a running instance of your application, use
112 g_application_get_is_remote() instead of unique_app_is_running().
116 <section><title>Commands and Messages</title>
118 libunique lets you send messages with commands to a running
119 instance using unique_app_send_message(). The commands can be either
120 predefined or custom. Some of the predefined libunique commands have
121 equivalents in GApplication. Instead of sending the %UNIQUE_ACTIVATE
122 command, call g_application_activate(), instead of sending the
123 %UNIQUE_OPEN command, call g_application_open(). The
124 %UNIQUE_NEW and %UNIQUE_CLOSE and user-defined commands don't
125 have direct replacement at this time.
129 As a replacement for custom commands, GApplication implements the
130 #GActionGroup interface and lets you add a group of actions with
131 g_application_set_action_group(). The actions can then be invoked,
132 either by using the D-Bus interface for #GAction directly, or by
133 calling g_action_group_activate_action() from another instance of
134 the GApplication. The #GApplication documentation contains an
135 example for using GApplication with actions.
139 For more complex needs, GApplication supports passing entire
140 commandlines to the running instance.