Fixes unknown meaning in GAppLaunchContext docs. Clarify asynchronous ops.
[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  * @stability: Unstable
34  * 
35  * #GAppInfo and #GAppLaunchContext are used for describing and launching 
36  * installed system applications. 
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, 
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 if @content_type support was removed
346  *     from @appinfo. %FALSE if not.
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 default 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, and loads the @launch_context for managing the application
401  * once it has been launched. On error, @error will be set accordingly.
402  *
403  * Returns: %TRUE on successful launch, %FALSE otherwise.
404  **/
405 gboolean
406 g_app_info_launch (GAppInfo           *appinfo,
407                    GList              *files,
408                    GAppLaunchContext  *launch_context,
409                    GError            **error)
410 {
411   GAppInfoIface *iface;
412   
413   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
414
415   iface = G_APP_INFO_GET_IFACE (appinfo);
416
417   return (* iface->launch) (appinfo, files, launch_context, error);
418 }
419
420
421 /**
422  * g_app_info_supports_uris:
423  * @appinfo: a #GAppInfo.
424  * 
425  * Checks if the application supports reading files and directories from URIs.
426  *
427  * Returns: %TRUE if the @appinfo supports URIs.
428  **/
429 gboolean
430 g_app_info_supports_uris (GAppInfo *appinfo)
431 {
432   GAppInfoIface *iface;
433   
434   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
435
436   iface = G_APP_INFO_GET_IFACE (appinfo);
437
438   return (* iface->supports_uris) (appinfo);
439 }
440
441
442 /**
443  * g_app_info_launch_uris:
444  * @appinfo: a #GAppInfo.
445  * @uris: a #GList containing URIs to launch. 
446  * @launch_context: a #GAppLaunchContext.
447  * @error: a #GError.
448  * 
449  * Launches the application. Passes @uris to the launched application 
450  * as arguments, and loads the @launch_context for managing the application
451  * once it has been launched. On error, @error will be set accordingly.
452  *
453  * Returns: %TRUE if the @appinfo was launched successfully, %FALSE otherwise.
454  **/
455 gboolean
456 g_app_info_launch_uris (GAppInfo           *appinfo,
457                         GList              *uris,
458                         GAppLaunchContext  *launch_context,
459                         GError            **error)
460 {
461   GAppInfoIface *iface;
462   
463   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
464
465   iface = G_APP_INFO_GET_IFACE (appinfo);
466
467   return (* iface->launch) (appinfo, uris, launch_context, error);
468 }
469
470
471 /**
472  * g_app_info_should_show:
473  * @appinfo: a #GAppInfo.
474  * @desktop_env: a string.
475  *
476  * Checks if the application info should be shown when listing
477  * applications available.
478  * 
479  * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
480  **/
481 gboolean
482 g_app_info_should_show (GAppInfo   *appinfo,
483                         const char *desktop_env)
484 {
485   GAppInfoIface *iface;
486   
487   g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
488
489   iface = G_APP_INFO_GET_IFACE (appinfo);
490
491   return (* iface->should_show) (appinfo, desktop_env);
492 }
493
494 G_DEFINE_TYPE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT);
495
496 /**
497  * g_app_launch_context_new:
498  * 
499  * Creates a new application launch context.
500  *
501  * Returns: a #GAppLaunchContext.
502  **/
503 GAppLaunchContext *
504 g_app_launch_context_new (void)
505 {
506   return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
507 }
508
509 static void
510 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
511 {
512 }
513
514 static void
515 g_app_launch_context_init (GAppLaunchContext *launch_context)
516 {
517 }
518
519 /**
520  * g_app_launch_context_get_display:
521  * @context: a #GAppLaunchContext.  
522  * @info: a #GAppInfo. 
523  * @files: a #GList of files.
524  *
525  * Gets the display string for the display. This is used to ensure new
526  * applications are started on the same display as the launching 
527  * application.
528  * 
529  * Returns: a display string for the display.
530  **/
531 char *
532 g_app_launch_context_get_display (GAppLaunchContext *context,
533                                   GAppInfo          *info,
534                                   GList             *files)
535 {
536   GAppLaunchContextClass *class;
537
538   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
539   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
540
541   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
542
543   if (class->get_display == NULL)
544     return NULL;
545
546   return class->get_display (context, info, files);
547 }
548
549 /* should this be moved to the g_desktop_app_ implementation? */
550 /**
551  * g_app_launch_context_get_startup_notify_id:
552  * @context: a #GAppLaunchContext.
553  * @info: a #GAppInfo.
554  * @files: a #GList of files.
555  * 
556  * Gets the DESKTOP_STARTUP_ID for the launched application, if supported. 
557  * Startup notification IDs are defined in the FreeDesktop.Org Startup 
558  * Notifications standard, at 
559  * <ulink url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt"/>.
560  *
561  * Returns: a startup notification ID for the application, or %NULL if 
562  *     not supported.
563  **/
564 char *
565 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
566                                             GAppInfo          *info,
567                                             GList             *files)
568 {
569   GAppLaunchContextClass *class;
570
571   g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
572   g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
573
574   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
575
576   if (class->get_startup_notify_id == NULL)
577     return NULL;
578
579   return class->get_startup_notify_id (context, info, files);
580 }
581
582
583 /**
584  * g_app_launch_context_launch_failed:
585  * @context: a #GAppLaunchContext.
586  * @startup_notify_id: a string containing the DESKTOP_STARTUP_ID 
587  *     of the launched application.
588  *
589  * Called when an application has failed to launch, and cancels the 
590  * application startup notification.
591  * 
592  **/
593 void
594 g_app_launch_context_launch_failed (GAppLaunchContext *context,
595                                     const char        *startup_notify_id)
596 {
597   GAppLaunchContextClass *class;
598
599   g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
600   g_return_if_fail (startup_notify_id != NULL);
601
602   class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
603
604   if (class->launch_failed != NULL)
605     class->launch_failed (context, startup_notify_id);
606 }
607
608 #define __G_APP_INFO_C__
609 #include "gioaliasdef.c"