Update Friulian translation
[glib.git] / gio / gosxappinfo.c
blobd62dfc0fde77e71ed8d9f21f9179497a9def0e79
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 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"
28 #import <CoreFoundation/CoreFoundation.h>
29 #import <Foundation/Foundation.h>
30 #import <ApplicationServices/ApplicationServices.h>
32 /**
33 * SECTION:gosxappinfo
34 * @title: GOsxAppInfo
35 * @short_description: Application information from NSBundles
36 * @include: gio/gosxappinfo.h
38 * #GOsxAppInfo is an implementation of #GAppInfo based on NSBundle information.
40 * Note that `<gio/gosxappinfo.h>` is unique to OSX.
43 static void g_osx_app_info_iface_init (GAppInfoIface *iface);
44 static const char *g_osx_app_info_get_id (GAppInfo *appinfo);
46 /**
47 * GOsxAppInfo:
49 * Information about an installed application from a NSBundle.
51 struct _GOsxAppInfo
53 GObject parent_instance;
55 NSBundle *bundle;
57 /* Note that these are all NULL until first call
58 * to getter at which point they are cached here
60 gchar *id;
61 gchar *name;
62 gchar *executable;
63 gchar *filename;
64 GIcon *icon;
67 G_DEFINE_TYPE_WITH_CODE (GOsxAppInfo, g_osx_app_info, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_osx_app_info_iface_init))
70 static GOsxAppInfo *
71 g_osx_app_info_new (NSBundle *bundle)
73 GOsxAppInfo *info = g_object_new (G_TYPE_OSX_APP_INFO, NULL);
75 info->bundle = [bundle retain];
77 return info;
80 static void
81 g_osx_app_info_init (GOsxAppInfo *info)
85 static void
86 g_osx_app_info_finalize (GObject *object)
88 GOsxAppInfo *info = G_OSX_APP_INFO (object);
90 g_free (info->id);
91 g_free (info->name);
92 g_free (info->executable);
93 g_free (info->filename);
94 g_clear_object (&info->icon);
96 [info->bundle release];
98 G_OBJECT_CLASS (g_osx_app_info_parent_class)->finalize (object);
101 static void
102 g_osx_app_info_class_init (GOsxAppInfoClass *klass)
104 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106 gobject_class->finalize = g_osx_app_info_finalize;
109 static GAppInfo *
110 g_osx_app_info_dup (GAppInfo *appinfo)
112 GOsxAppInfo *info;
113 GOsxAppInfo *new_info;
115 g_return_val_if_fail (appinfo != NULL, NULL);
117 info = G_OSX_APP_INFO (appinfo);
118 new_info = g_osx_app_info_new ([info->bundle retain]);
120 return G_APP_INFO (new_info);
123 static gboolean
124 g_osx_app_info_equal (GAppInfo *appinfo1,
125 GAppInfo *appinfo2)
127 const gchar *str1, *str2;
129 g_return_val_if_fail (appinfo1 != NULL, FALSE);
130 g_return_val_if_fail (appinfo2 != NULL, FALSE);
132 str1 = g_osx_app_info_get_id (appinfo1);
133 str2 = g_osx_app_info_get_id (appinfo2);
135 return (g_strcmp0 (str1, str2) == 0);
138 /*< internal >
139 * get_bundle_string_value:
140 * @bundle: a #NSBundle
141 * @key: an #NSString key
143 * Returns a value from a bundles info.plist file.
144 * It will be utf8 encoded and it must be g_free()'d.
147 static gchar *
148 get_bundle_string_value (NSBundle *bundle,
149 NSString *key)
151 NSString *value;
152 const gchar *cvalue;
153 gchar *ret;
155 g_return_val_if_fail (bundle != NULL, NULL);
157 value = (NSString *)[bundle objectForInfoDictionaryKey: key];
158 if (!value)
159 return NULL;
161 cvalue = [value cStringUsingEncoding: NSUTF8StringEncoding];
162 ret = g_strdup (cvalue);
164 return ret;
167 static CFStringRef
168 create_cfstring_from_cstr (const gchar *cstr)
170 return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
173 static gchar *
174 create_cstr_from_cfstring (CFStringRef str)
176 const gchar *cstr;
178 if (str == NULL)
179 return NULL;
181 cstr = CFStringGetCStringPtr (str, kCFStringEncodingUTF8);
182 CFRelease (str);
184 return g_strdup (cstr);
187 static char *
188 url_escape_hostname (const char *url)
190 char *host_start, *ret;
192 host_start = strstr (url, "://");
193 if (host_start != NULL)
195 char *host_end, *scheme, *host, *hostname;
197 scheme = g_strndup (url, host_start - url);
198 host_start += 3;
199 host_end = strchr (host_start, '/');
201 if (host_end != NULL)
202 host = g_strndup (host_start, host_end - host_start);
203 else
204 host = g_strdup (host_start);
206 hostname = g_hostname_to_ascii (host);
208 ret = g_strconcat (scheme, "://", hostname, host_end, NULL);
210 g_free (scheme);
211 g_free (host);
212 g_free (hostname);
214 return ret;
217 return g_strdup (url);
220 static CFURLRef
221 create_url_from_cstr (gchar *cstr,
222 gboolean is_file)
224 gchar *puny_cstr;
225 CFStringRef str;
226 CFURLRef url;
228 puny_cstr = url_escape_hostname (cstr);
229 str = CFStringCreateWithCString (NULL, puny_cstr ? puny_cstr : cstr, kCFStringEncodingUTF8);
231 if (is_file)
232 url = CFURLCreateWithFileSystemPath (NULL, str, kCFURLPOSIXPathStyle, FALSE);
233 else
234 url = CFURLCreateWithString (NULL, str, NULL);
236 if (!url)
237 g_debug ("Creating CFURL from %s %s failed!", cstr, is_file ? "file" : "uri");
239 g_free (puny_cstr);
240 CFRelease(str);
241 return url;
244 static CFArrayRef
245 create_url_list_from_glist (GList *uris,
246 gboolean are_files)
248 GList *lst;
249 int len = g_list_length (uris);
250 CFMutableArrayRef array;
252 if (!len)
253 return NULL;
255 array = CFArrayCreateMutable (NULL, len, &kCFTypeArrayCallBacks);
256 if (!array)
257 return NULL;
259 for (lst = uris; lst != NULL && lst->data; lst = lst->next)
261 CFURLRef url = create_url_from_cstr ((char*)lst->data, are_files);
262 if (url)
263 CFArrayAppendValue (array, url);
266 return (CFArrayRef)array;
269 static LSLaunchURLSpec *
270 create_urlspec_for_appinfo (GOsxAppInfo *info,
271 GList *uris,
272 gboolean are_files)
274 LSLaunchURLSpec *urlspec = g_new0 (LSLaunchURLSpec, 1);
275 gchar *app_cstr = g_osx_app_info_get_filename (info);
277 /* Strip file:// from app url but ensure filesystem url */
278 urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE);
279 urlspec->launchFlags = kLSLaunchDefaults;
280 urlspec->itemURLs = create_url_list_from_glist (uris, are_files);
282 return urlspec;
285 static void
286 free_urlspec (LSLaunchURLSpec *urlspec)
288 if (urlspec->itemURLs)
290 CFArrayRemoveAllValues ((CFMutableArrayRef)urlspec->itemURLs);
291 CFRelease (urlspec->itemURLs);
293 CFRelease (urlspec->appURL);
294 g_free (urlspec);
297 static NSBundle *
298 get_bundle_for_id (CFStringRef bundle_id)
300 CFURLRef app_url;
301 NSBundle *bundle;
303 #ifdef AVAILABLE_MAC_OS_VERSION_10_10_OR_LATER
304 CSArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
305 if (urls)
307 /* TODO: if there's multiple, we should perhaps prefer one thats in $HOME,
308 * instead of just always picking the first.
310 app_url = CFArrayGetValueAtIndex (urls, 0);
311 CFRetain (app_url);
312 CFRelease (urls);
314 else
315 #else
316 if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url))
317 #endif
319 #ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
320 gchar *id_str = create_cstr_from_cfstring (bundle_id);
321 g_debug ("Application not found for id \"%s\".", id_str);
322 g_free (id_str);
323 #endif
324 return NULL;
327 bundle = [NSBundle bundleWithURL: (NSURL*)app_url];
328 CFRelease (app_url);
330 if (!bundle)
332 g_debug ("Bundle not found for url.");
333 return NULL;
336 return bundle;
339 static const char *
340 g_osx_app_info_get_id (GAppInfo *appinfo)
342 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
344 if (!info->id)
345 info->id = get_bundle_string_value (info->bundle, @"CFBundleIdentifier");
347 return info->id;
350 static const char *
351 g_osx_app_info_get_name (GAppInfo *appinfo)
353 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
355 if (!info->name)
356 info->name = get_bundle_string_value (info->bundle, @"CFBundleName");
358 return info->name;
361 static const char *
362 g_osx_app_info_get_display_name (GAppInfo *appinfo)
364 return g_osx_app_info_get_name (appinfo);
367 static const char *
368 g_osx_app_info_get_description (GAppInfo *appinfo)
370 /* Bundles do not contain descriptions */
371 return NULL;
374 static const char *
375 g_osx_app_info_get_executable (GAppInfo *appinfo)
377 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
379 if (!info->executable)
380 info->executable = get_bundle_string_value (info->bundle, @"CFBundleExecutable");
382 return info->executable;
385 char *
386 g_osx_app_info_get_filename (GOsxAppInfo *info)
388 g_return_val_if_fail (info != NULL, NULL);
390 if (!info->filename)
392 info->filename = g_strconcat ("file://", [[info->bundle bundlePath]
393 cStringUsingEncoding: NSUTF8StringEncoding],
394 NULL);
397 return info->filename;
400 static const char *
401 g_osx_app_info_get_commandline (GAppInfo *appinfo)
403 /* There isn't really a command line value */
404 return NULL;
407 static GIcon *
408 g_osx_app_info_get_icon (GAppInfo *appinfo)
410 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
412 if (!info->icon)
414 gchar *icon_name, *app_uri, *icon_uri;
415 GFile *file;
417 icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile");
418 if (!icon_name)
419 return NULL;
421 app_uri = g_osx_app_info_get_filename (info);
422 icon_uri = g_strconcat (app_uri + 7, "/Contents/Resources/", icon_name,
423 g_str_has_suffix (icon_name, ".icns") ? NULL : ".icns", NULL);
424 g_free (icon_name);
426 file = g_file_new_for_path (icon_uri);
427 info->icon = g_file_icon_new (file);
428 g_object_unref (file);
429 g_free (icon_uri);
432 return info->icon;
435 static gboolean
436 g_osx_app_info_launch_internal (GAppInfo *appinfo,
437 GList *uris,
438 gboolean are_files,
439 GError **error)
441 GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
442 LSLaunchURLSpec *urlspec = create_urlspec_for_appinfo (info, uris, are_files);
443 gint ret, success = TRUE;
445 if ((ret = LSOpenFromURLSpec (urlspec, NULL)))
447 /* TODO: Better error codes */
448 g_set_error (error, G_IO_ERR, G_IO_ERROR_FAILED,
449 "Opening application failed with code %d", ret);
450 success = FALSE;
453 free_urlspec (urlspec);
454 return success;
457 static gboolean
458 g_osx_app_info_supports_uris (GAppInfo *appinfo)
460 return TRUE;
463 static gboolean
464 g_osx_app_info_supports_files (GAppInfo *appinfo)
466 return TRUE;
469 static gboolean
470 g_osx_app_info_launch (GAppInfo *appinfo,
471 GList *files,
472 GAppLaunchContext *launch_context,
473 GError **error)
475 return g_osx_app_info_launch_internal (appinfo, files, TRUE, error);
478 static gboolean
479 g_osx_app_info_launch_uris (GAppInfo *appinfo,
480 GList *uris,
481 GAppLaunchContext *launch_context,
482 GError **error)
484 return g_osx_app_info_launch_internal (appinfo, uris, FALSE, error);
487 static gboolean
488 g_osx_app_info_should_show (GAppInfo *appinfo)
490 /* Bundles don't have hidden attribute */
491 return TRUE;
494 static gboolean
495 g_osx_app_info_set_as_default_for_type (GAppInfo *appinfo,
496 const char *content_type,
497 GError **error)
499 return FALSE;
502 static const char **
503 g_osx_app_info_get_supported_types (GAppInfo *appinfo)
505 /* TODO: get CFBundleDocumentTypes */
506 return NULL;
509 static gboolean
510 g_osx_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
511 const char *content_type,
512 GError **error)
514 /* Not supported. */
515 return FALSE;
518 static gboolean
519 g_osx_app_info_can_delete (GAppInfo *appinfo)
521 return FALSE;
524 static void
525 g_osx_app_info_iface_init (GAppInfoIface *iface)
527 iface->dup = g_osx_app_info_dup;
528 iface->equal = g_osx_app_info_equal;
530 iface->get_id = g_osx_app_info_get_id;
531 iface->get_name = g_osx_app_info_get_name;
532 iface->get_display_name = g_osx_app_info_get_display_name;
533 iface->get_description = g_osx_app_info_get_description;
534 iface->get_executable = g_osx_app_info_get_executable;
535 iface->get_commandline = g_osx_app_info_get_commandline;
536 iface->get_icon = g_osx_app_info_get_icon;
537 iface->get_supported_types = g_osx_app_info_get_supported_types;
539 iface->set_as_last_used_for_type = g_osx_app_info_set_as_last_used_for_type;
540 iface->set_as_default_for_type = g_osx_app_info_set_as_default_for_type;
542 iface->launch = g_osx_app_info_launch;
543 iface->launch_uris = g_osx_app_info_launch_uris;
545 iface->supports_uris = g_osx_app_info_supports_uris;
546 iface->supports_files = g_osx_app_info_supports_files;
547 iface->should_show = g_osx_app_info_should_show;
548 iface->can_delete = g_osx_app_info_can_delete;
551 GAppInfo *
552 g_app_info_create_from_commandline (const char *commandline,
553 const char *application_name,
554 GAppInfoCreateFlags flags,
555 GError **error)
557 return NULL;
560 GList *
561 g_osx_app_info_get_all_for_scheme (const char *cscheme)
563 CFArrayRef bundle_list;
564 CFStringRef scheme;
565 NSBundle *bundle;
566 GList *info_list = NULL;
567 gint i;
569 scheme = create_cfstring_from_cstr (cscheme);
570 bundle_list = LSCopyAllHandlersForURLScheme (scheme);
571 CFRelease (scheme);
573 if (!bundle_list)
574 return NULL;
576 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
578 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
579 GAppInfo *info;
581 bundle = get_bundle_for_id (bundle_id);
583 if (!bundle)
584 continue;
586 info = G_APP_INFO (g_osx_app_info_new (bundle));
587 info_list = g_list_append (info_list, info);
590 return info_list;
593 GList *
594 g_app_info_get_all_for_type (const char *content_type)
596 gchar *type_cstr;
597 CFArrayRef bundle_list;
598 CFStringRef type;
599 NSBundle *bundle;
600 GList *info_list = NULL;
601 gint i;
603 if (g_str_has_prefix (content_type, "x-scheme-handler/"))
605 gchar *scheme = strchr (content_type, '/') + 1;
607 return g_osx_app_info_get_all_for_scheme (scheme);
610 type_cstr = g_content_type_from_mime_type (content_type);
611 type = create_cfstring_from_cstr (type_cstr);
612 g_free (type_cstr);
614 bundle_list = LSCopyAllRoleHandlersForContentType (type, kLSRolesAll);
615 CFRelease (type);
617 if (!bundle_list)
618 return NULL;
620 for (i = 0; i < CFArrayGetCount (bundle_list); i++)
622 CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
623 GAppInfo *info;
625 bundle = get_bundle_for_id (bundle_id);
627 if (!bundle)
628 continue;
630 info = G_APP_INFO (g_osx_app_info_new (bundle));
631 info_list = g_list_append (info_list, info);
634 return info_list;
637 GList *
638 g_app_info_get_recommended_for_type (const char *content_type)
640 return g_app_info_get_all_for_type (content_type);
643 GList *
644 g_app_info_get_fallback_for_type (const char *content_type)
646 return g_app_info_get_all_for_type (content_type);
649 GAppInfo *
650 g_app_info_get_default_for_type (const char *content_type,
651 gboolean must_support_uris)
653 gchar *type_cstr;
654 CFStringRef type, bundle_id;
655 NSBundle *bundle;
657 type_cstr = g_content_type_from_mime_type (content_type);
658 type = create_cfstring_from_cstr (type_cstr);
659 g_free (type_cstr);
661 bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
662 CFRelease (type);
664 if (!bundle_id)
666 g_warning ("No default handler found for mimetype '%s'.", content_type);
667 return NULL;
670 bundle = get_bundle_for_id (bundle_id);
671 CFRelease (bundle_id);
673 if (!bundle)
674 return NULL;
676 return G_APP_INFO (g_osx_app_info_new (bundle));
679 GAppInfo *
680 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
682 CFStringRef scheme, bundle_id;
683 NSBundle *bundle;
685 scheme = create_cfstring_from_cstr (uri_scheme);
686 bundle_id = LSCopyDefaultHandlerForURLScheme (scheme);
687 CFRelease (scheme);
689 if (!bundle_id)
691 g_warning ("No default handler found for url scheme '%s'.", uri_scheme);
692 return NULL;
695 bundle = get_bundle_for_id (bundle_id);
696 CFRelease (bundle_id);
698 if (!bundle)
699 return NULL;
701 return G_APP_INFO (g_osx_app_info_new (bundle));
704 GList *
705 g_app_info_get_all (void)
707 /* There is no API for this afaict
708 * could manually do it...
710 return NULL;
713 void
714 g_app_info_reset_type_associations (const char *content_type)