Blame gio/gosxappinfo.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2014 Patrick Griffis
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gappinfo.h"
Packit ae235b
#include "gosxappinfo.h"
Packit ae235b
#include "gcontenttype.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gfileicon.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
Packit ae235b
#import <CoreFoundation/CoreFoundation.h>
Packit ae235b
#import <Foundation/Foundation.h>
Packit ae235b
#import <ApplicationServices/ApplicationServices.h>
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gosxappinfo
Packit ae235b
 * @title: GOsxAppInfo
Packit ae235b
 * @short_description: Application information from NSBundles
Packit ae235b
 * @include: gio/gosxappinfo.h
Packit ae235b
 *
Packit ae235b
 * #GOsxAppInfo is an implementation of #GAppInfo based on NSBundle information.
Packit ae235b
 *
Packit ae235b
 * Note that `<gio/gosxappinfo.h>` is unique to OSX.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void        g_osx_app_info_iface_init (GAppInfoIface *iface);
Packit ae235b
static const char *g_osx_app_info_get_id     (GAppInfo      *appinfo);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GOsxAppInfo:
Packit ae235b
 *
Packit ae235b
 * Information about an installed application from a NSBundle.
Packit ae235b
 */
Packit ae235b
struct _GOsxAppInfo
Packit ae235b
{
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  NSBundle *bundle;
Packit ae235b
Packit ae235b
  /* Note that these are all NULL until first call
Packit ae235b
   * to getter at which point they are cached here
Packit ae235b
   */
Packit ae235b
  gchar *id;
Packit ae235b
  gchar *name;
Packit ae235b
  gchar *executable;
Packit ae235b
  gchar *filename;
Packit ae235b
  GIcon *icon;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GOsxAppInfo, g_osx_app_info, G_TYPE_OBJECT,
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_osx_app_info_iface_init))
Packit ae235b
Packit ae235b
static GOsxAppInfo *
Packit ae235b
g_osx_app_info_new (NSBundle *bundle)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = g_object_new (G_TYPE_OSX_APP_INFO, NULL);
Packit ae235b
Packit ae235b
  info->bundle = [bundle retain];
Packit ae235b
Packit ae235b
  return info;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_osx_app_info_init (GOsxAppInfo *info)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_osx_app_info_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = G_OSX_APP_INFO (object);
Packit ae235b
Packit ae235b
  g_free (info->id);
Packit ae235b
  g_free (info->name);
Packit ae235b
  g_free (info->executable);
Packit ae235b
  g_free (info->filename);
Packit ae235b
  g_clear_object (&info->icon);
