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 this library. If not, see <http://www.gnu.org/licenses/>.
17
18 Author: Alexander Larsson <alexl@redhat.com>
19*/
20
21#include "config.h"
22
23#include "gdkapplaunchcontextprivate.h"
24#include "gdkdisplay.h"
25#include "gdkintl.h"
26
27
28/**
29 * GdkAppLaunchContext:
30 *
31 * `GdkAppLaunchContext` handles launching an application in a graphical context.
32 *
33 * It is an implementation of `GAppLaunchContext` that provides startup
34 * notification and allows to launch applications on a specific workspace.
35 *
36 * ## Launching an application
37 *
38 * ```c
39 * GdkAppLaunchContext *context;
40 *
41 * context = gdk_display_get_app_launch_context (display);
42 *
43 * gdk_app_launch_context_set_timestamp (gdk_event_get_time (event));
44 *
45 * if (!g_app_info_launch_default_for_uri ("http://www.gtk.org", context, &error))
46 * g_warning ("Launching failed: %s\n", error->message);
47 *
48 * g_object_unref (context);
49 * ```
50 */
51
52static void gdk_app_launch_context_finalize (GObject *object);
53static char * gdk_app_launch_context_get_display_name (GAppLaunchContext *context,
54 GAppInfo *info,
55 GList *files);
56static char * gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
57 GAppInfo *info,
58 GList *files);
59static void gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
60 const char *startup_notify_id);
61
62
63enum
64{
65 PROP_0,
66 PROP_DISPLAY
67};
68
69G_DEFINE_TYPE (GdkAppLaunchContext, gdk_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
70
71static void
72gdk_app_launch_context_get_property (GObject *object,
73 guint prop_id,
74 GValue *value,
75 GParamSpec *pspec)
76{
77 GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
78
79 switch (prop_id)
80 {
81 case PROP_DISPLAY:
82 g_value_set_object (value, v_object: context->display);
83 break;
84 default:
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86 }
87}
88
89static void
90gdk_app_launch_context_set_property (GObject *object,
91 guint prop_id,
92 const GValue *value,
93 GParamSpec *pspec)
94{
95 GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
96
97 switch (prop_id)
98 {
99 case PROP_DISPLAY:
100 context->display = g_value_dup_object (value);
101 break;
102 default:
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 }
105}
106
107static void
108gdk_app_launch_context_class_init (GdkAppLaunchContextClass *klass)
109{
110 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
111 GAppLaunchContextClass *context_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);
112
113 gobject_class->set_property = gdk_app_launch_context_set_property,
114 gobject_class->get_property = gdk_app_launch_context_get_property;
115
116 gobject_class->finalize = gdk_app_launch_context_finalize;
117
118 context_class->get_display = gdk_app_launch_context_get_display_name;
119 context_class->get_startup_notify_id = gdk_app_launch_context_get_startup_notify_id;
120 context_class->launch_failed = gdk_app_launch_context_launch_failed;
121
122 /**
123 * GdkAppLaunchContext:display: (attributes org.gtk.Property.get=gdk_app_launch_context_get_display)
124 *
125 * The display that the `GdkAppLaunchContext` is on.
126 */
127 g_object_class_install_property (oclass: gobject_class, property_id: PROP_DISPLAY,
128 pspec: g_param_spec_object (name: "display", P_("Display"), P_("Display"),
129 GDK_TYPE_DISPLAY,
130 flags: G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
131}
132
133static void
134gdk_app_launch_context_init (GdkAppLaunchContext *context)
135{
136 context->workspace = -1;
137}
138
139static void
140gdk_app_launch_context_finalize (GObject *object)
141{
142 GdkAppLaunchContext *context = GDK_APP_LAUNCH_CONTEXT (object);
143
144 if (context->display)
145 g_object_unref (object: context->display);
146
147 if (context->icon)
148 g_object_unref (object: context->icon);
149
150 g_free (mem: context->icon_name);
151
152 G_OBJECT_CLASS (gdk_app_launch_context_parent_class)->finalize (object);
153}
154
155static char *
156gdk_app_launch_context_get_display_name (GAppLaunchContext *context,
157 GAppInfo *info,
158 GList *files)
159{
160 GdkAppLaunchContext *ctx = GDK_APP_LAUNCH_CONTEXT (context);
161 GdkDisplay *display;
162
163 if (ctx->display)
164 display = ctx->display;
165 else
166 display = gdk_display_get_default ();
167
168 return g_strdup (str: gdk_display_get_name (display));
169}
170
171/**
172 * gdk_app_launch_context_get_display: (attributes org.gtk.Method.get_property=display)
173 * @context: a `GdkAppLaunchContext`
174 *
175 * Gets the `GdkDisplay` that @context is for.
176 *
177 * Returns: (transfer none): the display of @context
178 */
179GdkDisplay *
180gdk_app_launch_context_get_display (GdkAppLaunchContext *context)
181{
182 g_return_val_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context), NULL);
183
184 return context->display;
185}
186
187/**
188 * gdk_app_launch_context_set_desktop:
189 * @context: a `GdkAppLaunchContext`
190 * @desktop: the number of a workspace, or -1
191 *
192 * Sets the workspace on which applications will be launched.
193 *
194 * This only works when running under a window manager that
195 * supports multiple workspaces, as described in the
196 * [Extended Window Manager Hints](http://www.freedesktop.org/Standards/wm-spec).
197 * Specifically this sets the `_NET_WM_DESKTOP` property described
198 * in that spec.
199 *
200 * This only works when using the X11 backend.
201 *
202 * When the workspace is not specified or @desktop is set to -1,
203 * it is up to the window manager to pick one, typically it will
204 * be the current workspace.
205 */
206void
207gdk_app_launch_context_set_desktop (GdkAppLaunchContext *context,
208 int desktop)
209{
210 g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
211
212 context->workspace = desktop;
213}
214
215/**
216 * gdk_app_launch_context_set_timestamp:
217 * @context: a `GdkAppLaunchContext`
218 * @timestamp: a timestamp
219 *
220 * Sets the timestamp of @context.
221 *
222 * The timestamp should ideally be taken from the event that
223 * triggered the launch.
224 *
225 * Window managers can use this information to avoid moving the
226 * focus to the newly launched application when the user is busy
227 * typing in another window. This is also known as 'focus stealing
228 * prevention'.
229 */
230void
231gdk_app_launch_context_set_timestamp (GdkAppLaunchContext *context,
232 guint32 timestamp)
233{
234 g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
235
236 context->timestamp = timestamp;
237}
238
239/**
240 * gdk_app_launch_context_set_icon:
241 * @context: a `GdkAppLaunchContext`
242 * @icon: (nullable): a `GIcon`
243 *
244 * Sets the icon for applications that are launched with this
245 * context.
246 *
247 * Window Managers can use this information when displaying startup
248 * notification.
249 *
250 * See also [method@Gdk.AppLaunchContext.set_icon_name].
251 */
252void
253gdk_app_launch_context_set_icon (GdkAppLaunchContext *context,
254 GIcon *icon)
255{
256 g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
257 g_return_if_fail (icon == NULL || G_IS_ICON (icon));
258
259 if (context->icon)
260 {
261 g_object_unref (object: context->icon);
262 context->icon = NULL;
263 }
264
265 if (icon)
266 context->icon = g_object_ref (icon);
267}
268
269/**
270 * gdk_app_launch_context_set_icon_name:
271 * @context: a `GdkAppLaunchContext`
272 * @icon_name: (nullable): an icon name
273 *
274 * Sets the icon for applications that are launched with this context.
275 *
276 * The @icon_name will be interpreted in the same way as the Icon field
277 * in desktop files. See also [method@Gdk.AppLaunchContext.set_icon].
278 *
279 * If both @icon and @icon_name are set, the @icon_name takes priority.
280 * If neither @icon or @icon_name is set, the icon is taken from either
281 * the file that is passed to launched application or from the `GAppInfo`
282 * for the launched application itself.
283 */
284void
285gdk_app_launch_context_set_icon_name (GdkAppLaunchContext *context,
286 const char *icon_name)
287{
288 g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
289
290 g_free (mem: context->icon_name);
291 context->icon_name = g_strdup (str: icon_name);
292}
293
294static char *
295gdk_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
296 GAppInfo *info,
297 GList *files)
298{
299 return NULL;
300}
301
302static void
303gdk_app_launch_context_launch_failed (GAppLaunchContext *context,
304 const char *startup_notify_id)
305{
306}
307

source code of gtk/gdk/gdkapplaunchcontext.c