Преглед изворни кода

【综合】存储文档补充PDF Bundle格式

tangchao пре 10 месеци
родитељ
комит
163b09d602

+ 6 - 0
PDF Office/PDF Master/Class/Common/Category/NSString+KMExtension.swift

@@ -227,3 +227,9 @@ extension NSString {
     }
 }
 
+extension String {
+    func isCaseInsensitiveEqual(_ aString: String) -> Bool {
+        return self.caseInsensitiveCompare(aString) == .orderedSame
+    }
+}
+

+ 4 - 0
PDF Office/PDF Master/Class/Common/OC/Property/KMInfoWindowController.swift

@@ -179,6 +179,10 @@ class KMInfoWindowController: NSWindowController {
         self.attributesTableView.reloadData()
     }
     
+    public func info(for document: NSDocument) -> NSDictionary {
+        return self._infoForDocument(document)
+    }
+    
     // MARK: - Private Methods
      
     private func _infoForDocument(_ doc: NSDocument?) -> NSDictionary {

+ 113 - 41
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -32,6 +32,8 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
     static let kLastExportedTypeKey = "SKLastExportedType"
     static let kLastExportedOptionKey = "SKLastExportedOption"
     
+    static let kBundleDataFilename = "data"
+    
     var mainViewController: KMMainViewController?
     var homeWindowController: KMHomeWindowController?
     var homeViewController: KMHomeViewController?
@@ -76,6 +78,8 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
     
     var exportAccessoryC: SKExportAccessoryController?
     
+    var pdfData: Data?
+    
     weak var watermarkSaveDelegate: AnyObject?
     private var _trackEvents = IndexSet()
     
@@ -363,54 +367,25 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                 }
             }
             
+            self._removeSavePanelOfFormatPopupItems(savePanel)
             if (formatPopup != nil) {
-                if let item = formatPopup?.item(withTitle: "Notes as Text") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "Notes as FDF") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "Notes as RTFD") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: NSLocalizedString("Text", comment: "")) {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: NSLocalizedString("text", comment: "")) {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "PDF Reader Pro Edition Notes") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "XDV") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "DVI") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "Images") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
-                if let item = formatPopup?.item(withTitle: "Encapsulated PostScript") {
-                    formatPopup?.removeItem(at: formatPopup!.index(of: item))
-                }
                 let lastExportedType = KMDataManager.ud_string(forKey: Self.kLastExportedTypeKey)
                 var lastExportedOption = KMDataManager.ud_integer(forKey: Self.kLastExportedOptionKey)
                 if lastExportedOption == 0 {
                     lastExportedOption = KMExportOption.withEmbeddedNotes.rawValue
                 }
-                if (lastExportedType != nil) {
-                    let idx = formatPopup?.indexOfItem(withRepresentedObject: lastExportedType) ?? -1
-                    let selectedIdx = formatPopup?.indexOfSelectedItem ?? 0
-                    if idx != -1 && idx != selectedIdx {
-                        formatPopup?.selectItem(at: idx)
-                        formatPopup?.sendAction(formatPopup?.action, to: formatPopup?.target)
+//                if (lastExportedType != nil) {
+//                    let idx = formatPopup?.indexOfItem(withRepresentedObject: lastExportedType) ?? -1
+//                    let selectedIdx = formatPopup?.indexOfSelectedItem ?? 0
+//                    if idx != -1 && idx != selectedIdx {
+//                        formatPopup?.selectItem(at: idx)
+//                        formatPopup?.sendAction(formatPopup?.action, to: formatPopup?.target)
 //                        [savePanel setAllowedFileTypes:[NSArray arrayWithObjects:[self fileNameExtensionForType:lastExportedType saveOperation:NSSaveToOperation], nil]];
-                        if let data = self.fileNameExtension(forType: lastExportedType!, saveOperation: .saveToOperation) {
-                            savePanel.allowedFileTypes = [data]
-                        }
-                    }
-                }
+//                        if let data = self.fileNameExtension(forType: lastExportedType!, saveOperation: .saveToOperation) {
+//                            savePanel.allowedFileTypes = [data]
+//                        }
+//                    }
+//                }
                 self.mdFlags?.exportOption = UInt32(lastExportedOption)
 //
 //                self.exportAccessoryC = SKExportAccessoryController()
@@ -420,6 +395,8 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
 //                savePanel.accessoryView = self.exportAccessoryC?.view
 //                self._updateExportAccessoryView()
             }
+        } else if success {
+            self._removeSavePanelOfFormatPopupItems(savePanel)
         }
         return success
     }
@@ -526,6 +503,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
         // Alternatively, you could remove this method and override read(from:ofType:) instead.  If you do, you should also override isEntireFileLoaded to return false if the contents are lazily loaded.
         
         let pdfDocument = CPDFDocument.init(data: data)
+        self.pdfData = data
         if pdfDocument == nil {
             throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
         }
