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 <literal>totem
44 * %U</literal> and a single URI,
45 * <literal>sftp://foo/file.avi</literal>, then
46 * <literal>/home/user/.gvfs/sftp on foo/file.avi</literal> will be
47 * passed. This will only work if a set of suitable GIO extensions
48 * (such as gvfs 2.26 compiled with FUSE support), is available and
49 * operational; if this is not the case, the URI will be passed
50 * unmodified to the application. Some URIs, such as
51 * <literal>mailto:</literal>, of course cannot be mapped to a POSIX
52 * path (in gvfs there's no FUSE mount for it); such URIs will be
53 * passed unmodified to the application.
55 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
56 * back to the GIO URI in the #GFile constructors (since gvfs
57 * implements the #GVfs extension point). As such, if the application
58 * needs to examine the URI, it needs to use g_file_get_uri() or
59 * similar on #GFile. In other words, an application cannot assume
60 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
61 * equal to the result of g_file_get_uri(). The following snippet
68 * file = g_file_new_for_commandline_arg (uri_from_commandline);
70 * uri = g_file_get_uri (file);
71 * strcmp (uri, uri_from_commandline) == 0; // FALSE
74 * if (g_file_has_uri_scheme (file, "cdda"))
76 * // do something special with uri
78 * g_object_unref (file);
81 * This code will work when both <literal>cdda://sr0/Track
82 * 1.wav</literal> and <literal>/home/user/.gvfs/cdda on sr0/Track
83 * 1.wav</literal> is passed to the application. It should be noted
84 * that it's generally not safe for applications to rely on the format
85 * of a particular URIs. Different launcher applications (e.g. file
86 * managers) may have different ideas of what a given URI means.
90 typedef GAppInfoIface GAppInfoInterface;
91 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
94 g_app_info_default_init (GAppInfoInterface *iface)
101 * @appinfo: a #GAppInfo.
103 * Creates a duplicate of a #GAppInfo.
105 * Returns: (transfer full): a duplicate of @appinfo.
108 g_app_info_dup (GAppInfo *appinfo)
110 GAppInfoIface *iface;
112 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
114 iface = G_APP_INFO_GET_IFACE (appinfo);
116 return (* iface->dup) (appinfo);
121 * @appinfo1: the first #GAppInfo.
122 * @appinfo2: the second #GAppInfo.
124 * Checks if two #GAppInfo<!-- -->s are equal.
126 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
129 g_app_info_equal (GAppInfo *appinfo1,
132 GAppInfoIface *iface;
134 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
135 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
137 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
140 iface = G_APP_INFO_GET_IFACE (appinfo1);
142 return (* iface->equal) (appinfo1, appinfo2);
147 * @appinfo: a #GAppInfo.
149 * Gets the ID of an application. An id is a string that
150 * identifies the application. The exact format of the id is
151 * platform dependent. For instance, on Unix this is the
152 * desktop file id from the xdg menu specification.
154 * Note that the returned ID may be %NULL, depending on how
155 * the @appinfo has been constructed.
157 * Returns: a string containing the application's ID.
160 g_app_info_get_id (GAppInfo *appinfo)
162 GAppInfoIface *iface;
164 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
166 iface = G_APP_INFO_GET_IFACE (appinfo);
168 return (* iface->get_id) (appinfo);
172 * g_app_info_get_name:
173 * @appinfo: a #GAppInfo.
175 * Gets the installed name of the application.
177 * Returns: the name of the application for @appinfo.
180 g_app_info_get_name (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_name) (appinfo);
192 * g_app_info_get_display_name:
193 * @appinfo: a #GAppInfo.
195 * Gets the display name of the application. The display name is often more
196 * descriptive to the user than the name itself.
198 * Returns: the display name of the application for @appinfo, or the name if
199 * no display name is available.
204 g_app_info_get_display_name (GAppInfo *appinfo)
206 GAppInfoIface *iface;
208 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
210 iface = G_APP_INFO_GET_IFACE (appinfo);
212 if (iface->get_display_name == NULL)
213 return (* iface->get_name) (appinfo);
215 return (* iface->get_display_name) (appinfo);
219 * g_app_info_get_description:
220 * @appinfo: a #GAppInfo.
222 * Gets a human-readable description of an installed application.
224 * Returns: a string containing a description of the
225 * application @appinfo, or %NULL if none.
228 g_app_info_get_description (GAppInfo *appinfo)
230 GAppInfoIface *iface;
232 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
234 iface = G_APP_INFO_GET_IFACE (appinfo);
236 return (* iface->get_description) (appinfo);
240 * g_app_info_get_executable:
241 * @appinfo: a #GAppInfo
243 * Gets the executable's name for the installed application.
245 * Returns: a string containing the @appinfo's application
249 g_app_info_get_executable (GAppInfo *appinfo)
251 GAppInfoIface *iface;
253 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
255 iface = G_APP_INFO_GET_IFACE (appinfo);
257 return (* iface->get_executable) (appinfo);
262 * g_app_info_get_commandline:
263 * @appinfo: a #GAppInfo
265 * Gets the commandline with which the application will be
268 * Returns: a string containing the @appinfo's commandline,
269 * or %NULL if this information is not available
274 g_app_info_get_commandline (GAppInfo *appinfo)
276 GAppInfoIface *iface;
278 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
280 iface = G_APP_INFO_GET_IFACE (appinfo);
282 if (iface->get_commandline)
283 return (* iface->get_commandline) (appinfo);
289 * g_app_info_set_as_default_for_type:
290 * @appinfo: a #GAppInfo.
291 * @content_type: the content type.
294 * Sets the application as the default handler for a given type.
296 * Returns: %TRUE on success, %FALSE on error.
299 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
300 const char *content_type,
303 GAppInfoIface *iface;
305 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
306 g_return_val_if_fail (content_type != NULL, FALSE);
308 iface = G_APP_INFO_GET_IFACE (appinfo);
310 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
314 * g_app_info_set_as_last_used_for_type:
315 * @appinfo: a #GAppInfo.
316 * @content_type: the content type.
319 * Sets the application as the last used application for a given type.
320 * This will make the application appear as first in the list returned
321 * by g_app_info_get_recommended_for_type(), regardless of the default
322 * application for that content type.
324 * Returns: %TRUE on success, %FALSE on error.
327 g_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
328 const char *content_type,
331 GAppInfoIface *iface;
333 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
334 g_return_val_if_fail (content_type != NULL, FALSE);
336 iface = G_APP_INFO_GET_IFACE (appinfo);
338 return (* iface->set_as_last_used_for_type) (appinfo, content_type, error);
342 * g_app_info_set_as_default_for_extension:
343 * @appinfo: a #GAppInfo.
344 * @extension: a string containing the file extension (without the dot).
347 * Sets the application as the default handler for the given file extension.
349 * Returns: %TRUE on success, %FALSE on error.
352 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
353 const char *extension,
356 GAppInfoIface *iface;
358 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
359 g_return_val_if_fail (extension != NULL, FALSE);
361 iface = G_APP_INFO_GET_IFACE (appinfo);
363 if (iface->set_as_default_for_extension)
364 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
366 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
367 "g_app_info_set_as_default_for_extension not supported yet");
373 * g_app_info_add_supports_type:
374 * @appinfo: a #GAppInfo.
375 * @content_type: a string.
378 * Adds a content type to the application information to indicate the
379 * application is capable of opening files with the given content type.
381 * Returns: %TRUE on success, %FALSE on error.
384 g_app_info_add_supports_type (GAppInfo *appinfo,
385 const char *content_type,
388 GAppInfoIface *iface;
390 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
391 g_return_val_if_fail (content_type != NULL, FALSE);
393 iface = G_APP_INFO_GET_IFACE (appinfo);
395 if (iface->add_supports_type)
396 return (* iface->add_supports_type) (appinfo, content_type, error);
398 g_set_error_literal (error, G_IO_ERROR,
399 G_IO_ERROR_NOT_SUPPORTED,
400 "g_app_info_add_supports_type not supported yet");
407 * g_app_info_can_remove_supports_type:
408 * @appinfo: a #GAppInfo.
410 * Checks if a supported content type can be removed from an application.
412 * Returns: %TRUE if it is possible to remove supported
413 * content types from a given @appinfo, %FALSE if not.
416 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
418 GAppInfoIface *iface;
420 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
422 iface = G_APP_INFO_GET_IFACE (appinfo);
424 if (iface->can_remove_supports_type)
425 return (* iface->can_remove_supports_type) (appinfo);
432 * g_app_info_remove_supports_type:
433 * @appinfo: a #GAppInfo.
434 * @content_type: a string.
437 * Removes a supported type from an application, if possible.
439 * Returns: %TRUE on success, %FALSE on error.
442 g_app_info_remove_supports_type (GAppInfo *appinfo,
443 const char *content_type,
446 GAppInfoIface *iface;
448 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
449 g_return_val_if_fail (content_type != NULL, FALSE);
451 iface = G_APP_INFO_GET_IFACE (appinfo);
453 if (iface->remove_supports_type)
454 return (* iface->remove_supports_type) (appinfo, content_type, error);
456 g_set_error_literal (error, G_IO_ERROR,
457 G_IO_ERROR_NOT_SUPPORTED,
458 "g_app_info_remove_supports_type not supported yet");
464 * g_app_info_get_supported_types:
465 * @appinfo: a #GAppInfo that can handle files
467 * Retrieves the list of content types that @app_info claims to support.
468 * If this information is not provided by the environment, this function
470 * This function does not take in consideration associations added with
471 * g_app_info_add_supports_type(), but only those exported directly by
474 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
475 * a list of content types.
480 g_app_info_get_supported_types (GAppInfo *appinfo)
482 GAppInfoIface *iface;
484 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
486 iface = G_APP_INFO_GET_IFACE (appinfo);
488 if (iface->get_supported_types)
489 return iface->get_supported_types (appinfo);
496 * g_app_info_get_icon:
497 * @appinfo: a #GAppInfo.
499 * Gets the icon for the application.
501 * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
502 * if there is no default icon.
505 g_app_info_get_icon (GAppInfo *appinfo)
507 GAppInfoIface *iface;
509 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
511 iface = G_APP_INFO_GET_IFACE (appinfo);
513 return (* iface->get_icon) (appinfo);
519 * @appinfo: a #GAppInfo
520 * @files: (allow-none) (element-type GFile): a #GList of #GFile objects
521 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
524 * Launches the application. Passes @files to the launched application
525 * as arguments, using the optional @launch_context to get information
526 * about the details of the launcher (like what screen it is on).
527 * On error, @error will be set accordingly.
529 * To launch the application without arguments pass a %NULL @files list.
531 * Note that even if the launch is successful the application launched
532 * can fail to start if it runs into problems during startup. There is
533 * no way to detect this.
535 * Some URIs can be changed when passed through a GFile (for instance
536 * unsupported URIs with strange formats like mailto:), so if you have
537 * a textual URI you want to pass in as argument, consider using
538 * g_app_info_launch_uris() instead.
540 * The launched application inherits the environment of the launching
541 * process, but it can be modified with g_app_launch_context_setenv() and
542 * g_app_launch_context_unsetenv().
544 * On UNIX, this function sets the <envar>GIO_LAUNCHED_DESKTOP_FILE</envar>
545 * environment variable with the path of the launched desktop file and
546 * <envar>GIO_LAUNCHED_DESKTOP_FILE_PID</envar> to the process
547 * id of the launched process. This can be used to ignore
548 * <envar>GIO_LAUNCHED_DESKTOP_FILE</envar>, should it be inherited
549 * by further processes. The <envar>DISPLAY</envar> and
550 * <envar>DESKTOP_STARTUP_ID</envar> environment variables are also
551 * set, based on information provided in @launch_context.
553 * Returns: %TRUE on successful launch, %FALSE otherwise.
556 g_app_info_launch (GAppInfo *appinfo,
558 GAppLaunchContext *launch_context,
561 GAppInfoIface *iface;
563 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
565 iface = G_APP_INFO_GET_IFACE (appinfo);
567 return (* iface->launch) (appinfo, files, launch_context, error);
572 * g_app_info_supports_uris:
573 * @appinfo: a #GAppInfo.
575 * Checks if the application supports reading files and directories from URIs.
577 * Returns: %TRUE if the @appinfo supports URIs.
580 g_app_info_supports_uris (GAppInfo *appinfo)
582 GAppInfoIface *iface;
584 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
586 iface = G_APP_INFO_GET_IFACE (appinfo);
588 return (* iface->supports_uris) (appinfo);
593 * g_app_info_supports_files:
594 * @appinfo: a #GAppInfo.
596 * Checks if the application accepts files as arguments.
598 * Returns: %TRUE if the @appinfo supports files.
601 g_app_info_supports_files (GAppInfo *appinfo)
603 GAppInfoIface *iface;
605 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
607 iface = G_APP_INFO_GET_IFACE (appinfo);
609 return (* iface->supports_files) (appinfo);
614 * g_app_info_launch_uris:
615 * @appinfo: a #GAppInfo
616 * @uris: (allow-none) (element-type utf8): a #GList containing URIs to launch.
617 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
620 * Launches the application. This passes the @uris to the launched application
621 * as arguments, using the optional @launch_context to get information
622 * about the details of the launcher (like what screen it is on).
623 * On error, @error will be set accordingly.
625 * To launch the application without arguments pass a %NULL @uris list.
627 * Note that even if the launch is successful the application launched
628 * can fail to start if it runs into problems during startup. There is
629 * no way to detect this.
631 * Returns: %TRUE on successful launch, %FALSE otherwise.
634 g_app_info_launch_uris (GAppInfo *appinfo,
636 GAppLaunchContext *launch_context,
639 GAppInfoIface *iface;
641 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
643 iface = G_APP_INFO_GET_IFACE (appinfo);
645 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
650 * g_app_info_should_show:
651 * @appinfo: a #GAppInfo.
653 * Checks if the application info should be shown in menus that
654 * list available applications.
656 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
659 g_app_info_should_show (GAppInfo *appinfo)
661 GAppInfoIface *iface;
663 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
665 iface = G_APP_INFO_GET_IFACE (appinfo);
667 return (* iface->should_show) (appinfo);
671 * g_app_info_launch_default_for_uri:
672 * @uri: the uri to show
673 * @launch_context: (allow-none): an optional #GAppLaunchContext.
676 * Utility function that launches the default application
677 * registered to handle the specified uri. Synchronous I/O
678 * is done on the uri to detect the type of the file if
681 * Returns: %TRUE on success, %FALSE on error.
684 g_app_info_launch_default_for_uri (const char *uri,
685 GAppLaunchContext *launch_context,
689 GAppInfo *app_info = NULL;
693 /* g_file_query_default_handler() calls
694 * g_app_info_get_default_for_uri_scheme() too, but we have to do it
695 * here anyway in case GFile can't parse @uri correctly.
697 uri_scheme = g_uri_parse_scheme (uri);
698 if (uri_scheme && uri_scheme[0] != '\0')
699 app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
706 file = g_file_new_for_uri (uri);
707 app_info = g_file_query_default_handler (file, NULL, error);
708 g_object_unref (file);
709 if (app_info == NULL)
712 /* We still use the original @uri rather than calling
713 * g_file_get_uri(), because GFile might have modified the URI
714 * in ways we don't want (eg, removing the fragment identifier
719 l.data = (char *)uri;
720 l.next = l.prev = NULL;
721 res = g_app_info_launch_uris (app_info, &l,
722 launch_context, error);
724 g_object_unref (app_info);
730 * g_app_info_can_delete:
731 * @appinfo: a #GAppInfo
733 * Obtains the information whether the #GAppInfo can be deleted.
734 * See g_app_info_delete().
736 * Returns: %TRUE if @appinfo can be deleted
741 g_app_info_can_delete (GAppInfo *appinfo)
743 GAppInfoIface *iface;
745 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
747 iface = G_APP_INFO_GET_IFACE (appinfo);
749 if (iface->can_delete)
750 return (* iface->can_delete) (appinfo);
758 * @appinfo: a #GAppInfo
760 * Tries to delete a #GAppInfo.
762 * On some platforms, there may be a difference between user-defined
763 * #GAppInfo<!-- -->s which can be deleted, and system-wide ones which
764 * cannot. See g_app_info_can_delete().
767 * Returns: %TRUE if @appinfo has been deleted
772 g_app_info_delete (GAppInfo *appinfo)
774 GAppInfoIface *iface;
776 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
778 iface = G_APP_INFO_GET_IFACE (appinfo);
780 if (iface->do_delete)
781 return (* iface->do_delete) (appinfo);
793 struct _GAppLaunchContextPrivate {
797 static guint signals[LAST_SIGNAL] = { 0 };
799 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
802 * g_app_launch_context_new:
804 * Creates a new application launch context. This is not normally used,
805 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
807 * Returns: a #GAppLaunchContext.
810 g_app_launch_context_new (void)
812 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
816 g_app_launch_context_finalize (GObject *object)
818 GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
820 g_strfreev (context->priv->envp);
822 G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
826 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
828 GObjectClass *object_class = G_OBJECT_CLASS (klass);
830 object_class->finalize = g_app_launch_context_finalize;
833 * GAppLaunchContext::launch-failed:
834 * @context: the object emitting the signal
835 * @startup_notify_id: the startup notification id for the failed launch
837 * The ::launch-failed signal is emitted when a #GAppInfo launch
838 * fails. The startup notification id is provided, so that the launcher
839 * can cancel the startup notification.
843 signals[LAUNCH_FAILED] = g_signal_new ("launch-failed",
844 G_OBJECT_CLASS_TYPE (object_class),
846 G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
848 G_TYPE_NONE, 1, G_TYPE_STRING);
851 * GAppLaunchContext::launched:
852 * @context: the object emitting the signal
853 * @info: the #GAppInfo that was just launched
854 * @platform_data: additional platform-specific data for this launch
856 * The ::launched signal is emitted when a #GAppInfo is successfully
857 * launched. The @platform_data is an GVariant dictionary mapping
858 * strings to variants (ie a{sv}), which contains additional,
859 * platform-specific data about this launch. On UNIX, at least the
860 * "pid" and "startup-notification-id" keys will be present.
864 signals[LAUNCHED] = g_signal_new ("launched",
865 G_OBJECT_CLASS_TYPE (object_class),
867 G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
870 G_TYPE_APP_INFO, G_TYPE_VARIANT);
874 g_app_launch_context_init (GAppLaunchContext *context)
876 context->priv = g_app_launch_context_get_instance_private (context);
880 * g_app_launch_context_setenv:
881 * @context: a #GAppLaunchContext
882 * @variable: the environment variable to set
883 * @value: the value for to set the variable to.
885 * Arranges for @variable to be set to @value in the child's
886 * environment when @context is used to launch an application.
891 g_app_launch_context_setenv (GAppLaunchContext *context,
892 const char *variable,
895 if (!context->priv->envp)
896 context->priv->envp = g_get_environ ();
898 context->priv->envp =
899 g_environ_setenv (context->priv->envp, variable, value, TRUE);
903 * g_app_launch_context_unsetenv:
904 * @context: a #GAppLaunchContext
905 * @variable: the environment variable to remove
907 * Arranges for @variable to be unset in the child's environment
908 * when @context is used to launch an application.
913 g_app_launch_context_unsetenv (GAppLaunchContext *context,
914 const char *variable)
916 if (!context->priv->envp)
917 context->priv->envp = g_get_environ ();
919 context->priv->envp =
920 g_environ_unsetenv (context->priv->envp, variable);
924 * g_app_launch_context_get_environment:
925 * @context: a #GAppLaunchContext
927 * Gets the complete environment variable list to be passed to
928 * the child process when @context is used to launch an application.
929 * This is a %NULL-terminated array of strings, where each string has
930 * the form <literal>KEY=VALUE</literal>.
932 * Return value: (array zero-terminated=1) (transfer full): the
933 * child's environment
938 g_app_launch_context_get_environment (GAppLaunchContext *context)
940 if (!context->priv->envp)
941 context->priv->envp = g_get_environ ();
943 return g_strdupv (context->priv->envp);
947 * g_app_launch_context_get_display:
948 * @context: a #GAppLaunchContext
950 * @files: (element-type GFile): a #GList of #GFile objects
952 * Gets the display string for the @context. This is used to ensure new
953 * applications are started on the same display as the launching
954 * application, by setting the <envar>DISPLAY</envar> environment variable.
956 * Returns: a display string for the display.
959 g_app_launch_context_get_display (GAppLaunchContext *context,
963 GAppLaunchContextClass *class;
965 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
966 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
968 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
970 if (class->get_display == NULL)
973 return class->get_display (context, info, files);
977 * g_app_launch_context_get_startup_notify_id:
978 * @context: a #GAppLaunchContext
980 * @files: (element-type GFile): a #GList of of #GFile objects
982 * Initiates startup notification for the application and returns the
983 * <envar>DESKTOP_STARTUP_ID</envar> for the launched operation,
986 * Startup notification IDs are defined in the <ulink
987 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">
988 * FreeDesktop.Org Startup Notifications standard</ulink>.
990 * Returns: a startup notification ID for the application, or %NULL if
994 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
998 GAppLaunchContextClass *class;
1000 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
1001 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
1003 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1005 if (class->get_startup_notify_id == NULL)
1008 return class->get_startup_notify_id (context, info, files);
1013 * g_app_launch_context_launch_failed:
1014 * @context: a #GAppLaunchContext.
1015 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1017 * Called when an application has failed to launch, so that it can cancel
1018 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1022 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1023 const char *startup_notify_id)
1025 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1026 g_return_if_fail (startup_notify_id != NULL);
1028 g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1033 * SECTION:gappinfomonitor
1034 * @short_description: Monitor application information for changes
1036 * #GAppInfoMonitor is a very simple object used for monitoring the app
1037 * info database for changes (ie: newly installed or removed
1040 * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1041 * to the "changed" signal.
1043 * In the usual case, applications should try to make note of the change
1044 * (doing things like invalidating caches) but not act on it. In
1045 * particular, applications should avoid making calls to #GAppInfo APIs
1046 * in response to the change signal, deferring these until the time that
1047 * the data is actually required. The exception to this case is when
1048 * application information is actually being displayed on the screen
1049 * (eg: during a search or when the list of all applications is shown).
1050 * The reason for this is that changes to the list of installed
1051 * applications often come in groups (like during system updates) and
1052 * rescanning the list on every change is pointless and expensive.
1060 * The only thing you can do with this is to get it via
1061 * g_app_info_monitor_get() and connect to the "changed" signal.
1066 /* We have one of each of these per main context and hand them out
1067 * according to the thread default main context at the time of the call
1068 * to g_app_info_monitor_get().
1070 * g_object_unref() is only ever called from the same context, so we
1071 * effectively have a single-threaded scenario for each GAppInfoMonitor.
1073 * We use a hashtable to cache the per-context monitor (but we do not
1074 * hold a ref). During finalize, we remove it. This is possible
1075 * because we don't have to worry about the usual races due to the
1076 * single-threaded nature of each object.
1078 * We keep a global list of all contexts that have a monitor for them,
1079 * which we have to access under a lock. When we dispatch the events to
1080 * be handled in each context, we don't pass the monitor, but the
1083 * We dispatch from the GLib worker context, so if we passed the
1084 * monitor, we would need to take a ref on it (in case it was destroyed
1085 * in its own thread meanwhile). The monitor holds a ref on a context
1086 * and the dispatch would mean that the context would hold a ref on the
1087 * monitor. If someone stopped iterating the context at just this
1088 * moment both the context and monitor would leak.
1090 * Instead, we dispatch the context to itself. We don't hold a ref.
1091 * There is the danger that the context will be destroyed during the
1092 * dispatch, but if that is the case then we just won't receive our
1095 * When the dispatch occurs we just lookup the monitor in the hashtable,
1096 * by context. We can now add and remove refs, since the context will
1097 * have been acquired.
1100 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1102 struct _GAppInfoMonitor
1104 GObject parent_instance;
1105 GMainContext *context;
1108 struct _GAppInfoMonitorClass
1110 GObjectClass parent_class;
1113 static GHashTable *g_app_info_monitors;
1114 static GMutex g_app_info_monitor_lock;
1115 static guint g_app_info_monitor_changed_signal;
1117 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1120 g_app_info_monitor_finalize (GObject *object)
1122 GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1124 g_mutex_lock (&g_app_info_monitor_lock);
1125 g_hash_table_remove (g_app_info_monitors, monitor->context);
1126 g_mutex_unlock (&g_app_info_monitor_lock);
1128 G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1132 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1137 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1139 GObjectClass *object_class = G_OBJECT_CLASS (class);
1141 g_app_info_monitor_changed_signal = g_signal_new ("changed", G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1142 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1144 object_class->finalize = g_app_info_monitor_finalize;
1148 * g_app_info_monitor_get:
1150 * Gets the #GAppInfoMonitor for the current thread-default main
1153 * The #GAppInfoMonitor will emit a "changed" signal in the
1154 * thread-default main context whenever the list of installed
1155 * applications (as reported by g_app_info_get_all()) may have changed.
1157 * You must only call g_object_unref() on the return value from under
1158 * the same main context as you created it.
1160 * Returns: (transfer full): a reference to a #GAppInfoMonitor
1165 g_app_info_monitor_get (void)
1167 GAppInfoMonitor *monitor;
1168 GMainContext *context;
1170 context = g_main_context_get_thread_default ();
1172 context = g_main_context_default ();
1174 g_return_val_if_fail (g_main_context_acquire (context), NULL);
1176 g_mutex_lock (&g_app_info_monitor_lock);
1177 if (!g_app_info_monitors)
1178 g_app_info_monitors = g_hash_table_new (NULL, NULL);
1180 monitor = g_hash_table_lookup (g_app_info_monitors, context);
1181 g_mutex_unlock (&g_app_info_monitor_lock);
1185 monitor = g_object_new (G_TYPE_APP_INFO_MONITOR, NULL);
1186 monitor->context = g_main_context_ref (context);
1188 g_mutex_lock (&g_app_info_monitor_lock);
1189 g_hash_table_insert (g_app_info_monitors, context, monitor);
1190 g_mutex_unlock (&g_app_info_monitor_lock);
1193 g_object_ref (monitor);
1195 g_main_context_release (context);
1201 g_app_info_monitor_emit (gpointer user_data)
1203 GMainContext *context = user_data;
1204 GAppInfoMonitor *monitor;
1206 g_mutex_lock (&g_app_info_monitor_lock);
1207 monitor = g_hash_table_lookup (g_app_info_monitors, context);
1208 g_mutex_unlock (&g_app_info_monitor_lock);
1210 /* It is possible that the monitor was already destroyed by the time
1211 * we get here, so make sure it's not NULL.
1213 if (monitor != NULL)
1215 /* We don't have to worry about another thread disposing the
1216 * monitor but we do have to worry about the possibility that one
1217 * of the attached handlers may do so.
1219 * Take a ref so that the monitor doesn't disappear in the middle
1222 g_object_ref (monitor);
1223 g_signal_emit (monitor, g_app_info_monitor_changed_signal, 0);
1224 g_object_unref (monitor);
1231 g_app_info_monitor_fire (void)
1233 GHashTableIter iter;
1236 g_mutex_lock (&g_app_info_monitor_lock);
1238 if (g_app_info_monitors)
1240 g_hash_table_iter_init (&iter, g_app_info_monitors);
1241 while (g_hash_table_iter_next (&iter, &context, NULL))
1245 idle = g_idle_source_new ();
1246 g_source_set_callback (idle, g_app_info_monitor_emit, context, NULL);
1247 g_source_attach (idle, context);
1248 g_source_unref (idle);
1252 g_mutex_unlock (&g_app_info_monitor_lock);