KMBookmark.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. //
  2. // KMBookmark.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/2/5.
  6. //
  7. import Cocoa
  8. let kType = "type"
  9. let kChildren = "children"
  10. let kLabel = "label"
  11. let kPageIndex = "pageIndex"
  12. let kBDAlias = "_BDAlias"
  13. enum KMBookmarkType: String {
  14. case bookmark = "bookmark"
  15. case folder = "folder"
  16. case session = "session"
  17. case setup = "setup"
  18. case separator = "separator"
  19. case file = "file"
  20. }
  21. class KMBookmark: NSObject {
  22. var properties: [String: Any]? {
  23. get {
  24. var data: [String: Any] = documentSetup
  25. var bdAlias: SKAlias?
  26. if self.fileURL != nil {
  27. bdAlias = SKAlias.init(url: self.fileURL)
  28. }
  29. let tempSetup = [kType: bookmarkType.rawValue,
  30. kBDAlias: self.aliasData ?? (bdAlias != nil ? bdAlias?.data: NSData()),
  31. kPageIndex: NSNumber(integerLiteral: Int(pageIndex)),
  32. kLabel: self.label
  33. ] as [String : Any]
  34. data.merge(tempSetup) { (_, new) in new }
  35. return data
  36. }
  37. set {
  38. }
  39. }
  40. var bookmarkType: KMBookmarkType = .bookmark
  41. var label: String = ""
  42. var alternateIcon: NSImage = NSImage()
  43. var fileURL: URL?
  44. var pageIndex: UInt = 0
  45. var pageNumber: NSNumber = 0
  46. var documentSetup: [String: Any] = [:] {
  47. didSet {
  48. if documentSetup[kType] != nil {
  49. self.bookmarkType = KMBookmarkType(rawValue: documentSetup[kType] as! String ) ?? .bookmark
  50. }
  51. let string: String = documentSetup[kPageIndex] as? String ?? "0"
  52. let intValue: UInt = UInt(string) ?? 0
  53. self.aliasData = documentSetup[kBDAlias] as? NSData
  54. self.pageIndex = intValue
  55. if documentSetup[kLabel] != nil {
  56. self.label = documentSetup[kLabel] as! String
  57. }
  58. }
  59. }
  60. var parent: KMBookmark?
  61. var children: [KMBookmark] = [] //子
  62. var aliasData: NSData?
  63. var icon: NSImage {
  64. get {
  65. if let fileURL = self.fileURL {
  66. return NSWorkspace.shared.icon(forFile: fileURL.path)
  67. } else {
  68. return KMFileBookmark.missingFileImage()
  69. }
  70. }
  71. set {
  72. }
  73. }
  74. static func bookmark(url: URL, pageIndex: UInt, label: String) -> KMBookmark {
  75. let bookmark = KMBookmark()
  76. bookmark.fileURL = url
  77. bookmark.pageIndex = pageIndex
  78. bookmark.label = label
  79. return bookmark
  80. }
  81. static func bookmarks(urls: [URL]) -> [KMBookmark] {
  82. var bookmarks: [KMBookmark] = []
  83. for url in urls {
  84. let bookmark = KMBookmark()
  85. bookmark.fileURL = url
  86. bookmarks.append(bookmark)
  87. }
  88. return bookmarks
  89. }
  90. func initWithProperties(properties: NSDictionary) -> KMBookmark {
  91. return KMBookmark()
  92. }
  93. // - (id)initWithProperties:(NSDictionary *)dictionary {
  94. // NSString *type = [dictionary objectForKey:TYPE_KEY];
  95. // if ([type isEqualToString:SEPARATOR_STRING]) {
  96. // return (id)[[SKSeparatorBookmark alloc] init];
  97. // } else if ([type isEqualToString:FOLDER_STRING] || [type isEqualToString:SESSION_STRING]) {
  98. // Class bookmarkClass = [type isEqualToString:FOLDER_STRING] ? [SKFolderBookmark class] : [SKSessionBookmark class];
  99. // NSMutableArray *newChildren = [NSMutableArray array];
  100. // SKBookmark *child;
  101. // for (NSDictionary *dict in [dictionary objectForKey:CHILDREN_KEY]) {
  102. // if ((child = [[SKBookmark alloc] initWithProperties:dict])) {
  103. // [newChildren addObject:child];
  104. // [child release];
  105. // } else
  106. // NSLog(@"Failed to read child bookmark: %@", dict);
  107. // }
  108. // return (id)[[bookmarkClass alloc] initFolderWithChildren:newChildren label:[dictionary objectForKey:LABEL_KEY]];
  109. // } else if ([dictionary objectForKey:@"windowFrame"]) {
  110. // return (id)[[SKFileBookmark alloc] initWithSetup:dictionary label:[dictionary objectForKey:LABEL_KEY]];
  111. // } else {
  112. // NSNumber *pageIndex = [dictionary objectForKey:PAGEINDEX_KEY];
  113. // return (id)[[SKFileBookmark alloc] initWithAliasData:[dictionary objectForKey:ALIASDATA_KEY] pageIndex:(pageIndex ? [pageIndex unsignedIntegerValue] : NSNotFound) label:[dictionary objectForKey:LABEL_KEY]];
  114. // }
  115. // }
  116. static func bookmark(properties: [String: Any]) -> KMBookmark? {
  117. guard let type: KMBookmarkType = KMBookmarkType(rawValue: properties[kType] as! String) else { return nil}
  118. var bookmark: KMBookmark = KMBookmark()
  119. if type == .separator {
  120. bookmark = KMSeparatorBookmark()
  121. } else if type == .folder || type == .session {
  122. let bookmarkClass: AnyClass = (type == .folder) ? KMFolderBookmark.self : KMSessionBookmark.self
  123. guard let childrenProperties = properties[kChildren] as? [[String: Any]] else { return nil }
  124. var newChildren = [KMBookmark]()
  125. for dict: [String: Any] in childrenProperties {
  126. if let child = KMBookmark.bookmark(properties: dict) {
  127. newChildren.append(child)
  128. } else {
  129. NSLog("Failed to read child bookmark: \(dict)")
  130. }
  131. }
  132. guard let label = properties[kLabel] as? String else { return nil }
  133. bookmark = KMFolderBookmark.bookmark(children: newChildren, label: label)
  134. } else if properties["windowFrame"] != nil {
  135. guard let label = properties[kLabel] as? String else { return nil }
  136. bookmark = KMFileBookmark.bookmark(setup: properties, label: label)
  137. bookmark.label = label
  138. } else {
  139. let pageIndex = properties[kPageIndex] as? UInt ?? UInt(NSNotFound)
  140. let label = properties[kLabel] as? String
  141. let aliasData = properties[kBDAlias] as? Data
  142. if aliasData != nil {
  143. bookmark = KMFileBookmark.bookmark(aliasData: aliasData! as NSData, pageIndex: Int(pageIndex), label: label!)
  144. }
  145. }
  146. return bookmark
  147. }
  148. static func bookmark(children: [KMBookmark], label: String) -> KMBookmark {
  149. let bookmark = KMFolderBookmark()
  150. return bookmark
  151. }
  152. static func bookmark(url: [KMBookmark], label: String) -> KMBookmark {
  153. let bookmark = KMFolderBookmark()
  154. return bookmark
  155. }
  156. static func bookmark(aliasData: NSData, pageIndex: Int, label: String) -> KMBookmark {
  157. let bookmark = KMFileBookmark()
  158. let alias = SKAlias.init(data: aliasData as Data)
  159. let url = alias?.fileURL
  160. bookmark.fileURL = url
  161. bookmark.aliasData = aliasData
  162. return bookmark
  163. }
  164. static func bookmark(setup: [String: Any], label: String) -> KMBookmark {
  165. let pageIndex = setup[kPageIndex] as? UInt ?? UInt(NSNotFound)
  166. let aliasData = setup[kBDAlias] as? Data
  167. let bookmark = KMFileBookmark.bookmark(aliasData: aliasData! as NSData, pageIndex: Int(pageIndex), label: label)
  168. return bookmark
  169. }
  170. // func resolveAliasData(aliasData: Data) -> URL? {
  171. // var resolvedURL: URL?
  172. // do {
  173. // // 尝试创建NSURL实例
  174. // let aliasURL = URL(dataRepresentation: aliasData, relativeTo: nil)
  175. //
  176. // // 解析别名并获取原始路径
  177. // try aliasURL?.resolveAliasFileURL()
  178. //
  179. // // 将NSURL转换为URL
  180. // if let resolvedFileURL = aliasURL as URL? {
  181. // resolvedURL = resolvedFileURL
  182. // }
  183. // } catch {
  184. // print("Error resolving alias data:", error)
  185. // }
  186. // return resolvedURL
  187. // }
  188. // - (id)initWithProperties:(NSDictionary *)dictionary {
  189. // NSString *type = [dictionary objectForKey:TYPE_KEY];
  190. // if ([type isEqualToString:SEPARATOR_STRING]) {
  191. // return (id)[[SKSeparatorBookmark alloc] init];
  192. // } else if ([type isEqualToString:FOLDER_STRING] || [type isEqualToString:SESSION_STRING]) {
  193. // Class bookmarkClass = [type isEqualToString:FOLDER_STRING] ? [SKFolderBookmark class] : [SKSessionBookmark class];
  194. // NSMutableArray *newChildren = [NSMutableArray array];
  195. // SKBookmark *child;
  196. // for (NSDictionary *dict in [dictionary objectForKey:CHILDREN_KEY]) {
  197. // if ((child = [[SKBookmark alloc] initWithProperties:dict])) {
  198. // [newChildren addObject:child];
  199. // [child release];
  200. // } else
  201. // NSLog(@"Failed to read child bookmark: %@", dict);
  202. // }
  203. // return (id)[[bookmarkClass alloc] initFolderWithChildren:newChildren label:[dictionary objectForKey:LABEL_KEY]];
  204. // } else if ([dictionary objectForKey:@"windowFrame"]) {
  205. // return (id)[[SKFileBookmark alloc] initWithSetup:dictionary label:[dictionary objectForKey:LABEL_KEY]];
  206. // } else {
  207. // NSNumber *pageIndex = [dictionary objectForKey:PAGEINDEX_KEY];
  208. // return (id)[[SKFileBookmark alloc] initWithAliasData:[dictionary objectForKey:ALIASDATA_KEY] pageIndex:(pageIndex ? [pageIndex unsignedIntegerValue] : NSNotFound) label:[dictionary objectForKey:LABEL_KEY]];
  209. // }
  210. // }
  211. func insert(child: KMBookmark, atIndex: Int) {
  212. children.insert(child, at: Int(atIndex))
  213. child.parent = self
  214. }
  215. func isDescendant(of bookmark: KMBookmark?) -> Bool {
  216. if bookmark == nil {
  217. return false
  218. }
  219. if self == bookmark {
  220. return true
  221. }
  222. for child in bookmark!.children {
  223. if self.isDescendant(of: child) {
  224. return true
  225. }
  226. }
  227. return false
  228. }
  229. func isDescendant(ofArray bookmarks: [KMBookmark]) -> Bool {
  230. for bm in bookmarks {
  231. if self.isDescendant(of: bm) {
  232. return true
  233. }
  234. }
  235. return false
  236. }
  237. func removeObjectFromChildren(index: Int) {
  238. children[index].parent = nil
  239. children.remove(at: index)
  240. }
  241. func open() {
  242. var tempFileURL = fileURL
  243. if tempFileURL == nil {
  244. if documentSetup[kBDAlias] != nil {
  245. tempFileURL = SKAlias.init(data: documentSetup[kBDAlias] as? Data).fileURL
  246. }
  247. if tempFileURL == nil && documentSetup[KMDocumentSetupFileNameKey] != nil {
  248. tempFileURL = NSURL.fileURL(withPath: documentSetup[KMDocumentSetupFileNameKey] as? String ?? "")
  249. }
  250. }
  251. guard let tempFileURL = tempFileURL else { return }
  252. NSDocumentController.shared.openDocument(withContentsOf: tempFileURL, display: true, completionHandler: { document, result, error in
  253. if (error != nil) {
  254. NSApp.presentError(error!)
  255. } else {
  256. if self.pageIndex > 1 {
  257. (document as! KMMainDocument).mainViewController?.setPageNumber = self.pageIndex + 1
  258. }
  259. }
  260. })
  261. }
  262. func objectOfChidren(index: Int) -> KMBookmark{
  263. return children[index]
  264. }
  265. }
  266. //MARK: Folder
  267. class KMFolderBookmark: KMBookmark {
  268. static func folderBookmark(label: String) -> KMFolderBookmark {
  269. let bookmark = KMFolderBookmark()
  270. bookmark.label = label
  271. return bookmark
  272. }
  273. override var bookmarkType: KMBookmarkType {
  274. get {
  275. return .folder
  276. }
  277. set {
  278. }
  279. }
  280. override func initWithProperties(properties: NSDictionary) -> KMBookmark {
  281. return KMFolderBookmark()
  282. }
  283. override var icon: NSImage {
  284. get {
  285. return NSImage(named: NSImage.folderName)!
  286. }
  287. set {
  288. }
  289. }
  290. override var alternateIcon: NSImage {
  291. get {
  292. return NSImage(named: NSImage.multipleDocumentsName)!
  293. }
  294. set {
  295. }
  296. }
  297. override func open() {
  298. var count = children.count
  299. while (count != 0) {
  300. count = count - 1
  301. children[count].open()
  302. }
  303. }
  304. }
  305. class KMRootBookmark: KMFolderBookmark {
  306. static func bookmarkRoot(childrenProperties: NSArray) -> KMRootBookmark {
  307. let rootBookmark = KMRootBookmark()
  308. var childs: [KMBookmark] = []
  309. for setup in childrenProperties {
  310. let bookmark = KMBookmark.bookmark(properties: setup as! [String : Any])
  311. bookmark?.documentSetup = setup as! [String : Any]
  312. bookmark?.parent = rootBookmark
  313. if bookmark != nil {
  314. childs.append(bookmark!)
  315. }
  316. }
  317. rootBookmark.children = childs
  318. rootBookmark.label = NSLocalizedString("Bookmarks Menu", comment: "")
  319. return rootBookmark
  320. }
  321. override var icon: NSImage {
  322. get {
  323. var iconImage = NSImage()
  324. let iconSize = NSSize(width: 16.0, height: 16.0)
  325. let drawingHandler: (NSRect) -> Bool = { rect in
  326. NSColor(calibratedWhite: 0.0, alpha: 0.2).set()
  327. NSBezierPath(rect: NSRect(x: 1.0, y: 1.0, width: 14.0, height: 13.0)).fill()
  328. NSGraphicsContext.saveGraphicsState()
  329. let path = NSBezierPath()
  330. path.move(to: NSPoint(x: 2.0, y: 2.0))
  331. path.line(to: NSPoint(x: 2.0, y: 15.0))
  332. path.line(to: NSPoint(x: 7.0, y: 15.0))
  333. path.line(to: NSPoint(x: 7.0, y: 13.0))
  334. path.line(to: NSPoint(x: 14.0, y: 13.0))
  335. path.line(to: NSPoint(x: 14.0, y: 2.0))
  336. path.close()
  337. NSColor.white.set()
  338. NSShadow.setShadowWith(NSColor(calibratedWhite: 0.0, alpha: 0.33333), blurRadius: 2.0, yOffset: -1.0)
  339. path.fill()
  340. NSGraphicsContext.restoreGraphicsState()
  341. NSColor(calibratedRed: 0.162, green: 0.304, blue: 0.755, alpha: 1.0).set()
  342. NSRect(x: 2.0, y: 13.0, width: 5.0, height: 2.0).fill()
  343. NSColor(calibratedRed: 0.894, green: 0.396, blue: 0.202, alpha: 1.0).set()
  344. NSRect(x: 3.0, y: 4.0, width: 1.0, height: 1.0).fill()
  345. NSRect(x: 3.0, y: 7.0, width: 1.0, height: 1.0).fill()
  346. NSRect(x: 3.0, y: 10.0, width: 1.0, height: 1.0).fill()
  347. NSColor(calibratedWhite: 0.6, alpha: 1.0).set()
  348. NSRect(x: 5.0, y: 4.0, width: 1.0, height: 1.0).fill()
  349. NSRect(x: 5.0, y: 7.0, width: 1.0, height: 1.0).fill()
  350. NSRect(x: 5.0, y: 10.0, width: 1.0, height: 1.0).fill()
  351. for i in 0..<7 {
  352. for j in 0..<3 {
  353. NSColor(calibratedWhite: 0.45 + 0.1 * CGFloat(Float(arc4random()) / Float(UInt32.max)), alpha: 1.0).set()
  354. NSRect(x: 6.0 + CGFloat(i), y: 4.0 + CGFloat(3 * j), width: 1.0, height: 1.0).fill()
  355. }
  356. }
  357. let gradient = NSGradient(starting: NSColor(calibratedWhite: 0.0, alpha: 0.1), ending: NSColor(calibratedWhite: 0.0, alpha: 0.0))
  358. gradient?.draw(in: NSRect(x: 2.0, y: 2.0, width: 12.0, height: 11.0), angle: 90.0)
  359. return true
  360. }
  361. iconImage = NSImage(size: iconSize, flipped: false, drawingHandler: drawingHandler)
  362. return iconImage
  363. }
  364. set {
  365. }
  366. }
  367. }
  368. //MARK: Session
  369. class KMSessionBookmark: KMFolderBookmark {
  370. override var bookmarkType: KMBookmarkType {
  371. get {
  372. return .session
  373. }
  374. set {
  375. }
  376. }
  377. static func bookmarkSession(setups: NSArray, label: String) -> KMSessionBookmark {
  378. let bookmark = KMSessionBookmark()
  379. var childs: [KMBookmark] = []
  380. for setup in setups {
  381. let bookmark = KMBookmark()
  382. bookmark.documentSetup = setup as! [String : Any]
  383. childs.append(bookmark)
  384. }
  385. bookmark.children = childs
  386. bookmark.label = label
  387. return bookmark
  388. }
  389. override func initWithProperties(properties: NSDictionary) -> KMBookmark {
  390. return KMSessionBookmark()
  391. }
  392. override var icon: NSImage {
  393. get {
  394. return NSImage(named: NSImage.multipleDocumentsName)!
  395. }
  396. set {
  397. }
  398. }
  399. }
  400. //MARK: Separator
  401. class KMSeparatorBookmark: KMBookmark {
  402. override var bookmarkType: KMBookmarkType {
  403. get {
  404. return .separator
  405. }
  406. set {
  407. }
  408. }
  409. override func initWithProperties(properties: NSDictionary) -> KMBookmark {
  410. return KMSeparatorBookmark()
  411. }
  412. }
  413. //MARK: File
  414. class KMFileBookmark: KMBookmark {
  415. override var bookmarkType: KMBookmarkType {
  416. get {
  417. return .file
  418. }
  419. set {
  420. }
  421. }
  422. static func missingFileImage() -> NSImage {
  423. let image: NSImage = {
  424. let size = NSSize(width: 16.0, height: 16.0)
  425. return NSImage(size: size, flipped: false, drawingHandler: { rect in
  426. let genericDocImage = NSWorkspace.shared.icon(forFileType: NSFileTypeForHFSTypeCode(OSType(kGenericDocumentIcon)))
  427. let questionMark = NSWorkspace.shared.icon(forFileType: NSFileTypeForHFSTypeCode(OSType(kQuestionMarkIcon)))
  428. genericDocImage.draw(in: rect, from: .zero, operation: .copy, fraction: 0.7)
  429. questionMark.draw(in: NSRect(x: 3.0, y: 2.0, width: 10.0, height: 10.0), from: .zero, operation: .sourceOver, fraction: 0.7)
  430. return true
  431. })
  432. }()
  433. return image
  434. }
  435. override var icon: NSImage {
  436. get {
  437. if let fileURL = self.fileURL {
  438. return NSWorkspace.shared.icon(forFile: fileURL.path)
  439. } else {
  440. return KMFileBookmark.missingFileImage()
  441. }
  442. }
  443. set {
  444. }
  445. }
  446. }
  447. class AliasData: Codable {
  448. }