-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleViewController.m
349 lines (283 loc) · 9.9 KB
/
SingleViewController.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
//
// SingleViewController.m
// lutrin
//
// Created by Shigeru Hagiwara on 2012/12/30.
//
//
#import "RegexKitLite.h"
#import "SingleViewController.h"
#import "LutrinWindowController.h"
@interface SingleViewController (Utils)
@end
@implementation SingleViewController
@synthesize windowController;
@synthesize imageView;
@synthesize imageProperties;
@synthesize currentFile;
@synthesize fileList;
@synthesize cacheDir;
- (id)initWithWindowController:(LutrinWindowController *)_windowController
nibName:(NSString *)nibNameOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nil];
if (self) {
windowController = _windowController;
[self setupCacheDir];
}
return self;
}
- (id)initWithWindowController:(LutrinWindowController *)_windowController
{
return [self initWithWindowController:_windowController
nibName:@"SingleViewController"];
}
- (void)dealloc
{
[imageProperties release];
[currentFile release];
[fileList release];
[cacheDir release];
[super dealloc];
}
- (IBAction)openFile:(id)sender
{
NSString *extensions =
@"tiff/tif/TIFF/TIF/bmp/BMP/jpg/jpeg/JPG/JPEG/gif/GIF/png/PNG/zip/ZIP";
NSArray *types = [extensions pathComponents];
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowedFileTypes:types];
[openPanel setCanSelectHiddenExtension:YES];
[openPanel beginSheetModalForWindow:self.windowController.window
completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
[self openFileImpl:[openPanel URL]];
}
}];
}
- (void)openFileImpl:(NSURL *)url
{
NSString *ext = [url.pathExtension uppercaseString];
if ([ext isEqualToString:@"ZIP"]) {
[self unzipFile:[url path]];
[self updateFileListAt:self.cacheDir recursively:YES];
if (self.fileList.count > 0) {
[self openImageURL:(NSURL *)[self.fileList objectAtIndex:0]];
}
} else {
[self updateFileListAt:[url URLByDeletingLastPathComponent]
recursively:NO];
[self openImageURL:url];
}
}
- (void)openImageURL:(NSURL *)url
{
self.currentFile = url;
if (url == nil) {
[self loadImageTo:self.imageView URL:[self transparentImage]
properties:&imageProperties];
[self.windowController setWindowTitle:@"no image"];
return;
}
[self loadImageTo:self.imageView URL:url properties:&imageProperties];
NSString *title = [NSString stringWithFormat:@"%@ (%ld/%ld)",
[url lastPathComponent],
([self getFileIndex] + 1), self.fileList.count];
[self.windowController setWindowTitle:title];
}
- (void)loadImageTo:(IKImageView *)imageView_
URL:(NSURL *)url_
properties:(NSDictionary **)imagePropRef_
{
CGImageRef image = NULL;
CGImageSourceRef isr = CGImageSourceCreateWithURL((CFURLRef)url_, NULL);
if (isr) {
NSDictionary *options =
[NSDictionary dictionaryWithObject:(id)kCFBooleanTrue
forKey:(id)kCGImageSourceShouldCache];
image = CGImageSourceCreateImageAtIndex(isr, 0, (CFDictionaryRef)options);
if (image) {
*imagePropRef_ = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(isr, 0, (CFDictionaryRef)(*imagePropRef_));
}
CFRelease(isr);
}
if (image) {
imageView_.autoresizes = YES;
// [imageView_ setImage:image imageProperties:*imagePropRef_];
[imageView_ setImageWithURL:url_];
NSNumber *pixelHeight = [*imagePropRef_ valueForKey:@"PixelHeight"];
NSNumber *pixelWidth = [*imagePropRef_ valueForKey:@"PixelWidth"];
if (imageView_.frame.size.height < pixelHeight.floatValue ||
imageView_.frame.size.width < pixelWidth.floatValue) {
[imageView_ zoomImageToFit:nil];
} else {
[imageView_ zoomImageToActualSize:nil];
}
imageView_.autoresizes = NO;
CGImageRelease(image);
}
}
- (void)windowDidResize:(NSNotification *)notification
{
[self.imageView zoomImageToFit:self];
}
- (void)clearImageView
{
[self loadImageTo:self.imageView URL:[self transparentImage]
properties:&imageProperties];
}
- (void)updateFileListAt:(NSURL *)directory recursively:(BOOL)doRecursively
{
if (directory == nil) {
self.fileList = [NSArray array];
return;
}
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *urls;
if (doRecursively) {
urls = [self scanURLImages:directory];
} else {
urls = [fm contentsOfDirectoryAtURL:directory
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
}
NSPredicate *predicate =
[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [[[(NSURL *)evaluatedObject pathExtension] uppercaseString]
isMatchedByRegex:@"(TIFF|TIF|BMP|JPG|JPEG|GIF|PNG)"];
}];
self.fileList = [[urls filteredArrayUsingPredicate:predicate]
sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSURL *a = (NSURL *)obj1;
NSURL *b = (NSURL *)obj2;
return [a.lastPathComponent compare:b.lastPathComponent];
}];
}
- (NSMutableArray *)scanURLImages:(NSURL *)directoryToScan
{
NSMutableArray *urls = [NSMutableArray array];
NSFileManager *fm = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnumerator =
[fm enumeratorAtURL:directoryToScan
includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLIsDirectoryKey,nil]
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:nil];
for (NSURL *theURL in dirEnumerator) {
NSNumber *isDirectory;
[theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
NSString *ext = [[(NSURL *)theURL pathExtension] uppercaseString];
if ([isDirectory boolValue] == NO &&
[ext isMatchedByRegex:@"(TIFF|TIF|BMP|JPG|JPEG|GIF|PNG)"]) {
[urls addObject:theURL];
}
}
return urls;
}
- (NSUInteger)getFileIndex
{
NSUInteger index = [self.fileList indexOfObject:self.currentFile];
if (index == NSNotFound) {
NSLog(@"not found %@ in file list", self.currentFile);
index = 0;
}
return index;
}
- (IBAction)nextFile: (id)sender
{
if (self.fileList.count == 0) return;
NSUInteger index = [self getFileIndex];
index += 1;
if (index >= self.fileList.count)
index = 0;
NSURL *url = (NSURL *)[self.fileList objectAtIndex:index];
if (url)
[self openImageURL:url];
}
- (IBAction)prevFile: (id)sender
{
if (self.fileList.count == 0) return;
NSUInteger index = [self getFileIndex];
if (index == 0)
index = self.fileList.count - 1;
else
index -= 1;
NSURL *url = (NSURL *)[self.fileList objectAtIndex:index];
if (url)
[self openImageURL:url];
}
- (IBAction)firstFile: (id)sender
{
if (self.fileList.count == 0) return;
NSURL *url = (NSURL *)[self.fileList objectAtIndex:0];
if (url)
[self openImageURL:url];
}
- (IBAction)lastFile: (id)sender
{
if (self.fileList.count == 0) return;
NSURL *url = (NSURL *)[self.fileList objectAtIndex:(self.fileList.count - 1)];
if (url)
[self openImageURL:url];
}
- (IBAction)zoomActualSize: (id)sender
{
[self.imageView zoomImageToActualSize:sender];
}
- (IBAction)zoomFitToWindow: (id)sender
{
[self.imageView zoomImageToFit:sender];
}
- (IBAction)zoomIn: (id)sender
{
[self.imageView zoomIn:sender];
}
- (IBAction)zoomOut: (id)sender
{
[self.imageView zoomOut:sender];
}
- (void)setupCacheDir {
NSFileManager *fm = [NSFileManager defaultManager];
NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
NSArray *urls = [fm URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
self.cacheDir = [(NSURL *)[urls lastObject] URLByAppendingPathComponent:appBundleID];
[fm createDirectoryAtPath:[self.cacheDir path] withIntermediateDirectories:YES
attributes:nil error:nil];
}
- (void)unzipFile:(NSString *)path
{
NSLog(@"self.cacheDir = %@", self.cacheDir);
// find {self.cacheDir} -type d -exec chmod 755 {} \;
NSTask *findAndChmod = [[NSTask alloc] init];
[findAndChmod setLaunchPath:@"/usr/bin/find"];
[findAndChmod setCurrentDirectoryPath:[self.cacheDir path]];
[findAndChmod setArguments:[NSArray arrayWithObjects:[self.cacheDir path], @"-type", @"d", @"-exec", @"chmod", @"755", @"{}", @";", nil]];
[findAndChmod launch];
[findAndChmod waitUntilExit];
int findAndChmodStatus = [findAndChmod terminationStatus];
[findAndChmod release];
if (findAndChmodStatus != 0)
NSLog(@"find %@ -type d -exec chmod 755 {} ; failed.", [self.cacheDir path]);
// rm -rf {self.cacheDir}
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtURL:self.cacheDir error:nil];
[fm createDirectoryAtPath:[self.cacheDir path] withIntermediateDirectories:YES
attributes:nil error:nil];
// unzip
NSArray *arguments = [NSArray arrayWithObject:path];
NSTask *unzipTask = [[NSTask alloc] init];
[unzipTask setLaunchPath:@"/usr/bin/unzip"];
[unzipTask setCurrentDirectoryPath:[self.cacheDir path]];
[unzipTask setArguments:arguments];
[unzipTask launch];
[unzipTask waitUntilExit];
int status = [unzipTask terminationStatus];
[unzipTask release];
if (status != 0)
NSLog(@"unzip %@ failed.", path);
}
- (NSURL *)transparentImage
{
return [[NSBundle mainBundle] URLForResource:@"transparent1x1" withExtension:@"png"];
}
@end