-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSSWebBrowsing.m
More file actions
175 lines (147 loc) · 5.94 KB
/
SSWebBrowsing.m
File metadata and controls
175 lines (147 loc) · 5.94 KB
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
#import "SSWebBrowsing.h"
#import "NSBundle+MainApp.h"
@implementation NSString (FirefoxQuicksearch)
+ (CFURLRef)sampleHttpUrl {
return (__bridge CFURLRef)[NSURL URLWithString:@"http://xx.xx"] ;
}
- (NSString*)stringByFixingFirefoxQuicksearch {
NSString* output ;
if ([self hasSuffix:@"%s"])
{
// This is one of Firefox' QuickSearches
// For examaple: "Type "dict <word>" in the location bar to perform a dictionary look-up"
// -URLWithString will fail with this, I suppose because the % at the end of its url is not allowed per some RFC.
// So, first we replace %s with our app name,
NSMutableString* temp = [self mutableCopy] ;
[temp replaceOccurrencesOfString:@"%s"
withString:[[NSBundle mainAppBundle] objectForInfoDictionaryKey:@"CFBundleName"]
options:NSBackwardsSearch
range:NSMakeRange(0, [temp length])] ;
output = [NSString stringWithString:temp] ;
// I tried removing with query (which wasn't easy because NSURL wouldn't initWithString until
// I first replaced %s with the app name). But this caused an "Internal Server Error" at
// answers.com, either with or without the ? query delimiter character.
#if !__has_feature(objc_arc)
[temp release];
#endif
}
else {
output = self ;
}
return output ;
}
@end
@implementation SSWebBrowsing
+ (CFURLRef)sampleHttpUrl {
return (__bridge CFURLRef)[NSURL URLWithString:@"http://xx.xx"] ;
}
+ (NSURL*)defaultBrowserUrl {
NSURL* answer = (NSURL*)CFBridgingRelease(LSCopyDefaultApplicationURLForURL([self sampleHttpUrl],
kLSRolesViewer,
NULL));
#if !__has_feature(objc_arc)
[answer autorelease];
#endif
return answer;
}
+ (NSString*)defaultBrowserDisplayName {
CFErrorRef error = nil ;
// Get browser's URL
NSURL *defaultBrowserURL = [self defaultBrowserUrl];
NSString* name = nil ;
if (!error && defaultBrowserURL) {
name = [[NSFileManager defaultManager] displayNameAtPath:[defaultBrowserURL path]] ;
// Remove suffix ".app":
if ([name hasSuffix:@".app"]) {
// This was not necessary in Mac OS 10.3.
name = [name substringToIndex:([name length] - 4)] ;
}
}
if (!name) {
name = @"??" ;
}
return name ;
}
+ (NSString*)defaultBrowserBundleIdentifier {
CFURLRef browserUrl = (__bridge CFURLRef)(CFBridgingRelease(LSCopyDefaultApplicationURLForURL(
[self sampleHttpUrl],
kLSRolesViewer,
NULL)));
// Get path (NSString*)
NSString* path = [(__bridge NSURL*)browserUrl path] ;
#if !__has_feature(objc_arc)
if (browserUrl) {
CFRelease(browserUrl) ;
}
#endif
// Get bundle
NSBundle* bundle = nil ;
if (path) {
bundle = [NSBundle bundleWithPath:(NSString*)path] ;
}
// Get bundleIdentifier
NSString* bundleIdentifier = nil ;
if (bundle) {
bundleIdentifier = [bundle bundleIdentifier] ;
}
else {
NSLog(@"Internal Error 324-2563. %@", path) ;
}
if (!bundleIdentifier) {
NSLog(@"Internal Error 324-4785. %@", bundle) ;
bundleIdentifier = @"" ;
}
return bundleIdentifier ;
}
+ (void)browseToURLString:(NSString*)urlString
browserBundleIdentifier:(NSString*)browserBundleIdentifier
activate:(BOOL)activate {
NSWorkspaceOpenConfiguration* configuration = [NSWorkspaceOpenConfiguration new];
if (!activate) {
configuration.activates = NO;
}
if (!browserBundleIdentifier) {
// Documentation for openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers:
// says that the app in the "default system binding" will be used if argument withAppBundleIdentifier is
// an empty string. However, it was found that this is not in general the desired default web browser!
// It is instead the user's "open with" for .html files, which some users may have incorrectly set
// to a non-web-browser application such as Preview or BBEdit. Fixed in Bookdog 4.3.13:
browserBundleIdentifier = [self defaultBrowserBundleIdentifier] ;
}
NSURL* url = nil ;
if (urlString) {
urlString = [urlString stringByFixingFirefoxQuicksearch] ;
url = [NSURL URLWithString:urlString] ;
}
if (url) {
NSArray* urls = [NSArray arrayWithObject:url] ;
NSURL* browserUrl = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:browserBundleIdentifier];
[[NSWorkspace sharedWorkspace] openURLs:urls
withApplicationAtURL:browserUrl
configuration:configuration
completionHandler:NULL];
}
else {
/* If this is a regular, foreground, dock-resident GUI app … */
if ([NSApp activationPolicy] == NSApplicationActivationPolicyRegular) {
/* This happens if we are passed an invalid URL such as a
JavaScript bookmarklet, for example…
javascript:var%20wRWMain1=window.open('','RefWorksBookmark');d=document;i='AddToRWScript';if(d.getElementById(i))RWAddToRW1();else{s=d.createElement('script');s.type='text/javascript';s.src='http://www.refworks.com/refworks/include/addtorw.asp';s.id=i;d.getElementsByTagName('head')[0].appendChild(s);}void(0); */
NSBeep();
}
}
#if !__has_feature(objc_arc)
[configuration release];
#endif
}
+ (NSImage*)faviconForDomain:(NSString*)domain {
NSString* urlString = [[NSString alloc] initWithFormat:
@"http://%@/favicon.ico", domain] ;
NSImage* favicon = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] ;
#if !__has_feature(objc_arc)
[urlString release];
[favicon autorelease];
#endif
return favicon;
}
@end