2008-05-26 09:51:48 +00:00
|
|
|
/*
|
2008-07-01 22:57:50 +00:00
|
|
|
* GTK - The GIMP Toolkit
|
2008-05-25 08:20:37 +00:00
|
|
|
* Copyright (C) 2008 Jaap Haitsma <jaap@haitsma.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2008-05-25 08:20:37 +00:00
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2008-05-25 08:20:37 +00:00
|
|
|
|
2008-06-23 10:47:22 +00:00
|
|
|
#include <gdk/gdk.h>
|
2008-05-25 08:20:37 +00:00
|
|
|
|
|
|
|
#include "gtkshow.h"
|
2016-07-26 19:44:49 +00:00
|
|
|
#include "gtkwindowprivate.h"
|
2020-04-12 01:56:32 +00:00
|
|
|
#include "gtkmessagedialog.h"
|
|
|
|
#include "gtkintl.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GtkWindow *parent;
|
|
|
|
GAppLaunchContext *context;
|
|
|
|
char *uri;
|
|
|
|
GTask *task;
|
|
|
|
} GtkShowUriData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_show_uri_data_free (GtkShowUriData *data)
|
|
|
|
{
|
|
|
|
if (data->parent)
|
|
|
|
gtk_window_unexport_handle (data->parent);
|
|
|
|
g_clear_object (&data->parent);
|
|
|
|
g_clear_object (&data->context);
|
|
|
|
g_free (data->uri);
|
|
|
|
g_clear_object (&data->task);
|
|
|
|
g_free (data);
|
|
|
|
}
|
2016-06-18 21:59:40 +00:00
|
|
|
|
2016-07-26 19:44:49 +00:00
|
|
|
static void
|
|
|
|
launch_uri_done (GObject *source,
|
|
|
|
GAsyncResult *result,
|
2020-04-12 01:56:32 +00:00
|
|
|
gpointer user_data)
|
2016-07-26 19:44:49 +00:00
|
|
|
{
|
2020-04-12 01:56:32 +00:00
|
|
|
GtkShowUriData *data = user_data;
|
|
|
|
GError *error = NULL;
|
2016-07-26 19:44:49 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
if (g_app_info_launch_default_for_uri_finish (result, &error))
|
|
|
|
g_task_return_boolean (data->task, TRUE);
|
|
|
|
else
|
|
|
|
g_task_return_error (data->task, error);
|
2016-07-26 19:44:49 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
gtk_show_uri_data_free (data);
|
2016-07-26 19:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
window_handle_exported (GtkWindow *window,
|
2020-04-12 01:56:32 +00:00
|
|
|
const char *handle,
|
2016-07-26 19:44:49 +00:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
2020-04-12 01:56:32 +00:00
|
|
|
GtkShowUriData *data = user_data;
|
2016-07-26 19:44:49 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
if (handle)
|
|
|
|
g_app_launch_context_setenv (data->context, "PARENT_WINDOW_ID", handle);
|
2016-07-26 19:44:49 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
g_app_info_launch_default_for_uri_async (data->uri,
|
|
|
|
data->context,
|
|
|
|
g_task_get_cancellable (data->task),
|
|
|
|
launch_uri_done,
|
|
|
|
data);
|
2016-07-26 19:44:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 21:59:40 +00:00
|
|
|
/**
|
2020-04-12 01:56:32 +00:00
|
|
|
* gtk_show_uri_full:
|
2020-04-16 18:06:04 +00:00
|
|
|
* @parent: (nullable): parent window
|
2016-06-18 21:59:40 +00:00
|
|
|
* @uri: the uri to show
|
2020-04-12 01:56:32 +00:00
|
|
|
* @timestamp: timestamp from the event that triggered this call, or %GDK_CURRENT_TIME
|
2020-04-16 18:06:04 +00:00
|
|
|
* @cancellable: (nullable): a #GCancellable to cancel the launch
|
2020-04-20 02:50:35 +00:00
|
|
|
* @callback: (scope async): a callback to call when the action is complete
|
2020-04-16 18:06:04 +00:00
|
|
|
* @user_data: (closure callback): data to pass to @callback
|
2016-06-18 21:59:40 +00:00
|
|
|
*
|
2020-04-12 01:56:32 +00:00
|
|
|
* This function launches the default application for showing
|
|
|
|
* a given uri.
|
|
|
|
*
|
|
|
|
* The @callback will be called when the launch is completed.
|
2020-05-11 16:47:20 +00:00
|
|
|
* It should call gtk_show_uri_full_finish() to obtain the result.
|
2017-02-15 12:35:13 +00:00
|
|
|
*
|
|
|
|
* This is the recommended call to be used as it passes information
|
|
|
|
* necessary for sandbox helpers to parent their dialogs properly.
|
2016-06-18 21:59:40 +00:00
|
|
|
*/
|
2020-04-12 01:56:32 +00:00
|
|
|
void
|
|
|
|
gtk_show_uri_full (GtkWindow *parent,
|
|
|
|
const char *uri,
|
|
|
|
guint32 timestamp,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2016-06-18 21:59:40 +00:00
|
|
|
{
|
2020-04-12 01:56:32 +00:00
|
|
|
GtkShowUriData *data;
|
2016-06-18 21:59:40 +00:00
|
|
|
GdkAppLaunchContext *context;
|
|
|
|
GdkDisplay *display;
|
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
|
|
|
|
g_return_if_fail (uri != NULL);
|
2016-06-18 21:59:40 +00:00
|
|
|
|
|
|
|
if (parent)
|
|
|
|
display = gtk_widget_get_display (GTK_WIDGET (parent));
|
|
|
|
else
|
|
|
|
display = gdk_display_get_default ();
|
|
|
|
|
|
|
|
context = gdk_display_get_app_launch_context (display);
|
2016-07-26 19:44:49 +00:00
|
|
|
gdk_app_launch_context_set_timestamp (context, timestamp);
|
2016-06-18 21:59:40 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
data = g_new0 (GtkShowUriData, 1);
|
|
|
|
data->parent = parent ? g_object_ref (parent) : NULL;
|
|
|
|
data->context = G_APP_LAUNCH_CONTEXT (context);
|
|
|
|
data->uri = g_strdup (uri);
|
|
|
|
data->task = g_task_new (parent, cancellable, callback, user_data);
|
|
|
|
g_task_set_source_tag (data->task, gtk_show_uri);
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
gtk_window_export_handle (parent, window_handle_exported, data);
|
|
|
|
else
|
|
|
|
window_handle_exported (parent, NULL, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_show_uri_full_finish:
|
|
|
|
* @parent: the #GtkWindow passed to gtk_show_uri()
|
2020-05-11 16:47:20 +00:00
|
|
|
* @result: #GAsyncResult that was passed to @callback
|
2020-04-12 01:56:32 +00:00
|
|
|
* @error: return location for an error
|
|
|
|
*
|
2021-02-04 20:02:22 +00:00
|
|
|
* Finishes the gtk_show_uri() call and returns the result
|
2020-04-12 01:56:32 +00:00
|
|
|
* of the operation.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the URI was shown successfully.
|
2021-05-18 21:05:26 +00:00
|
|
|
* Otherwise, %FALSE is returned and @error is set
|
2020-04-12 01:56:32 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gtk_show_uri_full_finish (GtkWindow *parent,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), FALSE);
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, parent), FALSE);
|
|
|
|
g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == gtk_show_uri, FALSE);
|
2016-06-18 21:59:40 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
|
|
|
}
|
2016-06-18 21:59:40 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
static void
|
|
|
|
show_uri_done (GObject *object,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWindow *parent = GTK_WINDOW (object);
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!gtk_show_uri_full_finish (parent, result, &error))
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (parent,
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT |
|
|
|
|
GTK_DIALOG_MODAL,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"%s", _("Could not show link"));
|
|
|
|
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
|
|
|
|
"%s", error->message);
|
|
|
|
|
|
|
|
g_signal_connect (dialog, "response",
|
2020-05-09 14:33:02 +00:00
|
|
|
G_CALLBACK (gtk_window_destroy), NULL);
|
2020-04-12 01:56:32 +00:00
|
|
|
|
|
|
|
gtk_window_present (GTK_WINDOW (dialog));
|
2020-09-16 19:59:49 +00:00
|
|
|
|
|
|
|
g_error_free (error);
|
2020-04-12 01:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-25 08:20:37 +00:00
|
|
|
|
2020-04-12 01:56:32 +00:00
|
|
|
/**
|
|
|
|
* gtk_show_uri:
|
2021-05-19 11:24:34 +00:00
|
|
|
* @parent: (nullable): parent window
|
2020-04-12 01:56:32 +00:00
|
|
|
* @uri: the uri to show
|
|
|
|
* @timestamp: timestamp from the event that triggered this call, or %GDK_CURRENT_TIME
|
|
|
|
*
|
|
|
|
* This function launches the default application for showing
|
|
|
|
* a given uri, or shows an error dialog if that fails.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_show_uri (GtkWindow *parent,
|
|
|
|
const char *uri,
|
|
|
|
guint32 timestamp)
|
|
|
|
{
|
|
|
|
gtk_show_uri_full (parent, uri, timestamp, NULL, show_uri_done, NULL);
|
2008-05-25 08:20:37 +00:00
|
|
|
}
|