KMBatchOperateBaseViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.viewController = self
  134. return batchOperate
  135. } else if self.operateType == .Compress {
  136. let batchOperate = KMCompressOperation(file: file, compressValue: 60)
  137. batchOperate.viewController = self
  138. return batchOperate
  139. } else if self.operateType == .AddPassword {
  140. // handle AddPassword operation
  141. } else if self.operateType == .RemovePassword {
  142. return KMBatchRemovePasswordOperation(file: file)
  143. } else if self.operateType == .AddWatermark {
  144. // handle AddWatermark operation
  145. } else if self.operateType == .RemoveWatermark {
  146. return KMBatchRemoveWatermarkOperation(file: file)
  147. } else if self.operateType == .AddBackground {
  148. // handle AddBackground operation
  149. } else if self.operateType == .RemoveBackground {
  150. return KMBatchRemoveBackgroundOperation(file: file)
  151. } else if self.operateType == .AddHeaderFooter {
  152. // handle AddHeaderFooter operation
  153. } else if self.operateType == .RemoveHeaderFooter {
  154. return KMBatchRemoveHeaderFooterOperation(file: file)
  155. } else if self.operateType == .AddBates {
  156. // operation = KMBatchRemoveHeaderFooterOperation(file: file)
  157. } else if self.operateType == .RemoveBates {
  158. var op = KMBatchRemoveHeaderFooterOperation(file: file)
  159. op.isBates = true
  160. return op
  161. } else {
  162. }
  163. return operation ?? KMBatchOperation(file: file)
  164. }
  165. func cancelBatchOperation() {
  166. for i in 0..<(self.queue?.operations.count)! {
  167. let operation = self.queue?.operations[i]
  168. if !operation!.isExecuting {
  169. operation?.cancel()
  170. } else {
  171. // Do nothing
  172. }
  173. }
  174. self.interfaceStatus = .PrepareProcess
  175. }
  176. func hiddenWindowCloseButtonIfNeeded() {
  177. let btn = self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)
  178. btn?.isHidden = true
  179. }
  180. func allPageNumbers(_ pageString: String) -> [NSNumber] {
  181. let array = pageString.components(separatedBy: ",")
  182. let pageNumbers = NSMutableArray()
  183. var isInvalid = false
  184. for s in array {
  185. if s == "" {
  186. isInvalid = true
  187. break
  188. } else {
  189. let pages = s.components(separatedBy: "-")
  190. if pages.count > 2 {
  191. isInvalid = true
  192. break
  193. } else if pages.count == 1 {
  194. let p = pages[0]
  195. if p == "" || Int(p) == 0 {
  196. isInvalid = true
  197. break
  198. } else {
  199. var isEqual = false
  200. for pageNumber in pageNumbers {
  201. if (pageNumber as AnyObject).intValue == Int(p) {
  202. isEqual = true
  203. isInvalid = true
  204. break
  205. }
  206. }
  207. if !isEqual {
  208. pageNumbers.add(NSNumber(value: Int(p)!))
  209. }
  210. }
  211. } else if pages.count == 2 {
  212. let p1 = pages[0]
  213. let p2 = pages[1]
  214. if p1 == "" || p2 == "" || Int(p1)! >= Int(p2)! || Int(p1) == 0 {
  215. isInvalid = true
  216. break
  217. } else {
  218. var isEqual = false
  219. for i in Int(p1)!...Int(p2)! {
  220. for pageNumber in pageNumbers {
  221. if (pageNumber as AnyObject).intValue == i {
  222. isEqual = true
  223. isInvalid = true
  224. break
  225. }
  226. }
  227. }
  228. if !isEqual {
  229. for i in Int(p1)!...Int(p2)! {
  230. pageNumbers.add(NSNumber(value: i))
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. return pageNumbers as! [NSNumber]
  238. }
  239. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  240. if keyPath == "operations" && context == &kBatchQueueOperationsChanged {
  241. if self.queue?.operations.count == 0 {
  242. DispatchQueue.main.async {
  243. self.operateCompleted()
  244. }
  245. }
  246. } else {
  247. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  248. }
  249. }
  250. }
  251. extension KMBatchOperateBaseViewController{
  252. func fileBeginOperate(_ file: KMBatchOperateFile, info: KMBatchBaseParameter) {
  253. DispatchQueue.main.async {
  254. info.status = .processing
  255. self.fetchFileListViewController()?.reloadFile(file)
  256. }
  257. }
  258. func fileOperating(_ file: KMBatchOperateFile, progress: CGFloat, info: KMBatchBaseParameter) {
  259. DispatchQueue.main.async {
  260. info.progress = Float(progress)
  261. self.fetchFileListViewController()?.reloadFile(file)
  262. }
  263. }
  264. func fileOperateSuccessed(_ file: KMBatchOperateFile, info: KMBatchBaseParameter) {
  265. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  266. info.status = .Success
  267. if info.outPutPath?.count ?? 0 > 0{
  268. let url: URL = URL(fileURLWithPath: info.outPutPath!)
  269. self.successFilePathURLArray?.append(url)
  270. }
  271. self.fetchFileListViewController()?.reloadFile(file)
  272. }
  273. }
  274. func fileOperateFailed(_ file: KMBatchOperateFile, error: NSError, info: KMBatchBaseParameter) {
  275. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  276. info.status = .Failed
  277. info.error = error
  278. self.fetchFileListViewController()?.reloadData()
  279. }
  280. }
  281. func fileOperateCanceled(_ file: KMBatchOperateFile, info: KMBatchBaseParameter) {
  282. DispatchQueue.main.async {
  283. info.status = .Waiting
  284. info.error = nil
  285. self.fetchFileListViewController()?.reloadFile(file)
  286. }
  287. }
  288. }