ci: collect test coverage and deploy a html report through gitlab pages
[glib.git] / gio / gosxappinfo.c
blob463b2da3c5978c9a3ac89b4b6a28344aba7e4ea5
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 #ifdef G_ENABLE_DEBUG
175 static gchar *
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);
185 if (success)
186 return buffer;
187 else
189 g_free (buffer);
190 return NULL;
193 #endif
195 static char *
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);
206 host_start += 3;
207 host_end = strchr (host_start, '/');
209 if (host_end != NULL)
210 host = g_strndup (host_start, host_end - host_start);
211 else
212 host = g_strdup (host_start);
214 hostname = g_hostname_to_ascii (host);
216 ret = g_strconcat (scheme, "://", hostname, host_end, NULL);
218 g_free (scheme);
219 g_free (host);
220 g_free (hostname);
222 return ret;
225 return g_strdup (url);
228 static CFURLRef
229 create_url_from_cstr (gchar *cstr,
230 gboolean is_file)
232 gchar *puny_cstr;
233 CFStringRef str;
234 CFURLRef url;
236 puny_cstr = url_escape_hostname (cstr);
237 str = CFStringCreateWithCString (NULL, puny_cstr ? puny_cstr : cstr, kCFStringEncodingUTF8);
239 if (is_file)
240 url = CFURLCreateWithFileSystemPath (NULL, str, kCFURLPOSIXPathStyle, FALSE);
241 else
242 url = CFURLCreateWithString (NULL, str, NULL);
244 if (!url)
245 g_debug ("Creating CFURL from %s %s failed!", cstr, is_file ? "file" : "uri");
247 g_free (puny_cstr);
248 CFRelease(str);
249 return url;
252 static CFArrayRef
253 create_url_list_from_glist (GList *uris,
254 gboolean are_files)
256 GList *lst;
257 int len = g_list_length (uris);
258 CFMutableArrayRef array;
260 if (!len)
261 return NULL;
263 array = CFArrayCreateMutable (NULL, len, &kCFTypeArrayCallBacks);
264 if (!array)
265 return NULL;
267 for (lst = uris; lst != NULL && lst->data; lst = lst->next)
269 CFURLRef url = create_url_from_cstr ((char*)lst->data, are_files);
270 if (url)
271 CFArrayAppendValue (array, url);
274 return (CFArrayRef)array;
277 static LSLaunchURLSpec *
278 create_urlspec_for_appinfo (GOsxAppInfo *info,
279 GList *uris,
280 gboolean are_files)
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);
290 return urlspec;
293 static void
294 free_urlspec (LSLaunchURLSpec *urlspec)
296 if (urlspec->itemURLs)
298 CFArrayRemoveAllValues ((CFMutableArrayRef)urlspec->itemURLs);
299 CFRelease (urlspec->itemURLs);
301 CFRelease (urlspec->appURL);
302 g_free (urlspec);
305 static NSBundle *
306 get_bundle_for_url (CFURLRef app_url)
308 NSBundle *bundle = [NSBundle bundleWithURL: (NSURL*)app_url];
310 if (!bundle)
312 g_debug ("Bundle not found for url.");
313 return NULL;
316 return bundle;
319 static NSBundle *
320 get_bundle_for_id (CFStringRef bundle_id)
322 CFURLRef app_url;
323 NSBundle *bundle;
325 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
326 CFArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
327 if (urls)
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);
333 CFRetain (app_url);
334 CFRelease (urls);
336 else
337 #else
338 if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url) == kLSApplicationNotFoundErr)
339 #endif
341 #ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
342 gchar *id_str = create_cstr_from_cfstring (bundle_id);
343 if (id_str)
345 g_debug ("Application not found for id \"%s\".", id_str);
346 g_free (id_str);
348 else
349 g_debug ("Application not found for unconvertable bundle id.");
350 #endif
351 return NULL;
354 bundle = get_bundle_for_url (app_url);
355 CFRelease (app_url);
356 return bundle;
359 static const char *
360 g_osx_app_info_get_id (GAppInfo *appinfo)
362 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
364 if (!info->id)
365 info->id = get_bundle_string_value (info->bundle, @"CFBundleIdentifier");
367 return info->id;
370 static const char *
371 g_osx_app_info_get_name (GAppInfo *appinfo)
373 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
375 if (!info->name)
376 info->name = get_bundle_string_value (info->bundle, @"CFBundleName");
378 return info->name;
381 static const char *
382 g_osx_app_info_get_display_name (GAppInfo *appinfo)
384 return g_osx_app_info_get_name (appinfo);
387 static const char *
388 g_osx_app_info_get_description (GAppInfo *appinfo)
390 /* Bundles do not contain descriptions */
391 return NULL;
394 static const char *
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;
405 char *
406 g_osx_app_info_get_filename (GOsxAppInfo *info)
408 g_return_val_if_fail (info != NULL, NULL);
410 if (!info->filename)
412 info->filename = g_strconcat ("file://", [[info->bundle bundlePath]
413 cStringUsingEncoding: NSUTF8StringEncoding],
414 NULL);
417 return info->filename;
420 static const char *
421 g_osx_app_info_get_commandline (GAppInfo *appinfo)
423 /* There isn't really a command line value */
424 return NULL;
427 static GIcon *
428 g_osx_app_info_get_icon (GAppInfo *appinfo)
430 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
432 if (!info->icon)
434 gchar *icon_name, *app_uri, *icon_uri;
435 GFile *file;
437 icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile");
438 if (!icon_name)
439 return NULL;
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);
444 g_free (icon_name);
446 file = g_file_new_for_path (icon_uri);
447 info->icon = g_file_icon_new (file);
448 g_object_unref (file);
449 g_free (icon_uri);
452 return info->icon;
455 static gboolean
456 g_osx_app_info_launch_internal (GAppInfo *appinfo,
457 GList *uris,
458 gboolean are_files,
459 GError **error)
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);
470 success = FALSE;
473 free_urlspec (urlspec);
474 return success;
477 static gboolean
478 g_osx_app_info_supports_uris (GAppInfo *appinfo)
480 return TRUE;
483 static gboolean
484 g_osx_app_info_supports_files (GAppInfo *appinfo)
486 return TRUE;
489 static gboolean
490 g_osx_app_info_launch (GAppInfo *appinfo,
491 GList *files,
492 GAppLaunchContext *launch_context,
493 GError **error)
495 return g_osx_app_info_launch_internal (appinfo, files, TRUE, error);
498 static gboolean
499 g_osx_app_info_launch_uris (GAppInfo *appinfo,
500 GList *uris,
501 GAppLaunchContext *launch_context,
502 GError **error)
504 return g_osx_app_info_launch_internal (appinfo, uris, FALSE, error);
507 static gboolean
508 g_osx_app_info_should_show (GAppInfo *appinfo)
510 /* Bundles don't have hidden attribute */
511 return TRUE;
514 static gboolean
515 g_osx_app_info_set_as_default_for_type (GAppInfo *appinfo,
516 const char *content_type,
517 GError **error)
519 return FALSE;
522 static const char **
523 g_osx_app_info_get_supported_types (GAppInfo *appinfo)
525 /* TODO: get CFBundleDocumentTypes */
526 return NULL;
529 static gboolean
530 g_osx_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
531 const char *content_type,
532 GError **error)
534 /* Not supported. */
535 return FALSE;
538 static gboolean
539 g_osx_app_info_can_delete (GAppInfo *appinfo)
541 return FALSE;
544 static void
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;
571 GAppInfo *
572 g_app_info_create_from_commandline (const char *commandline,
573 const char *application_name,
574 GAppInfoCreateFlags flags,
575 GError **error)
577 return NULL;
580 GList *
581 g_osx_app_info_get_all_for_scheme (const char *cscheme)
583 CFArrayRef bundle_list;
584 CFStringRef scheme;
585 NSBundle *bundle;
586 GList *info_list = NULL;
587 gint i;
589 scheme = create_cfstring_from_cstr (cscheme);
590 bundle_list = LSCopyAllHandlersForURLScheme (scheme);
591 CFRelease (scheme);
593 if (!bundle_list)
594 return NULL;
596 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
598 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
599 GAppInfo *info;
601 bundle = get_bundle_for_id (bundle_id);
603 if (!bundle)
604 continue;
606 info = G_APP_INFO (g_osx_app_info_new (bundle));
607 info_list = g_list_append (info_list, info);
609 CFRelease (bundle_list);
610 return info_list;
613 GList *
614 g_app_info_get_all_for_type (const char *content_type)
616 gchar *mime_type;
617 CFArrayRef bundle_list;
618 CFStringRef type;
619 NSBundle *bundle;
620 GList *info_list = NULL;
621 gint i;
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);
629 g_free (mime_type);
630 return ret;
632 g_free (mime_type);
634 type = create_cfstring_from_cstr (content_type);
635 bundle_list = LSCopyAllRoleHandlersForContentType (type, kLSRolesAll);
636 CFRelease (type);
638 if (!bundle_list)
639 return NULL;
641 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
643 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
644 GAppInfo *info;
646 bundle = get_bundle_for_id (bundle_id);
648 if (!bundle)
649 continue;
651 info = G_APP_INFO (g_osx_app_info_new (bundle));
652 info_list = g_list_append (info_list, info);
654 CFRelease (bundle_list);
655 return info_list;
658 GList *
659 g_app_info_get_recommended_for_type (const char *content_type)
661 return g_app_info_get_all_for_type (content_type);
664 GList *
665 g_app_info_get_fallback_for_type (const char *content_type)
667 return g_app_info_get_all_for_type (content_type);
670 GAppInfo *
671 g_app_info_get_default_for_type (const char *content_type,
672 gboolean must_support_uris)
674 gchar *mime_type;
675 CFStringRef type;
676 NSBundle *bundle;
677 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
678 CFURLRef bundle_id;
679 #else
680 CFStringRef bundle_id;
681 #endif
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);
689 g_free (mime_type);
690 return ret;
692 g_free (mime_type);
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);
698 #else
699 bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
700 #endif
701 CFRelease (type);
703 if (!bundle_id)
705 g_warning ("No default handler found for content type '%s'.", content_type);
706 return NULL;
709 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
710 bundle = get_bundle_for_url (bundle_id);
711 #else
712 bundle = get_bundle_for_id (bundle_id);
713 #endif
714 CFRelease (bundle_id);
716 if (!bundle)
717 return NULL;
719 return G_APP_INFO (g_osx_app_info_new (bundle));
722 GAppInfo *
723 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
725 CFStringRef scheme, bundle_id;
726 NSBundle *bundle;
728 scheme = create_cfstring_from_cstr (uri_scheme);
729 bundle_id = LSCopyDefaultHandlerForURLScheme (scheme);
730 CFRelease (scheme);
732 if (!bundle_id)
734 g_warning ("No default handler found for url scheme '%s'.", uri_scheme);
735 return NULL;
738 bundle = get_bundle_for_id (bundle_id);
739 CFRelease (bundle_id);
741 if (!bundle)
742 return NULL;
744 return G_APP_INFO (g_osx_app_info_new (bundle));
747 GList *
748 g_app_info_get_all (void)
750 /* There is no API for this afaict
751 * could manually do it...
753 return NULL;
756 void
757 g_app_info_reset_type_associations (const char *content_type)