Bumps documentation to 93% symbol coverage, touching most
[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 /**
29  * SECTION:gappinfo
30  * @short_description: Application information and launch contexts. 
31  * @stability: Unstable
32  * 
33  * #GAppInfo and #GAppLaunchContext are used for describing and launching 
34  * installed system applications. 
35  *
36  * @Note: These may/will be moved to Gtk+ in the future.
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.
138  *
139  * Returns: a string containing the application's ID.
140  **/
141 const char *
142 g_app_info_get_id (GAppInfo *appinfo)
143 {
144   GAppInfoIface *iface;
145   
146   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
147
148   iface = G_APP_INFO_GET_IFACE (appinfo);
149
150   return (* iface->get_id) (appinfo);
151 }
152
153 /**
154  * g_app_info_get_name:
155  * @appinfo: a #GAppInfo.
156  * 
157  * Gets the installed name of the application. 
158  *
159  * Returns: the name of the application for @appinfo.
160  **/
161 const char *
162 g_app_info_get_name (GAppInfo *appinfo)
163 {
164   GAppInfoIface *iface;
165   
166   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
167
168   iface = G_APP_INFO_GET_IFACE (appinfo);
169
170   return (* iface->get_name) (appinfo);
171 }
172
173 /**
174  * g_app_info_get_description:
175  * @appinfo: a #GAppInfo.
176  * 
177  * Gets a human-readable description of an installed application.
178  *
179  * Returns: a string containing a description of the 
180  * application @appinfo. The returned string should be not freed 
181  * when no longer needed.
182  **/
183 const char *
184 g_app_info_get_description (GAppInfo *appinfo)
185 {
186   GAppInfoIface *iface;
187   
188   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
189
190   iface = G_APP_INFO_GET_IFACE (appinfo);
191
192   return (* iface->get_description) (appinfo);
193 }
194
195 /**
196  * g_app_info_get_executable:
197  * @appinfo: a #GAppInfo.
198  * 
199  * Gets the executable's name for the installed application.
200  *
201  * Returns: a string containing the @appinfo's application 
202  * binary's name.
203  **/
204 const char *
205 g_app_info_get_executable (GAppInfo *appinfo)
206 {
207   GAppInfoIface *iface;
208   
209   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
210
211   iface = G_APP_INFO_GET_IFACE (appinfo);
212
213   return (* iface->get_executable) (appinfo);
214 }
215
216
217 /**
218  * g_app_info_set_as_default_for_type:
219  * @appinfo: a #GAppInfo.
220  * @content_type: the content type.
221  * @error: a #GError.
222  * 
223  * Sets an application as the default handler for a given type.
224  *
225  * Returns: %TRUE if the given @appinfo is the default 
226  * for the given @content_type. %FALSE if not, 
227  * or in case of an error.
228  **/
229 gboolean
230 g_app_info_set_as_default_for_type (GAppInfo    *appinfo,
231                                     const char  *content_type,
232                                     GError     **error)
233 {
234   GAppInfoIface *iface;
235   
236   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
237   g_return_val_if_fail (content_type != NULL, FALSE);
238
239   iface = G_APP_INFO_GET_IFACE (appinfo);
240
241   return (* iface->set_as_default_for_type) (appinfo, content_type, error);
242 }
243
244
245 /**
246  * g_app_info_set_as_default_for_extension:
247  * @appinfo: a #GAppInfo.
248  * @extension: a string containing the file extension.
249  * @error: a #GError.
250  * 
251  * Sets an application as the default handler for the given file extention.
252  *
253  * Returns: %TRUE if the given @appinfo is the default 
254  * for the given @extension. %FALSE if not, 
255  * or in case of an error.
256  **/
257 gboolean
258 g_app_info_set_as_default_for_extension (GAppInfo  *appinfo,
259                                          const char *extension,
260                                          GError **error)
261 {
262   GAppInfoIface *iface;
263   
264   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
265   g_return_val_if_fail (extension != NULL, FALSE);
266
267   iface = G_APP_INFO_GET_IFACE (appinfo);
268
269   if (iface->set_as_default_for_extension)
270     return (* iface->set_as_default_for_extension) (appinfo, extension, error);
271
272   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "g_app_info_set_as_default_for_extension not supported yet");
273   return FALSE;
274 }
275
276
277 /**
278  * g_app_info_add_supports_type:
279  * @appinfo: a #GAppInfo.
280  * @content_type: a string.
281  * @error: a #GError.
282  * 
283  * Adds a content type to the application information to indicate the 
284  * application is capable of opening files with the given content type.
285  *
286  * Returns: %TRUE if @appinfo supports @content_type.
287  * %FALSE if not, or in case of an 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, G_IO_ERROR_NOT_SUPPORTED, "g_app_info_add_supports_type not supported yet");
305   return FALSE;
306 }
307
308
309 /**
310  * g_app_info_can_remove_support_type:
311  * @appinfo: a #GAppInfo.
312  * 
313  * Checks if a supported content type can be removed from an application.
314  *
315  * Returns: %TRUE if it is possible to remove supported 
316  * content types from a given @appinfo, %FALSE if not.
317  **/
318 gboolean
319 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
320 {
321   GAppInfoIface *iface;
322   
323   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
324
325   iface = G_APP_INFO_GET_IFACE (appinfo);
326
327   if (iface->can_remove_supports_type)
328     return (* iface->can_remove_supports_type) (appinfo);
329
330   return FALSE;
331 }
332
333
334 /**
335  * g_app_info_remove_supports_type:
336  * @appinfo: a #GAppInfo.
337  * @content_type: a string.
338  * @error: a #GError.
339  *
340  * Removes a supported type from an application, if possible.
341  * 
342  * Returns: %TRUE if @content_type support was removed
343  * from @appinfo. %FALSE if not.
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, G_IO_ERROR_NOT_SUPPORTED, "g_app_info_remove_supports_type not supported yet");
361   return FALSE;
362 }
363
364
365 /**
366  * g_app_info_get_icon:
367  * @appinfo: a #GAppInfo.
368  * 
369  * Gets the default icon for the application.
370  *
371  * Returns: the default #GIcon for @appinfo.
372  **/
373 GIcon *
374 g_app_info_get_icon (GAppInfo *appinfo)
375 {
376   GAppInfoIface *iface;
377   
378   g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
379
380   iface = G_APP_INFO_GET_IFACE (appinfo);
381
382   return (* iface->get_icon) (appinfo);
383 }
384
385
386 /**
387  * g_app_info_launch:
388  * @appinfo: a #GAppInfo.
389  * @files: a #GList of #GFile objects.
390  * @launch_context: a #GAppLaunchContext.
391  * @error: a #GError.
392  * 
393  * Launches the application. Passes @files to the launched application 
394  * as arguments, and loads the @launch_context for managing the application
395  * once it has been launched. On error, @error will be set accordingly.
396  *
397  * Returns: %TRUE on successful launch, %FALSE otherwise.
398  **/
399 gboolean
400 g_app_info_launch (GAppInfo    *appinfo,
401                    GList       *files,
402                    GAppLaunchContext *launch_context,
403                    GError     **error)
404 {
405   GAppInfoIface *iface;
406   
407   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
408
409   iface = G_APP_INFO_GET_IFACE (appinfo);
410
411   return (* iface->launch) (appinfo, files, launch_context, error);
412 }
413
414
415 /**
416  * g_app_info_supports_uris:
417  * @appinfo: a #GAppInfo.
418  * 
419  * Checks if the application supports reading files and directories from URIs.
420  *
421  * Returns: %TRUE if the @appinfo supports URIs.
422  **/
423 gboolean
424 g_app_info_supports_uris (GAppInfo *appinfo)
425 {
426   GAppInfoIface *iface;
427   
428   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
429
430   iface = G_APP_INFO_GET_IFACE (appinfo);
431
432   return (* iface->supports_uris) (appinfo);
433 }
434
435
436 /**
437  * g_app_info_launch_uris:
438  * @appinfo: a #GAppInfo.
439  * @uris: a #GList containing URIs to launch. 
440  * @launch_context: a #GAppLaunchContext.
441  * @error: a #GError.
442  * 
443  * Launches the application. Passes @uris to the launched application 
444  * as arguments, and loads the @launch_context for managing the application
445  * once it has been launched. On error, @error will be set accordingly.
446  *
447  * Returns: %TRUE if the @appinfo was launched successfully, %FALSE otherwise.
448  **/
449 gboolean
450 g_app_info_launch_uris (GAppInfo    *appinfo,
451                         GList       *uris,
452                         GAppLaunchContext *launch_context,
453                         GError     **error)
454 {
455   GAppInfoIface *iface;
456   
457   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
458
459   iface = G_APP_INFO_GET_IFACE (appinfo);
460
461   return (* iface->launch) (appinfo, uris, launch_context, error);
462 }
463
464
465 /**
466  * g_app_info_should_show:
467  * @appinfo: a #GAppInfo.
468  * @desktop_env: a string.
469  *
470  * Checks if the application info should be shown when listing
471  * applications available.
472  * 
473  * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
474  **/
475 gboolean
476 g_app_info_should_show (GAppInfo    *appinfo,
477                         const char  *desktop_env)
478 {
479   GAppInfoIface *iface;
480   
481   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
482
483   iface = G_APP_INFO_GET_IFACE (appinfo);
484
485   return (* iface->should_show) (appinfo, desktop_env);
486 }
487
488 G_DEFINE_TYPE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT);
489
490 /**
491  * g_app_launch_context_new:
492  * 
493  * Creates a new application launch context.
494  *
495  * Returns: a #GAppLaunchContext.
496  **/
497 GAppLaunchContext *
498 g_app_launch_context_new (void)
499 {
500   return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
501 }
502
503 static void
504 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
505 {
506 }
507
508 static void
509 g_app_launch_context_init (GAppLaunchContext *launch_context)
510 {
511 }
512
513 /**
514  * g_app_launch_context_get_display:
515  * @context: a #GAppLaunchContext.  
516  * @info: a #GAppInfo. 
517  * @files: a #GList of files.
518  *
519  * Gets the DISPLAY for a launched application.
520  * TODO: can't find an implementation. DISPLAY as in X atom "DISPLAY"?
521  * Environmental variable "DISPLAY"?
522  * 
523  * Returns: a display string.
524  **/
525 char *
526 g_app_launch_context_get_display (GAppLaunchContext *context,
527                                   GAppInfo          *info,
528                                   GList             *files)
529 {
530   GAppLaunchContextClass *class;
531
532   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
533   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
534
535   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
536
537   if (class->get_display == NULL)
538     return NULL;
539
540   return class->get_display (context, info, files);
541 }
542
543 /* should this be moved to the g_desktop_app_ implementation? */
544 /**
545  * g_app_launch_context_get_startup_notify_id:
546  * @context: a #GAppLaunchContext.
547  * @info: a #GAppInfo.
548  * @files: a #GList of files.
549  * 
550  * Gets the DESKTOP_STARTUP_ID for the launched application, if supported. 
551  * Startup notification IDs are defined in the FreeDesktop.Org Startup Notifications standard, 
552  * at <ulink url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt"/>.
553  *
554  * Returns: a startup notifaction ID for the application, or %NULL if not supported.
555  **/
556 char *
557 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
558                                             GAppInfo          *info,
559                                             GList             *files)
560 {
561   GAppLaunchContextClass *class;
562
563   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
564   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
565
566   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
567
568   if (class->get_startup_notify_id == NULL)
569     return NULL;
570
571   return class->get_startup_notify_id (context, info, files);
572 }
573
574
575 /**
576  * g_app_launch_context_launch_failed:
577  * @context: a #GAppLaunchContext.
578  * @startup_notify_id: a string containing the DESKTOP_STARTUP_ID of the launched application.
579  *
580  * TODO: what does this do? Can't find it implemented anywhere.
581  * 
582  **/
583 void
584 g_app_launch_context_launch_failed (GAppLaunchContext *context,
585                                     const char *startup_notify_id)
586 {
587   GAppLaunchContextClass *class;
588
589   g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
590   g_return_if_fail (startup_notify_id != NULL);
591
592   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
593
594   if (class->launch_failed != NULL)
595     class->launch_failed (context, startup_notify_id);
596 }