OSDN Git Service

isEqualをより正確に
[kcd/KCD.git] / KCD / HMScreenshotInformation.swift
1 //
2 //  HMScreenshotInformation.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/12/23.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10 import Quartz
11
12 var formatter: NSDateFormatter?
13
14 class HMScreenshotInformation: NSObject
15 {
16         override class func initialize() {
17                 formatter = NSDateFormatter()
18                 formatter?.dateStyle = .ShortStyle
19                 formatter?.timeStyle = .ShortStyle
20                 formatter?.doesRelativeDateFormatting = true
21         }
22         
23         init(path aPath: String) {
24                 path = aPath
25                 super.init()
26         }
27         
28         var path: String
29         var version: UInt = 0
30         
31         lazy var url: NSURL? = {
32                 return NSURL.fileURLWithPath(self.path)
33         }()
34         lazy var creationDate: NSDate? = {
35                 let fm = NSFileManager.defaultManager()
36                 let fileAttr: NSDictionary = fm.attributesOfItemAtPath(self.path, error: nil)!
37                 return fileAttr.fileCreationDate()
38         }()
39         
40         var imageUID: String {
41                 return path
42         }
43         var imageRepresentationType: String = IKImageBrowserQuickLookPathRepresentationType
44         var imageRepresentation: AnyObject {
45                 return path
46         }
47         var imageTitle: String {
48                 return path.lastPathComponent.stringByDeletingPathExtension
49         }
50         var imageSubtitle: String? {
51                 if creationDate == nil {
52                         return nil
53                 }
54                 return formatter!.stringFromDate(creationDate!)
55         }
56         var imageVersion: UInt {
57                 return version
58         }
59         
60         var tags: [String]? {
61                 get {
62                         if url == nil {
63                                 return nil
64                         }
65                         var error: NSError? = nil
66                         var resource: AnyObject? = nil
67                         if !url!.getResourceValue(&resource, forKey: NSURLTagNamesKey, error: &error) {
68                                 if error != nil {
69                                         println("get tags error -> \(error)")
70                                 }
71                         }
72                         
73                         return resource as? [String]
74                 }
75                 set {
76                         if url == nil {
77                                 return
78                         }
79                         var error: NSError? = nil
80                         url!.setResourceValue(newValue, forKey: NSURLTagNamesKey, error: &error)
81                         if error != nil {
82                                 println("set tags error -> \(error)")
83                         }
84                 }
85         }
86         
87         override var hash: Int {
88                 return path.hash
89         }
90         override func isEqual(object: AnyObject?) -> Bool {
91                 if super.isEqual(object) { return true }
92                 let info = object as? HMScreenshotInformation
93                 if info == nil { return  false }
94                 return path.isEqual(object!.path)
95         }
96 }