Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 286 lines (232 sloc) 9.72 kB
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
1 //
2 // KSWebLocationPasteboardUtilities.m
3 //
863123f @danwood update copyrights
danwood authored
4 // Copyright (c) 2007-2011, Mike Abdullah, Dan Wood and Karelia Software
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL MIKE ABDULLAH, DAN WOOD OR KARELIA SOFTWARE BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28 #import "KSWebLocationPasteboardUtilities.h"
29
1eb3fb9 @mikeabdullah Can use +[KSURLFormatter URLFromString:] instead of internal +URLWith…
mikeabdullah authored
30 #import "KSURLFormatter.h"
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
31 #import "KSURLUtilities.h"
32
33
34 @interface KSWebLocation (PasteboardPrivate)
35 + (NSArray *)webLocationsWithBookmarkDictionariesPasteboardPropertyList:(id)propertyList;
36 + (NSArray *)webLocationsWithWebURLsWithTitlesPasteboardPropertyList:(id)propertyList;
37 + (NSArray *)webLocationsWithFilenamesPasteboardPropertyList:(id)propertyList;
38 @end
39
40
41 #pragma mark -
42
43
44 @implementation KSWebLocation (Pasteboard)
45
46 #pragma mark Pasteboard Reading
47
48 + (NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard;
49 {
50 NSArray *URLTypes = nil;
51 if ([NSURL respondsToSelector:@selector(readableTypesForPasteboard:)])
52 {
53 URLTypes = [NSURL performSelector:@selector(readableTypesForPasteboard:)
54 withObject:pasteboard];
55 }
56
57 NSArray *result = [NSArray arrayWithObjects:
58 NSURLPboardType,
59 NSStringPboardType,
60 NSRTFPboardType,
61 NSRTFDPboardType, nil];
62
63 if (URLTypes) result = [URLTypes arrayByAddingObjectsFromArray:result];
64 return result;
65 }
66
67 #if (defined MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
68 + (NSUInteger)readingOptionsForType:(NSString *)type
69 pasteboard:(NSPasteboard *)pasteboard;
70 {
71 NSPasteboardReadingOptions result = NSPasteboardReadingAsString;
72
73 if ([type isEqualToString:@"WebURLsWithTitlesPboardType"] ||
74 [type isEqualToString:@"BookmarkDictionaryListPboardType"] ||
75 [type isEqualToString:NSFilenamesPboardType])
76 {
77 result = NSPasteboardReadingAsPropertyList;
78 }
79
80 return result;
81 }
82 #endif
83
d773175 @mikeabdullah Routine to guess title for a URL
mikeabdullah authored
84 + (NSString *)guessTitleForURL:(NSURL *)URL;
85 {
86 NSString *result = [[URL ks_lastPathComponent] stringByDeletingPathExtension];
87 result = [result stringByReplacingOccurrencesOfString:@"_" withString:@" "];
88 return result;
89 }
90
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
91 - (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type;
92 {
93 // Try with NSURL
94 NSURL *URL = [[NSURL alloc] initWithPasteboardPropertyList:propertyList ofType:type];
95 if (URL)
96 {
90e262e @mikeabdullah Guess titles when reading from web locations from pboard URLs
mikeabdullah authored
97 self = [self initWithURL:URL title:[[self class] guessTitleForURL:URL]];
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
98 [URL release];
99 }
100
101 // Fallback to trying to get a string out of the plist
102 else
103 {
104 NSString *string = [propertyList description];
105 if ([string length] <= 2048) // No point processing particularly long strings
106 {
ee98d74 @danwood Rename local variables so they don't cover earlier
danwood authored
107 NSURL *plistURL = [KSURLFormatter URLFromString:string]; /// encodeLegally to handle accented characters
108 if (plistURL && [plistURL ks_hasNetworkLocation])
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
109 {
90e262e @mikeabdullah Guess titles when reading from web locations from pboard URLs
mikeabdullah authored
110 self = [self initWithURL:plistURL title:[[self class] guessTitleForURL:URL]];
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
111 }
112 else
113 {
114 [self release]; self = nil;
115 }
116 }
117 }
118
119 return self;
120 }
121
6e2e79d @mikeabdullah +webLocationFromPasteboard: doesn't need type argument.
mikeabdullah authored
122 + (KSWebLocation *)webLocationFromPasteboard:(NSPasteboard *)pasteboard;
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
123 {
124 KSWebLocation *result = nil;
125
af949f5 Patch up reading of multiple files from Finder.
Mike authored
126 NSURL *URL = [WebView URLFromPasteboard:pasteboard];
127 if (URL)
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
128 {
af949f5 Patch up reading of multiple files from Finder.
Mike authored
129 result = [KSWebLocation webLocationWithURL:URL
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
130 title:[WebView URLTitleFromPasteboard:pasteboard]];
131 }
132
133 return result;
134 }
135
136 #pragma mark Deprecated
137
138 + (NSArray *)webLocationPasteboardTypes
139 {
140 return [NSArray arrayWithObjects:
141 @"WebURLsWithTitlesPboardType",
142 @"BookmarkDictionaryListPboardType",
143 kUTTypeURL, // contains the target URL when dragging webloc
144 NSFilenamesPboardType,
145 NSURLPboardType,
146 NSStringPboardType,
147 NSRTFPboardType,
148 NSRTFDPboardType, nil];
149 }
150
151 /* Retrieve URLs and their titles from the pasteboard for the "BookmarkDictionaryListPboardType" type
152 */
153 + (NSArray *)webLocationsWithBookmarkDictionariesPasteboardPropertyList:(id)propertyList;
154 {
155 NSArray *result = nil;
156
157 NSArray *arrayFromData = propertyList;
158 if (arrayFromData && [arrayFromData isKindOfClass:[NSArray class]] && [arrayFromData count] > 0)
159 {
160 NSDictionary *objectInfo = [arrayFromData objectAtIndex:0];
161 if ([objectInfo isKindOfClass:[NSDictionary class]])
162 {
163 NSString *URLString = [objectInfo objectForKey:@"URLString"];
1eb3fb9 @mikeabdullah Can use +[KSURLFormatter URLFromString:] instead of internal +URLWith…
mikeabdullah authored
164 NSURL *URL = [KSURLFormatter URLFromString:URLString]; /// encodeLegally to handle accented characters
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
165
166 if (URL)
167 {
168 NSString *title = [[objectInfo objectForKey:@"URIDictionary"] objectForKey:@"title"];
90e262e @mikeabdullah Guess titles when reading from web locations from pboard URLs
mikeabdullah authored
169 if (!title) title = [[self class] guessTitleForURL:URL];
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
170
171 KSWebLocation *webLoc = [[KSWebLocation alloc] initWithURL:URL title:title];
172 result = [NSArray arrayWithObject:webLoc];
173 [webLoc release];
174 }
175 }
176 }
177
178 return result;
179 }
180
181 /* Retrieve URLs and their titles from the pasteboard for the "WebURLsWithTitlesPboardType" type
182 * /// Rewritten 1/5/07 to account for being passed a nil URL
183 */
184 + (NSArray *)webLocationsWithWebURLsWithTitlesPasteboardPropertyList:(id)propertyList
185 {
186 NSMutableArray *result = nil;
187
188 // Bail if we haven't been handed decent data
189 NSArray *rawDataArray = propertyList;
190 if (rawDataArray && [rawDataArray isKindOfClass:[NSArray class]] && [rawDataArray count] >= 2)
191 {
192 // Get the array of URLs and their titles
193 NSArray *URLStrings = [rawDataArray objectAtIndex:0];
194 NSArray *URLTitles = [rawDataArray objectAtIndex:1];
195
196
197 // Run through each URL
198 result = [NSMutableArray arrayWithCapacity:[URLStrings count]];
199
200 NSInteger i;
201 for (i=0; i<[URLStrings count]; i++)
202 {
203 // Convert the string to a proper URL. If actually valid, add it & title to the results
204 NSString *URLString = [URLStrings objectAtIndex:i];
1eb3fb9 @mikeabdullah Can use +[KSURLFormatter URLFromString:] instead of internal +URLWith…
mikeabdullah authored
205 NSURL *URL = [KSURLFormatter URLFromString:URLString]; /// encodeLegally to handle accented characters
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
206 if (URL)
207 {
208 KSWebLocation *aWebLocation = [[KSWebLocation alloc] initWithURL:URL title:[URLTitles objectAtIndex:i]];
209 [result addObject:aWebLocation];
210 [aWebLocation release];
211 }
212 }
213 }
214
215 return result;
216 }
217
218 + (NSArray *)webLocationsWithFilenamesPasteboardPropertyList:(id)propertyList;
219 {
220 NSArray *filenames = propertyList;
221 NSMutableArray *result = [NSMutableArray arrayWithCapacity:[filenames count]];
222
223 for (NSString *aFilename in filenames)
224 {
225 NSURL *URL = [NSURL fileURLWithPath:aFilename];
90e262e @mikeabdullah Guess titles when reading from web locations from pboard URLs
mikeabdullah authored
226 KSWebLocation *aLocation = [[KSWebLocation alloc] initWithURL:URL title:[[self class] guessTitleForURL:URL]];
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
227 [result addObject:aLocation];
228 [aLocation release];
229 }
230
231 return result;
232 }
233
234 @end
235
236
237 #pragma mark -
238
239
240 @implementation NSPasteboard (KSWebLocation)
241
242 - (NSArray *)readWebLocations;
243 {
244 NSArray *result = nil;
245
af949f5 Patch up reading of multiple files from Finder.
Mike authored
246 // Get the URLs and titles from the best type available on the pasteboard.
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
247 NSString *type = [self availableTypeFromArray:[KSWebLocation webLocationPasteboardTypes]];
248
af949f5 Patch up reading of multiple files from Finder.
Mike authored
249 // 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)
250 if ([type isEqualToString:(NSString *)kUTTypeFileURL])
251 {
15b3996 @mikeabdullah Apologies, NSARRAY() is a Karelia macro.
mikeabdullah authored
252 type = [self availableTypeFromArray:[NSArray arrayWithObjects:
253 NSFilenamesPboardType,
254 type,
255 nil]];
af949f5 Patch up reading of multiple files from Finder.
Mike authored
256 }
257
258
259 if ([type isEqualToString:@"BookmarkDictionaryListPboardType"])
260 {
261 result = [KSWebLocation webLocationsWithBookmarkDictionariesPasteboardPropertyList:
262 [self propertyListForType:type]];
263 }
264 else if ([type isEqualToString:@"WebURLsWithTitlesPboardType"])
265 {
266 result = [KSWebLocation webLocationsWithWebURLsWithTitlesPasteboardPropertyList:
267 [self propertyListForType:type]];
268 }
269 else if ([type isEqualToString:NSFilenamesPboardType])
270 {
271 result = [KSWebLocation webLocationsWithFilenamesPasteboardPropertyList:
272 [self propertyListForType:type]];
273 }
274 else
275 {
6e2e79d @mikeabdullah +webLocationFromPasteboard: doesn't need type argument.
mikeabdullah authored
276 KSWebLocation *webloc = [KSWebLocation webLocationFromPasteboard:self];
af949f5 Patch up reading of multiple files from Finder.
Mike authored
277 if (webloc) result = [NSArray arrayWithObject:webloc];
278 }
279
1dd85bd @mikeabdullah Split KSWebLocation pasteboard handling out to a seperate file so you…
mikeabdullah authored
280
281 return result;
282 }
283
284 @end
285
Something went wrong with that request. Please try again.