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 37 38
/**
 * 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.
 * <example>
 * <title>Launching an application</title>
39
 * <informalexample>
40 41 42
 * <programlisting>
 * 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 52
 * 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);
 * </programlisting>
53
 * </informalexample>
54 55 56 57
 * </example>
 */


58 59 60 61
static void    gdk_app_launch_context_finalize    (GObject           *object);
static gchar * gdk_app_launch_context_get_display (GAppLaunchContext *context,
                                                   GAppInfo          *info,
                                                   GList             *files);
62 63 64 65 66
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);
67 68


69 70 71 72 73 74
enum
{
  PROP_0,
  PROP_DISPLAY
};

75
G_DEFINE_TYPE (GdkAppLaunchContext, gdk_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
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 111 112
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);
    }
}

113 114 115 116 117 118
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);

119 120 121
  gobject_class->set_property = gdk_app_launch_context_set_property,
  gobject_class->get_property = gdk_app_launch_context_get_property;

122 123 124
  gobject_class->finalize = gdk_app_launch_context_finalize;

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

  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));
132 133 134 135 136
}

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

static void
gdk_app_launch_context_finalize (GObject *object)
{
143
  GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
144

145 146
  if (context->display)
    g_object_unref (context->display);
147

148 149
  if (context->screen)
    g_object_unref (context->screen);
150

151 152
  if (context->icon)
    g_object_unref (context->icon);
153

154
  g_free (context->icon_name);
155

156
  G_OBJECT_CLASS (gdk_app_launch_context_parent_class)->finalize (object);
157 158
}

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

167 168
  if (ctx->screen)
    return gdk_screen_make_display_name (ctx->screen);
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 241 242
 * using this context when running under a window manager that
 * supports multiple workspaces, as described in the
 * <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended
 * Window Manager Hints</ulink>.
Matthias Clasen's avatar
Matthias Clasen committed
243
 *
244
 * When the workspace is not specified or @desktop is set to -1,
Matthias Clasen's avatar
Matthias Clasen committed
245 246 247
 * it is up to the window manager to pick one, typically it will
 * be the current workspace.
 *
248
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
249
 */
250 251
void
gdk_app_launch_context_set_desktop (GdkAppLaunchContext *context,
252
                                    gint                 desktop)
253
{
254 255
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));

256
  context->workspace = desktop;
257 258
}

Matthias Clasen's avatar
Matthias Clasen committed
259 260 261 262 263 264
/**
 * gdk_app_launch_context_set_timestamp:
 * @context: a #GdkAppLaunchContext
 * @timestamp: a timestamp
 *
 * Sets the timestamp of @context. The timestamp should ideally
265
 * be taken from the event that triggered the launch.
Matthias Clasen's avatar
Matthias Clasen committed
266 267 268 269 270
 *
 * 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
271
 *
272
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
273
 */
274 275
void
gdk_app_launch_context_set_timestamp (GdkAppLaunchContext *context,
276
                                      guint32              timestamp)
277
{
278 279
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));

280
  context->timestamp = timestamp;
281 282
}

Matthias Clasen's avatar
Matthias Clasen committed
283 284 285
/**
 * gdk_app_launch_context_set_icon:
 * @context: a #GdkAppLaunchContext
286
 * @icon: (allow-none): a #GIcon, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
287 288
 *
 * Sets the icon for applications that are launched with this
289 290 291 292 293 294
 * 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
295
 *
296
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
297
 */
298
void
299
gdk_app_launch_context_set_icon (GdkAppLaunchContext *context,
300 301
                                 GIcon               *icon)
{
302 303 304
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
  g_return_if_fail (icon == NULL || G_IS_ICON (icon));

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

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

Matthias Clasen's avatar
Matthias Clasen committed
315 316 317
/**
 * gdk_app_launch_context_set_icon_name:
 * @context: a #GdkAppLaunchContext
318
 * @icon_name: (allow-none): an icon name, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
319
 *
320 321 322
 * 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
323 324
 *
 * If both @icon and @icon_name are set, the @icon_name takes priority.
325 326
 * 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
327
 * for the launched application itself.
328
 *
329
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
330
 */
331 332
void
gdk_app_launch_context_set_icon_name (GdkAppLaunchContext *context,
333
                                      const char          *icon_name)
334
{
335 336
  g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));

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

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