Add a wrapper for the AddFull document portal api
[glib.git] / gio / gappinfo.c
blob471f9755d4c63ed444afabb9ba4dacb1ac0aca57
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include "gappinfo.h"
24 #include "gappinfoprivate.h"
25 #include "gcontextspecificgroup.h"
26 #include "gtask.h"
28 #include "glibintl.h"
29 #include <gioerror.h>
30 #include <gfile.h>
32 #ifdef G_OS_UNIX
33 #include "gdbusconnection.h"
34 #include "gdbusmessage.h"
35 #include "gdocumentportal.h"
36 #include "gportalsupport.h"
37 #endif
39 #ifdef G_OS_UNIX
40 #define FLATPAK_OPENURI_PORTAL_BUS_NAME "org.freedesktop.portal.Desktop"
41 #define FLATPAK_OPENURI_PORTAL_PATH "/org/freedesktop/portal/desktop"
42 #define FLATPAK_OPENURI_PORTAL_IFACE "org.freedesktop.portal.OpenURI"
43 #define FLATPAK_OPENURI_PORTAL_METHOD "OpenURI"
44 #endif
46 /**
47 * SECTION:gappinfo
48 * @short_description: Application information and launch contexts
49 * @include: gio/gio.h
50 * @see_also: #GAppInfoMonitor
52 * #GAppInfo and #GAppLaunchContext are used for describing and launching
53 * applications installed on the system.
55 * As of GLib 2.20, URIs will always be converted to POSIX paths
56 * (using g_file_get_path()) when using g_app_info_launch() even if
57 * the application requested an URI and not a POSIX path. For example
58 * for an desktop-file based application with Exec key `totem
59 * %U` and a single URI, `sftp://foo/file.avi`, then
60 * `/home/user/.gvfs/sftp on foo/file.avi` will be passed. This will
61 * only work if a set of suitable GIO extensions (such as gvfs 2.26
62 * compiled with FUSE support), is available and operational; if this
63 * is not the case, the URI will be passed unmodified to the application.
64 * Some URIs, such as `mailto:`, of course cannot be mapped to a POSIX
65 * path (in gvfs there's no FUSE mount for it); such URIs will be
66 * passed unmodified to the application.
68 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
69 * back to the GIO URI in the #GFile constructors (since gvfs
70 * implements the #GVfs extension point). As such, if the application
71 * needs to examine the URI, it needs to use g_file_get_uri() or
72 * similar on #GFile. In other words, an application cannot assume
73 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
74 * equal to the result of g_file_get_uri(). The following snippet
75 * illustrates this:
77 * |[
78 * GFile *f;
79 * char *uri;
81 * file = g_file_new_for_commandline_arg (uri_from_commandline);
83 * uri = g_file_get_uri (file);
84 * strcmp (uri, uri_from_commandline) == 0;
85 * g_free (uri);
87 * if (g_file_has_uri_scheme (file, "cdda"))
88 * {
89 * // do something special with uri
90 * }
91 * g_object_unref (file);
92 * ]|
94 * This code will work when both `cdda://sr0/Track 1.wav` and
95 * `/home/user/.gvfs/cdda on sr0/Track 1.wav` is passed to the
96 * application. It should be noted that it's generally not safe
97 * for applications to rely on the format of a particular URIs.
98 * Different launcher applications (e.g. file managers) may have
99 * different ideas of what a given URI means.
102 struct _GAppLaunchContextPrivate {
103 char **envp;
106 typedef GAppInfoIface GAppInfoInterface;
107 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
109 static void
110 g_app_info_default_init (GAppInfoInterface *iface)
116 * g_app_info_dup:
117 * @appinfo: a #GAppInfo.
119 * Creates a duplicate of a #GAppInfo.
121 * Returns: (transfer full): a duplicate of @appinfo.
123 GAppInfo *
124 g_app_info_dup (GAppInfo *appinfo)
126 GAppInfoIface *iface;
128 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
130 iface = G_APP_INFO_GET_IFACE (appinfo);
132 return (* iface->dup) (appinfo);
136 * g_app_info_equal:
137 * @appinfo1: the first #GAppInfo.
138 * @appinfo2: the second #GAppInfo.
140 * Checks if two #GAppInfos are equal.
142 * Note that the check <em>may not</em> compare each individual field, and
143 * only does an identity check. In case detecting changes in the contents
144 * is needed, program code must additionally compare relevant fields.
146 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
148 gboolean
149 g_app_info_equal (GAppInfo *appinfo1,
150 GAppInfo *appinfo2)
152 GAppInfoIface *iface;
154 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
155 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
157 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
158 return FALSE;
160 iface = G_APP_INFO_GET_IFACE (appinfo1);
162 return (* iface->equal) (appinfo1, appinfo2);
166 * g_app_info_get_id:
167 * @appinfo: a #GAppInfo.
169 * Gets the ID of an application. An id is a string that
170 * identifies the application. The exact format of the id is
171 * platform dependent. For instance, on Unix this is the
172 * desktop file id from the xdg menu specification.
174 * Note that the returned ID may be %NULL, depending on how
175 * the @appinfo has been constructed.
177 * Returns: a string containing the application's ID.
179 const char *
180 g_app_info_get_id (GAppInfo *appinfo)
182 GAppInfoIface *iface;
184 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
186 iface = G_APP_INFO_GET_IFACE (appinfo);
188 return (* iface->get_id) (appinfo);
192 * g_app_info_get_name:
193 * @appinfo: a #GAppInfo.
195 * Gets the installed name of the application.
197 * Returns: the name of the application for @appinfo.
199 const char *
200 g_app_info_get_name (GAppInfo *appinfo)
202 GAppInfoIface *iface;
204 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
206 iface = G_APP_INFO_GET_IFACE (appinfo);
208 return (* iface->get_name) (appinfo);
212 * g_app_info_get_display_name:
213 * @appinfo: a #GAppInfo.
215 * Gets the display name of the application. The display name is often more
216 * descriptive to the user than the name itself.
218 * Returns: the display name of the application for @appinfo, or the name if
219 * no display name is available.
221 * Since: 2.24
223 const char *
224 g_app_info_get_display_name (GAppInfo *appinfo)
226 GAppInfoIface *iface;
228 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
230 iface = G_APP_INFO_GET_IFACE (appinfo);
232 if (iface->get_display_name == NULL)
233 return (* iface->get_name) (appinfo);
235 return (* iface->get_display_name) (appinfo);
239 * g_app_info_get_description:
240 * @appinfo: a #GAppInfo.
242 * Gets a human-readable description of an installed application.
244 * Returns: a string containing a description of the
245 * application @appinfo, or %NULL if none.
247 const char *
248 g_app_info_get_description (GAppInfo *appinfo)
250 GAppInfoIface *iface;
252 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
254 iface = G_APP_INFO_GET_IFACE (appinfo);
256 return (* iface->get_description) (appinfo);
260 * g_app_info_get_executable:
261 * @appinfo: a #GAppInfo
263 * Gets the executable's name for the installed application.
265 * Returns: (type filename): a string containing the @appinfo's application
266 * binaries name
268 const char *
269 g_app_info_get_executable (GAppInfo *appinfo)
271 GAppInfoIface *iface;
273 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
275 iface = G_APP_INFO_GET_IFACE (appinfo);
277 return (* iface->get_executable) (appinfo);
282 * g_app_info_get_commandline:
283 * @appinfo: a #GAppInfo
285 * Gets the commandline with which the application will be
286 * started.
288 * Returns: (type filename): a string containing the @appinfo's commandline,
289 * or %NULL if this information is not available
291 * Since: 2.20
293 const char *
294 g_app_info_get_commandline (GAppInfo *appinfo)
296 GAppInfoIface *iface;
298 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
300 iface = G_APP_INFO_GET_IFACE (appinfo);
302 if (iface->get_commandline)
303 return (* iface->get_commandline) (appinfo);
305 return NULL;
309 * g_app_info_set_as_default_for_type:
310 * @appinfo: a #GAppInfo.
311 * @content_type: the content type.
312 * @error: a #GError.
314 * Sets the application as the default handler for a given type.
316 * Returns: %TRUE on success, %FALSE on error.
318 gboolean
319 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
320 const char *content_type,
321 GError **error)
323 GAppInfoIface *iface;
325 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
326 g_return_val_if_fail (content_type != NULL, FALSE);
328 iface = G_APP_INFO_GET_IFACE (appinfo);
330 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
334 * g_app_info_set_as_last_used_for_type:
335 * @appinfo: a #GAppInfo.
336 * @content_type: the content type.
337 * @error: a #GError.
339 * Sets the application as the last used application for a given type.
340 * This will make the application appear as first in the list returned
341 * by g_app_info_get_recommended_for_type(), regardless of the default
342 * application for that content type.
344 * Returns: %TRUE on success, %FALSE on error.
346 gboolean
347 g_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
348 const char *content_type,
349 GError **error)
351 GAppInfoIface *iface;
353 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
354 g_return_val_if_fail (content_type != NULL, FALSE);
356 iface = G_APP_INFO_GET_IFACE (appinfo);
358 return (* iface->set_as_last_used_for_type) (appinfo, content_type, error);
362 * g_app_info_set_as_default_for_extension:
363 * @appinfo: a #GAppInfo.
364 * @extension: (type filename): a string containing the file extension
365 * (without the dot).
366 * @error: a #GError.
368 * Sets the application as the default handler for the given file extension.
370 * Returns: %TRUE on success, %FALSE on error.
372 gboolean
373 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
374 const char *extension,
375 GError **error)
377 GAppInfoIface *iface;
379 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
380 g_return_val_if_fail (extension != NULL, FALSE);
382 iface = G_APP_INFO_GET_IFACE (appinfo);
384 if (iface->set_as_default_for_extension)
385 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
387 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
388 "g_app_info_set_as_default_for_extension not supported yet");
389 return FALSE;
394 * g_app_info_add_supports_type:
395 * @appinfo: a #GAppInfo.
396 * @content_type: a string.
397 * @error: a #GError.
399 * Adds a content type to the application information to indicate the
400 * application is capable of opening files with the given content type.
402 * Returns: %TRUE on success, %FALSE on error.
404 gboolean
405 g_app_info_add_supports_type (GAppInfo *appinfo,
406 const char *content_type,
407 GError **error)
409 GAppInfoIface *iface;
411 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
412 g_return_val_if_fail (content_type != NULL, FALSE);
414 iface = G_APP_INFO_GET_IFACE (appinfo);
416 if (iface->add_supports_type)
417 return (* iface->add_supports_type) (appinfo, content_type, error);
419 g_set_error_literal (error, G_IO_ERROR,
420 G_IO_ERROR_NOT_SUPPORTED,
421 "g_app_info_add_supports_type not supported yet");
423 return FALSE;
428 * g_app_info_can_remove_supports_type:
429 * @appinfo: a #GAppInfo.
431 * Checks if a supported content type can be removed from an application.
433 * Returns: %TRUE if it is possible to remove supported
434 * content types from a given @appinfo, %FALSE if not.
436 gboolean
437 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
439 GAppInfoIface *iface;
441 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
443 iface = G_APP_INFO_GET_IFACE (appinfo);
445 if (iface->can_remove_supports_type)
446 return (* iface->can_remove_supports_type) (appinfo);
448 return FALSE;
453 * g_app_info_remove_supports_type:
454 * @appinfo: a #GAppInfo.
455 * @content_type: a string.
456 * @error: a #GError.
458 * Removes a supported type from an application, if possible.
460 * Returns: %TRUE on success, %FALSE on error.
462 gboolean
463 g_app_info_remove_supports_type (GAppInfo *appinfo,
464 const char *content_type,
465 GError **error)
467 GAppInfoIface *iface;
469 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
470 g_return_val_if_fail (content_type != NULL, FALSE);
472 iface = G_APP_INFO_GET_IFACE (appinfo);
474 if (iface->remove_supports_type)
475 return (* iface->remove_supports_type) (appinfo, content_type, error);
477 g_set_error_literal (error, G_IO_ERROR,
478 G_IO_ERROR_NOT_SUPPORTED,
479 "g_app_info_remove_supports_type not supported yet");
481 return FALSE;
485 * g_app_info_get_supported_types:
486 * @appinfo: a #GAppInfo that can handle files
488 * Retrieves the list of content types that @app_info claims to support.
489 * If this information is not provided by the environment, this function
490 * will return %NULL.
491 * This function does not take in consideration associations added with
492 * g_app_info_add_supports_type(), but only those exported directly by
493 * the application.
495 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
496 * a list of content types.
498 * Since: 2.34
500 const char **
501 g_app_info_get_supported_types (GAppInfo *appinfo)
503 GAppInfoIface *iface;
505 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
507 iface = G_APP_INFO_GET_IFACE (appinfo);
509 if (iface->get_supported_types)
510 return iface->get_supported_types (appinfo);
511 else
512 return NULL;
517 * g_app_info_get_icon:
518 * @appinfo: a #GAppInfo.
520 * Gets the icon for the application.
522 * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
523 * if there is no default icon.
525 GIcon *
526 g_app_info_get_icon (GAppInfo *appinfo)
528 GAppInfoIface *iface;
530 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
532 iface = G_APP_INFO_GET_IFACE (appinfo);
534 return (* iface->get_icon) (appinfo);
539 * g_app_info_launch:
540 * @appinfo: a #GAppInfo
541 * @files: (nullable) (element-type GFile): a #GList of #GFile objects
542 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
543 * @error: a #GError
545 * Launches the application. Passes @files to the launched application
546 * as arguments, using the optional @launch_context to get information
547 * about the details of the launcher (like what screen it is on).
548 * On error, @error will be set accordingly.
550 * To launch the application without arguments pass a %NULL @files list.
552 * Note that even if the launch is successful the application launched
553 * can fail to start if it runs into problems during startup. There is
554 * no way to detect this.
556 * Some URIs can be changed when passed through a GFile (for instance
557 * unsupported URIs with strange formats like mailto:), so if you have
558 * a textual URI you want to pass in as argument, consider using
559 * g_app_info_launch_uris() instead.
561 * The launched application inherits the environment of the launching
562 * process, but it can be modified with g_app_launch_context_setenv()
563 * and g_app_launch_context_unsetenv().
565 * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
566 * environment variable with the path of the launched desktop file and
567 * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
568 * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
569 * should it be inherited by further processes. The `DISPLAY` and
570 * `DESKTOP_STARTUP_ID` environment variables are also set, based
571 * on information provided in @launch_context.
573 * Returns: %TRUE on successful launch, %FALSE otherwise.
575 gboolean
576 g_app_info_launch (GAppInfo *appinfo,
577 GList *files,
578 GAppLaunchContext *launch_context,
579 GError **error)
581 GAppInfoIface *iface;
583 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
585 iface = G_APP_INFO_GET_IFACE (appinfo);
587 return (* iface->launch) (appinfo, files, launch_context, error);
592 * g_app_info_supports_uris:
593 * @appinfo: a #GAppInfo.
595 * Checks if the application supports reading files and directories from URIs.
597 * Returns: %TRUE if the @appinfo supports URIs.
599 gboolean
600 g_app_info_supports_uris (GAppInfo *appinfo)
602 GAppInfoIface *iface;
604 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
606 iface = G_APP_INFO_GET_IFACE (appinfo);
608 return (* iface->supports_uris) (appinfo);
613 * g_app_info_supports_files:
614 * @appinfo: a #GAppInfo.
616 * Checks if the application accepts files as arguments.
618 * Returns: %TRUE if the @appinfo supports files.
620 gboolean
621 g_app_info_supports_files (GAppInfo *appinfo)
623 GAppInfoIface *iface;
625 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
627 iface = G_APP_INFO_GET_IFACE (appinfo);
629 return (* iface->supports_files) (appinfo);
634 * g_app_info_launch_uris:
635 * @appinfo: a #GAppInfo
636 * @uris: (nullable) (element-type utf8): a #GList containing URIs to launch.
637 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
638 * @error: a #GError
640 * Launches the application. This passes the @uris to the launched application
641 * as arguments, using the optional @launch_context to get information
642 * about the details of the launcher (like what screen it is on).
643 * On error, @error will be set accordingly.
645 * To launch the application without arguments pass a %NULL @uris list.
647 * Note that even if the launch is successful the application launched
648 * can fail to start if it runs into problems during startup. There is
649 * no way to detect this.
651 * Returns: %TRUE on successful launch, %FALSE otherwise.
653 gboolean
654 g_app_info_launch_uris (GAppInfo *appinfo,
655 GList *uris,
656 GAppLaunchContext *launch_context,
657 GError **error)
659 GAppInfoIface *iface;
661 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
663 iface = G_APP_INFO_GET_IFACE (appinfo);
665 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
670 * g_app_info_should_show:
671 * @appinfo: a #GAppInfo.
673 * Checks if the application info should be shown in menus that
674 * list available applications.
676 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
678 gboolean
679 g_app_info_should_show (GAppInfo *appinfo)
681 GAppInfoIface *iface;
683 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
685 iface = G_APP_INFO_GET_IFACE (appinfo);
687 return (* iface->should_show) (appinfo);
690 #ifdef G_OS_UNIX
691 static void
692 response_received (GDBusConnection *connection,
693 const char *sender_name,
694 const char *object_path,
695 const char *interface_name,
696 const char *signal_name,
697 GVariant *parameters,
698 gpointer user_data)
700 GTask *task = user_data;
701 guint32 response;
702 guint signal_id;
704 signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
705 g_dbus_connection_signal_unsubscribe (connection, signal_id);
707 g_variant_get (parameters, "(u@a{sv})", &response, NULL);
709 if (response == 0)
710 g_task_return_boolean (task, TRUE);
711 else if (response == 1)
712 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Launch cancelled");
713 else
714 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "Launch failed");
716 g_object_unref (task);
719 static void
720 open_uri_done (GObject *source,
721 GAsyncResult *result,
722 gpointer user_data)
724 GDBusConnection *connection = G_DBUS_CONNECTION (source);
725 GTask *task = user_data;
726 GVariant *res;
727 GError *error = NULL;
728 const char *path;
729 guint signal_id;
731 res = g_dbus_connection_call_finish (connection, result, &error);
733 if (res == NULL)
735 g_task_return_error (task, error);
736 g_object_unref (task);
737 return;
740 g_variant_get (res, "(&o)", &path);
742 signal_id =
743 g_dbus_connection_signal_subscribe (connection,
744 "org.freedesktop.portal.Desktop",
745 "org.freedesktop.portal.Request",
746 "Response",
747 path,
748 NULL,
749 G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
750 response_received,
751 task, NULL);
753 g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
755 g_variant_unref (res);
758 static char *
759 real_uri_for_portal (const char *uri,
760 GAppLaunchContext *context,
761 GCancellable *cancellable,
762 GAsyncReadyCallback callback,
763 gpointer user_data,
764 GError **error)
766 GFile *file = NULL;
767 char *real_uri = NULL;
769 file = g_file_new_for_uri (uri);
770 if (g_file_is_native (file))
772 real_uri = g_document_portal_add_document (file, error);
773 g_object_unref (file);
775 if (real_uri == NULL)
777 g_task_report_error (context, callback, user_data, NULL, *error);
778 return NULL;
781 else
783 g_object_unref (file);
784 real_uri = g_strdup (uri);
787 return real_uri;
790 static void
791 launch_default_with_portal_async (const char *uri,
792 GAppLaunchContext *context,
793 GCancellable *cancellable,
794 GAsyncReadyCallback callback,
795 gpointer user_data)
797 GDBusConnection *session_bus;
798 GVariantBuilder opt_builder;
799 const char *parent_window = NULL;
800 char *real_uri;
801 GTask *task;
802 GAsyncReadyCallback dbus_callback;
803 GError *error = NULL;
805 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
806 if (session_bus == NULL)
808 g_task_report_error (context, callback, user_data, NULL, error);
809 return;
812 if (context && context->priv->envp)
813 parent_window = g_environ_getenv (context->priv->envp, "PARENT_WINDOW_ID");
815 real_uri = real_uri_for_portal (uri, context, cancellable, callback, user_data, &error);
816 if (real_uri == NULL)
818 g_object_unref (session_bus);
819 return;
822 g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
824 if (callback)
826 task = g_task_new (context, cancellable, callback, user_data);
827 dbus_callback = open_uri_done;
829 else
831 task = NULL;
832 dbus_callback = NULL;
835 g_dbus_connection_call (session_bus,
836 FLATPAK_OPENURI_PORTAL_BUS_NAME,
837 FLATPAK_OPENURI_PORTAL_PATH,
838 FLATPAK_OPENURI_PORTAL_IFACE,
839 FLATPAK_OPENURI_PORTAL_METHOD,
840 g_variant_new ("(ss@a{sv})",
841 parent_window ? parent_window : "",
842 real_uri,
843 g_variant_builder_end (&opt_builder)),
844 NULL,
845 G_DBUS_CALL_FLAGS_NONE,
846 G_MAXINT,
847 cancellable,
848 dbus_callback,
849 task);
851 g_dbus_connection_flush (session_bus, cancellable, NULL, NULL);
852 g_object_unref (session_bus);
853 g_free (real_uri);
856 static void
857 launch_default_with_portal_sync (const char *uri,
858 GAppLaunchContext *context)
860 GDBusConnection *session_bus;
861 GVariantBuilder opt_builder;
862 GVariant *res = NULL;
863 const char *parent_window = NULL;
864 char *real_uri;
865 GError *error = NULL;
867 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
868 if (session_bus == NULL)
870 g_task_report_error (context, NULL, NULL, NULL, error);
871 return;
874 if (context && context->priv->envp)
875 parent_window = g_environ_getenv (context->priv->envp, "PARENT_WINDOW_ID");
877 real_uri = real_uri_for_portal (uri, context, NULL, NULL, NULL, &error);
878 if (real_uri == NULL)
880 g_object_unref (session_bus);
881 return;
884 g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
886 /* Calling the D-Bus method for the OpenURI portal "protects" the logic from
887 * not ever having the remote method running in case the xdg-desktop-portal
888 * process is not yet running and the caller quits quickly after the call.
890 res = g_dbus_connection_call_sync (session_bus,
891 FLATPAK_OPENURI_PORTAL_BUS_NAME,
892 FLATPAK_OPENURI_PORTAL_PATH,
893 FLATPAK_OPENURI_PORTAL_IFACE,
894 FLATPAK_OPENURI_PORTAL_METHOD,
895 g_variant_new ("(ss@a{sv})",
896 parent_window ? parent_window : "",
897 real_uri,
898 g_variant_builder_end (&opt_builder)),
899 NULL,
900 G_DBUS_CALL_FLAGS_NONE,
901 G_MAXINT,
902 NULL,
903 &error);
904 if (res == NULL)
905 g_task_report_error (context, NULL, NULL, NULL, error);
906 else
907 g_variant_unref (res);
909 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
910 g_object_unref (session_bus);
911 g_free (real_uri);
914 static gboolean
915 launch_default_with_portal (const char *uri,
916 GAppLaunchContext *context,
917 GError **error)
919 launch_default_with_portal_sync (uri, context);
920 return TRUE;
922 #endif
924 static gboolean
925 launch_default_for_uri (const char *uri,
926 GAppLaunchContext *context,
927 GError **error)
929 char *uri_scheme;
930 GAppInfo *app_info = NULL;
931 GList l;
932 gboolean res;
934 /* g_file_query_default_handler() calls
935 * g_app_info_get_default_for_uri_scheme() too, but we have to do it
936 * here anyway in case GFile can't parse @uri correctly.
938 uri_scheme = g_uri_parse_scheme (uri);
939 if (uri_scheme && uri_scheme[0] != '\0')
940 app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
941 g_free (uri_scheme);
943 if (!app_info)
945 GFile *file;
947 file = g_file_new_for_uri (uri);
948 app_info = g_file_query_default_handler (file, NULL, error);
949 g_object_unref (file);
952 if (app_info == NULL)
953 return FALSE;
955 l.data = (char *)uri;
956 l.next = l.prev = NULL;
957 res = g_app_info_launch_uris (app_info, &l, context, error);
959 g_object_unref (app_info);
961 return res;
965 * g_app_info_launch_default_for_uri:
966 * @uri: the uri to show
967 * @launch_context: (nullable): an optional #GAppLaunchContext
968 * @error: (nullable): return location for an error, or %NULL
970 * Utility function that launches the default application
971 * registered to handle the specified uri. Synchronous I/O
972 * is done on the uri to detect the type of the file if
973 * required.
975 * Returns: %TRUE on success, %FALSE on error.
977 gboolean
978 g_app_info_launch_default_for_uri (const char *uri,
979 GAppLaunchContext *launch_context,
980 GError **error)
982 if (launch_default_for_uri (uri, launch_context, error))
983 return TRUE;
985 #ifdef G_OS_UNIX
986 if (glib_should_use_portal ())
988 /* Reset any error previously set by launch_default_for_uri */
989 g_clear_error (error);
991 return launch_default_with_portal (uri, launch_context, error);
993 #endif
995 return FALSE;
999 * g_app_info_launch_default_for_uri_async:
1000 * @uri: the uri to show
1001 * @context: (nullable): an optional #GAppLaunchContext
1002 * cancellable: (nullable): a #GCancellable
1003 * @callback: (nullable): a #GASyncReadyCallback to call when the request is done
1004 * @user_data: (nullable): data to pass to @callback
1006 * Async version of g_app_info_launch_default_for_uri().
1008 * This version is useful if you are interested in receiving
1009 * error information in the case where the application is
1010 * sandboxed and the portal may present an application chooser
1011 * dialog to the user.
1013 * Since: 2.50
1015 void
1016 g_app_info_launch_default_for_uri_async (const char *uri,
1017 GAppLaunchContext *context,
1018 GCancellable *cancellable,
1019 GAsyncReadyCallback callback,
1020 gpointer user_data)
1022 gboolean res;
1023 GError *error = NULL;
1024 GTask *task;
1026 res = launch_default_for_uri (uri, context, &error);
1028 #ifdef G_OS_UNIX
1029 if (!res && glib_should_use_portal ())
1031 launch_default_with_portal_async (uri, context, cancellable, callback, user_data);
1032 return;
1034 #endif
1036 task = g_task_new (context, cancellable, callback, user_data);
1037 if (!res)
1038 g_task_return_error (task, error);
1039 else
1040 g_task_return_boolean (task, TRUE);
1042 g_object_unref (task);
1046 * g_app_info_launch_default_for_uri_finish:
1047 * @result: a #GAsyncResult
1048 * @error: (nullable): return location for an error, or %NULL
1050 * Finishes an asynchronous launch-default-for-uri operation.
1052 * Returns: %TRUE if the launch was successful, %FALSE if @error is set
1054 * Since: 2.50
1056 gboolean
1057 g_app_info_launch_default_for_uri_finish (GAsyncResult *result,
1058 GError **error)
1060 return g_task_propagate_boolean (G_TASK (result), error);
1064 * g_app_info_can_delete:
1065 * @appinfo: a #GAppInfo
1067 * Obtains the information whether the #GAppInfo can be deleted.
1068 * See g_app_info_delete().
1070 * Returns: %TRUE if @appinfo can be deleted
1072 * Since: 2.20
1074 gboolean
1075 g_app_info_can_delete (GAppInfo *appinfo)
1077 GAppInfoIface *iface;
1079 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
1081 iface = G_APP_INFO_GET_IFACE (appinfo);
1083 if (iface->can_delete)
1084 return (* iface->can_delete) (appinfo);
1086 return FALSE;
1091 * g_app_info_delete:
1092 * @appinfo: a #GAppInfo
1094 * Tries to delete a #GAppInfo.
1096 * On some platforms, there may be a difference between user-defined
1097 * #GAppInfos which can be deleted, and system-wide ones which cannot.
1098 * See g_app_info_can_delete().
1100 * Virtual: do_delete
1101 * Returns: %TRUE if @appinfo has been deleted
1103 * Since: 2.20
1105 gboolean
1106 g_app_info_delete (GAppInfo *appinfo)
1108 GAppInfoIface *iface;
1110 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
1112 iface = G_APP_INFO_GET_IFACE (appinfo);
1114 if (iface->do_delete)
1115 return (* iface->do_delete) (appinfo);
1117 return FALSE;
1121 enum {
1122 LAUNCH_FAILED,
1123 LAUNCHED,
1124 LAST_SIGNAL
1127 static guint signals[LAST_SIGNAL] = { 0 };
1129 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
1132 * g_app_launch_context_new:
1134 * Creates a new application launch context. This is not normally used,
1135 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
1137 * Returns: a #GAppLaunchContext.
1139 GAppLaunchContext *
1140 g_app_launch_context_new (void)
1142 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
1145 static void
1146 g_app_launch_context_finalize (GObject *object)
1148 GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
1150 g_strfreev (context->priv->envp);
1152 G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
1155 static void
1156 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
1158 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1160 object_class->finalize = g_app_launch_context_finalize;
1163 * GAppLaunchContext::launch-failed:
1164 * @context: the object emitting the signal
1165 * @startup_notify_id: the startup notification id for the failed launch
1167 * The ::launch-failed signal is emitted when a #GAppInfo launch
1168 * fails. The startup notification id is provided, so that the launcher
1169 * can cancel the startup notification.
1171 * Since: 2.36
1173 signals[LAUNCH_FAILED] = g_signal_new (I_("launch-failed"),
1174 G_OBJECT_CLASS_TYPE (object_class),
1175 G_SIGNAL_RUN_LAST,
1176 G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
1177 NULL, NULL, NULL,
1178 G_TYPE_NONE, 1, G_TYPE_STRING);
1181 * GAppLaunchContext::launched:
1182 * @context: the object emitting the signal
1183 * @info: the #GAppInfo that was just launched
1184 * @platform_data: additional platform-specific data for this launch
1186 * The ::launched signal is emitted when a #GAppInfo is successfully
1187 * launched. The @platform_data is an GVariant dictionary mapping
1188 * strings to variants (ie a{sv}), which contains additional,
1189 * platform-specific data about this launch. On UNIX, at least the
1190 * "pid" and "startup-notification-id" keys will be present.
1192 * Since: 2.36
1194 signals[LAUNCHED] = g_signal_new (I_("launched"),
1195 G_OBJECT_CLASS_TYPE (object_class),
1196 G_SIGNAL_RUN_LAST,
1197 G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
1198 NULL, NULL, NULL,
1199 G_TYPE_NONE, 2,
1200 G_TYPE_APP_INFO, G_TYPE_VARIANT);
1203 static void
1204 g_app_launch_context_init (GAppLaunchContext *context)
1206 context->priv = g_app_launch_context_get_instance_private (context);
1210 * g_app_launch_context_setenv:
1211 * @context: a #GAppLaunchContext
1212 * @variable: the environment variable to set
1213 * @value: the value for to set the variable to.
1215 * Arranges for @variable to be set to @value in the child's
1216 * environment when @context is used to launch an application.
1218 * Since: 2.32
1220 void
1221 g_app_launch_context_setenv (GAppLaunchContext *context,
1222 const char *variable,
1223 const char *value)
1225 if (!context->priv->envp)
1226 context->priv->envp = g_get_environ ();
1228 context->priv->envp =
1229 g_environ_setenv (context->priv->envp, variable, value, TRUE);
1233 * g_app_launch_context_unsetenv:
1234 * @context: a #GAppLaunchContext
1235 * @variable: the environment variable to remove
1237 * Arranges for @variable to be unset in the child's environment
1238 * when @context is used to launch an application.
1240 * Since: 2.32
1242 void
1243 g_app_launch_context_unsetenv (GAppLaunchContext *context,
1244 const char *variable)
1246 if (!context->priv->envp)
1247 context->priv->envp = g_get_environ ();
1249 context->priv->envp =
1250 g_environ_unsetenv (context->priv->envp, variable);
1254 * g_app_launch_context_get_environment:
1255 * @context: a #GAppLaunchContext
1257 * Gets the complete environment variable list to be passed to
1258 * the child process when @context is used to launch an application.
1259 * This is a %NULL-terminated array of strings, where each string has
1260 * the form `KEY=VALUE`.
1262 * Returns: (array zero-terminated=1) (transfer full): the
1263 * child's environment
1265 * Since: 2.32
1267 char **
1268 g_app_launch_context_get_environment (GAppLaunchContext *context)
1270 if (!context->priv->envp)
1271 context->priv->envp = g_get_environ ();
1273 return g_strdupv (context->priv->envp);
1277 * g_app_launch_context_get_display:
1278 * @context: a #GAppLaunchContext
1279 * @info: a #GAppInfo
1280 * @files: (element-type GFile): a #GList of #GFile objects
1282 * Gets the display string for the @context. This is used to ensure new
1283 * applications are started on the same display as the launching
1284 * application, by setting the `DISPLAY` environment variable.
1286 * Returns: a display string for the display.
1288 char *
1289 g_app_launch_context_get_display (GAppLaunchContext *context,
1290 GAppInfo *info,
1291 GList *files)
1293 GAppLaunchContextClass *class;
1295 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
1296 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
1298 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1300 if (class->get_display == NULL)
1301 return NULL;
1303 return class->get_display (context, info, files);
1307 * g_app_launch_context_get_startup_notify_id:
1308 * @context: a #GAppLaunchContext
1309 * @info: a #GAppInfo
1310 * @files: (element-type GFile): a #GList of of #GFile objects
1312 * Initiates startup notification for the application and returns the
1313 * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
1315 * Startup notification IDs are defined in the
1316 * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
1318 * Returns: a startup notification ID for the application, or %NULL if
1319 * not supported.
1321 char *
1322 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
1323 GAppInfo *info,
1324 GList *files)
1326 GAppLaunchContextClass *class;
1328 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
1329 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
1331 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1333 if (class->get_startup_notify_id == NULL)
1334 return NULL;
1336 return class->get_startup_notify_id (context, info, files);
1341 * g_app_launch_context_launch_failed:
1342 * @context: a #GAppLaunchContext.
1343 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1345 * Called when an application has failed to launch, so that it can cancel
1346 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1349 void
1350 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1351 const char *startup_notify_id)
1353 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1354 g_return_if_fail (startup_notify_id != NULL);
1356 g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1361 * SECTION:gappinfomonitor
1362 * @short_description: Monitor application information for changes
1364 * #GAppInfoMonitor is a very simple object used for monitoring the app
1365 * info database for changes (ie: newly installed or removed
1366 * applications).
1368 * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1369 * to the "changed" signal.
1371 * In the usual case, applications should try to make note of the change
1372 * (doing things like invalidating caches) but not act on it. In
1373 * particular, applications should avoid making calls to #GAppInfo APIs
1374 * in response to the change signal, deferring these until the time that
1375 * the data is actually required. The exception to this case is when
1376 * application information is actually being displayed on the screen
1377 * (eg: during a search or when the list of all applications is shown).
1378 * The reason for this is that changes to the list of installed
1379 * applications often come in groups (like during system updates) and
1380 * rescanning the list on every change is pointless and expensive.
1382 * Since: 2.40
1386 * GAppInfoMonitor:
1388 * The only thing you can do with this is to get it via
1389 * g_app_info_monitor_get() and connect to the "changed" signal.
1391 * Since: 2.40
1394 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1396 struct _GAppInfoMonitor
1398 GObject parent_instance;
1399 GMainContext *context;
1402 struct _GAppInfoMonitorClass
1404 GObjectClass parent_class;
1407 static GContextSpecificGroup g_app_info_monitor_group;
1408 static guint g_app_info_monitor_changed_signal;
1410 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1412 static void
1413 g_app_info_monitor_finalize (GObject *object)
1415 GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1417 g_context_specific_group_remove (&g_app_info_monitor_group, monitor->context, monitor, NULL);
1419 G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1422 static void
1423 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1427 static void
1428 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1430 GObjectClass *object_class = G_OBJECT_CLASS (class);
1433 * GAppInfoMonitor::changed:
1435 * Signal emitted when the app info database for changes (ie: newly installed
1436 * or removed applications).
1438 g_app_info_monitor_changed_signal = g_signal_new (I_("changed"), G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1439 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1441 object_class->finalize = g_app_info_monitor_finalize;
1445 * g_app_info_monitor_get:
1447 * Gets the #GAppInfoMonitor for the current thread-default main
1448 * context.
1450 * The #GAppInfoMonitor will emit a "changed" signal in the
1451 * thread-default main context whenever the list of installed
1452 * applications (as reported by g_app_info_get_all()) may have changed.
1454 * You must only call g_object_unref() on the return value from under
1455 * the same main context as you created it.
1457 * Returns: (transfer full): a reference to a #GAppInfoMonitor
1459 * Since: 2.40
1461 GAppInfoMonitor *
1462 g_app_info_monitor_get (void)
1464 return g_context_specific_group_get (&g_app_info_monitor_group,
1465 G_TYPE_APP_INFO_MONITOR,
1466 G_STRUCT_OFFSET (GAppInfoMonitor, context),
1467 NULL);
1470 void
1471 g_app_info_monitor_fire (void)
1473 g_context_specific_group_emit (&g_app_info_monitor_group, g_app_info_monitor_changed_signal);