-
Notifications
You must be signed in to change notification settings - Fork 20
/
KSWebLocationPasteboardUtilities.m
312 lines (254 loc) · 10.4 KB
/
KSWebLocationPasteboardUtilities.m
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
//
// KSWebLocationPasteboardUtilities.m
//
// Created by Mike Abdullah
// Copyright © 2007 Karelia Software
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "KSWebLocationPasteboardUtilities.h"
#import "KSURLFormatter.h"
#import "KSURLUtilities.h"
@interface KSWebLocation (PasteboardPrivate)
+ (NSArray *)webLocationsWithBookmarkDictionariesPasteboardPropertyList:(id)propertyList;
+ (NSArray *)webLocationsWithWebURLsWithTitlesPasteboardPropertyList:(id)propertyList;
+ (NSArray *)webLocationsWithFilenamesPasteboardPropertyList:(id)propertyList;
@end
#pragma mark -
@implementation KSWebLocation (Pasteboard)
#pragma mark Pasteboard Reading
+ (NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard;
{
NSArray *URLTypes = nil;
if ([NSURL respondsToSelector:@selector(readableTypesForPasteboard:)])
{
URLTypes = [NSURL performSelector:@selector(readableTypesForPasteboard:)
withObject:pasteboard];
}
NSArray *result = [NSArray arrayWithObjects:
NSURLPboardType,
NSStringPboardType,
NSRTFPboardType,
NSRTFDPboardType, nil];
if (URLTypes) result = [URLTypes arrayByAddingObjectsFromArray:result];
return result;
}
#if (defined MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
+ (NSUInteger)readingOptionsForType:(NSString *)type
pasteboard:(NSPasteboard *)pasteboard;
{
NSPasteboardReadingOptions result = NSPasteboardReadingAsString;
if ([type isEqualToString:@"WebURLsWithTitlesPboardType"] ||
[type isEqualToString:@"BookmarkDictionaryListPboardType"] ||
[type isEqualToString:NSFilenamesPboardType])
{
result = NSPasteboardReadingAsPropertyList;
}
return result;
}
#endif
+ (NSString *)guessTitleForURL:(NSURL *)URL;
{
NSString *result = [[URL ks_lastPathComponent] stringByDeletingPathExtension];
result = [result stringByReplacingOccurrencesOfString:@"_" withString:@" "];
return result;
}
- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type;
{
// Try with NSURL
NSURL *URL = [[NSURL alloc] initWithPasteboardPropertyList:propertyList ofType:type];
if (URL)
{
self = [self initWithURL:URL title:[[self class] guessTitleForURL:URL]];
[URL release];
}
// Fallback to trying to get a string out of the plist
else
{
NSString *string = [propertyList description];
if ([string length] <= 2048) // No point processing particularly long strings
{
NSURL *plistURL = [KSURLFormatter URLFromString:string]; /// encodeLegally to handle accented characters
if (plistURL && [plistURL ks_hasNetworkLocation])
{
return [self initWithURL:plistURL title:[[self class] guessTitleForURL:URL]];
}
}
[self release]; self = nil;
}
return self;
}
+ (KSWebLocation *)webLocationFromPasteboard:(NSPasteboard *)pasteboard;
{
KSWebLocation *result = nil;
NSURL *URL = [self URLFromPasteboard:pasteboard];
if (URL)
{
result = [KSWebLocation webLocationWithURL:URL
title:[WebView URLTitleFromPasteboard:pasteboard]];
}
return result;
}
+ (NSURL *)URLFromPasteboard:(NSPasteboard *)pboard
{
NSURL *result = [WebView URLFromPasteboard:pboard];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
if (!result)
{
NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
if ([spellChecker respondsToSelector:@selector(checkString:range:types:options:inSpellDocumentWithTag:orthography:wordCount:)])
{
NSString *string = [pboard stringForType:NSStringPboardType];
NSArray *checkResults = [spellChecker checkString:string
range:NSMakeRange(0, [string length])
types:(NSTextCheckingTypes)NSTextCheckingTypeLink // cast to suppress oddity of SDK
options:nil
inSpellDocumentWithTag:0
orthography:NULL
wordCount:NULL];
if ([checkResults count])
{
result = [[checkResults objectAtIndex:0] URL];
}
}
}
#endif
return result;
}
#pragma mark Deprecated
+ (NSArray *)webLocationPasteboardTypes
{
return [NSArray arrayWithObjects:
@"WebURLsWithTitlesPboardType",
@"BookmarkDictionaryListPboardType",
kUTTypeURL, // contains the target URL when dragging webloc
NSFilenamesPboardType,
NSURLPboardType,
NSStringPboardType,
NSRTFPboardType,
NSRTFDPboardType, nil];
}
/* Retrieve URLs and their titles from the pasteboard for the "BookmarkDictionaryListPboardType" type
*/
+ (NSArray *)webLocationsWithBookmarkDictionariesPasteboardPropertyList:(id)propertyList;
{
NSArray *result = nil;
NSArray *arrayFromData = propertyList;
if (arrayFromData && [arrayFromData isKindOfClass:[NSArray class]] && [arrayFromData count] > 0)
{
NSDictionary *objectInfo = [arrayFromData objectAtIndex:0];
if ([objectInfo isKindOfClass:[NSDictionary class]])
{
NSString *URLString = [objectInfo objectForKey:@"URLString"];
NSURL *URL = [KSURLFormatter URLFromString:URLString]; /// encodeLegally to handle accented characters
if (URL)
{
NSString *title = [[objectInfo objectForKey:@"URIDictionary"] objectForKey:@"title"];
if (!title) title = [[self class] guessTitleForURL:URL];
KSWebLocation *webLoc = [[KSWebLocation alloc] initWithURL:URL title:title];
result = [NSArray arrayWithObject:webLoc];
[webLoc release];
}
}
}
return result;
}
/* Retrieve URLs and their titles from the pasteboard for the "WebURLsWithTitlesPboardType" type
* /// Rewritten 1/5/07 to account for being passed a nil URL
*/
+ (NSArray *)webLocationsWithWebURLsWithTitlesPasteboardPropertyList:(id)propertyList
{
NSMutableArray *result = nil;
// Bail if we haven't been handed decent data
NSArray *rawDataArray = propertyList;
if (rawDataArray && [rawDataArray isKindOfClass:[NSArray class]] && [rawDataArray count] >= 2)
{
// Get the array of URLs and their titles
NSArray *URLStrings = [rawDataArray objectAtIndex:0];
NSArray *URLTitles = [rawDataArray objectAtIndex:1];
// Run through each URL
result = [NSMutableArray arrayWithCapacity:[URLStrings count]];
NSInteger i;
for (i=0; i<[URLStrings count]; i++)
{
// Convert the string to a proper URL. If actually valid, add it & title to the results
NSString *URLString = [URLStrings objectAtIndex:i];
NSURL *URL = [KSURLFormatter URLFromString:URLString]; /// encodeLegally to handle accented characters
if (URL)
{
KSWebLocation *aWebLocation = [[KSWebLocation alloc] initWithURL:URL title:[URLTitles objectAtIndex:i]];
[result addObject:aWebLocation];
[aWebLocation release];
}
}
}
return result;
}
+ (NSArray *)webLocationsWithFilenamesPasteboardPropertyList:(id)propertyList;
{
NSArray *filenames = propertyList;
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[filenames count]];
for (NSString *aFilename in filenames)
{
NSURL *URL = [NSURL fileURLWithPath:aFilename];
KSWebLocation *aLocation = [[KSWebLocation alloc] initWithURL:URL title:[[self class] guessTitleForURL:URL]];
[result addObject:aLocation];
[aLocation release];
}
return result;
}
@end
#pragma mark -
@implementation NSPasteboard (KSWebLocation)
- (NSArray *)readWebLocations;
{
NSArray *result = nil;
// Get the URLs and titles from the best type available on the pasteboard.
NSString *type = [self availableTypeFromArray:[KSWebLocation webLocationPasteboardTypes]];
// Ideally we want to read multiple kUTTypeURLs, but this requires 10.6 or dropping down to CorePasteboard. So for now, fake it by falling back to NSFilenamesPboardType for file URLs (when possible)
if ([type isEqualToString:(NSString *)kUTTypeFileURL])
{
type = [self availableTypeFromArray:[NSArray arrayWithObjects:
NSFilenamesPboardType,
type,
nil]];
}
if ([type isEqualToString:@"BookmarkDictionaryListPboardType"])
{
result = [KSWebLocation webLocationsWithBookmarkDictionariesPasteboardPropertyList:
[self propertyListForType:type]];
}
else if ([type isEqualToString:@"WebURLsWithTitlesPboardType"])
{
result = [KSWebLocation webLocationsWithWebURLsWithTitlesPasteboardPropertyList:
[self propertyListForType:type]];
}
else if ([type isEqualToString:NSFilenamesPboardType])
{
result = [KSWebLocation webLocationsWithFilenamesPasteboardPropertyList:
[self propertyListForType:type]];
}
else
{
KSWebLocation *webloc = [KSWebLocation webLocationFromPasteboard:self];
if (webloc) result = [NSArray arrayWithObject:webloc];
}
return result;
}
@end