1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gappinfoprivate.h"
25 #include "gcontextspecificgroup.h"
33 #include "gdbusconnection.h"
34 #include "gdbusmessage.h"
35 #include "gportalsupport.h"
36 #include "gunixfdlist.h"
37 #include "gopenuriportal.h"
38 #include <sys/types.h>
45 * @short_description: Application information and launch contexts
47 * @see_also: #GAppInfoMonitor
49 * #GAppInfo and #GAppLaunchContext are used for describing and launching
50 * applications installed on the system.
52 * As of GLib 2.20, URIs will always be converted to POSIX paths
53 * (using g_file_get_path()) when using g_app_info_launch() even if
54 * the application requested an URI and not a POSIX path. For example
55 * for an desktop-file based application with Exec key `totem
56 * %U` and a single URI, `sftp://foo/file.avi`, then
57 * `/home/user/.gvfs/sftp on foo/file.avi` will be passed. This will
58 * only work if a set of suitable GIO extensions (such as gvfs 2.26
59 * compiled with FUSE support), is available and operational; if this
60 * is not the case, the URI will be passed unmodified to the application.
61 * Some URIs, such as `mailto:`, of course cannot be mapped to a POSIX
62 * path (in gvfs there's no FUSE mount for it); such URIs will be
63 * passed unmodified to the application.
65 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
66 * back to the GIO URI in the #GFile constructors (since gvfs
67 * implements the #GVfs extension point). As such, if the application
68 * needs to examine the URI, it needs to use g_file_get_uri() or
69 * similar on #GFile. In other words, an application cannot assume
70 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
71 * equal to the result of g_file_get_uri(). The following snippet
78 * file = g_file_new_for_commandline_arg (uri_from_commandline);
80 * uri = g_file_get_uri (file);
81 * strcmp (uri, uri_from_commandline) == 0;
84 * if (g_file_has_uri_scheme (file, "cdda"))
86 * // do something special with uri
88 * g_object_unref (file);
91 * This code will work when both `cdda://sr0/Track 1.wav` and
92 * `/home/user/.gvfs/cdda on sr0/Track 1.wav` is passed to the
93 * application. It should be noted that it's generally not safe
94 * for applications to rely on the format of a particular URIs.
95 * Different launcher applications (e.g. file managers) may have
96 * different ideas of what a given URI means.
99 struct _GAppLaunchContextPrivate
{
103 typedef GAppInfoIface GAppInfoInterface
;
104 G_DEFINE_INTERFACE (GAppInfo
, g_app_info
, G_TYPE_OBJECT
)
107 g_app_info_default_init (GAppInfoInterface
*iface
)
114 * @appinfo: a #GAppInfo.
116 * Creates a duplicate of a #GAppInfo.
118 * Returns: (transfer full): a duplicate of @appinfo.
121 g_app_info_dup (GAppInfo
*appinfo
)
123 GAppInfoIface
*iface
;
125 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
127 iface
= G_APP_INFO_GET_IFACE (appinfo
);
129 return (* iface
->dup
) (appinfo
);
134 * @appinfo1: the first #GAppInfo.
135 * @appinfo2: the second #GAppInfo.
137 * Checks if two #GAppInfos are equal.
139 * Note that the check <em>may not</em> compare each individual field, and
140 * only does an identity check. In case detecting changes in the contents
141 * is needed, program code must additionally compare relevant fields.
143 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
146 g_app_info_equal (GAppInfo
*appinfo1
,
149 GAppInfoIface
*iface
;
151 g_return_val_if_fail (G_IS_APP_INFO (appinfo1
), FALSE
);
152 g_return_val_if_fail (G_IS_APP_INFO (appinfo2
), FALSE
);
154 if (G_TYPE_FROM_INSTANCE (appinfo1
) != G_TYPE_FROM_INSTANCE (appinfo2
))
157 iface
= G_APP_INFO_GET_IFACE (appinfo1
);
159 return (* iface
->equal
) (appinfo1
, appinfo2
);
164 * @appinfo: a #GAppInfo.
166 * Gets the ID of an application. An id is a string that
167 * identifies the application. The exact format of the id is
168 * platform dependent. For instance, on Unix this is the
169 * desktop file id from the xdg menu specification.
171 * Note that the returned ID may be %NULL, depending on how
172 * the @appinfo has been constructed.
174 * Returns: a string containing the application's ID.
177 g_app_info_get_id (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_id
) (appinfo
);
189 * g_app_info_get_name:
190 * @appinfo: a #GAppInfo.
192 * Gets the installed name of the application.
194 * Returns: the name of the application for @appinfo.
197 g_app_info_get_name (GAppInfo
*appinfo
)
199 GAppInfoIface
*iface
;
201 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
203 iface
= G_APP_INFO_GET_IFACE (appinfo
);
205 return (* iface
->get_name
) (appinfo
);
209 * g_app_info_get_display_name:
210 * @appinfo: a #GAppInfo.
212 * Gets the display name of the application. The display name is often more
213 * descriptive to the user than the name itself.
215 * Returns: the display name of the application for @appinfo, or the name if
216 * no display name is available.
221 g_app_info_get_display_name (GAppInfo
*appinfo
)
223 GAppInfoIface
*iface
;
225 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
227 iface
= G_APP_INFO_GET_IFACE (appinfo
);
229 if (iface
->get_display_name
== NULL
)
230 return (* iface
->get_name
) (appinfo
);
232 return (* iface
->get_display_name
) (appinfo
);
236 * g_app_info_get_description:
237 * @appinfo: a #GAppInfo.
239 * Gets a human-readable description of an installed application.
241 * Returns: a string containing a description of the
242 * application @appinfo, or %NULL if none.
245 g_app_info_get_description (GAppInfo
*appinfo
)
247 GAppInfoIface
*iface
;
249 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
251 iface
= G_APP_INFO_GET_IFACE (appinfo
);
253 return (* iface
->get_description
) (appinfo
);
257 * g_app_info_get_executable:
258 * @appinfo: a #GAppInfo
260 * Gets the executable's name for the installed application.
262 * Returns: (type filename): a string containing the @appinfo's application
266 g_app_info_get_executable (GAppInfo
*appinfo
)
268 GAppInfoIface
*iface
;
270 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
272 iface
= G_APP_INFO_GET_IFACE (appinfo
);
274 return (* iface
->get_executable
) (appinfo
);
279 * g_app_info_get_commandline:
280 * @appinfo: a #GAppInfo
282 * Gets the commandline with which the application will be
285 * Returns: (type filename): a string containing the @appinfo's commandline,
286 * or %NULL if this information is not available
291 g_app_info_get_commandline (GAppInfo
*appinfo
)
293 GAppInfoIface
*iface
;
295 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
297 iface
= G_APP_INFO_GET_IFACE (appinfo
);
299 if (iface
->get_commandline
)
300 return (* iface
->get_commandline
) (appinfo
);
306 * g_app_info_set_as_default_for_type:
307 * @appinfo: a #GAppInfo.
308 * @content_type: the content type.
311 * Sets the application as the default handler for a given type.
313 * Returns: %TRUE on success, %FALSE on error.
316 g_app_info_set_as_default_for_type (GAppInfo
*appinfo
,
317 const char *content_type
,
320 GAppInfoIface
*iface
;
322 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
323 g_return_val_if_fail (content_type
!= NULL
, FALSE
);
325 iface
= G_APP_INFO_GET_IFACE (appinfo
);
327 return (* iface
->set_as_default_for_type
) (appinfo
, content_type
, error
);
331 * g_app_info_set_as_last_used_for_type:
332 * @appinfo: a #GAppInfo.
333 * @content_type: the content type.
336 * Sets the application as the last used application for a given type.
337 * This will make the application appear as first in the list returned
338 * by g_app_info_get_recommended_for_type(), regardless of the default
339 * application for that content type.
341 * Returns: %TRUE on success, %FALSE on error.
344 g_app_info_set_as_last_used_for_type (GAppInfo
*appinfo
,
345 const char *content_type
,
348 GAppInfoIface
*iface
;
350 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
351 g_return_val_if_fail (content_type
!= NULL
, FALSE
);
353 iface
= G_APP_INFO_GET_IFACE (appinfo
);
355 return (* iface
->set_as_last_used_for_type
) (appinfo
, content_type
, error
);
359 * g_app_info_set_as_default_for_extension:
360 * @appinfo: a #GAppInfo.
361 * @extension: (type filename): a string containing the file extension
365 * Sets the application as the default handler for the given file extension.
367 * Returns: %TRUE on success, %FALSE on error.
370 g_app_info_set_as_default_for_extension (GAppInfo
*appinfo
,
371 const char *extension
,
374 GAppInfoIface
*iface
;
376 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
377 g_return_val_if_fail (extension
!= NULL
, FALSE
);
379 iface
= G_APP_INFO_GET_IFACE (appinfo
);
381 if (iface
->set_as_default_for_extension
)
382 return (* iface
->set_as_default_for_extension
) (appinfo
, extension
, error
);
384 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
385 "g_app_info_set_as_default_for_extension not supported yet");
391 * g_app_info_add_supports_type:
392 * @appinfo: a #GAppInfo.
393 * @content_type: a string.
396 * Adds a content type to the application information to indicate the
397 * application is capable of opening files with the given content type.
399 * Returns: %TRUE on success, %FALSE on error.
402 g_app_info_add_supports_type (GAppInfo
*appinfo
,
403 const char *content_type
,
406 GAppInfoIface
*iface
;
408 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
409 g_return_val_if_fail (content_type
!= NULL
, FALSE
);
411 iface
= G_APP_INFO_GET_IFACE (appinfo
);
413 if (iface
->add_supports_type
)
414 return (* iface
->add_supports_type
) (appinfo
, content_type
, error
);
416 g_set_error_literal (error
, G_IO_ERROR
,
417 G_IO_ERROR_NOT_SUPPORTED
,
418 "g_app_info_add_supports_type not supported yet");
425 * g_app_info_can_remove_supports_type:
426 * @appinfo: a #GAppInfo.
428 * Checks if a supported content type can be removed from an application.
430 * Returns: %TRUE if it is possible to remove supported
431 * content types from a given @appinfo, %FALSE if not.
434 g_app_info_can_remove_supports_type (GAppInfo
*appinfo
)
436 GAppInfoIface
*iface
;
438 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
440 iface
= G_APP_INFO_GET_IFACE (appinfo
);
442 if (iface
->can_remove_supports_type
)
443 return (* iface
->can_remove_supports_type
) (appinfo
);
450 * g_app_info_remove_supports_type:
451 * @appinfo: a #GAppInfo.
452 * @content_type: a string.
455 * Removes a supported type from an application, if possible.
457 * Returns: %TRUE on success, %FALSE on error.
460 g_app_info_remove_supports_type (GAppInfo
*appinfo
,
461 const char *content_type
,
464 GAppInfoIface
*iface
;
466 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
467 g_return_val_if_fail (content_type
!= NULL
, FALSE
);
469 iface
= G_APP_INFO_GET_IFACE (appinfo
);
471 if (iface
->remove_supports_type
)
472 return (* iface
->remove_supports_type
) (appinfo
, content_type
, error
);
474 g_set_error_literal (error
, G_IO_ERROR
,
475 G_IO_ERROR_NOT_SUPPORTED
,
476 "g_app_info_remove_supports_type not supported yet");
482 * g_app_info_get_supported_types:
483 * @appinfo: a #GAppInfo that can handle files
485 * Retrieves the list of content types that @app_info claims to support.
486 * If this information is not provided by the environment, this function
488 * This function does not take in consideration associations added with
489 * g_app_info_add_supports_type(), but only those exported directly by
492 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
493 * a list of content types.
498 g_app_info_get_supported_types (GAppInfo
*appinfo
)
500 GAppInfoIface
*iface
;
502 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
504 iface
= G_APP_INFO_GET_IFACE (appinfo
);
506 if (iface
->get_supported_types
)
507 return iface
->get_supported_types (appinfo
);
514 * g_app_info_get_icon:
515 * @appinfo: a #GAppInfo.
517 * Gets the icon for the application.
519 * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
520 * if there is no default icon.
523 g_app_info_get_icon (GAppInfo
*appinfo
)
525 GAppInfoIface
*iface
;
527 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), NULL
);
529 iface
= G_APP_INFO_GET_IFACE (appinfo
);
531 return (* iface
->get_icon
) (appinfo
);
537 * @appinfo: a #GAppInfo
538 * @files: (nullable) (element-type GFile): a #GList of #GFile objects
539 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
542 * Launches the application. Passes @files to the launched application
543 * as arguments, using the optional @launch_context to get information
544 * about the details of the launcher (like what screen it is on).
545 * On error, @error will be set accordingly.
547 * To launch the application without arguments pass a %NULL @files list.
549 * Note that even if the launch is successful the application launched
550 * can fail to start if it runs into problems during startup. There is
551 * no way to detect this.
553 * Some URIs can be changed when passed through a GFile (for instance
554 * unsupported URIs with strange formats like mailto:), so if you have
555 * a textual URI you want to pass in as argument, consider using
556 * g_app_info_launch_uris() instead.
558 * The launched application inherits the environment of the launching
559 * process, but it can be modified with g_app_launch_context_setenv()
560 * and g_app_launch_context_unsetenv().
562 * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
563 * environment variable with the path of the launched desktop file and
564 * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
565 * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
566 * should it be inherited by further processes. The `DISPLAY` and
567 * `DESKTOP_STARTUP_ID` environment variables are also set, based
568 * on information provided in @launch_context.
570 * Returns: %TRUE on successful launch, %FALSE otherwise.
573 g_app_info_launch (GAppInfo
*appinfo
,
575 GAppLaunchContext
*launch_context
,
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
->launch
) (appinfo
, files
, launch_context
, error
);
589 * g_app_info_supports_uris:
590 * @appinfo: a #GAppInfo.
592 * Checks if the application supports reading files and directories from URIs.
594 * Returns: %TRUE if the @appinfo supports URIs.
597 g_app_info_supports_uris (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_uris
) (appinfo
);
610 * g_app_info_supports_files:
611 * @appinfo: a #GAppInfo.
613 * Checks if the application accepts files as arguments.
615 * Returns: %TRUE if the @appinfo supports files.
618 g_app_info_supports_files (GAppInfo
*appinfo
)
620 GAppInfoIface
*iface
;
622 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
624 iface
= G_APP_INFO_GET_IFACE (appinfo
);
626 return (* iface
->supports_files
) (appinfo
);
631 * g_app_info_launch_uris:
632 * @appinfo: a #GAppInfo
633 * @uris: (nullable) (element-type utf8): a #GList containing URIs to launch.
634 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
637 * Launches the application. This passes the @uris to the launched application
638 * as arguments, using the optional @launch_context to get information
639 * about the details of the launcher (like what screen it is on).
640 * On error, @error will be set accordingly.
642 * To launch the application without arguments pass a %NULL @uris list.
644 * Note that even if the launch is successful the application launched
645 * can fail to start if it runs into problems during startup. There is
646 * no way to detect this.
648 * Returns: %TRUE on successful launch, %FALSE otherwise.
651 g_app_info_launch_uris (GAppInfo
*appinfo
,
653 GAppLaunchContext
*launch_context
,
656 GAppInfoIface
*iface
;
658 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
660 iface
= G_APP_INFO_GET_IFACE (appinfo
);
662 return (* iface
->launch_uris
) (appinfo
, uris
, launch_context
, error
);
667 * g_app_info_should_show:
668 * @appinfo: a #GAppInfo.
670 * Checks if the application info should be shown in menus that
671 * list available applications.
673 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
676 g_app_info_should_show (GAppInfo
*appinfo
)
678 GAppInfoIface
*iface
;
680 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
682 iface
= G_APP_INFO_GET_IFACE (appinfo
);
684 return (* iface
->should_show
) (appinfo
);
688 launch_default_for_uri (const char *uri
,
689 GAppLaunchContext
*context
,
693 GAppInfo
*app_info
= NULL
;
697 /* g_file_query_default_handler() calls
698 * g_app_info_get_default_for_uri_scheme() too, but we have to do it
699 * here anyway in case GFile can't parse @uri correctly.
701 uri_scheme
= g_uri_parse_scheme (uri
);
702 if (uri_scheme
&& uri_scheme
[0] != '\0')
703 app_info
= g_app_info_get_default_for_uri_scheme (uri_scheme
);
710 file
= g_file_new_for_uri (uri
);
711 app_info
= g_file_query_default_handler (file
, NULL
, error
);
712 g_object_unref (file
);
715 if (app_info
== NULL
)
718 l
.data
= (char *)uri
;
719 l
.next
= l
.prev
= NULL
;
720 res
= g_app_info_launch_uris (app_info
, &l
, context
, error
);
722 g_object_unref (app_info
);
728 * g_app_info_launch_default_for_uri:
729 * @uri: the uri to show
730 * @launch_context: (nullable): an optional #GAppLaunchContext
731 * @error: (nullable): return location for an error, or %NULL
733 * Utility function that launches the default application
734 * registered to handle the specified uri. Synchronous I/O
735 * is done on the uri to detect the type of the file if
738 * Returns: %TRUE on success, %FALSE on error.
741 g_app_info_launch_default_for_uri (const char *uri
,
742 GAppLaunchContext
*launch_context
,
745 if (launch_default_for_uri (uri
, launch_context
, error
))
749 if (glib_should_use_portal ())
751 const char *parent_window
= NULL
;
753 /* Reset any error previously set by launch_default_for_uri */
754 g_clear_error (error
);
756 if (launch_context
&& launch_context
->priv
->envp
)
757 parent_window
= g_environ_getenv (launch_context
->priv
->envp
, "PARENT_WINDOW_ID");
759 return g_openuri_portal_open_uri (uri
, parent_window
, error
);
768 * g_app_info_launch_default_for_uri_async:
769 * @uri: the uri to show
770 * @context: (nullable): an optional #GAppLaunchContext
771 * cancellable: (nullable): a #GCancellable
772 * @callback: (nullable): a #GASyncReadyCallback to call when the request is done
773 * @user_data: (nullable): data to pass to @callback
775 * Async version of g_app_info_launch_default_for_uri().
777 * This version is useful if you are interested in receiving
778 * error information in the case where the application is
779 * sandboxed and the portal may present an application chooser
780 * dialog to the user.
785 g_app_info_launch_default_for_uri_async (const char *uri
,
786 GAppLaunchContext
*context
,
787 GCancellable
*cancellable
,
788 GAsyncReadyCallback callback
,
792 GError
*error
= NULL
;
795 res
= launch_default_for_uri (uri
, context
, &error
);
798 if (!res
&& glib_should_use_portal ())
800 const char *parent_window
= NULL
;
802 if (context
&& context
->priv
->envp
)
803 parent_window
= g_environ_getenv (context
->priv
->envp
, "PARENT_WINDOW_ID");
805 g_openuri_portal_open_uri_async (uri
, parent_window
, cancellable
, callback
, user_data
);
810 task
= g_task_new (context
, cancellable
, callback
, user_data
);
812 g_task_return_error (task
, error
);
814 g_task_return_boolean (task
, TRUE
);
816 g_object_unref (task
);
820 * g_app_info_launch_default_for_uri_finish:
821 * @result: a #GAsyncResult
822 * @error: (nullable): return location for an error, or %NULL
824 * Finishes an asynchronous launch-default-for-uri operation.
826 * Returns: %TRUE if the launch was successful, %FALSE if @error is set
831 g_app_info_launch_default_for_uri_finish (GAsyncResult
*result
,
835 return g_openuri_portal_open_uri_finish (result
, error
);
837 return g_task_propagate_boolean (G_TASK (result
), error
);
842 * g_app_info_can_delete:
843 * @appinfo: a #GAppInfo
845 * Obtains the information whether the #GAppInfo can be deleted.
846 * See g_app_info_delete().
848 * Returns: %TRUE if @appinfo can be deleted
853 g_app_info_can_delete (GAppInfo
*appinfo
)
855 GAppInfoIface
*iface
;
857 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
859 iface
= G_APP_INFO_GET_IFACE (appinfo
);
861 if (iface
->can_delete
)
862 return (* iface
->can_delete
) (appinfo
);
870 * @appinfo: a #GAppInfo
872 * Tries to delete a #GAppInfo.
874 * On some platforms, there may be a difference between user-defined
875 * #GAppInfos which can be deleted, and system-wide ones which cannot.
876 * See g_app_info_can_delete().
879 * Returns: %TRUE if @appinfo has been deleted
884 g_app_info_delete (GAppInfo
*appinfo
)
886 GAppInfoIface
*iface
;
888 g_return_val_if_fail (G_IS_APP_INFO (appinfo
), FALSE
);
890 iface
= G_APP_INFO_GET_IFACE (appinfo
);
892 if (iface
->do_delete
)
893 return (* iface
->do_delete
) (appinfo
);
905 static guint signals
[LAST_SIGNAL
] = { 0 };
907 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext
, g_app_launch_context
, G_TYPE_OBJECT
)
910 * g_app_launch_context_new:
912 * Creates a new application launch context. This is not normally used,
913 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
915 * Returns: a #GAppLaunchContext.
918 g_app_launch_context_new (void)
920 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT
, NULL
);
924 g_app_launch_context_finalize (GObject
*object
)
926 GAppLaunchContext
*context
= G_APP_LAUNCH_CONTEXT (object
);
928 g_strfreev (context
->priv
->envp
);
930 G_OBJECT_CLASS (g_app_launch_context_parent_class
)->finalize (object
);
934 g_app_launch_context_class_init (GAppLaunchContextClass
*klass
)
936 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
938 object_class
->finalize
= g_app_launch_context_finalize
;
941 * GAppLaunchContext::launch-failed:
942 * @context: the object emitting the signal
943 * @startup_notify_id: the startup notification id for the failed launch
945 * The ::launch-failed signal is emitted when a #GAppInfo launch
946 * fails. The startup notification id is provided, so that the launcher
947 * can cancel the startup notification.
951 signals
[LAUNCH_FAILED
] = g_signal_new (I_("launch-failed"),
952 G_OBJECT_CLASS_TYPE (object_class
),
954 G_STRUCT_OFFSET (GAppLaunchContextClass
, launch_failed
),
956 G_TYPE_NONE
, 1, G_TYPE_STRING
);
959 * GAppLaunchContext::launched:
960 * @context: the object emitting the signal
961 * @info: the #GAppInfo that was just launched
962 * @platform_data: additional platform-specific data for this launch
964 * The ::launched signal is emitted when a #GAppInfo is successfully
965 * launched. The @platform_data is an GVariant dictionary mapping
966 * strings to variants (ie a{sv}), which contains additional,
967 * platform-specific data about this launch. On UNIX, at least the
968 * "pid" and "startup-notification-id" keys will be present.
972 signals
[LAUNCHED
] = g_signal_new (I_("launched"),
973 G_OBJECT_CLASS_TYPE (object_class
),
975 G_STRUCT_OFFSET (GAppLaunchContextClass
, launched
),
978 G_TYPE_APP_INFO
, G_TYPE_VARIANT
);
982 g_app_launch_context_init (GAppLaunchContext
*context
)
984 context
->priv
= g_app_launch_context_get_instance_private (context
);
988 * g_app_launch_context_setenv:
989 * @context: a #GAppLaunchContext
990 * @variable: the environment variable to set
991 * @value: the value for to set the variable to.
993 * Arranges for @variable to be set to @value in the child's
994 * environment when @context is used to launch an application.
999 g_app_launch_context_setenv (GAppLaunchContext
*context
,
1000 const char *variable
,
1003 if (!context
->priv
->envp
)
1004 context
->priv
->envp
= g_get_environ ();
1006 context
->priv
->envp
=
1007 g_environ_setenv (context
->priv
->envp
, variable
, value
, TRUE
);
1011 * g_app_launch_context_unsetenv:
1012 * @context: a #GAppLaunchContext
1013 * @variable: the environment variable to remove
1015 * Arranges for @variable to be unset in the child's environment
1016 * when @context is used to launch an application.
1021 g_app_launch_context_unsetenv (GAppLaunchContext
*context
,
1022 const char *variable
)
1024 if (!context
->priv
->envp
)
1025 context
->priv
->envp
= g_get_environ ();
1027 context
->priv
->envp
=
1028 g_environ_unsetenv (context
->priv
->envp
, variable
);
1032 * g_app_launch_context_get_environment:
1033 * @context: a #GAppLaunchContext
1035 * Gets the complete environment variable list to be passed to
1036 * the child process when @context is used to launch an application.
1037 * This is a %NULL-terminated array of strings, where each string has
1038 * the form `KEY=VALUE`.
1040 * Returns: (array zero-terminated=1) (transfer full): the
1041 * child's environment
1046 g_app_launch_context_get_environment (GAppLaunchContext
*context
)
1048 if (!context
->priv
->envp
)
1049 context
->priv
->envp
= g_get_environ ();
1051 return g_strdupv (context
->priv
->envp
);
1055 * g_app_launch_context_get_display:
1056 * @context: a #GAppLaunchContext
1057 * @info: a #GAppInfo
1058 * @files: (element-type GFile): a #GList of #GFile objects
1060 * Gets the display string for the @context. This is used to ensure new
1061 * applications are started on the same display as the launching
1062 * application, by setting the `DISPLAY` environment variable.
1064 * Returns: a display string for the display.
1067 g_app_launch_context_get_display (GAppLaunchContext
*context
,
1071 GAppLaunchContextClass
*class;
1073 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context
), NULL
);
1074 g_return_val_if_fail (G_IS_APP_INFO (info
), NULL
);
1076 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context
);
1078 if (class->get_display
== NULL
)
1081 return class->get_display (context
, info
, files
);
1085 * g_app_launch_context_get_startup_notify_id:
1086 * @context: a #GAppLaunchContext
1087 * @info: a #GAppInfo
1088 * @files: (element-type GFile): a #GList of of #GFile objects
1090 * Initiates startup notification for the application and returns the
1091 * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
1093 * Startup notification IDs are defined in the
1094 * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
1096 * Returns: a startup notification ID for the application, or %NULL if
1100 g_app_launch_context_get_startup_notify_id (GAppLaunchContext
*context
,
1104 GAppLaunchContextClass
*class;
1106 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context
), NULL
);
1107 g_return_val_if_fail (G_IS_APP_INFO (info
), NULL
);
1109 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context
);
1111 if (class->get_startup_notify_id
== NULL
)
1114 return class->get_startup_notify_id (context
, info
, files
);
1119 * g_app_launch_context_launch_failed:
1120 * @context: a #GAppLaunchContext.
1121 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1123 * Called when an application has failed to launch, so that it can cancel
1124 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1128 g_app_launch_context_launch_failed (GAppLaunchContext
*context
,
1129 const char *startup_notify_id
)
1131 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context
));
1132 g_return_if_fail (startup_notify_id
!= NULL
);
1134 g_signal_emit (context
, signals
[LAUNCH_FAILED
], 0, startup_notify_id
);
1139 * SECTION:gappinfomonitor
1140 * @short_description: Monitor application information for changes
1142 * #GAppInfoMonitor is a very simple object used for monitoring the app
1143 * info database for changes (ie: newly installed or removed
1146 * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1147 * to the "changed" signal.
1149 * In the usual case, applications should try to make note of the change
1150 * (doing things like invalidating caches) but not act on it. In
1151 * particular, applications should avoid making calls to #GAppInfo APIs
1152 * in response to the change signal, deferring these until the time that
1153 * the data is actually required. The exception to this case is when
1154 * application information is actually being displayed on the screen
1155 * (eg: during a search or when the list of all applications is shown).
1156 * The reason for this is that changes to the list of installed
1157 * applications often come in groups (like during system updates) and
1158 * rescanning the list on every change is pointless and expensive.
1166 * The only thing you can do with this is to get it via
1167 * g_app_info_monitor_get() and connect to the "changed" signal.
1172 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass
;
1174 struct _GAppInfoMonitor
1176 GObject parent_instance
;
1177 GMainContext
*context
;
1180 struct _GAppInfoMonitorClass
1182 GObjectClass parent_class
;
1185 static GContextSpecificGroup g_app_info_monitor_group
;
1186 static guint g_app_info_monitor_changed_signal
;
1188 G_DEFINE_TYPE (GAppInfoMonitor
, g_app_info_monitor
, G_TYPE_OBJECT
)
1191 g_app_info_monitor_finalize (GObject
*object
)
1193 GAppInfoMonitor
*monitor
= G_APP_INFO_MONITOR (object
);
1195 g_context_specific_group_remove (&g_app_info_monitor_group
, monitor
->context
, monitor
, NULL
);
1197 G_OBJECT_CLASS (g_app_info_monitor_parent_class
)->finalize (object
);
1201 g_app_info_monitor_init (GAppInfoMonitor
*monitor
)
1206 g_app_info_monitor_class_init (GAppInfoMonitorClass
*class)
1208 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
1211 * GAppInfoMonitor::changed:
1213 * Signal emitted when the app info database for changes (ie: newly installed
1214 * or removed applications).
1216 g_app_info_monitor_changed_signal
= g_signal_new (I_("changed"), G_TYPE_APP_INFO_MONITOR
, G_SIGNAL_RUN_FIRST
,
1217 0, NULL
, NULL
, g_cclosure_marshal_VOID__VOID
, G_TYPE_NONE
, 0);
1219 object_class
->finalize
= g_app_info_monitor_finalize
;
1223 * g_app_info_monitor_get:
1225 * Gets the #GAppInfoMonitor for the current thread-default main
1228 * The #GAppInfoMonitor will emit a "changed" signal in the
1229 * thread-default main context whenever the list of installed
1230 * applications (as reported by g_app_info_get_all()) may have changed.
1232 * You must only call g_object_unref() on the return value from under
1233 * the same main context as you created it.
1235 * Returns: (transfer full): a reference to a #GAppInfoMonitor
1240 g_app_info_monitor_get (void)
1242 return g_context_specific_group_get (&g_app_info_monitor_group
,
1243 G_TYPE_APP_INFO_MONITOR
,
1244 G_STRUCT_OFFSET (GAppInfoMonitor
, context
),
1249 g_app_info_monitor_fire (void)
1251 g_context_specific_group_emit (&g_app_info_monitor_group
, g_app_info_monitor_changed_signal
);