[kdbus] Do not set body message if signature field is empty
[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 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
26 #include "glibintl.h"
27 #include <gioerror.h>
28 #include <gfile.h>
29
30
31 /**
32  * SECTION:gappinfo
33  * @short_description: Application information and launch contexts
34  * @include: gio/gio.h
35  * @see_also: #GAppInfoMonitor
36  * 
37  * #GAppInfo and #GAppLaunchContext are used for describing and launching
38  * applications installed on the system.
39  *
40  * As of GLib 2.20, URIs will always be converted to POSIX paths
41  * (using g_file_get_path()) when using g_app_info_launch() even if
42  * the application requested an URI and not a POSIX path. For example
43  * for an desktop-file based application with Exec key `totem
44  * %U` and a single URI, `sftp://foo/file.avi`, then
45  * `/home/user/.gvfs/sftp on foo/file.avi` will be passed. This will
46  * only work if a set of suitable GIO extensions (such as gvfs 2.26
47  * compiled with FUSE support), is available and operational; if this
48  * is not the case, the URI will be passed unmodified to the application.
49  * Some URIs, such as `mailto:`, of course cannot be mapped to a POSIX
50  * path (in gvfs there's no FUSE mount for it); such URIs will be
51  * passed unmodified to the application.
52  *
53  * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
54  * back to the GIO URI in the #GFile constructors (since gvfs
55  * implements the #GVfs extension point). As such, if the application
56  * needs to examine the URI, it needs to use g_file_get_uri() or
57  * similar on #GFile. In other words, an application cannot assume
58  * that the URI passed to e.g. g_file_new_for_commandline_arg() is
59  * equal to the result of g_file_get_uri(). The following snippet
60  * illustrates this:
61  *
62  * |[ 
63  * GFile *f;
64  * char *uri;
65  *
66  * file = g_file_new_for_commandline_arg (uri_from_commandline);
67  *
68  * uri = g_file_get_uri (file);
69  * strcmp (uri, uri_from_commandline) == 0;
70  * g_free (uri);
71  *
72  * if (g_file_has_uri_scheme (file, "cdda"))
73  *   {
74  *     // do something special with uri
75  *   }
76  * g_object_unref (file);
77  * ]|
78  *
79  * This code will work when both `cdda://sr0/Track 1.wav` and
80  * `/home/user/.gvfs/cdda on sr0/Track 1.wav` is passed to the
81  * application. It should be noted that it's generally not safe
82  * for applications to rely on the format of a particular URIs.
83  * Different launcher applications (e.g. file managers) may have
84  * different ideas of what a given URI means.
85  */
86
87 typedef GAppInfoIface GAppInfoInterface;
88 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
89
90 static void
91 g_app_info_default_init (GAppInfoInterface *iface)
92 {
93 }
94
95
96 /**
97  * g_app_info_dup:
98  * @appinfo: a #GAppInfo.
99  * 
100  * Creates a duplicate of a #GAppInfo.
101  *
102  * Returns: (transfer full): a duplicate of @appinfo.
103  **/
104 GAppInfo *
105 g_app_info_dup (GAppInfo *appinfo)
106 {
107   GAppInfoIface *iface;
108
109   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
110
111   iface = G_APP_INFO_GET_IFACE (appinfo);
112
113   return (* iface->dup) (appinfo);
114 }
115
116 /**
117  * g_app_info_equal:
118  * @appinfo1: the first #GAppInfo.
119  * @appinfo2: the second #GAppInfo.
120  *
121  * Checks if two #GAppInfos are equal.
122  *
123  * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
124  **/
125 gboolean
126 g_app_info_equal (GAppInfo *appinfo1,
127                   GAppInfo *appinfo2)
128 {
129   GAppInfoIface *iface;
130
131   g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
132   g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
133
134   if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
135     return FALSE;
136   
137   iface = G_APP_INFO_GET_IFACE (appinfo1);
138
139   return (* iface->equal) (appinfo1, appinfo2);
140 }
141
142 /**
143  * g_app_info_get_id:
144  * @appinfo: a #GAppInfo.
145  * 
146  * Gets the ID of an application. An id is a string that
147  * identifies the application. The exact format of the id is
148  * platform dependent. For instance, on Unix this is the
149  * desktop file id from the xdg menu specification.
150  *
151  * Note that the returned ID may be %NULL, depending on how
152  * the @appinfo has been constructed.
153  *
154  * Returns: a string containing the application's ID.
155  **/
156 const char *
157 g_app_info_get_id (GAppInfo *appinfo)
158 {
159   GAppInfoIface *iface;
160   
161   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
162
163   iface = G_APP_INFO_GET_IFACE (appinfo);
164
165   return (* iface->get_id) (appinfo);
166 }
167
168 /**
169  * g_app_info_get_name:
170  * @appinfo: a #GAppInfo.
171  * 
172  * Gets the installed name of the application. 
173  *
174  * Returns: the name of the application for @appinfo.
175  **/
176 const char *
177 g_app_info_get_name (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_name) (appinfo);
186 }
187
188 /**
189  * g_app_info_get_display_name:
190  * @appinfo: a #GAppInfo.
191  *
192  * Gets the display name of the application. The display name is often more
193  * descriptive to the user than the name itself.
194  *
195  * Returns: the display name of the application for @appinfo, or the name if
196  * no display name is available.
197  *
198  * Since: 2.24
199  **/
200 const char *
201 g_app_info_get_display_name (GAppInfo *appinfo)
202 {
203   GAppInfoIface *iface;
204
205   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
206
207   iface = G_APP_INFO_GET_IFACE (appinfo);
208
209   if (iface->get_display_name == NULL)
210     return (* iface->get_name) (appinfo);
211
212   return (* iface->get_display_name) (appinfo);
213 }
214
215 /**
216  * g_app_info_get_description:
217  * @appinfo: a #GAppInfo.
218  * 
219  * Gets a human-readable description of an installed application.
220  *
221  * Returns: a string containing a description of the 
222  * application @appinfo, or %NULL if none. 
223  **/
224 const char *
225 g_app_info_get_description (GAppInfo *appinfo)
226 {
227   GAppInfoIface *iface;
228   
229   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
230
231   iface = G_APP_INFO_GET_IFACE (appinfo);
232
233   return (* iface->get_description) (appinfo);
234 }
235
236 /**
237  * g_app_info_get_executable:
238  * @appinfo: a #GAppInfo
239  * 
240  * Gets the executable's name for the installed application.
241  *
242  * Returns: a string containing the @appinfo's application 
243  * binaries name
244  **/
245 const char *
246 g_app_info_get_executable (GAppInfo *appinfo)
247 {
248   GAppInfoIface *iface;
249   
250   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
251
252   iface = G_APP_INFO_GET_IFACE (appinfo);
253
254   return (* iface->get_executable) (appinfo);
255 }
256
257
258 /**
259  * g_app_info_get_commandline:
260  * @appinfo: a #GAppInfo
261  * 
262  * Gets the commandline with which the application will be
263  * started.  
264  *
265  * Returns: a string containing the @appinfo's commandline, 
266  *     or %NULL if this information is not available
267  *
268  * Since: 2.20
269  **/
270 const char *
271 g_app_info_get_commandline (GAppInfo *appinfo)
272 {
273   GAppInfoIface *iface;
274   
275   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
276
277   iface = G_APP_INFO_GET_IFACE (appinfo);
278
279   if (iface->get_commandline)
280     return (* iface->get_commandline) (appinfo);
281  
282   return NULL;
283 }
284
285 /**
286  * g_app_info_set_as_default_for_type:
287  * @appinfo: a #GAppInfo.
288  * @content_type: the content type.
289  * @error: a #GError.
290  * 
291  * Sets the application as the default handler for a given type.
292  *
293  * Returns: %TRUE on success, %FALSE on error.
294  **/
295 gboolean
296 g_app_info_set_as_default_for_type (GAppInfo    *appinfo,
297                                     const char  *content_type,
298                                     GError     **error)
299 {
300   GAppInfoIface *iface;
301   
302   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
303   g_return_val_if_fail (content_type != NULL, FALSE);
304
305   iface = G_APP_INFO_GET_IFACE (appinfo);
306
307   return (* iface->set_as_default_for_type) (appinfo, content_type, error);
308 }
309
310 /**
311  * g_app_info_set_as_last_used_for_type:
312  * @appinfo: a #GAppInfo.
313  * @content_type: the content type.
314  * @error: a #GError.
315  *
316  * Sets the application as the last used application for a given type.
317  * This will make the application appear as first in the list returned
318  * by g_app_info_get_recommended_for_type(), regardless of the default
319  * application for that content type.
320  *
321  * Returns: %TRUE on success, %FALSE on error.
322  **/
323 gboolean
324 g_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
325                                       const char  *content_type,
326                                       GError     **error)
327 {
328   GAppInfoIface *iface;
329   
330   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
331   g_return_val_if_fail (content_type != NULL, FALSE);
332
333   iface = G_APP_INFO_GET_IFACE (appinfo);
334
335   return (* iface->set_as_last_used_for_type) (appinfo, content_type, error);
336 }
337
338 /**
339  * g_app_info_set_as_default_for_extension:
340  * @appinfo: a #GAppInfo.
341  * @extension: a string containing the file extension (without the dot).
342  * @error: a #GError.
343  * 
344  * Sets the application as the default handler for the given file extension.
345  *
346  * Returns: %TRUE on success, %FALSE on error.
347  **/
348 gboolean
349 g_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
350                                          const char  *extension,
351                                          GError     **error)
352 {
353   GAppInfoIface *iface;
354   
355   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
356   g_return_val_if_fail (extension != NULL, FALSE);
357
358   iface = G_APP_INFO_GET_IFACE (appinfo);
359
360   if (iface->set_as_default_for_extension)
361     return (* iface->set_as_default_for_extension) (appinfo, extension, error);
362
363   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
364                        "g_app_info_set_as_default_for_extension not supported yet");
365   return FALSE;
366 }
367
368
369 /**
370  * g_app_info_add_supports_type:
371  * @appinfo: a #GAppInfo.
372  * @content_type: a string.
373  * @error: a #GError.
374  * 
375  * Adds a content type to the application information to indicate the 
376  * application is capable of opening files with the given content type.
377  *
378  * Returns: %TRUE on success, %FALSE on error.
379  **/
380 gboolean
381 g_app_info_add_supports_type (GAppInfo    *appinfo,
382                               const char  *content_type,
383                               GError     **error)
384 {
385   GAppInfoIface *iface;
386   
387   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
388   g_return_val_if_fail (content_type != NULL, FALSE);
389
390   iface = G_APP_INFO_GET_IFACE (appinfo);
391
392   if (iface->add_supports_type)
393     return (* iface->add_supports_type) (appinfo, content_type, error);
394
395   g_set_error_literal (error, G_IO_ERROR, 
396                        G_IO_ERROR_NOT_SUPPORTED, 
397                        "g_app_info_add_supports_type not supported yet");
398
399   return FALSE;
400 }
401
402
403 /**
404  * g_app_info_can_remove_supports_type:
405  * @appinfo: a #GAppInfo.
406  * 
407  * Checks if a supported content type can be removed from an application.
408  *
409  * Returns: %TRUE if it is possible to remove supported 
410  *     content types from a given @appinfo, %FALSE if not.
411  **/
412 gboolean
413 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
414 {
415   GAppInfoIface *iface;
416   
417   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
418
419   iface = G_APP_INFO_GET_IFACE (appinfo);
420
421   if (iface->can_remove_supports_type)
422     return (* iface->can_remove_supports_type) (appinfo);
423
424   return FALSE;
425 }
426
427
428 /**
429  * g_app_info_remove_supports_type:
430  * @appinfo: a #GAppInfo.
431  * @content_type: a string.
432  * @error: a #GError.
433  *
434  * Removes a supported type from an application, if possible.
435  * 
436  * Returns: %TRUE on success, %FALSE on error.
437  **/
438 gboolean
439 g_app_info_remove_supports_type (GAppInfo    *appinfo,
440                                  const char  *content_type,
441                                  GError     **error)
442 {
443   GAppInfoIface *iface;
444   
445   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
446   g_return_val_if_fail (content_type != NULL, FALSE);
447
448   iface = G_APP_INFO_GET_IFACE (appinfo);
449
450   if (iface->remove_supports_type)
451     return (* iface->remove_supports_type) (appinfo, content_type, error);
452
453   g_set_error_literal (error, G_IO_ERROR, 
454                        G_IO_ERROR_NOT_SUPPORTED, 
455                        "g_app_info_remove_supports_type not supported yet");
456
457   return FALSE;
458 }
459
460 /**
461  * g_app_info_get_supported_types:
462  * @appinfo: a #GAppInfo that can handle files
463  *
464  * Retrieves the list of content types that @app_info claims to support.
465  * If this information is not provided by the environment, this function
466  * will return %NULL.
467  * This function does not take in consideration associations added with
468  * g_app_info_add_supports_type(), but only those exported directly by
469  * the application.
470  *
471  * Returns: (transfer none) (array zero-terminated=1) (element-type utf8):
472  *    a list of content types.
473  *
474  * Since: 2.34
475  */
476 const char **
477 g_app_info_get_supported_types (GAppInfo *appinfo)
478 {
479   GAppInfoIface *iface;
480
481   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
482
483   iface = G_APP_INFO_GET_IFACE (appinfo);
484
485   if (iface->get_supported_types)
486     return iface->get_supported_types (appinfo);
487   else
488     return NULL;
489 }
490
491
492 /**
493  * g_app_info_get_icon:
494  * @appinfo: a #GAppInfo.
495  * 
496  * Gets the icon for the application.
497  *
498  * Returns: (transfer none): the default #GIcon for @appinfo or %NULL
499  * if there is no default icon.
500  **/
501 GIcon *
502 g_app_info_get_icon (GAppInfo *appinfo)
503 {
504   GAppInfoIface *iface;
505   
506   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
507
508   iface = G_APP_INFO_GET_IFACE (appinfo);
509
510   return (* iface->get_icon) (appinfo);
511 }
512
513
514 /**
515  * g_app_info_launch:
516  * @appinfo: a #GAppInfo
517  * @files: (allow-none) (element-type GFile): a #GList of #GFile objects
518  * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
519  * @error: a #GError
520  * 
521  * Launches the application. Passes @files to the launched application
522  * as arguments, using the optional @launch_context to get information
523  * about the details of the launcher (like what screen it is on).
524  * On error, @error will be set accordingly.
525  *
526  * To launch the application without arguments pass a %NULL @files list.
527  *
528  * Note that even if the launch is successful the application launched
529  * can fail to start if it runs into problems during startup. There is
530  * no way to detect this.
531  *
532  * Some URIs can be changed when passed through a GFile (for instance
533  * unsupported URIs with strange formats like mailto:), so if you have
534  * a textual URI you want to pass in as argument, consider using
535  * g_app_info_launch_uris() instead.
536  *
537  * The launched application inherits the environment of the launching
538  * process, but it can be modified with g_app_launch_context_setenv()
539  * and g_app_launch_context_unsetenv().
540  *
541  * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
542  * environment variable with the path of the launched desktop file and
543  * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
544  * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
545  * should it be inherited by further processes. The `DISPLAY` and
546  * `DESKTOP_STARTUP_ID` environment variables are also set, based
547  * on information provided in @launch_context.
548  *
549  * Returns: %TRUE on successful launch, %FALSE otherwise.
550  **/
551 gboolean
552 g_app_info_launch (GAppInfo           *appinfo,
553                    GList              *files,
554                    GAppLaunchContext  *launch_context,
555                    GError            **error)
556 {
557   GAppInfoIface *iface;
558   
559   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
560
561   iface = G_APP_INFO_GET_IFACE (appinfo);
562
563   return (* iface->launch) (appinfo, files, launch_context, error);
564 }
565
566
567 /**
568  * g_app_info_supports_uris:
569  * @appinfo: a #GAppInfo.
570  * 
571  * Checks if the application supports reading files and directories from URIs.
572  *
573  * Returns: %TRUE if the @appinfo supports URIs.
574  **/
575 gboolean
576 g_app_info_supports_uris (GAppInfo *appinfo)
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->supports_uris) (appinfo);
585 }
586
587
588 /**
589  * g_app_info_supports_files:
590  * @appinfo: a #GAppInfo.
591  * 
592  * Checks if the application accepts files as arguments.
593  *
594  * Returns: %TRUE if the @appinfo supports files.
595  **/
596 gboolean
597 g_app_info_supports_files (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_files) (appinfo);
606 }
607
608
609 /**
610  * g_app_info_launch_uris:
611  * @appinfo: a #GAppInfo
612  * @uris: (allow-none) (element-type utf8): a #GList containing URIs to launch.
613  * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
614  * @error: a #GError
615  * 
616  * Launches the application. This passes the @uris to the launched application
617  * as arguments, using the optional @launch_context to get information
618  * about the details of the launcher (like what screen it is on).
619  * On error, @error will be set accordingly.
620  *
621  * To launch the application without arguments pass a %NULL @uris list.
622  *
623  * Note that even if the launch is successful the application launched
624  * can fail to start if it runs into problems during startup. There is
625  * no way to detect this.
626  *
627  * Returns: %TRUE on successful launch, %FALSE otherwise.
628  **/
629 gboolean
630 g_app_info_launch_uris (GAppInfo           *appinfo,
631                         GList              *uris,
632                         GAppLaunchContext  *launch_context,
633                         GError            **error)
634 {
635   GAppInfoIface *iface;
636   
637   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
638
639   iface = G_APP_INFO_GET_IFACE (appinfo);
640
641   return (* iface->launch_uris) (appinfo, uris, launch_context, error);
642 }
643
644
645 /**
646  * g_app_info_should_show:
647  * @appinfo: a #GAppInfo.
648  *
649  * Checks if the application info should be shown in menus that 
650  * list available applications.
651  * 
652  * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
653  **/
654 gboolean
655 g_app_info_should_show (GAppInfo *appinfo)
656 {
657   GAppInfoIface *iface;
658   
659   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
660
661   iface = G_APP_INFO_GET_IFACE (appinfo);
662
663   return (* iface->should_show) (appinfo);
664 }
665
666 /**
667  * g_app_info_launch_default_for_uri:
668  * @uri: the uri to show
669  * @launch_context: (allow-none): an optional #GAppLaunchContext.
670  * @error: a #GError.
671  *
672  * Utility function that launches the default application
673  * registered to handle the specified uri. Synchronous I/O
674  * is done on the uri to detect the type of the file if
675  * required.
676  * 
677  * Returns: %TRUE on success, %FALSE on error.
678  **/
679 gboolean
680 g_app_info_launch_default_for_uri (const char         *uri,
681                                    GAppLaunchContext  *launch_context,
682                                    GError            **error)
683 {
684   char *uri_scheme;
685   GAppInfo *app_info = NULL;
686   GList l;
687   gboolean res;
688
689   /* g_file_query_default_handler() calls
690    * g_app_info_get_default_for_uri_scheme() too, but we have to do it
691    * here anyway in case GFile can't parse @uri correctly.
692    */
693   uri_scheme = g_uri_parse_scheme (uri);
694   if (uri_scheme && uri_scheme[0] != '\0')
695     app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
696   g_free (uri_scheme);
697
698   if (!app_info)
699     {
700       GFile *file;
701
702       file = g_file_new_for_uri (uri);
703       app_info = g_file_query_default_handler (file, NULL, error);
704       g_object_unref (file);
705       if (app_info == NULL)
706         return FALSE;
707
708       /* We still use the original @uri rather than calling
709        * g_file_get_uri(), because GFile might have modified the URI
710        * in ways we don't want (eg, removing the fragment identifier
711        * from a file: URI).
712        */
713     }
714
715   l.data = (char *)uri;
716   l.next = l.prev = NULL;
717   res = g_app_info_launch_uris (app_info, &l,
718                                 launch_context, error);
719
720   g_object_unref (app_info);
721   
722   return res;
723 }
724
725 /**
726  * g_app_info_can_delete:
727  * @appinfo: a #GAppInfo
728  *
729  * Obtains the information whether the #GAppInfo can be deleted.
730  * See g_app_info_delete().
731  *
732  * Returns: %TRUE if @appinfo can be deleted
733  *
734  * Since: 2.20
735  */
736 gboolean
737 g_app_info_can_delete (GAppInfo *appinfo)
738 {
739   GAppInfoIface *iface;
740   
741   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
742
743   iface = G_APP_INFO_GET_IFACE (appinfo);
744
745   if (iface->can_delete)
746     return (* iface->can_delete) (appinfo);
747  
748   return FALSE; 
749 }
750
751
752 /**
753  * g_app_info_delete:
754  * @appinfo: a #GAppInfo
755  *
756  * Tries to delete a #GAppInfo.
757  *
758  * On some platforms, there may be a difference between user-defined
759  * #GAppInfos which can be deleted, and system-wide ones which cannot.
760  * See g_app_info_can_delete().
761  *
762  * Virtual: do_delete
763  * Returns: %TRUE if @appinfo has been deleted
764  *
765  * Since: 2.20
766  */
767 gboolean
768 g_app_info_delete (GAppInfo *appinfo)
769 {
770   GAppInfoIface *iface;
771   
772   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
773
774   iface = G_APP_INFO_GET_IFACE (appinfo);
775
776   if (iface->do_delete)
777     return (* iface->do_delete) (appinfo);
778  
779   return FALSE; 
780 }
781
782
783 enum {
784   LAUNCH_FAILED,
785   LAUNCHED,
786   LAST_SIGNAL
787 };
788
789 struct _GAppLaunchContextPrivate {
790   char **envp;
791 };
792
793 static guint signals[LAST_SIGNAL] = { 0 };
794
795 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
796
797 /**
798  * g_app_launch_context_new:
799  * 
800  * Creates a new application launch context. This is not normally used,
801  * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
802  *
803  * Returns: a #GAppLaunchContext.
804  **/
805 GAppLaunchContext *
806 g_app_launch_context_new (void)
807 {
808   return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
809 }
810
811 static void
812 g_app_launch_context_finalize (GObject *object)
813 {
814   GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
815
816   g_strfreev (context->priv->envp);
817
818   G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
819 }
820
821 static void
822 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
823 {
824   GObjectClass *object_class = G_OBJECT_CLASS (klass);
825
826   object_class->finalize = g_app_launch_context_finalize;
827
828   /*
829    * GAppLaunchContext::launch-failed:
830    * @context: the object emitting the signal
831    * @startup_notify_id: the startup notification id for the failed launch
832    *
833    * The ::launch-failed signal is emitted when a #GAppInfo launch
834    * fails. The startup notification id is provided, so that the launcher
835    * can cancel the startup notification.
836    *
837    * Since: 2.36
838    */
839   signals[LAUNCH_FAILED] = g_signal_new ("launch-failed",
840                                          G_OBJECT_CLASS_TYPE (object_class),
841                                          G_SIGNAL_RUN_LAST,
842                                          G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
843                                          NULL, NULL, NULL,
844                                          G_TYPE_NONE, 1, G_TYPE_STRING);
845
846   /*
847    * GAppLaunchContext::launched:
848    * @context: the object emitting the signal
849    * @info: the #GAppInfo that was just launched
850    * @platform_data: additional platform-specific data for this launch
851    *
852    * The ::launched signal is emitted when a #GAppInfo is successfully
853    * launched. The @platform_data is an GVariant dictionary mapping
854    * strings to variants (ie a{sv}), which contains additional,
855    * platform-specific data about this launch. On UNIX, at least the
856    * "pid" and "startup-notification-id" keys will be present.
857    *
858    * Since: 2.36
859    */
860   signals[LAUNCHED] = g_signal_new ("launched",
861                                     G_OBJECT_CLASS_TYPE (object_class),
862                                     G_SIGNAL_RUN_LAST,
863                                     G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
864                                     NULL, NULL, NULL,
865                                     G_TYPE_NONE, 2,
866                                     G_TYPE_APP_INFO, G_TYPE_VARIANT);
867 }
868
869 static void
870 g_app_launch_context_init (GAppLaunchContext *context)
871 {
872   context->priv = g_app_launch_context_get_instance_private (context);
873 }
874
875 /**
876  * g_app_launch_context_setenv:
877  * @context: a #GAppLaunchContext
878  * @variable: the environment variable to set
879  * @value: the value for to set the variable to.
880  *
881  * Arranges for @variable to be set to @value in the child's
882  * environment when @context is used to launch an application.
883  *
884  * Since: 2.32
885  */
886 void
887 g_app_launch_context_setenv (GAppLaunchContext *context,
888                              const char        *variable,
889                              const char        *value)
890 {
891   if (!context->priv->envp)
892     context->priv->envp = g_get_environ ();
893
894   context->priv->envp =
895     g_environ_setenv (context->priv->envp, variable, value, TRUE);
896 }
897
898 /**
899  * g_app_launch_context_unsetenv:
900  * @context: a #GAppLaunchContext
901  * @variable: the environment variable to remove
902  *
903  * Arranges for @variable to be unset in the child's environment
904  * when @context is used to launch an application.
905  *
906  * Since: 2.32
907  */
908 void
909 g_app_launch_context_unsetenv (GAppLaunchContext *context,
910                                const char        *variable)
911 {
912   if (!context->priv->envp)
913     context->priv->envp = g_get_environ ();
914
915   context->priv->envp =
916     g_environ_unsetenv (context->priv->envp, variable);
917 }
918
919 /**
920  * g_app_launch_context_get_environment:
921  * @context: a #GAppLaunchContext
922  *
923  * Gets the complete environment variable list to be passed to
924  * the child process when @context is used to launch an application.
925  * This is a %NULL-terminated array of strings, where each string has
926  * the form `KEY=VALUE`.
927  *
928  * Returns: (array zero-terminated=1) (transfer full): the
929  *     child's environment
930  *
931  * Since: 2.32
932  */
933 char **
934 g_app_launch_context_get_environment (GAppLaunchContext *context)
935 {
936   if (!context->priv->envp)
937     context->priv->envp = g_get_environ ();
938
939   return g_strdupv (context->priv->envp);
940 }
941
942 /**
943  * g_app_launch_context_get_display:
944  * @context: a #GAppLaunchContext
945  * @info: a #GAppInfo
946  * @files: (element-type GFile): a #GList of #GFile objects
947  *
948  * Gets the display string for the @context. This is used to ensure new
949  * applications are started on the same display as the launching
950  * application, by setting the `DISPLAY` environment variable.
951  *
952  * Returns: a display string for the display.
953  */
954 char *
955 g_app_launch_context_get_display (GAppLaunchContext *context,
956                                   GAppInfo          *info,
957                                   GList             *files)
958 {
959   GAppLaunchContextClass *class;
960
961   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
962   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
963
964   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
965
966   if (class->get_display == NULL)
967     return NULL;
968
969   return class->get_display (context, info, files);
970 }
971
972 /**
973  * g_app_launch_context_get_startup_notify_id:
974  * @context: a #GAppLaunchContext
975  * @info: a #GAppInfo
976  * @files: (element-type GFile): a #GList of of #GFile objects
977  * 
978  * Initiates startup notification for the application and returns the
979  * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
980  *
981  * Startup notification IDs are defined in the 
982  * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
983  *
984  * Returns: a startup notification ID for the application, or %NULL if
985  *     not supported.
986  **/
987 char *
988 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
989                                             GAppInfo          *info,
990                                             GList             *files)
991 {
992   GAppLaunchContextClass *class;
993
994   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
995   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
996
997   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
998
999   if (class->get_startup_notify_id == NULL)
1000     return NULL;
1001
1002   return class->get_startup_notify_id (context, info, files);
1003 }
1004
1005
1006 /**
1007  * g_app_launch_context_launch_failed:
1008  * @context: a #GAppLaunchContext.
1009  * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1010  *
1011  * Called when an application has failed to launch, so that it can cancel
1012  * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1013  * 
1014  **/
1015 void
1016 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1017                                     const char        *startup_notify_id)
1018 {
1019   g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1020   g_return_if_fail (startup_notify_id != NULL);
1021
1022   g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1023 }
1024
1025
1026 /**
1027  * SECTION:gappinfomonitor
1028  * @short_description: Monitor application information for changes
1029  *
1030  * #GAppInfoMonitor is a very simple object used for monitoring the app
1031  * info database for changes (ie: newly installed or removed
1032  * applications).
1033  *
1034  * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1035  * to the "changed" signal.
1036  *
1037  * In the usual case, applications should try to make note of the change
1038  * (doing things like invalidating caches) but not act on it.  In
1039  * particular, applications should avoid making calls to #GAppInfo APIs
1040  * in response to the change signal, deferring these until the time that
1041  * the data is actually required.  The exception to this case is when
1042  * application information is actually being displayed on the screen
1043  * (eg: during a search or when the list of all applications is shown).
1044  * The reason for this is that changes to the list of installed
1045  * applications often come in groups (like during system updates) and
1046  * rescanning the list on every change is pointless and expensive.
1047  *
1048  * Since: 2.40
1049  **/
1050
1051 /**
1052  * GAppInfoMonitor:
1053  *
1054  * The only thing you can do with this is to get it via
1055  * g_app_info_monitor_get() and connect to the "changed" signal.
1056  *
1057  * Since: 2.40
1058  **/
1059
1060 /* We have one of each of these per main context and hand them out
1061  * according to the thread default main context at the time of the call
1062  * to g_app_info_monitor_get().
1063  *
1064  * g_object_unref() is only ever called from the same context, so we
1065  * effectively have a single-threaded scenario for each GAppInfoMonitor.
1066  *
1067  * We use a hashtable to cache the per-context monitor (but we do not
1068  * hold a ref).  During finalize, we remove it.  This is possible
1069  * because we don't have to worry about the usual races due to the
1070  * single-threaded nature of each object.
1071  *
1072  * We keep a global list of all contexts that have a monitor for them,
1073  * which we have to access under a lock.  When we dispatch the events to
1074  * be handled in each context, we don't pass the monitor, but the
1075  * context itself.
1076  *
1077  * We dispatch from the GLib worker context, so if we passed the
1078  * monitor, we would need to take a ref on it (in case it was destroyed
1079  * in its own thread meanwhile).  The monitor holds a ref on a context
1080  * and the dispatch would mean that the context would hold a ref on the
1081  * monitor.  If someone stopped iterating the context at just this
1082  * moment both the context and monitor would leak.
1083  *
1084  * Instead, we dispatch the context to itself.  We don't hold a ref.
1085  * There is the danger that the context will be destroyed during the
1086  * dispatch, but if that is the case then we just won't receive our
1087  * callback.
1088  *
1089  * When the dispatch occurs we just lookup the monitor in the hashtable,
1090  * by context.  We can now add and remove refs, since the context will
1091  * have been acquired.
1092  */
1093
1094 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1095
1096 struct _GAppInfoMonitor
1097 {
1098   GObject parent_instance;
1099   GMainContext *context;
1100 };
1101
1102 struct _GAppInfoMonitorClass
1103 {
1104   GObjectClass parent_class;
1105 };
1106
1107 static GHashTable *g_app_info_monitors;
1108 static GMutex      g_app_info_monitor_lock;
1109 static guint       g_app_info_monitor_changed_signal;
1110
1111 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1112
1113 static void
1114 g_app_info_monitor_finalize (GObject *object)
1115 {
1116   GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1117
1118   g_mutex_lock (&g_app_info_monitor_lock);
1119   g_hash_table_remove (g_app_info_monitors, monitor->context);
1120   g_mutex_unlock (&g_app_info_monitor_lock);
1121
1122   G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1123 }
1124
1125 static void
1126 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1127 {
1128 }
1129
1130 static void
1131 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1132 {
1133   GObjectClass *object_class = G_OBJECT_CLASS (class);
1134
1135   g_app_info_monitor_changed_signal = g_signal_new ("changed", G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1136                                                     0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1137
1138   object_class->finalize = g_app_info_monitor_finalize;
1139 }
1140
1141 /**
1142  * g_app_info_monitor_get:
1143  *
1144  * Gets the #GAppInfoMonitor for the current thread-default main
1145  * context.
1146  *
1147  * The #GAppInfoMonitor will emit a "changed" signal in the
1148  * thread-default main context whenever the list of installed
1149  * applications (as reported by g_app_info_get_all()) may have changed.
1150  *
1151  * You must only call g_object_unref() on the return value from under
1152  * the same main context as you created it.
1153  *
1154  * Returns: (transfer full): a reference to a #GAppInfoMonitor
1155  *
1156  * Since: 2.40
1157  **/
1158 GAppInfoMonitor *
1159 g_app_info_monitor_get (void)
1160 {
1161   GAppInfoMonitor *monitor;
1162   GMainContext *context;
1163
1164   context = g_main_context_get_thread_default ();
1165   if (!context)
1166     context = g_main_context_default ();
1167
1168   g_return_val_if_fail (g_main_context_acquire (context), NULL);
1169
1170   g_mutex_lock (&g_app_info_monitor_lock);
1171   if (!g_app_info_monitors)
1172     g_app_info_monitors = g_hash_table_new (NULL, NULL);
1173
1174   monitor = g_hash_table_lookup (g_app_info_monitors, context);
1175   g_mutex_unlock (&g_app_info_monitor_lock);
1176
1177   if (!monitor)
1178     {
1179       monitor = g_object_new (G_TYPE_APP_INFO_MONITOR, NULL);
1180       monitor->context = g_main_context_ref (context);
1181
1182       g_mutex_lock (&g_app_info_monitor_lock);
1183       g_hash_table_insert (g_app_info_monitors, context, monitor);
1184       g_mutex_unlock (&g_app_info_monitor_lock);
1185     }
1186   else
1187     g_object_ref (monitor);
1188
1189   g_main_context_release (context);
1190
1191   return monitor;
1192 }
1193
1194 static gboolean
1195 g_app_info_monitor_emit (gpointer user_data)
1196 {
1197   GMainContext *context = user_data;
1198   GAppInfoMonitor *monitor;
1199
1200   g_mutex_lock (&g_app_info_monitor_lock);
1201   monitor = g_hash_table_lookup (g_app_info_monitors, context);
1202   g_mutex_unlock (&g_app_info_monitor_lock);
1203
1204   /* It is possible that the monitor was already destroyed by the time
1205    * we get here, so make sure it's not NULL.
1206    */
1207   if (monitor != NULL)
1208     {
1209       /* We don't have to worry about another thread disposing the
1210        * monitor but we do have to worry about the possibility that one
1211        * of the attached handlers may do so.
1212        *
1213        * Take a ref so that the monitor doesn't disappear in the middle
1214        * of the emission.
1215        */
1216       g_object_ref (monitor);
1217       g_signal_emit (monitor, g_app_info_monitor_changed_signal, 0);
1218       g_object_unref (monitor);
1219     }
1220
1221   return FALSE;
1222 }
1223
1224 void
1225 g_app_info_monitor_fire (void)
1226 {
1227   GHashTableIter iter;
1228   gpointer context;
1229
1230   g_mutex_lock (&g_app_info_monitor_lock);
1231
1232   if (g_app_info_monitors)
1233     {
1234       g_hash_table_iter_init (&iter, g_app_info_monitors);
1235       while (g_hash_table_iter_next (&iter, &context, NULL))
1236         {
1237           GSource *idle;
1238
1239           idle = g_idle_source_new ();
1240           g_source_set_callback (idle, g_app_info_monitor_emit, context, NULL);
1241           g_source_set_name (idle, "[gio] g_app_info_monitor_emit");
1242           g_source_attach (idle, context);
1243           g_source_unref (idle);
1244         }
1245     }
1246
1247   g_mutex_unlock (&g_app_info_monitor_lock);
1248 }