|
@@ -435,6 +435,71 @@ extension KMPasswordInputWindow {
|
|
|
window.beginSheet(passwordWindow!)
|
|
|
}
|
|
|
|
|
|
+ @objc class func openWindow(window: NSWindow, url: URL, needOwner: Bool, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) {
|
|
|
+ let passwordWindow = KMPasswordInputWindow().window()
|
|
|
+ passwordWindow?.documentURL = url
|
|
|
+
|
|
|
+ let document = CPDFDocument(url: url)
|
|
|
+ if (document?.isLocked != nil && document!.isLocked) {
|
|
|
+ passwordWindow?.type = .open
|
|
|
+ } else if (document?.isEncrypted != nil && document!.isEncrypted) {
|
|
|
+ passwordWindow?.type = .owner
|
|
|
+ } else {
|
|
|
+ passwordWindow?.type = .open
|
|
|
+ }
|
|
|
+
|
|
|
+ passwordInputWindow = passwordWindow
|
|
|
+
|
|
|
+ passwordWindow?.itemClick = { index, string in
|
|
|
+ let type = passwordInputWindow?.type
|
|
|
+ if (passwordInputWindow?.sheetParent != nil) {
|
|
|
+ passwordInputWindow?.sheetParent?.endSheet(passwordInputWindow!)
|
|
|
+ }
|
|
|
+ passwordInputWindow = nil
|
|
|
+ if index == 1 { /// 关闭
|
|
|
+ callback(.cancel, "")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ /// 解密成功
|
|
|
+ if (type == .owner) { // 解除的是权限密码
|
|
|
+ callback(.success, string)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解除的是开启密码
|
|
|
+ if (needOwner == false) { // 不需要解除权限密码
|
|
|
+ callback(.success, string)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (document == nil) {
|
|
|
+ callback(.success, string)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ document?.unlock(withPassword: string)
|
|
|
+ if (document?.permissionsStatus == .owner) { // 用户是使用的权限密码解密
|
|
|
+ callback(.success, string)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (document!.allowsCopying == true && document!.allowsPrinting == true) { // 文件没有权限限制
|
|
|
+ callback(.success, string)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 需要解除权限密码
|
|
|
+ KMPasswordInputWindow.openWindow(window: window, type: .owner, url: url) { result, password in
|
|
|
+ if (result == .cancel) {
|
|
|
+ callback(.cancel, "")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ callback(.success, password)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ window.beginSheet(passwordWindow!)
|
|
|
+ }
|
|
|
+
|
|
|
class func saveDocument(_ document: CPDFDocument) -> Bool {
|
|
|
let toPath = document.documentURL.path
|
|
|
|