Packit ae235b
Packit ae235b
  [info->bundle release];
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_osx_app_info_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_osx_app_info_class_init (GOsxAppInfoClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_osx_app_info_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GAppInfo *
Packit ae235b
g_osx_app_info_dup (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info;
Packit ae235b
  GOsxAppInfo *new_info;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (appinfo != NULL, NULL);
Packit ae235b
Packit ae235b
  info = G_OSX_APP_INFO (appinfo);
Packit ae235b
  new_info = g_osx_app_info_new ([info->bundle retain]);
Packit ae235b
Packit ae235b
  return G_APP_INFO (new_info);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_equal (GAppInfo *appinfo1,
Packit ae235b
                           GAppInfo *appinfo2)
Packit ae235b
{
Packit ae235b
  const gchar *str1, *str2;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (appinfo1 != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (appinfo2 != NULL, FALSE);
Packit ae235b
Packit ae235b
  str1 = g_osx_app_info_get_id (appinfo1);
Packit ae235b
  str2 = g_osx_app_info_get_id (appinfo2);
Packit ae235b
Packit ae235b
  return (g_strcmp0 (str1, str2) == 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * get_bundle_string_value:
Packit ae235b
 * @bundle: a #NSBundle
Packit ae235b
 * @key: an #NSString key
Packit ae235b
 *
Packit ae235b
 * Returns a value from a bundles info.plist file.
Packit ae235b
 * It will be utf8 encoded and it must be g_free()'d.
Packit ae235b
 *
Packit ae235b
 */
Packit ae235b
static gchar *
Packit ae235b
get_bundle_string_value (NSBundle *bundle,
Packit ae235b
                         NSString *key)
Packit ae235b
{
Packit ae235b
  NSString *value;
Packit ae235b
  const gchar *cvalue;
Packit ae235b
  gchar *ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (bundle != NULL, NULL);
Packit ae235b
Packit ae235b
  value = (NSString *)[bundle objectForInfoDictionaryKey: key];
Packit ae235b
  if (!value)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  cvalue = [value cStringUsingEncoding: NSUTF8StringEncoding];
Packit ae235b
  ret = g_strdup (cvalue);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static CFStringRef
Packit ae235b
create_cfstring_from_cstr (const gchar *cstr)
Packit ae235b
{
Packit ae235b
  return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_ENABLE_DEBUG
Packit ae235b
static gchar *
Packit ae235b
create_cstr_from_cfstring (CFStringRef str)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (str != NULL, NULL);
Packit ae235b
Packit ae235b
  CFIndex length = CFStringGetLength (str);
Packit ae235b
  CFIndex maxlen = CFStringGetMaximumSizeForEncoding (length, kCFStringEncodingUTF8);
Packit ae235b
  gchar *buffer = g_malloc (maxlen + 1);
Packit ae235b
  Boolean success = CFStringGetCString (str, (char *) buffer, maxlen,
Packit ae235b
                                        kCFStringEncodingUTF8);
Packit ae235b
  if (success)
Packit ae235b
    return buffer;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_free (buffer);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static char *
Packit ae235b
url_escape_hostname (const char *url)
Packit ae235b
{
Packit ae235b
  char *host_start, *ret;
Packit ae235b
Packit ae235b
  host_start = strstr (url, "://");
Packit ae235b
  if (host_start != NULL)
Packit ae235b
    {
Packit ae235b
      char *host_end, *scheme, *host, *hostname;
Packit ae235b
Packit ae235b
      scheme = g_strndup (url, host_start - url);
Packit ae235b
      host_start += 3;
Packit ae235b
      host_end = strchr (host_start, '/');
Packit ae235b
Packit ae235b
      if (host_end != NULL)
Packit ae235b
        host = g_strndup (host_start, host_end - host_start);
Packit ae235b
      else
Packit ae235b
        host = g_strdup (host_start);
Packit ae235b
Packit ae235b
      hostname = g_hostname_to_ascii (host);
Packit ae235b
Packit ae235b
      ret = g_strconcat (scheme, "://", hostname, host_end, NULL);
Packit ae235b
Packit ae235b
      g_free (scheme);
Packit ae235b
      g_free (host);
Packit ae235b
      g_free (hostname);
Packit ae235b
Packit ae235b
      return ret;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_strdup (url);
Packit ae235b
}
Packit ae235b
Packit ae235b
static CFURLRef
Packit ae235b
create_url_from_cstr (gchar    *cstr,
Packit ae235b
                      gboolean  is_file)
Packit ae235b
{
Packit ae235b
  gchar *puny_cstr;
Packit ae235b
  CFStringRef str;
Packit ae235b
  CFURLRef url;
Packit ae235b
Packit ae235b
  puny_cstr = url_escape_hostname (cstr);
Packit ae235b
  str = CFStringCreateWithCString (NULL, puny_cstr ? puny_cstr : cstr, kCFStringEncodingUTF8);
Packit ae235b
Packit ae235b
  if (is_file)
Packit ae235b
    url = CFURLCreateWithFileSystemPath (NULL, str, kCFURLPOSIXPathStyle, FALSE);
Packit ae235b
  else
Packit ae235b
    url = CFURLCreateWithString (NULL, str, NULL);
Packit ae235b
Packit ae235b
  if (!url)
Packit ae235b
    g_debug ("Creating CFURL from %s %s failed!", cstr, is_file ? "file" : "uri");
Packit ae235b
Packit ae235b
  g_free (puny_cstr);
Packit ae235b
  CFRelease(str);
Packit ae235b
  return url;
Packit ae235b
}
Packit ae235b
Packit ae235b
static CFArrayRef
Packit ae235b
create_url_list_from_glist (GList    *uris,
Packit ae235b
                            gboolean  are_files)
Packit ae235b
{
Packit ae235b
  GList *lst;
Packit ae235b
  int len = g_list_length (uris);
Packit ae235b
  CFMutableArrayRef array;
Packit ae235b
Packit ae235b
  if (!len)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  array = CFArrayCreateMutable (NULL, len, &kCFTypeArrayCallBacks);
Packit ae235b
  if (!array)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  for (lst = uris; lst != NULL && lst->data; lst = lst->next)
Packit ae235b
    {
Packit ae235b
      CFURLRef url = create_url_from_cstr ((char*)lst->data, are_files);
Packit ae235b
      if (url)
Packit ae235b
        CFArrayAppendValue (array, url);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (CFArrayRef)array;
Packit ae235b
}
Packit ae235b
Packit ae235b
static LSLaunchURLSpec *
Packit ae235b
create_urlspec_for_appinfo (GOsxAppInfo *info,
Packit ae235b
                            GList            *uris,
Packit ae235b
                            gboolean          are_files)
Packit ae235b
{
Packit ae235b
  LSLaunchURLSpec *urlspec = g_new0 (LSLaunchURLSpec, 1);
Packit ae235b
  gchar *app_cstr = g_osx_app_info_get_filename (info);
Packit ae235b
Packit ae235b
  /* Strip file:// from app url but ensure filesystem url */
Packit ae235b
  urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE);
Packit ae235b
  urlspec->launchFlags = kLSLaunchDefaults;
Packit ae235b
  urlspec->itemURLs = create_url_list_from_glist (uris, are_files);
Packit ae235b
Packit ae235b
  return urlspec;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
free_urlspec (LSLaunchURLSpec *urlspec)
Packit ae235b
{
Packit ae235b
  if (urlspec->itemURLs)
Packit ae235b
    {
Packit ae235b
      CFArrayRemoveAllValues ((CFMutableArrayRef)urlspec->itemURLs);
Packit ae235b
      CFRelease (urlspec->itemURLs);
Packit ae235b
    }
Packit ae235b
  CFRelease (urlspec->appURL);
Packit ae235b
  g_free (urlspec);
Packit ae235b
}
Packit ae235b
Packit ae235b
static NSBundle *
Packit ae235b
get_bundle_for_url (CFURLRef app_url)
Packit ae235b
{
Packit ae235b
  NSBundle *bundle = [NSBundle bundleWithURL: (NSURL*)app_url];
Packit ae235b
Packit ae235b
  if (!bundle)
Packit ae235b
    {
Packit ae235b
      g_debug ("Bundle not found for url.");
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return bundle;
Packit ae235b
}
Packit ae235b
Packit ae235b
static NSBundle *
Packit ae235b
get_bundle_for_id (CFStringRef bundle_id)
Packit ae235b
{
Packit ae235b
  CFURLRef app_url;
Packit ae235b
  NSBundle *bundle;
Packit ae235b
Packit ae235b
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
Packit ae235b
  CFArrayRef urls = LSCopyApplicationURLsForBundleIdentifier (bundle_id, NULL);
Packit ae235b
  if (urls)
Packit ae235b
    {
Packit ae235b
      /* TODO: if there's multiple, we should perhaps prefer one thats in $HOME,
Packit ae235b
       * instead of just always picking the first.
Packit ae235b
       */
Packit ae235b
      app_url = CFArrayGetValueAtIndex (urls, 0);
Packit ae235b
      CFRetain (app_url);
Packit ae235b
      CFRelease (urls);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
#else
Packit ae235b
  if (LSFindApplicationForInfo (kLSUnknownCreator, bundle_id, NULL, NULL, &app_url) == kLSApplicationNotFoundErr)
Packit ae235b
#endif
Packit ae235b
    {
Packit ae235b
#ifdef G_ENABLE_DEBUG /* This can fail often, no reason to alloc strings */
Packit ae235b
      gchar *id_str = create_cstr_from_cfstring (bundle_id);
Packit ae235b
      if (id_str)
Packit ae235b
        {
Packit ae235b
          g_debug ("Application not found for id \"%s\".", id_str);
Packit ae235b
          g_free (id_str);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_debug ("Application not found for unconvertable bundle id.");
Packit ae235b
#endif
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  bundle = get_bundle_for_url (app_url);
Packit ae235b
  CFRelease (app_url);
Packit ae235b
  return bundle;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
g_osx_app_info_get_id (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
Packit ae235b
Packit ae235b
  if (!info->id)
Packit ae235b
    info->id = get_bundle_string_value (info->bundle, @"CFBundleIdentifier");
Packit ae235b
Packit ae235b
  return info->id;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
g_osx_app_info_get_name (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
Packit ae235b
Packit ae235b
  if (!info->name)
Packit ae235b
    info->name = get_bundle_string_value (info->bundle, @"CFBundleName");
Packit ae235b
Packit ae235b
  return info->name;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
g_osx_app_info_get_display_name (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  return g_osx_app_info_get_name (appinfo);
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
g_osx_app_info_get_description (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  /* Bundles do not contain descriptions */
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
g_osx_app_info_get_executable (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
Packit ae235b
Packit ae235b
  if (!info->executable)
Packit ae235b
    info->executable = get_bundle_string_value (info->bundle, @"CFBundleExecutable");
Packit ae235b
Packit ae235b
  return info->executable;
Packit ae235b
}
Packit ae235b
Packit ae235b
char *
Packit ae235b
g_osx_app_info_get_filename (GOsxAppInfo *info)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (info != NULL, NULL);
Packit ae235b
Packit ae235b
  if (!info->filename)
Packit ae235b
    {
Packit ae235b
      info->filename = g_strconcat ("file://", [[info->bundle bundlePath]
Packit ae235b
                                    cStringUsingEncoding: NSUTF8StringEncoding],
Packit ae235b
                                    NULL);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return info->filename;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
g_osx_app_info_get_commandline (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  /* There isn't really a command line value */
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_osx_app_info_get_icon (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
Packit ae235b
Packit ae235b
  if (!info->icon)
Packit ae235b
    {
Packit ae235b
      gchar *icon_name, *app_uri, *icon_uri;
Packit ae235b
      GFile *file;
Packit ae235b
Packit ae235b
      icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile");
Packit ae235b
      if (!icon_name)
Packit ae235b
        return NULL;
Packit ae235b
Packit ae235b
      app_uri = g_osx_app_info_get_filename (info);
Packit ae235b
      icon_uri = g_strconcat (app_uri + 7, "/Contents/Resources/", icon_name,
Packit ae235b
                              g_str_has_suffix (icon_name, ".icns") ? NULL : ".icns", NULL);
Packit ae235b
      g_free (icon_name);
Packit ae235b
Packit ae235b
      file = g_file_new_for_path (icon_uri);
Packit ae235b
      info->icon = g_file_icon_new (file);
Packit ae235b
      g_object_unref (file);
Packit ae235b
      g_free (icon_uri);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return info->icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_launch_internal (GAppInfo  *appinfo,
Packit ae235b
                                     GList     *uris,
Packit ae235b
                                     gboolean   are_files,
Packit ae235b
                                     GError   **error)
Packit ae235b
{
Packit ae235b
  GOsxAppInfo *info = G_OSX_APP_INFO (appinfo);
Packit ae235b
  LSLaunchURLSpec *urlspec = create_urlspec_for_appinfo (info, uris, are_files);
Packit ae235b
  gint ret, success = TRUE;
Packit ae235b
Packit ae235b
  if ((ret = LSOpenFromURLSpec (urlspec, NULL)))
Packit ae235b
    {
Packit ae235b
      /* TODO: Better error codes */
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                   "Opening application failed with code %d", ret);
Packit ae235b
      success = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  free_urlspec (urlspec);
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_supports_uris (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_supports_files (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_launch (GAppInfo           *appinfo,
Packit ae235b
                            GList              *files,
Packit ae235b
                            GAppLaunchContext  *launch_context,
Packit ae235b
                            GError            **error)
Packit ae235b
{
Packit ae235b
  return g_osx_app_info_launch_internal (appinfo, files, TRUE, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_launch_uris (GAppInfo           *appinfo,
Packit ae235b
                                 GList              *uris,
Packit ae235b
                                 GAppLaunchContext  *launch_context,
Packit ae235b
                                 GError            **error)
Packit ae235b
{
Packit ae235b
  return g_osx_app_info_launch_internal (appinfo, uris, FALSE, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_should_show (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  /* Bundles don't have hidden attribute */
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_set_as_default_for_type (GAppInfo    *appinfo,
Packit ae235b
                                             const char  *content_type,
Packit ae235b
                                             GError     **error)
Packit ae235b
{
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char **
Packit ae235b
g_osx_app_info_get_supported_types (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  /* TODO: get CFBundleDocumentTypes */
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_set_as_last_used_for_type (GAppInfo   *appinfo,
Packit ae235b
                                               const char  *content_type,
Packit ae235b
                                               GError     **error)
Packit ae235b
{
Packit ae235b
  /* Not supported. */
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_osx_app_info_can_delete (GAppInfo *appinfo)
Packit ae235b
{
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_osx_app_info_iface_init (GAppInfoIface *iface)
Packit ae235b
{
Packit ae235b
  iface->dup = g_osx_app_info_dup;
Packit ae235b
  iface->equal = g_osx_app_info_equal;
Packit ae235b
Packit ae235b
  iface->get_id = g_osx_app_info_get_id;
Packit ae235b
  iface->get_name = g_osx_app_info_get_name;
Packit ae235b
  iface->get_display_name = g_osx_app_info_get_display_name;
Packit ae235b
  iface->get_description = g_osx_app_info_get_description;
Packit ae235b
  iface->get_executable = g_osx_app_info_get_executable;
Packit ae235b
  iface->get_commandline = g_osx_app_info_get_commandline;
Packit ae235b
  iface->get_icon = g_osx_app_info_get_icon;
Packit ae235b
  iface->get_supported_types = g_osx_app_info_get_supported_types;
Packit ae235b
Packit ae235b
  iface->set_as_last_used_for_type = g_osx_app_info_set_as_last_used_for_type;
Packit ae235b
  iface->set_as_default_for_type = g_osx_app_info_set_as_default_for_type;
Packit ae235b
Packit ae235b
  iface->launch = g_osx_app_info_launch;
Packit ae235b
  iface->launch_uris = g_osx_app_info_launch_uris;
Packit ae235b
Packit ae235b
  iface->supports_uris = g_osx_app_info_supports_uris;
Packit ae235b
  iface->supports_files = g_osx_app_info_supports_files;
Packit ae235b
  iface->should_show = g_osx_app_info_should_show;
Packit ae235b
  iface->can_delete = g_osx_app_info_can_delete;
Packit ae235b
}
Packit ae235b
Packit ae235b
GAppInfo *
Packit ae235b
g_app_info_create_from_commandline (const char           *commandline,
Packit ae235b
                                    const char           *application_name,
Packit ae235b
                                    GAppInfoCreateFlags   flags,
Packit ae235b
                                    GError              **error)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
GList *
Packit ae235b
g_osx_app_info_get_all_for_scheme (const char *cscheme)
Packit ae235b
{
Packit ae235b
  CFArrayRef bundle_list;
Packit ae235b
  CFStringRef scheme;
Packit ae235b
  NSBundle *bundle;
Packit ae235b
  GList *info_list = NULL;
Packit ae235b
  gint i;
Packit ae235b
  
Packit ae235b
  scheme = create_cfstring_from_cstr (cscheme);
Packit ae235b
  bundle_list = LSCopyAllHandlersForURLScheme (scheme);
Packit ae235b
  CFRelease (scheme);
Packit ae235b
Packit ae235b
  if (!bundle_list)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  for (i = 0; i < CFArrayGetCount (bundle_list); i++)
Packit ae235b
    {
Packit ae235b
      CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
Packit ae235b
      GAppInfo *info;
Packit ae235b
Packit ae235b
      bundle = get_bundle_for_id (bundle_id);
Packit ae235b
Packit ae235b
      if (!bundle)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      info = G_APP_INFO (g_osx_app_info_new (bundle));
Packit ae235b
      info_list = g_list_append (info_list, info);
Packit ae235b
    }
Packit ae235b
  CFRelease (bundle_list);
Packit ae235b
  return info_list;
Packit ae235b
}
Packit ae235b
Packit ae235b
GList *
Packit ae235b
g_app_info_get_all_for_type (const char *content_type)
Packit ae235b
{
Packit ae235b
  gchar *mime_type;
Packit ae235b
  CFArrayRef bundle_list;
Packit ae235b
  CFStringRef type;
Packit ae235b
  NSBundle *bundle;
Packit ae235b
  GList *info_list = NULL;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  mime_type = g_content_type_get_mime_type (content_type);
Packit ae235b
  if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
Packit ae235b
    {
Packit ae235b
      gchar *scheme = strchr (mime_type, '/') + 1;
Packit ae235b
      GList *ret = g_osx_app_info_get_all_for_scheme (scheme);
Packit ae235b
Packit ae235b
      g_free (mime_type);
Packit ae235b
      return ret;
Packit ae235b
    }
Packit ae235b
  g_free (mime_type);
Packit ae235b
Packit ae235b
  type = create_cfstring_from_cstr (content_type);
Packit ae235b
  bundle_list = LSCopyAllRoleHandlersForContentType (type, kLSRolesAll);
Packit ae235b
  CFRelease (type);
Packit ae235b
Packit ae235b
  if (!bundle_list)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  for (i = 0; i < CFArrayGetCount (bundle_list); i++)
Packit ae235b
    {
Packit ae235b
      CFStringRef bundle_id = CFArrayGetValueAtIndex (bundle_list, i);
Packit ae235b
      GAppInfo *info;
Packit ae235b
Packit ae235b
      bundle = get_bundle_for_id (bundle_id);
Packit ae235b
Packit ae235b
      if (!bundle)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      info = G_APP_INFO (g_osx_app_info_new (bundle));
Packit ae235b
      info_list = g_list_append (info_list, info);
Packit ae235b
    }
Packit ae235b
  CFRelease (bundle_list);
Packit ae235b
  return info_list;
Packit ae235b
}
Packit ae235b
Packit ae235b
GList *
Packit ae235b
g_app_info_get_recommended_for_type (const char *content_type)
Packit ae235b
{
Packit ae235b
  return g_app_info_get_all_for_type (content_type);
Packit ae235b
}
Packit ae235b
Packit ae235b
GList *
Packit ae235b
g_app_info_get_fallback_for_type (const char *content_type)
Packit ae235b
{
Packit ae235b
  return g_app_info_get_all_for_type (content_type);
Packit ae235b
}
Packit ae235b
Packit ae235b
GAppInfo *
Packit ae235b
g_app_info_get_default_for_type (const char *content_type,
Packit ae235b
                                 gboolean    must_support_uris)
Packit ae235b
{
Packit ae235b
  gchar *mime_type;
Packit ae235b
  CFStringRef type;
Packit ae235b
  NSBundle *bundle;
Packit ae235b
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
Packit ae235b
  CFURLRef bundle_id;
Packit ae235b
#else
Packit ae235b
  CFStringRef bundle_id;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  mime_type = g_content_type_get_mime_type (content_type);
Packit ae235b
  if (g_str_has_prefix (mime_type, "x-scheme-handler/"))
Packit ae235b
    {
Packit ae235b
      gchar *scheme = strchr (mime_type, '/') + 1;
Packit ae235b
      GAppInfo *ret = g_app_info_get_default_for_uri_scheme (scheme);
Packit ae235b
Packit ae235b
      g_free (mime_type);
Packit ae235b
      return ret;
Packit ae235b
    }
Packit ae235b
  g_free (mime_type);
Packit ae235b
Packit ae235b
  type = create_cfstring_from_cstr (content_type);
Packit ae235b
Packit ae235b
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
Packit ae235b
  bundle_id = LSCopyDefaultApplicationURLForContentType (type, kLSRolesAll, NULL);
Packit ae235b
#else
Packit ae235b
  bundle_id = LSCopyDefaultRoleHandlerForContentType (type, kLSRolesAll);
Packit ae235b
#endif
Packit ae235b
  CFRelease (type);
Packit ae235b
Packit ae235b
  if (!bundle_id)
Packit ae235b
    {
Packit ae235b
      g_warning ("No default handler found for content type '%s'.", content_type);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_10_AND_LATER
Packit ae235b
  bundle = get_bundle_for_url (bundle_id);
Packit ae235b
#else
Packit ae235b
  bundle = get_bundle_for_id (bundle_id);
Packit ae235b
#endif
Packit ae235b
  CFRelease (bundle_id);
Packit ae235b
Packit ae235b
  if (!bundle)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return G_APP_INFO (g_osx_app_info_new (bundle));
Packit ae235b
}
Packit ae235b
Packit ae235b
GAppInfo *
Packit ae235b
g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
Packit ae235b
{
Packit ae235b
  CFStringRef scheme, bundle_id;
Packit ae235b
  NSBundle *bundle;
Packit ae235b
Packit ae235b
  scheme = create_cfstring_from_cstr (uri_scheme);
Packit ae235b
  bundle_id = LSCopyDefaultHandlerForURLScheme (scheme);
Packit ae235b
  CFRelease (scheme);
Packit ae235b
Packit ae235b
  if (!bundle_id)
Packit ae235b
    {
Packit ae235b
      g_warning ("No default handler found for url scheme '%s'.", uri_scheme);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  bundle = get_bundle_for_id (bundle_id);
Packit ae235b
  CFRelease (bundle_id);
Packit ae235b
Packit ae235b
  if (!bundle)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return G_APP_INFO (g_osx_app_info_new (bundle));
Packit ae235b
}
Packit ae235b
Packit ae235b
GList *
Packit ae235b
g_app_info_get_all (void)
Packit ae235b
{
Packit ae235b
  /* There is no API for this afaict
Packit ae235b
   * could manually do it...
Packit ae235b
   */
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_app_info_reset_type_associations (const char *content_type)
Packit ae235b
{
Packit ae235b
}