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.target = self
  146. cancelButtonVC.action = #selector(cancelButtonAction)
  147. cancelButtonVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
  148. cancelButton.title = KMLocalizedString("Cancel")
  149. cancelButton.action = #selector(cancelButtonAction)
  150. let confirmButtonVC = KMDesignButton(withType: .Text)
  151. confirmButtonVC.view.frame = self.confirmButton.bounds
  152. confirmButtonVC.view.autoresizingMask = [.width, .height]
  153. confirmButtonVC.stringValue = KMLocalizedString("Open")
  154. confirmButtonVC.target = self
  155. confirmButtonVC.action = #selector(confirmButtonAction)
  156. self.confirmButtonVC = confirmButtonVC
  157. self.confirmButtonVC?.button.keyEquivalent = KMKeyEquivalent.enter
  158. confirmButton.title = KMLocalizedString("Open")
  159. confirmButton.action = #selector(confirmButtonAction)
  160. dealConfirmButtonEnabledState(enabled: true)
  161. }
  162. // MARK: - Actions
  163. @objc func cancelButtonAction() {
  164. guard let callback = self.itemClick else {
  165. return
  166. }
  167. callback(self, 1, "")
  168. }
  169. @objc func confirmButtonAction() {
  170. if (!self.canEncrpty) {
  171. return
  172. }
  173. guard let documentURL = self.documentURL else {
  174. return
  175. }
  176. if (self.type == .open) {
  177. let document: CPDFDocument = CPDFDocument(url: documentURL)
  178. if document.permissionsStatus == .none {
  179. let reuslt = document.unlock(withPassword: secureTextFiled.password())
  180. /// CPDFDocumentPermissionsNone 解锁失败
  181. /// CPDFDocumentPermissionsUser 输入的开启密码
  182. /// CPDFDocumentPermissionsOwner 输入的权限密码
  183. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  184. if document.permissionsStatus != CPDFDocumentPermissions.none { /// 密码正确
  185. guard let callback = self.itemClick else {
  186. return
  187. }
  188. callback(self ,2, secureTextFiled.password())
  189. } else { /// 密码错误
  190. passwordErrorLabel.isHidden = false
  191. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  192. secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  193. }
  194. }
  195. return
  196. }
  197. /// 权限密码类型
  198. let document: CPDFDocument = CPDFDocument(url: documentURL)
  199. if (document.isLocked) {
  200. if document.permissionsStatus == CPDFDocumentPermissions.none {
  201. let reuslt = document.unlock(withPassword: secureTextFiled.password())
  202. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  203. if document.permissionsStatus == .owner { /// 密码正确
  204. guard let callback = self.itemClick else {
  205. return
  206. }
  207. callback(self, 2, secureTextFiled.password())
  208. } else { /// 密码错误
  209. passwordErrorLabel.isHidden = false
  210. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  211. secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  212. }
  213. }
  214. } else {
  215. if document.permissionsStatus == CPDFDocumentPermissions.user {
  216. document.unlock(withPassword: secureTextFiled.password())
  217. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  218. if document.permissionsStatus == .owner { /// 密码正确
  219. guard let callback = self.itemClick else {
  220. return
  221. }
  222. callback(self, 2, secureTextFiled.password())
  223. } else { /// 密码错误
  224. passwordErrorLabel.isHidden = false
  225. passwordErrorLabel.stringValue = KMLocalizedString("Incorrect password. Please try again.")
  226. secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  227. }
  228. }
  229. }
  230. }
  231. @objc func clearButtonAction() {
  232. secureTextFiled.clear()
  233. }
  234. func dealConfirmButtonEnabledState(enabled: Bool) {
  235. canEncrpty = enabled
  236. confirmButton.isEnabled = enabled
  237. }
  238. override func mouseUp(with event: NSEvent) {
  239. super.mouseUp(with: event)
  240. makeFirstResponder(nil)
  241. passwordErrorLabel.isHidden = true
  242. }
  243. }
  244. extension KMPasswordInputWindow {
  245. @objc class func openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) -> KMPasswordInputWindow {
  246. let passwordWindow = KMPasswordInputWindow.createWindow()
  247. passwordWindow?.documentURL = url
  248. passwordWindow?.type = type
  249. passwordWindow?.itemClick = { pwdWin, index, string in
  250. if let sheetParent = pwdWin.sheetParent {
  251. sheetParent.endSheet(pwdWin)
  252. }
  253. if index == 1 { /// 关闭
  254. callback(.cancel, "")
  255. return
  256. }
  257. /// 解密成功
  258. callback(.success, string)
  259. }
  260. window.beginSheet(passwordWindow!)
  261. return passwordWindow!
  262. }
  263. @objc class func success_openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (String)->Void) {
  264. let passwordWindow = KMPasswordInputWindow.createWindow()
  265. passwordWindow?.documentURL = url
  266. passwordWindow?.type = type
  267. passwordWindow?.itemClick = { pwdWin, index, string in
  268. if let sheetParent = pwdWin.sheetParent {
  269. sheetParent.endSheet(pwdWin)
  270. }
  271. if index == 1 { /// 关闭
  272. return
  273. }
  274. /// 解密成功
  275. callback(string)
  276. }
  277. window.beginSheet(passwordWindow!)
  278. }
  279. @objc class func openWindow(window: NSWindow, url: URL, needOwner: Bool, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) {
  280. let passwordWindow = KMPasswordInputWindow.createWindow()
  281. passwordWindow?.documentURL = url
  282. let document = CPDFDocument(url: url)
  283. if (document?.isLocked != nil && document!.isLocked) {
  284. passwordWindow?.type = .open
  285. } else if (document?.isEncrypted != nil && document!.isEncrypted) {
  286. passwordWindow?.type = .owner
  287. } else {
  288. passwordWindow?.type = .open
  289. }
  290. passwordWindow?.itemClick = { pwdWin, index, string in
  291. let type = pwdWin.type
  292. if let sheetParent = pwdWin.sheetParent {
  293. sheetParent.endSheet(pwdWin)
  294. }
  295. if index == 1 { /// 关闭
  296. callback(.cancel, "")
  297. return
  298. }
  299. /// 解密成功
  300. if (type == .owner) { // 解除的是权限密码
  301. callback(.success, string)
  302. return
  303. }
  304. // 解除的是开启密码
  305. if (needOwner == false) { // 不需要解除权限密码
  306. callback(.success, string)
  307. return
  308. }
  309. if (document == nil) {
  310. callback(.success, string)
  311. return
  312. }
  313. document?.unlock(withPassword: string)
  314. if (document?.permissionsStatus == .owner) { // 用户是使用的权限密码解密
  315. callback(.success, string)
  316. return
  317. }
  318. if (document!.allowsCopying == true && document!.allowsPrinting == true) { // 文件没有权限限制
  319. callback(.success, string)
  320. return
  321. }
  322. // 需要解除权限密码
  323. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: url) { result, password in
  324. if (result == .cancel) {
  325. callback(.cancel, "")
  326. return
  327. }
  328. callback(.success, password)
  329. }
  330. }
  331. window.beginSheet(passwordWindow!)
  332. }
  333. class func saveDocument(_ document: CPDFDocument) -> Bool {
  334. let toPath = document.documentURL.path
  335. let tempFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("\((document.documentURL.lastPathComponent))")
  336. if (FileManager.default.fileExists(atPath: tempFilePath!)) {
  337. /// 清空数据
  338. try?FileManager.default.removeItem(atPath: tempFilePath!)
  339. }
  340. var result: Bool = document.write(to: URL(fileURLWithPath: tempFilePath!))
  341. if (result == false) {
  342. return false
  343. }
  344. try?FileManager.default.removeItem(atPath: toPath)
  345. result = ((try?FileManager.default.moveItem(atPath: tempFilePath!, toPath: toPath)) != nil)
  346. /// 清空数据
  347. try?FileManager.default.removeItem(atPath: tempFilePath!)
  348. return result
  349. }
  350. class func saveDocumentForRemovePassword(_ document: CPDFDocument) -> Bool {
  351. let toPath = document.documentURL.path
  352. let tempFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("\((document.documentURL.lastPathComponent))")
  353. if (FileManager.default.fileExists(atPath: tempFilePath!)) {
  354. /// 清空数据
  355. try?FileManager.default.removeItem(atPath: tempFilePath!)
  356. }
  357. var result: Bool = document.writeDecrypt(to: URL(fileURLWithPath: tempFilePath!))
  358. if (result == false) {
  359. return false
  360. }
  361. try?FileManager.default.removeItem(atPath: toPath)
  362. result = ((try?FileManager.default.moveItem(atPath: tempFilePath!, toPath: toPath)) != nil)
  363. /// 清空数据
  364. try?FileManager.default.removeItem(atPath: tempFilePath!)
  365. return result
  366. }
  367. }
  368. extension NSOpenPanel {
  369. /**
  370. * 打开 NSOpenPanel 窗口(如果文档存在开启密码或者权限密码,则会弹密码输入框)
  371. * @param window 弹出 NSOpenPanel 的窗口 [可选] [默认为 主窗口]
  372. * @param needOwner 是否需要限制权限密码(如果存在权限密码,会在解锁后再弹权限密码弹窗(目前未实现)) [可选] [默认为 false]
  373. * @param callback 回调
  374. *
  375. *  默认弹开启密码输入框,needOwner = true 弹权限密码输入框
  376. */
  377. class func km_secure_openPanel(window: NSWindow = NSApp.mainWindow!, needOwner: Bool = false, callback:@escaping (URL?, KMPasswordInputWindowResult? , String?)->Void) {
  378. let panel = NSOpenPanel()
  379. panel.allowedFileTypes = ["pdf"]
  380. panel.beginSheetModal(for: window) { response in
  381. if (response == .cancel) {
  382. callback(nil, nil, nil)
  383. return
  384. }
  385. let document = CPDFDocument(url: panel.url)
  386. if ((document?.isLocked)! == false) {
  387. if (document?.isEncrypted == false) {
  388. callback(panel.url, nil, nil)
  389. return
  390. }
  391. if (!needOwner) {
  392. callback(panel.url, nil, nil)
  393. return
  394. }
  395. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: panel.url!) { result, password in
  396. if (result == .cancel) {
  397. callback(panel.url, .cancel , nil)
  398. return
  399. }
  400. callback(panel.url, .success , password)
  401. }
  402. return
  403. }
  404. /// 已加锁(开启密码)
  405. KMPasswordInputWindow.openWindow(window: window, url: panel.url!) { result, password in
  406. if (result == .cancel) {
  407. callback(panel.url, .cancel, nil)
  408. return
  409. }
  410. if (!needOwner) {
  411. callback(panel.url, .success, password)
  412. return
  413. }
  414. /// 用户输入的是权限密码
  415. if (KMPasswordInputWindow.permissionsStatus == .owner) {
  416. callback(panel.url, .success ,password)
  417. return
  418. }
  419. callback(panel.url, .success ,password)
  420. }
  421. }
  422. }
  423. /**
  424. * 打开 NSOpenPanel 窗口(如果文档存在开启密码或者权限密码,则会弹密码输入框)
  425. * @param window 弹出 NSOpenPanel 的窗口 [可选] [默认为 主窗口]
  426. * @param needOwner 是否需要限制权限密码(如果存在权限密码,会在解锁后再弹权限密码弹窗(目前未实现)) [可选] [默认为 false]
  427. * @param callback 回调
  428. *
  429. *  默认弹开启密码输入框,needOwner = true 弹权限密码输入框
  430. *  只返回成功的结果, 用户关闭的操作都未回调(如果有需要回调的需求可以使用 km_secure_openPanel 方法)
  431. */
  432. class func km_secure_openPanel_success(window: NSWindow = NSApp.mainWindow!, needOwner: Bool = false, callback:@escaping (URL, String?)->Void) {
  433. let panel = NSOpenPanel()
  434. panel.allowedFileTypes = ["pdf"]
  435. panel.beginSheetModal(for: window) { response in
  436. if (response == .cancel) {
  437. return
  438. }
  439. let document = CPDFDocument(url: panel.url)
  440. if ((document?.isLocked)! == false) {
  441. if (document?.isEncrypted == false) {
  442. callback(panel.url!, nil)
  443. return
  444. }
  445. if (!needOwner) {
  446. callback(panel.url!, nil)
  447. return
  448. }
  449. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: panel.url!) { result, password in
  450. if (result == .cancel) {
  451. return
  452. }
  453. callback(panel.url!, password)
  454. }
  455. return
  456. }
  457. /// 已加锁(开启密码)
  458. KMPasswordInputWindow.openWindow(window: window, url: panel.url!) { result, password in
  459. if (result == .cancel) {
  460. return
  461. }
  462. if (!needOwner) {
  463. callback(panel.url!, password)
  464. return
  465. }
  466. /// 用户输入的是权限密码
  467. if (KMPasswordInputWindow.permissionsStatus == .owner) {
  468. callback(panel.url!, password)
  469. return
  470. }
  471. callback(panel.url!, password)
  472. }
  473. }
  474. }
  475. }