gdkapplaunchcontext-x11.c 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* gdkapplaunchcontext-x11.c - Gtk+ implementation for GAppLaunchContext

   Copyright (C) 2007 Red Hat, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Alexander Larsson <alexl@redhat.com>
*/

23
#include "config.h"
24

25
#include "gdkapplaunchcontext.h"
26
#include "gdkinternals.h"
27 28 29 30 31

#include "gdkx.h"
#include "gdkscreen.h"
#include "gdkintl.h"

32 33 34 35 36
#include <glib.h>
#include <gio/gdesktopappinfo.h>

#include <string.h>
#include <unistd.h>
37 38

static char *
39 40
get_display_name (GFile     *file,
                  GFileInfo *info)
41 42 43 44 45
{
  char *name, *tmp;

  name = NULL;
  if (info)
46
    name = g_strdup (g_file_info_get_display_name (info));
47 48 49 50 51

  if (name == NULL)
    {
      name = g_file_get_basename (file);
      if (!g_utf8_validate (name, -1, NULL))
52 53 54 55 56 57
        {
          tmp = name;
          name =
            g_uri_escape_string (name, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
          g_free (tmp);
        }
58 59 60 61 62 63
    }

  return name;
}

static GIcon *
64 65
get_icon (GFile     *file,
          GFileInfo *info)
66 67 68 69
{
  GIcon *icon;

  icon = NULL;
70

71 72 73 74
  if (info)
    {
      icon = g_file_info_get_icon (info);
      if (icon)
75
        g_object_ref (icon);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    }

  return icon;
}

static char *
gicon_to_string (GIcon *icon)
{
  GFile *file;
  const char *const *names;

  if (G_IS_FILE_ICON (icon))
    {
      file = g_file_icon_get_file (G_FILE_ICON (icon));
      if (file)
91
        return g_file_get_path (file);
92 93 94 95 96
    }
  else if (G_IS_THEMED_ICON (icon))
    {
      names = g_themed_icon_get_names (G_THEMED_ICON (icon));
      if (names)
97
        return g_strdup (names[0]);
98
    }
Matthias Clasen's avatar
Matthias Clasen committed
99 100 101 102 103 104 105 106
  else if (G_IS_EMBLEMED_ICON (icon))
    {
      GIcon *base;

      base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));

      return gicon_to_string (base);
    }
107 108 109 110 111 112

  return NULL;
}

static void
end_startup_notification (GdkDisplay *display,
113
                          const char *startup_id)
114 115
{
  gdk_x11_display_broadcast_startup_message (display, "remove",
116 117
                                             "ID", startup_id,
                                             NULL);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}


/* This should be fairly long, as it's confusing to users if a startup
 * ends when it shouldn't (it appears that the startup failed, and
 * they have to relaunch the app). Also the timeout only matters when
 * there are bugs and apps don't end their own startup sequence.
 *
 * This timeout is a "last resort" timeout that ignores whether the
 * startup sequence has shown activity or not.  Metacity and the
 * tasklist have smarter, and correspondingly able-to-be-shorter
 * timeouts. The reason our timeout is dumb is that we don't monitor
 * the sequence (don't use an SnMonitorContext)
 */
#define STARTUP_TIMEOUT_LENGTH_SECONDS 30 
#define STARTUP_TIMEOUT_LENGTH (STARTUP_TIMEOUT_LENGTH_SECONDS * 1000)

typedef struct 
{
  GdkDisplay *display;
  char *startup_id;
  GTimeVal time;
} StartupNotificationData;

static void
free_startup_notification_data (gpointer data)
{
  StartupNotificationData *sn_data = data;

  g_object_unref (sn_data->display);
  g_free (sn_data->startup_id);
  g_free (sn_data);
}

typedef struct 
{
  GSList *contexts;
  guint timeout_id;
} StartupTimeoutData;

static void
free_startup_timeout (void *data)
{
  StartupTimeoutData *std;

  std = data;

  g_slist_foreach (std->contexts, (GFunc) free_startup_notification_data, NULL);
  g_slist_free (std->contexts);

  if (std->timeout_id != 0)
    {
      g_source_remove (std->timeout_id);
      std->timeout_id = 0;
    }

  g_free (std);
}

