-
Notifications
You must be signed in to change notification settings - Fork 28
/
CreeveyMainWindowController.m
1186 lines (1095 loc) · 42.9 KB
/
CreeveyMainWindowController.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
//Copyright 2005-2023 Dominic Yu. Some rights reserved.
//This work is licensed under the Creative Commons
//Attribution-NonCommercial-ShareAlike License. To view a copy of this
//license, visit http://creativecommons.org/licenses/by-nc-sa/2.0/ or send
//a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
//California 94305, USA.
#import "CreeveyMainWindowController.h"
#import "DYCarbonGoodies.h"
#import "NSMutableArray+DYMovable.h"
#import "DirBrowserDelegate.h"
#import "DYFileWatcher.h"
#import "CreeveyController.h"
#import "DYCreeveyBrowser.h"
#import "DYImageCache.h"
#import "DYWrappingMatrix.h"
#import <sys/stat.h>
#import "DYExiftags.h"
@implementation NSString (DateModifiedCompare)
/* Using file attributes to sort file paths and then rely on the sort order staying the same
* is somewhat dangerous because the file might have been modified (changing the modification date)
* or moved (making the path invalid). We try to mitigate this by watching for changes to the filesystem. */
- (NSComparisonResult)dateModifiedCompare:(NSString *)other
{
struct stat aBuf, bBuf;
if (stat(self.fileSystemRepresentation, &aBuf) == 0 &&
stat(other.fileSystemRepresentation, &bBuf) == 0) {
time_t aTime = aBuf.st_mtimespec.tv_sec;
time_t bTime = bBuf.st_mtimespec.tv_sec;
if (aTime != bTime)
return aTime < bTime ? NSOrderedAscending : NSOrderedDescending;
}
// use file name comparison as fallback; filenames are guaranteed to be unique, but mod times are not
return [self localizedStandardCompare:other];
}
//#define LOGSORT
static time_t ExifDateFromFile(NSString *s) {
s = ResolveAliasToPath(s);
NSString *x = s.pathExtension.lowercaseString;
const char *c = s.fileSystemRepresentation;
time_t t;
#ifdef LOGSORT
static NSMutableSet *seen;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
seen = [NSMutableSet set];
});
BOOL w = [seen containsObject:s];
[seen addObject:s];
#endif
if (IsRaw(x) &&
(t = ExifDateFromRawFile(c)) != -1) {
#ifdef LOGSORT
if (!w) NSLog(@"raw %@:%@", [NSDate dateWithTimeIntervalSince1970:t], s.lastPathComponent);
#endif
return t;
}
if ((IsJPEG(x) || [NSHFSTypeOfFile(s) isEqualToString:@"JPEG"]) &&
(t = ExifDatetimeForFile(c, JPEG)) != -1) {
#ifdef LOGSORT
if (!w) NSLog(@"jpg %@:%@", [NSDate dateWithTimeIntervalSince1970:t], s.lastPathComponent);
#endif
return t;
}
if (IsHeif(x) &&
(t = ExifDatetimeForFile(c, HEIF)) != -1) {
#ifdef LOGSORT
if (!w) NSLog(@"heic %@:%@", [NSDate dateWithTimeIntervalSince1970:t], s.lastPathComponent);
#endif
return t;
}
struct stat buf;
t = stat(c, &buf) ? -1 : buf.st_birthtimespec.tv_sec;
#ifdef LOGSORT
if (!w) NSLog(@"stat %@:%@", [NSDate dateWithTimeIntervalSince1970:t], s.lastPathComponent);
#endif
return t;
}
- (NSComparisonResult)exifDateCompare:(NSString *)other {
time_t aTime, bTime;
if ((aTime = ExifDateFromFile(self)) != -1 &&
(bTime = ExifDateFromFile(other)) != -1 &&
aTime != bTime) {
return aTime < bTime ? NSOrderedAscending : NSOrderedDescending;
}
return [self localizedStandardCompare:other];
}
@end
@interface CreeveyMainWindowController () <DYFileWatcherDelegate>
@property (nonatomic, readonly) NSSplitView *splitView;
@property BOOL wantsSubfolders;
@property (nonatomic, strong) NSString *recurseRoot;
@end
@implementation CreeveyMainWindowController
{
NSMutableArray *filenames, *displayedFilenames;
NSLock *loadImageLock; NSTimeInterval lastThreadTime;
CreeveyController * __weak appDelegate;
DirBrowserDelegate * __weak dirBrowserDelegate;
_Atomic char stopCaching;
NSConditionLock *imageCacheQueueLock;
NSMutableArray *imageCacheQueue, *secondaryImageCacheQueue;
_Atomic BOOL imageCacheQueueRunning;
_Atomic NSInteger _maxCellWidth;
BOOL exifWindowNeedsUpdate;
BOOL currentFilesDeletable;
BOOL filenamesDone, loadingDone, // loadingDone only meaningful if filenamesDone is true, always check both!
startSlideshowWhenReady;
NSMutableSet *filesBeingOpened; // to be selected
short int sortOrder;
time_t matrixModTime;
short int currCat;
_Atomic BOOL _background;
BOOL _wantsSubfolders;
NSImage *_brokenDoc, *_loadingImage;
NSMutableSet *_accessedFiles;
NSLock *_accessedLock, *_internalLock;
_Atomic(NSTimeInterval) _statusTime;
DYFileWatcher *_fileWatcher;
}
@synthesize dirBrowser, slidesBtn, imgMatrix, statusFld, bottomStatusFld;
- (instancetype)initWithWindowNibName:(NSString *)windowNibName {
if (self = [super initWithWindowNibName:windowNibName]) {
filenames = [[NSMutableArray alloc] init];
displayedFilenames = [[NSMutableArray alloc] init];
loadImageLock = [[NSLock alloc] init];
filesBeingOpened = [[NSMutableSet alloc] init];
sortOrder = 1; // by name
imageCacheQueueLock = [[NSConditionLock alloc] initWithCondition:0];
imageCacheQueue = [[NSMutableArray alloc] init];
secondaryImageCacheQueue = [[NSMutableArray alloc] init];
imageCacheQueueRunning = YES;
appDelegate = (CreeveyController *)NSApp.delegate;
_accessedFiles = [[NSMutableSet alloc] init];
_accessedLock = [[NSLock alloc] init];
_internalLock = [[NSLock alloc] init];
_fileWatcher = [[DYFileWatcher alloc] initWithDelegate:self];
}
return self;
}
- (void)windowDidLoad {
[self.window setFrameUsingName:@"MainWindowLoc"];
// otherwise it uses the frame in the nib
NSUserDefaults *u = NSUserDefaults.standardUserDefaults;
NSSplitView *splitView = self.splitView;
float height = [u floatForKey:@"MainWindowSplitViewTopHeight"];
if (height > 0.0) [splitView setPosition:height ofDividerAtIndex:0];
else dispatch_async(dispatch_get_main_queue(), ^{
// apparently the splitview can't be collapsed until after windowDidLoad returns
[splitView setPosition:0 ofDividerAtIndex:0];
});
splitView.delegate = self; // must set delegate after restoring position so the didResize notification doesn't save the height from the nib
[imgMatrix setFrameSize:imgMatrix.superview.frame.size];
imgMatrix.maxCellWidth = _maxCellWidth = [u integerForKey:@"DYWrappingMatrixMaxCellWidth"];
imgMatrix.cellWidth = [u floatForKey:@"thumbCellWidth"];
self.window.restorationClass = [CreeveyController class];
dirBrowserDelegate = dirBrowser.delegate;
dirBrowserDelegate.revealedDirectories = appDelegate.revealedDirectories;
_brokenDoc = [NSImage imageNamed:@"brokendoc.tif"];
_loadingImage = [NSImage imageNamed:@"loading.png"];
imgMatrix.loadingImage = _loadingImage;
[NSThread detachNewThreadSelector:@selector(thumbLoader:) toTarget:self withObject:nil];
[NSUserDefaultsController.sharedUserDefaultsController addObserver:self forKeyPath:@"values.DYWrappingMatrixMaxCellWidth" options:0 context:NULL];
}
- (void)dealloc {
[NSUserDefaultsController.sharedUserDefaultsController removeObserver:self forKeyPath:@"values.DYWrappingMatrixMaxCellWidth"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)c context:(void *)context
{
if ([keyPath isEqualToString:@"values.DYWrappingMatrixMaxCellWidth"]) {
_maxCellWidth = [NSUserDefaults.standardUserDefaults integerForKey:@"DYWrappingMatrixMaxCellWidth"];
}
}
- (void)window:(NSWindow *)window willEncodeRestorableState:(NSCoder *)state
{
BOOL collapsed = statusFld.superview.hidden;
NSDictionary *data = @{@"path":self.path, @"split1":@(collapsed ? 0.0 : statusFld.superview.frame.size.height)};
[state encodeObject:data forKey:@"creeveyWindowState"];
}
- (void)window:(NSWindow *)window didDecodeRestorableState:(NSCoder *)state
{
// as of macOS 12, apps must support secure restorable state (apparently malicious attacks could happen if there was bad data masquerading as your saved state)
// the fix is apparently to give a list of secure classes when you ask to decode the data. See the AppKit release notes for macOS 12.
NSDictionary *data = [state decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class],[NSString class],[NSNumber class]]] forKey:@"creeveyWindowState"];
if (![data isKindOfClass:[NSDictionary class]]) data = @{};
NSString *path = data[@"path"];
if (![path isKindOfClass:[NSString class]]) path = nil;
if (path == nil || ![self setPath:path])
if (![self setPath:[NSUserDefaults.standardUserDefaults stringForKey:@"picturesFolderPath"]])
[self setPath:NSHomeDirectory()];
NSNumber *heightObj = data[@"split1"];
if ([heightObj isKindOfClass:[NSNumber class]]) {
float height = heightObj.floatValue;
[self.splitView setPosition:height ofDividerAtIndex:0];
}
}
- (NSSplitView *)splitView
{
return self.window.contentView.subviews[0];
}
- (void)windowWillClose:(NSNotification *)notification {
[self removeAllPathsFromAccessedFilesArray];
imageCacheQueueRunning = NO;
[imageCacheQueueLock lock];
[imageCacheQueueLock unlockWithCondition:1];
}
#pragma mark sorting stuff
- (DYWrappingMatrix *)imageMatrix { return imgMatrix; }
- (short int)sortOrder { return sortOrder; }
- (void)setSortOrder:(short int)n {
sortOrder = n;
}
- (void)changeSortOrder:(short int)n {
sortOrder = n;
[NSThread detachNewThreadSelector:@selector(loadImages:)
toTarget:self
withObject:nil];
}
- (NSString *)path { return [dirBrowserDelegate path]; }
// returns NO if doesn't exist, useful for applicationDidFinishLaunching
- (BOOL)setPath:(NSString *)s {
NSFileManager *fm = NSFileManager.defaultManager;
BOOL isDir;
if (![fm fileExistsAtPath:s isDirectory:&isDir])
return NO;
if (!isDir)
s = s.stringByDeletingLastPathComponent;
[dirBrowserDelegate setPath:s];
[dirBrowser sendAction];
[self.window invalidateRestorableState];
return YES;
}
- (void)setDefaultPath {
NSUserDefaults *u = NSUserDefaults.standardUserDefaults;
NSString *s = [u integerForKey:@"startupOption"] == 0
? [u stringForKey:@"lastFolderPath"]
: [u stringForKey:@"picturesFolderPath"];
if (![self setPath:s])
if (![self setPath:CREEVEY_DEFAULT_PATH])
[self setPath:NSHomeDirectory()];
[self.window makeFirstResponder:dirBrowser]; //another stupid workaround, for hiliting
}
- (BOOL)pathIsCurrentDirectory:(NSString *)filename {
NSString *browserPath = [dirBrowserDelegate path];
if (self.wantsSubfolders) return [filename hasPrefix:[browserPath stringByAppendingString:@"/"]];
return [filename.stringByDeletingLastPathComponent isEqualToString:browserPath];
}
- (BOOL)pathIsVisibleThreaded:(NSString *)filename {
NSString *browserPath = dirBrowserDelegate.currPath;
if (self.wantsSubfolders) return [filename hasPrefix:[browserPath stringByAppendingString:@"/"]];
return [filename.stringByDeletingLastPathComponent isEqualToString:browserPath];
}
- (void)updateDefaults {
NSUserDefaults *u = NSUserDefaults.standardUserDefaults;
if ([u integerForKey:@"startupOption"] == 0)
[u setObject:[dirBrowserDelegate path] forKey:@"lastFolderPath"];
[u setFloat:imgMatrix.cellWidth forKey:@"thumbCellWidth"];
float height = statusFld.superview.hidden ? 0 : statusFld.superview.frame.size.height;
[u setFloat:height forKey:@"MainWindowSplitViewTopHeight"];
[self.window saveFrameUsingName:@"MainWindowLoc"];
}
- (BOOL)currentFilesDeletable { return currentFilesDeletable; }
- (BOOL)filenamesDone { return filenamesDone; }
- (NSArray *)displayedFilenames { return displayedFilenames; }
- (NSUInteger)indexOfFilename:(NSString *)s {
return [displayedFilenames indexOfObject:s inSortedRange:NSMakeRange(0, displayedFilenames.count) options:0 usingComparator:self.comparator];
}
NSComparator ComparatorForSortOrder(short sortOrder) {
switch (sortOrder) {
case 1:
default:
return ^NSComparisonResult(id a, id b) {
return [a localizedStandardCompare:b];
};
case 2:
return ^NSComparisonResult(id a, id b) {
return [a dateModifiedCompare:b];
};
case -1:
return ^NSComparisonResult(id a, id b) {
return [b localizedStandardCompare:a];
};
case -2:
return ^NSComparisonResult(id a, id b) {
return [b dateModifiedCompare:a];
};
case 3:
return ^NSComparisonResult(id a, id b) {
return [a exifDateCompare:b];
};
case -3:
return ^NSComparisonResult(id a, id b) {
return [b exifDateCompare:a];
};
}
}
- (NSComparator)comparator {
return ComparatorForSortOrder(sortOrder);
}
- (NSArray *)currentSelection {
return imgMatrix.selectedFilenames;
}
- (NSIndexSet *)selectedIndexes {
return imgMatrix.selectedIndexes;
}
- (void)selectIndex:(NSUInteger)i {
[imgMatrix selectIndex:i];
}
- (void)openFiles:(NSArray *)a withSlideshow:(BOOL)doSlides{
startSlideshowWhenReady = doSlides;
[filesBeingOpened addObjectsFromArray:a];
BOOL isDir;
NSString *aPath = a[0];
if ([NSFileManager.defaultManager fileExistsAtPath:aPath isDirectory:&isDir] && !isDir)
aPath = aPath.stringByDeletingLastPathComponent;
if ([aPath isEqualToString:self.path]) {
// special case where the path is the same. Don't reload, just change the selection
[imgMatrix selectFilenames:a comparator:self.comparator];
if (doSlides)
dispatch_async(dispatch_get_main_queue(), ^{
// need to dispatch this otherwise the slideshow comes up behind this window
[appDelegate slideshowFromAppOpen:imgMatrix.selectedFilenames];
});
} else {
[self setPath:aPath];
}
}
#pragma mark FSEvents stuff
- (BOOL)wantsSubfolders {
[_internalLock lock];
BOOL result = _wantsSubfolders;
[_internalLock unlock];
return result;
}
- (void)setWantsSubfolders:(BOOL)b {
[_internalLock lock];
_wantsSubfolders = b;
_fileWatcher.wantsSubfolders = b;
[_internalLock unlock];
}
- (void)watcherFiles:(NSArray *)files deleted:(NSArray *)deleted {
if (!filenamesDone) return;
BOOL sortByModTime = abs(self.sortOrder) == 2;
for (NSString *s in files) {
NSUInteger count = filenames.count;
struct stat buf;
if (sortByModTime && !stat(s.fileSystemRepresentation, &buf) && buf.st_mtimespec.tv_sec > matrixModTime) {
// when sorting by mod time, file list needs to be adjusted if the file's mod time has changed!
NSUInteger oldIdx, idx = [filenames updateIndexOfObject:s usingComparator:self.comparator oldIndex:&oldIdx];
if (idx != NSNotFound) {
if (displayedFilenames.count != filenames.count)
idx = [displayedFilenames updateIndexOfObject:s usingComparator:self.comparator oldIndex:&oldIdx];
else
[displayedFilenames moveObjectAtIndex:oldIdx toIndex:idx];
if (idx != NSNotFound)
[imgMatrix moveImageAtIndex:oldIdx toIndex:idx];
[self fileWasChanged:s];
continue;
}
}
NSUInteger idx = [filenames indexOfObject:s inSortedRange:NSMakeRange(0, count) options:NSBinarySearchingInsertionIndex usingComparator:self.comparator];
if (idx < count && [filenames[idx] isEqualToString:s]) {
[self fileWasChanged:s];
} else {
[self addFile:s atIndex:idx];
}
}
for (NSString *s in deleted) {
NSUInteger idx = (sortOrder == 1 || sortOrder == -1) ? [filenames indexOfObject:s inSortedRange:NSMakeRange(0, filenames.count) options:0 usingComparator:self.comparator] : [filenames indexOfObject:s];
if (idx != NSNotFound)
[self fileWasDeleted:s atIndex:idx];
}
if (sortByModTime) time(&matrixModTime);
}
- (void)watcherRootChanged:(NSURL *)fileRef {
if (!filenamesDone) return;
[self removeAllPathsFromAccessedFilesArray];
[self clearImageCacheQueue];
NSString *s = _fileWatcher.path, *newPath = fileRef.path;
if (newPath == nil) return;
[self.window setTitleWithRepresentedFilename:newPath];
[filenames changeBase:s toPath:newPath];
[displayedFilenames changeBase:s toPath:newPath];
[imgMatrix changeBase:s toPath:newPath];
}
- (void)addFile:(NSString *)s atIndex:(NSUInteger)idx {
if (displayedFilenames.count == filenames.count) {
[displayedFilenames insertObject:s atIndex:idx];
[imgMatrix addImage:nil withFilename:s atIndex:idx];
}
[filenames insertObject:s atIndex:idx];
[self updateStatusFld];
}
- (void)fileWasChanged:(NSString *)s {
if (![self pathIsCurrentDirectory:s]) return;
// update thumb
DYImageCache *thumbsCache = appDelegate.thumbsCache;
NSString *theFile = ResolveAliasToPath(s);
[_accessedLock lock];
if ([_accessedFiles containsObject:s]) {
[_accessedFiles removeObject:s];
[thumbsCache endAccess:theFile];
}
[_accessedLock unlock];
BOOL addedToCache = NO;
NSImage *thumb = [thumbsCache imageForKeyInvalidatingCacheIfNecessary:theFile];
if (thumb) {
[thumbsCache beginAccess:theFile];
addedToCache = YES;
} else { // ** dup
if ([thumbsCache attemptLockOnFile:theFile]) {
DYImageInfo *result = [[DYImageInfo alloc] initWithPath:theFile];
[thumbsCache createScaledImage:result];
if (result.image) {
[thumbsCache addImage:result forFile:theFile];
addedToCache = YES;
}
else [thumbsCache dontAddFile:theFile];
thumb = result.image;
} else {
thumb = [thumbsCache imageForKey:theFile];
if (thumb) {
[thumbsCache beginAccess:theFile];
addedToCache = YES;
}
}
}
if (!thumb) thumb = _brokenDoc;
// since we already checked if the file is in the current directory, we can assume the matrix's files have the same sort order
NSUInteger mtrxIdx = [imgMatrix.filenames indexOfObject:s inSortedRange:NSMakeRange(0, imgMatrix.filenames.count) options:0 usingComparator:self.comparator];
if (mtrxIdx != NSNotFound) {
[imgMatrix updateImage:thumb atIndex:mtrxIdx];
if (addedToCache) {
[_accessedLock lock];
[_accessedFiles addObject:s];
[_accessedLock unlock];
}
} else if (addedToCache) {
[thumbsCache endAccess:theFile];
}
}
- (void)fileWasDeleted:(NSString *)s {
[self fileWasDeleted:s atIndex:NSNotFound];
}
- (void)fileWasDeleted:(NSString *)s atIndex:(NSUInteger)i {
if (![self pathIsCurrentDirectory:s]) return;
BOOL linearSearch = abs(self.sortOrder) != 1;
NSUInteger mtrxIdx;
if (i == NSNotFound) {
i = linearSearch ? [filenames indexOfObject:s] : [filenames indexOfObject:s inSortedRange:NSMakeRange(0, filenames.count) options:0 usingComparator:self.comparator];
}
if (i != NSNotFound) {
stopCaching = 1;
[loadImageLock lock];
if ((mtrxIdx = linearSearch ? [imgMatrix.filenames indexOfObject:s] : [imgMatrix.filenames indexOfObject:s inSortedRange:NSMakeRange(0, imgMatrix.filenames.count) options:0 usingComparator:self.comparator]) != NSNotFound) {
[imgMatrix removeImageAtIndex:mtrxIdx];
[displayedFilenames removeObjectAtIndex:mtrxIdx];
}
[filenames removeObjectAtIndex:i];
[loadImageLock unlock];
[_accessedLock lock];
if ([_accessedFiles containsObject:s]) {
[_accessedFiles removeObject:s];
[appDelegate.thumbsCache endAccess:s];
}
[_accessedLock unlock];
if (!filenamesDone || !loadingDone) //[imgMatrix numCells] < [filenames count])
[NSThread detachNewThreadSelector:@selector(loadImages:)
toTarget:self
withObject:filenamesDone ? [dirBrowserDelegate path] : nil];
// must check filenamesDone in case interrupted
[self updateStatusFld];
if (imgMatrix.numCells == 0)
slidesBtn.enabled = NO; // **
}
}
- (void)filesWereUndeleted:(NSArray *)a {
NSString *currentPath = self.path;
BOOL subfolders = self.wantsSubfolders;
for (NSString *s in a) {
if (subfolders ? [s hasPrefix:currentPath] : [s.stringByDeletingLastPathComponent isEqualToString:currentPath])
dispatch_async(dispatch_get_main_queue(), ^{
if (!filenamesDone) return;
NSUInteger count = filenames.count;
NSUInteger idx = [filenames indexOfObject:s inSortedRange:NSMakeRange(0, count) options:NSBinarySearchingInsertionIndex usingComparator:self.comparator];
if (idx == count || ![filenames[idx] isEqualToString:s])
[self addFile:s atIndex:idx];
});
}
}
- (void)updateStatusFld {
id s = NSLocalizedString(@"%u images", @"");
NSString *status = currCat
? [NSString stringWithFormat:@"%@: %@",
[NSString stringWithFormat:NSLocalizedString(@"Group %i", @""), currCat],
[NSString stringWithFormat:s, displayedFilenames.count]]
: [NSString stringWithFormat:s, filenames.count];
[self updateStatusString:status];
}
- (void)updateStatusString:(NSString *)s {
_statusTime = NSDate.timeIntervalSinceReferenceDate;
statusFld.stringValue = s;
}
- (void)updateStatusOnMainThread:(NSString * (^)(void))f {
NSTimeInterval timeStamp = _statusTime = NSDate.timeIntervalSinceReferenceDate;
dispatch_async(dispatch_get_main_queue(), ^{
if (_statusTime > timeStamp) return;
NSString *s = f();
if (s) statusFld.stringValue = s;
});
}
- (void)clearImageCacheQueue {
[imageCacheQueueLock lock];
[imageCacheQueue removeAllObjects];
[secondaryImageCacheQueue removeAllObjects];
[imageCacheQueueLock unlockWithCondition:0];
}
- (void)removeAllPathsFromAccessedFilesArray {
DYImageCache *thumbsCache = appDelegate.thumbsCache;
[_accessedLock lock];
for (NSString *s in _accessedFiles) {
[thumbsCache endAccess:ResolveAliasToPath(s)];
}
[_accessedFiles removeAllObjects];
[_accessedLock unlock];
}
#pragma mark load thread
- (void)loadImages:(NSString *)thePath { // called in a separate thread
//NSLog(@"loadImages thread started for %@", thePath);
// assume (incorrectly?) that threads will be executed in the order detached
// better to set in loadDir and pass it in?
NSTimeInterval myThreadTime;
@autoreleasepool {
myThreadTime = lastThreadTime = NSDate.timeIntervalSinceReferenceDate;
// setting stopCaching stops only one thread (see below)
// if there's a backlog of several threads, need to check thread time instead
[loadImageLock lock];
if (myThreadTime < lastThreadTime) {
//NSLog(@"stale thread aborted, %@", thePath);
[filesBeingOpened removeAllObjects];
[loadImageLock unlock];
return;
}
stopCaching = 0;
NSThread.currentThread.name = [NSString stringWithFormat:@"loadImages:%@", thePath.lastPathComponent];
NSUInteger i = 0;
NSString *loadingMsg = NSLocalizedString(@"Getting filenames...", @"");
//NSTimeInterval imgloadstarttime = NSDate.timeIntervalSinceReferenceDate;
dispatch_async(dispatch_get_main_queue(), ^{
[imgMatrix removeAllImages];
});
NSMutableSet *filesForSlideshow = startSlideshowWhenReady ? [NSMutableSet setWithCapacity:filesBeingOpened.count] : nil;
if (thePath) {
dispatch_async(dispatch_get_main_queue(), ^{
[_fileWatcher stop];
});
[filenames removeAllObjects];
[self clearImageCacheQueue];
BOOL recurseSubfolders = self.wantsSubfolders;
NSDirectoryEnumerator *e = CreeveyEnumerator(thePath, recurseSubfolders);
for (NSURL *url in e) {
@autoreleasepool {
if ([appDelegate handledDirectory:url subfolders:recurseSubfolders e:e])
continue;
if ([appDelegate shouldShowFile:url]) {
NSString *aPath = url.path;
[filenames addObject:aPath];
if (startSlideshowWhenReady && [filesBeingOpened containsObject:aPath])
[filesForSlideshow addObject:aPath];
if (++i % 100 == 0)
[self updateStatusOnMainThread:^NSString *{
return [NSString stringWithFormat:@"%@ (%lu)", loadingMsg, i];
}];
}
if (stopCaching) {
[filenames removeAllObjects]; // so it fails count > 0 test below
break;
}
}
}
}
if (filenames.count) {
[self updateStatusOnMainThread:^NSString *{
return [NSString stringWithFormat:NSLocalizedString(@"Sorting %lu filenames…", @""), filenames.count];
}];
[filenames sortUsingComparator:self.comparator];
}
if (currCat) { // currCat > 0 whenever cat changes (see keydown)
// this means deleting when a cat is displayed will cause unsightly flashing
// but we can live with that for now. maybe temp set currcat to 0?
[self clearImageCacheQueue];
[displayedFilenames removeAllObjects];
if (currCat == 1) {
currCat = 0;
[displayedFilenames addObjectsFromArray:filenames];
} else {
for (NSString *path in filenames) {
if ([appDelegate.cats[currCat-2] containsObject:path])
[displayedFilenames addObject:path];
}
}
} else {
[displayedFilenames setArray:filenames];
}
time(&matrixModTime);
if (startSlideshowWhenReady) {
startSlideshowWhenReady = NO;
// set this back to NO so we don't get infinite slideshow looping if a category is selected (initiated by windowDidBecomeMain:)
if (filesForSlideshow.count) {
NSArray *files = [filesForSlideshow.allObjects sortedArrayUsingComparator:self.comparator];
[appDelegate performSelectorOnMainThread:@selector(slideshowFromAppOpen:) withObject:files waitUntilDone:NO]; // this must be called after displayedFilenames is sorted in case it calls back for indexOfFilename:
}
}
filenamesDone = YES;
//NSLog(@"got %d files.", [filenames count]);
}
#pragma mark populate matrix
@autoreleasepool {
DYImageCache *thumbsCache = appDelegate.thumbsCache;
[self removeAllPathsFromAccessedFilesArray];
NSUInteger i = 0;
NSMutableIndexSet *selectedIndexes = [NSMutableIndexSet indexSet];
if (displayedFilenames.count > 0) {
loadingDone = NO;
dispatch_async(dispatch_get_main_queue(), ^{
slidesBtn.enabled = YES;
});
currentFilesDeletable = [NSFileManager.defaultManager isDeletableFileAtPath:displayedFilenames[0]];
NSUInteger numFiles = displayedFilenames.count;
NSUInteger maxThumbs = [NSUserDefaults.standardUserDefaults
integerForKey:@"maxThumbsToLoad"];
for (; i<numFiles; ++i) {
if (stopCaching) {
//NSLog(@"aborted1 %@", origPath);
break;
}
NSString *origPath = displayedFilenames[i];
NSString *resolvedPath = ResolveAliasToPath(origPath);
NSImage *cachedImage = [thumbsCache imageForKeyInvalidatingCacheIfNecessary:resolvedPath]; // remember the thumbsCache's key is the resolved path!
// what happens if another window happens to invalidate a thumb that we started "access" to?
// Actually it won't matter if we make too many calls to endAccess:, worst case is we'll have to recache it at some point.
dispatch_async(dispatch_get_main_queue(), ^{
[imgMatrix addImage:cachedImage withFilename:origPath];
});
if (cachedImage != nil) {
[thumbsCache beginAccess:resolvedPath];
[_accessedLock lock];
[_accessedFiles addObject:origPath];
[_accessedLock unlock];
}
if ([filesBeingOpened containsObject:origPath])
[selectedIndexes addIndex:i];
// now, to simulate the original behavior, add a certain number of
// images to the queue automatically
if (cachedImage == nil && i < maxThumbs) {
[imageCacheQueueLock lock];
[secondaryImageCacheQueue addObject:@[origPath, @(i)]];
[imageCacheQueueLock unlockWithCondition:1];
}
}
}
loadingDone = (i==displayedFilenames.count);
/*if (i) {
NSTimeInterval delta = NSDate.timeIntervalSinceReferenceDate - imgloadstarttime;
NSLog(@"%d files/%f secs = %f/s; %f s/file", i, delta,
i / delta, delta/i);
}*/
if (myThreadTime == lastThreadTime) {
[self performSelectorOnMainThread:@selector(updateStatusFld) withObject:nil waitUntilDone:NO];
if (thePath)
dispatch_async(dispatch_get_main_queue(), ^{
[_fileWatcher watchDirectory:thePath];
});
}
if (loadingDone && filesBeingOpened.count) {
[filesBeingOpened removeAllObjects];
if (myThreadTime == lastThreadTime && selectedIndexes.count)
dispatch_async(dispatch_get_main_queue(), ^{
if ([thePath isEqualToString:dirBrowserDelegate.currPath])
[imgMatrix scrollToFirstSelected:selectedIndexes];
});
}
[loadImageLock unlock];
}
}
- (IBAction)displayDir:(id)sender {
stopCaching = 1;
currentFilesDeletable = NO;
filenamesDone = NO;
currCat = 0;
slidesBtn.enabled = NO;
NSString *currentPath = [dirBrowserDelegate path];
_subfoldersButton.enabled = ![currentPath isEqualToString:@"/"]; // let's not ever load up the entire file system
if (self.wantsSubfolders && sender) { // sender is dirBrowserDelegate when non-nil
if (![currentPath hasPrefix:_recurseRoot]) {
self.wantsSubfolders = NO;
_subfoldersButton.state = NSControlStateValueOff;
}
}
[self updateStatusString:NSLocalizedString(@"Getting filenames...", @"")];
[self.window setTitleWithRepresentedFilename:currentPath];
[NSThread detachNewThreadSelector:@selector(loadImages:)
toTarget:self withObject:currentPath];
}
- (IBAction)setRecurseSubfolders:(id)sender {
NSButton *button = sender;
self.wantsSubfolders = (button.state == NSOnState);
// remember where we started recursing subfolders
if (self.wantsSubfolders) {
NSString *path = [dirBrowserDelegate path];
// but don't reset if we're still in a subfolder from the last time this was set
if (_recurseRoot == nil || ![path hasPrefix:_recurseRoot])
// add a slash so we continue recursing for any sibling folders, but not the parent folder
self.recurseRoot = [[dirBrowserDelegate path].stringByDeletingLastPathComponent stringByAppendingString:@"/"];
} else {
// if user aborted, assume that's not a good place to recurse
if (!filenamesDone)
self.recurseRoot = nil;
}
[self displayDir:nil];
}
#pragma mark menu stuff
- (void)selectAll:(id)sender{
[self.window makeFirstResponder:imgMatrix];
[imgMatrix selectAll:sender];
}
- (void)selectNone:(id)sender{
[imgMatrix selectNone:sender];
}
#pragma mark event stuff
- (void)fakeKeyDown:(NSEvent *)e {
[self.window makeFirstResponder:imgMatrix];
[imgMatrix keyDown:e];
[self.window makeFirstResponder:dirBrowser];
}
- (void)keyDown:(NSEvent *)e {
if (e.characters.length == 0) return;
unichar c = [e.characters characterAtIndex:0];
if (filenamesDone && c >= NSF1FunctionKey && c <= NSF12FunctionKey) {
c = c - NSF1FunctionKey + 1;
if ((e.modifierFlags & NSEventModifierFlagCommand) != 0) {
NSUInteger i;
short j;
NSArray *a = imgMatrix.selectedFilenames;
if (!a.count) {
NSBeep();
return;
}
NSMutableSet * __strong *cats = appDelegate.cats;
for (i=a.count-1; i != -1; i--) { // TODO: this code is suspect
id fname = a[i];
if (c == 1) {
for (j=0; j<NUM_FNKEY_CATS; ++j)
[cats[j] removeObject:fname];
} else {
if ([cats[c-2] containsObject:fname])
[cats[c-2] removeObject:fname];
else
[cats[c-2] addObject:fname];
}
}
[appDelegate updateCats];
if (!currCat || (c && c != currCat)) {
[self updateStatusString:[NSString stringWithFormat:
NSLocalizedString(@"%u image(s) updated for Group %i", @""),
(unsigned int)a.count, c]];
[self performSelector:@selector(updateStatusFld)
withObject:nil
afterDelay:2];
return;
} // but reload, below, if displaying a cat
} else {
if (c==1) c = 0;
if (currCat == c) return;
currCat = c ? c : 1; // strictly speaking, should go after the lock
// but we're reloading anyway, it's OK
}
stopCaching = 1; // don't need to lock, not changing anything
currentFilesDeletable = NO; // dup code from displayDir?
filenamesDone = NO;
slidesBtn.enabled = NO;
[NSThread detachNewThreadSelector:@selector(loadImages:)
toTarget:self
withObject:nil];
return;
}
[super keyDown:e];
}
#pragma mark window delegate methods
- (void)windowDidResignMain:(NSNotification *)aNotification {
_background = YES;
}
- (void)windowDidBecomeMain:(NSNotification *)aNotification {
[self updateExifInfo];
_background = NO;
if (filenamesDone && currCat) { // reload in case category membership of certain files changed;
// ** we should probably notify when cats change instead
// ** and also handle the case where you change something's category so it no longer belongs in the current view
stopCaching = 1;
[loadImageLock lock];
// make reloading less bad by saving selection
[filesBeingOpened addObjectsFromArray:imgMatrix.selectedFilenames];
[loadImageLock unlock];
[NSThread detachNewThreadSelector:@selector(loadImages:)
toTarget:self
withObject:nil];
}
}
// the existence of this method enables the '+' button in the tab bar
- (void)newWindowForTab:(id)sender {
[appDelegate newTab:self];
}
- (void)updateExifInfo:(id)sender {
NSTextView *exifTextView = appDelegate.exifTextView;
if (!exifTextView.window.visible) return;
NSView *mainView = exifTextView.window.contentView;
NSImageView *thumbView = [mainView viewWithTag:2];
NSMutableAttributedString *attStr;
NSMutableIndexSet *selectedIndexes = imgMatrix.selectedIndexes;
if (selectedIndexes.count == 1) {
DYImageCache *cache = appDelegate.thumbsCache;
NSString *path = imgMatrix.firstSelectedFilename;
NSButton *moreBtn = [mainView viewWithTag:1];
attStr = Fileinfo2EXIFString(path, cache, moreBtn.state);
NSString *resolvedPath = ResolveAliasToPath(path);
exifWindowNeedsUpdate = [cache infoForKey:resolvedPath] == nil;
if (!exifWindowNeedsUpdate)
thumbView.image = [DYExiftags exifThumbForPath:resolvedPath];
} else {
id s = selectedIndexes.count
? [NSString stringWithFormat:NSLocalizedString(@"%d images selected.", @""),
(unsigned int)selectedIndexes.count]
: NSLocalizedString(@"No images selected.", @"");
attStr = [[NSMutableAttributedString alloc] initWithString:s attributes:@{NSFontAttributeName:[NSFont userFontOfSize:12]}];
thumbView.image = nil;
exifWindowNeedsUpdate = NO;
}
[attStr addAttribute:NSForegroundColorAttributeName value:NSColor.labelColor range:NSMakeRange(0,attStr.length)];
[exifTextView.textStorage setAttributedString:attStr];
}
- (void)updateExifInfo {
[self updateExifInfo:nil];
}
#pragma mark splitview delegate
- (void)splitViewDidResizeSubviews:(NSNotification *)notification
{
float height = statusFld.superview.hidden ? 0 : statusFld.superview.frame.size.height;
[NSUserDefaults.standardUserDefaults setFloat:height forKey:@"MainWindowSplitViewTopHeight"];
}
-(BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview {
return subview == dirBrowser.superview;
}
#pragma mark wrapping matrix methods
- (void)wrappingMatrixSelectionDidChange:(NSIndexSet *)selectedIndexes {
NSString *s;
NSUInteger count = selectedIndexes.count;
if (count == 0) {
s = @"";
} else {
DYImageInfo *info;
DYImageCache *thumbsCache = appDelegate.thumbsCache;
if (count == 1) {
NSString *path = imgMatrix.firstSelectedFilename;
NSString *theFile = ResolveAliasToPath(path);
info = [thumbsCache infoForKey:theFile];
NSSize pixelSize;
off_t fileSize;
if (info) {
pixelSize = info->pixelSize;
fileSize = info->fileSize;
} else {
struct stat buf;
if (!stat(theFile.fileSystemRepresentation, &buf))
fileSize = buf.st_size;
else
fileSize = 0;
if (IsNotCGImage(theFile.pathExtension.lowercaseString)) {
NSImage *img = [[NSImage alloc] initByReferencingFile:theFile];
pixelSize = img ? img.size : NSZeroSize;
} else {
CGImageSourceRef imgSrc = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:theFile isDirectory:NO], NULL);
if (imgSrc) {
NSDictionary *opts = @{(__bridge NSString *)kCGImageSourceShouldCache: @NO};
CGImageRef ref = CGImageSourceCreateImageAtIndex(imgSrc, 0, (__bridge CFDictionaryRef)opts);
if (ref) {
pixelSize.width = CGImageGetWidth(ref);
pixelSize.height = CGImageGetHeight(ref);
CFRelease(ref);
}
CFRelease(imgSrc);
} else
pixelSize = NSZeroSize;
}
}
NSUInteger idx = [dirBrowserDelegate path].length+1;
NSString *fileName = idx > path.length ? path : [path substringFromIndex:idx];
s = [fileName stringByAppendingFormat:@" %dx%d (%@)",
(int)pixelSize.width, (int)pixelSize.height, FileSize2String(fileSize)];
} else {
unsigned long long totalSize = 0;
for (NSString *path in imgMatrix.selectedFilenames) {
NSString *theFile = ResolveAliasToPath(path);
info = [thumbsCache infoForKey:theFile];
if (info)
totalSize += info->fileSize;
else {
struct stat buf;
if (!stat(theFile.fileSystemRepresentation, &buf))
totalSize += buf.st_size;
}
}
s = [NSString stringWithFormat:@"%@ (%@)",
[NSString stringWithFormat:NSLocalizedString(@"%d images selected.", @""), (unsigned int)selectedIndexes.count],
FileSize2String(totalSize)];
}
}