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 * On UNIX, this function sets the <envvar>GIO_LAUNCHED_DESKTOP_FILE</envvar>
481 * environment variable with the path of the launched desktop file and
482 * <envvar>GIO_LAUNCHED_DESKTOP_FILE_PID</envvar> to the process
483 * id of the launched process. This can be used to ignore
484 * <envvar>GIO_LAUNCHED_DESKTOP_FILE</envvar>, should it be inherited
485 * by further processes. The <envvar>DISPLAY</envvar> and
486 * <envvar>DESKTOP_STARTUP_ID</envvar> environment variables are also
487 * set, based on information provided in @launch_context.
489 * Returns: %TRUE on successful launch, %FALSE otherwise.
492 g_app_info_launch (GAppInfo *appinfo,
494 GAppLaunchContext *launch_context,
497 GAppInfoIface *iface;
499 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
501 iface = G_APP_INFO_GET_IFACE (appinfo);
503 return (* iface->launch) (appinfo, files, launch_context, error);
508 * g_app_info_supports_uris:
509 * @appinfo: a #GAppInfo.
511 * Checks if the application supports reading files and directories from URIs.
513 * Returns: %TRUE if the @appinfo supports URIs.
516 g_app_info_supports_uris (GAppInfo *appinfo)
518 GAppInfoIface *iface;
520 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
522 iface = G_APP_INFO_GET_IFACE (appinfo);
524 return (* iface->supports_uris) (appinfo);
529 * g_app_info_supports_files:
530 * @appinfo: a #GAppInfo.
532 * Checks if the application accepts files as arguments.
534 * Returns: %TRUE if the @appinfo supports files.
537 g_app_info_supports_files (GAppInfo *appinfo)
539 GAppInfoIface *iface;
541 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
543 iface = G_APP_INFO_GET_IFACE (appinfo);
545 return (* iface->supports_files) (appinfo);
550 * g_app_info_launch_uris:
551 * @appinfo: a #GAppInfo
552 * @uris: a #GList containing URIs to launch.
553 * @launch_context: a #GAppLaunchContext or %NULL
556 * Launches the application. Passes @uris to the launched application
557 * as arguments, using the optional @launch_context to get information
558 * about the details of the launcher (like what screen it is on).
559 * On error, @error will be set accordingly.
561 * To lauch the application without arguments pass a %NULL @uris list.
563 * Note that even if the launch is successful the application launched
564 * can fail to start if it runs into problems during startup. There is
565 * no way to detect this.
567 * Returns: %TRUE on successful launch, %FALSE otherwise.
570 g_app_info_launch_uris (GAppInfo *appinfo,
572 GAppLaunchContext *launch_context,
575 GAppInfoIface *iface;
577 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
579 iface = G_APP_INFO_GET_IFACE (appinfo);
581 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
586 * g_app_info_should_show:
587 * @appinfo: a #GAppInfo.
589 * Checks if the application info should be shown in menus that
590 * list available applications.
592 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
595 g_app_info_should_show (GAppInfo *appinfo)
597 GAppInfoIface *iface;
599 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
601 iface = G_APP_INFO_GET_IFACE (appinfo);
603 return (* iface->should_show) (appinfo);
607 * g_app_info_launch_default_for_uri:
608 * @uri: the uri to show
609 * @launch_context: an optional #GAppLaunchContext.
612 * Utility function that launches the default application
613 * registered to handle the specified uri. Synchronous I/O
614 * is done on the uri to detect the type of the file if
617 * Returns: %TRUE on success, %FALSE on error.
620 g_app_info_launch_default_for_uri (const char *uri,
621 GAppLaunchContext *launch_context,
629 file = g_file_new_for_uri (uri);
630 app_info = g_file_query_default_handler (file, NULL, error);
631 g_object_unref (file);
632 if (app_info == NULL)
635 /* Use the uri, not the GFile, as the GFile roundtrip may
636 * affect the uri which we don't want (for instance for a
639 l.data = (char *)uri;
640 l.next = l.prev = NULL;
641 res = g_app_info_launch_uris (app_info, &l,
642 launch_context, error);
644 g_object_unref (app_info);
650 * g_app_info_can_delete:
651 * @appinfo: a #GAppInfo
653 * Obtains the information whether the #GAppInfo can be deleted.
654 * See g_app_info_delete().
656 * Returns: %TRUE if @appinfo can be deleted
661 g_app_info_can_delete (GAppInfo *appinfo)
663 GAppInfoIface *iface;
665 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
667 iface = G_APP_INFO_GET_IFACE (appinfo);
669 if (iface->can_delete)
670 return (* iface->can_delete) (appinfo);
678 * @appinfo: a #GAppInfo
680 * Tries to delete a #GAppInfo.
682 * On some platforms, there may be a difference between user-defined
683 * #GAppInfo<!-- -->s which can be deleted, and system-wide ones which
684 * cannot. See g_app_info_can_delete().
686 * Returns: %TRUE if @appinfo has been deleted
691 g_app_info_delete (GAppInfo *appinfo)
693 GAppInfoIface *iface;
695 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
697 iface = G_APP_INFO_GET_IFACE (appinfo);
699 if (iface->do_delete)
700 return (* iface->do_delete) (appinfo);
706 G_DEFINE_TYPE (GAppLaunchContext, g_app_launch_context, G_TYPE_OBJECT);
709 * g_app_launch_context_new:
711 * Creates a new application launch context. This is not normally used,
712 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
714 * Returns: a #GAppLaunchContext.
717 g_app_launch_context_new (void)
719 return g_object_new (G_TYPE_APP_LAUNCH_CONTEXT, NULL);
723 g_app_launch_context_class_init (GAppLaunchContextClass *klass)
728 g_app_launch_context_init (GAppLaunchContext *launch_context)
733 * g_app_launch_context_get_display:
734 * @context: a #GAppLaunchContext
736 * @files: a #GList of #GFile objects
738 * Gets the display string for the @context. This is used to ensure new
739 * applications are started on the same display as the launching
740 * application, by setting the <envvar>DISPLAY</envvar> environment variable.
742 * Returns: a display string for the display.
745 g_app_launch_context_get_display (GAppLaunchContext *context,
749 GAppLaunchContextClass *class;
751 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
752 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
754 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
756 if (class->get_display == NULL)
759 return class->get_display (context, info, files);
763 * g_app_launch_context_get_startup_notify_id:
764 * @context: a #GAppLaunchContext
766 * @files: a #GList of of #GFile objects
768 * Initiates startup notification for the application and returns the
769 * <envvar>DESKTOP_STARTUP_ID</envvar> for the launched operation,
772 * Startup notification IDs are defined in the <ulink
773 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">
774 * FreeDesktop.Org Startup Notifications standard</ulink>.
776 * Returns: a startup notification ID for the application, or %NULL if
780 g_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
784 GAppLaunchContextClass *class;
786 g_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), NULL);
787 g_return_val_if_fail (G_IS_APP_INFO (info), NULL);
789 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
791 if (class->get_startup_notify_id == NULL)
794 return class->get_startup_notify_id (context, info, files);
799 * g_app_launch_context_launch_failed:
800 * @context: a #GAppLaunchContext.
801 * @startup_notify_id: the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
803 * Called when an application has failed to launch, so that it can cancel
804 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
808 g_app_launch_context_launch_failed (GAppLaunchContext *context,
809 const char *startup_notify_id)
811 GAppLaunchContextClass *class;
813 g_return_if_fail (G_IS_APP_LAUNCH_CONTEXT (context));
814 g_return_if_fail (startup_notify_id != NULL);
816 class = G_APP_LAUNCH_CONTEXT_GET_CLASS (context);
818 if (class->launch_failed != NULL)
819 class->launch_failed (context, startup_notify_id);
823 #define __G_APP_INFO_C__
824 #include "gioaliasdef.c"