123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // KMMergeWindowController.swift
- // PDF Master
- //
- // Created by lizhe on 2023/11/8.
- //
- import Cocoa
- typealias KMMergeWindowControllerCancelAction = (_ controller: KMMergeWindowController) -> Void
- typealias KMMergeWindowControllerAddFilesAction = (_ controller: KMMergeWindowController) -> Void
- typealias KMMergeWindowControllerMergeAction = (_ controller: KMMergeWindowController) -> Void
- typealias KMMergeWindowControllerClearAction = (_ controller: KMMergeWindowController) -> Void
- class KMMergeWindowController: NSWindowController {
- @IBOutlet weak var mergeView: KMMergeView!
-
- var cancelAction: KMMergeWindowControllerCancelAction?
-
- var pdfDocument: PDFDocument = PDFDocument()
- var password: String = ""
-
- var oriDucumentUrl: URL?
- // - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
- // {
- // if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
- //
- // // self.PDFDocument = document;
- // self.PDFDocument = [[PDFDocument alloc] init];
- // self.editType = KMPDFPageEditAppend;
- // _lockFilePathArr = [[NSMutableArray alloc] init];
- // _files = [[NSMutableArray alloc] init];
- //
- // KMFileAttribute *file = [[KMFileAttribute alloc] init];
- // file.myPDFDocument = document;
- // file.filePath = document.documentURL.path;
- // file.oriFilePath = self.oriDucumentUrl.path;
- // if (password && password.length > 0) {
- // file.password = password;
- // file.isLocked = YES;
- // }
- // [self.files addObject:file];
- // }
- // return self;
- // }
- convenience init(document: PDFDocument, password: String) {
- self.init(windowNibName: "KMMergeWindowController")
-
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- self.window!.title = NSLocalizedString("Merge PDF Files", comment: "");
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-
- mergeView.addFilesAction = { [unowned self] view in
- self.addFile()
- }
-
- mergeView.clearAction = { [unowned self] view in
-
- }
-
- mergeView.mergeAction = { [unowned self] view in
-
- }
-
- mergeView.cancelAction = { [unowned self] view in
- cancelAction?(self)
- }
- }
- }
- extension KMMergeWindowController {
- func addFile() {
-
- // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
- // //免費版只支援2個檔案做合併小于20M的文件合并
- // if (_files.count >= 2 || self.allFileSize > (20 * 1024 * 1024)) {
- // #if VERSION_DMG
- // [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
- // #else
- // KMToolCompareWindowController * vc = [KMToolCompareWindowController toolCompareWithType:KMCompareWithToolType_PageEdit setSelectIndex:1];
- // [vc showWindow:nil];
- // #endif
- // return;
- // }
- //
- // }
- let openPanel = NSOpenPanel()
- openPanel.allowedFileTypes = ["pdf"]
- if KMPurchaseManager.manager.state == .subscription {
- openPanel.allowsMultipleSelection = true
- openPanel.message = NSLocalizedString("Select files to merge. To select multiple files press cmd ⌘ button on the keyboard and click on the target files one by one.", comment: "")
- } else {
- openPanel.allowsMultipleSelection = false
- openPanel.message = NSLocalizedString("Select files to merge, only one file can be selected at a time.", comment: "")
- }
- openPanel.beginSheetModal(for: self.window!) { (result) in
- if result == NSApplication.ModalResponse.OK {
- var array: [URL] = []
- for fileURL in openPanel.urls {
- array.append(fileURL)
- // if let filePath = fileURL.path {
- // if !FileManager.default.isExecutableFile(atPath: filePath) {
- // continue
- // }
- //
- // do {
- // let attrib = try FileManager.default.attributesOfItem(atPath: filePath)
- //// if let fileSize = attrib[FileAttributeKey.size] as? NSNumber {
- //// self.allFileSize += fileSize.floatValue
- //// }
- ////
- //// if !IAPProductsManager.defaultManager.isAvailableAllFunction {
- //// if self.allFileSize > (20 * 1024 * 1024) || self.files.count >= 2 {
- //// let vc = KMToolCompareWindowController.toolCompare(withType: .pageEdit, setSelectIndex: 1)
- //// vc?.showWindow(nil)
- //// self.allFileSize -= fileSize.floatValue
- //// self.addFiles(array)
- //// return
- //// }
- //// }
- // array.append(fileURL)
- // } catch {
- // print("Error getting file attributes: \(error)")
- // }
- // }
- }
- self.mergeView.addFilePaths(urls: array)
- }
- }
- }
- }
|