1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2014 Patrick Griffis
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.1 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, see <http://www.gnu.org/licenses/>.
23 #include "gosxappinfo.h"
24 #include "gcontenttype.h"
26 #include "gfileicon.h"
29 #import <CoreFoundation/CoreFoundation.h>
30 #import <Foundation/Foundation.h>
31 #import <ApplicationServices/ApplicationServices.h>
36 * @short_description: Application information from NSBundles
37 * @include: gio/gosxappinfo.h
39 * #GOsxAppInfo is an implementation of #GAppInfo based on NSBundle information.
41 * Note that `<gio/gosxappinfo.h>` is unique to OSX.
44 static void g_osx_app_info_iface_init (GAppInfoIface *iface);
45 static const char *g_osx_app_info_get_id (GAppInfo *appinfo);
50 * Information about an installed application from a NSBundle.
54 GObject parent_instance;
58 /* Note that these are all NULL until first call
59 * to getter at which point they are cached here
68 G_DEFINE_TYPE_WITH_CODE (GOsxAppInfo, g_osx_app_info, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_osx_app_info_iface_init))
72 g_osx_app_info_new (NSBundle *bundle)
74 GOsxAppInfo *info = g_object_new (G_TYPE_OSX_APP_INFO, NULL);
76 info->bundle = [bundle retain];
82 g_osx_app_info_init (GOsxAppInfo *info)
87 g_osx_app_info_finalize (GObject *object)
89 GOsxAppInfo *info = G_OSX_APP_INFO (object);
93 g_free (info->executable);
94 g_free (info->filename);
95 g_clear_object (&info->icon);
97 [info->bundle release];
99 G_OBJECT_CLASS (g_osx_app_info_parent_class)->finalize (object);
103 g_osx_app_info_class_init (GOsxAppInfoClass *klass)
105 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
107 gobject_class->finalize = g_osx_app_info_finalize;
111 g_osx_app_info_dup (GAppInfo *appinfo)
114 GOsxAppInfo *new_info;
116 g_return_val_if_fail (appinfo != NULL, NULL);
118 info = G_OSX_APP_INFO (appinfo);
119 new_info = g_osx_app_info_new ([info->bundle retain]);
121 return G_APP_INFO (new_info);
125 g_osx_app_info_equal (GAppInfo *appinfo1,
128 const gchar *str1, *str2;
130 g_return_val_if_fail (appinfo1 != NULL, FALSE);
131 g_return_val_if_fail (appinfo2 != NULL, FALSE);
133 str1 = g_osx_app_info_get_id (appinfo1);
134 str2 = g_osx_app_info_get_id (appinfo2);
136 return (g_strcmp0 (str1, str2) == 0);
140 * get_bundle_string_value:
141 * @bundle: a #NSBundle
142 * @key: an #NSString key
144 * Returns a value from a bundles info.plist file.
145 * It will be utf8 encoded and it must be g_free()'d.
149 get_bundle_string_value (NSBundle *bundle,
156 g_return_val_if_fail (bundle != NULL, NULL);
158 value = (NSString *)[bundle objectForInfoDictionaryKey: key];
162 cvalue = [value cStringUsingEncoding: NSUTF8StringEncoding];
163 ret = g_strdup (cvalue);
169 create_cfstring_from_cstr (const gchar *cstr)
171 return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
174 #ifdef G_ENABLE_DEBUG
176 create_cstr_from_cfstring (CFStringRef str)
178 g_return_val_if_fail (str != NULL, NULL);
180 CFIndex length = CFStringGetLength (str);
181 CFIndex maxlen = CFStringGetMaximumSizeForEncoding (length, kCFStringEncodingUTF8);
182 gchar *buffer = g_malloc (maxlen + 1);
183 Boolean success = CFStringGetCString (str, (char *) buffer, maxlen,
184 kCFStringEncodingUTF8);
196 url_escape_hostname (const char *url)
198 char *host_start, *ret;
200 host_start = strstr (url, "://");
201 if (host_start != NULL)
203 char *host_end, *scheme, *host, *hostname;
205 scheme = g_strndup (url, host_start - url);
207 host_end = strchr (host_start, '/');
209 if (host_end != NULL)
210 host = g_strndup (host_start, host_end - host_start);
212 host = g_strdup (host_start);
214 hostname = g_hostname_to_ascii (host);
216 ret = g_strconcat (scheme, "://", hostname, host_end, NULL);
225 return g_strdup (url);
229 create_url_from_cstr (gchar *cstr,
236 puny_cstr = url_escape_hostname (cstr);
237 str = CFStringCreateWithCString (NULL, puny_cstr ? puny_cstr : cstr, kCFStringEncodingUTF8);
240 url = CFURLCreateWithFileSystemPath (NULL, str, kCFURLPOSIXPathStyle, FALSE);
242 url = CFURLCreateWithString (NULL, str, NULL);
245 g_debug ("Creating CFURL from %s %s failed!", cstr, is_file ? "file" : "uri");
253 create_url_list_from_glist (GList *uris,
257 int len = g_list_length (uris);
258 CFMutableArrayRef array;
263 array = CFArrayCreateMutable (NULL, len, &kCFTypeArrayCallBacks);
267 for (lst = uris; lst != NULL && lst->data; lst = lst->next)
269 CFURLRef url = create_url_from_cstr ((char*)lst->data, are_files);
271 CFArrayAppendValue (array, url);
274 return (CFArrayRef)array;
277 static LSLaunchURLSpec *
278 create_urlspec_for_appinfo (GOsxAppInfo *info,
282 LSLaunchURLSpec *urlspec = g_new0 (LSLaunchURLSpec, 1);
283 gchar *app_cstr = g_osx_app_info_get_filename (info);
285 /* Strip file:// from app url but ensure filesystem url */
286 urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE);
287 urlspec->launchFlags = kLSLaunchDefaults;
288 urlspec->itemURLs = create_url_list_from_glist (uris, are_files);
294 free_urlspec (LSLaunchURLSpec *urlspec)
296 if (urlspec->itemURLs)
298 CFArrayRemoveAllValues ((CFMutableArrayRef)urlspec->itemURLs);
299 CFRelease (urlspec->itemURLs);
301 CFRelease (urlspec->appURL);
306 get_bundle_for_url (CFURLRef app_url)
308 NSBundle *bundle = [NSBundle bundleWithURL: (NSURL*)app_url];
312 g_debug ("Bundle not found for url.");
320 get_bundle_for_id (CFStringRef bundle_id)
325 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
326 CFArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
329 /* TODO: if there's multiple, we should perhaps prefer one thats in $HOME,
330 * instead of just always picking the first.
332 app_url = CFArrayGetValueAtIndex (urls, 0);
338 if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url) == kLSApplicationNotFoundErr)
341 #ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
342 gchar *id_str = create_cstr_from_cfstring (bundle_id);
345 g_debug ("Application not found for id \"%s\".", id_str);
349 g_debug ("Application not found for unconvertable bundle id.");
354 bundle = get_bundle_for_url (app_url);
360 g_osx_app_info_get_id (GAppInfo *appinfo)
362 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
365 info->id = get_bundle_string_value (info->bundle, @"CFBundleIdentifier");
371 g_osx_app_info_get_name (GAppInfo *appinfo)
373 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
376 info->name = get_bundle_string_value (info->bundle, @"CFBundleName");
382 g_osx_app_info_get_display_name (GAppInfo *appinfo)
384 return g_osx_app_info_get_name (appinfo);
388 g_osx_app_info_get_description (GAppInfo *appinfo)
390 /* Bundles do not contain descriptions */
395 g_osx_app_info_get_executable (GAppInfo *appinfo)
397 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
399 if (!info->executable)
400 info->executable = get_bundle_string_value (info->bundle, @"CFBundleExecutable");
402 return info->executable;
406 g_osx_app_info_get_filename (GOsxAppInfo *info)
408 g_return_val_if_fail (info != NULL, NULL);
412 info->filename = g_strconcat ("file://", [[info->bundle bundlePath]
413 cStringUsingEncoding: NSUTF8StringEncoding],
417 return info->filename;
421 g_osx_app_info_get_commandline (GAppInfo *appinfo)
423 /* There isn't really a command line value */
428 g_osx_app_info_get_icon (GAppInfo *appinfo)
430 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
434 gchar *icon_name, *app_uri, *icon_uri;
437 icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile");
441 app_uri = g_osx_app_info_get_filename (info);
442 icon_uri = g_strconcat (app_uri + 7, "/Contents/Resources/", icon_name,
443 g_str_has_suffix (icon_name, ".icns") ? NULL : ".icns", NULL);
446 file = g_file_new_for_path (icon_uri);
447 info->icon = g_file_icon_new (file);
448 g_object_unref (file);
456 g_osx_app_info_launch_internal (GAppInfo *appinfo,
461 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
462 LSLaunchURLSpec *urlspec = create_urlspec_for_appinfo (info, uris, are_files);
463 gint ret, success = TRUE;
465 if ((ret = LSOpenFromURLSpec (urlspec, NULL)))
467 /* TODO: Better error codes */
468 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
469 "Opening application failed with code %d", ret);
473 free_urlspec (urlspec);
478 g_osx_app_info_supports_uris (GAppInfo *appinfo)
484 g_osx_app_info_supports_files (GAppInfo *appinfo)
490 g_osx_app_info_launch (GAppInfo *appinfo,
492 GAppLaunchContext *launch_context,
495 return g_osx_app_info_launch_internal (appinfo, files, TRUE, error);
499 g_osx_app_info_launch_uris (GAppInfo *appinfo,
501 GAppLaunchContext *launch_context,
504 return g_osx_app_info_launch_internal (appinfo, uris, FALSE, error);
508 g_osx_app_info_should_show (GAppInfo *appinfo)
510 /* Bundles don't have hidden attribute */
515 g_osx_app_info_set_as_default_for_type (GAppInfo *appinfo,
516 const char *content_type,
523 g_osx_app_info_get_supported_types (GAppInfo *appinfo)
525 /* TODO: get CFBundleDocumentTypes */
530 g_osx_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
531 const char *content_type,
539 g_osx_app_info_can_delete (GAppInfo *appinfo)
545 g_osx_app_info_iface_init (GAppInfoIface *iface)
547 iface->dup = g_osx_app_info_dup;
548 iface->equal = g_osx_app_info_equal;
550 iface->get_id = g_osx_app_info_get_id;
551 iface->get_name = g_osx_app_info_get_name;
552 iface->get_display_name = g_osx_app_info_get_display_name;
553 iface->get_description = g_osx_app_info_get_description;
554 iface->get_executable = g_osx_app_info_get_executable;
555 iface->get_commandline = g_osx_app_info_get_commandline;
556 iface->get_icon = g_osx_app_info_get_icon;
557 iface->get_supported_types = g_osx_app_info_get_supported_types;
559 iface->set_as_last_used_for_type = g_osx_app_info_set_as_last_used_for_type;
560 iface->set_as_default_for_type = g_osx_app_info_set_as_default_for_type;
562 iface->launch = g_osx_app_info_launch;
563 iface->launch_uris = g_osx_app_info_launch_uris;
565 iface->supports_uris = g_osx_app_info_supports_uris;
566 iface->supports_files = g_osx_app_info_supports_files;
567 iface->should_show = g_osx_app_info_should_show;
568 iface->can_delete = g_osx_app_info_can_delete;
572 g_app_info_create_from_commandline (const char *commandline,
573 const char *application_name,
574 GAppInfoCreateFlags flags,
581 g_osx_app_info_get_all_for_scheme (const char *cscheme)
583 CFArrayRef bundle_list;
586 GList *info_list = NULL;
589 scheme = create_cfstring_from_cstr (cscheme);
590 bundle_list = LSCopyAllHandlersForURLScheme (scheme);
596 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
598 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
601 bundle = get_bundle_for_id (bundle_id);
606 info = G_APP_INFO (g_osx_app_info_new (bundle));
607 info_list = g_list_append (info_list, info);
609 CFRelease (bundle_list);
614 g_app_info_get_all_for_type (const char *content_type)
617 CFArrayRef bundle_list;
620 GList *info_list = NULL;
623 mime_type = g_content_type_get_mime_type (content_type);
624 if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
626 gchar *scheme = strchr (mime_type, '/') + 1;
627 GList *ret = g_osx_app_info_get_all_for_scheme (scheme);
634 type = create_cfstring_from_cstr (content_type);
635 bundle_list = LSCopyAllRoleHandlersForContentType (type, kLSRolesAll);
641 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
643 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
646 bundle = get_bundle_for_id (bundle_id);
651 info = G_APP_INFO (g_osx_app_info_new (bundle));
652 info_list = g_list_append (info_list, info);
654 CFRelease (bundle_list);
659 g_app_info_get_recommended_for_type (const char *content_type)
661 return g_app_info_get_all_for_type (content_type);
665 g_app_info_get_fallback_for_type (const char *content_type)
667 return g_app_info_get_all_for_type (content_type);
671 g_app_info_get_default_for_type (const char *content_type,
672 gboolean must_support_uris)
677 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
680 CFStringRef bundle_id;
683 mime_type = g_content_type_get_mime_type (content_type);
684 if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
686 gchar *scheme = strchr (mime_type, '/') + 1;
687 GAppInfo *ret = g_app_info_get_default_for_uri_scheme (scheme);
694 type = create_cfstring_from_cstr (content_type);
696 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
697 bundle_id = LSCopyDefaultApplicationURLForContentType (type, kLSRolesAll, NULL);
699 bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
705 g_warning ("No default handler found for content type '%s'.", content_type);
709 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
710 bundle = get_bundle_for_url (bundle_id);
712 bundle = get_bundle_for_id (bundle_id);
714 CFRelease (bundle_id);
719 return G_APP_INFO (g_osx_app_info_new (bundle));
723 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
725 CFStringRef scheme, bundle_id;
728 scheme = create_cfstring_from_cstr (uri_scheme);
729 bundle_id = LSCopyDefaultHandlerForURLScheme (scheme);
734 g_warning ("No default handler found for url scheme '%s'.", uri_scheme);
738 bundle = get_bundle_for_id (bundle_id);
739 CFRelease (bundle_id);
744 return G_APP_INFO (g_osx_app_info_new (bundle));
748 g_app_info_get_all (void)
750 /* There is no API for this afaict
751 * could manually do it...
757 g_app_info_reset_type_associations (const char *content_type)