Bug 545350 – GAppInfo deletion Bug 545351 – Reset associations for
[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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gappinfo.h"
25 #include "glibintl.h"
26 #include <gioerror.h>
27 #include <gfile.h>
28
29 #include "gioalias.h"
30
31 /**
32  * SECTION:gappinfo
33  * @short_description: Application information and launch contexts
34  * @include: gio/gio.h
35  * 
36  * #GAppInfo and #GAppLaunchContext are used for describing and launching 
37  * applications installed on the system. 
38  *
39  **/
40
41 static void g_app_info_base_init (gpointer g_class);
42 static void g_app_info_class_init (gpointer g_class,
43                                    gpointer class_data);
44
45
46 GType
47 g_app_info_get_type (void)
48 {
49   static volatile gsize g_define_type_id__volatile = 0;
50
51   if (g_once_init_enter (&g_define_type_id__volatile))
52     {
53      const GTypeInfo app_info_info =
54       {
55         sizeof (GAppInfoIface), /* class_size */
56         g_app_info_base_init,   /* base_init */
57         NULL,           /* base_finalize */
58         g_app_info_class_init,
59         NULL,           /* class_finalize */
60         NULL,           /* class_data */
61         0,
62         0,              /* n_preallocs */
63         NULL
64       };
65       GType g_define_type_id =
66         g_type_register_static (G_TYPE_INTERFACE, I_("GAppInfo"),
67                                 &app_info_info, 0);
68
69       g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
70
71       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
72     }
73
74   return g_define_type_id__volatile;
75 }
76
77 static void
78 g_app_info_class_init (gpointer g_class,
79                        gpointer class_data)
80 {
81 }
82
83 static void
84 g_app_info_base_init (gpointer g_class)
85 {
86 }
87
88
89 /**
90  * g_app_info_dup:
91  * @appinfo: a #GAppInfo.
92  * 
93  * Creates a duplicate of a #GAppInfo.
94  *
95  * Returns: a duplicate of @appinfo.
96  **/
97 GAppInfo *
98 g_app_info_dup (GAppInfo *appinfo)
99 {
100   GAppInfoIface *iface;
101
102   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
103
104   iface = G_APP_INFO_GET_IFACE (appinfo);
105
106   return (* iface->dup) (appinfo);
107 }
108
109 /**
110  * g_app_info_equal:
111  * @appinfo1: the first #GAppInfo.  
112  * @appinfo2: the second #GAppInfo.
113  * 
114  * Checks if two #GAppInfos are equal.
115  *
116  * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
117  **/
118 gboolean
119 g_app_info_equal (GAppInfo *appinfo1,
120                   GAppInfo *appinfo2)
121 {
122   GAppInfoIface *iface;
123
124   g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
125   g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
126
127   if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
128     return FALSE;
129   
130   iface = G_APP_INFO_GET_IFACE (appinfo1);
131
132   return (* iface->equal) (appinfo1, appinfo2);
133 }
134
135 /**
136  * g_app_info_get_id:
137  * @appinfo: a #GAppInfo.
138  * 
139  * Gets the ID of an application. An id is a string that
140  * identifies the application. The exact format of the id is
141  * platform dependent. For instance, on Unix this is the
142  * desktop file id from the xdg menu specification.
143  *
144  * Note that the returned ID may be %NULL, depending on how
145  * the @appinfo has been constructed.
146  *
147  * Returns: a string containing the application's ID.
148  **/
149 const char *
150 g_app_info_get_id (GAppInfo *appinfo)
151 {
152   GAppInfoIface *iface;
153   
154   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
155
156   iface = G_APP_INFO_GET_IFACE (appinfo);
157
158   return (* iface->get_id) (appinfo);
159 }
160
161 /**
162  * g_app_info_get_name:
163  * @appinfo: a #GAppInfo.
164  * 
165  * Gets the installed name of the application. 
166  *
167  * Returns: the name of the application for @appinfo.
168  **/
169 const char *
170 g_app_info_get_name (GAppInfo *appinfo)
171 {
172   GAppInfoIface *iface;
173   
174   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
175
176   iface = G_APP_INFO_GET_IFACE (appinfo);
177
178   return (* iface->get_name) (appinfo);
179 }
180
181 /**
182  * g_app_info_get_description:
183  * @appinfo: a #GAppInfo.
184  * 
185  * Gets a human-readable description of an installed application.
186  *
187  * Returns: a string containing a description of the 
188  * application @appinfo, or %NULL if none. 
189  **/
190 const char *
191 g_app_info_get_description (GAppInfo *appinfo)
192 {
193   GAppInfoIface *iface;
194   
195   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
196
197   iface = G_APP_INFO_GET_IFACE (appinfo);
198
199   return (* iface->get_description) (appinfo);
200 }
201
202 /**
203  * g_app_info_get_executable:
204  * @appinfo: a #GAppInfo.
205  * 
206  * Gets the executable's name for the installed application.
207  *
208  * Returns: a string containing the @appinfo's application 
209  * binary's name.
210  **/
211 const char *
212 g_app_info_get_executable (GAppInfo *appinfo)
213 {
214   GAppInfoIface *iface;
215   
216   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
217
218   iface = G_APP_INFO_GET_IFACE (appinfo);
219
220   return (* iface->get_executable) (appinfo);
221 }
222
223
224 /**
225  * g_app_info_set_as_default_for_type:
226  * @appinfo: a #GAppInfo.
227  * @content_type: the content type.
228  * @error: a #GError.
229  * 
230  * Sets the application as the default handler for a given type.
231  *
232  * Returns: %TRUE on success, %FALSE on error.
233  **/
234 gboolean
235 g_app_info_set_as_default_for_type (GAppInfo    *appinfo,
236                                     const char  *content_type,
237                                     GError     **error)
238 {
239   GAppInfoIface *iface;
240   
241   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
242   g_return_val_if_fail (content_type != NULL, FALSE);
243
244   iface = G_APP_INFO_GET_IFACE (appinfo);
245
246   return (* iface->set_as_default_for_type) (appinfo, content_type, error);
247 }
248
249
250 /**
251  * g_app_info_set_as_default_for_extension:
252  * @appinfo: a #GAppInfo.
253  * @extension: a string containing the file extension (without the dot).
254  * @error: a #GError.
255  * 
256  * Sets the application as the default handler for the given file extention.
257  *
258  * Returns: %TRUE on success, %FALSE on error.
259  **/
260 gboolean
261 g_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
262                                          const char  *extension,
263                                          GError     **error)
264 {
265   GAppInfoIface *iface;
266   
267   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
268   g_return_val_if_fail (extension != NULL, FALSE);
269
270   iface = G_APP_INFO_GET_IFACE (appinfo);
271
272   if (iface->set_as_default_for_extension)
273     return (* iface->set_as_default_for_extension) (appinfo, extension, error);
274
275   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
276                        "g_app_info_set_as_default_for_extension not supported yet");
277   return FALSE;
278 }
279
280
281 /**
282  * g_app_info_add_supports_type:
283  * @appinfo: a #GAppInfo.
284  * @content_type: a string.
285  * @error: a #GError.
286  * 
287  * Adds a content type to the application information to indicate the 
288  * application is capable of opening files with the given content type.
289  *
290  * Returns: %TRUE on success, %FALSE on error.
291  **/
292 gboolean
293 g_app_info_add_supports_type (GAppInfo    *appinfo,
294                               const char  *content_type,
295                               GError     **error)
296 {
297   GAppInfoIface *iface;
298   
299   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
300   g_return_val_if_fail (content_type != NULL, FALSE);
301
302   iface = G_APP_INFO_GET_IFACE (appinfo);
303
304   if (iface->add_supports_type)
305     return (* iface->add_supports_type) (appinfo, content_type, error);
306
307   g_set_error_literal (error, G_IO_ERROR, 
308                        G_IO_ERROR_NOT_SUPPORTED, 
309                        "g_app_info_add_supports_type not supported yet");
310
311   return FALSE;
312 }
313
314
315 /**
316  * g_app_info_can_remove_supports_type:
317  * @appinfo: a #GAppInfo.
318  * 
319  * Checks if a supported content type can be removed from an application.
320  *
321  * Returns: %TRUE if it is possible to remove supported 
322  *     content types from a given @appinfo, %FALSE if not.
323  **/
324 gboolean
325 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
326 {
327   GAppInfoIface *iface;
328   
329   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
330
331   iface = G_APP_INFO_GET_IFACE (appinfo);
332
333   if (iface->can_remove_supports_type)
334     return (* iface->can_remove_supports_type) (appinfo);
335
336   return FALSE;
337 }
338
339
340 /**
341  * g_app_info_remove_supports_type:
342  * @appinfo: a #GAppInfo.
343  * @content_type: a string.
344  * @error: a #GError.
345  *
346  * Removes a supported type from an application, if possible.
347  * 
348  * Returns: %TRUE on success, %FALSE on error.
349  **/
350 gboolean
351 g_app_info_remove_supports_type (GAppInfo    *appinfo,
352                                  const char  *content_type,
353                                  GError     **error)
354 {
355   GAppInfoIface *iface;
356   
357   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
358   g_return_val_if_fail (content_type != NULL, FALSE);
359
360   iface = G_APP_INFO_GET_IFACE (appinfo);
361
362   if (iface->remove_supports_type)
363     return (* iface->remove_supports_type) (appinfo, content_type, error);
364
365   g_set_error_literal (error, G_IO_ERROR, 
366                        G_IO_ERROR_NOT_SUPPORTED, 
367                        "g_app_info_remove_supports_type not supported yet");
368
369   return FALSE;
370 }
371
372
373 /**
374  * g_app_info_get_icon:
375  * @appinfo: a #GAppInfo.
376  * 
377  * Gets the icon for the application.
378  *
379  * Returns: the default #GIcon for @appinfo.
380  **/
381 GIcon *
382 g_app_info_get_icon (GAppInfo *appinfo)
383 {
384   GAppInfoIface *iface;
385   
386   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
387
388   iface = G_APP_INFO_GET_IFACE (appinfo);
389
390   return (* iface->get_icon) (appinfo);
391 }
392
393
394 /**
395  * g_app_info_launch:
396  * @appinfo: a #GAppInfo
397  * @files: a #GList of #GFile objects
398  * @launch_context: a #GAppLaunchContext or %NULL
399  * @error: a #GError
400  * 
401  * Launches the application. Passes @files to the launched application 
402  * as arguments, using the optional @launch_context to get information
403  * about the details of the launcher (like what screen it is on).
404  * On error, @error will be set accordingly.
405  *
406  * To lauch the application without arguments pass a %NULL @files list.
407  *
408  * Note that even if the launch is successful the application launched
409  * can fail to start if it runs into problems during startup. There is
410  * no way to detect this.
411  *
412  * Some URIs can be changed when passed through a GFile (for instance
413  * unsupported uris with strange formats like mailto:), so if you have
414  * a textual uri you want to pass in as argument, consider using
415  * g_app_info_launch_uris() instead.
416  * 
417  * Returns: %TRUE on successful launch, %FALSE otherwise. 
418  **/
419 gboolean
420 g_app_info_launch (GAppInfo           *appinfo,
421                    GList              *files,
422                    GAppLaunchContext  *launch_context,
423                    GError            **error)
424 {
425   GAppInfoIface *iface;
426   
427   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
428
429   iface = G_APP_INFO_GET_IFACE (appinfo);
430
431   return (* iface->launch) (appinfo, files, launch_context, error);
432 }
433
434
435 /**
436  * g_app_info_supports_uris:
437  * @appinfo: a #GAppInfo.
438  * 
439  * Checks if the application supports reading files and directories from URIs.
440  *
441  * Returns: %TRUE if the @appinfo supports URIs.
442  **/
443 gboolean
444 g_app_info_supports_uris (GAppInfo *appinfo)
445 {
446   GAppInfoIface *iface;
447   
448   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
449
450   iface = G_APP_INFO_GET_IFACE (appinfo);
451
452   return (* iface->supports_uris) (appinfo);
453 }
454
455
456 /**
457  * g_app_info_supports_files:
458  * @appinfo: a #GAppInfo.
459  * 
460  * Checks if the application accepts files as arguments.
461  *
462  * Returns: %TRUE if the @appinfo supports files.
463  **/
464 gboolean
465 g_app_info_supports_files (GAppInfo *appinfo)
466 {
467   GAppInfoIface *iface;
468   
469   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
470
471   iface = G_APP_INFO_GET_IFACE (appinfo);
472
473   return (* iface->supports_files) (appinfo);
474 }
475
476
477 /**
478  * g_app_info_launch_uris:
479  * @appinfo: a #GAppInfo
480  * @uris: a #GList containing URIs to launch. 
481  * @launch_context: a #GAppLaunchContext or %NULL
482  * @error: a #GError
483  * 
484  * Launches the application. Passes @uris to the launched application 
485  * as arguments, using the optional @launch_context to get information
486  * about the details of the launcher (like what screen it is on).
487  * On error, @error will be set accordingly.
488  *
489  * To lauch the application without arguments pass a %NULL @uris list.
490  *
491  * Note that even if the launch is successful the application launched
492  * can fail to start if it runs into problems during startup. There is
493  * no way to detect this.
494  *
495  * Returns: %TRUE on successful launch, %FALSE otherwise. 
496  **/
497 gboolean
498 g_app_info_launch_uris (GAppInfo           *appinfo,
499                         GList              *uris,
500                         GAppLaunchContext  *launch_context,
501                         GError            **error)
502 {
503   GAppInfoIface *iface;
504   
505   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
506
507   iface = G_APP_INFO_GET_IFACE (appinfo);
508
509   return (* iface->launch_uris) (appinfo, uris, launch_context, error);
510 }
511
512
513 /**
514  * g_app_info_should_show:
515  * @appinfo: a #GAppInfo.
516  *
517  * Checks if the application info should be shown in menus that 
518  * list available applications.
519  * 
520  * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
521  **/
522 gboolean
523 g_app_info_should_show (GAppInfo *appinfo)
524 {
525   GAppInfoIface *iface;
526   
527   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
528
529   iface = G_APP_INFO_GET_IFACE (appinfo);
530
531   return (* iface->should_show) (appinfo);
532 }
533
534 /**
535  * g_app_info_launch_default_for_uri:
536  * @uri: the uri to show
537  * @launch_context: an optional #GAppLaunchContext.
538  * @error: a #GError.
539  *
540  * Utility function that launches the default application 
541  * registered to handle the specified uri. Synchronous I/O
542  * is done on the uri to detext the type of the file if
543  * required.
544  * 
545  * Returns: %TRUE on success, %FALSE on error.
546  **/
547 gboolean
548 g_app_info_launch_default_for_uri (const char         *uri,
549                                    GAppLaunchContext  *launch_context,
550                                    GError            **error)
551 {
552   GAppInfo *app_info;
553   GFile *file;
554   GList l;
555   gboolean res;
556
557   file = g_file_new_for_uri (uri);
558   app_info = g_file_query_default_handler (file, NULL, error);
559   g_object_unref (file);
560   if (app_info == NULL)
561     return FALSE;
562
563   /* Use the uri, not the GFile, as the GFile roundtrip may
564    * affect the uri which we don't want (for instance for a
565    * mailto: uri).
566    */
567   l.data = (char *)uri;
568   l.next = l.prev = NULL;
569   res = g_app_info_launch_uris (app_info, &l,
570                                 launch_context, error);
571
572   g_object_unref (app_info);
573   
574   return res;
575 }
576
577 /**
578  * g_app_info_can_delete:
579  * @appinfo: a #GAppInfo
580  *
581  * Obtains the information whether the GAppInfo can be deleted.
582  * See g_app_info_delete().
583  *
584  * Returns: %TRUE if @appinfo can be deleted
585  *
586  * Since: 2.20
587  */
588 gboolean
589 g_app_info_can_delete (GAppInfo *appinfo)
590 {
591   GAppInfoIface *iface;
592   
593   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
594
595   iface = G_APP_INFO_GET_IFACE (appinfo);
596
597   if (iface->can_delete)
598     return (* iface->can_delete) (appinfo);
599  
600   return FALSE; 
601 }
602
603
604 /**
605  * g_app_info_delete:
606  * @appinfo: a #GAppInfo
607  *
608  * Tries to delete an #GAppInfo. 
609  *
610  * On some platforms, there may be a difference between user-defined
611  * #GAppInfo<!-- -->s which can be deleted, and system-wide ones which
612  * cannot. See g_app_info_can_delete().
613  * 
614  * Returns: %TRUE if @appinfo has been deleted
615  *
616  * Since: 2.20
617  */
618 gboolean
619 g_app_info_delete (GAppInfo *appinfo)
620 {
621   GAppInfoIface *iface;
622   
623   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
624
625   iface = G_APP_INFO_GET_IFACE (appinfo);
626
627   if (iface->do_delete)
628     return (* iface->do_delete) (appinfo);
629  
630   return FALSE; 
631 }
632
633
634 G_DEFINE_TYPE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT);
635
636 /**
637  * g_app_launch_context_new:
638  * 
639  * Creates a new application launch context. This is not normally used,
640  * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
641  *
642  * Returns: a #GAppLaunchContext.
643  **/
644 GAppLaunchContext *
645 g_app_launch_context_new (void)
646 {
647   return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
648 }
649
650 static void
651 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
652 {
653 }
654
655 static void
656 g_app_launch_context_init (GAppLaunchContext *launch_context)
657 {
658 }
659
660 /**
661  * g_app_launch_context_get_display:
662  * @context: a #GAppLaunchContext
663  * @info: a #GAppInfo
664  * @files: a #GList of #GFile objects
665  *
666  * Gets the display string for the display. This is used to ensure new
667  * applications are started on the same display as the launching 
668  * application.
669  * 
670  * Returns: a display string for the display.
671  **/
672 char *
673 g_app_launch_context_get_display (GAppLaunchContext *context,
674                                   GAppInfo          *info,
675                                   GList             *files)
676 {
677   GAppLaunchContextClass *class;
678
679   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
680   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
681
682   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
683
684   if (class->get_display == NULL)
685     return NULL;
686
687   return class->get_display (context, info, files);
688 }
689
690 /**
691  * g_app_launch_context_get_startup_notify_id:
692  * @context: a #GAppLaunchContext
693  * @info: a #GAppInfo
694  * @files: a #GList of of #GFile objects
695  * 
696  * Initiates startup notification for the application and returns the
697  * DESKTOP_STARTUP_ID for the launched operation, if supported.
698  *
699  * Startup notification IDs are defined in the <ulink
700  * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">
701  * FreeDesktop.Org Startup Notifications standard</ulink>.
702  *
703  * Returns: a startup notification ID for the application, or %NULL if 
704  *     not supported.
705  **/
706 char *
707 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
708                                             GAppInfo          *info,
709                                             GList             *files)
710 {
711   GAppLaunchContextClass *class;
712
713   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
714   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
715
716   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
717
718   if (class->get_startup_notify_id == NULL)
719     return NULL;
720
721   return class->get_startup_notify_id (context, info, files);
722 }
723
724
725 /**
726  * g_app_launch_context_launch_failed:
727  * @context: a #GAppLaunchContext.
728  * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
729  *
730  * Called when an application has failed to launch, so that it can cancel
731  * the application startup notification started in g_app_launch_context_get_startup_notify_id().
732  * 
733  **/
734 void
735 g_app_launch_context_launch_failed (GAppLaunchContext *context,
736                                     const char        *startup_notify_id)
737 {
738   GAppLaunchContextClass *class;
739
740   g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
741   g_return_if_fail (startup_notify_id != NULL);
742
743   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
744
745   if (class->launch_failed != NULL)
746     class->launch_failed (context, startup_notify_id);
747 }
748
749
750 #define __G_APP_INFO_C__
751 #include "gioaliasdef.c"