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 #GAppInfos 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()
542 * and g_app_launch_context_unsetenv().
544 * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
545 * environment variable with the path of the launched desktop file and
546 * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
547 * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
548 * should it be inherited by further processes. The `DISPLAY` and
549 * `DESKTOP_STARTUP_ID` environment variables are also set, based
550 * on information provided in @launch_context.
552 * Returns: %TRUE on successful launch, %FALSE otherwise.
555 g_app_info_launch (GAppInfo *appinfo,
557 GAppLaunchContext *launch_context,
560 GAppInfoIface *iface;
562 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
564 iface = G_APP_INFO_GET_IFACE (appinfo);
566 return (* iface->launch) (appinfo, files, launch_context, error);
571 * g_app_info_supports_uris:
572 * @appinfo: a #GAppInfo.
574 * Checks if the application supports reading files and directories from URIs.
576 * Returns: %TRUE if the @appinfo supports URIs.
579 g_app_info_supports_uris (GAppInfo *appinfo)
581 GAppInfoIface *iface;
583 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
585 iface = G_APP_INFO_GET_IFACE (appinfo);
587 return (* iface->supports_uris) (appinfo);
592 * g_app_info_supports_files:
593 * @appinfo: a #GAppInfo.
595 * Checks if the application accepts files as arguments.
597 * Returns: %TRUE if the @appinfo supports files.
600 g_app_info_supports_files (GAppInfo *appinfo)
602 GAppInfoIface *iface;
604 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
606 iface = G_APP_INFO_GET_IFACE (appinfo);
608 return (* iface->supports_files) (appinfo);
613 * g_app_info_launch_uris:
614 * @appinfo: a #GAppInfo
615 * @uris: (allow-none) (element-type utf8): a #GList containing URIs to launch.
616 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
619 * Launches the application. This passes the @uris to the launched application
620 * as arguments, using the optional @launch_context to get information
621 * about the details of the launcher (like what screen it is on).
622 * On error, @error will be set accordingly.
624 * To launch the application without arguments pass a %NULL @uris list.
626 * Note that even if the launch is successful the application launched
627 * can fail to start if it runs into problems during startup. There is
628 * no way to detect this.
630 * Returns: %TRUE on successful launch, %FALSE otherwise.
633 g_app_info_launch_uris (GAppInfo *appinfo,
635 GAppLaunchContext *launch_context,
638 GAppInfoIface *iface;
640 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
642 iface = G_APP_INFO_GET_IFACE (appinfo);
644 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
649 * g_app_info_should_show:
650 * @appinfo: a #GAppInfo.
652 * Checks if the application info should be shown in menus that
653 * list available applications.
655 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
658 g_app_info_should_show (GAppInfo *appinfo)
660 GAppInfoIface *iface;
662 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
664 iface = G_APP_INFO_GET_IFACE (appinfo);
666 return (* iface->should_show) (appinfo);
670 * g_app_info_launch_default_for_uri:
671 * @uri: the uri to show
672 * @launch_context: (allow-none): an optional #GAppLaunchContext.
675 * Utility function that launches the default application
676 * registered to handle the specified uri. Synchronous I/O
677 * is done on the uri to detect the type of the file if
680 * Returns: %TRUE on success, %FALSE on error.
683 g_app_info_launch_default_for_uri (const char *uri,
684 GAppLaunchContext *launch_context,
688 GAppInfo *app_info = NULL;
692 /* g_file_query_default_handler() calls
693 * g_app_info_get_default_for_uri_scheme() too, but we have to do it
694 * here anyway in case GFile can't parse @uri correctly.
696 uri_scheme = g_uri_parse_scheme (uri);
697 if (uri_scheme && uri_scheme[0] != '\0')
698 app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
705 file = g_file_new_for_uri (uri);
706 app_info = g_file_query_default_handler (file, NULL, error);
707 g_object_unref (file);
708 if (app_info == NULL)
711 /* We still use the original @uri rather than calling
712 * g_file_get_uri(), because GFile might have modified the URI
713 * in ways we don't want (eg, removing the fragment identifier
718 l.data = (char *)uri;
719 l.next = l.prev = NULL;
720 res = g_app_info_launch_uris (app_info, &l,
721 launch_context, error);
723 g_object_unref (app_info);
729 * g_app_info_can_delete:
730 * @appinfo: a #GAppInfo
732 * Obtains the information whether the #GAppInfo can be deleted.
733 * See g_app_info_delete().
735 * Returns: %TRUE if @appinfo can be deleted
740 g_app_info_can_delete (GAppInfo *appinfo)
742 GAppInfoIface *iface;
744 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
746 iface = G_APP_INFO_GET_IFACE (appinfo);
748 if (iface->can_delete)
749 return (* iface->can_delete) (appinfo);
757 * @appinfo: a #GAppInfo
759 * Tries to delete a #GAppInfo.
761 * On some platforms, there may be a difference between user-defined
762 * #GAppInfos which can be deleted, and system-wide ones which cannot.
763 * See g_app_info_can_delete().
766 * Returns: %TRUE if @appinfo has been deleted
771 g_app_info_delete (GAppInfo *appinfo)
773 GAppInfoIface *iface;
775 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
777 iface = G_APP_INFO_GET_IFACE (appinfo);
779 if (iface->do_delete)
780 return (* iface->do_delete) (appinfo);
792 struct _GAppLaunchContextPrivate {
796 static guint signals[LAST_SIGNAL] = { 0 };
798 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
801 * g_app_launch_context_new:
803 * Creates a new application launch context. This is not normally used,
804 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
806 * Returns: a #GAppLaunchContext.
809 g_app_launch_context_new (void)
811 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
815 g_app_launch_context_finalize (GObject *object)
817 GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
819 g_strfreev (context->priv->envp);
821 G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
825 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
827 GObjectClass *object_class = G_OBJECT_CLASS (klass);
829 object_class->finalize = g_app_launch_context_finalize;
832 * GAppLaunchContext::launch-failed:
833 * @context: the object emitting the signal
834 * @startup_notify_id: the startup notification id for the failed launch
836 * The ::launch-failed signal is emitted when a #GAppInfo launch
837 * fails. The startup notification id is provided, so that the launcher
838 * can cancel the startup notification.
842 signals[LAUNCH_FAILED] = g_signal_new ("launch-failed",
843 G_OBJECT_CLASS_TYPE (object_class),
845 G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
847 G_TYPE_NONE, 1, G_TYPE_STRING);
850 * GAppLaunchContext::launched:
851 * @context: the object emitting the signal
852 * @info: the #GAppInfo that was just launched
853 * @platform_data: additional platform-specific data for this launch
855 * The ::launched signal is emitted when a #GAppInfo is successfully
856 * launched. The @platform_data is an GVariant dictionary mapping
857 * strings to variants (ie a{sv}), which contains additional,
858 * platform-specific data about this launch. On UNIX, at least the
859 * "pid" and "startup-notification-id" keys will be present.
863 signals[LAUNCHED] = g_signal_new ("launched",
864 G_OBJECT_CLASS_TYPE (object_class),
866 G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
869 G_TYPE_APP_INFO, G_TYPE_VARIANT);
873 g_app_launch_context_init (GAppLaunchContext *context)
875 context->priv = g_app_launch_context_get_instance_private (context);
879 * g_app_launch_context_setenv:
880 * @context: a #GAppLaunchContext
881 * @variable: the environment variable to set
882 * @value: the value for to set the variable to.
884 * Arranges for @variable to be set to @value in the child's
885 * environment when @context is used to launch an application.
890 g_app_launch_context_setenv (GAppLaunchContext *context,
891 const char *variable,
894 if (!context->priv->envp)
895 context->priv->envp = g_get_environ ();
897 context->priv->envp =
898 g_environ_setenv (context->priv->envp, variable, value, TRUE);
902 * g_app_launch_context_unsetenv:
903 * @context: a #GAppLaunchContext
904 * @variable: the environment variable to remove
906 * Arranges for @variable to be unset in the child's environment
907 * when @context is used to launch an application.
912 g_app_launch_context_unsetenv (GAppLaunchContext *context,
913 const char *variable)
915 if (!context->priv->envp)
916 context->priv->envp = g_get_environ ();
918 context->priv->envp =
919 g_environ_unsetenv (context->priv->envp, variable);
923 * g_app_launch_context_get_environment:
924 * @context: a #GAppLaunchContext
926 * Gets the complete environment variable list to be passed to
927 * the child process when @context is used to launch an application.
928 * This is a %NULL-terminated array of strings, where each string has
929 * the form <literal>KEY=VALUE</literal>.
931 * Return value: (array zero-terminated=1) (transfer full): the
932 * child's environment
937 g_app_launch_context_get_environment (GAppLaunchContext *context)
939 if (!context->priv->envp)
940 context->priv->envp = g_get_environ ();
942 return g_strdupv (context->priv->envp);
946 * g_app_launch_context_get_display:
947 * @context: a #GAppLaunchContext
949 * @files: (element-type GFile): a #GList of #GFile objects
951 * Gets the display string for the @context. This is used to ensure new
952 * applications are started on the same display as the launching
953 * application, by setting the `DISPLAY` environment variable.
955 * Returns: a display string for the display.
958 g_app_launch_context_get_display (GAppLaunchContext *context,
962 GAppLaunchContextClass *class;
964 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
965 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
967 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
969 if (class->get_display == NULL)
972 return class->get_display (context, info, files);
976 * g_app_launch_context_get_startup_notify_id:
977 * @context: a #GAppLaunchContext
979 * @files: (element-type GFile): a #GList of of #GFile objects
981 * Initiates startup notification for the application and returns the
982 * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
984 * Startup notification IDs are defined in the
985 * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
987 * Returns: a startup notification ID for the application, or %NULL if
991 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
995 GAppLaunchContextClass *class;
997 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
998 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
1000 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1002 if (class->get_startup_notify_id == NULL)
1005 return class->get_startup_notify_id (context, info, files);
1010 * g_app_launch_context_launch_failed:
1011 * @context: a #GAppLaunchContext.
1012 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1014 * Called when an application has failed to launch, so that it can cancel
1015 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1019 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1020 const char *startup_notify_id)
1022 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1023 g_return_if_fail (startup_notify_id != NULL);
1025 g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1030 * SECTION:gappinfomonitor
1031 * @short_description: Monitor application information for changes
1033 * #GAppInfoMonitor is a very simple object used for monitoring the app
1034 * info database for changes (ie: newly installed or removed
1037 * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1038 * to the "changed" signal.
1040 * In the usual case, applications should try to make note of the change
1041 * (doing things like invalidating caches) but not act on it. In
1042 * particular, applications should avoid making calls to #GAppInfo APIs
1043 * in response to the change signal, deferring these until the time that
1044 * the data is actually required. The exception to this case is when
1045 * application information is actually being displayed on the screen
1046 * (eg: during a search or when the list of all applications is shown).
1047 * The reason for this is that changes to the list of installed
1048 * applications often come in groups (like during system updates) and
1049 * rescanning the list on every change is pointless and expensive.
1057 * The only thing you can do with this is to get it via
1058 * g_app_info_monitor_get() and connect to the "changed" signal.
1063 /* We have one of each of these per main context and hand them out
1064 * according to the thread default main context at the time of the call
1065 * to g_app_info_monitor_get().
1067 * g_object_unref() is only ever called from the same context, so we
1068 * effectively have a single-threaded scenario for each GAppInfoMonitor.
1070 * We use a hashtable to cache the per-context monitor (but we do not
1071 * hold a ref). During finalize, we remove it. This is possible
1072 * because we don't have to worry about the usual races due to the
1073 * single-threaded nature of each object.
1075 * We keep a global list of all contexts that have a monitor for them,
1076 * which we have to access under a lock. When we dispatch the events to
1077 * be handled in each context, we don't pass the monitor, but the
1080 * We dispatch from the GLib worker context, so if we passed the
1081 * monitor, we would need to take a ref on it (in case it was destroyed
1082 * in its own thread meanwhile). The monitor holds a ref on a context
1083 * and the dispatch would mean that the context would hold a ref on the
1084 * monitor. If someone stopped iterating the context at just this
1085 * moment both the context and monitor would leak.
1087 * Instead, we dispatch the context to itself. We don't hold a ref.
1088 * There is the danger that the context will be destroyed during the
1089 * dispatch, but if that is the case then we just won't receive our
1092 * When the dispatch occurs we just lookup the monitor in the hashtable,
1093 * by context. We can now add and remove refs, since the context will
1094 * have been acquired.
1097 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1099 struct _GAppInfoMonitor
1101 GObject parent_instance;
1102 GMainContext *context;
1105 struct _GAppInfoMonitorClass
1107 GObjectClass parent_class;
1110 static GHashTable *g_app_info_monitors;
1111 static GMutex g_app_info_monitor_lock;
1112 static guint g_app_info_monitor_changed_signal;
1114 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1117 g_app_info_monitor_finalize (GObject *object)
1119 GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1121 g_mutex_lock (&g_app_info_monitor_lock);
1122 g_hash_table_remove (g_app_info_monitors, monitor->context);
1123 g_mutex_unlock (&g_app_info_monitor_lock);
1125 G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1129 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1134 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1136 GObjectClass *object_class = G_OBJECT_CLASS (class);
1138 g_app_info_monitor_changed_signal = g_signal_new ("changed", G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1139 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1141 object_class->finalize = g_app_info_monitor_finalize;
1145 * g_app_info_monitor_get:
1147 * Gets the #GAppInfoMonitor for the current thread-default main
1150 * The #GAppInfoMonitor will emit a "changed" signal in the
1151 * thread-default main context whenever the list of installed
1152 * applications (as reported by g_app_info_get_all()) may have changed.
1154 * You must only call g_object_unref() on the return value from under
1155 * the same main context as you created it.
1157 * Returns: (transfer full): a reference to a #GAppInfoMonitor
1162 g_app_info_monitor_get (void)
1164 GAppInfoMonitor *monitor;
1165 GMainContext *context;
1167 context = g_main_context_get_thread_default ();
1169 context = g_main_context_default ();
1171 g_return_val_if_fail (g_main_context_acquire (context), NULL);
1173 g_mutex_lock (&g_app_info_monitor_lock);
1174 if (!g_app_info_monitors)
1175 g_app_info_monitors = g_hash_table_new (NULL, NULL);
1177 monitor = g_hash_table_lookup (g_app_info_monitors, context);
1178 g_mutex_unlock (&g_app_info_monitor_lock);
1182 monitor = g_object_new (G_TYPE_APP_INFO_MONITOR, NULL);
1183 monitor->context = g_main_context_ref (context);
1185 g_mutex_lock (&g_app_info_monitor_lock);
1186 g_hash_table_insert (g_app_info_monitors, context, monitor);
1187 g_mutex_unlock (&g_app_info_monitor_lock);
1190 g_object_ref (monitor);
1192 g_main_context_release (context);
1198 g_app_info_monitor_emit (gpointer user_data)
1200 GMainContext *context = user_data;
1201 GAppInfoMonitor *monitor;
1203 g_mutex_lock (&g_app_info_monitor_lock);
1204 monitor = g_hash_table_lookup (g_app_info_monitors, context);
1205 g_mutex_unlock (&g_app_info_monitor_lock);
1207 /* It is possible that the monitor was already destroyed by the time
1208 * we get here, so make sure it's not NULL.
1210 if (monitor != NULL)
1212 /* We don't have to worry about another thread disposing the
1213 * monitor but we do have to worry about the possibility that one
1214 * of the attached handlers may do so.
1216 * Take a ref so that the monitor doesn't disappear in the middle
1219 g_object_ref (monitor);
1220 g_signal_emit (monitor, g_app_info_monitor_changed_signal, 0);
1221 g_object_unref (monitor);
1228 g_app_info_monitor_fire (void)
1230 GHashTableIter iter;
1233 g_mutex_lock (&g_app_info_monitor_lock);
1235 if (g_app_info_monitors)
1237 g_hash_table_iter_init (&iter, g_app_info_monitors);
1238 while (g_hash_table_iter_next (&iter, &context, NULL))
1242 idle = g_idle_source_new ();
1243 g_source_set_callback (idle, g_app_info_monitor_emit, context, NULL);
1244 g_source_attach (idle, context);
1245 g_source_unref (idle);
1249 g_mutex_unlock (&g_app_info_monitor_lock);