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