forked from nst/CocoaSlideShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSSImageInfo.m
490 lines (375 loc) · 13.7 KB
/
CSSImageInfo.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
//
// CSSImageContainer.m
// CocoaSlideShow
//
// Created by Nicolas Seriot on 25.08.07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "CSSImageInfo.h"
#import "NSString+CSS.h"
#import "AppDelegate.h"
#import "NSFileManager+CSS.h"
#import "NSImage+CSS.h"
static NSString *const kMultipleSelectionAllowsEdition = @"MultipleSelectionAllowsEdition";
static NSSet *keyPathsForValuesAffectingFlagIcon = nil;
@implementation CSSImageInfo
+ (NSSet *)keyPathsForValuesAffectingFlagIcon {
if(keyPathsForValuesAffectingFlagIcon == nil) {
keyPathsForValuesAffectingFlagIcon = [NSSet setWithObject:@"isFlagged"];
}
return keyPathsForValuesAffectingFlagIcon;
}
/*
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSMutableSet *set = [NSMutableSet set];
if([key isEqualToString:@"flagIcon"]) {
[set addObject:@"isFlagged"];
}
return set;
}
*/
//+ (void)initialize {
// [self setKeys:[NSArray arrayWithObjects:@"isFlagged", nil] triggerChangeNotificationsForDependentKey:@"flagIcon"];
//}
- (void)setPath:(NSString *)aPath {
if(aPath == nil) {
NSLog(@"-- error: path is nil");
return;
}
if(path != aPath) {
path = aPath;
}
}
- (NSString *)path {
return path;
}
- (id)initWithPath:(NSString *)aPath {
self = [super init];
[self setPath:aPath];
return self;
}
+ (CSSImageInfo *)containerWithPath:(NSString *)aPath {
return [[CSSImageInfo alloc] initWithPath:aPath];
}
- (void)dealloc {
//NSLog(@"-- dealloc %@", path);
if(source) {
CFRelease(source);
source = nil;
}
}
- (NSMutableDictionary *)metadata {
if(!sourceWasRead) [self loadSource];
return metadata;
}
- (NSString *)exifDateTime {
NSDictionary *exif = [[self metadata] valueForKey:(NSString *)kCGImagePropertyExifDictionary];
return [exif valueForKey:(NSString *)kCGImagePropertyExifDateTimeOriginal];
}
- (NSString *)prettyLatitude {
NSDictionary *gps = [self gps];
if(!gps) return @"";
NSNumber *latitude = [gps valueForKey:@"Latitude"];
NSString *latitudeRef = [gps valueForKey:@"LatitudeRef"];
if(!latitude) return @"";
return [latitudeRef isEqualToString:@"S"] ? [@"-" stringByAppendingFormat:@"%@", latitude] : [latitude description];
}
- (NSString *)prettyLongitude {
NSDictionary *gps = [self gps];
if(!gps) return @"";
NSNumber *longitude = [gps valueForKey:@"Longitude"];
if(!longitude) return @"";
NSString *longitudeRef = [gps valueForKey:@"LongitudeRef"];
return [longitudeRef isEqualToString:@"W"] ? [@"-" stringByAppendingFormat:@"%@", longitude] : [longitude description];
}
// FIXME: not thread safe, source might be read while export and released too early while displaying map, @synchronized seems to kill performance though
- (BOOL)loadSource {
NSObject *appDelegate = [NSApp delegate];
BOOL isMap = [[appDelegate valueForKey:@"isMap"] boolValue];
BOOL isExporting = [[appDelegate valueForKey:@"isExporting"] boolValue];
BOOL multipleImagesSelected = [[appDelegate valueForKeyPath:@"imagesController.multipleImagesSelected"] boolValue];
BOOL readOnMultiSelect = [[NSUserDefaults standardUserDefaults] boolForKey:kMultipleSelectionAllowsEdition];
if(!readOnMultiSelect && multipleImagesSelected && !isMap && !isExporting) {
return NO;
}
if([self source] == NULL) return NO;
// fill caches
CFDictionaryRef metadataRef = CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
if(metadataRef) {
NSDictionary *immutableMetadata = (__bridge NSDictionary *)metadataRef;
metadata = [immutableMetadata mutableCopy];
CFRelease(metadataRef);
}
NSString *UTI = (NSString *)CGImageSourceGetType(source);
// CFRelease(source);
// source = nil;
[self willChangeValueForKey:@"isJpeg"];
isJpeg = [UTI isEqualToString:@"public.jpeg"];
[self didChangeValueForKey:@"isJpeg"];
return YES;
}
- (void)rotateLeft {
userRotation -= 90;
}
- (void)rotateRight {
userRotation += 90;
}
//// http://www.impulseadventure.com/photo/exif-orientation.html
//- (int)orientationDegrees {
// NSNumber *n = [[self metadata] valueForKey:@"Orientation"];
// NSLog(@"-- n: %@", n);
//
// NSUInteger i = [n unsignedIntValue];
// NSLog(@"------ %d", i);
//
// if(i == 1) return 0 + userRotation;
// if(i == 8) return 90 + userRotation;
// if(i == 3) return 180 + userRotation;
// if(i == 6) return 270 + userRotation;
//
// return userRotation;
//}
- (NSString *)fileName {
return [path lastPathComponent];
}
- (void)setFileName:(NSString *)s {
NSString *newPath = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:s];
if([[NSFileManager defaultManager] fileExistsAtPath:newPath]) return; // necessary?
NSError *error = nil;
if([[NSFileManager defaultManager] moveItemAtPath:path toPath:newPath error:&error]) {
[self setValue:newPath forKey:@"path"];
} else {
NSLog(@"-- cannot move:%@ to:%@, error: %@", path, newPath, error);
}
}
- (NSString *)latitude {
return [[self gps] valueForKey:(NSString *)kCGImagePropertyGPSLatitude];
}
- (NSString *)longitude {
return [[self gps] valueForKey:(NSString *)kCGImagePropertyGPSLongitude];
}
- (NSString *)latitudeRef {
return [[self gps] valueForKey:(NSString *)kCGImagePropertyGPSLatitudeRef];
}
- (NSString *)longitudeRef {
return [[self gps] valueForKey:(NSString *)kCGImagePropertyGPSLongitudeRef];
}
- (NSDictionary *)exif {
return [[self metadata] valueForKey:(NSString *)kCGImagePropertyExifDictionary];
}
- (NSDictionary *)iptc {
return [[self metadata] valueForKey:(NSString *)kCGImagePropertyIPTCDictionary];
}
- (NSDictionary *)gps {
return [[self metadata] valueForKey:(NSString *)kCGImagePropertyGPSDictionary];
}
- (CGImageSourceRef)source {
if(sourceWasRead) return source;
if(source) return source;
if(path == nil) return NULL;
NSURL *url = [NSURL fileURLWithPath:path];
if(url == nil) return NULL;
source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
if(source) {
sourceWasRead = YES;
return source;
}
CGImageSourceStatus status = CGImageSourceGetStatus(source);
NSLog(@"Error: could not create image source. Status: %d", status);
return NULL;
}
- (BOOL)saveSourceWithMetadata {
if([self source] == NULL) return NO;
NSData *data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)data, (CFStringRef)@"public.jpeg", 1, NULL);
if(!destination) {
NSLog(@"Error: could not create image destination");
if(source) {
CFRelease(source);
source = nil;
}
return NO;
}
CGImageDestinationAddImageFromSource(destination, source, 0, (CFDictionaryRef)metadata);
BOOL success = CGImageDestinationFinalize(destination); // write metadata into the data object
if(!success) {
NSLog(@"Error: could not finalize destination");
CFRelease(destination);
if(source) {
CFRelease(source);
source = nil;
}
return NO;
}
CFRelease(destination);
/*
if(source) {
CFRelease(source);
source = nil;
}
*/
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;
success = [data writeToURL:url options:NSAtomicWrite error:&error];
if(error) {
NSLog(@"-- error: can't write data: %@", [error localizedDescription]);
}
return success;
}
- (BOOL)isJpeg {
if(!sourceWasRead) [self loadSource];
return isJpeg;
}
- (NSString *)jsAddPoint {
NSString *latitude = [self prettyLatitude];
NSString *longitude = [self prettyLongitude];
if([latitude length] == 0 || [longitude length] == 0) return nil;
NSString *filePath = [self path];
NSString *fileName = [filePath lastPathComponent];
// NSDictionary *fileAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:filePath traverseLink:YES];
NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
if(fileAttributes == nil) {
NSLog(@"-- can't get attributesOfItemAtPath:%@ error:%@", filePath, error);
return nil;
}
NSString *fileModDateString = fileAttributes ? [[fileAttributes valueForKey:NSFileModificationDate] description] : @"";
return [NSString stringWithFormat:@"addPoint(\"h%lu\", %@, %@, \"%@\", \"%@\", \"%@\", %d);", [self hash], latitude, longitude, fileName, filePath, fileModDateString, 0];
}
- (NSString *)jsRemovePoint {
return [NSString stringWithFormat:@"removePoint(\"h%lu\");", [self hash]];
}
- (NSString *)jsShowPoint {
return [NSString stringWithFormat:@"showPoint(\"h%lu\");", (unsigned long)[self hash]];
}
- (NSString *)jsHidePoint {
return [NSString stringWithFormat:@"hidePoint(\"h%lu\");", [self hash]];
}
- (void)setUserComment:(NSString *)comment {
if(![self isJpeg]) return;
if(!sourceWasRead) [self loadSource];
[self willChangeValueForKey:@"userComment"];
[self willChangeValueForKey:@"exif"];
NSMutableDictionary *exifData = [[metadata valueForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
if(!exifData) {
exifData = [[NSMutableDictionary alloc] init];
}
[exifData setObject:comment forKey:(NSString *)kCGImagePropertyExifUserComment];
[metadata setObject:exifData forKey:(NSString *)kCGImagePropertyExifDictionary];
[self didChangeValueForKey:@"exif"];
[self didChangeValueForKey:@"userComment"];
BOOL success = [self saveSourceWithMetadata];
if(!success) {
NSLog(@"Error: can't set user comment");
}
return;
}
- (void)setKeywords:(NSArray *)keywords {
if(![self isJpeg]) return;
if(!sourceWasRead) [self loadSource];
[self willChangeValueForKey:@"keywords"];
NSMutableDictionary *iptcDict = [[self iptc] mutableCopy];
if(!iptcDict) {
iptcDict = [[NSMutableDictionary alloc] init];
}
[iptcDict setObject:keywords forKey:(NSString *)kCGImagePropertyIPTCKeywords];
[metadata setObject:iptcDict forKey:(NSString *)kCGImagePropertyIPTCDictionary];
[self didChangeValueForKey:@"keywords"];
BOOL success = [self saveSourceWithMetadata];
if(!success) {
NSLog(@"Error: can't set keywords");
}
}
- (NSArray *)keywords {
return [[self iptc] valueForKey:(NSString *)kCGImagePropertyIPTCKeywords];
}
- (NSString *)userComment {
return [[self exif] valueForKey:(NSString *)kCGImagePropertyExifUserComment];
}
- (NSString *)prettyGPS {
NSDictionary *gps = [self gps];
if(!gps) return nil;
NSString *latitude = [[gps valueForKey:(NSString *)kCGImagePropertyGPSLatitude] description];
NSString *longitude = [[gps valueForKey:(NSString *)kCGImagePropertyGPSLongitude] description];
NSString *latitudeRef = [gps valueForKey:(NSString *)kCGImagePropertyGPSLatitudeRef];
NSString *longitudeRef = [gps valueForKey:(NSString *)kCGImagePropertyGPSLongitudeRef];
if(!latitude || !longitude || !latitudeRef || !longitudeRef) return nil;
NSString *trimedLatitude = [latitude length] > 8 ? [latitude substringToIndex:8] : latitude;
NSString *trimedLongitude = [longitude length] > 8 ? [longitude substringToIndex:8] : longitude;
return [NSString stringWithFormat:@"%@ %@, %@ %@", trimedLatitude, latitudeRef, trimedLongitude, longitudeRef];
}
- (NSImage *)image {
//int orientationDegrees = [self orientationDegrees];
return [[[NSImage alloc] initByReferencingFile:path] rotatedWithAngle:0];
}
// just to appear to be KVC compliant, useful when droping an image on the imageView
- (void)setImage:(NSImage *)anImage {
//NSLog(@"-- setImage:%@", anImage);
}
- (NSURL *)googleMapsURL {
NSString *latitude = [self prettyLatitude];
NSString *longitude = [self prettyLongitude];
if([latitude length] == 0 || [longitude length] == 0) return nil;
NSString *s = [NSString stringWithFormat:@"http://maps.google.com/?q=%@,%@", latitude, longitude];
return [NSURL URLWithString:s];
}
- (NSString *)prettyImageSize {
NSString *x = [[self exif] valueForKey:(NSString *)kCGImagePropertyExifPixelXDimension];
NSString *y = [[self exif] valueForKey:(NSString *)kCGImagePropertyExifPixelYDimension];
if(x && y) {
return [NSString stringWithFormat:@"%@x%@", x, y];
}
return nil;
}
- (NSString *)prettyFileSize {
return [[NSFileManager defaultManager] prettyFileSize:path];
}
- (void)flag {
[self setValue:[NSNumber numberWithBool:YES] forKey:@"isFlagged"];
}
- (void)unflag {
[self setValue:[NSNumber numberWithBool:NO] forKey:@"isFlagged"];
}
- (void)toggleFlag {
[self setValue:[NSNumber numberWithBool:!isFlagged] forKey:@"isFlagged"];
}
- (void)removeFlag {
[self setValue:[NSNumber numberWithBool:NO] forKey:@"isFlagged"];
}
- (BOOL)isFlagged {
return isFlagged;
}
- (NSImage *)flagIcon {
return isFlagged ? [NSImage imageNamed:@"Flagged.png"] : nil;
}
- (void)copyToDirectory:(NSURL *)destDirectoryURL {
if([destDirectoryURL isFileURL] == NO) return;
NSString *destPath = [[destDirectoryURL path] stringByAppendingPathComponent:[path lastPathComponent]];
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:path]) {
NSError *error = nil;
BOOL success = [fm copyItemAtPath:path toPath:destPath error:&error];
if(success == NO) {
NSLog(@"-- cannot copyItemAtPath:%@ toPath:%@ error:%@", path, destPath, error);
}
}
}
- (BOOL)moveToTrash {
AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSUndoManager *undoManager = [appDelegate undoManager];
[undoManager registerUndoWithTarget:appDelegate
selector:@selector(moveFromTrashToPath:)
object:path];
[undoManager setActionName:@"Move To Trash"];
NSString *trashPath = [[@"~/.Trash/" stringByExpandingTildeInPath] stringByAppendingPathComponent:[path lastPathComponent]];
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:path toPath:trashPath error:&error];
if(success == NO) {
NSLog(@"-- cannot move:%@ to:%@, error: %@", path, trashPath, error);
}
return success;
}
- (void)revealInFinder {
[[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:@""];
}
@end