Update Friulian translation
[glib.git] / gio / gappinfo.c
blobb0c03dda82982995a7b3daa253d755dd0b91883c
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>
21 #include "config.h"
23 #include "gappinfo.h"
24 #include "gappinfoprivate.h"
25 #include "gcontextspecificgroup.h"
26 #include "gtask.h"
28 #include "glibintl.h"
29 #include <gioerror.h>
30 #include <gfile.h>
32 #ifdef G_OS_UNIX
33 #include "gdbusconnection.h"
34 #include "gdbusmessage.h"
35 #include "gportalsupport.h"
36 #include "gunixfdlist.h"
37 #include "gopenuriportal.h"
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #endif
43 /**
44 * SECTION:gappinfo
45 * @short_description: Application information and launch contexts
46 * @include: gio/gio.h
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
72 * illustrates this:
74 * |[
75 * GFile *f;
76 * char *uri;
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;
82 * g_free (uri);
84 * if (g_file_has_uri_scheme (file, "cdda"))
85 * {
86 * // do something special with uri
87 * }
88 * g_object_unref (file);
89 * ]|
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 {
100 char **envp;
103 typedef GAppInfoIface GAppInfoInterface;
104 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
106 static void
107 g_app_info_default_init (GAppInfoInterface *iface)
113 * g_app_info_dup:
114 * @appinfo: a #GAppInfo.
116 * Creates a duplicate of a #GAppInfo.
118 * Returns: (transfer full): a duplicate of @appinfo.
120 GAppInfo *
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);
133 * g_app_info_equal:
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.
145 gboolean
146 g_app_info_equal (GAppInfo *appinfo1,
147 GAppInfo *appinfo2)
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))
155 return FALSE;
157 iface = G_APP_INFO_GET_IFACE (appinfo1);
159 return (* iface->equal) (appinfo1, appinfo2);
163 * g_app_info_get_id:
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.
176 const char *
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.
196 const char *
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.
218 * Since: 2.24
220 const char *
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.
244 const char *
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
263 * binaries name
265 const char *
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
283 * started.
285 * Returns: (type filename): a string containing the @appinfo's commandline,
286 * or %NULL if this information is not available
288 * Since: 2.20
290 const char *
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);
302 return NULL;
306 * g_app_info_set_as_default_for_type:
307 * @appinfo: a #GAppInfo.
308 * @content_type: the content type.
309 * @error: a #GError.
311 * Sets the application as the default handler for a given type.
313 * Returns: %TRUE on success, %FALSE on error.
315 gboolean
316 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
317 const char *content_type,
318 GError **error)
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.
334 * @error: a #GError.
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.
343 gboolean
344 g_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
345 const char *content_type,
346 GError **error)
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
362 * (without the dot).
363 * @error: a #GError.
365 * Sets the application as the default handler for the given file extension.
367 * Returns: %TRUE on success, %FALSE on error.
369 gboolean
370 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
371 const char *extension,
372 GError **error)
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");
386 return FALSE;
391 * g_app_info_add_supports_type:
392 * @appinfo: a #GAppInfo.
393 * @content_type: a string.
394 * @error: a #GError.
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.
401 gboolean
402 g_app_info_add_supports_type (GAppInfo *appinfo,
403 const char *content_type,
404 GError **error)
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");
420 return FALSE;
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.
433 gboolean
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);
445 return FALSE;
450 * g_app_info_remove_supports_type:
451 * @appinfo: a #GAppInfo.
452 * @content_type: a string.
453 * @error: a #GError.
455 * Removes a supported type from an application, if possible.
457 * Returns: %TRUE on success, %FALSE on error.
459 gboolean
460 g_app_info_remove_supports_type (GAppInfo *appinfo,
461 const char *content_type,
462 GError **error)
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");
478 return FALSE;
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
487 * will return %NULL.
488 * This function does not take in consideration associations added with
489 * g_app_info_add_supports_type(), but only those exported directly by
490 * the application.
492 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
493 * a list of content types.
495 * Since: 2.34
497 const char **
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);
508 else
509 return NULL;
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.
522 GIcon *
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);
536 * g_app_info_launch:
537 * @appinfo: a #GAppInfo
538 * @files: (nullable) (element-type GFile): a #GList of #GFile objects
539 * @launch_context: (nullable): a #GAppLaunchContext or %NULL
540 * @error: a #GError
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.
572 gboolean
573 g_app_info_launch (GAppInfo *appinfo,
574 GList *files,
575 GAppLaunchContext *launch_context,
576 GError **error)
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.
596 gboolean
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.
617 gboolean
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
635 * @error: a #GError
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.
650 gboolean
651 g_app_info_launch_uris (GAppInfo *appinfo,
652 GList *uris,
653 GAppLaunchContext *launch_context,
654 GError **error)
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.
675 gboolean
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);
687 static gboolean
688 launch_default_for_uri (const char *uri,
689 GAppLaunchContext *context,
690 GError **error)
692 char *uri_scheme;
693 GAppInfo *app_info = NULL;
694 GList l;
695 gboolean res;
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);
704 g_free (uri_scheme);
706 if (!app_info)
708 GFile *file;
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)
716 return FALSE;
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);
724 return res;
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
736 * required.
738 * Returns: %TRUE on success, %FALSE on error.
740 gboolean
741 g_app_info_launch_default_for_uri (const char *uri,
742 GAppLaunchContext *launch_context,
743 GError **error)
745 if (launch_default_for_uri (uri, launch_context, error))
746 return TRUE;
748 #ifdef G_OS_UNIX
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);
762 #endif
764 return FALSE;
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.
782 * Since: 2.50
784 void
785 g_app_info_launch_default_for_uri_async (const char *uri,
786 GAppLaunchContext *context,
787 GCancellable *cancellable,
788 GAsyncReadyCallback callback,
789 gpointer user_data)
791 gboolean res;
792 GError *error = NULL;
793 GTask *task;
795 res = launch_default_for_uri (uri, context, &error);
797 #ifdef G_OS_UNIX
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);
806 return;
808 #endif
810 task = g_task_new (context, cancellable, callback, user_data);
811 if (!res)
812 g_task_return_error (task, error);
813 else
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
828 * Since: 2.50
830 gboolean
831 g_app_info_launch_default_for_uri_finish (GAsyncResult *result,
832 GError **error)
834 #ifdef G_OS_UNIX
835 return g_openuri_portal_open_uri_finish (result, error);
836 #else
837 return g_task_propagate_boolean (G_TASK (result), error);
838 #endif
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
850 * Since: 2.20
852 gboolean
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);
864 return FALSE;
869 * g_app_info_delete:
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().
878 * Virtual: do_delete
879 * Returns: %TRUE if @appinfo has been deleted
881 * Since: 2.20
883 gboolean
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);
895 return FALSE;
899 enum {
900 LAUNCH_FAILED,
901 LAUNCHED,
902 LAST_SIGNAL
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.
917 GAppLaunchContext *
918 g_app_launch_context_new (void)
920 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
923 static void
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);
933 static void
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.
949 * Since: 2.36
951 signals[LAUNCH_FAILED] = g_signal_new (I_("launch-failed"),
952 G_OBJECT_CLASS_TYPE (object_class),
953 G_SIGNAL_RUN_LAST,
954 G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
955 NULL, NULL, NULL,
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.
970 * Since: 2.36
972 signals[LAUNCHED] = g_signal_new (I_("launched"),
973 G_OBJECT_CLASS_TYPE (object_class),
974 G_SIGNAL_RUN_LAST,
975 G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
976 NULL, NULL, NULL,
977 G_TYPE_NONE, 2,
978 G_TYPE_APP_INFO, G_TYPE_VARIANT);
981 static void
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.
996 * Since: 2.32
998 void
999 g_app_launch_context_setenv (GAppLaunchContext *context,
1000 const char *variable,
1001 const char *value)
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.
1018 * Since: 2.32
1020 void
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
1043 * Since: 2.32
1045 char **
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.
1066 char *
1067 g_app_launch_context_get_display (GAppLaunchContext *context,
1068 GAppInfo *info,
1069 GList *files)
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)
1079 return 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
1097 * not supported.
1099 char *
1100 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
1101 GAppInfo *info,
1102 GList *files)
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)
1112 return 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().
1127 void
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
1144 * applications).
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.
1160 * Since: 2.40
1164 * GAppInfoMonitor:
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.
1169 * Since: 2.40
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)
1190 static void
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);
1200 static void
1201 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1205 static void
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
1226 * context.
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
1237 * Since: 2.40
1239 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),
1245 NULL);
1248 void
1249 g_app_info_monitor_fire (void)
1251 g_context_specific_group_emit (&g_app_info_monitor_group, g_app_info_monitor_changed_signal);