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