gosxappinfo.m 18.3 KB
Newer Older
1 2 3 4 5 6 7
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2014 Patrick Griffis
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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"
27
#include "gioerror.h"
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

#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)
{
  return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
}

174
#ifdef G_ENABLE_DEBUG
175 176 177
static gchar *
create_cstr_from_cfstring (CFStringRef str)
{
178 179 180 181 182 183 184 185 186 187 188 189 190 191
  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;
    }
192
}
193
#endif
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

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);

214
      hostname = g_hostname_to_ascii (host);
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

      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 (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 = g_new0 (LSLaunchURLSpec, 1);
  gchar *app_cstr = g_osx_app_info_get_filename (info);

285 286
  /* Strip file:// from app url but ensure filesystem url */
  urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  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);
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318
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;
}

319 320 321 322 323 324
static NSBundle *
get_bundle_for_id (CFStringRef bundle_id)
{
  CFURLRef app_url;
  NSBundle *bundle;

325 326
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
  CFArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
327 328 329 330 331 332 333 334 335 336 337
  if (urls)
    {
      /* TODO: if there's multiple, we should perhaps prefer one thats in $HOME,
       * instead of just always picking the first.
       */
      app_url = CFArrayGetValueAtIndex (urls, 0);
      CFRetain (app_url);
      CFRelease (urls);
    }
  else
#else
338
  if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url) == kLSApplicationNotFoundErr)
339 340 341 342
#endif
    {
#ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
      gchar *id_str = create_cstr_from_cfstring (bundle_id);
343 344 345 346 347 348 349
      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.");
350 351 352 353
#endif
      return NULL;
    }

354
  bundle = get_bundle_for_url (app_url);
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
  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;
}

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)
    {
      gchar *icon_name, *app_uri, *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 + 7, "/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 = create_urlspec_for_appinfo (info, uris, are_files);
  gint ret, success = TRUE;

  if ((ret = LSOpenFromURLSpec (urlspec, NULL)))
    {
      /* TODO: Better error codes */
468
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
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
                   "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)
{
  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);
    }
609
  CFRelease (bundle_list);
610 611 612 613 614 615
  return info_list;
}

GList *
g_app_info_get_all_for_type (const char *content_type)
{
616
  gchar *mime_type;
617 618 619 620 621 622
  CFArrayRef bundle_list;
  CFStringRef type;
  NSBundle *bundle;
  GList *info_list = NULL;
  gint i;

623 624
  mime_type = g_content_type_get_mime_type (content_type);
  if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
625
    {
626 627
      gchar *scheme = strchr (mime_type, '/') + 1;
      GList *ret = g_osx_app_info_get_all_for_scheme (scheme);
628

629 630
      g_free (mime_type);
      return ret;
631
    }
632
  g_free (mime_type);
633

634
  type = create_cfstring_from_cstr (content_type);
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  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);
    }
654
  CFRelease (bundle_list);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
  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)
{
674
  gchar *mime_type;
675
  CFStringRef type;
676
  NSBundle *bundle;
677 678 679 680 681
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
  CFURLRef bundle_id;
#else
  CFStringRef bundle_id;
#endif
682

683 684 685 686 687 688 689 690 691 692 693
  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);

694
  type = create_cfstring_from_cstr (content_type);
695

696 697 698
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
  bundle_id = LSCopyDefaultApplicationURLForContentType (type, kLSRolesAll, NULL);
#else
699
  bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
700
#endif
701 702 703 704
  CFRelease (type);

  if (!bundle_id)
    {
705
      g_warning ("No default handler found for content type '%s'.", content_type);
706 707 708
      return NULL;
    }

709 710 711
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
  bundle = get_bundle_for_url (bundle_id);
#else
712
  bundle = get_bundle_for_id (bundle_id);
713
#endif
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
  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)
{
}