gdkapplaunchcontext.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* gdkapplaunchcontext.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
Javier Jardón's avatar
Javier Jardón committed
16
   License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20

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

21
#include "config.h"
22

23
#include "gdkapplaunchcontextprivate.h"
24 25 26 27
#include "gdkscreen.h"
#include "gdkintl.h"


28 29 30 31 32 33 34 35 36
/**
 * SECTION:gdkapplaunchcontext
 * @Short_description: Startup notification for applications
 * @Title: Application launching
 *
 * GdkAppLaunchContext is an implementation of #GAppLaunchContext that
 * handles launching an application in a graphical context. It provides
 * startup notification and allows to launch applications on a specific
 * screen or workspace.
37 38 39
 *
 * ## Launching an application
 *
40
 * |[<!-- language="C" -->
41 42
 * GdkAppLaunchContext *context;
 *
43
 * context = gdk_display_get_app_launch_context (display);
44
 *
45
 * gdk_app_launch_context_set_screen (screen);
46 47 48 49 50 51
 * gdk_app_launch_context_set_timestamp (event->time);
 *
 * if (!g_app_info_launch_default_for_uri ("http://www.gtk.org", context, &error))
 *   g_warning ("Launching failed: %s\n", error->message);
 *
 * g_object_unref (context);
52
 * ]|
53 54 55
 */


56 57 58 59
static void    gdk_app_launch_context_finalize    (GObject           *object);
static gchar * gdk_app_launch_context_get_display (GAppLaunchContext *context,
                                                   GAppInfo          *info,
                                                   GList             *files);
60 61 62 63 64
static gchar * gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
                                                             GAppInfo          *info,
                                                             GList             *files);
static void    gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
                                                     const gchar       *startup_notify_id);
65 66


67 68 69 70 71 72
enum
{
  PROP_0,
  PROP_DISPLAY
};

73
G_DEFINE_TYPE (GdkAppLaunchContext, gdk_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static void
gdk_app_launch_context_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);

  switch (prop_id)
    {
    case PROP_DISPLAY:
      g_value_set_object (value, context->display);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gdk_app_launch_context_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);

  switch (prop_id)
    {
    case PROP_DISPLAY:
      context->display = g_value_dup_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

111 112 113 114 115 116
static void
gdk_app_launch_context_class_init (GdkAppLaunchContextClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GAppLaunchContextClass *context_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);

117 118 119
  gobject_class->set_property = gdk_app_launch_context_set_property,
  gobject_class->get_property = gdk_app_launch_context_get_property;

120 121 122
  gobject_class->finalize = gdk_app_launch_context_finalize;

  context_class->get_display = gdk_app_launch_context_get_display;
123 124
  context_class->get_startup_notify_id = gdk_app_launch_context_get_startup_notify_id;
  context_class->launch_failed = gdk_app_launch_context_launch_failed;
125 126 127 128 129

  g_object_class_install_property (gobject_class, PROP_DISPLAY,
    g_param_spec_object ("display", P_("Display"), P_("Display"),
                         GDK_TYPE_DISPLAY,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
130 131 132 133 134
}

static void
gdk_app_launch_context_init (GdkAppLaunchContext *context)
{
135
  context->workspace = -1;
136
}
137 138 139 140

static void
gdk_app_launch_context_finalize (GObject *object)
{
141
  GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
142

143 144
  if (context->display)
    g_object_unref (context->display);
145

146 147
  if (context->screen)
    g_object_unref (context->screen);
148

149 150
  if (context->icon)
    g_object_unref (context->icon);
151

152
  g_free (context->icon_name);
153

154
  G_OBJECT_CLASS (gdk_app_launch_context_parent_class)->finalize (object);
155 156
}

157 158 159 160
static gchar *
gdk_app_launch_context_get_display (GAppLaunchContext *context,
                                    GAppInfo          *info,
                                    GList             *files)
161
{
162
  GdkAppLaunchContext *ctx = GDK_APP_LAUNCH_CONTEXT (context);
163 164
  GdkDisplay *display;

165
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
166 167
  if (ctx->screen)
    return gdk_screen_make_display_name (ctx->screen);
168
G_GNUC_END_IGNORE_DEPRECATIONS
169

170 171
  if (ctx->display)
    display = ctx->display;
172 173 174 175 176 177
  else
    display = gdk_display_get_default ();

  return g_strdup (gdk_display_get_name (display));
}

Matthias Clasen's avatar
Matthias Clasen committed
178 179 180 181 182 183 184 185
/**
 * gdk_app_launch_context_set_display:
 * @context: a #GdkAppLaunchContext
 * @display: a #GdkDisplay
 *
 * Sets the display on which applications will be launched when
 * using this context. See also gdk_app_launch_context_set_screen().
 *
186
 * Since: 2.14
187 188
 *
 * Deprecated: 3.0: Use gdk_display_get_app_launch_context() instead
Matthias Clasen's avatar
Matthias Clasen committed
189
 */
190 191
void
gdk_app_launch_context_set_display (GdkAppLaunchContext *context,
192
                                    GdkDisplay          *display)
193
{
194 195 196
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
  g_return_if_fail (display == NULL || GDK_IS_DISPLAY (display));

197
  g_warn_if_fail (display == NULL || display == context->display);
198
}
Matthias Clasen's avatar
Matthias Clasen committed
199 200 201 202 203 204 205 206

/**
 * gdk_app_launch_context_set_screen:
 * @context: a #GdkAppLaunchContext
 * @screen: a #GdkScreen
 *
 * Sets the screen on which applications will be launched when
 * using this context. See also gdk_app_launch_context_set_display().
Matthias Clasen's avatar
Matthias Clasen committed
207
 *
Matthias Clasen's avatar
Matthias Clasen committed
208
 * If both @screen and @display are set, the @screen takes priority.
Matthias Clasen's avatar
Matthias Clasen committed
209 210
 * If neither @screen or @display are set, the default screen and
 * display are used.
Matthias Clasen's avatar
Matthias Clasen committed
211
 *
212
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
213
 */
214 215
void
gdk_app_launch_context_set_screen (GdkAppLaunchContext *context,
216
                                   GdkScreen           *screen)
217
{
218 219 220
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
  g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));

221
  g_return_if_fail (screen == NULL || gdk_screen_get_display (screen) == context->display);
222

223
  if (context->screen)
224
    {
225 226
      g_object_unref (context->screen);
      context->screen = NULL;
227 228 229
    }

  if (screen)
230
    context->screen = g_object_ref (screen);
231 232
}

