-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFExtensionsManager.m
1295 lines (988 loc) · 39.4 KB
/
IFExtensionsManager.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// IFExtensionsManager.m
// Inform
//
// Created by Andrew Hunter on 06/03/2005.
// Copyright 2005 Andrew Hunter. All rights reserved.
//
#import "IFExtensionsManager.h"
#import "IFMaintenanceTask.h"
#import "IFTempObject.h"
NSString* IFExtensionsUpdatedNotification = @"IFExtensionsUpdatedNotification";
@implementation IFExtensionsManager
// = Shared extension managers =
+ (IFExtensionsManager*) sharedInform6ExtensionManager {
static IFExtensionsManager* mgr = nil;
if (!mgr) {
mgr = [[IFExtensionsManager alloc] init];
[mgr setSubdirectory: @"Inform 6 Extensions"];
[mgr setMergesMultipleExtensions: NO];
[mgr setExtensionsDefineName: NO];
}
return mgr;
}
+ (IFExtensionsManager*) sharedNaturalInformExtensionsManager {
static IFExtensionsManager* mgr = nil;
if (!mgr) {
mgr = [[IFExtensionsManager alloc] init];
[mgr setSubdirectory: @"Extensions"];
[mgr setMergesMultipleExtensions: YES];
[mgr setExtensionsDefineName: YES];
[mgr addExtensionDirectory: [[NSBundle mainBundle] pathForResource: @"Extensions"
ofType: @""
inDirectory: @"Inform7"]];
}
return mgr;
}
// = Initialisation =
- (id) init {
self = [super init];
if (self) {
extensionDirectories = [[NSMutableArray alloc] init];
customDirectories = [[NSMutableArray alloc] init];
subdirectory = [@"Extensions" retain];
tempExtensions = nil;
updateOutlineData = NO;
// Get the list of directories where extensions might live
NSArray* libraries = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSEnumerator* libEnum = [libraries objectEnumerator];
NSString* libDirectory;
while (libDirectory = [libEnum nextObject]) {
// FIXME: should really go in 'Application Data/Inform'
[extensionDirectories addObject: [libDirectory stringByAppendingPathComponent: @"Inform"]];
}
// We check for updates every time the application becomes active
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(updateTableData)
name: NSApplicationWillBecomeActiveNotification
object: nil];
}
return self;
}
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self];
[extensionDirectories release]; extensionDirectories = nil;
[customDirectories release]; customDirectories = nil;
[subdirectory release]; subdirectory = nil;
[extensionNames release]; extensionNames = nil;
[extensionContents release]; extensionContents = nil;
[outlineViewData release]; outlineViewData = nil;
[super dealloc];
}
// = Temporary objects =
- (void) tempObjectHasDeallocated: (NSObject*) obj {
if (obj == tempExtensions)
tempExtensions = nil;
else if (obj == tempAvailableExtensions)
tempAvailableExtensions = nil;
}
// = Setting up =
- (void) setExtensionDirectories: (NSArray*) directories {
// Directories must be an array of strings
[extensionDirectories release]; extensionDirectories = nil;
[customDirectories release]; customDirectories = [[NSMutableArray alloc] init];
extensionDirectories = [[NSMutableArray alloc] initWithArray: directories
copyItems: YES];
}
- (void) setExtensionsDefineName: (BOOL) defines {
extensionsDefineName = defines;
}
- (void) addExtensionDirectory: (NSString*) directory {
// customDirectories contains directories that do not have the subdirectory auto-appended
[customDirectories addObject: [[directory copy] autorelease]];
}
- (void) setSubdirectory: (NSString*) extensionSubdirectory {
[subdirectory release]; subdirectory = nil;
subdirectory = [extensionSubdirectory copy];
}
- (NSArray*) extensionDirectories {
return [[extensionDirectories copy] autorelease];
}
- (NSString*) subdirectory {
return [[subdirectory copy] autorelease];
}
- (void) setMergesMultipleExtensions: (BOOL) newMergeMultipleExtensions {
mergesMultipleExtensions = newMergeMultipleExtensions;
}
// = Retrieving the list of installed extensions =
- (NSArray*) directoriesToSearch {
NSFileManager* manager = [NSFileManager defaultManager];
// First, we work out the list of directories to search
NSMutableArray* directoriesToSearch = [NSMutableArray array];
NSEnumerator* extnEnum = [extensionDirectories objectEnumerator];
NSString* extnDir;
while (extnDir = [extnEnum nextObject]) {
// Append the subdirectory
NSString* dir = [extnDir stringByAppendingPathComponent: subdirectory];
// Only add to the list to search if it exists and it's a directory
BOOL exists;
BOOL isDir;
exists = [manager fileExistsAtPath: dir
isDirectory: &isDir];
if (exists && isDir) {
[directoriesToSearch addObject: dir];
}
}
// Append the custom directories
extnEnum = [customDirectories objectEnumerator];
while (extnDir = [extnEnum nextObject]) {
// Only add to the list to search if it exists and it's a directory
BOOL exists;
BOOL isDir;
exists = [manager fileExistsAtPath: extnDir
isDirectory: &isDir];
if (exists && isDir) {
[directoriesToSearch addObject: extnDir];
}
}
return directoriesToSearch;
}
- (NSDictionary*) extensionDictionary {
if (tempExtensions) return [[tempExtensions retain] autorelease];
NSFileManager* manager = [NSFileManager defaultManager];
// First, we work out the list of directories to search
NSArray* directoriesToSearch = [self directoriesToSearch];
// Retrieve the extensions that exist in the directory
NSMutableDictionary* resultSet = [NSMutableDictionary dictionary];
// We go through the directories backwards, as the directory added first has the highest priority
NSEnumerator* extnEnum = [directoriesToSearch reverseObjectEnumerator];
NSString* extnDir;
while (extnDir = [extnEnum nextObject]) {
NSArray* extnFiles = [manager directoryContentsAtPath: extnDir];
NSEnumerator* contentEnum = [extnFiles objectEnumerator];
NSString* filename;
while (filename = [contentEnum nextObject]) {
NSString* fullPath = [extnDir stringByAppendingPathComponent: filename];
// Must not be hidden from the finder
if ([fullPath characterAtIndex: 0] == '.') continue;
// Must exist and be a directory
BOOL exists;
BOOL isDir;
exists = [manager fileExistsAtPath: fullPath
isDirectory: &isDir];
// 'reserved' is a special case and never shows up in the list
if ([[filename lowercaseString] isEqualToString: @"reserved"]) exists = NO;
if (exists && isDir) {
// Add to the list of paths for this name
NSString* dirKey = [filename lowercaseString];
NSMutableArray* dirsWithName = [resultSet objectForKey: dirKey];
if (dirsWithName == nil) dirsWithName = [NSMutableArray array];
[dirsWithName addObject: fullPath];
// Add to the result
[resultSet setObject: dirsWithName
forKey: dirKey];
}
}
}
// Cache the results: this will ensure future calls to this function will be faster (at least until the
// autorelease pool is destroyed)
[[[IFTempObject alloc] initWithObject: tempExtensions=resultSet
delegate: self] autorelease];
// Return the result
return resultSet;
}
- (NSArray*) availableExtensions {
// Use the cached versions if they're around
if (tempAvailableExtensions) return [[tempAvailableExtensions retain] autorelease];
// Produce a list of extensions
// We use the 'last' (highest priority) extension name in the array of extensions as the 'actual' name of the extension
NSDictionary* extensionDictionary = [self extensionDictionary];
NSMutableArray* result = [NSMutableArray array];
NSEnumerator* extnEnum = [extensionDictionary keyEnumerator];
NSString* dirKey;
while (dirKey = [extnEnum nextObject]) {
NSArray* extnDetails = [extensionDictionary objectForKey: dirKey];
[result addObject: [[extnDetails objectAtIndex: [extnDetails count]-1] lastPathComponent]];
}
[result sortUsingSelector: @selector(caseInsensitiveCompare:)];
[[[IFTempObject alloc] initWithObject: tempAvailableExtensions=result
delegate: self]
autorelease];
return tempAvailableExtensions;
}
- (NSString*) pathForExtensionWithName: (NSString*) name {
return [[[self extensionDictionary] objectForKey: [name lowercaseString]] lastObject];
}
- (NSArray*) pathsForExtensionWithName: (NSString*) name {
return [[self extensionDictionary] objectForKey: [name lowercaseString]];
}
// = The list of files within a given extension (full paths) =
- (NSArray*) filesInExtensionWithName: (NSString*) name {
// Returns all the files in a particular extension (as full path names)
NSFileManager* manager = [NSFileManager defaultManager];
NSArray* pathsToSearch;
// Work out which paths to search (just one if we're not merging)
if (mergesMultipleExtensions) {
pathsToSearch = [self pathsForExtensionWithName: name];
} else {
pathsToSearch = [NSArray arrayWithObject: [[self pathsForExtensionWithName: name] lastObject]];
}
// Search all the paths to generate the list of files
// If a file with the same name exists in multiple places, then only the last one is used
NSMutableDictionary* resultDict = [NSMutableDictionary dictionary];
NSEnumerator* pathEnum = [pathsToSearch objectEnumerator];
NSString* path;
while (path = [pathEnum nextObject]) {
// Search this path
NSArray* files = [manager directoryContentsAtPath: path];
NSEnumerator* fileEnum = [files objectEnumerator];
NSString* file;
while (file = [fileEnum nextObject]) {
// Add to the result
[resultDict setObject: [path stringByAppendingPathComponent: file]
forKey: [file lowercaseString]];
}
}
NSMutableArray* result = [[resultDict allValues] mutableCopy];
[result sortUsingSelector: @selector(caseInsensitiveCompare:)];
return [result autorelease];
}
- (NSArray*) sourceFilesInExtensionWithName: (NSString*) name {
// Returns all the files that are probably source files in the extension with the given name
// (Also returns other files we can edit: .txt and .rtf files in particular)
NSFileManager* manager = [NSFileManager defaultManager];
// Use filesInExtensionWithName: to get all the files
NSArray* files = [self filesInExtensionWithName: name];
// Filter for valid source files
NSMutableArray* result = [NSMutableArray array];
NSEnumerator* fileEnum = [files objectEnumerator];
NSString* file;
BOOL isGrahamNelson = [[name lowercaseString] isEqualToString: @"graham nelson"];
while (file = [fileEnum nextObject]) {
// File must exist and not be a directory
BOOL exists, isDir;
exists = [manager fileExistsAtPath: file
isDirectory: &isDir];
if (!exists || isDir) continue;
// File must not begin with a '.'
if ([[file lastPathComponent] characterAtIndex: 0] == '.') continue;
// 'Standard Rules' in 'Graham Nelson' is a special case and is never returned (even though it's a source file, and will always be present in the natural inform extensions)
if (isGrahamNelson && [[[file lastPathComponent] lowercaseString] isEqualToString: @"standard rules"]) {
continue;
}
// File must have a suitable extension
NSString* extn = [[file pathExtension] lowercaseString];
if (extn == nil || [extn isEqualToString: @""] ||
[extn isEqualToString: @"i7x"] ||
[extn isEqualToString: @"txt"] || [extn isEqualToString: @"rtf"] ||
[extn isEqualToString: @"inf"] || [extn isEqualToString: @"h"] || [extn isEqualToString: @"i6"]) {
// Add to the result
[result addObject: file];
}
}
[result sortUsingSelector: @selector(caseInsensitiveCompare:)];
return result;
}
// = Editing the installed extensions =
- (void) createRecursiveDir: (NSString*) dir {
if (dir == nil || [dir isEqualToString: @""] || [dir isEqualToString: @"/"]) return;
BOOL exists, isDir;
exists = [[NSFileManager defaultManager] fileExistsAtPath: dir
isDirectory: &isDir];
if (exists) return;
[self createRecursiveDir: [dir stringByDeletingLastPathComponent]];
[[NSFileManager defaultManager] createDirectoryAtPath: dir
attributes: nil];
}
- (void) createDefaultExtnDir {
// Create the directory that will contain the extensions (if necessary)
NSString* directory = [[extensionDirectories objectAtIndex: 0] stringByAppendingPathComponent: subdirectory];
if (directory == nil) return;
// Do nothing if the directory already exists
BOOL exists, isDir;
exists = [[NSFileManager defaultManager] fileExistsAtPath: directory
isDirectory: &isDir];
if (exists) return;
// Otherwise, create the directory (and the hierarchy)
[self createRecursiveDir: directory];
}
- (NSString*) authorForNaturalInformExtension: (NSString*) file
title: (NSString**) title {
// Work out the author and title from a Natural Inform extension file
NSFileManager* mgr = [NSFileManager defaultManager];
if (title != nil) *title = nil;
// Can't do anything with a non-existant file
BOOL isDir;
BOOL exists = [mgr fileExistsAtPath: file
isDirectory: &isDir];
if (!exists || isDir) return nil;
// Read the first 1k of the extension
NSFileHandle* extensionFile = [NSFileHandle fileHandleForReadingAtPath: file];
NSData* extensionInitialData = [extensionFile readDataOfLength: 1024];
// Get up to the first newline
const unsigned char* bytes = [extensionInitialData bytes];
int x;
for (x=0; x<[extensionInitialData length] && bytes[x] != '\n' && bytes[x] != '\r'; x++);
// No newline means this is not an extension
if (x >= [extensionInitialData length]) return nil;
// Initial line should be "<foo> by <bar> begins here." or "Version <x> of <foo> by <bar> begins here."
NSString* extensionString = [[[NSString alloc] initWithBytes: bytes
length: x
encoding: NSUTF8StringEncoding]
autorelease];
if (extensionString == nil) return nil;
extensionString = [extensionString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
// Check that the ending is 'begins here'
if ([extensionString length] < [@" begins here." length]
|| ![[[extensionString substringFromIndex:
[extensionString length]-[@" begins here." length]] lowercaseString] isEqualToString: @" begins here."])
return nil;
// Get the indexes of ' by ', and ' of '
NSRange byPosition = [extensionString rangeOfString: @" by "
options: NSCaseInsensitiveSearch];
NSRange ofPosition = [extensionString rangeOfString: @" of "
options: NSCaseInsensitiveSearch];
NSRange versionPosition = [extensionString rangeOfString: @"version "
options: NSCaseInsensitiveSearch];
// Must be a ' by '
if (byPosition.location == NSNotFound) return nil;
if (ofPosition.location != NSNotFound && versionPosition.location == 0) {
// Must be version <x> of <foo> by ..., not version <x> by <foo> of ...
if (ofPosition.location > byPosition.location) return nil;
// Set ofPosition.location to be the start of the name
ofPosition.location += ofPosition.length;
} else {
// No 'of', or at least, no 'of' indicating a version
ofPosition.location = 0;
}
// Now we have enough information to work out the author and suggested extension name
NSString* titleName = [extensionString substringWithRange: NSMakeRange(ofPosition.location, byPosition.location-ofPosition.location)];
NSString* authorName = [extensionString substringWithRange: NSMakeRange(byPosition.location+byPosition.length, [extensionString length]-(byPosition.location+byPosition.length)-[@" begins here." length])];
authorName = [authorName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
titleName = [titleName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
if (title) *title = titleName;
return authorName;
}
- (BOOL) addExtension: (NSString*) extensionPath
finalPath: (NSString**) finalPath {
if (finalPath) *finalPath = nil;
// We can add directories (of all sorts, but with no subdirectories, and no larger than 1Mb total)
// We can also add .h, .inf and .i6 files on their own, creating a directory to do so
NSFileManager* mgr = [NSFileManager defaultManager];
extensionPath = [extensionPath stringByStandardizingPath];
[self createDefaultExtnDir];
// Check that we can add this file
BOOL exists, isDir;
exists = [mgr fileExistsAtPath: extensionPath
isDirectory: &isDir];
if (!exists) return NO; // Can't add something that does not exist
NSString* author = nil;
NSString* title = nil;
if (!isDir) {
NSString* extn = [[extensionPath pathExtension] lowercaseString];
if (extensionsDefineName) {
// Try to read out the author and title name
author = [self authorForNaturalInformExtension: extensionPath
title: &title];
if (author == nil || title == nil || [author length] <= 0 || [title length] <= 0) return NO;
} else if (!([extn isEqualToString: @"h"] || [extn isEqualToString: @"inf"] || [extn isEqualToString: @"i6"])) {
// File is probably not an Inform source file
return NO;
}
} else {
// The directory must not contain any subdirectories or be larger than 1Mb
NSDirectoryEnumerator* dirEnum = [mgr enumeratorAtPath: extensionPath];
int size = 0;
NSString* file;
while (file = [dirEnum nextObject]) {
NSString* path = [extensionPath stringByAppendingPathComponent: file];
BOOL fExists, fIsDir;
fExists = [mgr fileExistsAtPath: path
isDirectory: &fIsDir];
if (fExists && fIsDir) return NO; // Subdirectories are not allowed
size += [[[mgr fileAttributesAtPath: path
traverseLink: NO] objectForKey: NSFileSize]
intValue];
if (size > 1048576) return NO; // Not valid if >1Mb
}
}
// We should be able to add the file/directory
// Try to create the destination. First extensionDirectory should be the writable one
NSString* directory = [[extensionDirectories objectAtIndex: 0] stringByAppendingPathComponent: subdirectory];
NSString* destDir;
if (directory == nil) return NO;
if (isDir) {
destDir = [directory stringByAppendingPathComponent: [extensionPath lastPathComponent]];
} else {
if (author) {
destDir = [directory stringByAppendingPathComponent: author];
} else {
destDir = [directory stringByAppendingPathComponent: [[extensionPath lastPathComponent] stringByDeletingPathExtension]];
}
}
destDir = [destDir stringByStandardizingPath];
if ([[destDir lowercaseString] isEqualToString: [extensionPath lowercaseString]]) return NO; // Trying to re-add an extension that already exists
// If the old directory exists and we're not merging, then move the old directory to the trash
BOOL oldExists = [mgr fileExistsAtPath: destDir];
if (!mergesMultipleExtensions && oldExists) {
if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
source: [destDir stringByDeletingLastPathComponent]
destination: @""
files: [NSArray arrayWithObject: [destDir lastPathComponent]]
tag: 0]) {
// Failed to move to the trash
return NO;
}
oldExists = [mgr fileExistsAtPath: destDir];
if (oldExists) return NO; // Er, should probably never happen
}
// The list of extensions may have changed
[self updateTableData];
// If the directory does not exist, create it
if (!oldExists) {
if (![mgr createDirectoryAtPath: destDir
attributes: nil]) {
// Can't create the extension directory
return NO;
}
}
// Copy the files into the extension
if (isDir) {
NSDirectoryEnumerator* extnEnum = [mgr enumeratorAtPath: extensionPath];
if (finalPath) *finalPath = [[destDir copy] autorelease];
NSString* file;
while (file = [extnEnum nextObject]) {
NSString* path = [extensionPath stringByAppendingPathComponent: file];
BOOL exists = [mgr fileExistsAtPath: path];
// (Silently fail if we can't copy for some reason here)
if (exists) {
[mgr copyPath: path
toPath: [destDir stringByAppendingPathComponent: file]
handler: nil];
}
}
} else {
NSString* destFile;
if (title != nil)
destFile = title;
else
destFile = [extensionPath lastPathComponent];
if (extensionsDefineName && [destFile length] > 0 && [destFile characterAtIndex: [destFile length]-1] == ')') {
// The name of the extension may be followed by a proviso: remove it
int index;
for (index = [destFile length] - 1;
index >= 0 && [destFile characterAtIndex: index] != '(';
index--);
if (index > 1) {
if ([destFile characterAtIndex: index-1] == ' ') {
destFile = [destFile substringToIndex: index-1];
}
}
}
if (extensionsDefineName && [destFile length] > 31) {
// Extension filenames must be at most 31 characters
destFile = [destFile substringToIndex: 30];
}
if (extensionsDefineName) {
// Extension must have a .i7x extension
if (![[destFile pathExtension] isEqualToString: @"i7x"]) {
destFile = [destFile stringByAppendingPathExtension: @"i7x"];
}
}
NSString* dest = [destDir stringByAppendingPathComponent: destFile];
if (finalPath) *finalPath = [[dest copy] autorelease];
if ([mgr fileExistsAtPath: dest]) {
[mgr removeFileAtPath: dest
handler: nil];
}
if (![mgr copyPath: extensionPath
toPath: dest
handler: nil]) {
// Couldn't finish installing the extension
return NO;
}
}
// Success
[self updateTableData];
[[NSWorkspace sharedWorkspace] noteFileSystemChanged: [destDir stringByDeletingLastPathComponent]];
return YES;
}
- (BOOL) addFile: (NSString*) filePath
toExtension: (NSString*) extensionName {
// We can add .h, .inf, .i6 or extensionless files
NSFileManager* mgr = [NSFileManager defaultManager];
filePath = [filePath stringByStandardizingPath];
[self createDefaultExtnDir];
NSString* directory = [[extensionDirectories objectAtIndex: 0] stringByAppendingPathComponent: subdirectory];
// Check the extension
NSString* extn = [[filePath pathExtension] lowercaseString];
if (!(extn == nil || [extn isEqualToString: @""] || [extn isEqualToString: @"i7x"] || [extn isEqualToString: @"h"] || [extn isEqualToString: @"inf"] || [extn isEqualToString: @"i6"])) {
// Invalid file extension
return NO;
}
BOOL exists, isDir;
exists = [mgr fileExistsAtPath: filePath
isDirectory: &isDir];
if (!exists || isDir) return NO;
// Create the extension directory if necessary
NSString* extnDir = [directory stringByAppendingPathComponent: extensionName];
exists = [mgr fileExistsAtPath: extnDir
isDirectory: &isDir];
if (exists && !isDir) return NO; // Is not a directory
if (!exists) {
// Does not exist: attempt to create the extension directory
if (![mgr createDirectoryAtPath: extnDir
attributes: nil]) {
return NO;
}
[self updateTableData];
}
// Copy the extension into the directory
if (![mgr copyPath: filePath
toPath: [extnDir stringByAppendingPathComponent: [filePath lastPathComponent]]
handler: nil])
return NO;
[self updateTableData];
return YES;
}
- (BOOL) deleteExtension: (NSString*) extensionName {
// Get the location of the extension (as it would be if installed in the user's directory)
NSString* extn = [[[extensionDirectories objectAtIndex: 0] stringByAppendingPathComponent: subdirectory] stringByAppendingPathComponent: extensionName];
extn = [extn stringByStandardizingPath];
// Check that it exists and is a directory
BOOL exists, isDir;
exists = [[NSFileManager defaultManager] fileExistsAtPath: extn
isDirectory: &isDir];
if (!exists || !isDir) return NO;
// Try to move it to the trash
if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
source: [extn stringByDeletingLastPathComponent]
destination: @""
files: [NSArray arrayWithObject: [extn lastPathComponent]]
tag: 0]) {
return NO;
}
[self updateTableData];
// Success
return YES;
}
- (BOOL) deleteFile: (NSString*) file
inExtension: (NSString*) extensionName {
// Get the location of the extension file (as it would be if installed in the user's directory)
NSString* extn = [[[extensionDirectories objectAtIndex: 0] stringByAppendingPathComponent: subdirectory] stringByAppendingPathComponent: extensionName];
extn = [extn stringByStandardizingPath];
NSString* extnFile = [extn stringByAppendingPathComponent: file];
extnFile = [extnFile stringByStandardizingPath];
// Check that the extension file and the extension directory exist
BOOL exists, isDir;
exists = [[NSFileManager defaultManager] fileExistsAtPath: extn
isDirectory: &isDir];
if (!exists || !isDir) return NO;
exists = [[NSFileManager defaultManager] fileExistsAtPath: extnFile];
if (!exists) return NO;
// Try to move the file to trash
if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation
source: extn
destination: @""
files: [NSArray arrayWithObject: file]
tag: 0]) {
return NO;
}
[self updateTableData];
// Success
return YES;
}
// = Data source support functions =
static const float updateFrequency = 0.75; // Maximum frequency
//
// Occasionally we might need to update the data associated with the table or the outline view.
// We can't update immediately (as a data source, we have no means of informing the table of the
// update), so instead we use the runloop mechanism to delay the update until later.
//
// Change the 'updateFrequency' constant above to change how often updates occur.
//
- (void) updateTableData {
// Queues a request to update the table data, if one is not already pending
if (!updatingTableData) {
[self performSelector: @selector(reallyUpdateTableData)
withObject: nil
afterDelay: updateFrequency];
updatingTableData = YES;
}
}
static int compare_insensitive(id a, id b, void* context) {
return [a caseInsensitiveCompare: b];
}
- (void) reallyUpdateTableData: (BOOL) notify {
BOOL hasChanged = NO;
// Actually performs the update, and notifies if anything has changed
updatingTableData = NO;
NSArray* newTableData = [self availableExtensions];
newTableData = [newTableData sortedArrayUsingFunction: compare_insensitive
context: nil];
if (![newTableData isEqualToArray: extensionNames]) {
// Table data has updated
[extensionNames release]; extensionNames = nil;
extensionNames = [newTableData copy];
hasChanged = YES;
}
if (updateOutlineData) {
// Also update the data for the outline view
NSMutableDictionary* newContentData = [NSMutableDictionary dictionary];
NSEnumerator* extnEnum = [extensionNames objectEnumerator];
NSString* extn;
while (extn = [extnEnum nextObject]) {
NSArray* contents = [[self sourceFilesInExtensionWithName: extn] sortedArrayUsingFunction: compare_insensitive
context: nil];
if (contents != nil) {
[newContentData setObject: contents
forKey: extn];
}
}
if (![newContentData isEqualToDictionary: extensionContents]) {
[extensionContents release];
extensionContents = [newContentData retain];
hasChanged = YES;
}
}
if (hasChanged && updateOutlineData) {
// Build the data that forms the final outline view
// (Annoyingly, we have to keep the objects around, hence this seemingly pointless bit)
[outlineViewData release]; outlineViewData = nil;
outlineViewData = [[NSMutableArray alloc] init];
NSEnumerator* extnEnum = [extensionNames objectEnumerator];
NSString* extn;
while (extn = [extnEnum nextObject]) {
// Build the details for the contents of this extension
NSMutableArray* extnContents = [NSMutableArray array];
NSArray* contents = [extensionContents objectForKey: extn];
NSEnumerator* contentEnum = [contents objectEnumerator];
NSString* extnContent;
while (extnContent = [contentEnum nextObject]) {
// view items are arrays of type (type, name, data)
// type is 'Source' or 'Directory'
[extnContents addObject: [NSArray arrayWithObjects: @"Source", [extnContent lastPathComponent], extnContent, extn, nil]];
}
// Store this extension
// view items are arrays of type (type, name, data)
// type is 'Source' or 'Directory'
[outlineViewData addObject: [NSArray arrayWithObjects: @"Directory", extn, extnContents, nil]];
}
}
if (hasChanged && notify) {
// Tell anything that wants to know that the data has been updated
[[NSNotificationCenter defaultCenter] postNotificationName: IFExtensionsUpdatedNotification
object: self];
// Re-run the maintenance tasks
NSString* compilerPath = [[NSBundle mainBundle] pathForResource: @"ni"
ofType: @""
inDirectory: @"Compilers"];
if (compilerPath != nil) {
[[IFMaintenanceTask sharedMaintenanceTask] queueTask: compilerPath
withArguments: [NSArray arrayWithObjects:
@"-census",
@"-rules",
[[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"Inform7"] stringByAppendingPathComponent: @"Extensions"],
nil]];
}
}
}
- (void) reallyUpdateTableData {
[self reallyUpdateTableData: YES];
}
- (void) retrieveDataForItem: (id) item
extensionName: (NSString**) extension
fileName: (NSString**) filename {
if (!extensionContents) {
updateOutlineData = YES;
[self reallyUpdateTableData: NO];
}
// Clear out the items
if (extension) *extension = nil;
if (filename) *filename = nil;
if ([item isKindOfClass: [NSString class]]) {
// Will be a table view item (not sure of the circumstances these can arrive here, but deal with them anyway)
if (extension) *extension = item;
return;
} else if ([item isKindOfClass: [NSArray class]]) {
// Will be an outline view item
NSString* type = [item objectAtIndex: 0];
if (![type isKindOfClass: [NSString class]]) return; // Uh... Wasn't an item
if ([type isEqualToString: @"Directory"]) {
if (extension) *extension = [item objectAtIndex: 1];
} else if ([type isEqualToString: @"Source"]) {
if (extension) *extension = [item objectAtIndex: 3];
if (filename) *filename = [item objectAtIndex: 1];
}
}
}
- (NSString*) extensionForRow: (int) rowIndex {
if (!extensionNames)
[self reallyUpdateTableData: NO]; // Need update now
// Return nil if the row does not exist
if (rowIndex < 0) return nil;
if (rowIndex >= [extensionNames count]) return nil;
return [extensionNames objectAtIndex: rowIndex];
}
// = Table view datasource =
//
// When acting as a table view datasource, we display the list of installed 'master' extension names only.
// The setting for mergesMultipleExtensions doesn't matter in this mode.
//
// Recognised table column names:
// 'extension' - the name of the extension
//
- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
if (!extensionNames)
[self reallyUpdateTableData: NO]; // Need update now
return [extensionNames count];
}
- (id) tableView: (NSTableView *) aTableView
objectValueForTableColumn: (NSTableColumn *) aTableColumn
row: (int)rowIndex {
if (!extensionNames)
[self reallyUpdateTableData: NO]; // Need update now
NSString* identifier = [aTableColumn identifier];
if ([identifier isEqualToString: @"extension"]) {
if (rowIndex < 0 || rowIndex >= [extensionNames count]) return @"== BAD ROW INDEX ==";
return [extensionNames objectAtIndex: rowIndex];
}
return nil; // Unknown column type
}
- (NSDragOperation) tableView: (NSTableView*) tableView
validateDrop: (id<NSDraggingInfo>) info
proposedRow: (int)row
proposedDropOperation: (NSTableViewDropOperation)operation {
// Outline view drops follow NI semantics (which is different from Inform 6 in many ways)
if (!extensionNames)
[self reallyUpdateTableData: NO]; // Need update now
// We can accept files dropped on the outline view
NSArray* pbFiles = [[info draggingPasteboard] propertyListForType: NSFilenamesPboardType];
if (pbFiles == nil || ![pbFiles isKindOfClass: [NSArray class]]) {
return NSDragOperationNone;
}
// We accept directories or files of the right type
NSEnumerator* fileEnum = [pbFiles objectEnumerator];