1 /*
2  * GTK - The GIMP Toolkit
3  * Copyright (C) 2008  Jaap Haitsma <jaap@haitsma.org>
4  *
5  * All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <gdk/gdk.h>
24 
25 #include "gtkshow.h"
26 #include "gtkwindowprivate.h"
27 
28 /**
29  * gtk_show_uri:
30  * @screen: (allow-none): screen to show the uri on
31  *     or %NULL for the default screen
32  * @uri: the uri to show
33  * @timestamp: a timestamp to prevent focus stealing
34  * @error: a #GError that is returned in case of errors
35  *
36  * A convenience function for launching the default application
37  * to show the uri. Like gtk_show_uri_on_window(), but takes a screen
38  * as transient parent instead of a window.
39  *
40  * Note that this function is deprecated as it does not pass the necessary
41  * information for helpers to parent their dialog properly, when run from
42  * sandboxed applications for example.
43  *
44  * Returns: %TRUE on success, %FALSE on error
45  *
46  * Since: 2.14
47  *
48  * Deprecated: 3.22: Use gtk_show_uri_on_window() instead.
49  */
50 gboolean
gtk_show_uri(GdkScreen * screen,const gchar * uri,guint32 timestamp,GError ** error)51 gtk_show_uri (GdkScreen    *screen,
52               const gchar  *uri,
53               guint32       timestamp,
54               GError      **error)
55 {
56   GdkAppLaunchContext *context;
57   gboolean ret;
58   GdkDisplay *display;
59 
60   g_return_val_if_fail (uri != NULL, FALSE);
61 
62   if (screen != NULL)
63     display = gdk_screen_get_display (screen);
64   else
65     display = gdk_display_get_default ();
66 
67   context = gdk_display_get_app_launch_context (display);
68   gdk_app_launch_context_set_screen (context, screen);
69   gdk_app_launch_context_set_timestamp (context, timestamp);
70 
71   ret = g_app_info_launch_default_for_uri (uri, G_APP_LAUNCH_CONTEXT (context), error);
72   g_object_unref (context);
73 
74   return ret;
75 }
76 
77 static void
launch_uri_done(GObject * source,GAsyncResult * result,gpointer data)78 launch_uri_done (GObject      *source,
79                  GAsyncResult *result,
80                  gpointer      data)
81 {
82   GtkWindow *window = data;
83 
84   g_app_info_launch_default_for_uri_finish (result, NULL);
85 
86   if (window)
87     gtk_window_unexport_handle (window);
88 }
89 
90 static void
window_handle_exported(GtkWindow * window,const char * handle_str,gpointer user_data)91 window_handle_exported (GtkWindow  *window,
92                         const char *handle_str,
93                         gpointer    user_data)
94 {
95   GAppLaunchContext *context = user_data;
96   const char *uri;
97 
98   uri = (const char *)g_object_get_data (G_OBJECT (context), "uri");
99 
100   g_app_launch_context_setenv (context, "PARENT_WINDOW_ID", handle_str);
101 
102   g_app_info_launch_default_for_uri_async (uri, G_APP_LAUNCH_CONTEXT (context), NULL, launch_uri_done, window);
103 
104   g_object_unref (context);
105 }
106 
107 /**
108  * gtk_show_uri_on_window:
109  * @parent: (allow-none): parent window
110  * @uri: the uri to show
111  * @timestamp: a timestamp to prevent focus stealing
112  * @error: a #GError that is returned in case of errors
113  *
114  * This is a convenience function for launching the default application
115  * to show the uri. The uri must be of a form understood by GIO (i.e. you
116  * need to install gvfs to get support for uri schemes such as http://
117  * or ftp://, as only local files are handled by GIO itself).
118  * Typical examples are
119  * - `file:///home/gnome/pict.jpg`
120  * - `http://www.gnome.org`
121  * - `mailto:me@gnome.org`
122  *
123  * Ideally the timestamp is taken from the event triggering
124  * the gtk_show_uri() call. If timestamp is not known you can take
125  * %GDK_CURRENT_TIME.
126  *
127  * This is the recommended call to be used as it passes information
128  * necessary for sandbox helpers to parent their dialogs properly.
129  *
130  * Returns: %TRUE on success, %FALSE on error
131  *
132  * Since: 3.22
133  */
134 gboolean
gtk_show_uri_on_window(GtkWindow * parent,const char * uri,guint32 timestamp,GError ** error)135 gtk_show_uri_on_window (GtkWindow   *parent,
136                         const char  *uri,
137                         guint32      timestamp,
138                         GError     **error)
139 {
140   GdkAppLaunchContext *context;
141   gboolean ret;
142   GdkDisplay *display;
143 
144   g_return_val_if_fail (uri != NULL, FALSE);
145 
146   if (parent)
147     display = gtk_widget_get_display (GTK_WIDGET (parent));
148   else
149     display = gdk_display_get_default ();
150 
151   context = gdk_display_get_app_launch_context (display);
152   gdk_app_launch_context_set_timestamp (context, timestamp);
153 
154   g_object_set_data_full (G_OBJECT (context), "uri", g_strdup (uri), g_free);
155 
156   if (parent && gtk_window_export_handle (parent, window_handle_exported, context))
157     return TRUE;
158 
159   ret = g_app_info_launch_default_for_uri (uri, G_APP_LAUNCH_CONTEXT (context), error);
160   g_object_unref (context);
161 
162   return ret;
163 }
164