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