static gboolean
startup_timeout (void *data)
{
  StartupTimeoutData *std;
  GSList *tmp;
  GTimeVal now;
  int min_timeout;

  std = data;

  min_timeout = STARTUP_TIMEOUT_LENGTH;

  g_get_current_time (&now);

  tmp = std->contexts;
  while (tmp != NULL)
    {
      StartupNotificationData *sn_data;
      GSList *next;
      double elapsed;

      sn_data = tmp->data;
      next = tmp->next;

      elapsed =
202 203
        ((((double) now.tv_sec - sn_data->time.tv_sec) * G_USEC_PER_SEC +
          (now.tv_usec - sn_data->time.tv_usec))) / 1000.0;
204 205

      if (elapsed >= STARTUP_TIMEOUT_LENGTH)
206 207 208 209 210
        {
          std->contexts = g_slist_remove (std->contexts, sn_data);
          end_startup_notification (sn_data->display, sn_data->startup_id);
          free_startup_notification_data (sn_data);
        }
211
      else
212 213 214
        {
          min_timeout = MIN (min_timeout, (STARTUP_TIMEOUT_LENGTH - elapsed));
        }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

      tmp = next;
    }

  if (std->contexts == NULL)
    std->timeout_id = 0;
  else
    std->timeout_id = g_timeout_add_seconds ((min_timeout + 500)/1000, startup_timeout, std);

  /* always remove this one, but we may have reinstalled another one. */
  return FALSE;
}


static void
add_startup_timeout (GdkScreen  *screen,
231
                     const char *startup_id)
232 233 234 235 236 237 238 239 240 241 242 243 244
{
  StartupTimeoutData *data;
  StartupNotificationData *sn_data;

  data = g_object_get_data (G_OBJECT (screen), "appinfo-startup-data");

  if (data == NULL)
    {
      data = g_new (StartupTimeoutData, 1);
      data->contexts = NULL;
      data->timeout_id = 0;

      g_object_set_data_full (G_OBJECT (screen), "appinfo-startup-data",
245
                              data, free_startup_timeout);
246 247 248 249 250 251 252 253 254 255 256
    }

  sn_data = g_new (StartupNotificationData, 1);
  sn_data->display = g_object_ref (gdk_screen_get_display (screen));
  sn_data->startup_id = g_strdup (startup_id);
  g_get_current_time (&sn_data->time);

  data->contexts = g_slist_prepend (data->contexts, sn_data);

  if (data->timeout_id == 0)
    data->timeout_id = g_timeout_add_seconds (STARTUP_TIMEOUT_LENGTH_SECONDS,
257
                                              startup_timeout, data);
258 259 260
}


261 262 263 264
static char *
gdk_app_launch_context_x11_get_startup_notify_id (GAppLaunchContext *context,
                                                  GAppInfo          *info,
                                                  GList             *files)
265 266 267 268 269 270 271 272 273
{
  static int sequence = 0;
  GdkAppLaunchContextPrivate *priv;
  GdkDisplay *display;
  GdkScreen *screen;
  int files_count;
  char *description;
  char *icon_name;
  const char *binary_name;
274
  const char *application_id;
275 276 277 278 279
  char *screen_str;
  char *workspace_str;
  GIcon *icon;
  guint32 timestamp;
  char *startup_id;
280
  GFileInfo *fileinfo;
281 282 283

  priv = GDK_APP_LAUNCH_CONTEXT (context)->priv;

284
  display = priv->display;
285
  if (priv->screen)
286
    screen = priv->screen;
287
  else
288
    screen = gdk_display_get_default_screen (priv->display);
289

290 291
  fileinfo = NULL;

292 293
  files_count = g_list_length (files);
  if (files_count == 0)
294 295 296
    {
      description = g_strdup_printf (_("Starting %s"), g_app_info_get_name (info));
    }
297
  else if (files_count == 1)
Matthew Barnes's avatar
Matthew Barnes committed
298
    {
299 300 301 302 303 304 305 306 307
      gchar *display_name;

      if (g_file_is_native (files->data))
        fileinfo = g_file_query_info (files->data,
                                      G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
                                      G_FILE_ATTRIBUTE_STANDARD_ICON,
                                      0, NULL, NULL);

      display_name = get_display_name (files->data, fileinfo);
Matthew Barnes's avatar
Matthew Barnes committed
308 309 310
      description = g_strdup_printf (_("Opening %s"), display_name);
      g_free (display_name);
    }
311
  else
312
    description = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
313 314 315
                                                "Opening %d Item",
                                                "Opening %d Items",
                                                files_count), files_count);
