forked from quicksilver/Quicksilver
/
QSObject_Pasteboard.m
300 lines (270 loc) · 13.3 KB
/
QSObject_Pasteboard.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
#import "QSObject_Pasteboard.h"
#import "QSTypes.h"
#import "QSObject_FileHandling.h"
#import "QSObject_StringHandling.h"
NSString *QSPasteboardObjectIdentifier = @"QSObjectID";
NSString *QSPasteboardObjectAddress = @"QSObjectAddress";
#define QSPasteboardIgnoredTypes [NSArray arrayWithObjects:QSPasteboardObjectAddress, @"CorePasteboardFlavorType 0x4D555246", @"CorePasteboardFlavorType 0x54455854", nil]
id objectForPasteboardType(NSPasteboard *pasteboard, NSString *type) {
if ([PLISTTYPES containsObject:type])
return [pasteboard propertyListForType:type];
else if ([NSStringPboardType isEqualToString:type] || [type hasPrefix:@"QSObject"])
return [pasteboard stringForType:type];
else if ([NSURLPboardType isEqualToString:type])
return [[NSURL URLFromPasteboard:pasteboard] absoluteString];
else if ([NSColorPboardType isEqualToString:type])
return [NSKeyedArchiver archivedDataWithRootObject:[NSColor colorFromPasteboard:pasteboard]];
else if ([NSFileContentsPboardType isEqualToString:type]);
else
return [pasteboard dataForType:type];
return nil;
}
// writes the selected data to the general pasteboard
bool writeObjectToPasteboard(NSPasteboard *pasteboard, NSString *type, id data) {
if ([NSURLPboardType isEqualToString:type]) {
[pasteboard addTypes:[NSArray arrayWithObjects:NSURLPboardType, NSStringPboardType, nil] owner:nil];
[pasteboard setString:([data hasPrefix:@"mailto:"]) ?[data substringFromIndex:7] :data forType:NSStringPboardType];
[pasteboard setString:[data URLDecoding] forType:NSURLPboardType];
} else if ([PLISTTYPES containsObject:type] || [data isKindOfClass:[NSDictionary class]] || [data isKindOfClass:[NSArray class]])
[pasteboard setPropertyList:data forType:type];
else if ([data isKindOfClass:[NSString class]])
[pasteboard setString:data forType:type];
else if ([NSColorPboardType isEqualToString:type])
[data writeToPasteboard:pasteboard];
else if ([NSFileContentsPboardType isEqualToString:type]);
else
[pasteboard setData:data forType:type];
return YES;
}
@implementation QSObject (Pasteboard)
+ (id)objectWithPasteboard:(NSPasteboard *)pasteboard {
id theObject = nil;
if ([pasteboard isTransient] || [pasteboard isAutoGenerated])
return nil;
if ([[pasteboard types] containsObject:QSPasteboardObjectIdentifier])
theObject = [QSObject objectWithIdentifier:[pasteboard stringForType:QSPasteboardObjectIdentifier]];
if (!theObject && [[pasteboard types] containsObject:QSPasteboardObjectAddress]) {
NSArray *objectIdentifier = [[pasteboard stringForType:QSPasteboardObjectAddress] componentsSeparatedByString:@":"];
if ([[objectIdentifier objectAtIndex:0] intValue] == [[NSProcessInfo processInfo] processIdentifier]) {
QSObject *anObject = nil;
sscanf([[objectIdentifier lastObject] cStringUsingEncoding:NSUTF8StringEncoding], "%p", &anObject);
return anObject;
}
#ifdef DEBUG
else if (VERBOSE)
NSLog(@"Ignored old object: %@", objectIdentifier);
#endif
}
return [[[QSObject alloc] initWithPasteboard:pasteboard] autorelease];
}
- (id)initWithPasteboard:(NSPasteboard *)pasteboard {
return [self initWithPasteboard:pasteboard types:nil];
}
- (void)addContentsOfClipping:(NSString *)path { // Not thread safe?
NSPasteboard *pasteboard = [NSPasteboard pasteboardByFilteringClipping:path];
[self addContentsOfPasteboard:pasteboard types:nil];
[pasteboard releaseGlobally];
}
- (void)addContentsOfPasteboard:(NSPasteboard *)pasteboard types:(NSArray *)types {
NSMutableArray *typeArray = [NSMutableArray arrayWithCapacity:1];
for(NSString *thisType in (types?types:[pasteboard types])) {
if ([[pasteboard types] containsObject:thisType] && ![QSPasteboardIgnoredTypes containsObject:thisType]) {
id theObject = objectForPasteboardType(pasteboard, thisType);
if (theObject && thisType)
[self setObject:theObject forType:thisType];
else
NSLog(@"bad data for %@", thisType);
[typeArray addObject:[thisType decodedPasteboardType]];
}
}
// NSLog(@"data:%@", [self dataDictionary]);
/*
if (![[data allKeys] containsObject:NSURLPboardType]) {
NSURL *getURL = [[NSURL URLFromPasteboard:pasteboard] absoluteString];
if (getURL) {
[data setObject:getURL forType:NSURLPboardType];
NSLog(@"addingURL");
}
}
*/
}
- (id)initWithPasteboard:(NSPasteboard *)pasteboard types:(NSArray *)types {
if (self = [self init]) {
if (!types) types = [pasteboard types];
NSString *source = @"Clipboard";
if (pasteboard == [NSPasteboard generalPasteboard])
source = [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationBundleIdentifier"];
if ([source isEqualToString: @"com.microsoft.RDC"]) {
NSLog(@"Ignoring RDC Clipboard");
[self release];
return nil;
}
[self setDataDictionary:[NSMutableDictionary dictionaryWithCapacity:[[pasteboard types] count]]];
[self addContentsOfPasteboard:pasteboard types:types];
[self setObject:source forMeta:kQSObjectSource];
[self setObject:[NSDate date] forMeta:kQSObjectCreationDate];
id value;
if (value = [self objectForType:NSRTFPboardType]) {
value = [[NSAttributedString alloc] initWithRTF:value documentAttributes:nil];
[self setObject:[value string] forType:QSTextType];
[value release];
}
if ([self objectForType:QSTextType])
[self sniffString];
NSString *clippingPath = [self singleFilePath];
if (clippingPath) {
NSString *type = [[NSFileManager defaultManager] typeOfFile:clippingPath];
if ([clippingTypes containsObject:type])
[self addContentsOfClipping:clippingPath];
}
if ([self objectForType:kQSObjectPrimaryName])
[self setName:[self objectForType:kQSObjectPrimaryName]];
else {
[self setName:NSLocalizedString(@"Unknown Clipboard Object", @"Name for an unknown clipboard object")];
[self guessName];
}
[self loadIcon];
}
return self;
}
+ (id)objectWithClipping:(NSString *)clippingFile {
return [[[QSObject alloc] initWithClipping:clippingFile] autorelease];
}
- (id)initWithClipping:(NSString *)clippingFile {
NSPasteboard *pasteboard = [NSPasteboard pasteboardByFilteringClipping:clippingFile];
if (self = [self initWithPasteboard:pasteboard]) {
[self setLabel:[clippingFile lastPathComponent]];
}
[pasteboard releaseGlobally];
return self;
}
- (void)guessName {
if (itemForKey(NSFilenamesPboardType) ) {
[self setPrimaryType:NSFilenamesPboardType];
[self getNameFromFiles];
} else {
// Sometimes no dataForType:NSStringPboardType exists, so fall back to using the word "text" (avoids a crash)
NSString *textString = [itemForKey(NSStringPboardType) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (!textString) {
textString = NSLocalizedString(@"Text", @"Name of text object");
}
NSDictionary *namesAndKeys = [NSDictionary dictionaryWithObjectsAndKeys:
textString, NSStringPboardType,
NSLocalizedString(@"PDF Image", @"Name of PDF image "), NSPDFPboardType,
NSLocalizedString(@"Finder Icon", @"Name of icon file object"), [@"'icns'" encodedPasteboardType],
NSLocalizedString(@"PostScript Image", @"Name of PostScript image object"), NSPostScriptPboardType,
NSLocalizedString(@"TIFF Image", @"Name of TIFF image object"), NSTIFFPboardType,
NSLocalizedString(@"Color Data", @"Name of Color data object"), NSColorPboardType,
NSLocalizedString(@"File Contents", @"Name of File contents object"), NSFileContentsPboardType,
NSLocalizedString(@"Font Information", @"Name of Font information object"), NSFontPboardType,
NSLocalizedString(@"HTML Data", @"Name of HTML data object"), NSHTMLPboardType,
NSLocalizedString(@"Paragraph Formatting", @"Name of Paragraph Formatting object"), NSRulerPboardType,
NSLocalizedString(@"Tabular Text", @"Name of Tabular text object"), NSTabularTextPboardType,
NSLocalizedString(@"VCard Data", @"Name of VCard data object"), NSVCardPboardType,
NSLocalizedString(@"Promised Files", @"Name of Promised files object"), NSFilesPromisePboardType,
nil];
for (NSString *key in [namesAndKeys allKeys]) {
if (itemForKey(key) ) {
[self setName:[namesAndKeys objectForKey:key]];
break;
}
}
}
}
- (BOOL)putOnPasteboardAsPlainTextOnly:(NSPasteboard *)pboard {
NSArray *types = [NSArray arrayWithObject:NSStringPboardType];
[pboard declareTypes:types owner:nil];
NSString *string = [self stringValue];
[pboard setString:string forType:NSStringPboardType];
return YES;
}
// Declares the types that should be put on the pasteboard
- (BOOL)putOnPasteboard:(NSPasteboard *)pboard declareTypes:(NSArray *)types includeDataForTypes:(NSArray *)includeTypes {
if (!types) {
// get the different pboard types from the object's data dictionary -- they're all stored here
types = [[[[self dataDictionary] allKeys] mutableCopy] autorelease];
if ([types containsObject:QSProxyType])
[(NSMutableArray *)types addObjectsFromArray:[[(QSObject *)[self resolvedObject] dataDictionary] allKeys]];
}
else {
NSMutableSet *typeSet = [NSMutableSet setWithArray:types];
[typeSet intersectSet:[NSSet setWithArray:[[self dataDictionary] allKeys]]];
types = [[[typeSet allObjects] mutableCopy] autorelease];
}
// If there are no types for the object, we need to set one (using stringValue)
if (![types count]) {
[(NSMutableArray *)types addObject:NSStringPboardType];
[[self dataDictionary] setObject:[self stringValue] forKey:NSStringPboardType];
}
// define the types to be included on the pasteboard
if (!includeTypes) {
if ([types containsObject:NSFilenamesPboardType])
includeTypes = [NSArray arrayWithObject:NSFilenamesPboardType];
// [pboard declareTypes:includeTypes owner:self];
else if ([types containsObject:NSURLPboardType])
// for urls, define plain text, rtf and html
includeTypes = [NSArray arrayWithObjects:NSURLPboardType,NSHTMLPboardType,NSRTFPboardType,NSStringPboardType,nil];
else if ([types containsObject:NSColorPboardType])
includeTypes = [NSArray arrayWithObject:NSColorPboardType];
}
// last case: no other useful types: return a basic string
if (!includeTypes) {
includeTypes = [NSArray arrayWithObject:NSStringPboardType];
}
[pboard declareTypes:types owner:self];
/*
// ***warning ** Should add additional information for file items if ([paths count] == 1) {
[[self data] setObject:[[NSURL fileURLWithPath:[paths lastObject]]absoluteString] forKey:NSURLPboardType];
[[self data] setObject:[paths lastObject] forKey:NSStringPboardType];
}
*/
// NSLog(@"declareTypes: %@", [types componentsJoinedByString:@", "]);
// For URLs, create the RTF and HTML data to be stored in the clipboard
if ([types containsObject:NSURLPboardType]) {
// add the RTF and HTML types to the list of types
types = [types arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:NSHTMLPboardType,NSRTFPboardType,nil]];
// Create the HTML and RTF data
NSData *htmlData = [NSString dataForObject:self forType:NSHTMLPboardType];
NSData *rtfData = [NSString dataForObject:self forType:NSRTFPboardType];
// Add the HTML and RTF data to the object's data dictionary
[[self dataDictionary] setObject:htmlData forKey:NSHTMLPboardType];
[[self dataDictionary] setObject:rtfData forKey:NSRTFPboardType];
}
for (NSString *thisType in includeTypes) {
if ([types containsObject:thisType]) {
// NSLog(@"includedata, %@", thisType);
[self pasteboard:pboard provideDataForType:thisType];
}
}
if ([self identifier]) {
[pboard addTypes:[NSArray arrayWithObject:QSPasteboardObjectIdentifier] owner:self];
writeObjectToPasteboard(pboard, QSPasteboardObjectIdentifier, [self stringValue]);
}
[pboard addTypes:[NSArray arrayWithObject:QSPasteboardObjectAddress] owner:self];
// NSLog(@"types %@", [pboard types]);
return YES;
}
- (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type {
//if (VERBOSE) NSLog(@"Provide: %@", [type decodedPasteboardType]);
if ([type isEqualToString:QSPasteboardObjectAddress]) {
writeObjectToPasteboard(sender, type, [NSString stringWithFormat:@"%d:%p", [[NSProcessInfo processInfo] processIdentifier] , self]);
} else {
id theData = nil;
id handler = [self handlerForType:type selector:@selector(dataForObject:pasteboardType:)];
if (handler)
theData = [handler dataForObject:self pasteboardType:type];
if (!theData)
theData = [self objectForType:type];
if (theData) writeObjectToPasteboard(sender, type, theData);
}
}
- (void)pasteboardChangedOwner:(NSPasteboard *)sender {
//if (sender == [NSPasteboard generalPasteboard] && VERBOSE)
// NSLog(@"%@ Lost the Pasteboard: %@", self, sender);
}
- (NSData *)dataForType:(NSString *)dataType {
id theData = [data objectForKey:dataType];
if ([theData isKindOfClass:[NSData class]]) return theData;
return nil;
}
@end