KMPasswordInputWindow.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //
  2. // KMPasswordInputWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/29.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. import KMComponentLibrary
  10. @objc enum KMPasswordInputWindowType: Int {
  11. case open = 1
  12. case owner = 2
  13. }
  14. @objc enum KMPasswordInputWindowResult: Int {
  15. case cancel = 1
  16. case success = 2
  17. }
  18. typealias KMPasswordInputWindowItemClick = (KMPasswordInputWindow, Int, String) -> ()
  19. @objcMembers class KMPasswordInputWindow: NSWindow, NibLoadable {
  20. @IBOutlet weak var titleLabel: NSTextField!
  21. @IBOutlet weak var despLabel: NSTextField!
  22. @IBOutlet weak var secureTextFiled: KMSecureTextFiled!
  23. @IBOutlet weak var iconImageView: NSImageView!
  24. @IBOutlet weak var passwordErrorLabel: NSTextField!
  25. @IBOutlet weak var cancelButton: NSButton!
  26. @IBOutlet weak var confirmButton: NSButton!
  27. var confirmButtonVC: KMDesignButton?
  28. var documentURL: URL?
  29. var itemClick: KMPasswordInputWindowItemClick?
  30. var type: KMPasswordInputWindowType = .open {
  31. didSet {
  32. titleLabel?.stringValue = KMLocalizedString("Permission Password")
  33. var fileName = KMLocalizedString("")
  34. if (self.documentURL != nil) {
  35. fileName.append("\(self.documentURL!.lastPathComponent)")
  36. }
  37. despLabel?.maximumNumberOfLines = 3
  38. despLabel?.lineBreakMode = .byTruncatingTail
  39. despLabel?.cell?.truncatesLastVisibleLine = true
  40. let ps = NSMutableParagraphStyle()
  41. ps.lineSpacing = 5
  42. let despLabelString = "\"\(fileName)\"\(KMLocalizedString("This PDF is password protected. Please enter the password below to access this PDF."))"
  43. despLabel?.attributedStringValue = NSAttributedString(string: despLabelString, attributes: [.foregroundColor : ComponentLibrary.shared.getComponentColorFromKey("colorText/1"), .font : ComponentLibrary.shared.getFontFromKey("mac/body-m-bold"), .paragraphStyle : ps])
  44. }
  45. }
  46. var canEncrpty = false
  47. static var permissionsStatus: CPDFDocumentPermissions = .none
  48. deinit {
  49. KMPrint("KMPasswordInputWindow 已释放了")
  50. }
  51. static var nibName: String? {
  52. return "KMPasswordInputWindow"
  53. }
  54. static func createFromNib(in bundle: Bundle) -> Self? {
  55. guard let nibName = self.nibName else {
  56. return nil
  57. }
  58. var topLevelArray: NSArray? = nil
  59. bundle.loadNibNamed(NSNib.Name(nibName), owner: self, topLevelObjects: &topLevelArray)
  60. guard let results = topLevelArray else {
  61. return nil
  62. }
  63. let views = Array<Any>(results).filter { $0 is Self }
  64. return views.last as? Self
  65. }
  66. class func createWindow() -> Self? {
  67. KMPasswordInputWindow.permissionsStatus = .none
  68. return createFromNib(in: MainBundle)
  69. }
  70. override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
  71. super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
  72. }
  73. override func awakeFromNib() {
  74. super.awakeFromNib()
  75. titleLabel.stringValue = KMLocalizedString("Permission Password")
  76. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  77. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-l-bold")
  78. despLabel.stringValue = KMLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.")
  79. despLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  80. despLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  81. despLabel.isSelectable = false
  82. let ps = NSMutableParagraphStyle()
  83. ps.lineSpacing = 5
  84. despLabel.maximumNumberOfLines = 2
  85. despLabel.lineBreakMode = .byTruncatingTail
  86. ps.lineBreakMode = .byTruncatingTail
  87. despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : ComponentLibrary.shared.getComponentColorFromKey("colorText/1"), .font : ComponentLibrary.shared.getFontFromKey("mac/body-m-bold"), .paragraphStyle : ps])
  88. iconImageView.image = NSImage(named: "KMImageNameSecureIcon")
  89. secureTextFiled.backgroundView.wantsLayer = true
  90. secureTextFiled.backgroundView.layer?.borderWidth = 1
  91. secureTextFiled.backgroundView.layer?.cornerRadius = 4
  92. secureTextFiled.placeholderString = KMLocalizedString("Password")
  93. let rightView = NSView()
  94. rightView.frame = NSMakeRect(0, 0, 40, 32);
  95. secureTextFiled.rightView = rightView
  96. let clearButton = NSButton()
  97. rightView.addSubview(clearButton)
  98. clearButton.frame = NSMakeRect(10, 6, 20, 20)
  99. clearButton.wantsLayer = true
  100. clearButton.image = NSImage(named: "KMImageNameSecureClearIcon")
  101. clearButton.isBordered = false
  102. clearButton.target = self
  103. clearButton.action = #selector(clearButtonAction)
  104. rightView.isHidden = true
  105. secureTextFiled.becomeFirstResponderHandler = { [unowned self] securetextFiled in
  106. let mySecureTextField: KMSecureTextFiled = securetextFiled as! KMSecureTextFiled
  107. mySecureTextField.backgroundView.wantsLayer = true
  108. mySecureTextField.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
  109. if mySecureTextField.password().isEmpty {
  110. secureTextFiled.rightView?.isHidden = true
  111. } else {
  112. secureTextFiled.rightView?.isHidden = false
  113. }
  114. passwordErrorLabel.isHidden = true
  115. }
  116. secureTextFiled.valueDidChange = { [unowned self] view, string in
  117. view.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
  118. passwordErrorLabel.isHidden = true
  119. if string.isEmpty {
  120. view.rightView?.isHidden = true
  121. dealConfirmButtonEnabledState(enabled: false)
  122. } else {
  123. view.rightView?.isHidden = false
  124. dealConfirmButtonEnabledState(enabled: true)
  125. }
  126. }
  127. secureTextFiled.enterAction = { [unowned self] in
  128. self.confirmButtonAction()
  129. }
  130. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  131. passwordErrorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-xs-regular")
  132. passwordErrorLabel.wantsLayer = true
  133. passwordErrorLabel.textColor = NSColor.km_init(hex: "#F3465B")
  134. passwordErrorLabel.isHidden = true
  135. for button in [cancelButton, confirmButton] {
  136. button!.target = self
  137. if ((button?.isEqual(to: cancelButton))!) {
  138. } else {
  139. }
  140. }
  141. let cancelButtonVC = KMDesignButton(withType: .Text)
  142. cancelButtonVC.view.frame = self.cancelButton.bounds
  143. cancelButtonVC.view.autoresizingMask = [.width, .height]
  144. cancelButtonVC.stringValue = KMLocalizedString("Cancel")
  145. cancelButtonVC.button(type: .Sec_Icon, size: .m)
  146. cancelButtonVC.target = self
  147. cancelButtonVC.action = #selector(cancelButtonAction)
  148. cancelButtonVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
  149. cancelButton.title = KMLocalizedString("Cancel")
  150. cancelButton.action = #selector(cancelButtonAction)
  151. let confirmButtonVC = KMDesignButton(withType: .Text)
  152. confirmButtonVC.view.frame = self.confirmButton.bounds
  153. confirmButtonVC.view.autoresizingMask = [.width, .height]
  154. confirmButtonVC.stringValue = KMLocalizedString("Open")
  155. confirmButtonVC.button(type: .Cta, size: .m)
  156. confirmButtonVC.target = self
  157. confirmButtonVC.action = #selector(confirmButtonAction)
  158. self.confirmButtonVC = confirmButtonVC
  159. self.confirmButtonVC?.button.keyEquivalent = KMKeyEquivalent.enter
  160. confirmButton.title = KMLocalizedString("Open")
  161. confirmButton.action = #selector(confirmButtonAction)
  162. dealConfirmButtonEnabledState(enabled: true)
  163. }
  164. // MARK: - Actions
  165. @objc func cancelButtonAction() {
  166. guard let callback = self.itemClick else {
  167. return
  168. }
  169. callback(self, 1, "")
  170. }
  171. @objc func confirmButtonAction() {
  172. if (!self.canEncrpty) {
  173. return
  174. }
  175. guard let documentURL = self.documentURL else {
  176. return
  177. }
  178. if (self.type == .open) {
  179. let document: CPDFDocument = CPDFDocument(url: documentURL)
  180. if document.permissionsStatus == .none {
  181. let reuslt = document.unlock(withPassword: secureTextFiled.password())
  182. /// CPDFDocumentPermissionsNone 解锁失败
  183. /// CPDFDocumentPermissionsUser 输入的开启密码
  184. /// CPDFDocumentPermissionsOwner 输入的权限密码
  185. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  186. if document.permissionsStatus != CPDFDocumentPermissions.none { /// 密码正确
  187. guard let callback = self.itemClick else {
  188. return
  189. }
  190. callback(self ,2, secureTextFiled.password())
  191. } else { /// 密码错误
  192. passwordErrorLabel.isHidden = false
  193. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  194. secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  195. }
  196. }
  197. return
  198. }
  199. /// 权限密码类型
  200. let document: CPDFDocument = CPDFDocument(url: documentURL)
  201. if (document.isLocked) {
  202. if document.permissionsStatus == CPDFDocumentPermissions.none {
  203. let reuslt = document.unlock(withPassword: secureTextFiled.password())
  204. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  205. if document.permissionsStatus == .owner { /// 密码正确
  206. guard let callback = self.itemClick else {
  207. return
  208. }
  209. callback(self, 2, secureTextFiled.password())
  210. } else { /// 密码错误
  211. passwordErrorLabel.isHidden = false
  212. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  213. secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  214. }
  215. }
  216. } else {
  217. if document.permissionsStatus == CPDFDocumentPermissions.user {
  218. document.unlock(withPassword: secureTextFiled.password())
  219. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  220. if document.permissionsStatus == .owner { /// 密码正确
  221. guard let callback = self.itemClick else {
  222. return
  223. }
  224. callback(self, 2, secureTextFiled.password())
  225. } else { /// 密码错误
  226. passwordErrorLabel.isHidden = false
  227. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  228. secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  229. }
  230. }
  231. }
  232. }
  233. @objc func clearButtonAction() {
  234. secureTextFiled.clear()
  235. }
  236. func dealConfirmButtonEnabledState(enabled: Bool) {
  237. canEncrpty = enabled
  238. confirmButton.isEnabled = enabled
  239. }
  240. override func mouseUp(with event: NSEvent) {
  241. super.mouseUp(with: event)
  242. makeFirstResponder(nil)
  243. passwordErrorLabel.isHidden = true
  244. }
  245. }
  246. extension KMPasswordInputWindow {
  247. @objc class func openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) -> KMPasswordInputWindow {
  248. let passwordWindow = KMPasswordInputWindow.createWindow()
  249. passwordWindow?.documentURL = url
  250. passwordWindow?.type = type
  251. passwordWindow?.itemClick = { pwdWin, index, string in
  252. if let sheetParent = pwdWin.sheetParent {
  253. sheetParent.endSheet(pwdWin)
  254. }
  255. if index == 1 { /// 关闭
  256. callback(.cancel, "")
  257. return
  258. }
  259. /// 解密成功
  260. callback(.success, string)
  261. }
  262. window.beginSheet(passwordWindow!)
  263. return passwordWindow!
  264. }
  265. @objc class func success_openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (String)->Void) {
  266. let passwordWindow = KMPasswordInputWindow.createWindow()
  267. passwordWindow?.documentURL = url
  268. passwordWindow?.type = type
  269. passwordWindow?.itemClick = { pwdWin, index, string in
  270. if let sheetParent = pwdWin.sheetParent {
  271. sheetParent.endSheet(pwdWin)
  272. }
  273. if index == 1 { /// 关闭
  274. return
  275. }
  276. /// 解密成功
  277. callback(string)
  278. }
  279. window.beginSheet(passwordWindow!)
  280. }
  281. @objc class func openWindow(window: NSWindow, url: URL, needOwner: Bool, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) {
  282. let passwordWindow = KMPasswordInputWindow.createWindow()
  283. passwordWindow?.documentURL = url
  284. let document = CPDFDocument(url: url)
  285. if (document?.isLocked != nil && document!.isLocked) {
  286. passwordWindow?.type = .open
  287. } else if (document?.isEncrypted != nil && document!.isEncrypted) {
  288. passwordWindow?.type = .owner
  289. } else {
  290. passwordWindow?.type = .open
  291. }
  292. passwordWindow?.itemClick = { pwdWin, index, string in
  293. let type = pwdWin.type
  294. if let sheetParent = pwdWin.sheetParent {
  295. sheetParent.endSheet(pwdWin)
  296. }
  297. if index == 1 { /// 关闭
  298. callback(.cancel, "")
  299. return
  300. }
  301. /// 解密成功
  302. if (type == .owner) { // 解除的是权限密码
  303. callback(.success, string)
  304. return
  305. }
  306. // 解除的是开启密码
  307. if (needOwner == false) { // 不需要解除权限密码
  308. callback(.success, string)
  309. return
  310. }
  311. if (document == nil) {
  312. callback(.success, string)
  313. return
  314. }
  315. document?.unlock(withPassword: string)
  316. if (document?.permissionsStatus == .owner) { // 用户是使用的权限密码解密
  317. callback(.success, string)
  318. return
  319. }
  320. if (document!.allowsCopying == true && document!.allowsPrinting == true) { // 文件没有权限限制
  321. callback(.success, string)
  322. return
  323. }
  324. // 需要解除权限密码
  325. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: url) { result, password in
  326. if (result == .cancel) {
  327. callback(.cancel, "")
  328. return
  329. }
  330. callback(.success, password)
  331. }
  332. }
  333. window.beginSheet(passwordWindow!)
  334. }
  335. class func saveDocument(_ document: CPDFDocument) -> Bool {
  336. let toPath = document.documentURL.path
  337. let tempFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("\((document.documentURL.lastPathComponent))")
  338. if (FileManager.default.fileExists(atPath: tempFilePath!)) {
  339. /// 清空数据
  340. try?FileManager.default.removeItem(atPath: tempFilePath!)
  341. }
  342. var result: Bool = document.write(to: URL(fileURLWithPath: tempFilePath!))
  343. if (result == false) {
  344. return false
  345. }
  346. try?FileManager.default.removeItem(atPath: toPath)
  347. result = ((try?FileManager.default.moveItem(atPath: tempFilePath!, toPath: toPath)) != nil)
  348. /// 清空数据
  349. try?FileManager.default.removeItem(atPath: tempFilePath!)
  350. return result
  351. }
  352. class func saveDocumentForRemovePassword(_ document: CPDFDocument) -> Bool {
  353. let toPath = document.documentURL.path
  354. let tempFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("\((document.documentURL.lastPathComponent))")
  355. if (FileManager.default.fileExists(atPath: tempFilePath!)) {
  356. /// 清空数据
  357. try?FileManager.default.removeItem(atPath: tempFilePath!)
  358. }
  359. var result: Bool = document.writeDecrypt(to: URL(fileURLWithPath: tempFilePath!))
  360. if (result == false) {
  361. return false
  362. }
  363. try?FileManager.default.removeItem(atPath: toPath)
  364. result = ((try?FileManager.default.moveItem(atPath: tempFilePath!, toPath: toPath)) != nil)
  365. /// 清空数据
  366. try?FileManager.default.removeItem(atPath: tempFilePath!)
  367. return result
  368. }
  369. }
  370. extension NSOpenPanel {
  371. /**
  372. * 打开 NSOpenPanel 窗口(如果文档存在开启密码或者权限密码,则会弹密码输入框)
  373. * @param window 弹出 NSOpenPanel 的窗口 [可选] [默认为 主窗口]
  374. * @param needOwner 是否需要限制权限密码(如果存在权限密码,会在解锁后再弹权限密码弹窗(目前未实现)) [可选] [默认为 false]
  375. * @param callback 回调
  376. *
  377. *  默认弹开启密码输入框,needOwner = true 弹权限密码输入框
  378. */
  379. class func km_secure_openPanel(window: NSWindow = NSApp.mainWindow!, needOwner: Bool = false, callback:@escaping (URL?, KMPasswordInputWindowResult? , String?)->Void) {
  380. let panel = NSOpenPanel()
  381. panel.allowedFileTypes = ["pdf"]
  382. panel.beginSheetModal(for: window) { response in
  383. if (response == .cancel) {
  384. callback(nil, nil, nil)
  385. return
  386. }
  387. let document = CPDFDocument(url: panel.url)
  388. if ((document?.isLocked)! == false) {
  389. if (document?.isEncrypted == false) {
  390. callback(panel.url, nil, nil)
  391. return
  392. }
  393. if (!needOwner) {
  394. callback(panel.url, nil, nil)
  395. return
  396. }
  397. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: panel.url!) { result, password in
  398. if (result == .cancel) {
  399. callback(panel.url, .cancel , nil)
  400. return
  401. }
  402. callback(panel.url, .success , password)
  403. }
  404. return
  405. }
  406. /// 已加锁(开启密码)
  407. KMPasswordInputWindow.openWindow(window: window, url: panel.url!) { result, password in
  408. if (result == .cancel) {
  409. callback(panel.url, .cancel, nil)
  410. return
  411. }
  412. if (!needOwner) {
  413. callback(panel.url, .success, password)
  414. return
  415. }
  416. /// 用户输入的是权限密码
  417. if (KMPasswordInputWindow.permissionsStatus == .owner) {
  418. callback(panel.url, .success ,password)
  419. return
  420. }
  421. callback(panel.url, .success ,password)
  422. }
  423. }
  424. }
  425. /**
  426. * 打开 NSOpenPanel 窗口(如果文档存在开启密码或者权限密码,则会弹密码输入框)
  427. * @param window 弹出 NSOpenPanel 的窗口 [可选] [默认为 主窗口]
  428. * @param needOwner 是否需要限制权限密码(如果存在权限密码,会在解锁后再弹权限密码弹窗(目前未实现)) [可选] [默认为 false]
  429. * @param callback 回调
  430. *
  431. *  默认弹开启密码输入框,needOwner = true 弹权限密码输入框
  432. *  只返回成功的结果, 用户关闭的操作都未回调(如果有需要回调的需求可以使用 km_secure_openPanel 方法)
  433. */
  434. class func km_secure_openPanel_success(window: NSWindow = NSApp.mainWindow!, needOwner: Bool = false, callback:@escaping (URL, String?)->Void) {
  435. let panel = NSOpenPanel()
  436. panel.allowedFileTypes = ["pdf"]
  437. panel.beginSheetModal(for: window) { response in
  438. if (response == .cancel) {
  439. return
  440. }
  441. let document = CPDFDocument(url: panel.url)
  442. if ((document?.isLocked)! == false) {
  443. if (document?.isEncrypted == false) {
  444. callback(panel.url!, nil)
  445. return
  446. }
  447. if (!needOwner) {
  448. callback(panel.url!, nil)
  449. return
  450. }
  451. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: panel.url!) { result, password in
  452. if (result == .cancel) {
  453. return
  454. }
  455. callback(panel.url!, password)
  456. }
  457. return
  458. }
  459. /// 已加锁(开启密码)
  460. KMPasswordInputWindow.openWindow(window: window, url: panel.url!) { result, password in
  461. if (result == .cancel) {
  462. return
  463. }
  464. if (!needOwner) {
  465. callback(panel.url!, password)
  466. return
  467. }
  468. /// 用户输入的是权限密码
  469. if (KMPasswordInputWindow.permissionsStatus == .owner) {
  470. callback(panel.url!, password)
  471. return
  472. }
  473. callback(panel.url!, password)
  474. }
  475. }
  476. }
  477. }