-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController_Importing.m
306 lines (257 loc) · 13.4 KB
/
AppController_Importing.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
//
// AppController_Importing.m
// Notation
//
// Created by Zachary Schneirov on 1/14/11.
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
This file is part of Notational Velocity.
Notational Velocity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Notational Velocity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Notational Velocity. If not, see <http://www.gnu.org/licenses/>. */
#import "AppController_Importing.h"
#import "NotationController.h"
#import "NotationFileManager.h"
#import "BookmarksController.h"
#import "DualField.h"
#import "SyncSessionController.h"
#import "NotationSyncServiceManager.h"
#import "NotationDirectoryManager.h"
#import "AlienNoteImporter.h"
#import "NSString_NV.h"
#import <WebKit/WebArchive.h>
#import "GlobalPrefs.h"
#import "NSData_transformations.h"
#import "AttributedPlainText.h"
#import "NSCollection_utils.h"
#import "NoteObject.h"
#import "NotationPrefs.h"
@implementation AppController (Importing)
- (BOOL)addNotesFromPasteboard:(NSPasteboard*)pasteboard {
NSArray *types = [pasteboard types];
NSMutableAttributedString *newString = nil;
NSData *data = nil;
BOOL pbHasPlainText = [types containsObject:NSStringPboardType];
if ([types containsObject:NSFilenamesPboardType]) {
NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
if ([files isKindOfClass:[NSArray class]]) {
if ([notationController openFiles:files]) return YES;
}
}
NSString *sourceIdentifierString = nil;
//webkit URL!
if ([types containsObject:WebArchivePboardType]) {
sourceIdentifierString = [[pasteboard dataForType:WebArchivePboardType] pathURLFromWebArchive];
//gecko URL!
} else if ([types containsObject:[NSString customPasteboardTypeOfCode:0x4D5A0003]]) {
//lazilly use syntheticTitle to get first line, even though that's not how our API is documented
sourceIdentifierString = [[pasteboard stringForType:[NSString customPasteboardTypeOfCode:0x4D5A0003]] syntheticTitleAndTrimmedBody:NULL];
unichar nullChar = 0x0;
sourceIdentifierString = [sourceIdentifierString stringByReplacingOccurrencesOfString:
[NSString stringWithCharacters:&nullChar length:1] withString:@""];
}
if ([types containsObject:NSURLPboardType] || (pbHasPlainText && [[pasteboard stringForType:NSStringPboardType] superficiallyResemblesAnHTTPURL])) {
NSURL *url = [NSURL URLFromPasteboard:pasteboard];
if (!url) url = [NSURL URLWithString:[pasteboard stringForType:NSStringPboardType]];
NSString *potentialURLString = pbHasPlainText ? [pasteboard stringForType:NSStringPboardType] : nil;
if (potentialURLString && [[url absoluteString] isEqualToString:potentialURLString]) {
//only begin downloading if we know that there's no other useful string data
//because we've already checked NSFilenamesPboardType
if ([[url scheme] caseInsensitiveCompare:@"http"] == NSOrderedSame ||
[[url scheme] caseInsensitiveCompare:@"https"] == NSOrderedSame ||
[[url scheme] caseInsensitiveCompare:@"ftp"] == NSOrderedSame) {
NSString *linkTitleType = [NSString customPasteboardTypeOfCode:0x75726C6E];
NSString *linkTitle = [types containsObject:linkTitleType] ? [[pasteboard stringForType:linkTitleType] syntheticTitleAndTrimmedBody:NULL] : nil;
if (!linkTitle) {
//try urld instead of urln
linkTitleType = [NSString customPasteboardTypeOfCode:0x75726C64];
linkTitle = [types containsObject:linkTitleType] ? [[pasteboard stringForType:linkTitleType] syntheticTitleAndTrimmedBody:NULL] : nil;
}
[[[[AlienNoteImporter alloc] init] autorelease] importURLInBackground:url linkTitle:linkTitle receptionDelegate:self];
return YES;
}
}
}
//safari on 10.5 does not seem to provide a plain-text equivalent, so we must be able to dumb-down RTF data as well
//should fall-back to plain text if 1) user doesn't want styles and 2) plain text is actually available
BOOL shallUsePlainTextFallback = pbHasPlainText && ![prefsController pastePreservesStyle];
BOOL hasRTFData = NO;
if ([types containsObject:NVPTFPboardType]) {
if ((data = [pasteboard dataForType:NVPTFPboardType]))
newString = [[NSMutableAttributedString alloc] initWithRTF:data documentAttributes:NULL];
} else if ([types containsObject:NSRTFPboardType] && !shallUsePlainTextFallback) {
if ((data = [pasteboard dataForType:NSRTFPboardType]))
newString = [[NSMutableAttributedString alloc] initWithRTF:data documentAttributes:NULL];
hasRTFData = YES;
} else if ([types containsObject:NSRTFDPboardType] && !shallUsePlainTextFallback) {
if ((data = [pasteboard dataForType:NSRTFDPboardType]))
newString = [[NSMutableAttributedString alloc] initWithRTFD:data documentAttributes:NULL];
hasRTFData = YES;
} else if ([types containsObject:WebArchivePboardType] && !shallUsePlainTextFallback) {
if ((data = [pasteboard dataForType:WebArchivePboardType])) {
//set a timeout because -[NSHTMLReader _loadUsingWebKit] can sometimes hang
newString = [[NSMutableAttributedString alloc] initWithData:data options:[NSDictionary optionsDictionaryWithTimeout:10.0]
documentAttributes:NULL error:NULL];
}
hasRTFData = YES;
} else if ([types containsObject:NSHTMLPboardType] && !shallUsePlainTextFallback) {
if ((data = [pasteboard dataForType:NSHTMLPboardType]))
newString = [[NSMutableAttributedString alloc] initWithHTML:data documentAttributes:NULL];
hasRTFData = YES;
} else if (pbHasPlainText) {
NSString *pboardString = [pasteboard stringForType:NSStringPboardType];
if (pboardString) newString = [[NSMutableAttributedString alloc] initWithString:pboardString];
}
[newString autorelease];
if ([newString length] > 0) {
[newString removeAttachments];
if (hasRTFData && ![prefsController pastePreservesStyle]) //fallback scenario
newString = [[[NSMutableAttributedString alloc] initWithString:[newString string]] autorelease];
NSUInteger bodyLoc = 0, prefixedSourceLength = 0;
NSString *noteTitle = [[newString string] syntheticTitleAndSeparatorWithContext:NULL bodyLoc:&bodyLoc maxTitleLen:36];
if ([sourceIdentifierString length] > 0) {
//add the URL or wherever it was that this piece of text came from
prefixedSourceLength = [[newString prefixWithSourceString:sourceIdentifierString] length];
}
[newString santizeForeignStylesForImporting];
NoteObject *note = [[[NoteObject alloc] initWithNoteBody:newString title:noteTitle delegate:notationController
format:[notationController currentNoteStorageFormat] labels:nil] autorelease];
if (bodyLoc > 0 && [newString length] >= bodyLoc + prefixedSourceLength) [note setSelectedRange:NSMakeRange(prefixedSourceLength, bodyLoc)];
[notationController addNewNote:note];
return note != nil;
}
return NO;
}
- (BOOL)interpretNVURL:(NSURL*)aURL {
// currently supported:
// hostname -> command
// first level -> search term / title
// second level -> sync keys as parameters
// example: nv://find/url%20test/?SN=agtzaW1wbGUtbm90ZXINCxIETm90ZRiY-dEFDA&NV=5WJ0eP3YRaCjyQn%2F8p62iQ%3D%3D
NSUInteger j, i = 0;
if ([[aURL host] isEqualToString:@"find"]) {
//dispatch searchForString: and revealNote:options: as appropriate
//add currentNote to the snapback button back-stack
if (currentNote) {
[field pushFollowedLink:[[[NoteBookmark alloc] initWithNoteObject:currentNote searchString:[self fieldSearchString]] autorelease]];
}
NSString *terms = [aURL path];
[self searchForString:([terms length] && [terms characterAtIndex:0] == '/') ? [terms substringFromIndex:1] : terms];
NSArray *params = [[aURL query] componentsSeparatedByString:@"&"];
NSArray *svcs = [[SyncSessionController class] allServiceNames];
NoteObject *foundNote = nil;
for (i=0; i<[params count]; i++) {
NSString *idStr = [params objectAtIndex:i];
if ([idStr hasPrefix:@"NV="] && [idStr length] > 3) {
NSData *uuidData = [[[idStr substringFromIndex:3] stringByReplacingPercentEscapes] decodeBase64WithNewlines:NO];
if ((foundNote = [notationController noteForUUIDBytes:(CFUUIDBytes*)[uuidData bytes]]))
goto handleFound;
}
for (j=0; j<[svcs count]; j++) {
NSString *serviceName = [svcs objectAtIndex:j];
if ([idStr hasPrefix:[NSString stringWithFormat:@"%@=", serviceName]] && [idStr length] > [serviceName length] + 1) {
//lookup note with identical key for this service
NSString *key = [[idStr substringFromIndex:[serviceName length] + 1] stringByReplacingPercentEscapes];
if ((foundNote = [notationController noteForKey:key ofServiceClass:[[SyncSessionController allServiceClasses] objectAtIndex:j]]))
goto handleFound;
}
}
}
handleFound:
//if this search had initiated a clearing of the history, then make sure it doesn't happen
[NSObject cancelPreviousPerformRequestsWithTarget:field selector:@selector(clearFollowedLinks) object:nil];
if (foundNote) [self revealNote:foundNote options:NVOrderFrontWindow];
return YES;
} else if ([[aURL host] isEqualToString:@"make"]) {
NSArray *params = [[aURL query] componentsSeparatedByString:@"&"];
//parameters: "title" and one of the following for the body: "txt", "html" (maybe "md" for markdown in the future)
//if title is missing, add the body via -[addNotesFromPasteboard:]
NSString *title = nil, *txtBody = nil, *htmlBody = nil, *tags = nil;
for (i=0; i<[params count]; i++) {
NSString *compStr = [params objectAtIndex:i];
if ([compStr hasPrefix:@"title="] && [compStr length] > 6) {
title = [[compStr substringFromIndex:6] stringByReplacingPercentEscapes];
} else if ([compStr hasPrefix:@"txt="] && [compStr length] > 4) {
txtBody = [[compStr substringFromIndex:4] stringByReplacingPercentEscapes];
} else if ([compStr hasPrefix:@"html="] && [compStr length] > 5) {
htmlBody = [[compStr substringFromIndex:5] stringByReplacingPercentEscapes];
} else if ([compStr hasPrefix:@"tags="] && [compStr length] > 5) {
tags = [[compStr substringFromIndex:5] stringByReplacingPercentEscapes];
}
}
if (title && (txtBody || htmlBody)) {
NSMutableAttributedString *attributedContents = nil;
if (htmlBody) {
attributedContents = [[NSMutableAttributedString alloc] initWithHTML:[htmlBody dataUsingEncoding:NSUTF8StringEncoding]
options:[NSDictionary optionsDictionaryWithTimeout:10.0] documentAttributes:NULL];
} else {
attributedContents = [[NSMutableAttributedString alloc] initWithString:txtBody attributes:[prefsController noteBodyAttributes]];
}
[attributedContents removeAttachments];
[attributedContents santizeForeignStylesForImporting];
NoteObject *note = [[[NoteObject alloc] initWithNoteBody:[attributedContents autorelease] title:title delegate:notationController
format:[notationController currentNoteStorageFormat] labels:tags] autorelease];
[notationController addNewNote:note];
return YES;
} else if (txtBody || htmlBody) {
NSPasteboard *pboard = [NSPasteboard pasteboardWithUniqueName];
NSData *data = [htmlBody dataUsingEncoding:NSUTF8StringEncoding];
[pboard declareTypes:[NSArray arrayWithObject: data ? NSHTMLPboardType : NSStringPboardType] owner:nil];
if (data) {
[pboard setData:data forType:NSHTMLPboardType];
} else if (txtBody) {
[pboard setString:txtBody forType:NSStringPboardType];
} else {
NSLog(@"no txt or html to add to pboard");
return NO;
}
return [self addNotesFromPasteboard:pboard];
}
} else if ([[aURL host] length]) {
//assume find by default
if (currentNote) {
[field pushFollowedLink:[[[NoteBookmark alloc] initWithNoteObject:currentNote searchString:[self fieldSearchString]] autorelease]];
}
[self searchForString:[aURL host]];
return YES;
}
return NO;
}
- (NSString*)stringWithNoteURLsOnPasteboard:(NSPasteboard*)pboard {
//paste as a file:// URL, so that it can be linked
NSMutableString *allURLsString = [NSMutableString string];
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
if ([files isKindOfClass:[NSArray class]]) {
NSArray *unknownPaths = files;
NSUInteger i;
if ([notationController currentNoteStorageFormat] != SingleDatabaseFormat) {
//notes are stored as separate files, so if these paths are in the notes folder then NV can create double-bracketed-links to them instead
NSSet *existingNotes = [notationController notesWithFilenames:files unknownFiles:&unknownPaths];
if ([existingNotes count]) {
//create double-bracketed links using these notes' titles
NSArray *existingArray = [existingNotes allObjects];
for (i=0; i<[existingArray count]; i++) {
[allURLsString appendFormat:@"[[%@]]%s", titleOfNote([existingArray objectAtIndex:i]),
(i < [existingArray count] - 1) || [unknownPaths count] ? "\n" : ""];
}
}
}
//NSLog(@"paths not found in DB: %@", unknownPaths);
for (i=0; i<[unknownPaths count]; i++) {
NSURL *url = [NSURL fileURLWithPath:[unknownPaths objectAtIndex:i]];
if (url) {
[allURLsString appendFormat:@"<%@>%s",
[[url absoluteString] stringByReplacingOccurrencesOfString:@"file://localhost" withString:@"file://"],
(i < [unknownPaths count] - 1) ? "\n" : ""];
}
}
}
return allURLsString;
}
@end