KMPasswordInputWindow.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. @objc enum KMPasswordInputWindowType: Int {
  10. case open = 1
  11. case owner = 2
  12. }
  13. @objc enum KMPasswordInputWindowResult: Int {
  14. case cancel = 1
  15. case success = 2
  16. }
  17. typealias KMPasswordInputWindowItemClick = (KMPasswordInputWindow, Int, String) -> ()
  18. @objcMembers class KMPasswordInputWindow: NSWindow, NibLoadable {
  19. @IBOutlet weak var titleLabel: NSTextField!
  20. @IBOutlet weak var despLabel: NSTextField!
  21. @IBOutlet weak var secureTextFiled: KMSecureTextFiled!
  22. @IBOutlet weak var iconImageView: NSImageView!
  23. @IBOutlet weak var passwordErrorLabel: NSTextField!
  24. @IBOutlet weak var cancelButton: NSButton!
  25. @IBOutlet weak var confirmButton: NSButton!
  26. var confirmButtonVC: KMDesignButton?
  27. var documentURL: URL?
  28. var itemClick: KMPasswordInputWindowItemClick?
  29. var type: KMPasswordInputWindowType = .open {
  30. didSet {
  31. self.titleLabel?.stringValue = NSLocalizedString("Permission Password", comment: "")
  32. var fileName = NSLocalizedString("", comment: "")
  33. if (self.documentURL != nil) {
  34. fileName.append("\(self.documentURL!.lastPathComponent)")
  35. }
  36. self.despLabel?.maximumNumberOfLines = 3
  37. self.despLabel?.lineBreakMode = .byTruncatingTail
  38. self.despLabel?.cell?.truncatesLastVisibleLine = true
  39. let ps = NSMutableParagraphStyle()
  40. ps.lineSpacing = 5
  41. let despLabelString = "\"\(fileName)\"\(NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: ""))"
  42. self.despLabel?.attributedStringValue = NSAttributedString(string: despLabelString, attributes: [.foregroundColor : KMAppearance.Layout.h0Color(), .font : NSFont.SFProTextRegularFont(14), .paragraphStyle : ps])
  43. }
  44. }
  45. var canEncrpty = false
  46. static var permissionsStatus: CPDFDocumentPermissions = .none
  47. deinit {
  48. KMPrint("KMPasswordInputWindow 已释放了")
  49. }
  50. static var nibName: String? {
  51. return "KMPasswordInputWindow"
  52. }
  53. static func createFromNib(in bundle: Bundle) -> Self? {
  54. guard let nibName = self.nibName else {
  55. return nil
  56. }
  57. var topLevelArray: NSArray? = nil
  58. bundle.loadNibNamed(NSNib.Name(nibName), owner: self, topLevelObjects: &topLevelArray)
  59. guard let results = topLevelArray else {
  60. return nil
  61. }
  62. let views = Array<Any>(results).filter { $0 is Self }
  63. return views.last as? Self
  64. }
  65. class func createWindow() -> Self? {
  66. KMPasswordInputWindow.permissionsStatus = .none
  67. return self.createFromNib(in: MainBundle)
  68. }
  69. override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
  70. super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
  71. }
  72. override func awakeFromNib() {
  73. super.awakeFromNib()
  74. self.titleLabel.stringValue = NSLocalizedString("Permission Password", comment: "")
  75. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  76. self.titleLabel.font = NSFont.SFProTextRegularFont(16)
  77. self.despLabel.stringValue = NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: "")
  78. self.despLabel.textColor = KMAppearance.Layout.h0Color()
  79. self.despLabel.font = NSFont.SFProTextRegularFont(14)
  80. self.despLabel.isSelectable = false
  81. let ps = NSMutableParagraphStyle()
  82. ps.lineSpacing = 5
  83. self.despLabel.maximumNumberOfLines = 2
  84. self.despLabel.lineBreakMode = .byTruncatingTail
  85. ps.lineBreakMode = .byTruncatingTail
  86. self.despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : KMAppearance.Layout.h0Color(), .font : NSFont.SFProTextRegularFont(14), .paragraphStyle : ps])
  87. self.iconImageView.image = NSImage(named: "KMImageNameSecureIcon")
  88. self.secureTextFiled.backgroundView.wantsLayer = true
  89. self.secureTextFiled.backgroundView.layer?.borderWidth = 1
  90. self.secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  91. self.secureTextFiled.backgroundView.layer?.cornerRadius = 4
  92. self.secureTextFiled.placeholderString = NSLocalizedString("Password", comment: "")
  93. let rightView = NSView()
  94. rightView.frame = NSMakeRect(0, 0, 40, 32);
  95. self.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. self.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. self.secureTextFiled.rightView?.isHidden = true
  111. } else {
  112. self.secureTextFiled.rightView?.isHidden = false
  113. }
  114. self.passwordErrorLabel.isHidden = true
  115. }
  116. self.secureTextFiled.valueDidChange = { [unowned self] view, string in
  117. view.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
  118. self.passwordErrorLabel.isHidden = true
  119. if string.isEmpty {
  120. view.rightView?.isHidden = true
  121. self.dealConfirmButtonEnabledState(enabled: false)
  122. } else {
  123. view.rightView?.isHidden = false
  124. self.dealConfirmButtonEnabledState(enabled: true)
  125. }
  126. }
  127. self.secureTextFiled.enterAction = { [unowned self] in
  128. self.confirmButtonAction()
  129. }
  130. self.passwordErrorLabel.stringValue = NSLocalizedString("Incorrect password. Please try again.", comment: "")
  131. self.passwordErrorLabel.font = NSFont.systemFont(ofSize: 12)
  132. self.passwordErrorLabel.wantsLayer = true
  133. self.passwordErrorLabel.textColor = NSColor.km_init(hex: "#F3465B")
  134. self.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 = NSLocalizedString("Cancel", comment: "")
  145. cancelButtonVC.target = self
  146. cancelButtonVC.action = #selector(cancelButtonAction)
  147. cancelButtonVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
  148. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  149. self.cancelButton.action = #selector(cancelButtonAction)
  150. let confirmButtonVC = KMDesignButton(withType: .Text)
  151. // self.confirmButton.addSubview(confirmButtonVC.view)
  152. confirmButtonVC.view.frame = self.confirmButton.bounds
  153. confirmButtonVC.view.autoresizingMask = [.width, .height]
  154. confirmButtonVC.stringValue = NSLocalizedString("Open", comment: "")
  155. confirmButtonVC.target = self
  156. confirmButtonVC.action = #selector(confirmButtonAction)
  157. self.confirmButtonVC = confirmButtonVC
  158. self.confirmButtonVC?.button.keyEquivalent = KMKeyEquivalent.enter
  159. self.confirmButton.title = NSLocalizedString("Open", comment: "")
  160. self.confirmButton.action = #selector(confirmButtonAction)
  161. self.dealConfirmButtonEnabledState(enabled: true)
  162. }
  163. // MARK: - Actions
  164. @objc func cancelButtonAction() {
  165. guard let callback = self.itemClick else {
  166. return
  167. }
  168. callback(self, 1, "")
  169. }
  170. @objc func confirmButtonAction() {
  171. if (!self.canEncrpty) {
  172. return
  173. }
  174. guard let documentURL = self.documentURL else {
  175. return
  176. }
  177. if (self.type == .open) {
  178. let document: CPDFDocument = CPDFDocument(url: documentURL)
  179. if document.permissionsStatus == .none {
  180. let reuslt = document.unlock(withPassword: secureTextFiled.password())
  181. /// CPDFDocumentPermissionsNone 解锁失败
  182. /// CPDFDocumentPermissionsUser 输入的开启密码
  183. /// CPDFDocumentPermissionsOwner 输入的权限密码
  184. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  185. if document.permissionsStatus != CPDFDocumentPermissions.none { /// 密码正确
  186. guard let callback = self.itemClick else {
  187. return
  188. }
  189. callback(self ,2, secureTextFiled.password())
  190. } else { /// 密码错误
  191. self.passwordErrorLabel.isHidden = false
  192. self.passwordErrorLabel.stringValue = NSLocalizedString("Incorrect password. Please try again.", comment: "")
  193. self.secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  194. }
  195. }
  196. return
  197. }
  198. /// 权限密码类型
  199. let document: CPDFDocument = CPDFDocument(url: documentURL)
  200. if (document.isLocked) {
  201. if document.permissionsStatus == CPDFDocumentPermissions.none {
  202. let reuslt = document.unlock(withPassword: secureTextFiled.password())
  203. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  204. if document.permissionsStatus == .owner { /// 密码正确
  205. guard let callback = self.itemClick else {
  206. return
  207. }
  208. callback(self, 2, secureTextFiled.password())
  209. } else { /// 密码错误
  210. self.passwordErrorLabel.isHidden = false
  211. self.passwordErrorLabel.stringValue = NSLocalizedString("Incorrect password. Please try again.", comment: "")
  212. self.secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  213. }
  214. }
  215. } else {
  216. if document.permissionsStatus == CPDFDocumentPermissions.user {
  217. document.unlock(withPassword: secureTextFiled.password())
  218. KMPasswordInputWindow.permissionsStatus = document.permissionsStatus
  219. if document.permissionsStatus == .owner { /// 密码正确
  220. guard let callback = self.itemClick else {
  221. return
  222. }
  223. callback(self, 2, secureTextFiled.password())
  224. } else { /// 密码错误
  225. self.passwordErrorLabel.isHidden = false
  226. self.passwordErrorLabel.stringValue = NSLocalizedString("Incorrect password. Please try again.", comment: "")
  227. self.secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#F3465B").cgColor
  228. }
  229. }
  230. }
  231. }
  232. @objc func clearButtonAction() {
  233. self.secureTextFiled.clear()
  234. }
  235. func dealConfirmButtonEnabledState(enabled: Bool) {
  236. self.canEncrpty = enabled
  237. self.confirmButton.isEnabled = enabled
  238. }
  239. override func mouseUp(with event: NSEvent) {
  240. super.mouseUp(with: event)
  241. self.makeFirstResponder(nil)
  242. self.secureTextFiled.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  243. self.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. /// 用户输入的是开启密码 (无法判断是否还有权限未解密)
  422. callback(panel.url, .success ,password)
  423. }
  424. }
  425. }
  426. /**
  427. * 打开 NSOpenPanel 窗口(如果文档存在开启密码或者权限密码,则会弹密码输入框)
  428. * @param window 弹出 NSOpenPanel 的窗口 [可选] [默认为 主窗口]
  429. * @param needOwner 是否需要限制权限密码(如果存在权限密码,会在解锁后再弹权限密码弹窗(目前未实现)) [可选] [默认为 false]
  430. * @param callback 回调
  431. *
  432. *  默认弹开启密码输入框,needOwner = true 弹权限密码输入框
  433. *  只返回成功的结果, 用户关闭的操作都未回调(如果有需要回调的需求可以使用 km_secure_openPanel 方法)
  434. */
  435. class func km_secure_openPanel_success(window: NSWindow = NSApp.mainWindow!, needOwner: Bool = false, callback:@escaping (URL, String?)->Void) {
  436. let panel = NSOpenPanel()
  437. panel.allowedFileTypes = ["pdf"]
  438. panel.beginSheetModal(for: window) { response in
  439. if (response == .cancel) {
  440. return
  441. }
  442. let document = CPDFDocument(url: panel.url)
  443. if ((document?.isLocked)! == false) {
  444. if (document?.isEncrypted == false) {
  445. callback(panel.url!, nil)
  446. return
  447. }
  448. if (!needOwner) {
  449. callback(panel.url!, nil)
  450. return
  451. }
  452. KMPasswordInputWindow.openWindow(window: window, type: .owner, url: panel.url!) { result, password in
  453. if (result == .cancel) {
  454. return
  455. }
  456. callback(panel.url!, password)
  457. }
  458. return
  459. }
  460. /// 已加锁(开启密码)
  461. KMPasswordInputWindow.openWindow(window: window, url: panel.url!) { result, password in
  462. if (result == .cancel) {
  463. return
  464. }
  465. if (!needOwner) {
  466. callback(panel.url!, password)
  467. return
  468. }
  469. /// 用户输入的是权限密码
  470. if (KMPasswordInputWindow.permissionsStatus == .owner) {
  471. callback(panel.url!, password)
  472. return
  473. }
  474. callback(panel.url!, password)
  475. }
  476. }
  477. }
  478. }