]> Pileus Git - ~andy/gtk/blob - gdk/gdkapplaunchcontext.c
9916c82ca7bb5ed49a7204bff90f3cd51b986ff5
[~andy/gtk] / gdk / gdkapplaunchcontext.c
1 /* gdkapplaunchcontext.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 <alexl@redhat.com>
21 */
22
23 #include "config.h"
24
25 #include "gdkapplaunchcontextprivate.h"
26 #include "gdkscreen.h"
27 #include "gdkintl.h"
28
29
30 /**
31  * SECTION:gdkapplaunchcontext
32  * @Short_description: Startup notification for applications
33  * @Title: Application launching
34  *
35  * GdkAppLaunchContext is an implementation of #GAppLaunchContext that
36  * handles launching an application in a graphical context. It provides
37  * startup notification and allows to launch applications on a specific
38  * screen or workspace.
39  * <example>
40  * <title>Launching an application</title>
41  * <informalexample>
42  * <programlisting>
43  * GdkAppLaunchContext *context;
44  *
45  * context = gdk_display_get_app_launch_context (display);
46  *
47  * gdk_app_launch_context_set_screen (screen);
48  * gdk_app_launch_context_set_timestamp (event->time);
49  *
50  * if (!g_app_info_launch_default_for_uri ("http://www.gtk.org", context, &error))
51  *   g_warning ("Launching failed: %s\n", error->message);
52  *
53  * g_object_unref (context);
54  * </programlisting>
55  * </informalexample>
56  * </example>
57  */
58
59
60 static void    gdk_app_launch_context_finalize    (GObject           *object);
61 static gchar * gdk_app_launch_context_get_display (GAppLaunchContext *context,
62                                                    GAppInfo          *info,
63                                                    GList             *files);
64 static gchar * gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
65                                                              GAppInfo          *info,
66                                                              GList             *files);
67 static void    gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
68                                                      const gchar       *startup_notify_id);
69
70
71 enum
72 {
73   PROP_0,
74   PROP_DISPLAY
75 };
76
77 G_DEFINE_TYPE (GdkAppLaunchContext, gdk_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
78
79 static void
80 gdk_app_launch_context_get_property (GObject    *object,
81                                      guint       prop_id,
82                                      GValue     *value,
83                                      GParamSpec *pspec)
84 {
85   GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
86
87   switch (prop_id)
88     {
89     case PROP_DISPLAY:
90       g_value_set_object (value, context->display);
91       break;
92     default:
93       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94     }
95 }
96
97 static void
98 gdk_app_launch_context_set_property (GObject      *object,
99                                      guint         prop_id,
100                                      const GValue *value,
101                                      GParamSpec   *pspec)
102 {
103   GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
104
105   switch (prop_id)
106     {
107     case PROP_DISPLAY:
108       context->display = g_value_dup_object (value);
109       break;
110     default:
111       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112     }
113 }
114
115 static void
116 gdk_app_launch_context_class_init (GdkAppLaunchContextClass *klass)
117 {
118   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119   GAppLaunchContextClass *context_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);
120
121   gobject_class->set_property = gdk_app_launch_context_set_property,
122   gobject_class->get_property = gdk_app_launch_context_get_property;
123
124   gobject_class->finalize = gdk_app_launch_context_finalize;
125
126   context_class->get_display = gdk_app_launch_context_get_display;
127   context_class->get_startup_notify_id = gdk_app_launch_context_get_startup_notify_id;
128   context_class->launch_failed = gdk_app_launch_context_launch_failed;
129
130   g_object_class_install_property (gobject_class, PROP_DISPLAY,
131     g_param_spec_object ("display", P_("Display"), P_("Display"),
132                          GDK_TYPE_DISPLAY,
133                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
134 }
135
136 static void
137 gdk_app_launch_context_init (GdkAppLaunchContext *context)
138 {
139   context->workspace = -1;
140 }
141
142 static void
143 gdk_app_launch_context_finalize (GObject *object)
144 {
145   GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
146
147   if (context->display)
148     g_object_unref (context->display);
149
150   if (context->screen)
151     g_object_unref (context->screen);
152
153   if (context->icon)
154     g_object_unref (context->icon);
155
156   g_free (context->icon_name);
157
158   G_OBJECT_CLASS (gdk_app_launch_context_parent_class)->finalize (object);
159 }
160
161 static gchar *
162 gdk_app_launch_context_get_display (GAppLaunchContext *context,
163                                     GAppInfo          *info,
164                                     GList             *files)
165 {
166   GdkAppLaunchContext *ctx = GDK_APP_LAUNCH_CONTEXT (context);
167   GdkDisplay *display;
168
169   if (ctx->screen)
170     return gdk_screen_make_display_name (ctx->screen);
171
172   if (ctx->display)
173     display = ctx->display;
174   else
175     display = gdk_display_get_default ();
176
177   return g_strdup (gdk_display_get_name (display));
178 }
179
180 /**
181  * gdk_app_launch_context_set_display:
182  * @context: a #GdkAppLaunchContext
183  * @display: a #GdkDisplay
184  *
185  * Sets the display on which applications will be launched when
186  * using this context. See also gdk_app_launch_context_set_screen().
187  *
188  * Since: 2.14
189  *
190  * Deprecated: 3.0: Use gdk_display_get_app_launch_context() instead
191  */
192 void
193 gdk_app_launch_context_set_display (GdkAppLaunchContext *context,
194                                     GdkDisplay          *display)
195 {
196   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
197   g_return_if_fail (display == NULL || GDK_IS_DISPLAY (display));
198
199   g_warn_if_fail (display == NULL || display == context->display);
200 }
201
202 /**
203  * gdk_app_launch_context_set_screen:
204  * @context: a #GdkAppLaunchContext
205  * @screen: a #GdkScreen
206  *
207  * Sets the screen on which applications will be launched when
208  * using this context. See also gdk_app_launch_context_set_display().
209  *
210  * If both @screen and @display are set, the @screen takes priority.
211  * If neither @screen or @display are set, the default screen and
212  * display are used.
213  *
214  * Since: 2.14
215  */
216 void
217 gdk_app_launch_context_set_screen (GdkAppLaunchContext *context,
218                                    GdkScreen           *screen)
219 {
220   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
221   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
222
223   g_return_if_fail (screen == NULL || gdk_screen_get_display (screen) == context->display);
224
225   if (context->screen)
226     {
227       g_object_unref (context->screen);
228       context->screen = NULL;
229     }
230
231   if (screen)
232     context->screen = g_object_ref (screen);
233 }
234
235 /**
236  * gdk_app_launch_context_set_desktop:
237  * @context: a #GdkAppLaunchContext
238  * @desktop: the number of a workspace, or -1
239  *
240  * Sets the workspace on which applications will be launched when
241  * using this context when running under a window manager that
242  * supports multiple workspaces, as described in the
243  * <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended
244  * Window Manager Hints</ulink>.
245  *
246  * When the workspace is not specified or @desktop is set to -1,
247  * it is up to the window manager to pick one, typically it will
248  * be the current workspace.
249  *
250  * Since: 2.14
251  */
252 void
253 gdk_app_launch_context_set_desktop (GdkAppLaunchContext *context,
254                                     gint                 desktop)
255 {
256   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
257
258   context->workspace = desktop;
259 }
260
261 /**
262  * gdk_app_launch_context_set_timestamp:
263  * @context: a #GdkAppLaunchContext
264  * @timestamp: a timestamp
265  *
266  * Sets the timestamp of @context. The timestamp should ideally
267  * be taken from the event that triggered the launch.
268  *
269  * Window managers can use this information to avoid moving the
270  * focus to the newly launched application when the user is busy
271  * typing in another window. This is also known as 'focus stealing
272  * prevention'.
273  *
274  * Since: 2.14
275  */
276 void
277 gdk_app_launch_context_set_timestamp (GdkAppLaunchContext *context,
278                                       guint32              timestamp)
279 {
280   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
281
282   context->timestamp = timestamp;
283 }
284
285 /**
286  * gdk_app_launch_context_set_icon:
287  * @context: a #GdkAppLaunchContext
288  * @icon: (allow-none): a #GIcon, or %NULL
289  *
290  * Sets the icon for applications that are launched with this
291  * context.
292  *
293  * Window Managers can use this information when displaying startup
294  * notification.
295  *
296  * See also gdk_app_launch_context_set_icon_name().
297  *
298  * Since: 2.14
299  */
300 void
301 gdk_app_launch_context_set_icon (GdkAppLaunchContext *context,
302                                  GIcon               *icon)
303 {
304   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
305   g_return_if_fail (icon == NULL || G_IS_ICON (icon));
306
307   if (context->icon)
308     {
309       g_object_unref (context->icon);
310       context->icon = NULL;
311     }
312
313   if (icon)
314     context->icon = g_object_ref (icon);
315 }
316
317 /**
318  * gdk_app_launch_context_set_icon_name:
319  * @context: a #GdkAppLaunchContext
320  * @icon_name: (allow-none): an icon name, or %NULL
321  *
322  * Sets the icon for applications that are launched with this context.
323  * The @icon_name will be interpreted in the same way as the Icon field
324  * in desktop files. See also gdk_app_launch_context_set_icon().
325  *
326  * If both @icon and @icon_name are set, the @icon_name takes priority.
327  * If neither @icon or @icon_name is set, the icon is taken from either
328  * the file that is passed to launched application or from the #GAppInfo
329  * for the launched application itself.
330  *
331  * Since: 2.14
332  */
333 void
334 gdk_app_launch_context_set_icon_name (GdkAppLaunchContext *context,
335                                       const char          *icon_name)
336 {
337   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
338
339   g_free (context->icon_name);
340   context->icon_name = g_strdup (icon_name);
341 }
342
343 /**
344  * gdk_app_launch_context_new:
345  *
346  * Creates a new #GdkAppLaunchContext.
347  *
348  * Returns: a new #GdkAppLaunchContext
349  *
350  * Since: 2.14
351  *
352  * Deprecated: 3.0: Use gdk_display_get_app_launch_context() instead
353  */
354 GdkAppLaunchContext *
355 gdk_app_launch_context_new (void)
356 {
357   return gdk_display_get_app_launch_context (gdk_display_get_default ());
358 }
359
360 static char *
361 gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
362                                               GAppInfo          *info,
363                                               GList             *files)
364 {
365  return NULL;
366 }
367
368 static void
369 gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
370                                       const gchar       *startup_notify_id)
371 {
372 }