@@ -971,7 +949,59 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
     
     // MARK: - Private Methods
     
+    private func _PDFBundleFileWrapper(for name: String) -> FileWrapper {
+        var aName = name
+        if name.isCaseInsensitiveEqual(Self.kBundleDataFilename) {
+            aName = aName + "1"
+        }
+        
+//        var data: Data?
+        let fileWrapper = FileWrapper(directoryWithFileWrappers: [:])
+        let info = KMInfoWindowController.shared.info(for: self)
+//        NSDictionary *options = [[self mainWindowController] presentationOptions];
+//        if (options) {
+//            info = [[info mutableCopy] autorelease];
+//            [(NSMutableDictionary *)info setObject:options forKey:SKPresentationOptionsKey];
+//        }
+        if let data = self.pdfData {
+            fileWrapper.addRegularFile(withContents: data, preferredFilename: aName + ".pdf")
+        }
+        if let data = self.mainViewController?.document?.string()?.data(using: .utf8) {
+            fileWrapper.addRegularFile(withContents: data, preferredFilename: Self.kBundleDataFilename + ".txt")
+        }
+        if let data = try?PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: PropertyListSerialization.WriteOptions(0)) {
+            fileWrapper.addRegularFile(withContents: data, preferredFilename: Self.kBundleDataFilename + ".plist")
+        }
+//        if ((data = [NSPropertyListSerialization dataWithPropertyList:info format:NSPropertyListXMLFormat_v1_0 options:0 error:NULL]))
+//            [fileWrapper addRegularFileWithContents:data preferredFilename:[BUNDLE_DATA_FILENAME stringByAppendingPathExtension:@"plist"]];
+//        if ([[self notes] count] > 0) {
+//            if ((data = [self notesData]))
+//                [fileWrapper addRegularFileWithContents:data preferredFilename:[name stringByAppendingPathExtension:@"skim"]];
+//            if ((data = [[self notesString] dataUsingEncoding:NSUTF8StringEncoding]))
+//                [fileWrapper addRegularFileWithContents:data preferredFilename:[name stringByAppendingPathExtension:@"txt"]];
+//            if ((data = [self notesRTFData]))
+//                [fileWrapper addRegularFileWithContents:data preferredFilename:[name stringByAppendingPathExtension:@"rtf"]];
+//            if ((data = [self notesFDFDataForFile:[name stringByAppendingPathExtension:@"pdf"] fileIDStrings:[[self pdfDocument] fileIDStrings]]))
+//                [fileWrapper addRegularFileWithContents:data preferredFilename:[name stringByAppendingPathExtension:@"fdf"]];
+//        }
+        return fileWrapper
+    }
+    
     private func _km_write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws {
+        if typeName == KMPDFBundleDocumentType {
+//            NSFileWrapper *fileWrapper = [self PDFBundleFileWrapperForName:[[absoluteURL lastPathComponent] stringByDeletingPathExtension]];
+//            if (fileWrapper)
+//                didWrite = [fileWrapper writeToURL:absoluteURL options:0 originalContentsURL:nil error:&error];
+//            else
+//                error = [NSError writeFileErrorWithLocalizedDescription:NSLocalizedString(@"Unable to write file", @"Error description")];
+            let fileWrapper = self._PDFBundleFileWrapper(for: url.deletingPathExtension().lastPathComponent)
+            do {
+                try fileWrapper.write(to: url, options: FileWrapper.WritingOptions(rawValue: 0), originalContentsURL: nil)
+            } catch {
+                NSApp.presentError(error)
+            }
+            return
+        }
         var success = true
         if !self.isHome {
             if mainViewController != nil {
@@ -1126,6 +1156,48 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
         let ws = NSWorkspace.shared
         return ws.type(typeName, conformsToType: KMPDFDocumentType) || ws.type(typeName, conformsToType: KMPostScriptDocumentType) || ws.type(typeName, conformsToType: KMDVIDocumentType) || ws.type(typeName, conformsToType: KMXDVDocumentType)
     }
+    
+    private func _removeSavePanelOfFormatPopupItems(_ savePanel: NSSavePanel) {
+        var formatPopup: NSPopUpButton?
+        let svs = savePanel.accessoryView?.subviews.first?.subviews ?? []
+        for sv in svs {
+            if let data = sv as? NSPopUpButton {
+                formatPopup = data
+                break
+            }
+        }
+        
+        if let item = formatPopup?.item(withTitle: "Notes as Text") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "Notes as FDF") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "Notes as RTFD") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: NSLocalizedString("Text", comment: "")) {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: NSLocalizedString("text", comment: "")) {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "PDF Reader Pro Edition Notes") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "XDV") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "DVI") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "Images") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+        if let item = formatPopup?.item(withTitle: "Encapsulated PostScript") {
+            formatPopup?.removeItem(at: formatPopup!.index(of: item))
+        }
+    }
 }
 
 extension KMMainDocument {