OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMScreenshotInformation.m
1 //
2 //  HMScreenshotInformation.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/11/09.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMScreenshotInformation.h"
10
11 #import <Quartz/Quartz.h>
12
13
14 static NSDateFormatter *formatter = nil;
15
16 @interface HMScreenshotInformation ()
17 @property (nonatomic, strong) NSURL *url;
18 @end
19
20 @implementation HMScreenshotInformation
21 @synthesize creationDate = _creationDate;
22 @synthesize url = _url;
23
24 + (void)initialize
25 {
26         static dispatch_once_t onceToken;
27         dispatch_once(&onceToken, ^{
28                 formatter = [NSDateFormatter new];
29                 formatter.dateStyle = NSDateFormatterShortStyle;
30                 formatter.timeStyle = NSDateFormatterShortStyle;
31                 formatter.doesRelativeDateFormatting = YES;
32         });
33 }
34 - (NSString *)imageUID
35 {
36         return self.path;
37 }
38 - (NSString *)imageRepresentationType
39 {
40         return IKImageBrowserQuickLookPathRepresentationType;
41 }
42 - (id)imageRepresentation
43 {
44         return self.path;
45 }
46 - (NSString *)imageTitle
47 {
48         return self.path.lastPathComponent.stringByDeletingPathExtension;
49 }
50 - (NSString *)imageSubtitle
51 {
52         return [formatter stringFromDate:self.creationDate];
53 }
54 - (NSUInteger)imageVersion
55 {
56         return self.version;
57 }
58
59 - (NSDate *)creationDate
60 {
61         if(!_creationDate) {
62                 NSFileManager *fm = [NSFileManager defaultManager];
63                 NSDictionary *fileAttr = [fm attributesOfItemAtPath:self.path error:NULL];
64                 _creationDate = [fileAttr fileCreationDate];
65         }
66         return _creationDate;
67 }
68 - (void)setCreationDate:(NSDate *)creationDate
69 {
70         _creationDate = creationDate;
71 }
72 - (NSURL *)url
73 {
74         if(_url) return _url;
75         _url = [NSURL fileURLWithPath:self.path];
76         return _url;
77 }
78
79 - (NSArray *)tags
80 {
81         NSError *error = nil;
82         NSArray *tags;
83         if(![self.url getResourceValue:&tags forKey:NSURLTagNamesKey error:&error]) {
84                 if(error) {
85                         NSLog(@"get tags error -> %@", error);
86                         return @[];
87                 }
88         }
89         return tags;
90 }
91 - (void)setTags:(NSArray *)tags
92 {
93         NSError *error = nil;
94         [self.url setResourceValue:tags forKey:NSURLTagNamesKey error:&error];
95         if(error) {
96                 NSLog(@"set tags error -> %@", error);
97         }
98 }
99
100
101 - (NSUInteger)hash
102 {
103         return [self.path hash];
104 }
105 - (BOOL)isEqual:(id)object
106 {
107         if(![object isMemberOfClass:[self class]]) return NO;
108         
109         HMScreenshotInformation *obj = object;
110         return [self.path isEqual:obj.path];
111 }
112
113 #pragma mark - NSCoding
114 - (void)encodeWithCoder:(NSCoder *)aCoder
115 {
116         [aCoder encodeObject:self.path forKey:@"HMScreenshotInformationPathKey"];
117         [aCoder encodeDouble:self.creationDate.timeIntervalSince1970 forKey:@"HMScreenshotInformationCreationDateKey"];
118         [aCoder encodeInt64:self.version forKey:@"HMScreenshotInformationVersionKey"];
119 }
120 - (id)initWithCoder:(NSCoder *)aDecoder
121 {
122         self = [super init];
123         if(self) {
124                 _path = [aDecoder decodeObjectForKey:@"HMScreenshotInformationPathKey"];
125                 NSTimeInterval t = [aDecoder decodeDoubleForKey:@"HMScreenshotInformationCreationDateKey"];
126                 _creationDate = [NSDate dateWithTimeIntervalSince1970:t];
127                 _version = [aDecoder decodeInt64ForKey:@"HMScreenshotInformationVersionKey"];
128         }
129         return self;
130 }
131
132 @end