KMBatchOperateAddHeaderFooterViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // KMBatchOperateAddHeaderFooterViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/7.
  6. //
  7. import Cocoa
  8. class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewController, NSTableViewDelegate,NSTableViewDataSource{
  9. var isBates = false
  10. var onlyManagerTemplate = false
  11. var isBatchOperation = false
  12. var pdfView: CPDFView?
  13. @IBOutlet var topBaseView: NSView!
  14. @IBOutlet var bottomBaseView: NSView!
  15. @IBOutlet var tableView: NSTableView!
  16. @IBOutlet var titleLabel: NSTextField!
  17. @IBOutlet var actionButton: NSButton!
  18. @IBOutlet var managerTemplateTitleLabel: NSTextField!
  19. @IBOutlet var addButton: NSButton!
  20. @IBOutlet var blankView: KMBlankView!
  21. @IBOutlet var managerTemplateTopConstraint: NSLayoutConstraint!
  22. @IBOutlet var managerTemplateButtonHeightConstraint: NSButton!
  23. @IBOutlet var topHeightConstraint: NSLayoutConstraint!
  24. @IBOutlet var lineView: NSView!
  25. var haveFiles = false
  26. var currentObject: KMHeaderFooterObject?
  27. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  28. set{
  29. super.interfaceStatus = newValue
  30. if newValue == .PrepareProcess {
  31. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  32. let files = NSMutableArray()
  33. for url in self.successFilePathURLArray! {
  34. if FileManager.default.fileExists(atPath: url.path) {
  35. files.add(url)
  36. }
  37. }
  38. if files.count > 0 {
  39. let workspace = NSWorkspace.shared
  40. workspace.activateFileViewerSelecting(files as! [URL])
  41. }
  42. }
  43. self.actionButton.tag = 1
  44. self.actionButton.title = KMLocalizedString("Apply", nil)
  45. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  46. } else {
  47. self.actionButton.tag = 0
  48. self.actionButton.title = KMLocalizedString("Cancel", nil)
  49. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  50. }
  51. }
  52. get{
  53. return super.interfaceStatus
  54. }
  55. }
  56. deinit {
  57. NotificationCenter.default.removeObserver(self)
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. localizedLanguage()
  62. configuInterface()
  63. NotificationCenter.default.addObserver(self, selector: #selector(headerFootersNotification(notification:)), name: Notification.Name(rawValue: "KMBatchOperateHeaderFootersNotification"), object: nil)
  64. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  65. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  66. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  67. }
  68. func localizedLanguage() {
  69. self.addButton.title = " " + KMLocalizedString("Add Template", nil)
  70. self.actionButton.title = KMLocalizedString("Apply", nil)
  71. self.titleLabel.stringValue = KMLocalizedString("Apply", nil)
  72. }
  73. func configuInterface() {
  74. self.titleLabel.font = NSFont.systemFont(ofSize: 14)
  75. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  76. self.tableView.enclosingScrollView?.borderType = .noBorder
  77. self.actionButton.wantsLayer = true
  78. self.addButton.wantsLayer = true
  79. self.topHeightConstraint.constant = 0
  80. var color = NSColor(red: 227.0/255.0, green: 228.0/255.0, blue: 230.0/255.0, alpha: 1)
  81. if KMAppearance.isSupportNewColor() {
  82. if KMAppearance.isDarkMode() {
  83. color = NSColor(red: 59.0/255.0, green: 61/255.0, blue: 64.0/255.0, alpha: 1)
  84. }
  85. }
  86. self.lineView.wantsLayer = true
  87. self.lineView.layer?.backgroundColor = color.cgColor
  88. self.addButton.imagePosition = .imageLeft
  89. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  90. self.addButton.layer?.backgroundColor = KMAppearance.Interactive.s0Color().cgColor
  91. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  92. if (onlyManagerTemplate){
  93. managerTemplateTitleLabel.isHidden = false
  94. titleLabel.isHidden = true
  95. self.topBaseView.isHidden = false
  96. topHeightConstraint.constant = 40
  97. }else{
  98. managerTemplateTitleLabel.isHidden = true
  99. titleLabel.isHidden = true
  100. self.topBaseView.isHidden = true
  101. topHeightConstraint.constant = 0
  102. }
  103. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  104. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  105. self.actionButton.imagePosition = .noImage
  106. if !self.onlyManagerTemplate {
  107. if self.files?.count ?? 0 > 0 {
  108. self.haveFiles = true
  109. } else {
  110. self.haveFiles = false
  111. }
  112. } else {
  113. self.haveFiles = true
  114. }
  115. updateViewColor()
  116. updateActionButtonbackgroundColor()
  117. self.addButton.layer?.cornerRadius = 1.0
  118. self.actionButton.layer?.cornerRadius = 1.0
  119. self.topBaseView.wantsLayer = true
  120. self.topBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  121. self.bottomBaseView.wantsLayer = true
  122. self.bottomBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  123. self.managerTemplateTitleLabel.font = NSFont.systemFont(ofSize: 14)
  124. self.managerTemplateTitleLabel.textColor = KMAppearance.Layout.h0Color()
  125. self.addButton.isHidden = false
  126. self.managerTemplateTitleLabel.stringValue = NSLocalizedString("Manage Templates", comment: "")
  127. self.view.addSubview(self.blankView)
  128. self.blankView.mas_makeConstraints { make in
  129. make?.top.equalTo()(self.topBaseView.mas_bottom)
  130. make?.left.equalTo()(self.view)
  131. make?.right.equalTo()(self.view)
  132. make?.bottom.equalTo()(self.bottomBaseView.mas_top)
  133. make?.height.greaterThanOrEqualTo()(200)
  134. }
  135. self.blankView.titleLabel.stringValue = KMLocalizedString("No Templates", nil)
  136. self.tableView.backgroundColor = KMAppearance.Layout.l0Color()
  137. let menu = NSMenu(title: "")
  138. if !self.isBatchOperation && !self.isBates {
  139. menu.addItem(withTitle: NSLocalizedString("Batch Add Header & Footer", comment: ""), action: #selector(buttonItemClick_addBatch(_:)), keyEquivalent: "")
  140. } else if !self.isBatchOperation && self.isBates {
  141. menu.addItem(withTitle: NSLocalizedString("Batch Add Bates Numbers", comment: ""), action: #selector(buttonItemClick_addBatch(_:)), keyEquivalent: "")
  142. }
  143. if !self.isBates {
  144. menu.addItem(withTitle: NSLocalizedString("Remove All Header & Footer Templates", comment: ""), action: #selector(buttonItemClick_CleanAll(_:)), keyEquivalent: "")
  145. } else {
  146. menu.addItem(withTitle: NSLocalizedString("Remove All Bates Numbers Templates", comment: ""), action: #selector(buttonItemClick_CleanAll(_:)), keyEquivalent: "")
  147. }
  148. self.view.menu = menu
  149. }
  150. func updateActionButtonbackgroundColor() {
  151. let row = self.tableView.selectedRow
  152. if (self.files?.count ?? 0 > 0 || self.pdfView != nil) && row > -1 {
  153. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  154. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  155. } else {
  156. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  157. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  158. }
  159. }
  160. @objc func buttonItemClick_addBatch(_ sender: Any) {
  161. let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
  162. if #available(macOS 10.13, *) {
  163. baseWindowController.window?.makeKeyAndOrderFront(nil)
  164. } else {
  165. baseWindowController.showWindow(nil)
  166. }
  167. let arr = NSMutableArray()
  168. let file = KMBatchOperateFile(filePath: self.pdfView!.document!.documentURL!.path, type: self.isBates ? .AddBates : .AddHeaderFooter)
  169. arr.add(file)
  170. baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: self.isBates ? .AddBates : .AddHeaderFooter, files: arr as! [KMBatchOperateFile])
  171. }
  172. @objc func buttonItemClick_CleanAll(_ sender: Any) {
  173. let alert = NSAlert()
  174. alert.alertStyle = .warning
  175. alert.messageText = ""
  176. alert.informativeText = NSLocalizedString("Are you sure to delete all templates?", comment: "")
  177. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  178. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  179. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  180. if response == .alertFirstButtonReturn {
  181. self.deleteAll()
  182. }
  183. }
  184. }
  185. @objc func headerFootersNotification(notification: Notification) {
  186. if let addHeaderFooter = notification.object as? KMBatchOperateAddHeaderFooterViewController {
  187. if self != addHeaderFooter {
  188. loadData()
  189. }
  190. }
  191. }
  192. @objc func themeChanged(notification: Notification) {
  193. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  194. self.updateViewColor()
  195. }
  196. }
  197. @objc func batchFilesCountNotification(notification: Notification) {
  198. let files: Array? = notification.object as? [KMBatchOperateFile]
  199. if let data = files?.first {
  200. if data.currentOperateType != self.operateType {
  201. return
  202. }
  203. }
  204. let viewC = notification.userInfo?[kObjectKey] as? NSViewController
  205. if let data = self.view.window?.isEqual(to: viewC?.view.window), data == false {
  206. return
  207. }
  208. self.files? = files ?? []
  209. if files?.count ?? 0 > 0 {
  210. haveFiles = true
  211. } else {
  212. haveFiles = false
  213. }
  214. updateActionButtonbackgroundColor()
  215. }
  216. func loadData() {
  217. self.tableView.reloadData()
  218. }
  219. func updateViewColor() {
  220. if KMAppearance.isDarkMode() {
  221. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  222. self.addButton.layer?.backgroundColor = NSColor(red: 0.337, green: 0.345, blue: 0.353, alpha: 1).cgColor
  223. self.addButton.setTitleColor(NSColor.white)
  224. self.actionButton.layer?.backgroundColor = NSColor(red: 0.306, green: 0.498, blue: 0.859, alpha: 1).cgColor
  225. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  226. } else {
  227. self.bottomBaseView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1).cgColor
  228. self.addButton.layer?.backgroundColor = NSColor(red: 0.855, green: 0.859, blue: 0.871, alpha: 1).cgColor
  229. self.addButton.setTitleColor(NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1))
  230. self.actionButton.layer?.backgroundColor = NSColor(red: 0.153, green: 0.235, blue: 0.384, alpha: 1).cgColor
  231. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  232. }
  233. }
  234. func deleteAll() {
  235. if self.isBates {
  236. for waterMark in KMHeaderFooterManager.defaultManager.onlyBatesObjects {
  237. KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
  238. }
  239. } else {
  240. for waterMark in KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects {
  241. KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
  242. }
  243. }
  244. reloadTable()
  245. loadData()
  246. postNotification()
  247. }
  248. func reloadTable() {
  249. self.tableView.noteNumberOfRowsChanged()
  250. var count = 0
  251. var array: [KMHeaderFooterObject] = []
  252. if self.isBates {
  253. count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
  254. array = KMHeaderFooterManager.defaultManager.onlyBatesObjects
  255. } else {
  256. count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
  257. array = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects
  258. }
  259. if array.contains(self.currentObject ?? KMHeaderFooterObject()) {
  260. let row = array.firstIndex(of: self.currentObject!)!
  261. let indexSet = IndexSet(integer: row)
  262. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  263. }
  264. updateActionButtonbackgroundColor()
  265. }
  266. func postNotification() {
  267. NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateHeaderFootersNotification"), object: self)
  268. }
  269. @IBAction func buttonClicked_AddHeaderFooter(_ sender: Any) {
  270. // guard let windowController = self.view.window?.windowController else { return }
  271. // var filePath: String?
  272. // var password: String?
  273. // if let mainWC = windowController as? MainWindowController {
  274. // // filePath = mainWC.pdfView?.document?.documentURL?.path
  275. // // password = (mainWC.pdfView?.document as? CPDFDocument)?.password
  276. // }
  277. // let wc = KMHeaderFooterManagerWindowController(baseFile: filePath, headerFooter: nil, password: password, type: .KMBatchModifyTemplateType_Add)
  278. // wc.isBates = self.isBates
  279. // wc.operateCallBack = { [weak self] obj in
  280. // self?.currentObject = obj
  281. // self?.loadData()
  282. // self?.postNotification()
  283. // DispatchQueue.main.async {
  284. // guard let strongSelf = self else {
  285. // return
  286. // }
  287. // strongSelf.tableView.selectRowIndexes(IndexSet(integer: 0), byExtendingSelection: false)
  288. // }
  289. // }
  290. // wc.beginSheetModal(for: NSApp.keyWindow!, completionHandler: nil)
  291. let filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  292. var cdocument = self.pdfView?.document
  293. if isBatchOperation {
  294. cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  295. }
  296. let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
  297. windowController.isBatch = isBatchOperation
  298. windowController.type = .Add
  299. windowController.isBates = isBates
  300. windowController.pdfDocument = cdocument
  301. windowController.cancelAction = { [weak self] controller in
  302. self?.km_endSheet()
  303. }
  304. windowController.operateCallBack = { [weak self] wController, model in
  305. self?.currentObject = model
  306. self?.loadData()
  307. self?.postNotification()
  308. let set = NSIndexSet(index: 0)
  309. self?.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  310. }
  311. self.km_beginSheet(windowC: windowController)
  312. }
  313. @IBAction func buttonClicked_Action(_ sender: NSButton) {
  314. if (!self.onlyManagerTemplate) {
  315. if (!self.haveFiles) {
  316. return
  317. }
  318. }
  319. if (self.onlyManagerTemplate) {
  320. if (sender.tag == 1) {
  321. var headFooter: KMHeaderFooterObject?
  322. let row = self.tableView.selectedRow
  323. if (row == -1) {
  324. NSSound.beep()
  325. return
  326. } else {
  327. if (self.isBates) {
  328. headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  329. } else {
  330. headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  331. }
  332. }
  333. let openPanel = NSOpenPanel()
  334. openPanel.canChooseFiles = false
  335. openPanel.canChooseDirectories = true
  336. openPanel.canCreateDirectories = true
  337. openPanel.beginSheetModal(for: self.view.window!) { result in
  338. if (result == .OK) {
  339. for fileURL in openPanel.urls {
  340. self.hiddenWindowCloseButtonIfNeeded()
  341. self.successFilePathURLArray?.removeAll()
  342. if (self.isBates) {
  343. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBates)
  344. if file.fileType == .PDF {
  345. file.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
  346. file.addBatesInfo.pageRangeString = headFooter?.pageRangeString
  347. file.addBatesInfo.savePath = fileURL.path
  348. if file.status == .Waiting {
  349. let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
  350. op.delegate = self
  351. self.queue?.addOperation(op)
  352. }
  353. }
  354. } else {
  355. self.successFilePathURLArray?.removeAll()
  356. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddHeaderFooter)
  357. if file.fileType == .PDF {
  358. file.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
  359. file.addHeaderFooterInfo.pageRangeString = headFooter?.pageRangeString
  360. file.addHeaderFooterInfo.savePath = fileURL.path
  361. if file.status == .Waiting {
  362. let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
  363. op.delegate = self
  364. self.queue?.addOperation(op)
  365. }
  366. }
  367. }
  368. if self.queue?.operations.count ?? 0 > 0 {
  369. self.interfaceStatus = .Processing
  370. }
  371. }
  372. }
  373. }
  374. } else if (sender.tag == 0) {
  375. if let cnt = self.queue?.operations.count, cnt > 0 {
  376. self.queue?.cancelAllOperations()
  377. }
  378. self.interfaceStatus = .PrepareProcess
  379. }
  380. } else {
  381. //点击开始
  382. if (sender.tag == 1) {
  383. if (!self.checkAndResetTask()) {
  384. NSSound.beep()
  385. return;
  386. }
  387. var headFooter: KMHeaderFooterObject?
  388. let row = self.tableView.selectedRow
  389. if (row == -1 || self.files?.count ?? 0 < 1) {
  390. NSSound.beep()
  391. return;
  392. } else {
  393. if (self.isBates) {
  394. headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  395. } else {
  396. headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  397. }
  398. }
  399. self.choosePathAndBeginOperation(headFooter!)
  400. } else if (sender.tag == 0) {
  401. if let cnt = self.queue?.operations.count, cnt > 0 {
  402. self.queue?.cancelAllOperations()
  403. }
  404. self.interfaceStatus = .PrepareProcess
  405. }
  406. }
  407. }
  408. func checkAndResetTask() -> Bool {
  409. if files?.count ?? 0 < 1 {
  410. return false
  411. }
  412. for i in 0..<(files?.count ?? 0) {
  413. if let file = files?[i] as? KMBatchOperateFile {
  414. if isBates {
  415. file.addBatesInfo.resetState()
  416. } else {
  417. file.addHeaderFooterInfo.resetState()
  418. }
  419. }
  420. }
  421. return true
  422. }
  423. func choosePathAndBeginOperation(_ obj: Any) {
  424. let openPanel = NSOpenPanel()
  425. openPanel.canChooseFiles = false
  426. openPanel.canChooseDirectories = true
  427. openPanel.canCreateDirectories = true
  428. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  429. if result == NSApplication.ModalResponse.OK {
  430. for fileURL in openPanel.urls {
  431. self.choosePath = fileURL.path
  432. if self.isBates {
  433. self.beginAddBates(obj as! KMHeaderFooterObject)
  434. } else {
  435. self.beginAddHeaderFooter(obj as! KMHeaderFooterObject)
  436. }
  437. }
  438. }
  439. }
  440. }
  441. func beginAddBates(_ bates: KMHeaderFooterObject) {
  442. hiddenWindowCloseButtonIfNeeded()
  443. successFilePathURLArray?.removeAll()
  444. for i in 0..<(files?.count ?? 0) {
  445. if let file = files?[i] as? KMBatchOperateFile {
  446. if file.fileType == .PDF {
  447. file.addBatesInfo.savePath = choosePath
  448. if file.status == .Waiting {
  449. let operation = KMBatchAddHeaderFooterOperation(file: file, headerFooter: bates)
  450. operation.delegate = self
  451. queue?.addOperation(operation)
  452. }
  453. }
  454. }
  455. }
  456. if queue?.operationCount ?? 0 > 0 {
  457. interfaceStatus = .Processing
  458. }
  459. }
  460. func beginAddHeaderFooter(_ headerFooter: KMHeaderFooterObject) {
  461. hiddenWindowCloseButtonIfNeeded()
  462. successFilePathURLArray?.removeAll()
  463. for i in 0..<(files?.count ?? 0) {
  464. if let file = files?[i] as? KMBatchOperateFile {
  465. if file.fileType == .PDF {
  466. file.addHeaderFooterInfo.savePath = choosePath
  467. if file.status == .Waiting {
  468. let operation = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headerFooter)
  469. operation.delegate = self
  470. queue?.addOperation(operation)
  471. }
  472. }
  473. }
  474. }
  475. if queue?.operationCount ?? 0 > 0 {
  476. interfaceStatus = .Processing
  477. }
  478. }
  479. func modify(obj: KMHeaderFooterObject) {
  480. // let windowController = self.view.window?.windowController
  481. // var filePath: String? = nil
  482. // var password: String? = nil
  483. // if let mainWindowController = windowController as? MainWindowController {
  484. // filePath = mainWindowController.mainViewController.listView?.document?.documentURL?.path
  485. // let document = mainWindowController.mainViewController.listView?.document as? CPDFDocument
  486. // password = document?.password
  487. // }
  488. // let wc = KMHeaderFooterManagerWindowController(baseFile: filePath, headerFooter: obj, password: password, type: .KMBatchModifyTemplateType_Edit)
  489. // wc?.isBates = self.isBates
  490. // let blockSelf = self
  491. // wc?.operateCallBack = { obj in
  492. // blockSelf.currentObject = obj
  493. // blockSelf.loadData()
  494. // blockSelf.reloadTable()
  495. // blockSelf.postNotification()
  496. // }
  497. // wc?.beginSheetModalForWindow(NSApp.keyWindow, completionHandler: nil)
  498. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  499. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  500. filePath = self.pdfView?.document.documentURL.path ?? ""
  501. }
  502. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  503. let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
  504. windowController.headerFooterObj = obj
  505. windowController.isBatch = isBatchOperation
  506. windowController.type = .Edit
  507. windowController.isBates = isBates
  508. windowController.pdfDocument = cdocument
  509. windowController.cancelAction = { [weak self] controller in
  510. self?.km_endSheet()
  511. }
  512. windowController.operateCallBack = { wController, model in
  513. self.currentObject = model
  514. self.loadData()
  515. self.postNotification()
  516. let set = NSIndexSet(index: 0)
  517. self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  518. }
  519. self.km_beginSheet(windowC: windowController)
  520. }
  521. func delete(obj: KMHeaderFooterObject) {
  522. KMHeaderFooterManager.defaultManager.removeHeaderFooter(obj)
  523. loadData()
  524. reloadTable()
  525. postNotification()
  526. }
  527. func headerFooterInterfaceSelectHeaderFooter(headerFooter: KMHeaderFooterObject) {
  528. self.tableView.reloadData()
  529. if let index = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.firstIndex(of: headerFooter) {
  530. let indexSet = IndexSet(integer: index)
  531. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  532. }
  533. }
  534. func batesInterfaceSelectBates(headerFooter: KMHeaderFooterObject) {
  535. self.tableView.reloadData()
  536. if let index = KMHeaderFooterManager.defaultManager.onlyBatesObjects.firstIndex(of: headerFooter) {
  537. let indexSet = IndexSet(integer: index)
  538. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  539. }
  540. }
  541. func numberOfRows(in tableView: NSTableView) -> Int {
  542. var count = 0
  543. if self.isBates {
  544. count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
  545. } else {
  546. count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
  547. }
  548. self.blankView.isHidden = count != 0
  549. return count
  550. }
  551. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  552. var obj: KMHeaderFooterObject? = nil
  553. if self.isBates {
  554. obj = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  555. } else {
  556. obj = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  557. }
  558. guard let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "mainCell"), owner: self) as? KMHeaderFooterTableCellView else {
  559. return nil
  560. }
  561. cellView.updateInterface(obj!)
  562. cellView.headerFooterTableCellViewCallback = { type in
  563. if type == .Edit {
  564. let indexSet = IndexSet(integer: row)
  565. self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
  566. self.currentObject = obj
  567. self.modify(obj: obj!)
  568. } else {
  569. self.delete(obj: obj!)
  570. }
  571. }
  572. return cellView
  573. }
  574. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  575. let rowView = KMTableRowView()
  576. return rowView
  577. }
  578. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  579. if self.isBates {
  580. return KMHeaderFooterManager.defaultManager.onlyBatesObjects[row].cellHeight
  581. } else {
  582. return KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row].cellHeight
  583. }
  584. }
  585. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  586. return true
  587. }
  588. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  589. return true
  590. }
  591. func tableViewSelectionDidChange(_ notification: Notification) {
  592. let row = self.tableView.selectedRow
  593. updateActionButtonbackgroundColor()
  594. if row == -1 { return }
  595. if self.isBates {
  596. let bates = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
  597. self.currentObject = bates
  598. for i in 0..<(self.files?.count ?? 0) {
  599. let file = self.files?[i]
  600. file?.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: bates.pageRangeType.rawValue) ?? .All
  601. if file?.addBatesInfo.pageChoice == .Input {
  602. let arr = allPageNumbers(bates.pageRangeString)
  603. let sortedArray: NSArray = file!.pagesArrayIntersect(with: arr) as NSArray
  604. if sortedArray.count < 1 {
  605. file?.addBatesInfo.pageChoice = .All
  606. } else {
  607. file!.addBatesInfo.pageRangeString = sortedArray.componentsJoined(by: ",")
  608. }
  609. }
  610. }
  611. } else {
  612. let headerFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
  613. self.currentObject = headerFooter
  614. for i in 0..<(self.files?.count ?? 0) {
  615. let file = self.files?[i]
  616. file?.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headerFooter.pageRangeType.rawValue) ?? .All
  617. if file?.addHeaderFooterInfo.pageChoice == .Input {
  618. let arr = allPageNumbers(headerFooter.pageRangeString)
  619. let sortedArray: NSArray = file!.pagesArrayIntersect(with: arr) as NSArray
  620. if sortedArray.count < 1 {
  621. file?.addHeaderFooterInfo.pageChoice = .All
  622. } else {
  623. file!.addHeaderFooterInfo.pageRangeString = sortedArray.componentsJoined(by: ",")
  624. }
  625. }
  626. }
  627. }
  628. reloadTable()
  629. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNeedChangePageRangeNotification), object: nil)
  630. }
  631. }
  632. extension KMBatchOperateAddHeaderFooterViewController: NSMenuDelegate, NSMenuItemValidation{
  633. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  634. let action = menuItem.action
  635. if action == #selector(buttonItemClick_CleanAll(_:)) {
  636. if (self.isBates && KMHeaderFooterManager.defaultManager.onlyBatesObjects.count < 1) || (!self.isBates && KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count < 1){
  637. return false
  638. }
  639. return true
  640. }
  641. return true
  642. }
  643. }