KMBatchOperateBaseViewController.swift 12 KB

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