forked from sbarex/SourceCodeSyntaxHighlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTI.swift
More file actions
221 lines (196 loc) · 6.99 KB
/
Copy pathUTI.swift
File metadata and controls
221 lines (196 loc) · 6.99 KB
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
//
// UTI.swift
// SyntaxHighlight
//
// Created by Sbarex on 15/11/2019.
// Copyright © 2019 sbarex. All rights reserved.
//
//
// This file is part of SyntaxHighlight.
// SyntaxHighlight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SyntaxHighlight is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SyntaxHighlight. If not, see <http://www.gnu.org/licenses/>.
import Cocoa
import UniformTypeIdentifiers
class UTI: Equatable {
typealias SuppressedExtension = (ext: String, uti: String)
static func == (lhs: UTI, rhs: UTI) -> Bool {
return lhs.UTI == rhs.UTI
}
let UTI: String
lazy var description: String = {
let type = self.UTI as CFString
if #available(macOS 11.0, *) {
if let desc = UTType(self.UTI)?.localizedDescription, !desc.isEmpty {
return desc.prefix(1).uppercased() + desc.dropFirst()
} else if let desc = UTType(self.UTI)?.description, !desc.isEmpty {
return desc
} else {
return self.UTI
}
} else {
if let desc = UTTypeCopyDescription(type)?.takeRetainedValue() as String?, !desc.isEmpty {
return desc.prefix(1).uppercased() + desc.dropFirst()
} else {
return self.UTI
}
// Fallback on earlier versions
}
}()
/// Full description with supported extensions.
lazy var fullDescription: String = {
var label: String = self.description
let exts = self.extensions
if exts.count > 0 {
label += " (." + exts.joined(separator: ", .") + ")"
}
return label
}()
lazy var extensions: [String] = {
if #available(macOS 11.0, *) {
return UTType(self.UTI)?.tags[UTTagClass.filenameExtension] ?? []
} else {
guard let tags = UTTypeCopyAllTagsWithClass(self.UTI as CFString, kUTTagClassFilenameExtension as CFString)?.takeRetainedValue() else {
return []
}
return tags as NSArray as? [String] ?? []
}
}()
lazy var suppressedExtensions: [SuppressedExtension] = {
var e: [SuppressedExtension] = []
for ext in extensions {
if #available(macOS 11.0, *) {
if let u = UTType(filenameExtension: ext) {
if u.identifier != self.UTI {
e.append((ext: ext, uti: u.identifier as String))
}
}
} else {
if let u = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, ext as CFString, nil)?.takeRetainedValue() {
if u as String != self.UTI {
e.append((ext: ext, uti: u as String))
}
}
}
}
return e
}()
var isSuppressed: Bool {
return self.extensions.count > 0 && self.suppressedExtensions.count == self.extensions.count
}
lazy var conformsTo: [String] = {
if #available(macOS 12.0, *) {
return UTType(UTI)?.supertypes.map { $0.identifier } ?? []
} else {
if let info = UTTypeCopyDeclaration(UTI as CFString)?.takeRetainedValue() as? [String: AnyObject] {
if let c = info[kUTTypeConformsToKey as String] as? String {
return [c]
} else if let c = info[kUTTypeConformsToKey as String] as? [String] {
return c
}
}
return []
}
}()
lazy var mimeTypes: [String] = {
if #available(macOS 12.0, *) {
if let t = UTType(self.UTI) {
return t.tags[.mimeType] ?? []
} else {
return []
}
} else {
guard let tags = UTTypeCopyAllTagsWithClass(self.UTI as CFString, kUTTagClassMIMEType as CFString)?.takeRetainedValue() else {
return []
}
return tags as NSArray as? [String] ?? []
}
}()
fileprivate var _image_fetched = false
fileprivate var _image: NSImage?
var isImageFetched: Bool {
return _image_fetched
}
var image: NSImage? {
return _image
}
func fetchIcon(async: Bool = true) {
if #available(macOS 11.0, *) {
guard !_image_fetched, let uti = UTType(self.UTI) else {
return
}
if async {
DispatchQueue.global(qos: .userInitiated).async() {
self._image = NSWorkspace.shared.icon(for: uti)
self._image_fetched = true
}
} else {
_image = NSWorkspace.shared.icon(for: uti)
_image_fetched = true
}
} else {
// FIXME: On Catalina NSWorkspace.shared.icon freeze the app!
}
}
lazy var isDynamic: Bool = {
if #available(macOS 12.0, *) {
if let u = UTType(UTI) {
return u.isDynamic
} else {
return false
}
} else {
return UTTypeIsDynamic(UTI as CFString)
}
}()
/// Return if the system know the file extensions or mime types for the UTI.
lazy var isValid: Bool = {
if UTI.hasPrefix("public.") {
return true
}
return mimeTypes.count > 0 || extensions.count > 0
}()
init(_ UTI: String) {
self.UTI = UTI
}
convenience init?(URL url: URL) {
if let uti = (try? url.resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier {
self.init(uti)
} else {
return nil
}
}
func getSuppressedExtensions(handledUti: [String]) -> [(suppress: SuppressedExtension, handled: Bool)] {
var e: [(suppress: SuppressedExtension, handled: Bool)] = []
for suppress in suppressedExtensions {
e.append((suppress: suppress, handled: handledUti.contains(suppress.uti)))
}
return e
}
func initLazyVars(async: Bool = true) {
let initVars = { () in
_ = self.description
_ = self.fullDescription
_ = self.extensions
_ = self.suppressedExtensions
_ = self.isDynamic
_ = self.isValid
}
if async {
DispatchQueue.global(qos: .userInitiated).async() {
initVars()
}
} else {
initVars()
}
}
}