Debian Codegraph @

gdk/x11/gdkapplaunchcontext-x11.c

raw
1 /* gdkapplaunchcontext-x11.c - Gtk+ implementation for GAppLaunchContext
2
3 Copyright (C) 2007 Red Hat, Inc.
4
5 The Gnome Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The Gnome Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library. If not, see <http://www.gnu.org/licenses/>.
17
18 Author: Alexander Larsson <[email protected]>
19 */
20
21 #include "config.h"
22
23 #include "gdkx11applaunchcontext.h"
24 #include "gdkapplaunchcontextprivate.h"
25 #include "gdkscreen.h"
26 #include "gdkintl.h"
27 #include "gdkprivate-x11.h"
28
29 #include <glib.h>
30 #include <gio/gdesktopappinfo.h>
31
32 #include <string.h>
33 #include <unistd.h>
34
35 static char *
36 get_display_name (GFile *file,
37 GFileInfo *info)
38 {
39 char *name, *tmp;
40
41 name = NULL;
42 if (info)
43 name = g_strdup (g_file_info_get_display_name (info));
44
45 if (name == NULL)
46 {
47 name = g_file_get_basename (file);
48 if (name == NULL)
49 name = g_file_get_uri (file);
50
51 if (!g_utf8_validate (name, -1, NULL))
52 {
53 tmp = name;
54 name =
55 g_uri_escape_string (name, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
56 g_free (tmp);
57 }
58 }
59
60 return name;
61 }
62
63 static GIcon *
64 get_icon (GFile *file,
65 GFileInfo *info)
66 {
67 GIcon *icon;
68
69 icon = NULL;
70
71 if (info)
72 {
73 icon = g_file_info_get_icon (info);
74 if (icon)
75 g_object_ref (icon);
76 }
77
78 return icon;
79 }
80
81 static char *
82 gicon_to_string (GIcon *icon)
83 {
84 GFile *file;
85 const char *const *names;
86
87 if (G_IS_FILE_ICON (icon))
88 {
89 file = g_file_icon_get_file (G_FILE_ICON (icon));
90 if (file)
91 return g_file_get_path (file);
92 }
93 else if (G_IS_THEMED_ICON (icon))
94 {
95 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
96 if (names)
97 return g_strdup (names[0]);
98 }
99 else if (G_IS_EMBLEMED_ICON (icon))
100 {
101 GIcon *base;
102
103 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
104
105 return gicon_to_string (base);
106 }
107
108 return NULL;
109 }
110
111 static void
112 end_startup_notification (GdkDisplay *display,
113 const char *startup_id)
114 {
115 gdk_x11_display_broadcast_startup_message (display, "remove",
116 "ID", startup_id,
117 NULL);
118 }
119
120
121 /* This should be fairly long, as it's confusing to users if a startup
122 * ends when it shouldn’t (it appears that the startup failed, and
123 * they have to relaunch the app). Also the timeout only matters when
124 * there are bugs and apps don’t end their own startup sequence.
125 *
126 * This timeout is a "last resort" timeout that ignores whether the
127 * startup sequence has shown activity or not. Metacity and the
128 * tasklist have smarter, and correspondingly able-to-be-shorter
129 * timeouts. The reason our timeout is dumb is that we don’t monitor
130 * the sequence (don’t use an SnMonitorContext)
131 */
132 #define STARTUP_TIMEOUT_LENGTH_SECONDS 30
133 #define STARTUP_TIMEOUT_LENGTH (STARTUP_TIMEOUT_LENGTH_SECONDS * 1000)
134
135 typedef struct
136 {
137 GdkDisplay *display;
138 char *startup_id;
139 GTimeVal time;
140 } StartupNotificationData;
141
142 static void
143 free_startup_notification_data (gpointer data)
144 {
145 StartupNotificationData *sn_data = data;
146
147 g_object_unref (sn_data->display);
148 g_free (sn_data->startup_id);
149 g_free (sn_data);
150 }
151
152 typedef struct
153 {
154 GSList *contexts;
155 guint timeout_id;
156 } StartupTimeoutData;
157
158 static void
159 free_startup_timeout (void *data)
160 {
161 StartupTimeoutData *std;
162
163 std = data;
164
165 g_slist_free_full (std->contexts, free_startup_notification_data);
166
167 if (std->timeout_id != 0)
168 {
169 g_source_remove (std->timeout_id);
170 std->timeout_id = 0;
171 }
172
173 g_free (std);
174 }
175
176 static gboolean
177 startup_timeout (void *data)
178 {
179 StartupTimeoutData *std;
180 GSList *tmp;
181 GTimeVal now;
182 int min_timeout;
183
184 std = data;
185
186 min_timeout = STARTUP_TIMEOUT_LENGTH;
187
188 g_get_current_time (&now);
189
190 tmp = std->contexts;
191 while (tmp != NULL)
192 {
193 StartupNotificationData *sn_data;
194 GSList *next;
195 double elapsed;
196
197 sn_data = tmp->data;
198 next = tmp->next;
199
200 elapsed =
201 ((((double) now.tv_sec - sn_data->time.tv_sec) * G_USEC_PER_SEC +
202 (now.tv_usec - sn_data->time.tv_usec))) / 1000.0;
203
204 if (elapsed >= STARTUP_TIMEOUT_LENGTH)
205 {
206 std->contexts = g_slist_remove (std->contexts, sn_data);
207 end_startup_notification (sn_data->display, sn_data->startup_id);
208 free_startup_notification_data (sn_data);
209 }
210 else
211 {
212 min_timeout = MIN (min_timeout, (STARTUP_TIMEOUT_LENGTH - elapsed));
213 }
214
215 tmp = next;
216 }
217
218 if (std->contexts == NULL)
219 std->timeout_id = 0;
220 else {
221 std->timeout_id = g_timeout_add_seconds ((min_timeout + 500)/1000, startup_timeout, std);
222 g_source_set_name_by_id (std->timeout_id, "[gtk+] startup_timeout");
223 }
224
225 /* always remove this one, but we may have reinstalled another one. */
226 return G_SOURCE_REMOVE;
227 }
228
229
230 static void
231 add_startup_timeout (GdkScreen *screen,
232 const char *startup_id)
233 {
234 StartupTimeoutData *data;
235 StartupNotificationData *sn_data;
236
237 data = g_object_get_data (G_OBJECT (screen), "appinfo-startup-data");
238
239 if (data == NULL)
240 {
241 data = g_new (StartupTimeoutData, 1);
242 data->contexts = NULL;
243 data->timeout_id = 0;
244
245 g_object_set_data_full (G_OBJECT (screen), "appinfo-startup-data",
246 data, free_startup_timeout);
247 }
248
249 sn_data = g_new (StartupNotificationData, 1);
250 sn_data->display = g_object_ref (gdk_screen_get_display (screen));
251 sn_data->startup_id = g_strdup (startup_id);
252 g_get_current_time (&sn_data->time);
253
254 data->contexts = g_slist_prepend (data->contexts, sn_data);
255
256 if (data->timeout_id == 0) {
257 data->timeout_id = g_timeout_add_seconds (STARTUP_TIMEOUT_LENGTH_SECONDS,
258 startup_timeout, data);
259 g_source_set_name_by_id (data->timeout_id, "[gtk+] startup_timeout");
260 }
261 }
262
263
264 static char *
265 gdk_x11_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
266 GAppInfo *info,
267 GList *files)
268 {
269 static int sequence = 0;
270 GdkDisplay *display;
271 GdkScreen *screen;
272 int files_count;
273 char *description;
274 char *icon_name;
275 const char *binary_name;
276 const char *application_id;
277 char *screen_str;
278 char *workspace_str;
279 GIcon *icon;
280 guint32 timestamp;
281 char *startup_id;
282 GFileInfo *fileinfo;
283 GdkAppLaunchContext *ctx;
284
285 ctx = GDK_APP_LAUNCH_CONTEXT (context);
286
287 display = ctx->display;
288 if (ctx->screen)
289 screen = ctx->screen;
290 else
291 screen = gdk_display_get_default_screen (ctx->display);
292
293 fileinfo = NULL;
294
295 files_count = g_list_length (files);
296 if (files_count == 0)
297 {
298 description = g_strdup_printf (_("Starting %s"), g_app_info_get_name (info));
299 }
300 else if (files_count == 1)
301 {
302 gchar *display_name;
303
304 if (g_file_is_native (files->data))
305 fileinfo = g_file_query_info (files->data,
306 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
307 G_FILE_ATTRIBUTE_STANDARD_ICON,
308 0, NULL, NULL);
309
310 display_name = get_display_name (files->data, fileinfo);
311 description = g_strdup_printf (_("Opening %s"), display_name);
312 g_free (display_name);
313 }
314 else
315 description = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
316 "Opening %d Item",
317 "Opening %d Items",
318 files_count), files_count);
319
320 icon_name = NULL;
321 if (ctx->icon_name)
322 icon_name = g_strdup (ctx->icon_name);
323 else
324 {
325 icon = NULL;
326
327 if (ctx->icon != NULL)
328 icon = g_object_ref (ctx->icon);
329 else if (files_count == 1)
330 icon = get_icon (files->data, fileinfo);
331
332 if (icon == NULL)
333 {
334 icon = g_app_info_get_icon (info);
335 if (icon != NULL)
336 g_object_ref (icon);
337 }
338
339 if (icon != NULL)
340 {
341 icon_name = gicon_to_string (icon);
342 g_object_unref (icon);
343 }
344 }
345
346 binary_name = g_app_info_get_executable (info);
347
348 timestamp = ctx->timestamp;
349 if (timestamp == GDK_CURRENT_TIME)
350 timestamp = gdk_x11_display_get_user_time (display);
351
352 screen_str = g_strdup_printf ("%d", gdk_x11_screen_get_number (screen));
353 if (ctx->workspace > -1)
354 workspace_str = g_strdup_printf ("%d", ctx->workspace);
355 else
356 workspace_str = NULL;
357
358 if (G_IS_DESKTOP_APP_INFO (info))
359 application_id = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (info));
360 else
361 application_id = NULL;
362
363 startup_id = g_strdup_printf ("%s-%lu-%s-%s-%d_TIME%lu",
364 g_get_prgname (),
365 (unsigned long)getpid (),
366 g_get_host_name (),
367 binary_name,
368 sequence++,
369 (unsigned long)timestamp);
370
371 gdk_x11_display_broadcast_startup_message (display, "new",
372 "ID", startup_id,
373 "NAME", g_app_info_get_name (info),
374 "SCREEN", screen_str,
375 "BIN", binary_name,
376 "ICON", icon_name,
377 "DESKTOP", workspace_str,
378 "DESCRIPTION", description,
379 "WMCLASS", NULL, /* FIXME */
380 "APPLICATION_ID", application_id,
381 NULL);
382
383 g_free (description);
384 g_free (screen_str);
385 g_free (workspace_str);
386 g_free (icon_name);
387 if (fileinfo)
388 g_object_unref (fileinfo);
389
390 add_startup_timeout (screen, startup_id);
391
392 return startup_id;
393 }
394
395
396 static void
397 gdk_x11_app_launch_context_launch_failed (GAppLaunchContext *context,
398 const gchar *startup_notify_id)
399 {
400 GdkAppLaunchContext *ctx;
401 GdkScreen *screen;
402 StartupTimeoutData *data;
403 StartupNotificationData *sn_data;
404 GSList *l;
405
406 ctx = GDK_APP_LAUNCH_CONTEXT (context);
407
408 if (ctx->screen)
409 screen = ctx->screen;
410 else
411 screen = gdk_display_get_default_screen (ctx->display);
412
413 data = g_object_get_data (G_OBJECT (screen), "appinfo-startup-data");
414
415 if (data)
416 {
417 for (l = data->contexts; l != NULL; l = l->next)
418 {
419 sn_data = l->data;
420 if (strcmp (startup_notify_id, sn_data->startup_id) == 0)
421 {
422 data->contexts = g_slist_remove (data->contexts, sn_data);
423 end_startup_notification (sn_data->display, sn_data->startup_id);
424 free_startup_notification_data (sn_data);
425
426 break;
427 }
428 }
429
430 if (data->contexts == NULL)
431 {
432 g_source_remove (data->timeout_id);
433 data->timeout_id = 0;
434 }
435 }
436 }
437
438 struct _GdkX11AppLaunchContext
439 {
440 GdkAppLaunchContext parent_instance;
441 };
442
443 struct _GdkX11AppLaunchContextClass
444 {
445 GdkAppLaunchContextClass parent_class;
446 };
447
448
449 G_DEFINE_TYPE (GdkX11AppLaunchContext, gdk_x11_app_launch_context, GDK_TYPE_APP_LAUNCH_CONTEXT)
450
451 static void
452 gdk_x11_app_launch_context_class_init (GdkX11AppLaunchContextClass *klass)
453 {
454 GAppLaunchContextClass *ctx_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);
455
456 ctx_class->get_startup_notify_id = gdk_x11_app_launch_context_get_startup_notify_id;
457 ctx_class->launch_failed = gdk_x11_app_launch_context_launch_failed;
458 }
459
460 static void
461 gdk_x11_app_launch_context_init (GdkX11AppLaunchContext *ctx)
462 {
463 }
464
465 GdkAppLaunchContext *
466 _gdk_x11_display_get_app_launch_context (GdkDisplay *display)
467 {
468 GdkAppLaunchContext *ctx;
469 const gchar *display_name;
470
471 ctx = g_object_new (GDK_TYPE_X11_APP_LAUNCH_CONTEXT,
472 "display", display,
473 NULL);
474
475 display_name = gdk_display_get_name (display);
476 if (display_name)
477 g_app_launch_context_setenv (G_APP_LAUNCH_CONTEXT (ctx),
478 "DISPLAY", display_name);
479
480 return ctx;
481 }