KMBatchOperateBaseViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // KMBatchOperateBaseViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/30.
  6. //
  7. import Cocoa
  8. //var kBatchQueueOperationsChanged = "kBatchQueueOperationsChanged"
  9. private var kBatchQueueOperationsChanged = 0x000101
  10. class KMBatchOperateBaseViewController: KMNBaseViewController, KMBatchOperateProtocol{
  11. var operateType: KMBatchOperationType?
  12. var convertType: KMConvertWithPDFType?
  13. var files: [KMBatchOperateFile]?
  14. lazy var queue: KMOperationQueue? = {
  15. var queue: KMOperationQueue?
  16. if !((self.view.window as? KMBatchWindow)?.isBatch ?? false) {
  17. queue = KMOperationQueue()
  18. } else {
  19. if self.operateType == .Convert {
  20. queue = KMConvertOperationQueue.sharedQueue
  21. } else if self.operateType == .Compress {
  22. queue = KMCompressOperationQueue.sharedQueue
  23. } else if self.operateType == .AddPassword {
  24. queue = KMAddPasswordOperationQueue.sharedQueue
  25. } else if self.operateType == .RemovePassword {
  26. queue = KMRemovePasswordOperationQueue.sharedQueue
  27. } else if self.operateType == .AddWatermark {
  28. queue = KMAddWatermarkOprationQueue.sharedQueue
  29. } else if self.operateType == .RemoveWatermark {
  30. } else if self.operateType == .AddBackground {
  31. queue = KMAddBackgroundOperationQueue.sharedQueue
  32. } else if self.operateType == .RemoveBackground {
  33. queue = KMRemoveBackgroundOperationQueue.sharedQueue
  34. } else if self.operateType == .AddHeaderFooter {
  35. queue = KMAddHeaderFooterOperationQueue.sharedQueue
  36. } else if self.operateType == .RemoveHeaderFooter {
  37. queue = KMRemoveHeaderFooterQueue.sharedQueue
  38. } else if self.operateType == .AddBates {
  39. queue = KMAddBatesOperationQueue.sharedQueue
  40. } else if self.operateType == .RemoveBates {
  41. queue = KMRemoveBatesOperationQueue.sharedQueue
  42. }
  43. }
  44. return queue
  45. }()
  46. lazy var successFilePathURLArray: [URL]? = {
  47. let arr = NSMutableArray()
  48. return (arr as! [URL])
  49. }()
  50. var name: String = ""
  51. var interfaceStatus: KMBatchOperateInterfaceStatus?
  52. var choosePath: String = ""
  53. init(files: [KMBatchOperateFile]) {
  54. super.init(nibName: String(describing: type(of: self)), bundle: nil)
  55. self.files = (files)
  56. }
  57. required init?(coder: NSCoder) {
  58. super.init(coder: coder)
  59. }
  60. deinit {
  61. // queue?.removeObserver(self, forKeyPath: "operations")
  62. }
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65. self.queue?.addObserver(self, forKeyPath: "operations", options: .new, context: &kBatchQueueOperationsChanged)
  66. }
  67. func operateCompleted() {
  68. showWindowCloseButton()
  69. if successFilePathURLArray!.count > 0 {
  70. NSWorkspace.shared.activateFileViewerSelecting(successFilePathURLArray!)
  71. }
  72. self.interfaceStatus = .PrepareProcess
  73. }
  74. func showWindowCloseButton() {
  75. let btn = self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)
  76. btn?.isHidden = false
  77. }
  78. func beginBatchOperation() {
  79. for i in 0..<self.files!.count {
  80. let file = self.files?[i]
  81. file?.currentOperateInfo?.resetState()
  82. }
  83. if self.files!.count < 1 {
  84. NSSound.beep()
  85. return
  86. }
  87. self.fetchFileListViewController()!.reloadData()
  88. let openPanel = NSOpenPanel()
  89. openPanel.canChooseFiles = false
  90. openPanel.canChooseDirectories = true
  91. openPanel.canCreateDirectories = true
  92. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  93. if result == NSApplication.ModalResponse.OK {
  94. for fileURL in openPanel.urls {
  95. self.choosePath = fileURL.path
  96. self.beginQueueOperation()
  97. }
  98. }
  99. }
  100. }
  101. func fetchFileListViewController() -> KMBatchOperateLeftViewController? {
  102. guard let vc = self.view.window?.contentViewController as? KMBatchOperateSplitViewController,
  103. let viewController = vc.splitViewItems.first?.viewController as? KMBatchOperateLeftViewController
  104. else {
  105. return nil
  106. }
  107. return viewController
  108. }
  109. func beginQueueOperation() {//业务逻辑转移到数据处理层
  110. // hiddenWindowCloseButtonIfNeeded()
  111. self.successFilePathURLArray?.removeAll()
  112. for i in 0..<self.files!.count {
  113. let file = self.files![i]
  114. if file.fileType == .PDF {
  115. if self.operateType == .Convert {
  116. file.convertType = self.convertType ?? .WordAdvance
  117. }
  118. file.currentOperateInfo?.savePath = self.choosePath
  119. let operation = operationWithFile(file)
  120. operation.delegate = self
  121. self.queue?.addOperation(operation)
  122. }
  123. }
  124. if self.queue?.operations.count ?? 0 > 0 {
  125. self.interfaceStatus = .Processing
  126. }
  127. }
  128. func operationWithFile(_ file: KMBatchOperateFile) -> KMBatchOperation {
  129. var operation: KMBatchOperation?
  130. if self.operateType == .Convert {
  131. let batchOperate = KMBatchConvertOperation(file: file, convertType: self.convertType!)
  132. batchOperate.isAllowOCR = file.isAllowOCR
  133. batchOperate.ocrLanguage = file.ocrLanguage
  134. batchOperate.viewController = self
  135. return batchOperate
  136. } else if self.operateType == .Compress {
  137. let batchOperate = KMCompressOperation(file: file, compressValue: 60)
  138. batchOperate.viewController = self
  139. return batchOperate
  140. } else if self.operateType == .AddPassword {
  141. // handle AddPassword operation
  142. } else if self.operateType == .RemovePassword {
  143. return KMBatchRemovePasswordOperation(file: file)
  144. } else if self.operateType == .AddWatermark {
  145. // handle AddWatermark operation
  146. } else if self.operateType == .RemoveWatermark {
  147. } else if self.operateType == .AddBackground {
  148. // handle AddBackground operation
  149. } else if self.operateType == .RemoveBackground {
  150. } else if self.operateType == .AddHeaderFooter {
  151. // handle AddHeaderFooter operation
  152. } else if self.operateType == .RemoveHeaderFooter {
  153. return KMBatchRemoveHeaderFooterOperation(file: file)
  154. } else if self.operateType == .AddBates {
  155. } else if self.operateType == .RemoveBates {
  156. var op = KMBatchRemoveHeaderFooterOperation(file: file)
  157. op.isBates = true
  158. return op
  159. } else {
  160. }
  161. return operation ?? KMBatchOperation(file: file)
  162. }
  163. func cancelBatchOperation() {
  164. for i in 0..<(self.queue?.operations.count)! {
  165. let operation = self.queue?.operations[i]
  166. if !operation!.isExecuting {
  167. operation?.cancel()
  168. } else {
  169. // Do nothing
  170. }
  171. }
  172. self.interfaceStatus = .PrepareProcess
  173. }
  174. func hiddenWindowCloseButtonIfNeeded() {
  175. let btn = self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)
  176. btn?.isHidden = true
  177. }
  178. func allPageNumbers(_ pageString: String) -> [NSNumber] {
  179. let array = pageString.components(separatedBy: ",")
  180. let pageNumbers = NSMutableArray()
  181. var isInvalid = false
  182. for s in array {
  183. if s == "" {
  184. isInvalid = true
  185. break
  186. } else {
  187. let pages = s.components(separatedBy: "-")
  188. if pages.count > 2 {
  189. isInvalid = true
  190. break
  191. } else if pages.count == 1 {
  192. let p = pages[0]
  193. if p == "" || Int(p) == 0 {
  194. isInvalid = true
  195. break
  196. } else {
  197. var isEqual = false
  198. for pageNumber in pageNumbers {
  199. if (pageNumber as AnyObject).intValue == Int(p) {
  200. isEqual = true
  201. isInvalid = true
  202. break
  203. }
  204. }
  205. if !isEqual {
  206. pageNumbers.add(NSNumber(value: Int(p)!))
  207. }
  208. }
  209. } else if pages.count == 2 {
  210. let p1 = pages[0]
  211. let p2 = pages[1]
  212. if p1 == "" || p2 == "" || Int(p1)! >= Int(p2)! || Int(p1) == 0 {
  213. isInvalid = true
  214. break
  215. } else {
  216. var isEqual = false
  217. for i in Int(p1)!...Int(p2)! {
  218. for pageNumber in pageNumbers {
  219. if (pageNumber as AnyObject).intValue == i {
  220. isEqual = true
  221. isInvalid = true
  222. break
  223. }
  224. }
  225. }
  226. if !isEqual {
  227. for i in Int(p1)!...Int(p2)! {
  228. pageNumbers.add(NSNumber(value: i))
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. return pageNumbers as! [NSNumber]
  236. }
  237. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  238. if keyPath == "operations" && context == &kBatchQueueOperationsChanged {
  239. if self.queue?.operations.count == 0 {
  240. DispatchQueue.main.async {
  241. self.operateCompleted()
  242. }
  243. }
  244. } else {
  245. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  246. }
  247. }
  248. }
  249. extension KMBatchOperateBaseViewController{
  250. func fileBeginOperate(_ file: KMBatchOperateFile, info: KMBatchBaseParameter) {
  251. DispatchQueue.main.async {
  252. info.status = .processing
  253. self.fetchFileListViewController()?.reloadFile(file)
  254. }
  255. }
  256. func fileOperating(_ file: KMBatchOperateFile, progress: CGFloat, info: KMBatchBaseParameter) {
  257. DispatchQueue.main.async {
  258. info.progress = Float(progress)
  259. self.fetchFileListViewController()?.reloadFile(file)
  260. }
  261. }
  262. func fileOperateSuccessed(_ file: KMBatchOperateFile, info: KMBatchBaseParameter) {
  263. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  264. info.status = .Success
  265. if info.outPutPath?.count ?? 0 > 0{
  266. let url: URL = URL(fileURLWithPath: info.outPutPath!)
  267. self.successFilePathURLArray?.append(url)
  268. }
  269. self.fetchFileListViewController()?.reloadFile(file)
  270. }
  271. }
  272. func fileOperateFailed(_ file: KMBatchOperateFile, error: NSError, info: KMBatchBaseParameter) {
  273. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  274. info.status = .Failed
  275. info.error = error
  276. self.fetchFileListViewController()?.reloadData()
  277. }
  278. }
  279. func fileOperateCanceled(_ file: KMBatchOperateFile, info: KMBatchBaseParameter) {
  280. DispatchQueue.main.async {
  281. info.status = .Waiting
  282. info.error = nil
  283. self.fetchFileListViewController()?.reloadFile(file)
  284. }
  285. }
  286. }