/
UTI.swift
256 lines (203 loc) · 9.03 KB
/
UTI.swift
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
// The MIT License (MIT)
//
// Copyright (c) 2015 Hiroki Kato
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Foundation
#if os(iOS)
import MobileCoreServices
#endif
public struct UTI: CustomStringConvertible, CustomDebugStringConvertible, Equatable {
public let UTIString: String
// MARK: - Initialize
public init(_ UTIString: String) {
self.UTIString = UTIString
}
public init?(filenameExtension: String, conformingToUTI: UTI? = nil) {
let conformingToUTIString: CFString? = conformingToUTI?.UTIString
guard let rawUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filenameExtension, conformingToUTIString)?.takeRetainedValue() else { return nil }
self.UTIString = rawUTI as String
}
public init?(MIMEType: String, conformingToUTI: UTI? = nil) {
let conformingToUTIString: CFString? = conformingToUTI?.UTIString
guard let rawUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, MIMEType, conformingToUTIString)?.takeRetainedValue() else { return nil }
self.UTIString = rawUTI as String
}
#if os(OSX)
public init?(pasteBoardType: String, conformingToUTI: UTI? = nil) {
let conformingToUTIString: CFString? = conformingToUTI?.UTIString
guard let rawUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, pasteBoardType, conformingToUTIString)?.takeRetainedValue() else { return nil }
self.UTIString = rawUTI as String
}
public init?(OSType: String, conformingToUTI: UTI? = nil) {
let conformingToUTIString: CFString? = conformingToUTI?.UTIString
guard let rawUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassOSType, OSType, conformingToUTIString)?.takeRetainedValue() else { return nil }
self.UTIString = rawUTI as String
}
#endif
// MARK: -
private static func UTIsForTagClass(_ tagClass: String, tag: String, conformingToUTI: UTI?) -> [UTI] {
let conformingToUTIString: CFString? = conformingToUTI?.UTIString
guard let rawUTIs = UTTypeCreateAllIdentifiersForTag(tagClass, tag, conformingToUTIString)?.takeRetainedValue() else { return [] }
return (rawUTIs as NSArray as? [String] ?? []).map { UTI($0) }
}
public static func UTIsFromFilenameExtension(_ filenameExtension: String, conformingToUTI: UTI? = nil) -> [UTI] {
return UTIsForTagClass(kUTTagClassFilenameExtension as String, tag: filenameExtension, conformingToUTI: conformingToUTI)
}
public static func UTIsFromMIMEType(_ MIMEType: String, conformingToUTI: UTI? = nil) -> [UTI] {
return UTIsForTagClass(kUTTagClassMIMEType as String, tag: MIMEType, conformingToUTI: conformingToUTI)
}
#if os(OSX)
public static func UTIsFromPasteBoardType(pasteBoardType: String, conformingToUTI: UTI? = nil) -> [UTI] {
return UTIsForTagClass(kUTTagClassNSPboardType as String, tag: pasteBoardType, conformingToUTI: conformingToUTI)
}
public static func UTIsFromOSType(OSType: String, conformingToUTI: UTI? = nil) -> [UTI] {
return UTIsForTagClass(kUTTagClassOSType as String, tag: OSType, conformingToUTI: conformingToUTI)
}
#endif
// MARK: - Tags
private func tagWithClass(_ tagClass: String) -> String? {
return UTTypeCopyPreferredTagWithClass(UTIString, tagClass)?.takeRetainedValue() as String?
}
@available(OSX, introduced:10.10)
@available(iOS, introduced:8.0)
private func tagsWithClass(_ tagClass: String) -> [String] {
guard let tags = UTTypeCopyAllTagsWithClass(UTIString, tagClass)?.takeRetainedValue() else { return [] }
return tags as NSArray as? [String] ?? []
}
public var filenameExtension: String? {
return tagWithClass(kUTTagClassFilenameExtension as String)
}
@available(OSX, introduced:10.10)
@available(iOS, introduced:8.0)
public var filenameExtensions: [String] {
return tagsWithClass(kUTTagClassFilenameExtension as String)
}
public var MIMEType: String? {
return tagWithClass(kUTTagClassMIMEType as String)
}
@available(OSX, introduced:10.10)
@available(iOS, introduced:8.0)
public var MIMETypes: [String] {
return tagsWithClass(kUTTagClassMIMEType as String)
}
#if os(OSX)
public var pasteBoardType: String? {
return tagWithClass(kUTTagClassNSPboardType as String)
}
@available(OSX, introduced:10.10)
public var pasteBoardTypes: [String] {
return tagsWithClass(kUTTagClassNSPboardType as String)
}
public var OSType: String? {
return tagWithClass(kUTTagClassOSType as String)
}
@available(OSX, introduced:10.10)
public var OSTypes: [String] {
return tagsWithClass(kUTTagClassOSType as String)
}
#endif
// MARK: - Status
@available(OSX, introduced:10.10)
@available(iOS, introduced:8.0)
public var isDeclared: Bool {
return UTTypeIsDeclared(UTIString)
}
@available(OSX, introduced:10.10)
@available(iOS, introduced:8.0)
public var isDynamic: Bool {
return UTTypeIsDynamic(UTIString)
}
// MARK: - Declaration
public struct Declaration: CustomStringConvertible, CustomDebugStringConvertible {
private let raw: [ NSObject: AnyObject ]
public var exportedTypeDeclarations: [Declaration] {
return (raw[kUTExportedTypeDeclarationsKey] as? [[ NSObject : AnyObject ]] ?? []).map { Declaration(declaration: $0) }
}
public var importedTypeDeclarations: [Declaration] {
return (raw[kUTImportedTypeDeclarationsKey] as? [[ NSObject : AnyObject ]] ?? []).map { Declaration(declaration: $0) }
}
public var identifier: String? {
return raw[kUTTypeIdentifierKey] as? String
}
public var tagSpecification: [ NSObject : AnyObject ] {
return raw[kUTTypeTagSpecificationKey] as? [ NSObject : AnyObject ] ?? [:]
}
public var conformsTo: [UTI] {
switch raw[kUTTypeConformsToKey] {
case let array as [String]:
return array.map { UTI($0) }
case let string as String:
return [ UTI(string) ]
default:
return []
}
}
public var iconFile: String? {
return raw[kUTTypeIconFileKey] as? String
}
public var referenceURL: URL? {
if let reference = raw[kUTTypeReferenceURLKey] as? String {
return URL(string: reference)
}
return nil
}
public var version: String? {
return raw[kUTTypeIconFileKey] as? String
}
init(declaration: [ NSObject : AnyObject ]) {
self.raw = declaration
}
public var description: String {
return raw.description
}
public var debugDescription: String {
return raw.debugDescription
}
}
public var declaration: Declaration {
return Declaration(declaration: UTTypeCopyDeclaration(self.UTIString)?.takeRetainedValue() as [NSObject: AnyObject]? ?? [:])
}
public var declaringBundle: Bundle? {
if let url = UTTypeCopyDeclaringBundleURL(UTIString)?.takeRetainedValue() {
return Bundle(url: url as URL)
}
return nil
}
public var iconFileURL: URL? {
if let iconFile = declaration.iconFile {
return self.declaringBundle?.urlForResource(iconFile, withExtension: nil) ??
self.declaringBundle?.urlForResource(iconFile, withExtension: "icns")
}
return nil
}
// MARK: - Printable, DebugPrintable
public var description: String {
return UTTypeCopyDescription(UTIString)?.takeRetainedValue() as? String ?? UTIString
}
public var debugDescription: String {
return UTIString
}
}
public func ==(lhs: UTI, rhs: UTI) -> Bool {
return UTTypeEqual(lhs.UTIString, rhs.UTIString)
}
public func ~=(pattern: UTI, value: UTI) -> Bool {
return UTTypeConformsTo(value.UTIString, pattern.UTIString)
}