316 317 318 319 320 321 322 323 324

  icon_name = NULL;
  if (priv->icon_name)
    icon_name = g_strdup (priv->icon_name);
  else
    {
      icon = NULL;

      if (priv->icon != NULL)
325
        icon = g_object_ref (priv->icon);
326
      else if (files_count == 1)
327
        icon = get_icon (files->data, fileinfo);
328 329

      if (icon == NULL)
330 331 332 333
        {
          icon = g_app_info_get_icon (info);
          g_object_ref (icon);
        }
334 335

      if (icon)
336
        icon_name = gicon_to_string (icon);
337 338 339 340 341 342 343 344 345 346 347

      g_object_unref (icon);
    }

  binary_name = g_app_info_get_executable (info);

  timestamp = priv->timestamp;
  if (timestamp == GDK_CURRENT_TIME)
    timestamp = gdk_x11_display_get_user_time (display);

  screen_str = g_strdup_printf ("%d", gdk_screen_get_number (screen));
348
  if (priv->workspace > -1)
349 350 351 352
    workspace_str = g_strdup_printf ("%d", priv->workspace);
  else
    workspace_str = NULL;

353 354 355 356
  if (G_IS_DESKTOP_APP_INFO (info))
    application_id = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (info));
  else
    application_id = NULL;
357 358

  startup_id = g_strdup_printf ("%s-%lu-%s-%s-%d_TIME%lu",
359 360 361 362 363 364 365
                                g_get_prgname (),
                                (unsigned long)getpid (),
                                g_get_host_name (),
                                binary_name,
                                sequence++,
                                (unsigned long)timestamp);

366
  gdk_x11_display_broadcast_startup_message (display, "new",
367 368 369 370 371 372 373 374 375 376
                                             "ID", startup_id,
                                             "NAME", g_app_info_get_name (info),
                                             "SCREEN", screen_str,
                                             "BIN", binary_name,
                                             "ICON", icon_name,
                                             "DESKTOP", workspace_str,
                                             "DESCRIPTION", description,
                                             "WMCLASS", NULL, /* FIXME */
                                             "APPLICATION_ID", application_id,
                                             NULL);
377 378 379 380 381

  g_free (description);
  g_free (screen_str);
  g_free (workspace_str);
  g_free (icon_name);
382 383
  if (fileinfo)
    g_object_unref (fileinfo);
384 385 386 387 388 389 390

  add_startup_timeout (screen, startup_id);

  return startup_id;
}


391 392 393
static void
gdk_app_launch_context_x11_launch_failed (GAppLaunchContext *context,
                                          const char        *startup_notify_id)
394 395 396 397 398 399 400 401 402 403 404 405
{
  GdkAppLaunchContextPrivate *priv;
  GdkScreen *screen;
  StartupTimeoutData *data;
  StartupNotificationData *sn_data;
  GSList *l;

  priv = GDK_APP_LAUNCH_CONTEXT (context)->priv;

  if (priv->screen)
    screen = priv->screen;
  else
406
    screen = gdk_display_get_default_screen (priv->display);
407 408 409 410 411 412

  data = g_object_get_data (G_OBJECT (screen), "appinfo-startup-data");

  if (data)
    {
      for (l = data->contexts; l != NULL; l = l->next)
413 414 415 416 417 418 419 420 421 422 423
        {
          sn_data = l->data;
          if (strcmp (startup_notify_id, sn_data->startup_id) == 0)
            {
              data->contexts = g_slist_remove (data->contexts, sn_data);
              end_startup_notification (sn_data->display, sn_data->startup_id);
              free_startup_notification_data (sn_data);

              break;
            }
        }
424 425

      if (data->contexts == NULL)
426 427 428 429
        {
          g_source_remove (data->timeout_id);
          data->timeout_id = 0;
        }
430 431
    }
}
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

typedef struct GdkAppLaunchContext GdkAppLaunchContextX11;
typedef struct GdkAppLaunchContextClass GdkAppLaunchContextX11Class;

G_DEFINE_TYPE (GdkAppLaunchContextX11, _gdk_app_launch_context_x11, GDK_TYPE_APP_LAUNCH_CONTEXT)

static void
_gdk_app_launch_context_x11_class_init (GdkAppLaunchContextX11Class *klass)
{
  GAppLaunchContextClass *ctx_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);

  ctx_class->get_startup_notify_id = gdk_app_launch_context_x11_get_startup_notify_id;
  ctx_class->launch_failed = gdk_app_launch_context_x11_launch_failed;
}

static void
_gdk_app_launch_context_x11_init (GdkAppLaunchContextX11 *ctx)
{
}

GdkAppLaunchContext *
_gdk_x11_display_get_app_launch_context (GdkDisplay *display)
{
  GdkAppLaunchContext *ctx;

  ctx = g_object_new (_gdk_app_launch_context_x11_get_type (), NULL);
  gdk_app_launch_context_set_display (ctx, display);

  return ctx;
}