-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExternalEditorListController.m
442 lines (351 loc) · 15.4 KB
/
ExternalEditorListController.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// ExternalEditorListController.m
// Notation
//
// Created by Zachary Schneirov on 3/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 "ExternalEditorListController.h"
#import "NoteObject.h"
#import "NotationController.h"
#import "NotationPrefs.h"
#import "NSBezierPath_NV.h"
static NSString *UserEEIdentifiersKey = @"UserEEIdentifiers";
static NSString *DefaultEEIdentifierKey = @"DefaultEEIdentifier";
NSString *ExternalEditorsChangedNotification = @"ExternalEditorsChanged";
@implementation ExternalEditor
- (id)initWithBundleID:(NSString*)aBundleIdentifier resolvedURL:(NSURL*)aURL {
if ([self init]) {
bundleIdentifier = [aBundleIdentifier retain];
resolvedURL = [aURL retain];
NSAssert(resolvedURL || bundleIdentifier, @"the bundle identifier and URL cannot both be nil");
if (!bundleIdentifier) {
if (!(bundleIdentifier = [[[NSBundle bundleWithPath:[aURL path]] bundleIdentifier] copy])) {
NSLog(@"initWithBundleID:resolvedURL: URL does not seem to point to a valid bundle");
return nil;
}
}
}
return self;
}
- (BOOL)canEditNoteDirectly:(NoteObject*)aNote {
NSAssert(aNote != nil, @"aNote is nil");
//for determining whether this potentially non-ODB-editor can open a non-plain-text file
//process: does pathExtension key exist in knownPathExtensions dict?
//if not, check this path extension w/ launch services
//then add a corresponding YES/NO NSNumber value to the knownPathExtensions dict
//but first, this editor can't handle any path if it's not actually installed
if (![self isInstalled]) return NO;
//and if this note isn't actually stored in a separate file, then obviously it can't be opened directly
if ([[aNote delegate] currentNoteStorageFormat] == SingleDatabaseFormat) return NO;
//and if aNote is in plaintext format and this editor is ODB-capable, then it should also be a general-purpose texteditor
//conversely ODB editors should never be allowed to open non-plain-text documents; for some reason LSCanURLAcceptURL claims they can do that
//one exception known: writeroom can edit rich-text documents
if ([self isODBEditor] && ![bundleIdentifier hasPrefix:@"com.hogbaysoftware.WriteRoom"]) {
return storageFormatOfNote(aNote) == PlainTextFormat;
}
if (!knownPathExtensions) knownPathExtensions = [NSMutableDictionary new];
NSString *extension = [[filenameOfNote(aNote) pathExtension] lowercaseString];
NSNumber *canHandleNumber = [knownPathExtensions objectForKey:extension];
if (!canHandleNumber) {
NSString *path = [aNote noteFilePath];
Boolean canAccept = false;
OSStatus err = LSCanURLAcceptURL((CFURLRef)[NSURL fileURLWithPath:path], (CFURLRef)[self resolvedURL], kLSRolesEditor, kLSAcceptAllowLoginUI, &canAccept);
if (noErr != err) {
NSLog(@"LSCanURLAcceptURL '%@' err: %d", path, err);
}
[knownPathExtensions setObject:[NSNumber numberWithBool:(BOOL)canAccept] forKey:extension];
return (BOOL)canAccept;
}
return [canHandleNumber boolValue];
}
- (BOOL)canEditAllNotes:(NSArray*)notes {
NSUInteger i = 0;
for (i=0; i<[notes count]; i++) {
if (![self isODBEditor] && ![self canEditNoteDirectly:[notes objectAtIndex:i]])
return NO;
}
return YES;
}
- (NSImage*)iconImage {
if (!iconImg) {
FSRef appRef;
if (CFURLGetFSRef((CFURLRef)[self resolvedURL], &appRef))
iconImg = [[NSImage smallIconForFSRef:&appRef] retain];
}
return iconImg;
}
- (NSString*)displayName {
if (!displayName) {
LSCopyDisplayNameForURL((CFURLRef)[self resolvedURL], (CFStringRef*)&displayName);
}
return displayName;
}
- (NSURL*)resolvedURL {
if (!resolvedURL && !installCheckFailed) {
OSStatus err = LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)bundleIdentifier, NULL, NULL, (CFURLRef*)&resolvedURL);
if (kLSApplicationNotFoundErr == err) {
installCheckFailed = YES;
} else if (noErr != err) {
NSLog(@"LSFindApplicationForInfo error for bundle identifier '%@': %d", bundleIdentifier, err);
}
}
return resolvedURL;
}
- (BOOL)isInstalled {
return [self resolvedURL] != nil;
}
- (BOOL)isODBEditor {
return [[ExternalEditorListController ODBAppIdentifiers] containsObject:bundleIdentifier];
}
- (NSString*)bundleIdentifier {
return bundleIdentifier;
}
- (NSString*)description {
return [bundleIdentifier stringByAppendingFormat:@" (URL: %@)", resolvedURL];
}
- (NSUInteger)hash {
return [bundleIdentifier hash];
}
- (BOOL)isEqual:(id)otherEntry {
return [[otherEntry bundleIdentifier] isEqualToString:bundleIdentifier];
}
- (NSComparisonResult)compareDisplayName:(ExternalEditor *)otherEd {
return [[self displayName] caseInsensitiveCompare:[otherEd displayName]];
}
- (void)dealloc {
[knownPathExtensions release];
[bundleIdentifier release];
[displayName release];
[resolvedURL release];
[iconImg release];
[super dealloc];
}
@end
@implementation ExternalEditorListController
static ExternalEditorListController* sharedInstance = nil;
+ (ExternalEditorListController*)sharedInstance {
if (sharedInstance == nil)
sharedInstance = [[ExternalEditorListController alloc] initWithUserDefaults];
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
return nil; // on subsequent allocation attempts return nil
}
- (id)initWithUserDefaults {
if ([self init]) {
//TextEdit is not an ODB editor, but can be used to open files directly
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObject:[NSArray arrayWithObject:@"com.apple.TextEdit"] forKey:UserEEIdentifiersKey]];
[self _initDefaults];
}
return self;
}
- (id)init {
if ([super init]) {
userEditorList = [[NSMutableArray alloc] init];
}
return self;
}
- (void)_initDefaults {
NSArray *userIdentifiers = [[NSUserDefaults standardUserDefaults] arrayForKey:UserEEIdentifiersKey];
NSUInteger i = 0;
for (i=0; i<[userIdentifiers count]; i++) {
ExternalEditor *ed = [[ExternalEditor alloc] initWithBundleID:[userIdentifiers objectAtIndex:i] resolvedURL:nil];
[userEditorList addObject:ed];
[ed release];
}
//initialize the default editor if one has not already been set or if the identifier was somehow lost from the list
if (![self editorIsMember:[self defaultExternalEditor]] || ![[self defaultExternalEditor] isInstalled]) {
if ([[self _installedODBEditors] count]) {
[self setDefaultEditor:[[self _installedODBEditors] lastObject]];
}
}
}
- (NSArray*)_installedODBEditors {
if (!_installedODBEditors) {
_installedODBEditors = [[NSMutableArray alloc] initWithCapacity:5];
NSArray *ODBApps = [[[self class] ODBAppIdentifiers] allObjects];
NSUInteger i = 0;
for (i=0; i<[ODBApps count]; i++) {
ExternalEditor *ed = [[ExternalEditor alloc] initWithBundleID:[ODBApps objectAtIndex:i] resolvedURL:nil];
if ([ed isInstalled]) {
[_installedODBEditors addObject:ed];
}
[ed release];
}
[_installedODBEditors sortUsingSelector:@selector(compareDisplayName:)];
}
return _installedODBEditors;
}
+ (NSSet*)ODBAppIdentifiers {
static NSSet *_ODBAppIdentifiers = nil;
if (!_ODBAppIdentifiers)
_ODBAppIdentifiers = [[NSSet alloc] initWithObjects:
@"de.codingmonkeys.SubEthaEdit", @"com.barebones.bbedit", @"com.barebones.textwrangler",
@"com.macromates.textmate", @"com.transtex.texeditplus", @"jp.co.artman21.JeditX", @"org.gnu.Aquamacs",
@"org.smultron.Smultron", @"com.peterborgapps.Smultron", @"org.fraise.Fraise", @"com.aynimac.CotEditor", @"com.macrabbit.cssedit",
@"com.talacia.Tag", @"org.skti.skEdit", @"com.cgerdes.ji", @"com.optima.PageSpinner", @"com.hogbaysoftware.WriteRoom",
@"com.hogbaysoftware.WriteRoom.mac", @"org.vim.MacVim", @"com.forgedit.ForgEdit", @"com.tacosw.TacoHTMLEdit", @"com.macrabbit.espresso", nil];
return _ODBAppIdentifiers;
}
- (void)addUserEditorFromDialog:(id)sender {
//always send menuChanged notification because this class is the target of its menus,
//so the notification is the only way to maintain a consistent selected item in PrefsWindowController
[self performSelector:@selector(menusChanged) withObject:nil afterDelay:0.0];
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setResolvesAliases:YES];
[openPanel setAllowsMultipleSelection:NO];
if ([openPanel runModalForDirectory:@"/Applications" file:nil types:[NSArray arrayWithObject:@"app"]] == NSOKButton) {
if (![openPanel filename]) goto errorReturn;
NSURL *appURL = [NSURL fileURLWithPath:[openPanel filename]];
if (!appURL) goto errorReturn;
ExternalEditor *ed = [[ExternalEditor alloc] initWithBundleID:nil resolvedURL:appURL];
if (!ed) goto errorReturn;
//check against lists of all known editors, installed or not
if (![self editorIsMember:ed]) {
[userEditorList addObject:ed];
[[NSUserDefaults standardUserDefaults] setObject:[self userEditorIdentifiers] forKey:UserEEIdentifiersKey];
}
[self setDefaultEditor:ed];
}
return;
errorReturn:
NSBeep();
NSLog(@"Unable to add external editor");
}
- (void)resetUserEditors:(id)sender {
[userEditorList removeAllObjects];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:UserEEIdentifiersKey];
[self _initDefaults];
[self menusChanged];
}
- (NSArray*)userEditorIdentifiers {
//for storing in nsuserdefaults
//extract bundle identifiers
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[userEditorList count]];
NSUInteger i = 0;
for (i=0; i<[userEditorList count]; i++) {
[array addObject:[[userEditorList objectAtIndex:i] bundleIdentifier]];
}
return array;
}
- (BOOL)editorIsMember:(ExternalEditor*)anEditor {
//does the editor exist in any of the lists?
return [userEditorList containsObject:anEditor] || [[ExternalEditorListController ODBAppIdentifiers] containsObject:[anEditor bundleIdentifier]];
}
- (NSMenu*)addEditorPrefsMenu {
if (!editorPrefsMenus) editorPrefsMenus = [NSMutableSet new];
NSMenu *aMenu = [[NSMenu alloc] initWithTitle:@"External Editors Menu"];
[aMenu setAutoenablesItems:NO];
[aMenu setDelegate:self];
[editorPrefsMenus addObject:[aMenu autorelease]];
[self _updateMenu:aMenu];
return aMenu;
}
- (NSMenu*)addEditNotesMenu {
if (!editNotesMenus) editNotesMenus = [NSMutableSet new];
NSMenu *aMenu = [[NSMenu alloc] initWithTitle:@"Edit Note Menu"];
[aMenu setAutoenablesItems:YES];
[aMenu setDelegate:self];
[editNotesMenus addObject:[aMenu autorelease]];
[self _updateMenu:aMenu];
return aMenu;
}
- (void)menusChanged {
[editNotesMenus makeObjectsPerformSelector:@selector(_updateMenuForEEListController:) withObject:self];
[editorPrefsMenus makeObjectsPerformSelector:@selector(_updateMenuForEEListController:) withObject:self];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:ExternalEditorsChangedNotification object:self]];
}
- (void)_updateMenu:(NSMenu*)theMenu {
//for allowing the user to configure external editors in the preferences window
if (IsSnowLeopardOrLater) {
[theMenu performSelector:@selector(removeAllItems)];
} else {
while ([theMenu numberOfItems])
[theMenu removeItemAtIndex:0];
}
BOOL isPrefsMenu = [editorPrefsMenus containsObject:theMenu];
BOOL didAddItem = NO;
NSMutableArray *editors = [NSMutableArray arrayWithArray:[self _installedODBEditors]];
[editors addObjectsFromArray:[userEditorList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"isInstalled == YES"]]];
[editors sortUsingSelector:@selector(compareDisplayName:)];
NSUInteger i = 0;
for (i=0; i<[editors count]; i++) {
ExternalEditor *ed = [editors objectAtIndex:i];
//change action SEL based on whether this is coming from Notes menu or preferences window
NSMenuItem *theMenuItem = isPrefsMenu ?
[[[NSMenuItem alloc] initWithTitle:[ed displayName] action:@selector(setDefaultEditor:) keyEquivalent:@""] autorelease] :
[[[NSMenuItem alloc] initWithTitle:[ed displayName] action:@selector(editNoteExternally:) keyEquivalent:@""] autorelease];
if (!isPrefsMenu && [[self defaultExternalEditor] isEqual:ed]) {
[theMenuItem setKeyEquivalent:@"E"];
[theMenuItem setKeyEquivalentModifierMask: NSCommandKeyMask | NSShiftKeyMask];
}
//PrefsWindowController maintains default-editor selection by updating on ExternalEditorsChangedNotification
[theMenuItem setTarget: isPrefsMenu ? self : [NSApp delegate]];
[theMenuItem setRepresentedObject:ed];
if ([ed iconImage])
[theMenuItem setImage:[ed iconImage]];
[theMenu addItem:theMenuItem];
didAddItem = YES;
}
if (!didAddItem) {
//disabled placeholder menu item; will probably not be displayed, but would be necessary for preferences list
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"(None)", @"description for no key combination") action:NULL keyEquivalent:@""] autorelease];
[theMenuItem setEnabled:NO];
[theMenu addItem:theMenuItem];
}
if ([userEditorList count] > 1 && isPrefsMenu) {
//if the user added at least one editor (in addition to the default TextEdit item), then allow items to be reset to their default
[theMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Reset", @"menu command to clear out custom external editors")
action:@selector(resetUserEditors:) keyEquivalent:@""] autorelease];
[theMenuItem setTarget:self];
[theMenu addItem:theMenuItem];
}
[theMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Other...", @"title of menu item for selecting a different notes folder")
action:@selector(addUserEditorFromDialog:) keyEquivalent:@""] autorelease];
[theMenuItem setTarget:self];
[theMenu addItem:theMenuItem];
}
- (ExternalEditor*)defaultExternalEditor {
if (!defaultEditor) {
NSString *defaultIdentifier = [[NSUserDefaults standardUserDefaults] stringForKey:DefaultEEIdentifierKey];
if (defaultIdentifier)
defaultEditor = [[ExternalEditor alloc] initWithBundleID:defaultIdentifier resolvedURL:nil];
}
return defaultEditor;
}
- (void)setDefaultEditor:(id)anEditor {
if ((anEditor = ([anEditor isKindOfClass:[NSMenuItem class]] ? [anEditor representedObject] : anEditor))) {
[defaultEditor release];
defaultEditor = [anEditor retain];
[[NSUserDefaults standardUserDefaults] setObject:[defaultEditor bundleIdentifier] forKey:DefaultEEIdentifierKey];
[self menusChanged];
}
}
@end
//this category exists because I want to use -makeObjectsPerformSelector: in -menusChanged
@interface NSMenu (ExternalEditorListMenu)
- (void)_updateMenuForEEListController:(ExternalEditorListController*)controller;
@end
@implementation NSMenu (ExternalEditorListMenu)
- (void)_updateMenuForEEListController:(ExternalEditorListController*)controller {
[controller _updateMenu:self];
}
@end