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>
24 #include "gappinfoprivate.h"
33 * @short_description: Application information and launch contexts
35 * @see_also: #GAppInfoMonitor
37 * #GAppInfo and #GAppLaunchContext are used for describing and launching
38 * applications installed on the system.
40 * As of GLib 2.20, URIs will always be converted to POSIX paths
41 * (using g_file_get_path()) when using g_app_info_launch() even if
42 * the application requested an URI and not a POSIX path. For example
43 * for an desktop-file based application with Exec key `totem
44 * %U` and a single URI, `sftp://foo/file.avi`, then
45 * `/home/user/.gvfs/sftp on foo/file.avi` will be passed. This will
46 * only work if a set of suitable GIO extensions (such as gvfs 2.26
47 * compiled with FUSE support), is available and operational; if this
48 * is not the case, the URI will be passed unmodified to the application.
49 * Some URIs, such as `mailto:`, of course cannot be mapped to a POSIX
50 * path (in gvfs there's no FUSE mount for it); such URIs will be
51 * passed unmodified to the application.
53 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
54 * back to the GIO URI in the #GFile constructors (since gvfs
55 * implements the #GVfs extension point). As such, if the application
56 * needs to examine the URI, it needs to use g_file_get_uri() or
57 * similar on #GFile. In other words, an application cannot assume
58 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
59 * equal to the result of g_file_get_uri(). The following snippet
66 * file = g_file_new_for_commandline_arg (uri_from_commandline);
68 * uri = g_file_get_uri (file);
69 * strcmp (uri, uri_from_commandline) == 0;
72 * if (g_file_has_uri_scheme (file, "cdda"))
74 * // do something special with uri
76 * g_object_unref (file);
79 * This code will work when both `cdda://sr0/Track 1.wav` and
80 * `/home/user/.gvfs/cdda on sr0/Track 1.wav` is passed to the
81 * application. It should be noted that it's generally not safe
82 * for applications to rely on the format of a particular URIs.
83 * Different launcher applications (e.g. file managers) may have
84 * different ideas of what a given URI means.
87 typedef GAppInfoIface GAppInfoInterface;
88 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
91 g_app_info_default_init (GAppInfoInterface *iface)
98 * @appinfo: a #GAppInfo.
100 * Creates a duplicate of a #GAppInfo.
102 * Returns: (transfer full): a duplicate of @appinfo.
105 g_app_info_dup (GAppInfo *appinfo)
107 GAppInfoIface *iface;
109 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
111 iface = G_APP_INFO_GET_IFACE (appinfo);
113 return (* iface->dup) (appinfo);
118 * @appinfo1: the first #GAppInfo.
119 * @appinfo2: the second #GAppInfo.
121 * Checks if two #GAppInfos are equal.
123 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
126 g_app_info_equal (GAppInfo *appinfo1,
129 GAppInfoIface *iface;
131 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
132 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
134 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
137 iface = G_APP_INFO_GET_IFACE (appinfo1);
139 return (* iface->equal) (appinfo1, appinfo2);
144 * @appinfo: a #GAppInfo.
146 * Gets the ID of an application. An id is a string that
147 * identifies the application. The exact format of the id is
148 * platform dependent. For instance, on Unix this is the
149 * desktop file id from the xdg menu specification.
151 * Note that the returned ID may be %NULL, depending on how
152 * the @appinfo has been constructed.
154 * Returns: a string containing the application's ID.
157 g_app_info_get_id (GAppInfo *appinfo)
159 GAppInfoIface *iface;
161 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
163 iface = G_APP_INFO_GET_IFACE (appinfo);
165 return (* iface->get_id) (appinfo);
169 * g_app_info_get_name:
170 * @appinfo: a #GAppInfo.
172 * Gets the installed name of the application.
174 * Returns: the name of the application for @appinfo.
177 g_app_info_get_name (GAppInfo *appinfo)
179 GAppInfoIface *iface;
181 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
183 iface = G_APP_INFO_GET_IFACE (appinfo);
185 return (* iface->get_name) (appinfo);
189 * g_app_info_get_display_name:
190 * @appinfo: a #GAppInfo.
192 * Gets the display name of the application. The display name is often more
193 * descriptive to the user than the name itself.
195 * Returns: the display name of the application for @appinfo, or the name if
196 * no display name is available.
201 g_app_info_get_display_name (GAppInfo *appinfo)
203 GAppInfoIface *iface;
205 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
207 iface = G_APP_INFO_GET_IFACE (appinfo);
209 if (iface->get_display_name == NULL)
210 return (* iface->get_name) (appinfo);
212 return (* iface->get_display_name) (appinfo);
216 * g_app_info_get_description:
217 * @appinfo: a #GAppInfo.
219 * Gets a human-readable description of an installed application.
221 * Returns: a string containing a description of the
222 * application @appinfo, or %NULL if none.
225 g_app_info_get_description (GAppInfo *appinfo)
227 GAppInfoIface *iface;
229 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
231 iface = G_APP_INFO_GET_IFACE (appinfo);
233 return (* iface->get_description) (appinfo);
237 * g_app_info_get_executable:
238 * @appinfo: a #GAppInfo
240 * Gets the executable's name for the installed application.
242 * Returns: a string containing the @appinfo's application
246 g_app_info_get_executable (GAppInfo *appinfo)
248 GAppInfoIface *iface;
250 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
252 iface = G_APP_INFO_GET_IFACE (appinfo);
254 return (* iface->get_executable) (appinfo);
259 * g_app_info_get_commandline:
260 * @appinfo: a #GAppInfo
262 * Gets the commandline with which the application will be
265 * Returns: a string containing the @appinfo's commandline,
266 * or %NULL if this information is not available
271 g_app_info_get_commandline (GAppInfo *appinfo)
273 GAppInfoIface *iface;
275 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
277 iface = G_APP_INFO_GET_IFACE (appinfo);
279 if (iface->get_commandline)
280 return (* iface->get_commandline) (appinfo);
286 * g_app_info_set_as_default_for_type:
287 * @appinfo: a #GAppInfo.
288 * @content_type: the content type.
291 * Sets the application as the default handler for a given type.
293 * Returns: %TRUE on success, %FALSE on error.
296 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
297 const char *content_type,
300 GAppInfoIface *iface;
302 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
303 g_return_val_if_fail (content_type != NULL, FALSE);
305 iface = G_APP_INFO_GET_IFACE (appinfo);
307 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
311 * g_app_info_set_as_last_used_for_type:
312 * @appinfo: a #GAppInfo.
313 * @content_type: the content type.
316 * Sets the application as the last used application for a given type.
317 * This will make the application appear as first in the list returned
318 * by g_app_info_get_recommended_for_type(), regardless of the default
319 * application for that content type.
321 * Returns: %TRUE on success, %FALSE on error.
324 g_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
325 const char *content_type,
328 GAppInfoIface *iface;
330 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
331 g_return_val_if_fail (content_type != NULL, FALSE);
333 iface = G_APP_INFO_GET_IFACE (appinfo);
335 return (* iface->set_as_last_used_for_type) (appinfo, content_type, error);
339 * g_app_info_set_as_default_for_extension:
340 * @appinfo: a #GAppInfo.
341 * @extension: a string containing the file extension (without the dot).
344 * Sets the application as the default handler for the given file extension.
346 * Returns: %TRUE on success, %FALSE on error.
349 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
350 const char *extension,
353 GAppInfoIface *iface;
355 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
356 g_return_val_if_fail (extension != NULL, FALSE);
358 iface = G_APP_INFO_GET_IFACE (appinfo);
360 if (iface->set_as_default_for_extension)
361 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
363 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
364 "g_app_info_set_as_default_for_extension not supported yet");
370 * g_app_info_add_supports_type:
371 * @appinfo: a #GAppInfo.
372 * @content_type: a string.
375 * Adds a content type to the application information to indicate the
376 * application is capable of opening files with the given content type.
378 * Returns: %TRUE on success, %FALSE on error.
381 g_app_info_add_supports_type (GAppInfo *appinfo,
382 const char *content_type,
385 GAppInfoIface *iface;
387 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
388 g_return_val_if_fail (content_type != NULL, FALSE);
390 iface = G_APP_INFO_GET_IFACE (appinfo);
392 if (iface->add_supports_type)
393 return (* iface->add_supports_type) (appinfo, content_type, error);
395 g_set_error_literal (error, G_IO_ERROR,
396 G_IO_ERROR_NOT_SUPPORTED,
397 "g_app_info_add_supports_type not supported yet");
404 * g_app_info_can_remove_supports_type:
405 * @appinfo: a #GAppInfo.
407 * Checks if a supported content type can be removed from an application.
409 * Returns: %TRUE if it is possible to remove supported
410 * content types from a given @appinfo, %FALSE if not.
413 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
415 GAppInfoIface *iface;
417 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
419 iface = G_APP_INFO_GET_IFACE (appinfo);
421 if (iface->can_remove_supports_type)
422 return (* iface->can_remove_supports_type) (appinfo);
429 * g_app_info_remove_supports_type:
430 * @appinfo: a #GAppInfo.
431 * @content_type: a string.
434 * Removes a supported type from an application, if possible.
436 * Returns: %TRUE on success, %FALSE on error.
439 g_app_info_remove_supports_type (GAppInfo *appinfo,
440 const char *content_type,
443 GAppInfoIface *iface;
445 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
446 g_return_val_if_fail (content_type != NULL, FALSE);
448 iface = G_APP_INFO_GET_IFACE (appinfo);
450 if (iface->remove_supports_type)
451 return (* iface->remove_supports_type) (appinfo, content_type, error);
453 g_set_error_literal (error, G_IO_ERROR,
454 G_IO_ERROR_NOT_SUPPORTED,
455 "g_app_info_remove_supports_type not supported yet");
461 * g_app_info_get_supported_types:
462 * @appinfo: a #GAppInfo that can handle files
464 * Retrieves the list of content types that @app_info claims to support.
465 * If this information is not provided by the environment, this function
467 * This function does not take in consideration associations added with
468 * g_app_info_add_supports_type(), but only those exported directly by
471 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
472 * a list of content types.
477 g_app_info_get_supported_types (GAppInfo *appinfo)
479 GAppInfoIface *iface;
481 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
483 iface = G_APP_INFO_GET_IFACE (appinfo);
485 if (iface->get_supported_types)
486 return iface->get_supported_types (appinfo);
493 * g_app_info_get_icon:
494 * @appinfo: a #GAppInfo.
496 * Gets the icon for the application.
498 * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
499 * if there is no default icon.
502 g_app_info_get_icon (GAppInfo *appinfo)
504 GAppInfoIface *iface;
506 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
508 iface = G_APP_INFO_GET_IFACE (appinfo);
510 return (* iface->get_icon) (appinfo);
516 * @appinfo: a #GAppInfo
517 * @files: (allow-none) (element-type GFile): a #GList of #GFile objects
518 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
521 * Launches the application. Passes @files to the launched application
522 * as arguments, using the optional @launch_context to get information
523 * about the details of the launcher (like what screen it is on).
524 * On error, @error will be set accordingly.
526 * To launch the application without arguments pass a %NULL @files list.
528 * Note that even if the launch is successful the application launched
529 * can fail to start if it runs into problems during startup. There is
530 * no way to detect this.
532 * Some URIs can be changed when passed through a GFile (for instance
533 * unsupported URIs with strange formats like mailto:), so if you have
534 * a textual URI you want to pass in as argument, consider using
535 * g_app_info_launch_uris() instead.
537 * The launched application inherits the environment of the launching
538 * process, but it can be modified with g_app_launch_context_setenv()
539 * and g_app_launch_context_unsetenv().
541 * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
542 * environment variable with the path of the launched desktop file and
543 * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
544 * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
545 * should it be inherited by further processes. The `DISPLAY` and
546 * `DESKTOP_STARTUP_ID` environment variables are also set, based
547 * on information provided in @launch_context.
549 * Returns: %TRUE on successful launch, %FALSE otherwise.
552 g_app_info_launch (GAppInfo *appinfo,
554 GAppLaunchContext *launch_context,
557 GAppInfoIface *iface;
559 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
561 iface = G_APP_INFO_GET_IFACE (appinfo);
563 return (* iface->launch) (appinfo, files, launch_context, error);
568 * g_app_info_supports_uris:
569 * @appinfo: a #GAppInfo.
571 * Checks if the application supports reading files and directories from URIs.
573 * Returns: %TRUE if the @appinfo supports URIs.
576 g_app_info_supports_uris (GAppInfo *appinfo)
578 GAppInfoIface *iface;
580 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
582 iface = G_APP_INFO_GET_IFACE (appinfo);
584 return (* iface->supports_uris) (appinfo);
589 * g_app_info_supports_files:
590 * @appinfo: a #GAppInfo.
592 * Checks if the application accepts files as arguments.
594 * Returns: %TRUE if the @appinfo supports files.
597 g_app_info_supports_files (GAppInfo *appinfo)
599 GAppInfoIface *iface;
601 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
603 iface = G_APP_INFO_GET_IFACE (appinfo);
605 return (* iface->supports_files) (appinfo);
610 * g_app_info_launch_uris:
611 * @appinfo: a #GAppInfo
612 * @uris: (allow-none) (element-type utf8): a #GList containing URIs to launch.
613 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
616 * Launches the application. This passes the @uris to the launched application
617 * as arguments, using the optional @launch_context to get information
618 * about the details of the launcher (like what screen it is on).
619 * On error, @error will be set accordingly.
621 * To launch the application without arguments pass a %NULL @uris list.
623 * Note that even if the launch is successful the application launched
624 * can fail to start if it runs into problems during startup. There is
625 * no way to detect this.
627 * Returns: %TRUE on successful launch, %FALSE otherwise.
630 g_app_info_launch_uris (GAppInfo *appinfo,
632 GAppLaunchContext *launch_context,
635 GAppInfoIface *iface;
637 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
639 iface = G_APP_INFO_GET_IFACE (appinfo);
641 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
646 * g_app_info_should_show:
647 * @appinfo: a #GAppInfo.
649 * Checks if the application info should be shown in menus that
650 * list available applications.
652 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
655 g_app_info_should_show (GAppInfo *appinfo)
657 GAppInfoIface *iface;
659 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
661 iface = G_APP_INFO_GET_IFACE (appinfo);
663 return (* iface->should_show) (appinfo);
667 * g_app_info_launch_default_for_uri:
668 * @uri: the uri to show
669 * @launch_context: (allow-none): an optional #GAppLaunchContext.
672 * Utility function that launches the default application
673 * registered to handle the specified uri. Synchronous I/O
674 * is done on the uri to detect the type of the file if
677 * Returns: %TRUE on success, %FALSE on error.
680 g_app_info_launch_default_for_uri (const char *uri,
681 GAppLaunchContext *launch_context,
685 GAppInfo *app_info = NULL;
689 /* g_file_query_default_handler() calls
690 * g_app_info_get_default_for_uri_scheme() too, but we have to do it
691 * here anyway in case GFile can't parse @uri correctly.
693 uri_scheme = g_uri_parse_scheme (uri);
694 if (uri_scheme && uri_scheme[0] != '\0')
695 app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
702 file = g_file_new_for_uri (uri);
703 app_info = g_file_query_default_handler (file, NULL, error);
704 g_object_unref (file);
705 if (app_info == NULL)
708 /* We still use the original @uri rather than calling
709 * g_file_get_uri(), because GFile might have modified the URI
710 * in ways we don't want (eg, removing the fragment identifier
715 l.data = (char *)uri;
716 l.next = l.prev = NULL;
717 res = g_app_info_launch_uris (app_info, &l,
718 launch_context, error);
720 g_object_unref (app_info);
726 * g_app_info_can_delete:
727 * @appinfo: a #GAppInfo
729 * Obtains the information whether the #GAppInfo can be deleted.
730 * See g_app_info_delete().
732 * Returns: %TRUE if @appinfo can be deleted
737 g_app_info_can_delete (GAppInfo *appinfo)
739 GAppInfoIface *iface;
741 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
743 iface = G_APP_INFO_GET_IFACE (appinfo);
745 if (iface->can_delete)
746 return (* iface->can_delete) (appinfo);
754 * @appinfo: a #GAppInfo
756 * Tries to delete a #GAppInfo.
758 * On some platforms, there may be a difference between user-defined
759 * #GAppInfos which can be deleted, and system-wide ones which cannot.
760 * See g_app_info_can_delete().
763 * Returns: %TRUE if @appinfo has been deleted
768 g_app_info_delete (GAppInfo *appinfo)
770 GAppInfoIface *iface;
772 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
774 iface = G_APP_INFO_GET_IFACE (appinfo);
776 if (iface->do_delete)
777 return (* iface->do_delete) (appinfo);
789 struct _GAppLaunchContextPrivate {
793 static guint signals[LAST_SIGNAL] = { 0 };
795 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
798 * g_app_launch_context_new:
800 * Creates a new application launch context. This is not normally used,
801 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
803 * Returns: a #GAppLaunchContext.
806 g_app_launch_context_new (void)
808 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
812 g_app_launch_context_finalize (GObject *object)
814 GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
816 g_strfreev (context->priv->envp);
818 G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
822 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
824 GObjectClass *object_class = G_OBJECT_CLASS (klass);
826 object_class->finalize = g_app_launch_context_finalize;
829 * GAppLaunchContext::launch-failed:
830 * @context: the object emitting the signal
831 * @startup_notify_id: the startup notification id for the failed launch
833 * The ::launch-failed signal is emitted when a #GAppInfo launch
834 * fails. The startup notification id is provided, so that the launcher
835 * can cancel the startup notification.
839 signals[LAUNCH_FAILED] = g_signal_new ("launch-failed",
840 G_OBJECT_CLASS_TYPE (object_class),
842 G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
844 G_TYPE_NONE, 1, G_TYPE_STRING);
847 * GAppLaunchContext::launched:
848 * @context: the object emitting the signal
849 * @info: the #GAppInfo that was just launched
850 * @platform_data: additional platform-specific data for this launch
852 * The ::launched signal is emitted when a #GAppInfo is successfully
853 * launched. The @platform_data is an GVariant dictionary mapping
854 * strings to variants (ie a{sv}), which contains additional,
855 * platform-specific data about this launch. On UNIX, at least the
856 * "pid" and "startup-notification-id" keys will be present.
860 signals[LAUNCHED] = g_signal_new ("launched",
861 G_OBJECT_CLASS_TYPE (object_class),
863 G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
866 G_TYPE_APP_INFO, G_TYPE_VARIANT);
870 g_app_launch_context_init (GAppLaunchContext *context)
872 context->priv = g_app_launch_context_get_instance_private (context);
876 * g_app_launch_context_setenv:
877 * @context: a #GAppLaunchContext
878 * @variable: the environment variable to set
879 * @value: the value for to set the variable to.
881 * Arranges for @variable to be set to @value in the child's
882 * environment when @context is used to launch an application.
887 g_app_launch_context_setenv (GAppLaunchContext *context,
888 const char *variable,
891 if (!context->priv->envp)
892 context->priv->envp = g_get_environ ();
894 context->priv->envp =
895 g_environ_setenv (context->priv->envp, variable, value, TRUE);
899 * g_app_launch_context_unsetenv:
900 * @context: a #GAppLaunchContext
901 * @variable: the environment variable to remove
903 * Arranges for @variable to be unset in the child's environment
904 * when @context is used to launch an application.
909 g_app_launch_context_unsetenv (GAppLaunchContext *context,
910 const char *variable)
912 if (!context->priv->envp)
913 context->priv->envp = g_get_environ ();
915 context->priv->envp =
916 g_environ_unsetenv (context->priv->envp, variable);
920 * g_app_launch_context_get_environment:
921 * @context: a #GAppLaunchContext
923 * Gets the complete environment variable list to be passed to
924 * the child process when @context is used to launch an application.
925 * This is a %NULL-terminated array of strings, where each string has
926 * the form `KEY=VALUE`.
928 * Returns: (array zero-terminated=1) (transfer full): the
929 * child's environment
934 g_app_launch_context_get_environment (GAppLaunchContext *context)
936 if (!context->priv->envp)
937 context->priv->envp = g_get_environ ();
939 return g_strdupv (context->priv->envp);
943 * g_app_launch_context_get_display:
944 * @context: a #GAppLaunchContext
946 * @files: (element-type GFile): a #GList of #GFile objects
948 * Gets the display string for the @context. This is used to ensure new
949 * applications are started on the same display as the launching
950 * application, by setting the `DISPLAY` environment variable.
952 * Returns: a display string for the display.
955 g_app_launch_context_get_display (GAppLaunchContext *context,
959 GAppLaunchContextClass *class;
961 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
962 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
964 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
966 if (class->get_display == NULL)
969 return class->get_display (context, info, files);
973 * g_app_launch_context_get_startup_notify_id:
974 * @context: a #GAppLaunchContext
976 * @files: (element-type GFile): a #GList of of #GFile objects
978 * Initiates startup notification for the application and returns the
979 * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
981 * Startup notification IDs are defined in the
982 * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
984 * Returns: a startup notification ID for the application, or %NULL if
988 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
992 GAppLaunchContextClass *class;
994 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
995 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
997 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
999 if (class->get_startup_notify_id == NULL)
1002 return class->get_startup_notify_id (context, info, files);
1007 * g_app_launch_context_launch_failed:
1008 * @context: a #GAppLaunchContext.
1009 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1011 * Called when an application has failed to launch, so that it can cancel
1012 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1016 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1017 const char *startup_notify_id)
1019 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1020 g_return_if_fail (startup_notify_id != NULL);
1022 g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1027 * SECTION:gappinfomonitor
1028 * @short_description: Monitor application information for changes
1030 * #GAppInfoMonitor is a very simple object used for monitoring the app
1031 * info database for changes (ie: newly installed or removed
1034 * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1035 * to the "changed" signal.
1037 * In the usual case, applications should try to make note of the change
1038 * (doing things like invalidating caches) but not act on it. In
1039 * particular, applications should avoid making calls to #GAppInfo APIs
1040 * in response to the change signal, deferring these until the time that
1041 * the data is actually required. The exception to this case is when
1042 * application information is actually being displayed on the screen
1043 * (eg: during a search or when the list of all applications is shown).
1044 * The reason for this is that changes to the list of installed
1045 * applications often come in groups (like during system updates) and
1046 * rescanning the list on every change is pointless and expensive.
1054 * The only thing you can do with this is to get it via
1055 * g_app_info_monitor_get() and connect to the "changed" signal.
1060 /* We have one of each of these per main context and hand them out
1061 * according to the thread default main context at the time of the call
1062 * to g_app_info_monitor_get().
1064 * g_object_unref() is only ever called from the same context, so we
1065 * effectively have a single-threaded scenario for each GAppInfoMonitor.
1067 * We use a hashtable to cache the per-context monitor (but we do not
1068 * hold a ref). During finalize, we remove it. This is possible
1069 * because we don't have to worry about the usual races due to the
1070 * single-threaded nature of each object.
1072 * We keep a global list of all contexts that have a monitor for them,
1073 * which we have to access under a lock. When we dispatch the events to
1074 * be handled in each context, we don't pass the monitor, but the
1077 * We dispatch from the GLib worker context, so if we passed the
1078 * monitor, we would need to take a ref on it (in case it was destroyed
1079 * in its own thread meanwhile). The monitor holds a ref on a context
1080 * and the dispatch would mean that the context would hold a ref on the
1081 * monitor. If someone stopped iterating the context at just this
1082 * moment both the context and monitor would leak.
1084 * Instead, we dispatch the context to itself. We don't hold a ref.
1085 * There is the danger that the context will be destroyed during the
1086 * dispatch, but if that is the case then we just won't receive our
1089 * When the dispatch occurs we just lookup the monitor in the hashtable,
1090 * by context. We can now add and remove refs, since the context will
1091 * have been acquired.
1094 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1096 struct _GAppInfoMonitor
1098 GObject parent_instance;
1099 GMainContext *context;
1102 struct _GAppInfoMonitorClass
1104 GObjectClass parent_class;
1107 static GHashTable *g_app_info_monitors;
1108 static GMutex g_app_info_monitor_lock;
1109 static guint g_app_info_monitor_changed_signal;
1111 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1114 g_app_info_monitor_finalize (GObject *object)
1116 GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1118 g_mutex_lock (&g_app_info_monitor_lock);
1119 g_hash_table_remove (g_app_info_monitors, monitor->context);
1120 g_mutex_unlock (&g_app_info_monitor_lock);
1122 G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1126 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1131 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1133 GObjectClass *object_class = G_OBJECT_CLASS (class);
1135 g_app_info_monitor_changed_signal = g_signal_new ("changed", G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1136 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1138 object_class->finalize = g_app_info_monitor_finalize;
1142 * g_app_info_monitor_get:
1144 * Gets the #GAppInfoMonitor for the current thread-default main
1147 * The #GAppInfoMonitor will emit a "changed" signal in the
1148 * thread-default main context whenever the list of installed
1149 * applications (as reported by g_app_info_get_all()) may have changed.
1151 * You must only call g_object_unref() on the return value from under
1152 * the same main context as you created it.
1154 * Returns: (transfer full): a reference to a #GAppInfoMonitor
1159 g_app_info_monitor_get (void)
1161 GAppInfoMonitor *monitor;
1162 GMainContext *context;
1164 context = g_main_context_get_thread_default ();
1166 context = g_main_context_default ();
1168 g_return_val_if_fail (g_main_context_acquire (context), NULL);
1170 g_mutex_lock (&g_app_info_monitor_lock);
1171 if (!g_app_info_monitors)
1172 g_app_info_monitors = g_hash_table_new (NULL, NULL);
1174 monitor = g_hash_table_lookup (g_app_info_monitors, context);
1175 g_mutex_unlock (&g_app_info_monitor_lock);
1179 monitor = g_object_new (G_TYPE_APP_INFO_MONITOR, NULL);
1180 monitor->context = g_main_context_ref (context);
1182 g_mutex_lock (&g_app_info_monitor_lock);
1183 g_hash_table_insert (g_app_info_monitors, context, monitor);
1184 g_mutex_unlock (&g_app_info_monitor_lock);
1187 g_object_ref (monitor);
1189 g_main_context_release (context);
1195 g_app_info_monitor_emit (gpointer user_data)
1197 GMainContext *context = user_data;
1198 GAppInfoMonitor *monitor;
1200 g_mutex_lock (&g_app_info_monitor_lock);
1201 monitor = g_hash_table_lookup (g_app_info_monitors, context);
1202 g_mutex_unlock (&g_app_info_monitor_lock);
1204 /* It is possible that the monitor was already destroyed by the time
1205 * we get here, so make sure it's not NULL.
1207 if (monitor != NULL)
1209 /* We don't have to worry about another thread disposing the
1210 * monitor but we do have to worry about the possibility that one
1211 * of the attached handlers may do so.
1213 * Take a ref so that the monitor doesn't disappear in the middle
1216 g_object_ref (monitor);
1217 g_signal_emit (monitor, g_app_info_monitor_changed_signal, 0);
1218 g_object_unref (monitor);
1225 g_app_info_monitor_fire (void)
1227 GHashTableIter iter;
1230 g_mutex_lock (&g_app_info_monitor_lock);
1232 if (g_app_info_monitors)
1234 g_hash_table_iter_init (&iter, g_app_info_monitors);
1235 while (g_hash_table_iter_next (&iter, &context, NULL))
1239 idle = g_idle_source_new ();
1240 g_source_set_callback (idle, g_app_info_monitor_emit, context, NULL);
1241 g_source_set_name (idle, "[gio] g_app_info_monitor_emit");
1242 g_source_attach (idle, context);
1243 g_source_unref (idle);
1247 g_mutex_unlock (&g_app_info_monitor_lock);