1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
33 * @short_description: Application information and launch contexts
36 * #GAppInfo and #GAppLaunchContext are used for describing and launching
37 * applications installed on the system.
39 * As of GLib 2.20, URIs will always be converted to POSIX paths
40 * (using g_file_get_path()) when using g_app_info_launch() even if
41 * the application requested an URI and not a POSIX path. For example
42 * for an desktop-file based application with Exec key <literal>totem
43 * %%U</literal> and a single URI,
44 * <literal>sftp://foo/file.avi</literal>, then
45 * <literal>/home/user/.gvfs/sftp on foo/file.avi</literal> will be
46 * passed. This will only work if a set of suitable GIO extensions
47 * (such as gvfs 2.26 compiled with FUSE support), is available and
48 * operational; if this is not the case, the URI will be passed
49 * unmodified to the application. Some URIs, such as
50 * <literal>mailto:</literal>, of course cannot be mapped to a POSIX
51 * path (in gvfs there's no FUSE mount for it); such URIs will be
52 * passed unmodified to the application.
54 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
55 * back to the GIO URI in the #GFile constructors (since gvfs
56 * implements the #GVfs extension point). As such, if the application
57 * needs to examine the URI, it needs to use g_file_get_uri() or
58 * similar on #GFile. In other words, an application cannot assume
59 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
60 * equal to the result of g_file_get_uri(). The following snippet
67 * file = g_file_new_for_commandline_arg (uri_from_commandline);
69 * uri = g_file_get_uri (file);
70 * strcmp (uri, uri_from_commandline) == 0; // FALSE
73 * if (g_file_has_uri_scheme (file, "cdda"))
75 * // do something special with uri
77 * g_object_unref (file);
80 * This code will work when both <literal>cdda://sr0/Track
81 * 1.wav</literal> and <literal>/home/user/.gvfs/cdda on sr0/Track
82 * 1.wav</literal> is passed to the application. It should be noted
83 * that it's generally not safe for applications to rely on the format
84 * of a particular URIs. Different launcher applications (e.g. file
85 * managers) may have different ideas of what a given URI means.
89 static void g_app_info_base_init (gpointer g_class);
90 static void g_app_info_class_init (gpointer g_class,
95 g_app_info_get_type (void)
97 static volatile gsize g_define_type_id__volatile = 0;
99 if (g_once_init_enter (&g_define_type_id__volatile))
101 const GTypeInfo app_info_info =
103 sizeof (GAppInfoIface), /* class_size */
104 g_app_info_base_init, /* base_init */
105 NULL, /* base_finalize */
106 g_app_info_class_init,
107 NULL, /* class_finalize */
108 NULL, /* class_data */
113 GType g_define_type_id =
114 g_type_register_static (G_TYPE_INTERFACE, I_("GAppInfo"),
117 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
119 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
122 return g_define_type_id__volatile;
126 g_app_info_class_init (gpointer g_class,
132 g_app_info_base_init (gpointer g_class)
139 * @appinfo: a #GAppInfo.
141 * Creates a duplicate of a #GAppInfo.
143 * Returns: a duplicate of @appinfo.
146 g_app_info_dup (GAppInfo *appinfo)
148 GAppInfoIface *iface;
150 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
152 iface = G_APP_INFO_GET_IFACE (appinfo);
154 return (* iface->dup) (appinfo);
159 * @appinfo1: the first #GAppInfo.
160 * @appinfo2: the second #GAppInfo.
162 * Checks if two #GAppInfos are equal.
164 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
167 g_app_info_equal (GAppInfo *appinfo1,
170 GAppInfoIface *iface;
172 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
173 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
175 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
178 iface = G_APP_INFO_GET_IFACE (appinfo1);
180 return (* iface->equal) (appinfo1, appinfo2);
185 * @appinfo: a #GAppInfo.
187 * Gets the ID of an application. An id is a string that
188 * identifies the application. The exact format of the id is
189 * platform dependent. For instance, on Unix this is the
190 * desktop file id from the xdg menu specification.
192 * Note that the returned ID may be %NULL, depending on how
193 * the @appinfo has been constructed.
195 * Returns: a string containing the application's ID.
198 g_app_info_get_id (GAppInfo *appinfo)
200 GAppInfoIface *iface;
202 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
204 iface = G_APP_INFO_GET_IFACE (appinfo);
206 return (* iface->get_id) (appinfo);
210 * g_app_info_get_name:
211 * @appinfo: a #GAppInfo.
213 * Gets the installed name of the application.
215 * Returns: the name of the application for @appinfo.
218 g_app_info_get_name (GAppInfo *appinfo)
220 GAppInfoIface *iface;
222 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
224 iface = G_APP_INFO_GET_IFACE (appinfo);
226 return (* iface->get_name) (appinfo);
230 * g_app_info_get_description:
231 * @appinfo: a #GAppInfo.
233 * Gets a human-readable description of an installed application.
235 * Returns: a string containing a description of the
236 * application @appinfo, or %NULL if none.
239 g_app_info_get_description (GAppInfo *appinfo)
241 GAppInfoIface *iface;
243 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
245 iface = G_APP_INFO_GET_IFACE (appinfo);
247 return (* iface->get_description) (appinfo);
251 * g_app_info_get_executable:
252 * @appinfo: a #GAppInfo
254 * Gets the executable's name for the installed application.
256 * Returns: a string containing the @appinfo's application
260 g_app_info_get_executable (GAppInfo *appinfo)
262 GAppInfoIface *iface;
264 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
266 iface = G_APP_INFO_GET_IFACE (appinfo);
268 return (* iface->get_executable) (appinfo);
273 * g_app_info_get_commandline:
274 * @appinfo: a #GAppInfo
276 * Gets the commandline with which the application will be
279 * Returns: a string containing the @appinfo's commandline,
280 * or %NULL if this information is not available
285 g_app_info_get_commandline (GAppInfo *appinfo)
287 GAppInfoIface *iface;
289 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
291 iface = G_APP_INFO_GET_IFACE (appinfo);
293 if (iface->get_commandline)
294 return (* iface->get_commandline) (appinfo);
300 * g_app_info_set_as_default_for_type:
301 * @appinfo: a #GAppInfo.
302 * @content_type: the content type.
305 * Sets the application as the default handler for a given type.
307 * Returns: %TRUE on success, %FALSE on error.
310 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
311 const char *content_type,
314 GAppInfoIface *iface;
316 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
317 g_return_val_if_fail (content_type != NULL, FALSE);
319 iface = G_APP_INFO_GET_IFACE (appinfo);
321 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
326 * g_app_info_set_as_default_for_extension:
327 * @appinfo: a #GAppInfo.
328 * @extension: a string containing the file extension (without the dot).
331 * Sets the application as the default handler for the given file extention.
333 * Returns: %TRUE on success, %FALSE on error.
336 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
337 const char *extension,
340 GAppInfoIface *iface;
342 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
343 g_return_val_if_fail (extension != NULL, FALSE);
345 iface = G_APP_INFO_GET_IFACE (appinfo);
347 if (iface->set_as_default_for_extension)
348 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
350 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
351 "g_app_info_set_as_default_for_extension not supported yet");
357 * g_app_info_add_supports_type:
358 * @appinfo: a #GAppInfo.
359 * @content_type: a string.
362 * Adds a content type to the application information to indicate the
363 * application is capable of opening files with the given content type.
365 * Returns: %TRUE on success, %FALSE on error.
368 g_app_info_add_supports_type (GAppInfo *appinfo,
369 const char *content_type,
372 GAppInfoIface *iface;
374 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
375 g_return_val_if_fail (content_type != NULL, FALSE);
377 iface = G_APP_INFO_GET_IFACE (appinfo);
379 if (iface->add_supports_type)
380 return (* iface->add_supports_type) (appinfo, content_type, error);
382 g_set_error_literal (error, G_IO_ERROR,
383 G_IO_ERROR_NOT_SUPPORTED,
384 "g_app_info_add_supports_type not supported yet");
391 * g_app_info_can_remove_supports_type:
392 * @appinfo: a #GAppInfo.
394 * Checks if a supported content type can be removed from an application.
396 * Returns: %TRUE if it is possible to remove supported
397 * content types from a given @appinfo, %FALSE if not.
400 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
402 GAppInfoIface *iface;
404 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
406 iface = G_APP_INFO_GET_IFACE (appinfo);
408 if (iface->can_remove_supports_type)
409 return (* iface->can_remove_supports_type) (appinfo);
416 * g_app_info_remove_supports_type:
417 * @appinfo: a #GAppInfo.
418 * @content_type: a string.
421 * Removes a supported type from an application, if possible.
423 * Returns: %TRUE on success, %FALSE on error.
426 g_app_info_remove_supports_type (GAppInfo *appinfo,
427 const char *content_type,
430 GAppInfoIface *iface;
432 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
433 g_return_val_if_fail (content_type != NULL, FALSE);
435 iface = G_APP_INFO_GET_IFACE (appinfo);
437 if (iface->remove_supports_type)
438 return (* iface->remove_supports_type) (appinfo, content_type, error);
440 g_set_error_literal (error, G_IO_ERROR,
441 G_IO_ERROR_NOT_SUPPORTED,
442 "g_app_info_remove_supports_type not supported yet");
449 * g_app_info_get_icon:
450 * @appinfo: a #GAppInfo.
452 * Gets the icon for the application.
454 * Returns: the default #GIcon for @appinfo.
457 g_app_info_get_icon (GAppInfo *appinfo)
459 GAppInfoIface *iface;
461 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
463 iface = G_APP_INFO_GET_IFACE (appinfo);
465 return (* iface->get_icon) (appinfo);
471 * @appinfo: a #GAppInfo
472 * @files: a #GList of #GFile objects
473 * @launch_context: a #GAppLaunchContext or %NULL
476 * Launches the application. Passes @files to the launched application
477 * as arguments, using the optional @launch_context to get information
478 * about the details of the launcher (like what screen it is on).
479 * On error, @error will be set accordingly.
481 * To lauch the application without arguments pass a %NULL @files list.
483 * Note that even if the launch is successful the application launched
484 * can fail to start if it runs into problems during startup. There is
485 * no way to detect this.
487 * Some URIs can be changed when passed through a GFile (for instance
488 * unsupported uris with strange formats like mailto:), so if you have
489 * a textual uri you want to pass in as argument, consider using
490 * g_app_info_launch_uris() instead.
492 * Returns: %TRUE on successful launch, %FALSE otherwise.
495 g_app_info_launch (GAppInfo *appinfo,
497 GAppLaunchContext *launch_context,
500 GAppInfoIface *iface;
502 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
504 iface = G_APP_INFO_GET_IFACE (appinfo);
506 return (* iface->launch) (appinfo, files, launch_context, error);
511 * g_app_info_supports_uris:
512 * @appinfo: a #GAppInfo.
514 * Checks if the application supports reading files and directories from URIs.
516 * Returns: %TRUE if the @appinfo supports URIs.
519 g_app_info_supports_uris (GAppInfo *appinfo)
521 GAppInfoIface *iface;
523 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
525 iface = G_APP_INFO_GET_IFACE (appinfo);
527 return (* iface->supports_uris) (appinfo);
532 * g_app_info_supports_files:
533 * @appinfo: a #GAppInfo.
535 * Checks if the application accepts files as arguments.
537 * Returns: %TRUE if the @appinfo supports files.
540 g_app_info_supports_files (GAppInfo *appinfo)
542 GAppInfoIface *iface;
544 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
546 iface = G_APP_INFO_GET_IFACE (appinfo);
548 return (* iface->supports_files) (appinfo);
553 * g_app_info_launch_uris:
554 * @appinfo: a #GAppInfo
555 * @uris: a #GList containing URIs to launch.
556 * @launch_context: a #GAppLaunchContext or %NULL
559 * Launches the application. Passes @uris to the launched application
560 * as arguments, using the optional @launch_context to get information
561 * about the details of the launcher (like what screen it is on).
562 * On error, @error will be set accordingly.
564 * To lauch the application without arguments pass a %NULL @uris list.
566 * Note that even if the launch is successful the application launched
567 * can fail to start if it runs into problems during startup. There is
568 * no way to detect this.
570 * Returns: %TRUE on successful launch, %FALSE otherwise.
573 g_app_info_launch_uris (GAppInfo *appinfo,
575 GAppLaunchContext *launch_context,
578 GAppInfoIface *iface;
580 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
582 iface = G_APP_INFO_GET_IFACE (appinfo);
584 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
589 * g_app_info_should_show:
590 * @appinfo: a #GAppInfo.
592 * Checks if the application info should be shown in menus that
593 * list available applications.
595 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
598 g_app_info_should_show (GAppInfo *appinfo)
600 GAppInfoIface *iface;
602 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
604 iface = G_APP_INFO_GET_IFACE (appinfo);
606 return (* iface->should_show) (appinfo);
610 * g_app_info_launch_default_for_uri:
611 * @uri: the uri to show
612 * @launch_context: an optional #GAppLaunchContext.
615 * Utility function that launches the default application
616 * registered to handle the specified uri. Synchronous I/O
617 * is done on the uri to detect the type of the file if
620 * Returns: %TRUE on success, %FALSE on error.
623 g_app_info_launch_default_for_uri (const char *uri,
624 GAppLaunchContext *launch_context,
632 file = g_file_new_for_uri (uri);
633 app_info = g_file_query_default_handler (file, NULL, error);
634 g_object_unref (file);
635 if (app_info == NULL)
638 /* Use the uri, not the GFile, as the GFile roundtrip may
639 * affect the uri which we don't want (for instance for a
642 l.data = (char *)uri;
643 l.next = l.prev = NULL;
644 res = g_app_info_launch_uris (app_info, &l,
645 launch_context, error);
647 g_object_unref (app_info);
653 * g_app_info_can_delete:
654 * @appinfo: a #GAppInfo
656 * Obtains the information whether the GAppInfo can be deleted.
657 * See g_app_info_delete().
659 * Returns: %TRUE if @appinfo can be deleted
664 g_app_info_can_delete (GAppInfo *appinfo)
666 GAppInfoIface *iface;
668 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
670 iface = G_APP_INFO_GET_IFACE (appinfo);
672 if (iface->can_delete)
673 return (* iface->can_delete) (appinfo);
681 * @appinfo: a #GAppInfo
683 * Tries to delete an #GAppInfo.
685 * On some platforms, there may be a difference between user-defined
686 * #GAppInfo<!-- -->s which can be deleted, and system-wide ones which
687 * cannot. See g_app_info_can_delete().
689 * Returns: %TRUE if @appinfo has been deleted
694 g_app_info_delete (GAppInfo *appinfo)
696 GAppInfoIface *iface;
698 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
700 iface = G_APP_INFO_GET_IFACE (appinfo);
702 if (iface->do_delete)
703 return (* iface->do_delete) (appinfo);
709 G_DEFINE_TYPE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT);
712 * g_app_launch_context_new:
714 * Creates a new application launch context. This is not normally used,
715 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
717 * Returns: a #GAppLaunchContext.
720 g_app_launch_context_new (void)
722 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
726 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
731 g_app_launch_context_init (GAppLaunchContext *launch_context)
736 * g_app_launch_context_get_display:
737 * @context: a #GAppLaunchContext
739 * @files: a #GList of #GFile objects
741 * Gets the display string for the display. This is used to ensure new
742 * applications are started on the same display as the launching
745 * Returns: a display string for the display.
748 g_app_launch_context_get_display (GAppLaunchContext *context,
752 GAppLaunchContextClass *class;
754 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
755 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
757 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
759 if (class->get_display == NULL)
762 return class->get_display (context, info, files);
766 * g_app_launch_context_get_startup_notify_id:
767 * @context: a #GAppLaunchContext
769 * @files: a #GList of of #GFile objects
771 * Initiates startup notification for the application and returns the
772 * DESKTOP_STARTUP_ID for the launched operation, if supported.
774 * Startup notification IDs are defined in the <ulink
775 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">
776 * FreeDesktop.Org Startup Notifications standard</ulink>.
778 * Returns: a startup notification ID for the application, or %NULL if
782 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
786 GAppLaunchContextClass *class;
788 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
789 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
791 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
793 if (class->get_startup_notify_id == NULL)
796 return class->get_startup_notify_id (context, info, files);
801 * g_app_launch_context_launch_failed:
802 * @context: a #GAppLaunchContext.
803 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
805 * Called when an application has failed to launch, so that it can cancel
806 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
810 g_app_launch_context_launch_failed (GAppLaunchContext *context,
811 const char *startup_notify_id)
813 GAppLaunchContextClass *class;
815 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
816 g_return_if_fail (startup_notify_id != NULL);
818 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
820 if (class->launch_failed != NULL)
821 class->launch_failed (context, startup_notify_id);
825 #define __G_APP_INFO_C__
826 #include "gioaliasdef.c"