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 "gdkapplaunchcontext.h"
26 #include "gdkinternals.h"
27 #include "gdkscreen.h"
28 #include "gdkintl.h"
29 #include "gdkalias.h"
30 
31 
32 static void    gdk_app_launch_context_finalize    (GObject           *object);
33 static gchar * gdk_app_launch_context_get_display (GAppLaunchContext *context,
34                                                    GAppInfo          *info,
35                                                    GList             *files);
36 
37 
G_DEFINE_TYPE(GdkAppLaunchContext,gdk_app_launch_context,G_TYPE_APP_LAUNCH_CONTEXT)38 G_DEFINE_TYPE (GdkAppLaunchContext, gdk_app_launch_context,
39 	       G_TYPE_APP_LAUNCH_CONTEXT)
40 
41 static void
42 gdk_app_launch_context_class_init (GdkAppLaunchContextClass *klass)
43 {
44   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
45   GAppLaunchContextClass *context_class = G_APP_LAUNCH_CONTEXT_CLASS (klass);
46 
47   gobject_class->finalize = gdk_app_launch_context_finalize;
48 
49   context_class->get_display = gdk_app_launch_context_get_display;
50   context_class->get_startup_notify_id = _gdk_windowing_get_startup_notify_id;
51   context_class->launch_failed = _gdk_windowing_launch_failed;
52 
53   g_type_class_add_private (klass, sizeof (GdkAppLaunchContextPrivate));
54 }
55 
56 static void
gdk_app_launch_context_init(GdkAppLaunchContext * context)57 gdk_app_launch_context_init (GdkAppLaunchContext *context)
58 {
59   context->priv = G_TYPE_INSTANCE_GET_PRIVATE (context,
60 					       GDK_TYPE_APP_LAUNCH_CONTEXT,
61 					       GdkAppLaunchContextPrivate);
62   context->priv->workspace = -1;
63 }
64 
65 static void
gdk_app_launch_context_finalize(GObject * object)66 gdk_app_launch_context_finalize (GObject *object)
67 {
68   GdkAppLaunchContext *context;
69   GdkAppLaunchContextPrivate *priv;
70 
71   context = GDK_APP_LAUNCH_CONTEXT (object);
72 
73   priv = context->priv;
74 
75   if (priv->display)
76     g_object_unref (priv->display);
77 
78   if (priv->screen)
79     g_object_unref (priv->screen);
80 
81   if (priv->icon)
82     g_object_unref (priv->icon);
83 
84   g_free (priv->icon_name);
85 
86   G_OBJECT_CLASS (gdk_app_launch_context_parent_class)->finalize (object);
87 }
88 
89 static gchar *
gdk_app_launch_context_get_display(GAppLaunchContext * context,GAppInfo * info,GList * files)90 gdk_app_launch_context_get_display (GAppLaunchContext *context,
91                                     GAppInfo          *info,
92                                     GList             *files)
93 {
94   GdkDisplay *display;
95   GdkAppLaunchContextPrivate *priv;
96 
97   priv = GDK_APP_LAUNCH_CONTEXT (context)->priv;
98 
99   if (priv->screen)
100     return gdk_screen_make_display_name (priv->screen);
101 
102   if (priv->display)
103     display = priv->display;
104   else
105     display = gdk_display_get_default ();
106 
107   return g_strdup (gdk_display_get_name (display));
108 }
109 
110 /**
111  * gdk_app_launch_context_set_display:
112  * @context: a #GdkAppLaunchContext
113  * @display: a #GdkDisplay
114  *
115  * Sets the display on which applications will be launched when
116  * using this context. See also gdk_app_launch_context_set_screen().
117  *
118  * Since: 2.14
119  */
120 void
gdk_app_launch_context_set_display(GdkAppLaunchContext * context,GdkDisplay * display)121 gdk_app_launch_context_set_display (GdkAppLaunchContext *context,
122 				    GdkDisplay          *display)
123 {
124   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
125   g_return_if_fail (display == NULL || GDK_IS_DISPLAY (display));
126 
127   if (context->priv->display)
128     {
129       g_object_unref (context->priv->display);
130       context->priv->display = NULL;
131     }
132 
133   if (display)
134     context->priv->display = g_object_ref (display);
135 }
136 
137 /**
138  * gdk_app_launch_context_set_screen:
139  * @context: a #GdkAppLaunchContext
140  * @screen: a #GdkScreen
141  *
142  * Sets the screen on which applications will be launched when
143  * using this context. See also gdk_app_launch_context_set_display().
144  *
145  * If both @screen and @display are set, the @screen takes priority.
146  * If neither @screen or @display are set, the default screen and
147  * display are used.
148  *
149  * Since: 2.14
150  */
151 void
gdk_app_launch_context_set_screen(GdkAppLaunchContext * context,GdkScreen * screen)152 gdk_app_launch_context_set_screen (GdkAppLaunchContext *context,
153 				   GdkScreen           *screen)
154 {
155   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
156   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
157 
158   if (context->priv->screen)
159     {
160       g_object_unref (context->priv->screen);
161       context->priv->screen = NULL;
162     }
163 
164   if (screen)
165     context->priv->screen = g_object_ref (screen);
166 }
167 
168 /**
169  * gdk_app_launch_context_set_desktop:
170  * @context: a #GdkAppLaunchContext
171  * @desktop: the number of a workspace, or -1
172  *
173  * Sets the workspace on which applications will be launched when
174  * using this context when running under a window manager that
175  * supports multiple workspaces, as described in the
176  * <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended
177  * Window Manager Hints</ulink>.
178  *
179  * When the workspace is not specified or @desktop is set to -1,
180  * it is up to the window manager to pick one, typically it will
181  * be the current workspace.
182  *
183  * Since: 2.14
184  */
185 void
gdk_app_launch_context_set_desktop(GdkAppLaunchContext * context,gint desktop)186 gdk_app_launch_context_set_desktop (GdkAppLaunchContext *context,
187 				    gint                 desktop)
188 {
189   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
190 
191   context->priv->workspace = desktop;
192 }
193 
194 /**
195  * gdk_app_launch_context_set_timestamp:
196  * @context: a #GdkAppLaunchContext
197  * @timestamp: a timestamp
198  *
199  * Sets the timestamp of @context. The timestamp should ideally
200  * be taken from the event that triggered the launch.
201  *
202  * Window managers can use this information to avoid moving the
203  * focus to the newly launched application when the user is busy
204  * typing in another window. This is also known as 'focus stealing
205  * prevention'.
206  *
207  * Since: 2.14
208  */
209 void
gdk_app_launch_context_set_timestamp(GdkAppLaunchContext * context,guint32 timestamp)210 gdk_app_launch_context_set_timestamp (GdkAppLaunchContext *context,
211 				      guint32              timestamp)
212 {
213   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
214 
215   context->priv->timestamp = timestamp;
216 }
217 
218 /**
219  * gdk_app_launch_context_set_icon:
220  * @context: a #GdkAppLaunchContext
221  * @icon: (allow-none): a #GIcon, or %NULL
222  *
223  * Sets the icon for applications that are launched with this
224  * context.
225  *
226  * Window Managers can use this information when displaying startup
227  * notification.
228  *
229  * See also gdk_app_launch_context_set_icon_name().
230  *
231  * Since: 2.14
232  */
233 void
gdk_app_launch_context_set_icon(GdkAppLaunchContext * context,GIcon * icon)234 gdk_app_launch_context_set_icon (GdkAppLaunchContext *context,
235                                  GIcon               *icon)
236 {
237   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
238   g_return_if_fail (icon == NULL || G_IS_ICON (icon));
239 
240   if (context->priv->icon)
241     {
242       g_object_unref (context->priv->icon);
243       context->priv->icon = NULL;
244     }
245 
246   if (icon)
247     context->priv->icon = g_object_ref (icon);
248 }
249 
250 /**
251  * gdk_app_launch_context_set_icon_name:
252  * @context: a #GdkAppLaunchContext
253  * @icon_name: (allow-none): an icon name, or %NULL
254  *
255  * Sets the icon for applications that are launched with this context.
256  * The @icon_name will be interpreted in the same way as the Icon field
257  * in desktop files. See also gdk_app_launch_context_set_icon().
258  *
259  * If both @icon and @icon_name are set, the @icon_name takes priority.
260  * If neither @icon or @icon_name is set, the icon is taken from either
261  * the file that is passed to launched application or from the #GAppInfo
262  * for the launched application itself.
263  *
264  * Since: 2.14
265  */
266 void
gdk_app_launch_context_set_icon_name(GdkAppLaunchContext * context,const char * icon_name)267 gdk_app_launch_context_set_icon_name (GdkAppLaunchContext *context,
268 				      const char          *icon_name)
269 {
270   g_return_if_fail (GDK_IS_APP_LAUNCH_CONTEXT (context));
271 
272   g_free (context->priv->icon_name);
273   context->priv->icon_name = g_strdup (icon_name);
274 }
275 
276 /**
277  * gdk_app_launch_context_new:
278  *
279  * Creates a new #GdkAppLaunchContext.
280  *
281  * Returns: a new #GdkAppLaunchContext
282  *
283  * Since: 2.14
284  */
285 GdkAppLaunchContext *
gdk_app_launch_context_new(void)286 gdk_app_launch_context_new (void)
287 {
288   return g_object_new (GDK_TYPE_APP_LAUNCH_CONTEXT, NULL);
289 }
290 
291 #define __GDK_APP_LAUNCH_CONTEXT_C__
292 #include "gdkaliasdef.c"
293