| 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 the Gnome Library; see the file COPYING.LIB. If not, |
| 17 |
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
Boston, MA 02111-1307, USA. |
| 19 |
|
| 20 |
Author: Alexander Larsson <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <string.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include <glib.h> |
| 29 |
#include <gio/gdesktopappinfo.h> |
| 30 |
|
| 31 |
#include "gdkx.h" |
| 32 |
#include "gdkapplaunchcontext.h" |
| 33 |
#include "gdkscreen.h" |
| 34 |
#include "gdkinternals.h" |
| 35 |
#include "gdkintl.h" |
| 36 |
#include "gdkalias.h" |
| 37 |
|
| 38 |
|
| 39 |
static char * |
| 40 |
get_display_name (GFile *file, |
| 41 |
GFileInfo *info) |
| 42 |
{ |
| 43 |
char *name, *tmp; |
| 44 |
|
| 45 |
name = NULL; |
| 46 |
if (info) |
| 47 |
name = g_strdup (g_file_info_get_display_name (info)); |
| 48 |
|
| 49 |
if (name == NULL) |
| 50 |
{ |
| 51 |
name = g_file_get_basename (file); |
| 52 |
if (!g_utf8_validate (name, -1, NULL)) |
| 53 |
{ |
| 54 |
tmp = name; |
| 55 |
name = |
| 56 |
g_uri_escape_string (name, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE); |
| 57 |
g_free (tmp); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return name; |
| 62 |
} |
| 63 |
|
| 64 |
static GIcon * |
| 65 |
get_icon (GFile *file, |
| 66 |
GFileInfo *info) |
| 67 |
{ |
| 68 |
GIcon *icon; |
| 69 |
|
| 70 |
icon = NULL; |
| 71 |
|
| 72 |
if (info) |
| 73 |
{ |
| 74 |
icon = g_file_info_get_icon (info); |
| 75 |
if (icon) |
| 76 |
g_object_ref (icon); |
| 77 |
} |
| 78 |
|
| 79 |
return icon; |
| 80 |
} |
| 81 |
|
| 82 |
static char * |
| 83 |
gicon_to_string (GIcon *icon) |
| 84 |
{ |
| 85 |
GFile *file; |
| 86 |
const char *const *names; |
| 87 |
|
| 88 |
if (G_IS_FILE_ICON (icon)) |
| 89 |
{ |
| 90 |
file = g_file_icon_get_file (G_FILE_ICON (icon)); |
| 91 |
if (file) |
| 92 |
return g_file_get_path (file); |
| 93 |
} |
| 94 |
else if (G_IS_THEMED_ICON (icon)) |
| 95 |
{ |
| 96 |
names = g_themed_icon_get_names (G_THEMED_ICON (icon)); |
| 97 |
if (names) |
| 98 |
return g_strdup (names[0]); |
| 99 |
} |
| 100 |
else if (G_IS_EMBLEMED_ICON (icon)) |
| 101 |
{ |
| 102 |
GIcon *base; |
| 103 |
|
| 104 |
base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon)); |
| 105 |
|
| 106 |
return gicon_to_string (base); |
| 107 |
} |
| 108 |
|
| 109 |
return NULL; |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
end_startup_notification (GdkDisplay *display, |
| 114 |
const char *startup_id) |
| 115 |
{ |
| 116 |
gdk_x11_display_broadcast_startup_message (display, "remove", |
| 117 |
"ID", startup_id, |
| 118 |
NULL); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/* This should be fairly long, as it's confusing to users if a startup |
| 123 |
* ends when it shouldn't (it appears that the startup failed, and |
| 124 |
* they have to relaunch the app). Also the timeout only matters when |
| 125 |
* there are bugs and apps don't end their own startup sequence. |
| 126 |
* |
| 127 |
* This timeout is a "last resort" timeout that ignores whether the |
| 128 |
* startup sequence has shown activity or not. Metacity and the |
| 129 |
* tasklist have smarter, and correspondingly able-to-be-shorter |
| 130 |
* timeouts. The reason our timeout is dumb is that we don't monitor |
| 131 |
* the sequence (don't use an SnMonitorContext) |
| 132 |
*/ |
| 133 |
#define STARTUP_TIMEOUT_LENGTH_SECONDS 30 |
| 134 |
#define STARTUP_TIMEOUT_LENGTH (STARTUP_TIMEOUT_LENGTH_SECONDS * 1000) |
| 135 |
|
| 136 |
typedef struct |
| 137 |
{ |
| 138 |
GdkDisplay *display; |
| 139 |
char *startup_id; |
| 140 |
GTimeVal time; |
| 141 |
} StartupNotificationData; |
| 142 |
|
| 143 |
static void |
| 144 |
free_startup_notification_data (gpointer data) |
| 145 |
{ |
| 146 |
StartupNotificationData *sn_data = data; |
| 147 |
|
| 148 |
g_object_unref (sn_data->display); |
| 149 |
g_free (sn_data->startup_id); |
| 150 |
g_free (sn_data); |
| 151 |
} |
| 152 |
|
| 153 |
typedef struct |
| 154 |
{ |
| 155 |
GSList *contexts; |
| 156 |
guint timeout_id; |
| 157 |
} StartupTimeoutData; |
| 158 |
|
| 159 |
static void |
| 160 |
free_startup_timeout (void *data) |
| 161 |
{ |
| 162 |
StartupTimeoutData *std; |
| 163 |
|
| 164 |
std = data; |
| 165 |
|
| 166 |
g_slist_foreach (std->contexts, (GFunc) free_startup_notification_data, NULL); |
| 167 |
g_slist_free (std->contexts); |
| 168 |
|
| 169 |
if (std->timeout_id != 0) |
| 170 |
{ |
| 171 |
g_source_remove (std->timeout_id); |
| 172 |
std->timeout_id = 0; |
| 173 |
} |
| 174 |
|
| 175 |
g_free (std); |
| 176 |
} |
| 177 |
|
| 178 |
static gboolean |
| 179 |
startup_timeout (void *data) |
| 180 |
{ |
| 181 |
StartupTimeoutData *std; |
| 182 |
GSList *tmp; |
| 183 |
GTimeVal now; |
| 184 |
int min_timeout; |
| 185 |
|
| 186 |
std = data; |
| 187 |
|
| 188 |
min_timeout = STARTUP_TIMEOUT_LENGTH; |
| 189 |
|
| 190 |
g_get_current_time (&now); |
| 191 |
|
| 192 |
tmp = std->contexts; |
| 193 |
while (tmp != NULL) |
| 194 |
{ |
| 195 |
StartupNotificationData *sn_data; |
| 196 |
GSList *next; |
| 197 |
double elapsed; |
| 198 |
|
| 199 |
sn_data = tmp->data; |
| 200 |
next = tmp->next; |
| 201 |
|
| 202 |
elapsed = |
| 203 |
((((double) now.tv_sec - sn_data->time.tv_sec) * G_USEC_PER_SEC + |
| 204 |
(now.tv_usec - sn_data->time.tv_usec))) / 1000.0; |
| 205 |
|
| 206 |
if (elapsed >= STARTUP_TIMEOUT_LENGTH) |
| 207 |
{ |
| 208 |
std->contexts = g_slist_remove (std->contexts, sn_data); |
| 209 |
end_startup_notification (sn_data->display, sn_data->startup_id); |
| 210 |
free_startup_notification_data (sn_data); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
min_timeout = MIN (min_timeout, (STARTUP_TIMEOUT_LENGTH - elapsed)); |
| 215 |
} |
| 216 |
|
| 217 |
tmp = next; |
| 218 |
} |
| 219 |
|
| 220 |
if (std->contexts == NULL) |
| 221 |
std->timeout_id = 0; |
| 222 |
else |
| 223 |
std->timeout_id = g_timeout_add_seconds ((min_timeout + 500)/1000, startup_timeout, std); |
| 224 |
|
| 225 |
/* always remove this one, but we may have reinstalled another one. */ |
| 226 |
return FALSE; |
| 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 |
} |
| 260 |
|
| 261 |
|
| 262 |
char * |
| 263 |
_gdk_windowing_get_startup_notify_id (GAppLaunchContext *context, |
| 264 |
GAppInfo *info, |
| 265 |
GList *files) |
| 266 |
{ |
| 267 |
static int sequence = 0; |
| 268 |
GdkAppLaunchContextPrivate *priv; |
| 269 |
GdkDisplay *display; |
| 270 |
GdkScreen *screen; |
| 271 |
int files_count; |
| 272 |
char *description; |
| 273 |
char *icon_name; |
| 274 |
const char *binary_name; |
| 275 |
const char *application_id; |
| 276 |
char *screen_str; |
| 277 |
char *workspace_str; |
| 278 |
GIcon *icon; |
| 279 |
guint32 timestamp; |
| 280 |
char *startup_id; |
| 281 |
GFileInfo *fileinfo; |
| 282 |
|
| 283 |
priv = GDK_APP_LAUNCH_CONTEXT (context)->priv; |
| 284 |
|
| 285 |
if (priv->screen) |
| 286 |
{ |
| 287 |
screen = priv->screen; |
| 288 |
display = gdk_screen_get_display (priv->screen); |
| 289 |
} |
| 290 |
else if (priv->display) |
| 291 |
{ |
| 292 |
display = priv->display; |
| 293 |
screen = gdk_display_get_default_screen (display); |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
display = gdk_display_get_default (); |
| 298 |
screen = gdk_display_get_default_screen (display); |
| 299 |
} |
| 300 |
|
| 301 |
fileinfo = NULL; |
| 302 |
|
| 303 |
files_count = g_list_length (files); |
| 304 |
if (files_count == 0) |
| 305 |
{ |
| 306 |
description = g_strdup_printf (_("Starting %s"), g_app_info_get_name (info)); |
| 307 |
} |
| 308 |
else if (files_count == 1) |
| 309 |
{ |
| 310 |
gchar *display_name; |
| 311 |
|
| 312 |
if (g_file_is_native (files->data)) |
| 313 |
fileinfo = g_file_query_info (files->data, |
| 314 |
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," |
| 315 |
G_FILE_ATTRIBUTE_STANDARD_ICON, |
| 316 |
0, NULL, NULL); |
| 317 |
|
| 318 |
display_name = get_display_name (files->data, fileinfo); |
| 319 |
description = g_strdup_printf (_("Opening %s"), display_name); |
| 320 |
g_free (display_name); |
| 321 |
} |
| 322 |
else |
| 323 |
description = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE, |
| 324 |
"Opening %d Item", |
| 325 |
"Opening %d Items", |
| 326 |
files_count), files_count); |
| 327 |
|
| 328 |
icon_name = NULL; |
| 329 |
if (priv->icon_name) |
| 330 |
icon_name = g_strdup (priv->icon_name); |
| 331 |
else |
| 332 |
{ |
| 333 |
icon = NULL; |
| 334 |
|
| 335 |
if (priv->icon != NULL) |
| 336 |
icon = g_object_ref (priv->icon); |
| 337 |
else if (files_count == 1) |
| 338 |
icon = get_icon (files->data, fileinfo); |
| 339 |
|
| 340 |
if (icon == NULL) |
| 341 |
{ |
| 342 |
icon = g_app_info_get_icon (info); |
| 343 |
if (icon != NULL) |
| 344 |
g_object_ref (icon); |
| 345 |
} |
| 346 |
|
| 347 |
if (icon != NULL) |
| 348 |
{ |
| 349 |
icon_name = gicon_to_string (icon); |
| 350 |
g_object_unref (icon); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
binary_name = g_app_info_get_executable (info); |
| 355 |
|
| 356 |
timestamp = priv->timestamp; |
| 357 |
if (timestamp == GDK_CURRENT_TIME) |
| 358 |
timestamp = gdk_x11_display_get_user_time (display); |
| 359 |
|
| 360 |
screen_str = g_strdup_printf ("%d", gdk_screen_get_number (screen)); |
| 361 |
if (priv->workspace > -1) |
| 362 |
workspace_str = g_strdup_printf ("%d", priv->workspace); |
| 363 |
else |
| 364 |
workspace_str = NULL; |
| 365 |
|
| 366 |
if (G_IS_DESKTOP_APP_INFO (info)) |
| 367 |
application_id = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (info)); |
| 368 |
else |
| 369 |
application_id = NULL; |
| 370 |
|
| 371 |
startup_id = g_strdup_printf ("%s-%lu-%s-%s-%d_TIME%lu", |
| 372 |
g_get_prgname (), |
| 373 |
(unsigned long)getpid (), |
| 374 |
g_get_host_name (), |
| 375 |
binary_name, |
| 376 |
sequence++, |
| 377 |
(unsigned long)timestamp); |
| 378 |
|
| 379 |
gdk_x11_display_broadcast_startup_message (display, "new", |
| 380 |
"ID", startup_id, |
| 381 |
"NAME", g_app_info_get_name (info), |
| 382 |
"SCREEN", screen_str, |
| 383 |
"BIN", binary_name, |
| 384 |
"ICON", icon_name, |
| 385 |
"DESKTOP", workspace_str, |
| 386 |
"DESCRIPTION", description, |
| 387 |
"WMCLASS", NULL, /* FIXME */ |
| 388 |
"APPLICATION_ID", application_id, |
| 389 |
NULL); |
| 390 |
|
| 391 |
g_free (description); |
| 392 |
g_free (screen_str); |
| 393 |
g_free (workspace_str); |
| 394 |
g_free (icon_name); |
| 395 |
if (fileinfo) |
| 396 |
g_object_unref (fileinfo); |
| 397 |
|
| 398 |
add_startup_timeout (screen, startup_id); |
| 399 |
|
| 400 |
return startup_id; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
void |
| 405 |
_gdk_windowing_launch_failed (GAppLaunchContext *context, |
| 406 |
const char *startup_notify_id) |
| 407 |
{ |
| 408 |
GdkAppLaunchContextPrivate *priv; |
| 409 |
GdkScreen *screen; |
| 410 |
StartupTimeoutData *data; |
| 411 |
StartupNotificationData *sn_data; |
| 412 |
GSList *l; |
| 413 |
|
| 414 |
priv = GDK_APP_LAUNCH_CONTEXT (context)->priv; |
| 415 |
|
| 416 |
if (priv->screen) |
| 417 |
screen = priv->screen; |
| 418 |
else if (priv->display) |
| 419 |
screen = gdk_display_get_default_screen (priv->display); |
| 420 |
else |
| 421 |
screen = gdk_display_get_default_screen (gdk_display_get_default ()); |
| 422 |
|
| 423 |
data = g_object_get_data (G_OBJECT (screen), "appinfo-startup-data"); |
| 424 |
|
| 425 |
if (data) |
| 426 |
{ |
| 427 |
for (l = data->contexts; l != NULL; l = l->next) |
| 428 |
{ |
| 429 |
sn_data = l->data; |
| 430 |
if (strcmp (startup_notify_id, sn_data->startup_id) == 0) |
| 431 |
{ |
| 432 |
data->contexts = g_slist_remove (data->contexts, sn_data); |
| 433 |
end_startup_notification (sn_data->display, sn_data->startup_id); |
| 434 |
free_startup_notification_data (sn_data); |
| 435 |
|
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if (data->contexts == NULL) |
| 441 |
{ |
| 442 |
g_source_remove (data->timeout_id); |
| 443 |
data->timeout_id = 0; |
| 444 |
} |
| 445 |
} |
| 446 |
} |