KMMergeWindowController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // KMMergeWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMMergeWindowControllerCancelAction = (_ controller: KMMergeWindowController) -> Void
  9. typealias KMMergeWindowControllerAddFilesAction = (_ controller: KMMergeWindowController) -> Void
  10. typealias KMMergeWindowControllerMergeAction = (_ controller: KMMergeWindowController) -> Void
  11. typealias KMMergeWindowControllerClearAction = (_ controller: KMMergeWindowController) -> Void
  12. class KMMergeWindowController: NSWindowController {
  13. @IBOutlet weak var mergeView: KMMergeView!
  14. var cancelAction: KMMergeWindowControllerCancelAction?
  15. var pdfDocument: PDFDocument = PDFDocument()
  16. var password: String = ""
  17. var oriDucumentUrl: URL?
  18. // - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
  19. // {
  20. // if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
  21. //
  22. // // self.PDFDocument = document;
  23. // self.PDFDocument = [[PDFDocument alloc] init];
  24. // self.editType = KMPDFPageEditAppend;
  25. // _lockFilePathArr = [[NSMutableArray alloc] init];
  26. // _files = [[NSMutableArray alloc] init];
  27. //
  28. // KMFileAttribute *file = [[KMFileAttribute alloc] init];
  29. // file.myPDFDocument = document;
  30. // file.filePath = document.documentURL.path;
  31. // file.oriFilePath = self.oriDucumentUrl.path;
  32. // if (password && password.length > 0) {
  33. // file.password = password;
  34. // file.isLocked = YES;
  35. // }
  36. // [self.files addObject:file];
  37. // }
  38. // return self;
  39. // }
  40. convenience init(document: PDFDocument, password: String) {
  41. self.init(windowNibName: "KMMergeWindowController")
  42. }
  43. override func windowDidLoad() {
  44. super.windowDidLoad()
  45. self.window!.title = NSLocalizedString("Merge PDF Files", comment: "");
  46. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  47. mergeView.addFilesAction = { [unowned self] view in
  48. self.addFile()
  49. }
  50. mergeView.clearAction = { [unowned self] view in
  51. }
  52. mergeView.mergeAction = { [unowned self] view in
  53. }
  54. mergeView.cancelAction = { [unowned self] view in
  55. cancelAction?(self)
  56. }
  57. }
  58. }
  59. extension KMMergeWindowController {
  60. func addFile() {
  61. // if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
  62. // //免費版只支援2個檔案做合併小于20M的文件合并
  63. // if (_files.count >= 2 || self.allFileSize > (20 * 1024 * 1024)) {
  64. // #if VERSION_DMG
  65. // [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
  66. // #else
  67. // KMToolCompareWindowController * vc = [KMToolCompareWindowController toolCompareWithType:KMCompareWithToolType_PageEdit setSelectIndex:1];
  68. // [vc showWindow:nil];
  69. // #endif
  70. // return;
  71. // }
  72. //
  73. // }
  74. let openPanel = NSOpenPanel()
  75. openPanel.allowedFileTypes = ["pdf"]
  76. if KMPurchaseManager.manager.state == .subscription {
  77. openPanel.allowsMultipleSelection = true
  78. 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: "")
  79. } else {
  80. openPanel.allowsMultipleSelection = false
  81. openPanel.message = NSLocalizedString("Select files to merge, only one file can be selected at a time.", comment: "")
  82. }
  83. openPanel.beginSheetModal(for: self.window!) { (result) in
  84. if result == NSApplication.ModalResponse.OK {
  85. var array: [URL] = []
  86. for fileURL in openPanel.urls {
  87. array.append(fileURL)
  88. // if let filePath = fileURL.path {
  89. // if !FileManager.default.isExecutableFile(atPath: filePath) {
  90. // continue
  91. // }
  92. //
  93. // do {
  94. // let attrib = try FileManager.default.attributesOfItem(atPath: filePath)
  95. //// if let fileSize = attrib[FileAttributeKey.size] as? NSNumber {
  96. //// self.allFileSize += fileSize.floatValue
  97. //// }
  98. ////
  99. //// if !IAPProductsManager.defaultManager.isAvailableAllFunction {
  100. //// if self.allFileSize > (20 * 1024 * 1024) || self.files.count >= 2 {
  101. //// let vc = KMToolCompareWindowController.toolCompare(withType: .pageEdit, setSelectIndex: 1)
  102. //// vc?.showWindow(nil)
  103. //// self.allFileSize -= fileSize.floatValue
  104. //// self.addFiles(array)
  105. //// return
  106. //// }
  107. //// }
  108. // array.append(fileURL)
  109. // } catch {
  110. // print("Error getting file attributes: \(error)")
  111. // }
  112. // }
  113. }
  114. self.mergeView.addFilePaths(urls: array)
  115. }
  116. }
  117. }
  118. }