1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2014 Patrick Griffis
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "gappinfo.h"
#include "gosxappinfo.h"
#include "gcontenttype.h"
#include "gfile.h"
#include "gfileicon.h"
#include "gioerror.h"
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
/**
* SECTION:gosxappinfo
* @title: GOsxAppInfo
* @short_description: Application information from NSBundles
* @include: gio/gosxappinfo.h
*
* #GOsxAppInfo is an implementation of #GAppInfo based on NSBundle information.
*
* Note that `<gio/gosxappinfo.h>` is unique to OSX.
*/
static void g_osx_app_info_iface_init (GAppInfoIface *iface);
static const char *g_osx_app_info_get_id (GAppInfo *appinfo);
/**
* GOsxAppInfo:
*
* Information about an installed application from a NSBundle.
*/
struct _GOsxAppInfo
{
GObject parent_instance;
NSBundle *bundle;
/* Note that these are all NULL until first call
* to getter at which point they are cached here
*/
gchar *id;
gchar *name;
gchar *executable;
gchar *filename;
GIcon *icon;
};
G_DEFINE_TYPE_WITH_CODE (GOsxAppInfo, g_osx_app_info, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_osx_app_info_iface_init))
static GOsxAppInfo *
g_osx_app_info_new (NSBundle *bundle)
{
GOsxAppInfo *info = g_object_new (G_TYPE_OSX_APP_INFO, NULL);
info->bundle = [bundle retain];
return info;
}
static void
g_osx_app_info_init (GOsxAppInfo *info)
{
}
static void
g_osx_app_info_finalize (GObject *object)
{
GOsxAppInfo *info = G_OSX_APP_INFO (object);
g_free (info->id);
g_free (info->name);
g_free (info->executable);
g_free (info->filename);
g_clear_object (&info->icon);
[info->bundle release];
G_OBJECT_CLASS (g_osx_app_info_parent_class)->finalize (object);
}
static void
g_osx_app_info_class_init (GOsxAppInfoClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = g_osx_app_info_finalize;
}
static GAppInfo *
g_osx_app_info_dup (GAppInfo *appinfo)
{
GOsxAppInfo *info;
GOsxAppInfo *new_info;
g_return_val_if_fail (appinfo != NULL, NULL);
info = G_OSX_APP_INFO (appinfo);
new_info = g_osx_app_info_new ([info->bundle retain]);
return G_APP_INFO (new_info);
}
static gboolean
g_osx_app_info_equal (GAppInfo *appinfo1,
GAppInfo *appinfo2)
{
const gchar *str1, *str2;
g_return_val_if_fail (appinfo1 != NULL, FALSE);
g_return_val_if_fail (appinfo2 != NULL, FALSE);
str1 = g_osx_app_info_get_id (appinfo1);
str2 = g_osx_app_info_get_id (appinfo2);
return (g_strcmp0 (str1, str2) == 0);
}
/*< internal >
* get_bundle_string_value:
* @bundle: a #NSBundle
* @key: an #NSString key
*
* Returns a value from a bundles info.plist file.
* It will be utf8 encoded and it must be g_free()'d.
*
*/
static gchar *
get_bundle_string_value (NSBundle *bundle,
NSString *key)
{
NSString *value;
const gchar *cvalue;
gchar *ret;
g_return_val_if_fail (bundle != NULL, NULL);
value = (NSString *)[bundle objectForInfoDictionaryKey: key];
if (!value)
return NULL;
cvalue = [value cStringUsingEncoding: NSUTF8StringEncoding];
ret = g_strdup (cvalue);
return ret;
}
static CFStringRef
create_cfstring_from_cstr (const gchar *cstr)
{
g_return_val_if_fail (cstr != NULL, NULL);
return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
}
#ifdef G_ENABLE_DEBUG
static gchar *
create_cstr_from_cfstring (CFStringRef str)
{
g_return_val_if_fail (str != NULL, NULL);
CFIndex length = CFStringGetLength (str);
CFIndex maxlen = CFStringGetMaximumSizeForEncoding (length, kCFStringEncodingUTF8);
gchar *buffer = g_malloc (maxlen + 1);
Boolean success = CFStringGetCString (str, (char *) buffer, maxlen,
kCFStringEncodingUTF8);
if (success)
return buffer;
else
{
g_free (buffer);
return NULL;
}
}
#endif
static char *
url_escape_hostname (const char *url)
{
char *host_start, *ret;
host_start = strstr (url, "://");
if (host_start != NULL)
{
char *host_end, *scheme, *host, *hostname;
scheme = g_strndup (url, host_start - url);
host_start += 3;
host_end = strchr (host_start, '/');
if (host_end != NULL)
host = g_strndup (host_start, host_end - host_start);
else
host = g_strdup (host_start);
hostname = g_hostname_to_ascii (host);
ret = g_strconcat (scheme, "://", hostname, host_end, NULL);
g_free (scheme);
g_free (host);
g_free (hostname);
return ret;
}
return g_strdup (url);
}
static CFURLRef
create_url_from_cstr (const gchar *cstr,
gboolean is_file)
{
gchar *puny_cstr;
CFStringRef str;
CFURLRef url;
puny_cstr = url_escape_hostname (cstr);
str = CFStringCreateWithCString (NULL, puny_cstr ? puny_cstr : cstr, kCFStringEncodingUTF8);
if (is_file)
url = CFURLCreateWithFileSystemPath (NULL, str, kCFURLPOSIXPathStyle, FALSE);
else
url = CFURLCreateWithString (NULL, str, NULL);
if (!url)
g_debug ("Creating CFURL from %s %s failed!", cstr, is_file ? "file" : "uri");
g_free (puny_cstr);
CFRelease(str);
return url;
}
static CFArrayRef
create_url_list_from_glist (GList *uris,
gboolean are_files)
{
GList *lst;
int len = g_list_length (uris);
CFMutableArrayRef array;
if (!len)
return NULL;
array = CFArrayCreateMutable (NULL, len, &kCFTypeArrayCallBacks);
if (!array)
return NULL;
for (lst = uris; lst != NULL && lst->data; lst = lst->next)
{
CFURLRef url = create_url_from_cstr ((char*)lst->data, are_files);
if (url)
CFArrayAppendValue (array, url);
}
return (CFArrayRef)array;
}
static LSLaunchURLSpec *
create_urlspec_for_appinfo (GOsxAppInfo *info,
GList *uris,
gboolean are_files)
{
LSLaunchURLSpec *urlspec = NULL;
const gchar *app_cstr;
g_return_val_if_fail (G_IS_OSX_APP_INFO (info), NULL);
urlspec = g_new0 (LSLaunchURLSpec, 1);
app_cstr = g_osx_app_info_get_filename (info);
g_assert (app_cstr != NULL);
/* Strip file:// from app url but ensure filesystem url */
urlspec->appURL = create_url_from_cstr (app_cstr + strlen ("file://"), TRUE);
urlspec->launchFlags = kLSLaunchDefaults;
urlspec->itemURLs = create_url_list_from_glist (uris, are_files);
return urlspec;
}
static void
free_urlspec (LSLaunchURLSpec *urlspec)
{
if (urlspec->itemURLs)
{
CFArrayRemoveAllValues ((CFMutableArrayRef)urlspec->itemURLs);
CFRelease (urlspec->itemURLs);
}
CFRelease (urlspec->appURL);
g_free (urlspec);
}
static NSBundle *
get_bundle_for_url (CFURLRef app_url)
{
NSBundle *bundle = [NSBundle bundleWithURL: (NSURL*)app_url];
if (!bundle)
{
g_debug ("Bundle not found for url.");
return NULL;
}
return bundle;
}
static NSBundle *
get_bundle_for_id (CFStringRef bundle_id)
{
CFURLRef app_url;
NSBundle *bundle;
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
CFArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
if (urls)
{
/* TODO: if there's multiple, we should perhaps prefer one that's in $HOME,
* instead of just always picking the first.
*/
app_url = CFArrayGetValueAtIndex (urls, 0);
CFRetain (app_url);
CFRelease (urls);
}
else
#else
if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url) == kLSApplicationNotFoundErr)
#endif
{
#ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
gchar *id_str = create_cstr_from_cfstring (bundle_id);
if (id_str)
{
g_debug ("Application not found for id \"%s\".", id_str);
g_free (id_str);
}
else
g_debug ("Application not found for unconvertable bundle id.");
#endif
return NULL;
}
bundle = get_bundle_for_url (app_url);
CFRelease (app_url);
return bundle;
}
static const char *
g_osx_app_info_get_id (GAppInfo *appinfo)
{
GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
if (!info->id)
info->id = get_bundle_string_value (info->bundle, @"CFBundleIdentifier");
return info->id;
}
static const char *
g_osx_app_info_get_name (GAppInfo *appinfo)
{
GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
if (!info->name)
info->name = get_bundle_string_value (info->bundle, @"CFBundleName");
return info->name;
}
static const char *
g_osx_app_info_get_display_name (GAppInfo *appinfo)
{
return g_osx_app_info_get_name (appinfo);
}
static const char *
g_osx_app_info_get_description (GAppInfo *appinfo)
{
/* Bundles do not contain descriptions */
return NULL;
}
static const char *
g_osx_app_info_get_executable (GAppInfo *appinfo)
{
GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
if (!info->executable)
info->executable = get_bundle_string_value (info->bundle, @"CFBundleExecutable");
return info->executable;
}
const char *
g_osx_app_info_get_filename (GOsxAppInfo *info)
{
g_return_val_if_fail (info != NULL, NULL);
if (!info->filename)
{
info->filename = g_strconcat ("file://", [[info->bundle bundlePath]
cStringUsingEncoding: NSUTF8StringEncoding],
NULL);
}
return info->filename;
}
static const char *
g_osx_app_info_get_commandline (GAppInfo *appinfo)
{
/* There isn't really a command line value */
return NULL;
}
static GIcon *
g_osx_app_info_get_icon (GAppInfo *appinfo)
{
GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
if (!info->icon)
{
const gchar *app_uri;
gchar *icon_name, *icon_uri;
GFile *file;
icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile");
if (!icon_name)
return NULL;
app_uri = g_osx_app_info_get_filename (info);
icon_uri = g_strconcat (app_uri + strlen ("file://"), "/Contents/Resources/", icon_name,
g_str_has_suffix (icon_name, ".icns") ? NULL : ".icns", NULL);
g_free (icon_name);
file = g_file_new_for_path (icon_uri);
info->icon = g_file_icon_new (file);
g_object_unref (file);
g_free (icon_uri);
}
return info->icon;
}
static gboolean
g_osx_app_info_launch_internal (GAppInfo *appinfo,
GList *uris,
gboolean are_files,
GError **error)
{
GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
LSLaunchURLSpec *urlspec;
gint ret, success = TRUE;
g_return_val_if_fail (G_IS_OSX_APP_INFO (appinfo), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
urlspec = create_urlspec_for_appinfo (info, uris, are_files);
if ((ret = LSOpenFromURLSpec (urlspec, NULL)))
{
/* TODO: Better error codes */
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Opening application failed with code %d", ret);
success = FALSE;
}
free_urlspec (urlspec);
return success;
}
static gboolean
g_osx_app_info_supports_uris (GAppInfo *appinfo)
{
return TRUE;
}
static gboolean
g_osx_app_info_supports_files (GAppInfo *appinfo)
{
return TRUE;
}
static gboolean
g_osx_app_info_launch (GAppInfo *appinfo,
GList *files,
GAppLaunchContext *launch_context,
GError **error)
{
return g_osx_app_info_launch_internal (appinfo, files, TRUE, error);
}
static gboolean
g_osx_app_info_launch_uris (GAppInfo *appinfo,
GList *uris,
GAppLaunchContext *launch_context,
GError **error)
{
return g_osx_app_info_launch_internal (appinfo, uris, FALSE, error);
}
static gboolean
g_osx_app_info_should_show (GAppInfo *appinfo)
{
/* Bundles don't have hidden attribute */
return TRUE;
}
static gboolean
g_osx_app_info_set_as_default_for_type (GAppInfo *appinfo,
const char *content_type,
GError **error)
{
return FALSE;
}
static const char **
g_osx_app_info_get_supported_types (GAppInfo *appinfo)
{
/* TODO: get CFBundleDocumentTypes */
return NULL;
}
static gboolean
g_osx_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
const char *content_type,
GError **error)
{
/* Not supported. */
return FALSE;
}
static gboolean
g_osx_app_info_can_delete (GAppInfo *appinfo)
{
return FALSE;
}
static void
g_osx_app_info_iface_init (GAppInfoIface *iface)
{
iface->dup = g_osx_app_info_dup;
iface->equal = g_osx_app_info_equal;
iface->get_id = g_osx_app_info_get_id;
iface->get_name = g_osx_app_info_get_name;
iface->get_display_name = g_osx_app_info_get_display_name;
iface->get_description = g_osx_app_info_get_description;
iface->get_executable = g_osx_app_info_get_executable;
iface->get_commandline = g_osx_app_info_get_commandline;
iface->get_icon = g_osx_app_info_get_icon;
iface->get_supported_types = g_osx_app_info_get_supported_types;
iface->set_as_last_used_for_type = g_osx_app_info_set_as_last_used_for_type;
iface->set_as_default_for_type = g_osx_app_info_set_as_default_for_type;
iface->launch = g_osx_app_info_launch;
iface->launch_uris = g_osx_app_info_launch_uris;
iface->supports_uris = g_osx_app_info_supports_uris;
iface->supports_files = g_osx_app_info_supports_files;
iface->should_show = g_osx_app_info_should_show;
iface->can_delete = g_osx_app_info_can_delete;
}
GAppInfo *
g_app_info_create_from_commandline (const char *commandline,
const char *application_name,
GAppInfoCreateFlags flags,
GError **error)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Creating an app info from a command line not currently supported");
return NULL;
}
GList *
g_osx_app_info_get_all_for_scheme (const char *cscheme)
{
CFArrayRef bundle_list;
CFStringRef scheme;
NSBundle *bundle;
GList *info_list = NULL;
gint i;
scheme = create_cfstring_from_cstr (cscheme);
bundle_list = LSCopyAllHandlersForURLScheme (scheme);
CFRelease (scheme);
if (!bundle_list)
return NULL;
for (i = 0; i < CFArrayGetCount (bundle_list); i++)
{
CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
GAppInfo *info;
bundle = get_bundle_for_id (bundle_id);
if (!bundle)
continue;
info = G_APP_INFO (g_osx_app_info_new (bundle));
info_list = g_list_append (info_list, info);
}
CFRelease (bundle_list);
return info_list;
}
GList *
g_app_info_get_all_for_type (const char *content_type)
{
gchar *mime_type;
CFArrayRef bundle_list;
CFStringRef type;
NSBundle *bundle;
GList *info_list = NULL;
gint i;
mime_type = g_content_type_get_mime_type (content_type);
if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
{
gchar *scheme = strchr (mime_type, '/') + 1;
GList *ret = g_osx_app_info_get_all_for_scheme (scheme);
g_free (mime_type);
return ret;
}
g_free (mime_type);
type = create_cfstring_from_cstr (content_type);
bundle_list = LSCopyAllRoleHandlersForContentType (type, kLSRolesAll);
CFRelease (type);
if (!bundle_list)
return NULL;
for (i = 0; i < CFArrayGetCount (bundle_list); i++)
{
CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
GAppInfo *info;
bundle = get_bundle_for_id (bundle_id);
if (!bundle)
continue;
info = G_APP_INFO (g_osx_app_info_new (bundle));
info_list = g_list_append (info_list, info);
}
CFRelease (bundle_list);
return info_list;
}
GList *
g_app_info_get_recommended_for_type (const char *content_type)
{
return g_app_info_get_all_for_type (content_type);
}
GList *
g_app_info_get_fallback_for_type (const char *content_type)
{
return g_app_info_get_all_for_type (content_type);
}
GAppInfo *
g_app_info_get_default_for_type (const char *content_type,
gboolean must_support_uris)
{
gchar *mime_type;
CFStringRef type;
NSBundle *bundle;
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
CFURLRef bundle_id;
#else
CFStringRef bundle_id;
#endif
mime_type = g_content_type_get_mime_type (content_type);
if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
{
gchar *scheme = strchr (mime_type, '/') + 1;
GAppInfo *ret = g_app_info_get_default_for_uri_scheme (scheme);
g_free (mime_type);
return ret;
}
g_free (mime_type);
type = create_cfstring_from_cstr (content_type);
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
bundle_id = LSCopyDefaultApplicationURLForContentType (type, kLSRolesAll, NULL);
#else
bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
#endif
CFRelease (type);
if (!bundle_id)
{
g_warning ("No default handler found for content type '%s'.", content_type);
return NULL;
}
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
bundle = get_bundle_for_url (bundle_id);
#else
bundle = get_bundle_for_id (bundle_id);
#endif
CFRelease (bundle_id);
if (!bundle)
return NULL;
return G_APP_INFO (g_osx_app_info_new (bundle));
}
GAppInfo *
g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
{
CFStringRef scheme, bundle_id;
NSBundle *bundle;
scheme = create_cfstring_from_cstr (uri_scheme);
bundle_id = LSCopyDefaultHandlerForURLScheme (scheme);
CFRelease (scheme);
if (!bundle_id)
{
g_warning ("No default handler found for url scheme '%s'.", uri_scheme);
return NULL;
}
bundle = get_bundle_for_id (bundle_id);
CFRelease (bundle_id);
if (!bundle)
return NULL;
return G_APP_INFO (g_osx_app_info_new (bundle));
}
GList *
g_app_info_get_all (void)
{
/* There is no API for this afaict
* could manually do it...
*/
return NULL;
}
void
g_app_info_reset_type_associations (const char *content_type)
{
}
|