Matthias Clasen's avatar
Matthias Clasen committed
233 234 235 236 237 238
/**
 * gdk_app_launch_context_set_desktop:
 * @context: a #GdkAppLaunchContext
 * @desktop: the number of a workspace, or -1
 *
 * Sets the workspace on which applications will be launched when
239 240
 * using this context when running under a window manager that
 * supports multiple workspaces, as described in the
241
 * [Extended Window Manager Hints](http://www.freedesktop.org/Standards/wm-spec).
Matthias Clasen's avatar
Matthias Clasen committed
242
 *
243
 * When the workspace is not specified or @desktop is set to -1,
Matthias Clasen's avatar
Matthias Clasen committed
244 245 246
 * it is up to the window manager to pick one, typically it will
 * be the current workspace.
 *
247
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
248
 */
249 250
void
gdk_app_launch_context_set_desktop (GdkAppLaunchContext *context,
251
                                    gint                 desktop)
252
{
253 254
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));

255
  context->workspace = desktop;
256 257
}

Matthias Clasen's avatar
Matthias Clasen committed
258 259 260 261 262 263
/**
 * gdk_app_launch_context_set_timestamp:
 * @context: a #GdkAppLaunchContext
 * @timestamp: a timestamp
 *
 * Sets the timestamp of @context. The timestamp should ideally
264
 * be taken from the event that triggered the launch.
Matthias Clasen's avatar
Matthias Clasen committed
265 266 267 268 269
 *
 * Window managers can use this information to avoid moving the
 * focus to the newly launched application when the user is busy
 * typing in another window. This is also known as 'focus stealing
 * prevention'.
Matthias Clasen's avatar
Matthias Clasen committed
270
 *
271
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
272
 */
273 274
void
gdk_app_launch_context_set_timestamp (GdkAppLaunchContext *context,
275
                                      guint32              timestamp)
276
{
277 278
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));

279
  context->timestamp = timestamp;
280 281
}

Matthias Clasen's avatar
Matthias Clasen committed
282 283 284
/**
 * gdk_app_launch_context_set_icon:
 * @context: a #GdkAppLaunchContext
285
 * @icon: (allow-none): a #GIcon, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
286 287
 *
 * Sets the icon for applications that are launched with this
288 289 290 291 292 293
 * context.
 *
 * Window Managers can use this information when displaying startup
 * notification.
 *
 * See also gdk_app_launch_context_set_icon_name().
Matthias Clasen's avatar
Matthias Clasen committed
294
 *
295
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
296
 */
297
void
298
gdk_app_launch_context_set_icon (GdkAppLaunchContext *context,
299 300
                                 GIcon               *icon)
{
301 302 303
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
  g_return_if_fail (icon == NULL || G_IS_ICON (icon));

304
  if (context->icon)
305
    {
306 307
      g_object_unref (context->icon);
      context->icon = NULL;
308 309 310
    }

  if (icon)
311
    context->icon = g_object_ref (icon);
312 313
}

Matthias Clasen's avatar
Matthias Clasen committed
314 315 316
/**
 * gdk_app_launch_context_set_icon_name:
 * @context: a #GdkAppLaunchContext
317
 * @icon_name: (allow-none): an icon name, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
318
 *
319 320 321
 * Sets the icon for applications that are launched with this context.
 * The @icon_name will be interpreted in the same way as the Icon field
 * in desktop files. See also gdk_app_launch_context_set_icon().
Matthias Clasen's avatar
Matthias Clasen committed
322 323
 *
 * If both @icon and @icon_name are set, the @icon_name takes priority.
324 325
 * If neither @icon or @icon_name is set, the icon is taken from either
 * the file that is passed to launched application or from the #GAppInfo
Matthias Clasen's avatar
Matthias Clasen committed
326
 * for the launched application itself.
327
 *
328
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
329
 */
330 331
void
gdk_app_launch_context_set_icon_name (GdkAppLaunchContext *context,
332
                                      const char          *icon_name)
333
{
334 335
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));

336 337
  g_free (context->icon_name);
  context->icon_name = g_strdup (icon_name);
338 339
}

Matthias Clasen's avatar
Matthias Clasen committed
340 341 342 343 344 345 346
/**
 * gdk_app_launch_context_new:
 *
 * Creates a new #GdkAppLaunchContext.
 *
 * Returns: a new #GdkAppLaunchContext
 *
347
 * Since: 2.14
348 349
 *
 * Deprecated: 3.0: Use gdk_display_get_app_launch_context() instead
Matthias Clasen's avatar
Matthias Clasen committed
350
 */
351 352 353
GdkAppLaunchContext *
gdk_app_launch_context_new (void)
{
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  return gdk_display_get_app_launch_context (gdk_display_get_default ());
}

static char *
gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
                                              GAppInfo          *info,
                                              GList             *files)
{
 return NULL;
}

static void
gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
                                      const gchar       *startup_notify_id)
{
369
}