gtestutils: Fix a typo in a documentation comment
[glib.git] / gio / gappinfo.c
blob5c4baa22f3922c498660cb78df2c14bbb298214c
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 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
40 /**
41 * SECTION:gappinfo
42 * @short_description: Application information and launch contexts
43 * @include: gio/gio.h
44 * @see_also: #GAppInfoMonitor
46 * #GAppInfo and #GAppLaunchContext are used for describing and launching
47 * applications installed on the system.
49 * As of GLib 2.20, URIs will always be converted to POSIX paths
50 * (using g_file_get_path()) when using g_app_info_launch() even if
51 * the application requested an URI and not a POSIX path. For example
52 * for an desktop-file based application with Exec key `totem
53 * %U` and a single URI, `sftp://foo/file.avi`, then
54 * `/home/user/.gvfs/sftp on foo/file.avi` will be passed. This will
55 * only work if a set of suitable GIO extensions (such as gvfs 2.26
56 * compiled with FUSE support), is available and operational; if this
57 * is not the case, the URI will be passed unmodified to the application.
58 * Some URIs, such as `mailto:`, of course cannot be mapped to a POSIX
59 * path (in gvfs there's no FUSE mount for it); such URIs will be
60 * passed unmodified to the application.
62 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
63 * back to the GIO URI in the #GFile constructors (since gvfs
64 * implements the #GVfs extension point). As such, if the application
65 * needs to examine the URI, it needs to use g_file_get_uri() or
66 * similar on #GFile. In other words, an application cannot assume
67 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
68 * equal to the result of g_file_get_uri(). The following snippet
69 * illustrates this:
71 * |[
72 * GFile *f;
73 * char *uri;
75 * file = g_file_new_for_commandline_arg (uri_from_commandline);
77 * uri = g_file_get_uri (file);
78 * strcmp (uri, uri_from_commandline) == 0;
79 * g_free (uri);
81 * if (g_file_has_uri_scheme (file, "cdda"))
82 * {
83 * // do something special with uri
84 * }
85 * g_object_unref (file);
86 * ]|
88 * This code will work when both `cdda://sr0/Track 1.wav` and
89 * `/home/user/.gvfs/cdda on sr0/Track 1.wav` is passed to the
90 * application. It should be noted that it's generally not safe
91 * for applications to rely on the format of a particular URIs.
92 * Different launcher applications (e.g. file managers) may have
93 * different ideas of what a given URI means.
96 struct _GAppLaunchContextPrivate {
97 char **envp;
100 typedef GAppInfoIface GAppInfoInterface;
101 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
103 static void
104 g_app_info_default_init (GAppInfoInterface *iface)
110 * g_app_info_dup:
111 * @appinfo: a #GAppInfo.
113 * Creates a duplicate of a #GAppInfo.
115 * Returns: (transfer full): a duplicate of @appinfo.
117 GAppInfo *
118 g_app_info_dup (GAppInfo *appinfo)
120 GAppInfoIface *iface;
122 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
124 iface = G_APP_INFO_GET_IFACE (appinfo);
126 return (* iface->dup) (appinfo);
130 * g_app_info_equal:
131 * @appinfo1: the first #GAppInfo.
132 * @appinfo2: the second #GAppInfo.
134 * Checks if two #GAppInfos are equal.
136 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
138 gboolean
139 g_app_info_equal (GAppInfo *appinfo1,
140 GAppInfo *appinfo2)
142 GAppInfoIface *iface;
144 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
145 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
147 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
148 return FALSE;
150 iface = G_APP_INFO_GET_IFACE (appinfo1);
152 return (* iface->equal) (appinfo1, appinfo2);
156 * g_app_info_get_id:
157 * @appinfo: a #GAppInfo.
159 * Gets the ID of an application. An id is a string that
160 * identifies the application. The exact format of the id is
161 * platform dependent. For instance, on Unix this is the
162 * desktop file id from the xdg menu specification.
164 * Note that the returned ID may be %NULL, depending on how
165 * the @appinfo has been constructed.
167 * Returns: a string containing the application's ID.
169 const char *
170 g_app_info_get_id (GAppInfo *appinfo)
172 GAppInfoIface *iface;
174 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
176 iface = G_APP_INFO_GET_IFACE (appinfo);
178 return (* iface->get_id) (appinfo);
182 * g_app_info_get_name:
183 * @appinfo: a #GAppInfo.
185 * Gets the installed name of the application.
187 * Returns: the name of the application for @appinfo.
189 const char *
190 g_app_info_get_name (GAppInfo *appinfo)
192 GAppInfoIface *iface;
194 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
196 iface = G_APP_INFO_GET_IFACE (appinfo);
198 return (* iface->get_name) (appinfo);
202 * g_app_info_get_display_name:
203 * @appinfo: a #GAppInfo.
205 * Gets the display name of the application. The display name is often more
206 * descriptive to the user than the name itself.
208 * Returns: the display name of the application for @appinfo, or the name if
209 * no display name is available.
211 * Since: 2.24
213 const char *
214 g_app_info_get_display_name (GAppInfo *appinfo)
216 GAppInfoIface *iface;
218 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
220 iface = G_APP_INFO_GET_IFACE (appinfo);
222 if (iface->get_display_name == NULL)
223 return (* iface->get_name) (appinfo);
225 return (* iface->get_display_name) (appinfo);
229 * g_app_info_get_description:
230 * @appinfo: a #GAppInfo.
232 * Gets a human-readable description of an installed application.
234 * Returns: a string containing a description of the
235 * application @appinfo, or %NULL if none.
237 const char *
238 g_app_info_get_description (GAppInfo *appinfo)
240 GAppInfoIface *iface;
242 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
244 iface = G_APP_INFO_GET_IFACE (appinfo);
246 return (* iface->get_description) (appinfo);
250 * g_app_info_get_executable:
251 * @appinfo: a #GAppInfo
253 * Gets the executable's name for the installed application.
255 * Returns: (type filename): a string containing the @appinfo's application
256 * binaries name
258 const char *
259 g_app_info_get_executable (GAppInfo *appinfo)
261 GAppInfoIface *iface;
263 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
265 iface = G_APP_INFO_GET_IFACE (appinfo);
267 return (* iface->get_executable) (appinfo);
272 * g_app_info_get_commandline:
273 * @appinfo: a #GAppInfo
275 * Gets the commandline with which the application will be
276 * started.
278 * Returns: (type filename): a string containing the @appinfo's commandline,
279 * or %NULL if this information is not available
281 * Since: 2.20
283 const char *
284 g_app_info_get_commandline (GAppInfo *appinfo)
286 GAppInfoIface *iface;
288 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
290 iface = G_APP_INFO_GET_IFACE (appinfo);
292 if (iface->get_commandline)
293 return (* iface->get_commandline) (appinfo);
295 return NULL;
299 * g_app_info_set_as_default_for_type:
300 * @appinfo: a #GAppInfo.
301 * @content_type: the content type.
302 * @error: a #GError.
304 * Sets the application as the default handler for a given type.
306 * Returns: %TRUE on success, %FALSE on error.
308 gboolean
309 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
310 const char *content_type,
311 GError **error)
313 GAppInfoIface *iface;
315 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
316 g_return_val_if_fail (content_type != NULL, FALSE);
318 iface = G_APP_INFO_GET_IFACE (appinfo);
320 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
324 * g_app_info_set_as_last_used_for_type:
325 * @appinfo: a #GAppInfo.
326 * @content_type: the content type.
327 * @error: a #GError.
329 * Sets the application as the last used application for a given type.
330 * This will make the application appear as first in the list returned
331 * by g_app_info_get_recommended_for_type(), regardless of the default
332 * application for that content type.
334 * Returns: %TRUE on success, %FALSE on error.
336 gboolean
337 g_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
338 const char *content_type,
339 GError **error)
341 GAppInfoIface *iface;
343 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
344 g_return_val_if_fail (content_type != NULL, FALSE);
346 iface = G_APP_INFO_GET_IFACE (appinfo);
348 return (* iface->set_as_last_used_for_type) (appinfo, content_type, error);
352 * g_app_info_set_as_default_for_extension:
353 * @appinfo: a #GAppInfo.
354 * @extension: (type filename): a string containing the file extension
355 * (without the dot).
356 * @error: a #GError.
358 * Sets the application as the default handler for the given file extension.
360 * Returns: %TRUE on success, %FALSE on error.
362 gboolean
363 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
364 const char *extension,
365 GError **error)
367 GAppInfoIface *iface;
369 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
370 g_return_val_if_fail (extension != NULL, FALSE);
372 iface = G_APP_INFO_GET_IFACE (appinfo);
374 if (iface->set_as_default_for_extension)
375 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
377 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
378 "g_app_info_set_as_default_for_extension not supported yet");
379 return FALSE;
384 * g_app_info_add_supports_type:
385 * @appinfo: a #GAppInfo.
386 * @content_type: a string.
387 * @error: a #GError.
389 * Adds a content type to the application information to indicate the
390 * application is capable of opening files with the given content type.
392 * Returns: %TRUE on success, %FALSE on error.
394 gboolean
395 g_app_info_add_supports_type (GAppInfo *appinfo,
396 const char *content_type,
397 GError **error)
399 GAppInfoIface *iface;
401 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
402 g_return_val_if_fail (content_type != NULL, FALSE);
404 iface = G_APP_INFO_GET_IFACE (appinfo);
406 if (iface->add_supports_type)
407 return (* iface->add_supports_type) (appinfo, content_type, error);
409 g_set_error_literal (error, G_IO_ERROR,
410 G_IO_ERROR_NOT_SUPPORTED,
411 "g_app_info_add_supports_type not supported yet");
413 return FALSE;
418 * g_app_info_can_remove_supports_type:
419 * @appinfo: a #GAppInfo.
421 * Checks if a supported content type can be removed from an application.
423 * Returns: %TRUE if it is possible to remove supported
424 * content types from a given @appinfo, %FALSE if not.
426 gboolean
427 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
429 GAppInfoIface *iface;
431 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
433 iface = G_APP_INFO_GET_IFACE (appinfo);
435 if (iface->can_remove_supports_type)
436 return (* iface->can_remove_supports_type) (appinfo);
438 return FALSE;
443 * g_app_info_remove_supports_type:
444 * @appinfo: a #GAppInfo.
445 * @content_type: a string.
446 * @error: a #GError.
448 * Removes a supported type from an application, if possible.
450 * Returns: %TRUE on success, %FALSE on error.
452 gboolean
453 g_app_info_remove_supports_type (GAppInfo *appinfo,
454 const char *content_type,
455 GError **error)
457 GAppInfoIface *iface;
459 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
460 g_return_val_if_fail (content_type != NULL, FALSE);
462 iface = G_APP_INFO_GET_IFACE (appinfo);
464 if (iface->remove_supports_type)
465 return (* iface->remove_supports_type) (appinfo, content_type, error);
467 g_set_error_literal (error, G_IO_ERROR,
468 G_IO_ERROR_NOT_SUPPORTED,
469 "g_app_info_remove_supports_type not supported yet");
471 return FALSE;
475 * g_app_info_get_supported_types:
476 * @appinfo: a #GAppInfo that can handle files
478 * Retrieves the list of content types that @app_info claims to support.
479 * If this information is not provided by the environment, this function
480 * will return %NULL.
481 * This function does not take in consideration associations added with
482 * g_app_info_add_supports_type(), but only those exported directly by
483 * the application.
485 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
486 * a list of content types.
488 * Since: 2.34
490 const char **
491 g_app_info_get_supported_types (GAppInfo *appinfo)
493 GAppInfoIface *iface;
495 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
497 iface = G_APP_INFO_GET_IFACE (appinfo);
499 if (iface->get_supported_types)
500 return iface->get_supported_types (appinfo);
501 else
502 return NULL;
507 * g_app_info_get_icon:
508 * @appinfo: a #GAppInfo.
510 * Gets the icon for the application.
512 * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
513 * if there is no default icon.
515 GIcon *
516 g_app_info_get_icon (GAppInfo *appinfo)
518 GAppInfoIface *iface;
520 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
522 iface = G_APP_INFO_GET_IFACE (appinfo);
524 return (* iface->get_icon) (appinfo);
529 * g_app_info_launch:
530 * @appinfo: a #GAppInfo
531 * @files: (nullable) (element-type GFile): a #GList of #GFile objects
532 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
533 * @error: a #GError
535 * Launches the application. Passes @files to the launched application
536 * as arguments, using the optional @launch_context to get information
537 * about the details of the launcher (like what screen it is on).
538 * On error, @error will be set accordingly.
540 * To launch the application without arguments pass a %NULL @files list.
542 * Note that even if the launch is successful the application launched
543 * can fail to start if it runs into problems during startup. There is
544 * no way to detect this.
546 * Some URIs can be changed when passed through a GFile (for instance
547 * unsupported URIs with strange formats like mailto:), so if you have
548 * a textual URI you want to pass in as argument, consider using
549 * g_app_info_launch_uris() instead.
551 * The launched application inherits the environment of the launching
552 * process, but it can be modified with g_app_launch_context_setenv()
553 * and g_app_launch_context_unsetenv().
555 * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
556 * environment variable with the path of the launched desktop file and
557 * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
558 * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
559 * should it be inherited by further processes. The `DISPLAY` and
560 * `DESKTOP_STARTUP_ID` environment variables are also set, based
561 * on information provided in @launch_context.
563 * Returns: %TRUE on successful launch, %FALSE otherwise.
565 gboolean
566 g_app_info_launch (GAppInfo *appinfo,
567 GList *files,
568 GAppLaunchContext *launch_context,
569 GError **error)
571 GAppInfoIface *iface;
573 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
575 iface = G_APP_INFO_GET_IFACE (appinfo);
577 return (* iface->launch) (appinfo, files, launch_context, error);
582 * g_app_info_supports_uris:
583 * @appinfo: a #GAppInfo.
585 * Checks if the application supports reading files and directories from URIs.
587 * Returns: %TRUE if the @appinfo supports URIs.
589 gboolean
590 g_app_info_supports_uris (GAppInfo *appinfo)
592 GAppInfoIface *iface;
594 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
596 iface = G_APP_INFO_GET_IFACE (appinfo);
598 return (* iface->supports_uris) (appinfo);
603 * g_app_info_supports_files:
604 * @appinfo: a #GAppInfo.
606 * Checks if the application accepts files as arguments.
608 * Returns: %TRUE if the @appinfo supports files.
610 gboolean
611 g_app_info_supports_files (GAppInfo *appinfo)
613 GAppInfoIface *iface;
615 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
617 iface = G_APP_INFO_GET_IFACE (appinfo);
619 return (* iface->supports_files) (appinfo);
624 * g_app_info_launch_uris:
625 * @appinfo: a #GAppInfo
626 * @uris: (nullable) (element-type utf8): a #GList containing URIs to launch.
627 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
628 * @error: a #GError
630 * Launches the application. This passes the @uris to the launched application
631 * as arguments, using the optional @launch_context to get information
632 * about the details of the launcher (like what screen it is on).
633 * On error, @error will be set accordingly.
635 * To launch the application without arguments pass a %NULL @uris list.
637 * Note that even if the launch is successful the application launched
638 * can fail to start if it runs into problems during startup. There is
639 * no way to detect this.
641 * Returns: %TRUE on successful launch, %FALSE otherwise.
643 gboolean
644 g_app_info_launch_uris (GAppInfo *appinfo,
645 GList *uris,
646 GAppLaunchContext *launch_context,
647 GError **error)
649 GAppInfoIface *iface;
651 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
653 iface = G_APP_INFO_GET_IFACE (appinfo);
655 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
660 * g_app_info_should_show:
661 * @appinfo: a #GAppInfo.
663 * Checks if the application info should be shown in menus that
664 * list available applications.
666 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
668 gboolean
669 g_app_info_should_show (GAppInfo *appinfo)
671 GAppInfoIface *iface;
673 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
675 iface = G_APP_INFO_GET_IFACE (appinfo);
677 return (* iface->should_show) (appinfo);
680 #ifdef G_OS_UNIX
681 static void
682 response_received (GDBusConnection *connection,
683 const char *sender_name,
684 const char *object_path,
685 const char *interface_name,
686 const char *signal_name,
687 GVariant *parameters,
688 gpointer user_data)
690 GTask *task = user_data;
691 guint32 response;
692 guint signal_id;
694 signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
695 g_dbus_connection_signal_unsubscribe (connection, signal_id);
697 g_variant_get (parameters, "(u@a{sv})", &response, NULL);
699 if (response == 0)
700 g_task_return_boolean (task, TRUE);
701 else if (response == 1)
702 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Launch cancelled");
703 else
704 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "Launch failed");
706 g_object_unref (task);
709 static void
710 open_uri_done (GObject *source,
711 GAsyncResult *result,
712 gpointer user_data)
714 GDBusConnection *connection = G_DBUS_CONNECTION (source);
715 GTask *task = user_data;
716 GVariant *res;
717 GError *error = NULL;
718 const char *path;
719 guint signal_id;
721 res = g_dbus_connection_call_finish (connection, result, &error);
723 if (res == NULL)
725 g_task_return_error (task, error);
726 g_object_unref (task);
727 return;
730 g_variant_get (res, "(&o)", &path);
732 signal_id =
733 g_dbus_connection_signal_subscribe (connection,
734 "org.freedesktop.portal.Desktop",
735 "org.freedesktop.portal.Request",
736 "Response",
737 path,
738 NULL,
739 G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
740 response_received,
741 task, NULL);
743 g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
745 g_variant_unref (res);
748 static void
749 launch_default_with_portal_async (const char *uri,
750 GAppLaunchContext *context,
751 GCancellable *cancellable,
752 GAsyncReadyCallback callback,
753 gpointer user_data)
755 GDBusConnection *session_bus;
756 GVariantBuilder opt_builder;
757 const char *parent_window = NULL;
758 GFile *file;
759 char *real_uri;
760 GTask *task;
761 GAsyncReadyCallback dbus_callback;
762 GError *error = NULL;
764 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
765 if (session_bus == NULL)
767 g_task_report_error (context, callback, user_data, NULL, error);
768 return;
771 if (context && context->priv->envp)
772 parent_window = g_environ_getenv (context->priv->envp, "PARENT_WINDOW_ID");
774 g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
776 file = g_file_new_for_uri (uri);
778 if (g_file_is_native (file))
780 real_uri = g_document_portal_add_document (file, &error);
781 g_object_unref (file);
783 if (real_uri == NULL)
785 g_task_report_error (context, callback, user_data, NULL, error);
786 return;
789 else
791 g_object_unref (file);
792 real_uri = g_strdup (uri);
795 if (callback)
797 task = g_task_new (context, cancellable, callback, user_data);
798 dbus_callback = open_uri_done;
800 else
802 task = NULL;
803 dbus_callback = NULL;
806 g_dbus_connection_call (session_bus,
807 "org.freedesktop.portal.Desktop",
808 "/org/freedesktop/portal/desktop",
809 "org.freedesktop.portal.OpenURI",
810 "OpenURI",
811 g_variant_new ("(ss@a{sv})",
812 parent_window ? parent_window : "",
813 real_uri,
814 g_variant_builder_end (&opt_builder)),
815 NULL,
816 G_DBUS_CALL_FLAGS_NONE,
817 G_MAXINT,
818 cancellable,
819 dbus_callback,
820 task);
822 g_dbus_connection_flush (session_bus, cancellable, NULL, NULL);
823 g_object_unref (session_bus);
824 g_free (real_uri);
827 static gboolean
828 launch_default_with_portal (const char *uri,
829 GAppLaunchContext *context,
830 GError **error)
832 launch_default_with_portal_async (uri, context, NULL, NULL, NULL);
833 return TRUE;
835 #endif
837 static gboolean
838 launch_default_for_uri (const char *uri,
839 GAppLaunchContext *context,
840 GError **error)
842 char *uri_scheme;
843 GAppInfo *app_info = NULL;
844 GList l;
845 gboolean res;
847 /* g_file_query_default_handler() calls
848 * g_app_info_get_default_for_uri_scheme() too, but we have to do it
849 * here anyway in case GFile can't parse @uri correctly.
851 uri_scheme = g_uri_parse_scheme (uri);
852 if (uri_scheme && uri_scheme[0] != '\0')
853 app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
854 g_free (uri_scheme);
856 if (!app_info)
858 GFile *file;
860 file = g_file_new_for_uri (uri);
861 app_info = g_file_query_default_handler (file, NULL, error);
862 g_object_unref (file);
865 if (app_info == NULL)
866 return FALSE;
868 l.data = (char *)uri;
869 l.next = l.prev = NULL;
870 res = g_app_info_launch_uris (app_info, &l, context, error);
872 g_object_unref (app_info);
874 return res;
878 * g_app_info_launch_default_for_uri:
879 * @uri: the uri to show
880 * @launch_context: (nullable): an optional #GAppLaunchContext
881 * @error: (nullable): return location for an error, or %NULL
883 * Utility function that launches the default application
884 * registered to handle the specified uri. Synchronous I/O
885 * is done on the uri to detect the type of the file if
886 * required.
888 * Returns: %TRUE on success, %FALSE on error.
890 gboolean
891 g_app_info_launch_default_for_uri (const char *uri,
892 GAppLaunchContext *launch_context,
893 GError **error)
895 #ifdef G_OS_UNIX
896 if (glib_should_use_portal ())
897 return launch_default_with_portal (uri, launch_context, error);
898 else
899 #endif
900 return launch_default_for_uri (uri, launch_context, error);
904 * g_app_info_launch_default_for_uri_async:
905 * @uri: the uri to show
906 * @context: (nullable): an optional #GAppLaunchContext
907 * cancellable: (nullable): a #GCancellable
908 * @callback: (nullable): a #GASyncReadyCallback to call when the request is done
909 * @user_data: (nullable): data to pass to @callback
911 * Async version of g_app_info_launch_default_for_uri().
913 * This version is useful if you are interested in receiving
914 * error information in the case where the application is
915 * sandboxed and the portal may present an application chooser
916 * dialog to the user.
918 * Since: 2.50
920 void
921 g_app_info_launch_default_for_uri_async (const char *uri,
922 GAppLaunchContext *context,
923 GCancellable *cancellable,
924 GAsyncReadyCallback callback,
925 gpointer user_data)
927 gboolean res;
928 GError *error = NULL;
929 GTask *task;
931 #ifdef G_OS_UNIX
932 if (glib_should_use_portal ())
934 launch_default_with_portal_async (uri, context, cancellable, callback, user_data);
935 return;
937 #endif
939 res = launch_default_for_uri (uri, context, &error);
941 task = g_task_new (context, cancellable, callback, user_data);
942 if (!res)
943 g_task_return_error (task, error);
944 else
945 g_task_return_boolean (task, TRUE);
947 g_object_unref (task);
951 * g_app_info_launch_default_for_uri_finish:
952 * @result: a #GAsyncResult
953 * @error: (nullable): return location for an error, or %NULL
955 * Finishes an asynchronous launch-default-for-uri operation.
957 * Returns: %TRUE if the launch was successful, %FALSE if @error is set
959 * Since: 2.50
961 gboolean
962 g_app_info_launch_default_for_uri_finish (GAsyncResult *result,
963 GError **error)
965 return g_task_propagate_boolean (G_TASK (result), error);
969 * g_app_info_can_delete:
970 * @appinfo: a #GAppInfo
972 * Obtains the information whether the #GAppInfo can be deleted.
973 * See g_app_info_delete().
975 * Returns: %TRUE if @appinfo can be deleted
977 * Since: 2.20
979 gboolean
980 g_app_info_can_delete (GAppInfo *appinfo)
982 GAppInfoIface *iface;
984 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
986 iface = G_APP_INFO_GET_IFACE (appinfo);
988 if (iface->can_delete)
989 return (* iface->can_delete) (appinfo);
991 return FALSE;
996 * g_app_info_delete:
997 * @appinfo: a #GAppInfo
999 * Tries to delete a #GAppInfo.
1001 * On some platforms, there may be a difference between user-defined
1002 * #GAppInfos which can be deleted, and system-wide ones which cannot.
1003 * See g_app_info_can_delete().
1005 * Virtual: do_delete
1006 * Returns: %TRUE if @appinfo has been deleted
1008 * Since: 2.20
1010 gboolean
1011 g_app_info_delete (GAppInfo *appinfo)
1013 GAppInfoIface *iface;
1015 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
1017 iface = G_APP_INFO_GET_IFACE (appinfo);
1019 if (iface->do_delete)
1020 return (* iface->do_delete) (appinfo);
1022 return FALSE;
1026 enum {
1027 LAUNCH_FAILED,
1028 LAUNCHED,
1029 LAST_SIGNAL
1032 static guint signals[LAST_SIGNAL] = { 0 };
1034 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
1037 * g_app_launch_context_new:
1039 * Creates a new application launch context. This is not normally used,
1040 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
1042 * Returns: a #GAppLaunchContext.
1044 GAppLaunchContext *
1045 g_app_launch_context_new (void)
1047 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
1050 static void
1051 g_app_launch_context_finalize (GObject *object)
1053 GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
1055 g_strfreev (context->priv->envp);
1057 G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
1060 static void
1061 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
1063 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1065 object_class->finalize = g_app_launch_context_finalize;
1068 * GAppLaunchContext::launch-failed:
1069 * @context: the object emitting the signal
1070 * @startup_notify_id: the startup notification id for the failed launch
1072 * The ::launch-failed signal is emitted when a #GAppInfo launch
1073 * fails. The startup notification id is provided, so that the launcher
1074 * can cancel the startup notification.
1076 * Since: 2.36
1078 signals[LAUNCH_FAILED] = g_signal_new (I_("launch-failed"),
1079 G_OBJECT_CLASS_TYPE (object_class),
1080 G_SIGNAL_RUN_LAST,
1081 G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
1082 NULL, NULL, NULL,
1083 G_TYPE_NONE, 1, G_TYPE_STRING);
1086 * GAppLaunchContext::launched:
1087 * @context: the object emitting the signal
1088 * @info: the #GAppInfo that was just launched
1089 * @platform_data: additional platform-specific data for this launch
1091 * The ::launched signal is emitted when a #GAppInfo is successfully
1092 * launched. The @platform_data is an GVariant dictionary mapping
1093 * strings to variants (ie a{sv}), which contains additional,
1094 * platform-specific data about this launch. On UNIX, at least the
1095 * "pid" and "startup-notification-id" keys will be present.
1097 * Since: 2.36
1099 signals[LAUNCHED] = g_signal_new (I_("launched"),
1100 G_OBJECT_CLASS_TYPE (object_class),
1101 G_SIGNAL_RUN_LAST,
1102 G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
1103 NULL, NULL, NULL,
1104 G_TYPE_NONE, 2,
1105 G_TYPE_APP_INFO, G_TYPE_VARIANT);
1108 static void
1109 g_app_launch_context_init (GAppLaunchContext *context)
1111 context->priv = g_app_launch_context_get_instance_private (context);
1115 * g_app_launch_context_setenv:
1116 * @context: a #GAppLaunchContext
1117 * @variable: the environment variable to set
1118 * @value: the value for to set the variable to.
1120 * Arranges for @variable to be set to @value in the child's
1121 * environment when @context is used to launch an application.
1123 * Since: 2.32
1125 void
1126 g_app_launch_context_setenv (GAppLaunchContext *context,
1127 const char *variable,
1128 const char *value)
1130 if (!context->priv->envp)
1131 context->priv->envp = g_get_environ ();
1133 context->priv->envp =
1134 g_environ_setenv (context->priv->envp, variable, value, TRUE);
1138 * g_app_launch_context_unsetenv:
1139 * @context: a #GAppLaunchContext
1140 * @variable: the environment variable to remove
1142 * Arranges for @variable to be unset in the child's environment
1143 * when @context is used to launch an application.
1145 * Since: 2.32
1147 void
1148 g_app_launch_context_unsetenv (GAppLaunchContext *context,
1149 const char *variable)
1151 if (!context->priv->envp)
1152 context->priv->envp = g_get_environ ();
1154 context->priv->envp =
1155 g_environ_unsetenv (context->priv->envp, variable);
1159 * g_app_launch_context_get_environment:
1160 * @context: a #GAppLaunchContext
1162 * Gets the complete environment variable list to be passed to
1163 * the child process when @context is used to launch an application.
1164 * This is a %NULL-terminated array of strings, where each string has
1165 * the form `KEY=VALUE`.
1167 * Returns: (array zero-terminated=1) (transfer full): the
1168 * child's environment
1170 * Since: 2.32
1172 char **
1173 g_app_launch_context_get_environment (GAppLaunchContext *context)
1175 if (!context->priv->envp)
1176 context->priv->envp = g_get_environ ();
1178 return g_strdupv (context->priv->envp);
1182 * g_app_launch_context_get_display:
1183 * @context: a #GAppLaunchContext
1184 * @info: a #GAppInfo
1185 * @files: (element-type GFile): a #GList of #GFile objects
1187 * Gets the display string for the @context. This is used to ensure new
1188 * applications are started on the same display as the launching
1189 * application, by setting the `DISPLAY` environment variable.
1191 * Returns: a display string for the display.
1193 char *
1194 g_app_launch_context_get_display (GAppLaunchContext *context,
1195 GAppInfo *info,
1196 GList *files)
1198 GAppLaunchContextClass *class;
1200 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
1201 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
1203 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1205 if (class->get_display == NULL)
1206 return NULL;
1208 return class->get_display (context, info, files);
1212 * g_app_launch_context_get_startup_notify_id:
1213 * @context: a #GAppLaunchContext
1214 * @info: a #GAppInfo
1215 * @files: (element-type GFile): a #GList of of #GFile objects
1217 * Initiates startup notification for the application and returns the
1218 * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
1220 * Startup notification IDs are defined in the
1221 * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
1223 * Returns: a startup notification ID for the application, or %NULL if
1224 * not supported.
1226 char *
1227 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
1228 GAppInfo *info,
1229 GList *files)
1231 GAppLaunchContextClass *class;
1233 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
1234 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
1236 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1238 if (class->get_startup_notify_id == NULL)
1239 return NULL;
1241 return class->get_startup_notify_id (context, info, files);
1246 * g_app_launch_context_launch_failed:
1247 * @context: a #GAppLaunchContext.
1248 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1250 * Called when an application has failed to launch, so that it can cancel
1251 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1254 void
1255 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1256 const char *startup_notify_id)
1258 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1259 g_return_if_fail (startup_notify_id != NULL);
1261 g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1266 * SECTION:gappinfomonitor
1267 * @short_description: Monitor application information for changes
1269 * #GAppInfoMonitor is a very simple object used for monitoring the app
1270 * info database for changes (ie: newly installed or removed
1271 * applications).
1273 * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1274 * to the "changed" signal.
1276 * In the usual case, applications should try to make note of the change
1277 * (doing things like invalidating caches) but not act on it. In
1278 * particular, applications should avoid making calls to #GAppInfo APIs
1279 * in response to the change signal, deferring these until the time that
1280 * the data is actually required. The exception to this case is when
1281 * application information is actually being displayed on the screen
1282 * (eg: during a search or when the list of all applications is shown).
1283 * The reason for this is that changes to the list of installed
1284 * applications often come in groups (like during system updates) and
1285 * rescanning the list on every change is pointless and expensive.
1287 * Since: 2.40
1291 * GAppInfoMonitor:
1293 * The only thing you can do with this is to get it via
1294 * g_app_info_monitor_get() and connect to the "changed" signal.
1296 * Since: 2.40
1299 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1301 struct _GAppInfoMonitor
1303 GObject parent_instance;
1304 GMainContext *context;
1307 struct _GAppInfoMonitorClass
1309 GObjectClass parent_class;
1312 static GContextSpecificGroup g_app_info_monitor_group;
1313 static guint g_app_info_monitor_changed_signal;
1315 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1317 static void
1318 g_app_info_monitor_finalize (GObject *object)
1320 GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1322 g_context_specific_group_remove (&g_app_info_monitor_group, monitor->context, monitor, NULL);
1324 G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1327 static void
1328 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1332 static void
1333 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1335 GObjectClass *object_class = G_OBJECT_CLASS (class);
1338 * GAppInfoMonitor::changed:
1340 * Signal emitted when the app info database for changes (ie: newly installed
1341 * or removed applications).
1343 g_app_info_monitor_changed_signal = g_signal_new (I_("changed"), G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1344 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1346 object_class->finalize = g_app_info_monitor_finalize;
1350 * g_app_info_monitor_get:
1352 * Gets the #GAppInfoMonitor for the current thread-default main
1353 * context.
1355 * The #GAppInfoMonitor will emit a "changed" signal in the
1356 * thread-default main context whenever the list of installed
1357 * applications (as reported by g_app_info_get_all()) may have changed.
1359 * You must only call g_object_unref() on the return value from under
1360 * the same main context as you created it.
1362 * Returns: (transfer full): a reference to a #GAppInfoMonitor
1364 * Since: 2.40
1366 GAppInfoMonitor *
1367 g_app_info_monitor_get (void)
1369 return g_context_specific_group_get (&g_app_info_monitor_group,
1370 G_TYPE_APP_INFO_MONITOR,
1371 G_STRUCT_OFFSET (GAppInfoMonitor, context),
1372 NULL);
1375 void
1376 g_app_info_monitor_fire (void)
1378 g_context_specific_group_emit (&g_app_info_monitor_group, g_app_info_monitor_changed_signal);