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 typedef GAppInfoIface GAppInfoInterface;
90 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
93 g_app_info_default_init (GAppInfoInterface *iface)
100 * @appinfo: a #GAppInfo.
102 * Creates a duplicate of a #GAppInfo.
104 * Returns: a duplicate of @appinfo.
107 g_app_info_dup (GAppInfo *appinfo)
109 GAppInfoIface *iface;
111 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
113 iface = G_APP_INFO_GET_IFACE (appinfo);
115 return (* iface->dup) (appinfo);
120 * @appinfo1: the first #GAppInfo.
121 * @appinfo2: the second #GAppInfo.
123 * Checks if two #GAppInfo<!-- -->s are equal.
125 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
128 g_app_info_equal (GAppInfo *appinfo1,
131 GAppInfoIface *iface;
133 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
134 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
136 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
139 iface = G_APP_INFO_GET_IFACE (appinfo1);
141 return (* iface->equal) (appinfo1, appinfo2);
146 * @appinfo: a #GAppInfo.
148 * Gets the ID of an application. An id is a string that
149 * identifies the application. The exact format of the id is
150 * platform dependent. For instance, on Unix this is the
151 * desktop file id from the xdg menu specification.
153 * Note that the returned ID may be %NULL, depending on how
154 * the @appinfo has been constructed.
156 * Returns: a string containing the application's ID.
159 g_app_info_get_id (GAppInfo *appinfo)
161 GAppInfoIface *iface;
163 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
165 iface = G_APP_INFO_GET_IFACE (appinfo);
167 return (* iface->get_id) (appinfo);
171 * g_app_info_get_name:
172 * @appinfo: a #GAppInfo.
174 * Gets the installed name of the application.
176 * Returns: the name of the application for @appinfo.
179 g_app_info_get_name (GAppInfo *appinfo)
181 GAppInfoIface *iface;
183 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
185 iface = G_APP_INFO_GET_IFACE (appinfo);
187 return (* iface->get_name) (appinfo);
191 * g_app_info_get_display_name:
192 * @appinfo: a #GAppInfo.
194 * Gets the display name of the application. The display name is often more
195 * descriptive to the user than the name itself.
197 * Returns: the display name of the application for @appinfo, or the name if
198 * no display name is available.
203 g_app_info_get_display_name (GAppInfo *appinfo)
205 GAppInfoIface *iface;
207 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
209 iface = G_APP_INFO_GET_IFACE (appinfo);
211 if (iface->get_display_name == NULL)
212 return (* iface->get_name) (appinfo);
214 return (* iface->get_display_name) (appinfo);
218 * g_app_info_get_description:
219 * @appinfo: a #GAppInfo.
221 * Gets a human-readable description of an installed application.
223 * Returns: a string containing a description of the
224 * application @appinfo, or %NULL if none.
227 g_app_info_get_description (GAppInfo *appinfo)
229 GAppInfoIface *iface;
231 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
233 iface = G_APP_INFO_GET_IFACE (appinfo);
235 return (* iface->get_description) (appinfo);
239 * g_app_info_get_executable:
240 * @appinfo: a #GAppInfo
242 * Gets the executable's name for the installed application.
244 * Returns: a string containing the @appinfo's application
248 g_app_info_get_executable (GAppInfo *appinfo)
250 GAppInfoIface *iface;
252 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
254 iface = G_APP_INFO_GET_IFACE (appinfo);
256 return (* iface->get_executable) (appinfo);
261 * g_app_info_get_commandline:
262 * @appinfo: a #GAppInfo
264 * Gets the commandline with which the application will be
267 * Returns: a string containing the @appinfo's commandline,
268 * or %NULL if this information is not available
273 g_app_info_get_commandline (GAppInfo *appinfo)
275 GAppInfoIface *iface;
277 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
279 iface = G_APP_INFO_GET_IFACE (appinfo);
281 if (iface->get_commandline)
282 return (* iface->get_commandline) (appinfo);
288 * g_app_info_set_as_default_for_type:
289 * @appinfo: a #GAppInfo.
290 * @content_type: the content type.
293 * Sets the application as the default handler for a given type.
295 * Returns: %TRUE on success, %FALSE on error.
298 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
299 const char *content_type,
302 GAppInfoIface *iface;
304 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
305 g_return_val_if_fail (content_type != NULL, FALSE);
307 iface = G_APP_INFO_GET_IFACE (appinfo);
309 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
314 * g_app_info_set_as_default_for_extension:
315 * @appinfo: a #GAppInfo.
316 * @extension: a string containing the file extension (without the dot).
319 * Sets the application as the default handler for the given file extension.
321 * Returns: %TRUE on success, %FALSE on error.
324 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
325 const char *extension,
328 GAppInfoIface *iface;
330 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
331 g_return_val_if_fail (extension != NULL, FALSE);
333 iface = G_APP_INFO_GET_IFACE (appinfo);
335 if (iface->set_as_default_for_extension)
336 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
338 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
339 "g_app_info_set_as_default_for_extension not supported yet");
345 * g_app_info_add_supports_type:
346 * @appinfo: a #GAppInfo.
347 * @content_type: a string.
350 * Adds a content type to the application information to indicate the
351 * application is capable of opening files with the given content type.
353 * Returns: %TRUE on success, %FALSE on error.
356 g_app_info_add_supports_type (GAppInfo *appinfo,
357 const char *content_type,
360 GAppInfoIface *iface;
362 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
363 g_return_val_if_fail (content_type != NULL, FALSE);
365 iface = G_APP_INFO_GET_IFACE (appinfo);
367 if (iface->add_supports_type)
368 return (* iface->add_supports_type) (appinfo, content_type, error);
370 g_set_error_literal (error, G_IO_ERROR,
371 G_IO_ERROR_NOT_SUPPORTED,
372 "g_app_info_add_supports_type not supported yet");
379 * g_app_info_can_remove_supports_type:
380 * @appinfo: a #GAppInfo.
382 * Checks if a supported content type can be removed from an application.
384 * Returns: %TRUE if it is possible to remove supported
385 * content types from a given @appinfo, %FALSE if not.
388 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
390 GAppInfoIface *iface;
392 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
394 iface = G_APP_INFO_GET_IFACE (appinfo);
396 if (iface->can_remove_supports_type)
397 return (* iface->can_remove_supports_type) (appinfo);
404 * g_app_info_remove_supports_type:
405 * @appinfo: a #GAppInfo.
406 * @content_type: a string.
409 * Removes a supported type from an application, if possible.
411 * Returns: %TRUE on success, %FALSE on error.
414 g_app_info_remove_supports_type (GAppInfo *appinfo,
415 const char *content_type,
418 GAppInfoIface *iface;
420 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
421 g_return_val_if_fail (content_type != NULL, FALSE);
423 iface = G_APP_INFO_GET_IFACE (appinfo);
425 if (iface->remove_supports_type)
426 return (* iface->remove_supports_type) (appinfo, content_type, error);
428 g_set_error_literal (error, G_IO_ERROR,
429 G_IO_ERROR_NOT_SUPPORTED,
430 "g_app_info_remove_supports_type not supported yet");
437 * g_app_info_get_icon:
438 * @appinfo: a #GAppInfo.
440 * Gets the icon for the application.
442 * Returns: the default #GIcon for @appinfo.
445 g_app_info_get_icon (GAppInfo *appinfo)
447 GAppInfoIface *iface;
449 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
451 iface = G_APP_INFO_GET_IFACE (appinfo);
453 return (* iface->get_icon) (appinfo);
459 * @appinfo: a #GAppInfo
460 * @files: a #GList of #GFile objects
461 * @launch_context: a #GAppLaunchContext or %NULL
464 * Launches the application. Passes @files to the launched application
465 * as arguments, using the optional @launch_context to get information
466 * about the details of the launcher (like what screen it is on).
467 * On error, @error will be set accordingly.
469 * To lauch the application without arguments pass a %NULL @files list.
471 * Note that even if the launch is successful the application launched
472 * can fail to start if it runs into problems during startup. There is
473 * no way to detect this.
475 * Some URIs can be changed when passed through a GFile (for instance
476 * unsupported uris with strange formats like mailto:), so if you have
477 * a textual uri you want to pass in as argument, consider using
478 * g_app_info_launch_uris() instead.
480 * Returns: %TRUE on successful launch, %FALSE otherwise.
483 g_app_info_launch (GAppInfo *appinfo,
485 GAppLaunchContext *launch_context,
488 GAppInfoIface *iface;
490 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
492 iface = G_APP_INFO_GET_IFACE (appinfo);
494 return (* iface->launch) (appinfo, files, launch_context, error);
499 * g_app_info_supports_uris:
500 * @appinfo: a #GAppInfo.
502 * Checks if the application supports reading files and directories from URIs.
504 * Returns: %TRUE if the @appinfo supports URIs.
507 g_app_info_supports_uris (GAppInfo *appinfo)
509 GAppInfoIface *iface;
511 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
513 iface = G_APP_INFO_GET_IFACE (appinfo);
515 return (* iface->supports_uris) (appinfo);
520 * g_app_info_supports_files:
521 * @appinfo: a #GAppInfo.
523 * Checks if the application accepts files as arguments.
525 * Returns: %TRUE if the @appinfo supports files.
528 g_app_info_supports_files (GAppInfo *appinfo)
530 GAppInfoIface *iface;
532 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
534 iface = G_APP_INFO_GET_IFACE (appinfo);
536 return (* iface->supports_files) (appinfo);
541 * g_app_info_launch_uris:
542 * @appinfo: a #GAppInfo
543 * @uris: a #GList containing URIs to launch.
544 * @launch_context: a #GAppLaunchContext or %NULL
547 * Launches the application. Passes @uris to the launched application
548 * as arguments, using the optional @launch_context to get information
549 * about the details of the launcher (like what screen it is on).
550 * On error, @error will be set accordingly.
552 * To lauch the application without arguments pass a %NULL @uris list.
554 * Note that even if the launch is successful the application launched
555 * can fail to start if it runs into problems during startup. There is
556 * no way to detect this.
558 * Returns: %TRUE on successful launch, %FALSE otherwise.
561 g_app_info_launch_uris (GAppInfo *appinfo,
563 GAppLaunchContext *launch_context,
566 GAppInfoIface *iface;
568 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
570 iface = G_APP_INFO_GET_IFACE (appinfo);
572 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
577 * g_app_info_should_show:
578 * @appinfo: a #GAppInfo.
580 * Checks if the application info should be shown in menus that
581 * list available applications.
583 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
586 g_app_info_should_show (GAppInfo *appinfo)
588 GAppInfoIface *iface;
590 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
592 iface = G_APP_INFO_GET_IFACE (appinfo);
594 return (* iface->should_show) (appinfo);
598 * g_app_info_launch_default_for_uri:
599 * @uri: the uri to show
600 * @launch_context: an optional #GAppLaunchContext.
603 * Utility function that launches the default application
604 * registered to handle the specified uri. Synchronous I/O
605 * is done on the uri to detect the type of the file if
608 * Returns: %TRUE on success, %FALSE on error.
611 g_app_info_launch_default_for_uri (const char *uri,
612 GAppLaunchContext *launch_context,
620 file = g_file_new_for_uri (uri);
621 app_info = g_file_query_default_handler (file, NULL, error);
622 g_object_unref (file);
623 if (app_info == NULL)
626 /* Use the uri, not the GFile, as the GFile roundtrip may
627 * affect the uri which we don't want (for instance for a
630 l.data = (char *)uri;
631 l.next = l.prev = NULL;
632 res = g_app_info_launch_uris (app_info, &l,
633 launch_context, error);
635 g_object_unref (app_info);
641 * g_app_info_can_delete:
642 * @appinfo: a #GAppInfo
644 * Obtains the information whether the #GAppInfo can be deleted.
645 * See g_app_info_delete().
647 * Returns: %TRUE if @appinfo can be deleted
652 g_app_info_can_delete (GAppInfo *appinfo)
654 GAppInfoIface *iface;
656 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
658 iface = G_APP_INFO_GET_IFACE (appinfo);
660 if (iface->can_delete)
661 return (* iface->can_delete) (appinfo);
669 * @appinfo: a #GAppInfo
671 * Tries to delete a #GAppInfo.
673 * On some platforms, there may be a difference between user-defined
674 * #GAppInfo<!-- -->s which can be deleted, and system-wide ones which
675 * cannot. See g_app_info_can_delete().
677 * Returns: %TRUE if @appinfo has been deleted
682 g_app_info_delete (GAppInfo *appinfo)
684 GAppInfoIface *iface;
686 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
688 iface = G_APP_INFO_GET_IFACE (appinfo);
690 if (iface->do_delete)
691 return (* iface->do_delete) (appinfo);
697 G_DEFINE_TYPE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT);
700 * g_app_launch_context_new:
702 * Creates a new application launch context. This is not normally used,
703 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
705 * Returns: a #GAppLaunchContext.
708 g_app_launch_context_new (void)
710 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
714 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
719 g_app_launch_context_init (GAppLaunchContext *launch_context)
724 * g_app_launch_context_get_display:
725 * @context: a #GAppLaunchContext
727 * @files: a #GList of #GFile objects
729 * Gets the display string for the display. This is used to ensure new
730 * applications are started on the same display as the launching
733 * Returns: a display string for the display.
736 g_app_launch_context_get_display (GAppLaunchContext *context,
740 GAppLaunchContextClass *class;
742 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
743 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
745 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
747 if (class->get_display == NULL)
750 return class->get_display (context, info, files);
754 * g_app_launch_context_get_startup_notify_id:
755 * @context: a #GAppLaunchContext
757 * @files: a #GList of of #GFile objects
759 * Initiates startup notification for the application and returns the
760 * DESKTOP_STARTUP_ID for the launched operation, if supported.
762 * Startup notification IDs are defined in the <ulink
763 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">
764 * FreeDesktop.Org Startup Notifications standard</ulink>.
766 * Returns: a startup notification ID for the application, or %NULL if
770 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
774 GAppLaunchContextClass *class;
776 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
777 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
779 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
781 if (class->get_startup_notify_id == NULL)
784 return class->get_startup_notify_id (context, info, files);
789 * g_app_launch_context_launch_failed:
790 * @context: a #GAppLaunchContext.
791 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
793 * Called when an application has failed to launch, so that it can cancel
794 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
798 g_app_launch_context_launch_failed (GAppLaunchContext *context,
799 const char *startup_notify_id)
801 GAppLaunchContextClass *class;
803 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
804 g_return_if_fail (startup_notify_id != NULL);
806 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
808 if (class->launch_failed != NULL)
809 class->launch_failed (context, startup_notify_id);
813 #define __G_APP_INFO_C__
814 #include "gioaliasdef.c"