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