Debian Codegraph @

portal-test/gtk3/portal-test-app.c

raw
1 #include <unistd.h>
2 #include <gtk/gtk.h>
3
4 #include "portal-test-app.h"
5 #include "portal-test-win.h"
6
7 struct _PortalTestApp
8 {
9 GtkApplication parent;
10 };
11
12 struct _PortalTestAppClass
13 {
14 GtkApplicationClass parent_class;
15 };
16
17 G_DEFINE_TYPE(PortalTestApp, portal_test_app, GTK_TYPE_APPLICATION)
18
19 static void
20 portal_test_app_init (PortalTestApp *app)
21 {
22 /* this option is handled explicitly in main(),
23 * just add it here so GApplication is not surprised.
24 */
25 g_application_add_main_option (G_APPLICATION (app),
26 "replace",
27 0,
28 G_OPTION_FLAG_NONE,
29 G_OPTION_ARG_NONE,
30 "Replace the running instance", NULL);
31 }
32
33 static void
34 name_appeared (GDBusConnection *bus,
35 const char *name,
36 const char *name_owner,
37 gpointer data)
38 {
39 gboolean *name_free = data;
40
41 g_message ("Name %s owned by %s", name, name_owner);
42
43 *name_free = FALSE;
44 }
45
46 static void
47 name_vanished (GDBusConnection *bus,
48 const char *name,
49 gpointer data)
50 {
51 gboolean *name_free = data;
52
53 g_message ("Name %s unowned", name);
54
55 *name_free = TRUE;
56 }
57
58 void
59 portal_test_app_stop_running_instance (void)
60 {
61 g_autoptr(GDBusConnection) bus = NULL;
62 g_autoptr(GError) error = NULL;
63 g_autoptr(GVariant) ret = NULL;
64 GVariantBuilder options;
65 GVariantBuilder pdata;
66 guint watcher_id;
67 gboolean name_free = FALSE;
68
69 g_message ("Replacing the running instance");
70
71 /* Can't use g_application_activate_action here,
72 * since we're not set up as a remote instance yet.
73 */
74 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
75 if (bus == NULL)
76 {
77 g_warning ("Failed to get session bus");
78 exit (1);
79 }
80
81 watcher_id = g_bus_watch_name_on_connection (bus,
82 "org.gnome.PortalTest",
83 0,
84 name_appeared,
85 name_vanished,
86 &name_free,
87 NULL);
88
89 g_variant_builder_init (&options, G_VARIANT_TYPE ("av"));
90 g_variant_builder_init (&pdata, G_VARIANT_TYPE ("a{sv}"));
91 ret = g_dbus_connection_call_sync (bus,
92 "org.gnome.PortalTest",
93 "/org/gnome/PortalTest",
94 "org.gtk.Actions",
95 "Activate",
96 g_variant_new ("(sava{sv})",
97 "quit", &options, &pdata),
98 NULL,
99 G_DBUS_CALL_FLAGS_NONE,
100 -1,
101 NULL,
102 &error);
103 if (error != NULL)
104 {
105 g_warning ("Failed to quit the running instance: %s", error->message);
106 exit (1);
107 }
108
109 if (!name_free)
110 g_message ("Waiting for running instance to give up name ownership");
111
112 while (!name_free)
113 g_main_context_iteration (NULL, TRUE);
114
115 g_bus_unwatch_name (watcher_id);
116 }
117
118 static void
119 portal_test_app_startup (GApplication *app)
120 {
121 g_autoptr(GMenu) menu = NULL;
122
123 G_APPLICATION_CLASS (portal_test_app_parent_class)->startup (app);
124
125 menu = g_menu_new ();
126 g_menu_append (menu, "Restart", "app.restart");
127 g_menu_append (menu, "Quit", "app.quit");
128
129 gtk_application_set_app_menu (GTK_APPLICATION (app), G_MENU_MODEL (menu));
130 }
131
132 static void
133 portal_test_app_activate (GApplication *app)
134 {
135 GtkWindow *win;
136
137 win = gtk_application_get_active_window (GTK_APPLICATION (app));
138 if (!win)
139 win = GTK_WINDOW (portal_test_win_new (PORTAL_TEST_APP (app)));
140 gtk_window_present (win);
141 }
142
143 static void
144 portal_test_app_class_init (PortalTestAppClass *class)
145 {
146 G_APPLICATION_CLASS (class)->startup = portal_test_app_startup;
147 G_APPLICATION_CLASS (class)->activate = portal_test_app_activate;
148 }
149
150 static void
151 acktivate (GSimpleAction *action,
152 GVariant *parameter,
153 gpointer user_data)
154 {
155 GtkApplication *app = user_data;
156 GtkWindow *win;
157
158 win = gtk_application_get_active_window (app);
159 portal_test_win_ack (PORTAL_TEST_WIN (win));
160 }
161
162 static void
163 spawned_cb (GObject *source,
164 GAsyncResult *res,
165 gpointer data)
166 {
167 GDBusConnection *bus = G_DBUS_CONNECTION (source);
168 g_autoptr(GError) error = NULL;
169 g_autoptr(GVariant) ret = NULL;
170 guint32 pid;
171
172 ret = g_dbus_connection_call_finish (bus, res, &error);
173
174 if (!ret)
175 {
176 g_warning ("Restart failed: %s\n", error->message);
177 return;
178 }
179
180 g_variant_get (ret, "(u)", &pid);
181 g_message ("Restarted with pid %d", pid);
182 }
183
184 void
185 portal_test_app_restart (PortalTestApp *app)
186 {
187 GDBusConnection *bus;
188 const char *strv[3] = { "portal-test", "--replace", NULL };
189 GVariant *args;
190
191 bus = g_application_get_dbus_connection (G_APPLICATION (app));
192
193 g_message ("Calling org.freedesktop.portal.Flatpak.Spawn");
194 args = g_variant_new ("(@ay@aay@a{uh}@a{ss}u@a{sv})",
195 g_variant_new_bytestring ("/"),
196 g_variant_new_bytestring_array (strv, -1),
197 g_variant_new_array (G_VARIANT_TYPE ("{uh}"), NULL, 0),
198 g_variant_new_array (G_VARIANT_TYPE ("{ss}"), NULL, 0),
199 2,
200 g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0)),
201
202 g_dbus_connection_call (bus,
203 "org.freedesktop.portal.Flatpak",
204 "/org/freedesktop/portal/Flatpak",
205 "org.freedesktop.portal.Flatpak",
206 "Spawn",
207 args,
208 G_VARIANT_TYPE ("(u)"),
209 0,
210 -1,
211 NULL,
212 spawned_cb,
213 app);
214 }
215
216 static void
217 restart (GSimpleAction *action,
218 GVariant *parameter,
219 gpointer user_data)
220 {
221 portal_test_app_restart (PORTAL_TEST_APP (user_data));
222 }
223
224 static void
225 quit (GSimpleAction *action,
226 GVariant *parameter,
227 gpointer user_data)
228 {
229 g_message ("Received a request to quit");
230 g_application_quit (G_APPLICATION (user_data));
231 }
232
233 static GActionEntry entries[] = {
234 { "ack", acktivate, NULL, NULL, NULL },
235 { "restart", restart, NULL, NULL, NULL },
236 { "quit", quit, NULL, NULL, NULL }
237 };
238
239 GApplication *
240 portal_test_app_new (void)
241 {
242 GApplication *app;
243
244 app = g_object_new (portal_test_app_get_type (),
245 "application-id", "org.gnome.PortalTest.Gtk3",
246 NULL);
247
248 g_action_map_add_action_entries (G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), app);
249
250 return app;
251 }