Imported Upstream version 2.53.3
[platform/upstream/glib.git] / gio / gappinfo.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "gappinfo.h"
24 #include "gappinfoprivate.h"
25 #include "gcontextspecificgroup.h"
26 #include "gtask.h"
27
28 #include "glibintl.h"
29 #include <gioerror.h>
30 #include <gfile.h>
31
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
42
43 /**
44  * SECTION:gappinfo
45  * @short_description: Application information and launch contexts
46  * @include: gio/gio.h
47  * @see_also: #GAppInfoMonitor
48  * 
49  * #GAppInfo and #GAppLaunchContext are used for describing and launching
50  * applications installed on the system.
51  *
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.
64  *
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:
73  *
74  * |[ 
75  * GFile *f;
76  * char *uri;
77  *
78  * file = g_file_new_for_commandline_arg (uri_from_commandline);
79  *
80  * uri = g_file_get_uri (file);
81  * strcmp (uri, uri_from_commandline) == 0;
82  * g_free (uri);
83  *
84  * if (g_file_has_uri_scheme (file, "cdda"))
85  *   {
86  *     // do something special with uri
87  *   }
88  * g_object_unref (file);
89  * ]|
90  *
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.
97  */
98
99 struct _GAppLaunchContextPrivate {
100   char **envp;
101 };
102
103 typedef GAppInfoIface GAppInfoInterface;
104 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
105
106 static void
107 g_app_info_default_init (GAppInfoInterface *iface)
108 {
109 }
110
111
112 /**
113  * g_app_info_dup:
114  * @appinfo: a #GAppInfo.
115  * 
116  * Creates a duplicate of a #GAppInfo.
117  *
118  * Returns: (transfer full): a duplicate of @appinfo.
119  **/
120 GAppInfo *
121 g_app_info_dup (GAppInfo *appinfo)
122 {
123   GAppInfoIface *iface;
124
125   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
126
127   iface = G_APP_INFO_GET_IFACE (appinfo);
128
129   return (* iface->dup) (appinfo);
130 }
131
132 /**
133  * g_app_info_equal:
134  * @appinfo1: the first #GAppInfo.
135  * @appinfo2: the second #GAppInfo.
136  *
137  * Checks if two #GAppInfos are equal.
138  *
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.
142  *
143  * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
144  **/
145 gboolean
146 g_app_info_equal (GAppInfo *appinfo1,
147                   GAppInfo *appinfo2)
148 {
149   GAppInfoIface *iface;
150
151   g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
152   g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
153
154   if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
155     return FALSE;
156   
157   iface = G_APP_INFO_GET_IFACE (appinfo1);
158
159   return (* iface->equal) (appinfo1, appinfo2);
160 }
161
162 /**
163  * g_app_info_get_id:
164  * @appinfo: a #GAppInfo.
165  * 
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.
170  *
171  * Note that the returned ID may be %NULL, depending on how
172  * the @appinfo has been constructed.
173  *
174  * Returns: a string containing the application's ID.
175  **/
176 const char *
177 g_app_info_get_id (GAppInfo *appinfo)
178 {
179   GAppInfoIface *iface;
180   
181   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
182
183   iface = G_APP_INFO_GET_IFACE (appinfo);
184
185   return (* iface->get_id) (appinfo);
186 }
187
188 /**
189  * g_app_info_get_name:
190  * @appinfo: a #GAppInfo.
191  * 
192  * Gets the installed name of the application. 
193  *
194  * Returns: the name of the application for @appinfo.
195  **/
196 const char *
197 g_app_info_get_name (GAppInfo *appinfo)
198 {
199   GAppInfoIface *iface;
200   
201   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
202
203   iface = G_APP_INFO_GET_IFACE (appinfo);
204
205   return (* iface->get_name) (appinfo);
206 }
207
208 /**
209  * g_app_info_get_display_name:
210  * @appinfo: a #GAppInfo.
211  *
212  * Gets the display name of the application. The display name is often more
213  * descriptive to the user than the name itself.
214  *
215  * Returns: the display name of the application for @appinfo, or the name if
216  * no display name is available.
217  *
218  * Since: 2.24
219  **/
220 const char *
221 g_app_info_get_display_name (GAppInfo *appinfo)
222 {
223   GAppInfoIface *iface;
224
225   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
226
227   iface = G_APP_INFO_GET_IFACE (appinfo);
228
229   if (iface->get_display_name == NULL)
230     return (* iface->get_name) (appinfo);
231
232   return (* iface->get_display_name) (appinfo);
233 }
234
235 /**
236  * g_app_info_get_description:
237  * @appinfo: a #GAppInfo.
238  * 
239  * Gets a human-readable description of an installed application.
240  *
241  * Returns: a string containing a description of the 
242  * application @appinfo, or %NULL if none. 
243  **/
244 const char *
245 g_app_info_get_description (GAppInfo *appinfo)
246 {
247   GAppInfoIface *iface;
248   
249   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
250
251   iface = G_APP_INFO_GET_IFACE (appinfo);
252
253   return (* iface->get_description) (appinfo);
254 }
255
256 /**
257  * g_app_info_get_executable:
258  * @appinfo: a #GAppInfo
259  * 
260  * Gets the executable's name for the installed application.
261  *
262  * Returns: (type filename): a string containing the @appinfo's application
263  * binaries name
264  **/
265 const char *
266 g_app_info_get_executable (GAppInfo *appinfo)
267 {
268   GAppInfoIface *iface;
269   
270   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
271
272   iface = G_APP_INFO_GET_IFACE (appinfo);
273
274   return (* iface->get_executable) (appinfo);
275 }
276
277
278 /**
279  * g_app_info_get_commandline:
280  * @appinfo: a #GAppInfo
281  * 
282  * Gets the commandline with which the application will be
283  * started.  
284  *
285  * Returns: (type filename): a string containing the @appinfo's commandline,
286  *     or %NULL if this information is not available
287  *
288  * Since: 2.20
289  **/
290 const char *
291 g_app_info_get_commandline (GAppInfo *appinfo)
292 {
293   GAppInfoIface *iface;
294   
295   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
296
297   iface = G_APP_INFO_GET_IFACE (appinfo);
298
299   if (iface->get_commandline)
300     return (* iface->get_commandline) (appinfo);
301  
302   return NULL;
303 }
304
305 /**
306  * g_app_info_set_as_default_for_type:
307  * @appinfo: a #GAppInfo.
308  * @content_type: the content type.
309  * @error: a #GError.
310  * 
311  * Sets the application as the default handler for a given type.
312  *
313  * Returns: %TRUE on success, %FALSE on error.
314  **/
315 gboolean
316 g_app_info_set_as_default_for_type (GAppInfo    *appinfo,
317                                     const char  *content_type,
318                                     GError     **error)
319 {
320   GAppInfoIface *iface;
321   
322   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
323   g_return_val_if_fail (content_type != NULL, FALSE);
324
325   iface = G_APP_INFO_GET_IFACE (appinfo);
326
327   return (* iface->set_as_default_for_type) (appinfo, content_type, error);
328 }
329
330 /**
331  * g_app_info_set_as_last_used_for_type:
332  * @appinfo: a #GAppInfo.
333  * @content_type: the content type.
334  * @error: a #GError.
335  *
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.
340  *
341  * Returns: %TRUE on success, %FALSE on error.
342  **/
343 gboolean
344 g_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
345                                       const char  *content_type,
346                                       GError     **error)
347 {
348   GAppInfoIface *iface;
349   
350   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
351   g_return_val_if_fail (content_type != NULL, FALSE);
352
353   iface = G_APP_INFO_GET_IFACE (appinfo);
354
355   return (* iface->set_as_last_used_for_type) (appinfo, content_type, error);
356 }
357
358 /**
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.
364  * 
365  * Sets the application as the default handler for the given file extension.
366  *
367  * Returns: %TRUE on success, %FALSE on error.
368  **/
369 gboolean
370 g_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
371                                          const char  *extension,
372                                          GError     **error)
373 {
374   GAppInfoIface *iface;
375   
376   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
377   g_return_val_if_fail (extension != NULL, FALSE);
378
379   iface = G_APP_INFO_GET_IFACE (appinfo);
380
381   if (iface->set_as_default_for_extension)
382     return (* iface->set_as_default_for_extension) (appinfo, extension, error);
383
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;
387 }
388
389
390 /**
391  * g_app_info_add_supports_type:
392  * @appinfo: a #GAppInfo.
393  * @content_type: a string.
394  * @error: a #GError.
395  * 
396  * Adds a content type to the application information to indicate the 
397  * application is capable of opening files with the given content type.
398  *
399  * Returns: %TRUE on success, %FALSE on error.
400  **/
401 gboolean
402 g_app_info_add_supports_type (GAppInfo    *appinfo,
403                               const char  *content_type,
404                               GError     **error)
405 {
406   GAppInfoIface *iface;
407   
408   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
409   g_return_val_if_fail (content_type != NULL, FALSE);
410
411   iface = G_APP_INFO_GET_IFACE (appinfo);
412
413   if (iface->add_supports_type)
414     return (* iface->add_supports_type) (appinfo, content_type, error);
415
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");
419
420   return FALSE;
421 }
422
423
424 /**
425  * g_app_info_can_remove_supports_type:
426  * @appinfo: a #GAppInfo.
427  * 
428  * Checks if a supported content type can be removed from an application.
429  *
430  * Returns: %TRUE if it is possible to remove supported 
431  *     content types from a given @appinfo, %FALSE if not.
432  **/
433 gboolean
434 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
435 {
436   GAppInfoIface *iface;
437   
438   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
439
440   iface = G_APP_INFO_GET_IFACE (appinfo);
441
442   if (iface->can_remove_supports_type)
443     return (* iface->can_remove_supports_type) (appinfo);
444
445   return FALSE;
446 }
447
448
449 /**
450  * g_app_info_remove_supports_type:
451  * @appinfo: a #GAppInfo.
452  * @content_type: a string.
453  * @error: a #GError.
454  *
455  * Removes a supported type from an application, if possible.
456  * 
457  * Returns: %TRUE on success, %FALSE on error.
458  **/
459 gboolean
460 g_app_info_remove_supports_type (GAppInfo    *appinfo,
461                                  const char  *content_type,
462                                  GError     **error)
463 {
464   GAppInfoIface *iface;
465   
466   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
467   g_return_val_if_fail (content_type != NULL, FALSE);
468
469   iface = G_APP_INFO_GET_IFACE (appinfo);
470
471   if (iface->remove_supports_type)
472     return (* iface->remove_supports_type) (appinfo, content_type, error);
473
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");
477
478   return FALSE;
479 }
480
481 /**
482  * g_app_info_get_supported_types:
483  * @appinfo: a #GAppInfo that can handle files
484  *
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.
491  *
492  * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
493  *    a list of content types.
494  *
495  * Since: 2.34
496  */
497 const char **
498 g_app_info_get_supported_types (GAppInfo *appinfo)
499 {
500   GAppInfoIface *iface;
501
502   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
503
504   iface = G_APP_INFO_GET_IFACE (appinfo);
505
506   if (iface->get_supported_types)
507     return iface->get_supported_types (appinfo);
508   else
509     return NULL;
510 }
511
512
513 /**
514  * g_app_info_get_icon:
515  * @appinfo: a #GAppInfo.
516  * 
517  * Gets the icon for the application.
518  *
519  * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
520  * if there is no default icon.
521  **/
522 GIcon *
523 g_app_info_get_icon (GAppInfo *appinfo)
524 {
525   GAppInfoIface *iface;
526   
527   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
528
529   iface = G_APP_INFO_GET_IFACE (appinfo);
530
531   return (* iface->get_icon) (appinfo);
532 }
533
534
535 /**
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
541  * 
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.
546  *
547  * To launch the application without arguments pass a %NULL @files list.
548  *
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.
552  *
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.
557  *
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().
561  *
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.
569  *
570  * Returns: %TRUE on successful launch, %FALSE otherwise.
571  **/
572 gboolean
573 g_app_info_launch (GAppInfo           *appinfo,
574                    GList              *files,
575                    GAppLaunchContext  *launch_context,
576                    GError            **error)
577 {
578   GAppInfoIface *iface;
579   
580   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
581
582   iface = G_APP_INFO_GET_IFACE (appinfo);
583
584   return (* iface->launch) (appinfo, files, launch_context, error);
585 }
586
587
588 /**
589  * g_app_info_supports_uris:
590  * @appinfo: a #GAppInfo.
591  * 
592  * Checks if the application supports reading files and directories from URIs.
593  *
594  * Returns: %TRUE if the @appinfo supports URIs.
595  **/
596 gboolean
597 g_app_info_supports_uris (GAppInfo *appinfo)
598 {
599   GAppInfoIface *iface;
600   
601   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
602
603   iface = G_APP_INFO_GET_IFACE (appinfo);
604
605   return (* iface->supports_uris) (appinfo);
606 }
607
608
609 /**
610  * g_app_info_supports_files:
611  * @appinfo: a #GAppInfo.
612  * 
613  * Checks if the application accepts files as arguments.
614  *
615  * Returns: %TRUE if the @appinfo supports files.
616  **/
617 gboolean
618 g_app_info_supports_files (GAppInfo *appinfo)
619 {
620   GAppInfoIface *iface;
621   
622   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
623
624   iface = G_APP_INFO_GET_IFACE (appinfo);
625
626   return (* iface->supports_files) (appinfo);
627 }
628
629
630 /**
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
636  * 
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.
641  *
642  * To launch the application without arguments pass a %NULL @uris list.
643  *
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.
647  *
648  * Returns: %TRUE on successful launch, %FALSE otherwise.
649  **/
650 gboolean
651 g_app_info_launch_uris (GAppInfo           *appinfo,
652                         GList              *uris,
653                         GAppLaunchContext  *launch_context,
654                         GError            **error)
655 {
656   GAppInfoIface *iface;
657   
658   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
659
660   iface = G_APP_INFO_GET_IFACE (appinfo);
661
662   return (* iface->launch_uris) (appinfo, uris, launch_context, error);
663 }
664
665
666 /**
667  * g_app_info_should_show:
668  * @appinfo: a #GAppInfo.
669  *
670  * Checks if the application info should be shown in menus that 
671  * list available applications.
672  * 
673  * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
674  **/
675 gboolean
676 g_app_info_should_show (GAppInfo *appinfo)
677 {
678   GAppInfoIface *iface;
679   
680   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
681
682   iface = G_APP_INFO_GET_IFACE (appinfo);
683
684   return (* iface->should_show) (appinfo);
685 }
686
687 static gboolean
688 launch_default_for_uri (const char         *uri,
689                         GAppLaunchContext  *context,
690                         GError            **error)
691 {
692   char *uri_scheme;
693   GAppInfo *app_info = NULL;
694   GList l;
695   gboolean res;
696
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.
700    */
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);
705
706   if (!app_info)
707     {
708       GFile *file;
709
710       file = g_file_new_for_uri (uri);
711       app_info = g_file_query_default_handler (file, NULL, error);
712       g_object_unref (file);
713     }
714
715   if (app_info == NULL)
716     return FALSE;
717
718   l.data = (char *)uri;
719   l.next = l.prev = NULL;
720   res = g_app_info_launch_uris (app_info, &l, context, error);
721
722   g_object_unref (app_info);
723
724   return res;
725 }
726
727 /**
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
732  *
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.
737  * 
738  * Returns: %TRUE on success, %FALSE on error.
739  **/
740 gboolean
741 g_app_info_launch_default_for_uri (const char         *uri,
742                                    GAppLaunchContext  *launch_context,
743                                    GError            **error)
744 {
745   if (launch_default_for_uri (uri, launch_context, error))
746     return TRUE;
747
748 #ifdef G_OS_UNIX
749   if (glib_should_use_portal ())
750     {
751       const char *parent_window = NULL;
752
753       /* Reset any error previously set by launch_default_for_uri */
754       g_clear_error (error);
755
756       if (launch_context && launch_context->priv->envp)
757         parent_window = g_environ_getenv (launch_context->priv->envp, "PARENT_WINDOW_ID");
758
759       return g_openuri_portal_open_uri (uri, parent_window, error);
760
761     }
762 #endif
763
764   return FALSE;
765 }
766
767 /**
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
774  *
775  * Async version of g_app_info_launch_default_for_uri().
776  *
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.
781  *
782  * Since: 2.50
783  */
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)
790 {
791   gboolean res;
792   GError *error = NULL;
793   GTask *task;
794
795   res = launch_default_for_uri (uri, context, &error);
796
797 #ifdef G_OS_UNIX
798   if (!res && glib_should_use_portal ())
799     {
800       const  char *parent_window = NULL;
801
802       if (context && context->priv->envp)
803         parent_window = g_environ_getenv (context->priv->envp, "PARENT_WINDOW_ID");
804
805       g_openuri_portal_open_uri_async (uri, parent_window, cancellable, callback, user_data);
806       return;
807     }
808 #endif
809
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);
815
816   g_object_unref (task);
817 }
818
819 /**
820  * g_app_info_launch_default_for_uri_finish:
821  * @result: a #GAsyncResult
822  * @error: (nullable): return location for an error, or %NULL
823  *
824  * Finishes an asynchronous launch-default-for-uri operation.
825  *
826  * Returns: %TRUE if the launch was successful, %FALSE if @error is set
827  *
828  * Since: 2.50
829  */
830 gboolean
831 g_app_info_launch_default_for_uri_finish (GAsyncResult  *result,
832                                           GError       **error)
833 {
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
839 }
840
841 /**
842  * g_app_info_can_delete:
843  * @appinfo: a #GAppInfo
844  *
845  * Obtains the information whether the #GAppInfo can be deleted.
846  * See g_app_info_delete().
847  *
848  * Returns: %TRUE if @appinfo can be deleted
849  *
850  * Since: 2.20
851  */
852 gboolean
853 g_app_info_can_delete (GAppInfo *appinfo)
854 {
855   GAppInfoIface *iface;
856   
857   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
858
859   iface = G_APP_INFO_GET_IFACE (appinfo);
860
861   if (iface->can_delete)
862     return (* iface->can_delete) (appinfo);
863  
864   return FALSE; 
865 }
866
867
868 /**
869  * g_app_info_delete:
870  * @appinfo: a #GAppInfo
871  *
872  * Tries to delete a #GAppInfo.
873  *
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().
877  *
878  * Virtual: do_delete
879  * Returns: %TRUE if @appinfo has been deleted
880  *
881  * Since: 2.20
882  */
883 gboolean
884 g_app_info_delete (GAppInfo *appinfo)
885 {
886   GAppInfoIface *iface;
887   
888   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
889
890   iface = G_APP_INFO_GET_IFACE (appinfo);
891
892   if (iface->do_delete)
893     return (* iface->do_delete) (appinfo);
894  
895   return FALSE; 
896 }
897
898
899 enum {
900   LAUNCH_FAILED,
901   LAUNCHED,
902   LAST_SIGNAL
903 };
904
905 static guint signals[LAST_SIGNAL] = { 0 };
906
907 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
908
909 /**
910  * g_app_launch_context_new:
911  * 
912  * Creates a new application launch context. This is not normally used,
913  * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
914  *
915  * Returns: a #GAppLaunchContext.
916  **/
917 GAppLaunchContext *
918 g_app_launch_context_new (void)
919 {
920   return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
921 }
922
923 static void
924 g_app_launch_context_finalize (GObject *object)
925 {
926   GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
927
928   g_strfreev (context->priv->envp);
929
930   G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
931 }
932
933 static void
934 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
935 {
936   GObjectClass *object_class = G_OBJECT_CLASS (klass);
937
938   object_class->finalize = g_app_launch_context_finalize;
939
940   /**
941    * GAppLaunchContext::launch-failed:
942    * @context: the object emitting the signal
943    * @startup_notify_id: the startup notification id for the failed launch
944    *
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.
948    *
949    * Since: 2.36
950    */
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);
957
958   /**
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
963    *
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.
969    *
970    * Since: 2.36
971    */
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);
979 }
980
981 static void
982 g_app_launch_context_init (GAppLaunchContext *context)
983 {
984   context->priv = g_app_launch_context_get_instance_private (context);
985 }
986
987 /**
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.
992  *
993  * Arranges for @variable to be set to @value in the child's
994  * environment when @context is used to launch an application.
995  *
996  * Since: 2.32
997  */
998 void
999 g_app_launch_context_setenv (GAppLaunchContext *context,
1000                              const char        *variable,
1001                              const char        *value)
1002 {
1003   if (!context->priv->envp)
1004     context->priv->envp = g_get_environ ();
1005
1006   context->priv->envp =
1007     g_environ_setenv (context->priv->envp, variable, value, TRUE);
1008 }
1009
1010 /**
1011  * g_app_launch_context_unsetenv:
1012  * @context: a #GAppLaunchContext
1013  * @variable: the environment variable to remove
1014  *
1015  * Arranges for @variable to be unset in the child's environment
1016  * when @context is used to launch an application.
1017  *
1018  * Since: 2.32
1019  */
1020 void
1021 g_app_launch_context_unsetenv (GAppLaunchContext *context,
1022                                const char        *variable)
1023 {
1024   if (!context->priv->envp)
1025     context->priv->envp = g_get_environ ();
1026
1027   context->priv->envp =
1028     g_environ_unsetenv (context->priv->envp, variable);
1029 }
1030
1031 /**
1032  * g_app_launch_context_get_environment:
1033  * @context: a #GAppLaunchContext
1034  *
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`.
1039  *
1040  * Returns: (array zero-terminated=1) (transfer full): the
1041  *     child's environment
1042  *
1043  * Since: 2.32
1044  */
1045 char **
1046 g_app_launch_context_get_environment (GAppLaunchContext *context)
1047 {
1048   if (!context->priv->envp)
1049     context->priv->envp = g_get_environ ();
1050
1051   return g_strdupv (context->priv->envp);
1052 }
1053
1054 /**
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
1059  *
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.
1063  *
1064  * Returns: a display string for the display.
1065  */
1066 char *
1067 g_app_launch_context_get_display (GAppLaunchContext *context,
1068                                   GAppInfo          *info,
1069                                   GList             *files)
1070 {
1071   GAppLaunchContextClass *class;
1072
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);
1075
1076   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1077
1078   if (class->get_display == NULL)
1079     return NULL;
1080
1081   return class->get_display (context, info, files);
1082 }
1083
1084 /**
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
1089  * 
1090  * Initiates startup notification for the application and returns the
1091  * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
1092  *
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").
1095  *
1096  * Returns: a startup notification ID for the application, or %NULL if
1097  *     not supported.
1098  **/
1099 char *
1100 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
1101                                             GAppInfo          *info,
1102                                             GList             *files)
1103 {
1104   GAppLaunchContextClass *class;
1105
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);
1108
1109   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1110
1111   if (class->get_startup_notify_id == NULL)
1112     return NULL;
1113
1114   return class->get_startup_notify_id (context, info, files);
1115 }
1116
1117
1118 /**
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().
1122  *
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().
1125  * 
1126  **/
1127 void
1128 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1129                                     const char        *startup_notify_id)
1130 {
1131   g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1132   g_return_if_fail (startup_notify_id != NULL);
1133
1134   g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1135 }
1136
1137
1138 /**
1139  * SECTION:gappinfomonitor
1140  * @short_description: Monitor application information for changes
1141  *
1142  * #GAppInfoMonitor is a very simple object used for monitoring the app
1143  * info database for changes (ie: newly installed or removed
1144  * applications).
1145  *
1146  * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1147  * to the "changed" signal.
1148  *
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.
1159  *
1160  * Since: 2.40
1161  **/
1162
1163 /**
1164  * GAppInfoMonitor:
1165  *
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.
1168  *
1169  * Since: 2.40
1170  **/
1171
1172 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1173
1174 struct _GAppInfoMonitor
1175 {
1176   GObject parent_instance;
1177   GMainContext *context;
1178 };
1179
1180 struct _GAppInfoMonitorClass
1181 {
1182   GObjectClass parent_class;
1183 };
1184
1185 static GContextSpecificGroup g_app_info_monitor_group;
1186 static guint                 g_app_info_monitor_changed_signal;
1187
1188 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1189
1190 static void
1191 g_app_info_monitor_finalize (GObject *object)
1192 {
1193   GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1194
1195   g_context_specific_group_remove (&g_app_info_monitor_group, monitor->context, monitor, NULL);
1196
1197   G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1198 }
1199
1200 static void
1201 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1202 {
1203 }
1204
1205 static void
1206 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1207 {
1208   GObjectClass *object_class = G_OBJECT_CLASS (class);
1209
1210   /**
1211    * GAppInfoMonitor::changed:
1212    *
1213    * Signal emitted when the app info database for changes (ie: newly installed
1214    * or removed applications).
1215    **/
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);
1218
1219   object_class->finalize = g_app_info_monitor_finalize;
1220 }
1221
1222 /**
1223  * g_app_info_monitor_get:
1224  *
1225  * Gets the #GAppInfoMonitor for the current thread-default main
1226  * context.
1227  *
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.
1231  *
1232  * You must only call g_object_unref() on the return value from under
1233  * the same main context as you created it.
1234  *
1235  * Returns: (transfer full): a reference to a #GAppInfoMonitor
1236  *
1237  * Since: 2.40
1238  **/
1239 GAppInfoMonitor *
1240 g_app_info_monitor_get (void)
1241 {
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);
1246 }
1247
1248 void
1249 g_app_info_monitor_fire (void)
1250 {
1251   g_context_specific_group_emit (&g_app_info_monitor_group, g_app_info_monitor_changed_signal);
1252 }