Convert external links to markdown syntax
[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 #GAppInfos 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()
542  * and g_app_launch_context_unsetenv().
543  *
544  * On UNIX, this function sets the `GIO_LAUNCHED_DESKTOP_FILE`
545  * environment variable with the path of the launched desktop file and
546  * `GIO_LAUNCHED_DESKTOP_FILE_PID` to the process id of the launched
547  * process. This can be used to ignore `GIO_LAUNCHED_DESKTOP_FILE`,
548  * should it be inherited by further processes. The `DISPLAY` and
549  * `DESKTOP_STARTUP_ID` environment variables are also set, based
550  * on information provided in @launch_context.
551  *
552  * Returns: %TRUE on successful launch, %FALSE otherwise.
553  **/
554 gboolean
555 g_app_info_launch (GAppInfo           *appinfo,
556                    GList              *files,
557                    GAppLaunchContext  *launch_context,
558                    GError            **error)
559 {
560   GAppInfoIface *iface;
561   
562   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
563
564   iface = G_APP_INFO_GET_IFACE (appinfo);
565
566   return (* iface->launch) (appinfo, files, launch_context, error);
567 }
568
569
570 /**
571  * g_app_info_supports_uris:
572  * @appinfo: a #GAppInfo.
573  * 
574  * Checks if the application supports reading files and directories from URIs.
575  *
576  * Returns: %TRUE if the @appinfo supports URIs.
577  **/
578 gboolean
579 g_app_info_supports_uris (GAppInfo *appinfo)
580 {
581   GAppInfoIface *iface;
582   
583   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
584
585   iface = G_APP_INFO_GET_IFACE (appinfo);
586
587   return (* iface->supports_uris) (appinfo);
588 }
589
590
591 /**
592  * g_app_info_supports_files:
593  * @appinfo: a #GAppInfo.
594  * 
595  * Checks if the application accepts files as arguments.
596  *
597  * Returns: %TRUE if the @appinfo supports files.
598  **/
599 gboolean
600 g_app_info_supports_files (GAppInfo *appinfo)
601 {
602   GAppInfoIface *iface;
603   
604   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
605
606   iface = G_APP_INFO_GET_IFACE (appinfo);
607
608   return (* iface->supports_files) (appinfo);
609 }
610
611
612 /**
613  * g_app_info_launch_uris:
614  * @appinfo: a #GAppInfo
615  * @uris: (allow-none) (element-type utf8): a #GList containing URIs to launch.
616  * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
617  * @error: a #GError
618  * 
619  * Launches the application. This passes the @uris to the launched application
620  * as arguments, using the optional @launch_context to get information
621  * about the details of the launcher (like what screen it is on).
622  * On error, @error will be set accordingly.
623  *
624  * To launch the application without arguments pass a %NULL @uris list.
625  *
626  * Note that even if the launch is successful the application launched
627  * can fail to start if it runs into problems during startup. There is
628  * no way to detect this.
629  *
630  * Returns: %TRUE on successful launch, %FALSE otherwise.
631  **/
632 gboolean
633 g_app_info_launch_uris (GAppInfo           *appinfo,
634                         GList              *uris,
635                         GAppLaunchContext  *launch_context,
636                         GError            **error)
637 {
638   GAppInfoIface *iface;
639   
640   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
641
642   iface = G_APP_INFO_GET_IFACE (appinfo);
643
644   return (* iface->launch_uris) (appinfo, uris, launch_context, error);
645 }
646
647
648 /**
649  * g_app_info_should_show:
650  * @appinfo: a #GAppInfo.
651  *
652  * Checks if the application info should be shown in menus that 
653  * list available applications.
654  * 
655  * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
656  **/
657 gboolean
658 g_app_info_should_show (GAppInfo *appinfo)
659 {
660   GAppInfoIface *iface;
661   
662   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
663
664   iface = G_APP_INFO_GET_IFACE (appinfo);
665
666   return (* iface->should_show) (appinfo);
667 }
668
669 /**
670  * g_app_info_launch_default_for_uri:
671  * @uri: the uri to show
672  * @launch_context: (allow-none): an optional #GAppLaunchContext.
673  * @error: a #GError.
674  *
675  * Utility function that launches the default application
676  * registered to handle the specified uri. Synchronous I/O
677  * is done on the uri to detect the type of the file if
678  * required.
679  * 
680  * Returns: %TRUE on success, %FALSE on error.
681  **/
682 gboolean
683 g_app_info_launch_default_for_uri (const char         *uri,
684                                    GAppLaunchContext  *launch_context,
685                                    GError            **error)
686 {
687   char *uri_scheme;
688   GAppInfo *app_info = NULL;
689   GList l;
690   gboolean res;
691
692   /* g_file_query_default_handler() calls
693    * g_app_info_get_default_for_uri_scheme() too, but we have to do it
694    * here anyway in case GFile can't parse @uri correctly.
695    */
696   uri_scheme = g_uri_parse_scheme (uri);
697   if (uri_scheme && uri_scheme[0] != '\0')
698     app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
699   g_free (uri_scheme);
700
701   if (!app_info)
702     {
703       GFile *file;
704
705       file = g_file_new_for_uri (uri);
706       app_info = g_file_query_default_handler (file, NULL, error);
707       g_object_unref (file);
708       if (app_info == NULL)
709         return FALSE;
710
711       /* We still use the original @uri rather than calling
712        * g_file_get_uri(), because GFile might have modified the URI
713        * in ways we don't want (eg, removing the fragment identifier
714        * from a file: URI).
715        */
716     }
717
718   l.data = (char *)uri;
719   l.next = l.prev = NULL;
720   res = g_app_info_launch_uris (app_info, &l,
721                                 launch_context, error);
722
723   g_object_unref (app_info);
724   
725   return res;
726 }
727
728 /**
729  * g_app_info_can_delete:
730  * @appinfo: a #GAppInfo
731  *
732  * Obtains the information whether the #GAppInfo can be deleted.
733  * See g_app_info_delete().
734  *
735  * Returns: %TRUE if @appinfo can be deleted
736  *
737  * Since: 2.20
738  */
739 gboolean
740 g_app_info_can_delete (GAppInfo *appinfo)
741 {
742   GAppInfoIface *iface;
743   
744   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
745
746   iface = G_APP_INFO_GET_IFACE (appinfo);
747
748   if (iface->can_delete)
749     return (* iface->can_delete) (appinfo);
750  
751   return FALSE; 
752 }
753
754
755 /**
756  * g_app_info_delete:
757  * @appinfo: a #GAppInfo
758  *
759  * Tries to delete a #GAppInfo.
760  *
761  * On some platforms, there may be a difference between user-defined
762  * #GAppInfos which can be deleted, and system-wide ones which cannot.
763  * See g_app_info_can_delete().
764  *
765  * Virtual: do_delete
766  * Returns: %TRUE if @appinfo has been deleted
767  *
768  * Since: 2.20
769  */
770 gboolean
771 g_app_info_delete (GAppInfo *appinfo)
772 {
773   GAppInfoIface *iface;
774   
775   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
776
777   iface = G_APP_INFO_GET_IFACE (appinfo);
778
779   if (iface->do_delete)
780     return (* iface->do_delete) (appinfo);
781  
782   return FALSE; 
783 }
784
785
786 enum {
787   LAUNCH_FAILED,
788   LAUNCHED,
789   LAST_SIGNAL
790 };
791
792 struct _GAppLaunchContextPrivate {
793   char **envp;
794 };
795
796 static guint signals[LAST_SIGNAL] = { 0 };
797
798 G_DEFINE_TYPE_WITH_PRIVATE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT)
799
800 /**
801  * g_app_launch_context_new:
802  * 
803  * Creates a new application launch context. This is not normally used,
804  * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
805  *
806  * Returns: a #GAppLaunchContext.
807  **/
808 GAppLaunchContext *
809 g_app_launch_context_new (void)
810 {
811   return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
812 }
813
814 static void
815 g_app_launch_context_finalize (GObject *object)
816 {
817   GAppLaunchContext *context = G_APP_LAUNCH_CONTEXT (object);
818
819   g_strfreev (context->priv->envp);
820
821   G_OBJECT_CLASS (g_app_launch_context_parent_class)->finalize (object);
822 }
823
824 static void
825 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
826 {
827   GObjectClass *object_class = G_OBJECT_CLASS (klass);
828
829   object_class->finalize = g_app_launch_context_finalize;
830
831   /*
832    * GAppLaunchContext::launch-failed:
833    * @context: the object emitting the signal
834    * @startup_notify_id: the startup notification id for the failed launch
835    *
836    * The ::launch-failed signal is emitted when a #GAppInfo launch
837    * fails. The startup notification id is provided, so that the launcher
838    * can cancel the startup notification.
839    *
840    * Since: 2.36
841    */
842   signals[LAUNCH_FAILED] = g_signal_new ("launch-failed",
843                                          G_OBJECT_CLASS_TYPE (object_class),
844                                          G_SIGNAL_RUN_LAST,
845                                          G_STRUCT_OFFSET (GAppLaunchContextClass, launch_failed),
846                                          NULL, NULL, NULL,
847                                          G_TYPE_NONE, 1, G_TYPE_STRING);
848
849   /*
850    * GAppLaunchContext::launched:
851    * @context: the object emitting the signal
852    * @info: the #GAppInfo that was just launched
853    * @platform_data: additional platform-specific data for this launch
854    *
855    * The ::launched signal is emitted when a #GAppInfo is successfully
856    * launched. The @platform_data is an GVariant dictionary mapping
857    * strings to variants (ie a{sv}), which contains additional,
858    * platform-specific data about this launch. On UNIX, at least the
859    * "pid" and "startup-notification-id" keys will be present.
860    *
861    * Since: 2.36
862    */
863   signals[LAUNCHED] = g_signal_new ("launched",
864                                     G_OBJECT_CLASS_TYPE (object_class),
865                                     G_SIGNAL_RUN_LAST,
866                                     G_STRUCT_OFFSET (GAppLaunchContextClass, launched),
867                                     NULL, NULL, NULL,
868                                     G_TYPE_NONE, 2,
869                                     G_TYPE_APP_INFO, G_TYPE_VARIANT);
870 }
871
872 static void
873 g_app_launch_context_init (GAppLaunchContext *context)
874 {
875   context->priv = g_app_launch_context_get_instance_private (context);
876 }
877
878 /**
879  * g_app_launch_context_setenv:
880  * @context: a #GAppLaunchContext
881  * @variable: the environment variable to set
882  * @value: the value for to set the variable to.
883  *
884  * Arranges for @variable to be set to @value in the child's
885  * environment when @context is used to launch an application.
886  *
887  * Since: 2.32
888  */
889 void
890 g_app_launch_context_setenv (GAppLaunchContext *context,
891                              const char        *variable,
892                              const char        *value)
893 {
894   if (!context->priv->envp)
895     context->priv->envp = g_get_environ ();
896
897   context->priv->envp =
898     g_environ_setenv (context->priv->envp, variable, value, TRUE);
899 }
900
901 /**
902  * g_app_launch_context_unsetenv:
903  * @context: a #GAppLaunchContext
904  * @variable: the environment variable to remove
905  *
906  * Arranges for @variable to be unset in the child's environment
907  * when @context is used to launch an application.
908  *
909  * Since: 2.32
910  */
911 void
912 g_app_launch_context_unsetenv (GAppLaunchContext *context,
913                                const char        *variable)
914 {
915   if (!context->priv->envp)
916     context->priv->envp = g_get_environ ();
917
918   context->priv->envp =
919     g_environ_unsetenv (context->priv->envp, variable);
920 }
921
922 /**
923  * g_app_launch_context_get_environment:
924  * @context: a #GAppLaunchContext
925  *
926  * Gets the complete environment variable list to be passed to
927  * the child process when @context is used to launch an application.
928  * This is a %NULL-terminated array of strings, where each string has
929  * the form <literal>KEY=VALUE</literal>.
930  *
931  * Return value: (array zero-terminated=1) (transfer full): the
932  *     child's environment
933  *
934  * Since: 2.32
935  */
936 char **
937 g_app_launch_context_get_environment (GAppLaunchContext *context)
938 {
939   if (!context->priv->envp)
940     context->priv->envp = g_get_environ ();
941
942   return g_strdupv (context->priv->envp);
943 }
944
945 /**
946  * g_app_launch_context_get_display:
947  * @context: a #GAppLaunchContext
948  * @info: a #GAppInfo
949  * @files: (element-type GFile): a #GList of #GFile objects
950  *
951  * Gets the display string for the @context. This is used to ensure new
952  * applications are started on the same display as the launching
953  * application, by setting the `DISPLAY` environment variable.
954  *
955  * Returns: a display string for the display.
956  */
957 char *
958 g_app_launch_context_get_display (GAppLaunchContext *context,
959                                   GAppInfo          *info,
960                                   GList             *files)
961 {
962   GAppLaunchContextClass *class;
963
964   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
965   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
966
967   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
968
969   if (class->get_display == NULL)
970     return NULL;
971
972   return class->get_display (context, info, files);
973 }
974
975 /**
976  * g_app_launch_context_get_startup_notify_id:
977  * @context: a #GAppLaunchContext
978  * @info: a #GAppInfo
979  * @files: (element-type GFile): a #GList of of #GFile objects
980  * 
981  * Initiates startup notification for the application and returns the
982  * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
983  *
984  * Startup notification IDs are defined in the 
985  * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
986  *
987  * Returns: a startup notification ID for the application, or %NULL if
988  *     not supported.
989  **/
990 char *
991 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
992                                             GAppInfo          *info,
993                                             GList             *files)
994 {
995   GAppLaunchContextClass *class;
996
997   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
998   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
999
1000   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
1001
1002   if (class->get_startup_notify_id == NULL)
1003     return NULL;
1004
1005   return class->get_startup_notify_id (context, info, files);
1006 }
1007
1008
1009 /**
1010  * g_app_launch_context_launch_failed:
1011  * @context: a #GAppLaunchContext.
1012  * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
1013  *
1014  * Called when an application has failed to launch, so that it can cancel
1015  * the application startup notification started in g_app_launch_context_get_startup_notify_id().
1016  * 
1017  **/
1018 void
1019 g_app_launch_context_launch_failed (GAppLaunchContext *context,
1020                                     const char        *startup_notify_id)
1021 {
1022   g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
1023   g_return_if_fail (startup_notify_id != NULL);
1024
1025   g_signal_emit (context, signals[LAUNCH_FAILED], 0, startup_notify_id);
1026 }
1027
1028
1029 /**
1030  * SECTION:gappinfomonitor
1031  * @short_description: Monitor application information for changes
1032  *
1033  * #GAppInfoMonitor is a very simple object used for monitoring the app
1034  * info database for changes (ie: newly installed or removed
1035  * applications).
1036  *
1037  * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
1038  * to the "changed" signal.
1039  *
1040  * In the usual case, applications should try to make note of the change
1041  * (doing things like invalidating caches) but not act on it.  In
1042  * particular, applications should avoid making calls to #GAppInfo APIs
1043  * in response to the change signal, deferring these until the time that
1044  * the data is actually required.  The exception to this case is when
1045  * application information is actually being displayed on the screen
1046  * (eg: during a search or when the list of all applications is shown).
1047  * The reason for this is that changes to the list of installed
1048  * applications often come in groups (like during system updates) and
1049  * rescanning the list on every change is pointless and expensive.
1050  *
1051  * Since: 2.40
1052  **/
1053
1054 /**
1055  * GAppInfoMonitor:
1056  *
1057  * The only thing you can do with this is to get it via
1058  * g_app_info_monitor_get() and connect to the "changed" signal.
1059  *
1060  * Since: 2.40
1061  **/
1062
1063 /* We have one of each of these per main context and hand them out
1064  * according to the thread default main context at the time of the call
1065  * to g_app_info_monitor_get().
1066  *
1067  * g_object_unref() is only ever called from the same context, so we
1068  * effectively have a single-threaded scenario for each GAppInfoMonitor.
1069  *
1070  * We use a hashtable to cache the per-context monitor (but we do not
1071  * hold a ref).  During finalize, we remove it.  This is possible
1072  * because we don't have to worry about the usual races due to the
1073  * single-threaded nature of each object.
1074  *
1075  * We keep a global list of all contexts that have a monitor for them,
1076  * which we have to access under a lock.  When we dispatch the events to
1077  * be handled in each context, we don't pass the monitor, but the
1078  * context itself.
1079  *
1080  * We dispatch from the GLib worker context, so if we passed the
1081  * monitor, we would need to take a ref on it (in case it was destroyed
1082  * in its own thread meanwhile).  The monitor holds a ref on a context
1083  * and the dispatch would mean that the context would hold a ref on the
1084  * monitor.  If someone stopped iterating the context at just this
1085  * moment both the context and monitor would leak.
1086  *
1087  * Instead, we dispatch the context to itself.  We don't hold a ref.
1088  * There is the danger that the context will be destroyed during the
1089  * dispatch, but if that is the case then we just won't receive our
1090  * callback.
1091  *
1092  * When the dispatch occurs we just lookup the monitor in the hashtable,
1093  * by context.  We can now add and remove refs, since the context will
1094  * have been acquired.
1095  */
1096
1097 typedef struct _GAppInfoMonitorClass GAppInfoMonitorClass;
1098
1099 struct _GAppInfoMonitor
1100 {
1101   GObject parent_instance;
1102   GMainContext *context;
1103 };
1104
1105 struct _GAppInfoMonitorClass
1106 {
1107   GObjectClass parent_class;
1108 };
1109
1110 static GHashTable *g_app_info_monitors;
1111 static GMutex      g_app_info_monitor_lock;
1112 static guint       g_app_info_monitor_changed_signal;
1113
1114 G_DEFINE_TYPE (GAppInfoMonitor, g_app_info_monitor, G_TYPE_OBJECT)
1115
1116 static void
1117 g_app_info_monitor_finalize (GObject *object)
1118 {
1119   GAppInfoMonitor *monitor = G_APP_INFO_MONITOR (object);
1120
1121   g_mutex_lock (&g_app_info_monitor_lock);
1122   g_hash_table_remove (g_app_info_monitors, monitor->context);
1123   g_mutex_unlock (&g_app_info_monitor_lock);
1124
1125   G_OBJECT_CLASS (g_app_info_monitor_parent_class)->finalize (object);
1126 }
1127
1128 static void
1129 g_app_info_monitor_init (GAppInfoMonitor *monitor)
1130 {
1131 }
1132
1133 static void
1134 g_app_info_monitor_class_init (GAppInfoMonitorClass *class)
1135 {
1136   GObjectClass *object_class = G_OBJECT_CLASS (class);
1137
1138   g_app_info_monitor_changed_signal = g_signal_new ("changed", G_TYPE_APP_INFO_MONITOR, G_SIGNAL_RUN_FIRST,
1139                                                     0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1140
1141   object_class->finalize = g_app_info_monitor_finalize;
1142 }
1143
1144 /**
1145  * g_app_info_monitor_get:
1146  *
1147  * Gets the #GAppInfoMonitor for the current thread-default main
1148  * context.
1149  *
1150  * The #GAppInfoMonitor will emit a "changed" signal in the
1151  * thread-default main context whenever the list of installed
1152  * applications (as reported by g_app_info_get_all()) may have changed.
1153  *
1154  * You must only call g_object_unref() on the return value from under
1155  * the same main context as you created it.
1156  *
1157  * Returns: (transfer full): a reference to a #GAppInfoMonitor
1158  *
1159  * Since: 2.40
1160  **/
1161 GAppInfoMonitor *
1162 g_app_info_monitor_get (void)
1163 {
1164   GAppInfoMonitor *monitor;
1165   GMainContext *context;
1166
1167   context = g_main_context_get_thread_default ();
1168   if (!context)
1169     context = g_main_context_default ();
1170
1171   g_return_val_if_fail (g_main_context_acquire (context), NULL);
1172
1173   g_mutex_lock (&g_app_info_monitor_lock);
1174   if (!g_app_info_monitors)
1175     g_app_info_monitors = g_hash_table_new (NULL, NULL);
1176
1177   monitor = g_hash_table_lookup (g_app_info_monitors, context);
1178   g_mutex_unlock (&g_app_info_monitor_lock);
1179
1180   if (!monitor)
1181     {
1182       monitor = g_object_new (G_TYPE_APP_INFO_MONITOR, NULL);
1183       monitor->context = g_main_context_ref (context);
1184
1185       g_mutex_lock (&g_app_info_monitor_lock);
1186       g_hash_table_insert (g_app_info_monitors, context, monitor);
1187       g_mutex_unlock (&g_app_info_monitor_lock);
1188     }
1189   else
1190     g_object_ref (monitor);
1191
1192   g_main_context_release (context);
1193
1194   return monitor;
1195 }
1196
1197 static gboolean
1198 g_app_info_monitor_emit (gpointer user_data)
1199 {
1200   GMainContext *context = user_data;
1201   GAppInfoMonitor *monitor;
1202
1203   g_mutex_lock (&g_app_info_monitor_lock);
1204   monitor = g_hash_table_lookup (g_app_info_monitors, context);
1205   g_mutex_unlock (&g_app_info_monitor_lock);
1206
1207   /* It is possible that the monitor was already destroyed by the time
1208    * we get here, so make sure it's not NULL.
1209    */
1210   if (monitor != NULL)
1211     {
1212       /* We don't have to worry about another thread disposing the
1213        * monitor but we do have to worry about the possibility that one
1214        * of the attached handlers may do so.
1215        *
1216        * Take a ref so that the monitor doesn't disappear in the middle
1217        * of the emission.
1218        */
1219       g_object_ref (monitor);
1220       g_signal_emit (monitor, g_app_info_monitor_changed_signal, 0);
1221       g_object_unref (monitor);
1222     }
1223
1224   return FALSE;
1225 }
1226
1227 void
1228 g_app_info_monitor_fire (void)
1229 {
1230   GHashTableIter iter;
1231   gpointer context;
1232
1233   g_mutex_lock (&g_app_info_monitor_lock);
1234
1235   if (g_app_info_monitors)
1236     {
1237       g_hash_table_iter_init (&iter, g_app_info_monitors);
1238       while (g_hash_table_iter_next (&iter, &context, NULL))
1239         {
1240           GSource *idle;
1241
1242           idle = g_idle_source_new ();
1243           g_source_set_callback (idle, g_app_info_monitor_emit, context, NULL);
1244           g_source_attach (idle, context);
1245           g_source_unref (idle);
1246         }
1247     }
1248
1249   g_mutex_unlock (&g_app_info_monitor_lock);
1250 }