KMBaseWindowController.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // KMBaseWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/5/11.
  6. //
  7. import Cocoa
  8. class KMBaseWindowController: NSWindowController {
  9. var cancelAction: KMCommonBlock?
  10. var pdfDocument: CPDFDocument?
  11. var isBates: Bool = false
  12. var isBatch: Bool = false //是否批量模块进入
  13. deinit {
  14. Swift.debugPrint(self.className + "已释放")
  15. self.removeNotification()
  16. }
  17. override func windowDidLoad() {
  18. super.windowDidLoad()
  19. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  20. self.initSubViews()
  21. self.initDefaultValue()
  22. self.initNotification()
  23. }
  24. func initSubViews() {}
  25. func initDefaultValue() {
  26. self.window?.appearance = NSApp.appearance
  27. }
  28. func initNotification() {
  29. DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  30. }
  31. func removeNotification() {
  32. DistributedNotificationCenter.default().removeObserver(self)
  33. }
  34. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  35. }
  36. // MARK: - 判断有无网络
  37. public func isConnectionAvailable() -> Bool {
  38. let reachability = Reachability(hostname: "www.apple.com")
  39. let state = reachability?.currentReachabilityStatus()
  40. if(state == NotReachable) {
  41. return false
  42. }
  43. return true
  44. }
  45. public func showHud(msg: String) {
  46. if let data = self.window?.contentView {
  47. _ = CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black)
  48. }
  49. }
  50. }
  51. // MARK: - Private Methods
  52. extension KMBaseWindowController {
  53. @objc private func _themeChanged(_ sender: Notification) {
  54. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  55. if isDarkModel {
  56. self.window?.appearance = .init(named: .darkAqua)
  57. } else {
  58. self.window?.appearance = .init(named: .aqua)
  59. }
  60. Task { @MainActor in
  61. self.interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  62. }
  63. }
  64. }
  65. extension NSWindowController {
  66. func kCheckPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  67. // 判断路径 + document
  68. guard let document = CPDFDocument.init(url: url) else {
  69. return completion(false, "")
  70. }
  71. // 判断是否为加密文档
  72. if document.isLocked == false {
  73. if type == .open {
  74. completion(true, "")
  75. return
  76. }
  77. }
  78. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  79. completion(true, "")
  80. return
  81. }
  82. // 加密文件,尝试解锁
  83. if password.isEmpty == false {
  84. let preStatus = document.permissionsStatus
  85. document.unlock(withPassword: password)
  86. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  87. completion(true, password)
  88. return
  89. }
  90. }
  91. // 弹密码弹窗
  92. Task { @MainActor in
  93. KMPasswordInputWindow.openWindow(window: self.window!, type: type, url: url) { result , password in
  94. if (result == .cancel) {
  95. completion(false, "")
  96. return
  97. } else {
  98. completion(true, password ?? "")
  99. }
  100. }
  101. }
  102. }
  103. }
  104. extension KMBaseWindowController {
  105. static func checkPassword(url: URL, type: KMPasswordInputWindowType, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
  106. // 判断路径 + document
  107. guard let document = CPDFDocument.init(url: url) else {
  108. return completion(false, "")
  109. }
  110. // 判断是否为加密文档
  111. if document.isLocked == false {
  112. if type == .open {
  113. completion(true, "")
  114. return
  115. }
  116. }
  117. if document.isLocked == false && (document.allowsCopying && document.allowsPrinting) {
  118. completion(true, "")
  119. return
  120. }
  121. // 加密文件,尝试解锁
  122. if password.isEmpty == false {
  123. let preStatus = document.permissionsStatus
  124. document.unlock(withPassword: password)
  125. if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
  126. completion(true, password)
  127. return
  128. }
  129. }
  130. // 弹密码弹窗
  131. Task { @MainActor in
  132. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: type, url: url) { result , password in
  133. if (result == .cancel) {
  134. completion(false, "")
  135. return
  136. } else {
  137. completion(true, password ?? "")
  138. }
  139. }
  140. }
  141. }
  142. }