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>
32 * @short_description: Application information and launch contexts
35 * #GAppInfo and #GAppLaunchContext are used for describing and launching
36 * applications installed on the system.
38 * As of GLib 2.20, URIs will always be converted to POSIX paths
39 * (using g_file_get_path()) when using g_app_info_launch() even if
40 * the application requested an URI and not a POSIX path. For example
41 * for an desktop-file based application with Exec key <literal>totem
42 * %%U</literal> and a single URI,
43 * <literal>sftp://foo/file.avi</literal>, then
44 * <literal>/home/user/.gvfs/sftp on foo/file.avi</literal> will be
45 * passed. This will only work if a set of suitable GIO extensions
46 * (such as gvfs 2.26 compiled with FUSE support), is available and
47 * operational; if this is not the case, the URI will be passed
48 * unmodified to the application. Some URIs, such as
49 * <literal>mailto:</literal>, of course cannot be mapped to a POSIX
50 * path (in gvfs there's no FUSE mount for it); such URIs will be
51 * passed unmodified to the application.
53 * Specifically for gvfs 2.26 and later, the POSIX URI will be mapped
54 * back to the GIO URI in the #GFile constructors (since gvfs
55 * implements the #GVfs extension point). As such, if the application
56 * needs to examine the URI, it needs to use g_file_get_uri() or
57 * similar on #GFile. In other words, an application cannot assume
58 * that the URI passed to e.g. g_file_new_for_commandline_arg() is
59 * equal to the result of g_file_get_uri(). The following snippet
66 * file = g_file_new_for_commandline_arg (uri_from_commandline);
68 * uri = g_file_get_uri (file);
69 * strcmp (uri, uri_from_commandline) == 0; // FALSE
72 * if (g_file_has_uri_scheme (file, "cdda"))
74 * // do something special with uri
76 * g_object_unref (file);
79 * This code will work when both <literal>cdda://sr0/Track
80 * 1.wav</literal> and <literal>/home/user/.gvfs/cdda on sr0/Track
81 * 1.wav</literal> is passed to the application. It should be noted
82 * that it's generally not safe for applications to rely on the format
83 * of a particular URIs. Different launcher applications (e.g. file
84 * managers) may have different ideas of what a given URI means.
88 typedef GAppInfoIface GAppInfoInterface;
89 G_DEFINE_INTERFACE (GAppInfo, g_app_info, G_TYPE_OBJECT)
92 g_app_info_default_init (GAppInfoInterface *iface)
99 * @appinfo: a #GAppInfo.
101 * Creates a duplicate of a #GAppInfo.
103 * Returns: (transfer full): a duplicate of @appinfo.
106 g_app_info_dup (GAppInfo *appinfo)
108 GAppInfoIface *iface;
110 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
112 iface = G_APP_INFO_GET_IFACE (appinfo);
114 return (* iface->dup) (appinfo);
119 * @appinfo1: the first #GAppInfo.
120 * @appinfo2: the second #GAppInfo.
122 * Checks if two #GAppInfo<!-- -->s are equal.
124 * Returns: %TRUE if @appinfo1 is equal to @appinfo2. %FALSE otherwise.
127 g_app_info_equal (GAppInfo *appinfo1,
130 GAppInfoIface *iface;
132 g_return_val_if_fail (G_IS_APP_INFO (appinfo1), FALSE);
133 g_return_val_if_fail (G_IS_APP_INFO (appinfo2), FALSE);
135 if (G_TYPE_FROM_INSTANCE (appinfo1) != G_TYPE_FROM_INSTANCE (appinfo2))
138 iface = G_APP_INFO_GET_IFACE (appinfo1);
140 return (* iface->equal) (appinfo1, appinfo2);
145 * @appinfo: a #GAppInfo.
147 * Gets the ID of an application. An id is a string that
148 * identifies the application. The exact format of the id is
149 * platform dependent. For instance, on Unix this is the
150 * desktop file id from the xdg menu specification.
152 * Note that the returned ID may be %NULL, depending on how
153 * the @appinfo has been constructed.
155 * Returns: a string containing the application's ID.
158 g_app_info_get_id (GAppInfo *appinfo)
160 GAppInfoIface *iface;
162 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
164 iface = G_APP_INFO_GET_IFACE (appinfo);
166 return (* iface->get_id) (appinfo);
170 * g_app_info_get_name:
171 * @appinfo: a #GAppInfo.
173 * Gets the installed name of the application.
175 * Returns: the name of the application for @appinfo.
178 g_app_info_get_name (GAppInfo *appinfo)
180 GAppInfoIface *iface;
182 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
184 iface = G_APP_INFO_GET_IFACE (appinfo);
186 return (* iface->get_name) (appinfo);
190 * g_app_info_get_display_name:
191 * @appinfo: a #GAppInfo.
193 * Gets the display name of the application. The display name is often more
194 * descriptive to the user than the name itself.
196 * Returns: the display name of the application for @appinfo, or the name if
197 * no display name is available.
202 g_app_info_get_display_name (GAppInfo *appinfo)
204 GAppInfoIface *iface;
206 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
208 iface = G_APP_INFO_GET_IFACE (appinfo);
210 if (iface->get_display_name == NULL)
211 return (* iface->get_name) (appinfo);
213 return (* iface->get_display_name) (appinfo);
217 * g_app_info_get_description:
218 * @appinfo: a #GAppInfo.
220 * Gets a human-readable description of an installed application.
222 * Returns: a string containing a description of the
223 * application @appinfo, or %NULL if none.
226 g_app_info_get_description (GAppInfo *appinfo)
228 GAppInfoIface *iface;
230 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
232 iface = G_APP_INFO_GET_IFACE (appinfo);
234 return (* iface->get_description) (appinfo);
238 * g_app_info_get_executable:
239 * @appinfo: a #GAppInfo
241 * Gets the executable's name for the installed application.
243 * Returns: a string containing the @appinfo's application
247 g_app_info_get_executable (GAppInfo *appinfo)
249 GAppInfoIface *iface;
251 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
253 iface = G_APP_INFO_GET_IFACE (appinfo);
255 return (* iface->get_executable) (appinfo);
260 * g_app_info_get_commandline:
261 * @appinfo: a #GAppInfo
263 * Gets the commandline with which the application will be
266 * Returns: a string containing the @appinfo's commandline,
267 * or %NULL if this information is not available
272 g_app_info_get_commandline (GAppInfo *appinfo)
274 GAppInfoIface *iface;
276 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
278 iface = G_APP_INFO_GET_IFACE (appinfo);
280 if (iface->get_commandline)
281 return (* iface->get_commandline) (appinfo);
287 * g_app_info_set_as_default_for_type:
288 * @appinfo: a #GAppInfo.
289 * @content_type: the content type.
292 * Sets the application as the default handler for a given type.
294 * Returns: %TRUE on success, %FALSE on error.
297 g_app_info_set_as_default_for_type (GAppInfo *appinfo,
298 const char *content_type,
301 GAppInfoIface *iface;
303 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
304 g_return_val_if_fail (content_type != NULL, FALSE);
306 iface = G_APP_INFO_GET_IFACE (appinfo);
308 return (* iface->set_as_default_for_type) (appinfo, content_type, error);
313 * g_app_info_set_as_default_for_extension:
314 * @appinfo: a #GAppInfo.
315 * @extension: a string containing the file extension (without the dot).
318 * Sets the application as the default handler for the given file extension.
320 * Returns: %TRUE on success, %FALSE on error.
323 g_app_info_set_as_default_for_extension (GAppInfo *appinfo,
324 const char *extension,
327 GAppInfoIface *iface;
329 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
330 g_return_val_if_fail (extension != NULL, FALSE);
332 iface = G_APP_INFO_GET_IFACE (appinfo);
334 if (iface->set_as_default_for_extension)
335 return (* iface->set_as_default_for_extension) (appinfo, extension, error);
337 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
338 "g_app_info_set_as_default_for_extension not supported yet");
344 * g_app_info_add_supports_type:
345 * @appinfo: a #GAppInfo.
346 * @content_type: a string.
349 * Adds a content type to the application information to indicate the
350 * application is capable of opening files with the given content type.
352 * Returns: %TRUE on success, %FALSE on error.
355 g_app_info_add_supports_type (GAppInfo *appinfo,
356 const char *content_type,
359 GAppInfoIface *iface;
361 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
362 g_return_val_if_fail (content_type != NULL, FALSE);
364 iface = G_APP_INFO_GET_IFACE (appinfo);
366 if (iface->add_supports_type)
367 return (* iface->add_supports_type) (appinfo, content_type, error);
369 g_set_error_literal (error, G_IO_ERROR,
370 G_IO_ERROR_NOT_SUPPORTED,
371 "g_app_info_add_supports_type not supported yet");
378 * g_app_info_can_remove_supports_type:
379 * @appinfo: a #GAppInfo.
381 * Checks if a supported content type can be removed from an application.
383 * Returns: %TRUE if it is possible to remove supported
384 * content types from a given @appinfo, %FALSE if not.
387 g_app_info_can_remove_supports_type (GAppInfo *appinfo)
389 GAppInfoIface *iface;
391 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
393 iface = G_APP_INFO_GET_IFACE (appinfo);
395 if (iface->can_remove_supports_type)
396 return (* iface->can_remove_supports_type) (appinfo);
403 * g_app_info_remove_supports_type:
404 * @appinfo: a #GAppInfo.
405 * @content_type: a string.
408 * Removes a supported type from an application, if possible.
410 * Returns: %TRUE on success, %FALSE on error.
413 g_app_info_remove_supports_type (GAppInfo *appinfo,
414 const char *content_type,
417 GAppInfoIface *iface;
419 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
420 g_return_val_if_fail (content_type != NULL, FALSE);
422 iface = G_APP_INFO_GET_IFACE (appinfo);
424 if (iface->remove_supports_type)
425 return (* iface->remove_supports_type) (appinfo, content_type, error);
427 g_set_error_literal (error, G_IO_ERROR,
428 G_IO_ERROR_NOT_SUPPORTED,
429 "g_app_info_remove_supports_type not supported yet");
436 * g_app_info_get_icon:
437 * @appinfo: a #GAppInfo.
439 * Gets the icon for the application.
441 * Returns: (transfer none): the default #GIcon for @appinfo.
444 g_app_info_get_icon (GAppInfo *appinfo)
446 GAppInfoIface *iface;
448 g_return_val_if_fail (G_IS_APP_INFO (appinfo), NULL);
450 iface = G_APP_INFO_GET_IFACE (appinfo);
452 return (* iface->get_icon) (appinfo);
458 * @appinfo: a #GAppInfo
459 * @files: (element-type GFile): a #GList of #GFile objects
460 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
463 * Launches the application. Passes @files to the launched application
464 * as arguments, using the optional @launch_context to get information
465 * about the details of the launcher (like what screen it is on).
466 * On error, @error will be set accordingly.
468 * To launch the application without arguments pass a %NULL @files list.
470 * Note that even if the launch is successful the application launched
471 * can fail to start if it runs into problems during startup. There is
472 * no way to detect this.
474 * Some URIs can be changed when passed through a GFile (for instance
475 * unsupported uris with strange formats like mailto:), so if you have
476 * a textual uri you want to pass in as argument, consider using
477 * g_app_info_launch_uris() instead.
479 * On UNIX, this function sets the <envvar>GIO_LAUNCHED_DESKTOP_FILE</envvar>
480 * environment variable with the path of the launched desktop file and
481 * <envvar>GIO_LAUNCHED_DESKTOP_FILE_PID</envvar> to the process
482 * id of the launched process. This can be used to ignore
483 * <envvar>GIO_LAUNCHED_DESKTOP_FILE</envvar>, should it be inherited
484 * by further processes. The <envvar>DISPLAY</envvar> and
485 * <envvar>DESKTOP_STARTUP_ID</envvar> environment variables are also
486 * set, based on information provided in @launch_context.
488 * Returns: %TRUE on successful launch, %FALSE otherwise.
491 g_app_info_launch (GAppInfo *appinfo,
493 GAppLaunchContext *launch_context,
496 GAppInfoIface *iface;
498 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
500 iface = G_APP_INFO_GET_IFACE (appinfo);
502 return (* iface->launch) (appinfo, files, launch_context, error);
507 * g_app_info_supports_uris:
508 * @appinfo: a #GAppInfo.
510 * Checks if the application supports reading files and directories from URIs.
512 * Returns: %TRUE if the @appinfo supports URIs.
515 g_app_info_supports_uris (GAppInfo *appinfo)
517 GAppInfoIface *iface;
519 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
521 iface = G_APP_INFO_GET_IFACE (appinfo);
523 return (* iface->supports_uris) (appinfo);
528 * g_app_info_supports_files:
529 * @appinfo: a #GAppInfo.
531 * Checks if the application accepts files as arguments.
533 * Returns: %TRUE if the @appinfo supports files.
536 g_app_info_supports_files (GAppInfo *appinfo)
538 GAppInfoIface *iface;
540 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
542 iface = G_APP_INFO_GET_IFACE (appinfo);
544 return (* iface->supports_files) (appinfo);
549 * g_app_info_launch_uris:
550 * @appinfo: a #GAppInfo
551 * @uris: (element-type char*): a #GList containing URIs to launch.
552 * @launch_context: (allow-none): a #GAppLaunchContext or %NULL
555 * Launches the application. Passes @uris to the launched application
556 * as arguments, using the optional @launch_context to get information
557 * about the details of the launcher (like what screen it is on).
558 * On error, @error will be set accordingly.
560 * To lauch the application without arguments pass a %NULL @uris list.
562 * Note that even if the launch is successful the application launched
563 * can fail to start if it runs into problems during startup. There is
564 * no way to detect this.
566 * Returns: %TRUE on successful launch, %FALSE otherwise.
569 g_app_info_launch_uris (GAppInfo *appinfo,
571 GAppLaunchContext *launch_context,
574 GAppInfoIface *iface;
576 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
578 iface = G_APP_INFO_GET_IFACE (appinfo);
580 return (* iface->launch_uris) (appinfo, uris, launch_context, error);
585 * g_app_info_should_show:
586 * @appinfo: a #GAppInfo.
588 * Checks if the application info should be shown in menus that
589 * list available applications.
591 * Returns: %TRUE if the @appinfo should be shown, %FALSE otherwise.
594 g_app_info_should_show (GAppInfo *appinfo)
596 GAppInfoIface *iface;
598 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
600 iface = G_APP_INFO_GET_IFACE (appinfo);
602 return (* iface->should_show) (appinfo);
606 * g_app_info_launch_default_for_uri:
607 * @uri: the uri to show
608 * @launch_context: an optional #GAppLaunchContext.
611 * Utility function that launches the default application
612 * registered to handle the specified uri. Synchronous I/O
613 * is done on the uri to detect the type of the file if
616 * Returns: %TRUE on success, %FALSE on error.
619 g_app_info_launch_default_for_uri (const char *uri,
620 GAppLaunchContext *launch_context,
628 file = g_file_new_for_uri (uri);
629 app_info = g_file_query_default_handler (file, NULL, error);
630 g_object_unref (file);
631 if (app_info == NULL)
634 /* Use the uri, not the GFile, as the GFile roundtrip may
635 * affect the uri which we don't want (for instance for a
638 l.data = (char *)uri;
639 l.next = l.prev = NULL;
640 res = g_app_info_launch_uris (app_info, &l,
641 launch_context, error);
643 g_object_unref (app_info);
649 * g_app_info_can_delete:
650 * @appinfo: a #GAppInfo
652 * Obtains the information whether the #GAppInfo can be deleted.
653 * See g_app_info_delete().
655 * Returns: %TRUE if @appinfo can be deleted
660 g_app_info_can_delete (GAppInfo *appinfo)
662 GAppInfoIface *iface;
664 g_return_val_if_fail (G_IS_APP_INFO (appinfo), FALSE);
666 iface = G_APP_INFO_GET_IFACE (appinfo);
668 if (iface->can_delete)
669 return (* iface->can_delete) (appinfo);
677 * @appinfo: a #GAppInfo
679 * Tries to delete a #GAppInfo.
681 * On some platforms, there may be a difference between user-defined
682 * #GAppInfo<!-- -->s which can be deleted, and system-wide ones which
683 * 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: (element-type GFile): 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: (element-type GFile): 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);