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