genmarshal Only wrap body prototypes in C++ guards
[glib.git] / gio / gosxappinfo.c
blobb24b6fffa4be01e832440af66609e675d51633bd
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/>.
20 #include "config.h"
22 #include "gappinfo.h"
23 #include "gosxappinfo.h"
24 #include "gcontenttype.h"
25 #include "gfile.h"
26 #include "gfileicon.h"
27 #include "gioerror.h"
29 #import <CoreFoundation/CoreFoundation.h>
30 #import <Foundation/Foundation.h>
31 #import <ApplicationServices/ApplicationServices.h>
33 /**
34 * SECTION:gosxappinfo
35 * @title: GOsxAppInfo
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);
47 /**
48 * GOsxAppInfo:
50 * Information about an installed application from a NSBundle.
52 struct _GOsxAppInfo
54 GObject parent_instance;
56 NSBundle *bundle;
58 /* Note that these are all NULL until first call
59 * to getter at which point they are cached here
61 gchar *id;
62 gchar *name;
63 gchar *executable;
64 gchar *filename;
65 GIcon *icon;
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))
71 static GOsxAppInfo *
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];
78 return info;
81 static void
82 g_osx_app_info_init (GOsxAppInfo *info)
86 static void
87 g_osx_app_info_finalize (GObject *object)
89 GOsxAppInfo *info = G_OSX_APP_INFO (object);
91 g_free (info->id);
92 g_free (info->name);
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);
102 static void
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;
110 static GAppInfo *
111 g_osx_app_info_dup (GAppInfo *appinfo)
113 GOsxAppInfo *info;
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);
124 static gboolean
125 g_osx_app_info_equal (GAppInfo *appinfo1,
126 GAppInfo *appinfo2)
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);
139 /*< internal >
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.
148 static gchar *
149 get_bundle_string_value (NSBundle *bundle,
150 NSString *key)
152 NSString *value;
153 const gchar *cvalue;
154 gchar *ret;
156 g_return_val_if_fail (bundle != NULL, NULL);
158 value = (NSString *)[bundle objectForInfoDictionaryKey: key];
159 if (!value)
160 return NULL;
162 cvalue = [value cStringUsingEncoding: NSUTF8StringEncoding];
163 ret = g_strdup (cvalue);
165 return ret;
168 static CFStringRef
169 create_cfstring_from_cstr (const gchar *cstr)
171 return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
174 static gchar *
175 create_cstr_from_cfstring (CFStringRef str)
177 const gchar *cstr;
179 if (str == NULL)
180 return NULL;
182 cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
183 CFRelease (str);
185 return g_strdup (cstr);
188 static char *
189 url_escape_hostname (const char *url)
191 char *host_start, *ret;
193 host_start = strstr (url, "://");
194 if (host_start != NULL)
196 char *host_end, *scheme, *host, *hostname;
198 scheme = g_strndup (url, host_start - url);
199 host_start += 3;
200 host_end = strchr (host_start, '/');
202 if (host_end != NULL)
203 host = g_strndup (host_start, host_end - host_start);
204 else
205 host = g_strdup (host_start);
207 hostname = g_hostname_to_ascii (host);
209 ret = g_strconcat (scheme, "://", hostname, host_end, NULL);
211 g_free (scheme);
212 g_free (host);
213 g_free (hostname);
215 return ret;
218 return g_strdup (url);
221 static CFURLRef
222 create_url_from_cstr (gchar *cstr,
223 gboolean is_file)
225 gchar *puny_cstr;
226 CFStringRef str;
227 CFURLRef url;
229 puny_cstr = url_escape_hostname (cstr);
230 str = CFStringCreateWithCString (NULL, puny_cstr ? puny_cstr : cstr, kCFStringEncodingUTF8);
232 if (is_file)
233 url = CFURLCreateWithFileSystemPath (NULL, str, kCFURLPOSIXPathStyle, FALSE);
234 else
235 url = CFURLCreateWithString (NULL, str, NULL);
237 if (!url)
238 g_debug ("Creating CFURL from %s %s failed!", cstr, is_file ? "file" : "uri");
240 g_free (puny_cstr);
241 CFRelease(str);
242 return url;
245 static CFArrayRef
246 create_url_list_from_glist (GList *uris,
247 gboolean are_files)
249 GList *lst;
250 int len = g_list_length (uris);
251 CFMutableArrayRef array;
253 if (!len)
254 return NULL;
256 array = CFArrayCreateMutable (NULL, len, &kCFTypeArrayCallBacks);
257 if (!array)
258 return NULL;
260 for (lst = uris; lst != NULL && lst->data; lst = lst->next)
262 CFURLRef url = create_url_from_cstr ((char*)lst->data, are_files);
263 if (url)
264 CFArrayAppendValue (array, url);
267 return (CFArrayRef)array;
270 static LSLaunchURLSpec *
271 create_urlspec_for_appinfo (GOsxAppInfo *info,
272 GList *uris,
273 gboolean are_files)
275 LSLaunchURLSpec *urlspec = g_new0 (LSLaunchURLSpec, 1);
276 gchar *app_cstr = g_osx_app_info_get_filename (info);
278 /* Strip file:// from app url but ensure filesystem url */
279 urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE);
280 urlspec->launchFlags = kLSLaunchDefaults;
281 urlspec->itemURLs = create_url_list_from_glist (uris, are_files);
283 return urlspec;
286 static void
287 free_urlspec (LSLaunchURLSpec *urlspec)
289 if (urlspec->itemURLs)
291 CFArrayRemoveAllValues ((CFMutableArrayRef)urlspec->itemURLs);
292 CFRelease (urlspec->itemURLs);
294 CFRelease (urlspec->appURL);
295 g_free (urlspec);
298 static NSBundle *
299 get_bundle_for_url (CFURLRef app_url)
301 NSBundle *bundle = [NSBundle bundleWithURL: (NSURL*)app_url];
303 if (!bundle)
305 g_debug ("Bundle not found for url.");
306 return NULL;
309 return bundle;
312 static NSBundle *
313 get_bundle_for_id (CFStringRef bundle_id)
315 CFURLRef app_url;
316 NSBundle *bundle;
318 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
319 CFArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
320 if (urls)
322 /* TODO: if there's multiple, we should perhaps prefer one thats in $HOME,
323 * instead of just always picking the first.
325 app_url = CFArrayGetValueAtIndex (urls, 0);
326 CFRetain (app_url);
327 CFRelease (urls);
329 else
330 #else
331 if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url))
332 #endif
334 #ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
335 gchar *id_str = create_cstr_from_cfstring (bundle_id);
336 g_debug ("Application not found for id \"%s\".", id_str);
337 g_free (id_str);
338 #endif
339 return NULL;
342 bundle = get_bundle_for_url (app_url);
343 CFRelease (app_url);
344 return bundle;
347 static const char *
348 g_osx_app_info_get_id (GAppInfo *appinfo)
350 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
352 if (!info->id)
353 info->id = get_bundle_string_value (info->bundle, @"CFBundleIdentifier");
355 return info->id;
358 static const char *
359 g_osx_app_info_get_name (GAppInfo *appinfo)
361 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
363 if (!info->name)
364 info->name = get_bundle_string_value (info->bundle, @"CFBundleName");
366 return info->name;
369 static const char *
370 g_osx_app_info_get_display_name (GAppInfo *appinfo)
372 return g_osx_app_info_get_name (appinfo);
375 static const char *
376 g_osx_app_info_get_description (GAppInfo *appinfo)
378 /* Bundles do not contain descriptions */
379 return NULL;
382 static const char *
383 g_osx_app_info_get_executable (GAppInfo *appinfo)
385 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
387 if (!info->executable)
388 info->executable = get_bundle_string_value (info->bundle, @"CFBundleExecutable");
390 return info->executable;
393 char *
394 g_osx_app_info_get_filename (GOsxAppInfo *info)
396 g_return_val_if_fail (info != NULL, NULL);
398 if (!info->filename)
400 info->filename = g_strconcat ("file://", [[info->bundle bundlePath]
401 cStringUsingEncoding: NSUTF8StringEncoding],
402 NULL);
405 return info->filename;
408 static const char *
409 g_osx_app_info_get_commandline (GAppInfo *appinfo)
411 /* There isn't really a command line value */
412 return NULL;
415 static GIcon *
416 g_osx_app_info_get_icon (GAppInfo *appinfo)
418 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
420 if (!info->icon)
422 gchar *icon_name, *app_uri, *icon_uri;
423 GFile *file;
425 icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile");
426 if (!icon_name)
427 return NULL;
429 app_uri = g_osx_app_info_get_filename (info);
430 icon_uri = g_strconcat (app_uri + 7, "/Contents/Resources/", icon_name,
431 g_str_has_suffix (icon_name, ".icns") ? NULL : ".icns", NULL);
432 g_free (icon_name);
434 file = g_file_new_for_path (icon_uri);
435 info->icon = g_file_icon_new (file);
436 g_object_unref (file);
437 g_free (icon_uri);
440 return info->icon;
443 static gboolean
444 g_osx_app_info_launch_internal (GAppInfo *appinfo,
445 GList *uris,
446 gboolean are_files,
447 GError **error)
449 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
450 LSLaunchURLSpec *urlspec = create_urlspec_for_appinfo (info, uris, are_files);
451 gint ret, success = TRUE;
453 if ((ret = LSOpenFromURLSpec (urlspec, NULL)))
455 /* TODO: Better error codes */
456 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
457 "Opening application failed with code %d", ret);
458 success = FALSE;
461 free_urlspec (urlspec);
462 return success;
465 static gboolean
466 g_osx_app_info_supports_uris (GAppInfo *appinfo)
468 return TRUE;
471 static gboolean
472 g_osx_app_info_supports_files (GAppInfo *appinfo)
474 return TRUE;
477 static gboolean
478 g_osx_app_info_launch (GAppInfo *appinfo,
479 GList *files,
480 GAppLaunchContext *launch_context,
481 GError **error)
483 return g_osx_app_info_launch_internal (appinfo, files, TRUE, error);
486 static gboolean
487 g_osx_app_info_launch_uris (GAppInfo *appinfo,
488 GList *uris,
489 GAppLaunchContext *launch_context,
490 GError **error)
492 return g_osx_app_info_launch_internal (appinfo, uris, FALSE, error);
495 static gboolean
496 g_osx_app_info_should_show (GAppInfo *appinfo)
498 /* Bundles don't have hidden attribute */
499 return TRUE;
502 static gboolean
503 g_osx_app_info_set_as_default_for_type (GAppInfo *appinfo,
504 const char *content_type,
505 GError **error)
507 return FALSE;
510 static const char **
511 g_osx_app_info_get_supported_types (GAppInfo *appinfo)
513 /* TODO: get CFBundleDocumentTypes */
514 return NULL;
517 static gboolean
518 g_osx_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
519 const char *content_type,
520 GError **error)
522 /* Not supported. */
523 return FALSE;
526 static gboolean
527 g_osx_app_info_can_delete (GAppInfo *appinfo)
529 return FALSE;
532 static void
533 g_osx_app_info_iface_init (GAppInfoIface *iface)
535 iface->dup = g_osx_app_info_dup;
536 iface->equal = g_osx_app_info_equal;
538 iface->get_id = g_osx_app_info_get_id;
539 iface->get_name = g_osx_app_info_get_name;
540 iface->get_display_name = g_osx_app_info_get_display_name;
541 iface->get_description = g_osx_app_info_get_description;
542 iface->get_executable = g_osx_app_info_get_executable;
543 iface->get_commandline = g_osx_app_info_get_commandline;
544 iface->get_icon = g_osx_app_info_get_icon;
545 iface->get_supported_types = g_osx_app_info_get_supported_types;
547 iface->set_as_last_used_for_type = g_osx_app_info_set_as_last_used_for_type;
548 iface->set_as_default_for_type = g_osx_app_info_set_as_default_for_type;
550 iface->launch = g_osx_app_info_launch;
551 iface->launch_uris = g_osx_app_info_launch_uris;
553 iface->supports_uris = g_osx_app_info_supports_uris;
554 iface->supports_files = g_osx_app_info_supports_files;
555 iface->should_show = g_osx_app_info_should_show;
556 iface->can_delete = g_osx_app_info_can_delete;
559 GAppInfo *
560 g_app_info_create_from_commandline (const char *commandline,
561 const char *application_name,
562 GAppInfoCreateFlags flags,
563 GError **error)
565 return NULL;
568 GList *
569 g_osx_app_info_get_all_for_scheme (const char *cscheme)
571 CFArrayRef bundle_list;
572 CFStringRef scheme;
573 NSBundle *bundle;
574 GList *info_list = NULL;
575 gint i;
577 scheme = create_cfstring_from_cstr (cscheme);
578 bundle_list = LSCopyAllHandlersForURLScheme (scheme);
579 CFRelease (scheme);
581 if (!bundle_list)
582 return NULL;
584 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
586 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
587 GAppInfo *info;
589 bundle = get_bundle_for_id (bundle_id);
591 if (!bundle)
592 continue;
594 info = G_APP_INFO (g_osx_app_info_new (bundle));
595 info_list = g_list_append (info_list, info);
598 return info_list;
601 GList *
602 g_app_info_get_all_for_type (const char *content_type)
604 gchar *mime_type;
605 CFArrayRef bundle_list;
606 CFStringRef type;
607 NSBundle *bundle;
608 GList *info_list = NULL;
609 gint i;
611 mime_type = g_content_type_get_mime_type (content_type);
612 if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
614 gchar *scheme = strchr (mime_type, '/') + 1;
615 GList *ret = g_osx_app_info_get_all_for_scheme (scheme);
617 g_free (mime_type);
618 return ret;
620 g_free (mime_type);
622 type = create_cfstring_from_cstr (content_type);
623 bundle_list = LSCopyAllRoleHandlersForContentType (type, kLSRolesAll);
624 CFRelease (type);
626 if (!bundle_list)
627 return NULL;
629 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
631 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
632 GAppInfo *info;
634 bundle = get_bundle_for_id (bundle_id);
636 if (!bundle)
637 continue;
639 info = G_APP_INFO (g_osx_app_info_new (bundle));
640 info_list = g_list_append (info_list, info);
643 return info_list;
646 GList *
647 g_app_info_get_recommended_for_type (const char *content_type)
649 return g_app_info_get_all_for_type (content_type);
652 GList *
653 g_app_info_get_fallback_for_type (const char *content_type)
655 return g_app_info_get_all_for_type (content_type);
658 GAppInfo *
659 g_app_info_get_default_for_type (const char *content_type,
660 gboolean must_support_uris)
662 gchar *mime_type;
663 CFStringRef type;
664 NSBundle *bundle;
665 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
666 CFURLRef bundle_id;
667 #else
668 CFStringRef bundle_id;
669 #endif
671 mime_type = g_content_type_get_mime_type (content_type);
672 if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
674 gchar *scheme = strchr (mime_type, '/') + 1;
675 GAppInfo *ret = g_app_info_get_default_for_uri_scheme (scheme);
677 g_free (mime_type);
678 return ret;
680 g_free (mime_type);
682 type = create_cfstring_from_cstr (content_type);
684 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
685 bundle_id = LSCopyDefaultApplicationURLForContentType (type, kLSRolesAll, NULL);
686 #else
687 bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
688 #endif
689 CFRelease (type);
691 if (!bundle_id)
693 g_warning ("No default handler found for content type '%s'.", content_type);
694 return NULL;
697 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
698 bundle = get_bundle_for_url (bundle_id);
699 #else
700 bundle = get_bundle_for_id (bundle_id);
701 #endif
702 CFRelease (bundle_id);
704 if (!bundle)
705 return NULL;
707 return G_APP_INFO (g_osx_app_info_new (bundle));
710 GAppInfo *
711 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
713 CFStringRef scheme, bundle_id;
714 NSBundle *bundle;
716 scheme = create_cfstring_from_cstr (uri_scheme);
717 bundle_id = LSCopyDefaultHandlerForURLScheme (scheme);
718 CFRelease (scheme);
720 if (!bundle_id)
722 g_warning ("No default handler found for url scheme '%s'.", uri_scheme);
723 return NULL;
726 bundle = get_bundle_for_id (bundle_id);
727 CFRelease (bundle_id);
729 if (!bundle)
730 return NULL;
732 return G_APP_INFO (g_osx_app_info_new (bundle));
735 GList *
736 g_app_info_get_all (void)
738 /* There is no API for this afaict
739 * could manually do it...
741 return NULL;
744 void
745 g_app_info_reset_type_associations (const char *content_type)