Просмотр исходного кода

综合-合并调整密码阻挡

tangchao 2 лет назад
Родитель
Сommit
c4b75bc81d

BIN
PDF Office/PDF Office.xcodeproj/project.xcworkspace/xcuserdata/kdanmobile.xcuserdatad/UserInterfaceState.xcuserstate


+ 37 - 0
PDF Office/PDF Office/Class/Common/Tools/KMTools.swift

@@ -43,4 +43,41 @@ import Cocoa
         }
         return files
     }
+    
+    // MARK: -
+    // MARK: 无法区分 [权限+开启] [开启] 这两种情况 请不要使用
+    private class func isDocumentHasPermissionsPassword(_ url: URL) -> Bool {
+        let document = PDFDocument(url: url)
+        if (document == nil) {
+            return false
+        }
+        
+        if (document?.permissionsStatus == .user) {
+            return true
+        }
+        
+        // document?.permissionsStatus == .none
+        if (document!.isLocked == false) { // 没有加锁
+            return false
+        }
+        
+        // 已加锁 [权限+开启] [开启]
+        if (KMTools.hasPermissionsLimit(document!)) { // 有权限限制
+            return true
+        }
+        
+        return false
+    }
+    
+    // MARK: -
+    // MARK: 暂时只处理了复制和打印两项(后续项目需求有新增时,可以再此方法里扩展)
+    @objc class func hasPermissionsLimit(_ document: PDFDocument) -> Bool {
+        if (document.allowsCopying == false) {
+            return true
+        }
+        if (document.allowsPrinting == false) {
+            return true
+        }
+        return false
+    }
 }

+ 10 - 5
PDF Office/PDF Office/Class/Merge/OCPart/KMPDFEditAppendWindow.m

@@ -488,8 +488,9 @@ static NSString * const KMTableColumnSizeID = @"size";
             
             if ([document isLocked]) {
                 [_lockFilePathArr addObject:filePath];
-            } else{
-                
+            } else if ([KMTools hasPermissionsLimit:document]) {
+                [_lockFilePathArr addObject:filePath];
+            } else {
                 [_files addObject:file];
             }
         } else {
@@ -512,6 +513,11 @@ static NSString * const KMTableColumnSizeID = @"size";
     
     if ([_lockFilePathArr count]) {
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+            if (self.files.count == 0) {
+                self.insertRow = 0;
+            } else {
+                self.insertRow = self.files.count-1;
+            }
             [self openPasswordWindow];
         });
     }
@@ -533,8 +539,7 @@ static NSString * const KMTableColumnSizeID = @"size";
 {
     if ([_lockFilePathArr count] > _lockFileIndex) {
         NSString *filePath = [_lockFilePathArr objectAtIndex:_lockFileIndex];
-        
-        [KMPasswordInputWindow openWindowWithWindow:self.window type:KMPasswordInputWindowTypeOwner url:[NSURL fileURLWithPath:filePath] callback:^(enum KMPasswordInputWindowResult result, NSString * _Nullable password) {
+        [KMPasswordInputWindow openWindowWithWindow:self.window url:[NSURL fileURLWithPath:filePath] needOwner:YES callback:^(enum KMPasswordInputWindowResult result, NSString * _Nullable password) {
             if (password.length > 0) {
                 KMFileAttribute *file = [[KMFileAttribute alloc] init];
                 file.filePath = filePath;
@@ -543,7 +548,7 @@ static NSString * const KMTableColumnSizeID = @"size";
                 [self.files insertObject:file atIndex:self.insertRow];
                 self.insertRow ++;
             }
-
+            
             self.lockFileIndex++;
             if ([self.lockFilePathArr count] > self.lockFileIndex){
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

+ 65 - 0
PDF Office/PDF Office/Class/PDFTools/Secure/Window/KMPasswordInputWindow.swift

@@ -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