1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *   Simon Fraser <smfr@smfr.org>
24 *   Josh Aas <josh@mozilla.com>
25 *   Nick Kreeger <nick.kreeger@park.edu>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41#import "NSWorkspace+Utils.h"
42
43@implementation NSWorkspace(CaminoDefaultBrowserAdditions)
44
45- (NSArray*)installedBrowserIdentifiers
46{
47  NSArray* apps = [(NSArray*)LSCopyAllHandlersForURLScheme(CFSTR("https")) autorelease];
48
49  // add the default if it isn't there
50  NSString* defaultHandler = [self defaultBrowserIdentifier];
51  if (defaultHandler && ([apps indexOfObject:defaultHandler] == NSNotFound))
52    apps = [apps arrayByAddingObject:defaultHandler];
53
54  return apps;
55}
56
57- (NSArray*)installedFeedViewerIdentifiers
58{
59  NSArray* apps = [(NSArray*)LSCopyAllHandlersForURLScheme(CFSTR("feed")) autorelease];
60
61  // add the default if it isn't there
62  NSString* defaultHandler = [self defaultFeedViewerIdentifier];
63  if (defaultHandler && ([apps indexOfObject:defaultHandler] == NSNotFound))
64    apps = [apps arrayByAddingObject:defaultHandler];
65
66  return apps;
67}
68
69- (NSString*)defaultBrowserIdentifier
70{
71  NSString* defaultBundleId = [(NSString*)LSCopyDefaultHandlerForURLScheme(CFSTR("http")) autorelease];
72  // Sometimes LaunchServices likes to pretend there's no default browser.
73  // If that happens, we'll assume it's probably Safari.
74  if (!defaultBundleId)
75    defaultBundleId = @"com.apple.safari";
76  return defaultBundleId;
77}
78
79- (NSString*)defaultFeedViewerIdentifier
80{
81  return [(NSString*)LSCopyDefaultHandlerForURLScheme(CFSTR("feed")) autorelease];
82}
83
84- (NSURL*)defaultBrowserURL
85{
86  NSString* defaultBundleId = [self defaultBrowserIdentifier];
87  if (defaultBundleId)
88    return [self urlOfApplicationWithIdentifier:defaultBundleId];
89  return nil;
90}
91
92- (NSURL*)defaultFeedViewerURL
93{
94  NSString* defaultBundleId = [self defaultFeedViewerIdentifier];
95  if (defaultBundleId)
96    return [self urlOfApplicationWithIdentifier:defaultBundleId];
97  return nil;
98}
99
100- (void)setDefaultBrowserWithIdentifier:(NSString*)bundleID
101{
102  LSSetDefaultHandlerForURLScheme(CFSTR("http"), (CFStringRef)bundleID);
103  LSSetDefaultHandlerForURLScheme(CFSTR("https"), (CFStringRef)bundleID);
104  LSSetDefaultRoleHandlerForContentType(kUTTypeHTML, kLSRolesViewer, (CFStringRef)bundleID);
105  LSSetDefaultRoleHandlerForContentType(kUTTypeURL, kLSRolesViewer, (CFStringRef)bundleID);
106}
107
108- (void)setDefaultFeedViewerWithIdentifier:(NSString*)bundleID
109{
110  LSSetDefaultHandlerForURLScheme(CFSTR("feed"), (CFStringRef)bundleID);
111}
112
113- (NSURL*)urlOfApplicationWithIdentifier:(NSString*)bundleID
114{
115  if (!bundleID)
116    return nil;
117  NSURL* appURL = nil;
118  if (LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)bundleID, NULL, NULL, (CFURLRef*)&appURL) == noErr)
119    return [appURL autorelease];
120
121  return nil;
122}
123
124- (NSString*)identifierForBundle:(NSURL*)inBundleURL
125{
126  if (!inBundleURL) return nil;
127
128  NSBundle* tmpBundle = [NSBundle bundleWithPath:[[inBundleURL path] stringByStandardizingPath]];
129  if (tmpBundle)
130  {
131    NSString* tmpBundleID = [tmpBundle bundleIdentifier];
132    if (tmpBundleID && ([tmpBundleID length] > 0)) {
133      return tmpBundleID;
134    }
135  }
136  return nil;
137}
138
139- (NSString*)displayNameForFile:(NSURL*)inFileURL
140{
141  NSString *name;
142  LSCopyDisplayNameForURL((CFURLRef)inFileURL, (CFStringRef *)&name);
143  return [name autorelease];
144}
145
146//
147// +osVersionString
148//
149// Returns the system version string from
150// /System/Library/CoreServices/SystemVersion.plist
151// (as recommended by Apple).
152//
153+ (NSString*)osVersionString
154{
155  NSDictionary* versionInfo = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
156  return [versionInfo objectForKey:@"ProductVersion"];
157}
158
159//
160// +systemVersion
161//
162// Returns the host's OS version as returned by the 'sysv' gestalt selector,
163// 10.x.y = 0x000010xy
164//
165+ (long)systemVersion
166{
167  static SInt32 sSystemVersion = 0;
168  if (!sSystemVersion)
169    Gestalt(gestaltSystemVersion, &sSystemVersion);
170  return (long)sSystemVersion;
171}
172
173//
174// +isLeopardOrHigher
175//
176// returns YES if we're on 10.5 or better
177//
178+ (BOOL)isLeopardOrHigher
179{
180#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4
181  return YES;
182#else
183  return [self systemVersion] >= 0x1050;
184#endif
185}
186
187@end
188