KMBatchOperateConvertViewController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //
  2. // KMBatchOperateConvertViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/11/1.
  6. //
  7. import Cocoa
  8. typealias detailInfoViewMouseDownCallback = (_ mouseDown: Bool) -> ()
  9. class KMDetailInfoView: NSView{
  10. var mouseDownCallback: detailInfoViewMouseDownCallback?
  11. override func mouseDown(with event: NSEvent) {
  12. super.mouseDown(with: event)
  13. guard let callBack = mouseDownCallback else { return }
  14. callBack(true)
  15. }
  16. }
  17. class KMBatchOperateConvertViewController: KMBatchOperateBaseViewController,NSCollectionViewDelegate,NSCollectionViewDataSource{
  18. var dataSourcesArray: [KMConvertWithPDFType]?
  19. @IBOutlet var collectionView: NSCollectionView!
  20. @IBOutlet var bottomView: NSView!
  21. @IBOutlet var convertEveryPageButton: NSButton!
  22. @IBOutlet var convertButton: NSButton!
  23. @IBOutlet var detailInfoView: KMDetailInfoView!
  24. @IBOutlet var detailInfoLabel: NSTextField!
  25. @IBOutlet var checkBoxTopConstraint: NSLayoutConstraint!
  26. @IBOutlet var containerViewTopConstraint: NSLayoutConstraint!
  27. @IBOutlet var checkBoxBottomConstaint: NSLayoutConstraint!
  28. @IBOutlet var convertEveryPageButtonLabel: NSTextField!
  29. @IBOutlet var extractButton: NSButton!
  30. @IBOutlet var wordButtonOne: NSButton!
  31. @IBOutlet var wordButtonTwo: NSButton!
  32. @IBOutlet var csvExtractButton: NSButton!
  33. @IBOutlet var onlyTextBtn: NSButton!
  34. @IBOutlet var onlyTableBtn: NSButton!
  35. @IBOutlet var allContentBtn: NSButton!
  36. @IBOutlet var tableMenu1: NSMenuItem!
  37. @IBOutlet var tableMenu2: NSMenuItem!
  38. @IBOutlet var tableMenu3: NSMenuItem!
  39. @IBOutlet var allContentMenu2: NSMenuItem!
  40. @IBOutlet var allContentMenu3: NSMenuItem!
  41. @IBOutlet var tableMenu: NSPopUpButton!
  42. @IBOutlet var allContentMenu: NSPopUpButton!
  43. var excelContentOption: CPDFConvertExcelContentOptions?
  44. var excelWorksheetOption: CPDFConvertExcelWorksheetOptions?
  45. var haveFiles: Bool = false
  46. deinit {
  47. NotificationCenter.default.removeObserver(self)
  48. }
  49. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  50. set{
  51. super.interfaceStatus = newValue
  52. self.convertButton.isEnabled = true
  53. if newValue == .Processing {
  54. self.collectionView.isSelectable = false
  55. self.convertButton.title = NSLocalizedString("Cancel", comment: "")
  56. self.convertButton.tag = 2
  57. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  58. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  59. if self.convertType == .WordStandard {
  60. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  61. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  62. self.convertButton.isEnabled = false
  63. }
  64. } else {
  65. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  66. var files: [URL] = []
  67. for url in self.successFilePathURLArray! {
  68. if FileManager.default.fileExists(atPath: url.path) {
  69. files.append(url)
  70. }
  71. }
  72. if files.count > 0 {
  73. let workspace = NSWorkspace.shared
  74. workspace.activateFileViewerSelecting(files)
  75. }
  76. }
  77. self.collectionView.isSelectable = true
  78. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  79. self.convertButton.tag = 1
  80. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  81. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  82. }
  83. }
  84. get{
  85. return super.interfaceStatus
  86. }
  87. }
  88. override var convertType: KMConvertWithPDFType?{
  89. set{
  90. super.convertType = newValue
  91. changeAllFilesToConvertType(convertType!)
  92. fetchFileListViewController()?.reloadConvertInterface(withType: convertType!)
  93. }
  94. get{
  95. return super.convertType
  96. }
  97. }
  98. func changeAllFilesToConvertType(_ type: KMConvertWithPDFType) {
  99. for i in 0..<self.files!.count {
  100. let file = self.files![i]
  101. file.convertType = type
  102. }
  103. }
  104. func switchToConvertType(_ convertType: KMConvertWithPDFType) {
  105. self.convertType = convertType
  106. self.manualSelectRow(convertType)
  107. self.updateBottomView()
  108. }
  109. func manualSelectRow(_ convertType: KMConvertWithPDFType) {
  110. self.collectionView.deselectAll(nil)
  111. self.collectionView.reloadData()
  112. var set = Set<IndexPath>()
  113. var index = -1
  114. for i in 0..<self.dataSourcesArray!.count {
  115. let number = self.dataSourcesArray![i]
  116. if number.rawValue == convertType.rawValue {
  117. index = i
  118. break
  119. }
  120. }
  121. if index != -1 {
  122. let indexPath = IndexPath(item: index, section: 0)
  123. set.insert(indexPath)
  124. self.collectionView.selectItems(at: set, scrollPosition: [])
  125. let selectionRect = self.collectionView.frameForItem(at: self.collectionView.selectionIndexes.first ?? 0)
  126. self.collectionView.scrollToVisible(selectionRect)
  127. }
  128. }
  129. func updateBottomView() {
  130. var moreLabelString = ""
  131. self.onlyTextBtn.isHidden = true
  132. self.onlyTableBtn.isHidden = true
  133. self.allContentBtn.isHidden = true
  134. self.tableMenu.isHidden = true
  135. self.allContentMenu.isHidden = true
  136. #if VERSION_FREE
  137. if !IAPProductsManager.default().isAvailableAllFunction() {
  138. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  139. }else {
  140. if !IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  141. if .WordAdvance == self.convertType ||
  142. .WordStandard == self.convertType ||
  143. .Excel == self.convertType ||
  144. .PowerPoint == self.convertType ||
  145. .CSV == self.convertType ||
  146. .RTF == self.convertType ||
  147. .GIF == self.convertType ||
  148. .TIFF == self.convertType ||
  149. .JPEG2000 == self.convertType ||
  150. .BMP == self.convertType ||
  151. .TGA == self.convertType {
  152. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  153. }
  154. }
  155. }
  156. #else
  157. // 付费版
  158. if !IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  159. if .WordAdvance == self.convertType ||
  160. .WordStandard == self.convertType ||
  161. .Excel == self.convertType ||
  162. .PowerPoint == self.convertType ||
  163. .CSV == self.convertType ||
  164. .RTF == self.convertType ||
  165. .GIF == self.convertType ||
  166. .TIFF == self.convertType ||
  167. .JPEG2000 == self.convertType ||
  168. .BMP == self.convertType ||
  169. .TGA == self.convertType {
  170. moreLabelString = String(format: "%@ %@", KMLocalizedString("The first 10 pages for free"), KMLocalizedString("Unlimited Convert"))
  171. }
  172. }
  173. #endif
  174. var hasInfo = false
  175. var isExcel = false
  176. self.detailInfoLabel.stringValue = moreLabelString
  177. if let range = moreLabelString.range(of: NSLocalizedString("Unlimited Convert", comment: "")) {
  178. let newR: NSRange = moreLabelString.nsRange(from: range)!
  179. let attributedStr = NSMutableAttributedString(string: moreLabelString)
  180. attributedStr.addAttribute(.foregroundColor, value: NSColor.labelColor, range: NSRange(location: 0, length: newR.location - 1))
  181. attributedStr.addAttribute(.foregroundColor, value: NSColor(red: 8/255, green: 124/255, blue: 1, alpha: 1), range: newR)
  182. attributedStr.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: newR)
  183. detailInfoLabel.attributedStringValue = attributedStr
  184. }
  185. if moreLabelString.count > 0 {
  186. self.detailInfoView.isHidden = false
  187. self.containerViewTopConstraint.constant = 16
  188. hasInfo = true
  189. } else {
  190. self.detailInfoView.isHidden = true
  191. self.containerViewTopConstraint.constant = 11
  192. }
  193. if self.convertType == .WordAdvance {
  194. isExcel = true
  195. self.convertEveryPageButton.isHidden = true
  196. self.wordButtonOne.isHidden = false
  197. self.wordButtonOne.title = NSLocalizedString("Retain Flowing Text", comment: "")
  198. self.convertEveryPageButtonLabel.isHidden = true
  199. self.extractButton.isHidden = true
  200. self.csvExtractButton.isHidden = true
  201. self.wordButtonTwo.isHidden = false
  202. self.wordButtonTwo.title = NSLocalizedString("Retain Page Layout", comment: "")
  203. self.checkBoxBottomConstaint.constant = 15 + self.wordButtonTwo.frame.size.height + self.wordButtonOne.frame.size.height
  204. self.checkBoxTopConstraint.constant = 5
  205. } else if self.convertType == .Excel {
  206. isExcel = true
  207. self.convertEveryPageButtonLabel.stringValue = NSLocalizedString("Convert each page to a separate worksheet", comment: "")
  208. self.onlyTextBtn.isHidden = false
  209. self.onlyTableBtn.isHidden = false
  210. self.allContentBtn.isHidden = false
  211. self.tableMenu.isHidden = false
  212. self.allContentMenu.isHidden = false
  213. self.convertEveryPageButtonLabel.isHidden = true
  214. self.convertEveryPageButton.isHidden = true
  215. self.wordButtonOne.isHidden = true
  216. self.extractButton.isHidden = true
  217. self.wordButtonTwo.isHidden = true
  218. self.csvExtractButton.isHidden = true
  219. self.extractButton.title = NSLocalizedString("Extract Tables Only", comment: "")
  220. var oneHeight = self.convertEveryPageButtonLabel.frame.size.height
  221. let language = Bundle.main.preferredLocalizations[0]
  222. if language != "zh_CN" && language != "zh_TW" {
  223. oneHeight = 34.0
  224. }
  225. self.checkBoxBottomConstaint.constant = 83
  226. self.checkBoxTopConstraint.constant = 5
  227. } else if self.convertType == .CSV {
  228. isExcel = true
  229. self.convertEveryPageButtonLabel.isHidden = true
  230. self.convertEveryPageButton.isHidden = true
  231. self.extractButton.isHidden = true
  232. self.wordButtonTwo.isHidden = true
  233. self.wordButtonOne.isHidden = true
  234. self.csvExtractButton.isHidden = false
  235. self.csvExtractButton.title = NSLocalizedString("Extract Tables Only", comment: "")
  236. self.checkBoxBottomConstaint.constant = 15 + self.csvExtractButton.frame.size.height
  237. self.checkBoxTopConstraint.constant = 5
  238. } else {
  239. self.convertEveryPageButton.isHidden = true
  240. self.extractButton.isHidden = true
  241. self.wordButtonTwo.isHidden = true
  242. self.wordButtonOne.isHidden = true
  243. self.csvExtractButton.isHidden = true
  244. self.convertEveryPageButtonLabel.stringValue = NSLocalizedString("", comment: "")
  245. self.checkBoxTopConstraint.constant = -10
  246. self.checkBoxBottomConstaint.constant = 5
  247. }
  248. if !hasInfo && !isExcel {
  249. self.containerViewTopConstraint.constant = 0
  250. self.checkBoxTopConstraint.constant = 0
  251. self.checkBoxBottomConstaint.constant = 1
  252. }
  253. }
  254. override func viewDidLoad() {
  255. super.viewDidLoad()
  256. self.prepareData()
  257. self.view.wantsLayer = true
  258. collectionView.allowsMultipleSelection = false
  259. collectionView.enclosingScrollView?.borderType = .noBorder
  260. collectionView.allowsEmptySelection = false
  261. convertButton.wantsLayer = true
  262. convertButton.font = NSFont.systemFont(ofSize: 13)
  263. convertButton.layer?.cornerRadius = 1.0
  264. interfaceStatus = .PrepareProcess
  265. convertEveryPageButton.title = NSLocalizedString("", comment: "")
  266. // Convert each page to a separate worksheet
  267. convertEveryPageButtonLabel.stringValue = NSLocalizedString("Convert each page to a separate worksheet", comment: "")
  268. self.collectionView.register(KMConvertCollectionViewHeader.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"))
  269. self.collectionView.register(KMBatchoperateConvertCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchoperateConvertCollectionViewItem"))
  270. self.collectionView.register(KMConvertCollectionViewHeader.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "convertHeader"))
  271. collectionView.enclosingScrollView?.drawsBackground = false
  272. let v = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
  273. v.wantsLayer = true
  274. v.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  275. collectionView.backgroundView = v
  276. collectionView.enclosingScrollView?.horizontalScrollElasticity = .none
  277. collectionView.enclosingScrollView?.verticalScrollElasticity = .none
  278. allContentBtn.state = .on
  279. allContentMenu.isEnabled = true
  280. tableMenu.isEnabled = false
  281. excelContentOption = .allContent
  282. excelWorksheetOption = .forEachPage
  283. onlyTextBtn.title = NSLocalizedString("Only Text", comment: "")
  284. onlyTableBtn.title = NSLocalizedString("Only Table", comment: "")
  285. allContentBtn.title = NSLocalizedString("All Content", comment: "")
  286. onlyTextBtn.toolTip = NSLocalizedString("Only Text", comment: "")
  287. onlyTableBtn.toolTip = NSLocalizedString("Only Table", comment: "")
  288. allContentBtn.toolTip = NSLocalizedString("All Content", comment: "")
  289. tableMenu1.title = NSLocalizedString("Create Sheet for each Table", comment: "")
  290. tableMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  291. allContentMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  292. tableMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  293. allContentMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  294. self.detailInfoView.mouseDownCallback = { [weak self] (downEntered: Bool) in
  295. if downEntered {
  296. self?.moreConvertInfoAction()
  297. }
  298. }
  299. self.updateViewColor()
  300. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  301. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name("KMIAPProductPurchasedNotification"), object: nil)
  302. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name("KMIAPProductRestoreFinishedNotification"), object: nil)
  303. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  304. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  305. NotificationCenter.default.addObserver(self, selector: #selector(deviceActivateStatusChanged(notification:)), name: NSNotification.Name("kDeviceActivateNotification"), object: nil)
  306. }
  307. @objc func batchFilesCountNotification(notification: NSNotification) {
  308. let arr: Array? = notification.object as? [KMBatchOperateFile]
  309. self.files? = arr ?? []
  310. if files!.count > 0 {
  311. self.convertButton.setTitleColor(NSColor.white)
  312. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  313. self.haveFiles = true
  314. } else {
  315. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  316. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  317. self.haveFiles = false
  318. }
  319. }
  320. @objc func themeChanged(notification: NSNotification) {
  321. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  322. self.updateViewColor()
  323. }
  324. }
  325. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  326. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  327. self.updateBottomView()
  328. self.prepareData()
  329. self.collectionView.reloadData()
  330. }
  331. }
  332. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  333. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  334. self.updateBottomView()
  335. self.prepareData()
  336. self.collectionView.reloadData()
  337. }
  338. }
  339. @objc func deviceActivateStatusChanged(notification: NSNotification) {
  340. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  341. self.updateBottomView()
  342. self.prepareData()
  343. self.collectionView.reloadData()
  344. }
  345. }
  346. func updateViewColor() {
  347. self.view.wantsLayer = true
  348. if KMAppearance.isDarkMode() {
  349. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  350. self.collectionView.backgroundView?.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  351. } else {
  352. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  353. self.collectionView.backgroundView?.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  354. }
  355. }
  356. func convertActionVC() {
  357. var vc: KMToolCompareWindowController? = nil
  358. if(.WordStandard == self.convertType ||
  359. .WordAdvance == self.convertType) {
  360. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  361. } else if (.Excel == self.convertType) {
  362. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2)
  363. } else if (.PowerPoint == self.convertType) {
  364. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3)
  365. } else if (self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP){
  366. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4)
  367. } else {
  368. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0)
  369. }
  370. vc?.showWindow(nil)
  371. }
  372. func prepareData() {
  373. let arr: [KMConvertWithPDFType] = [.Excel, .PowerPoint, .RTF, .CSV, .HTML, .Text, .JPEG, .JPG, .PNG, .GIF, .TIFF, .TGA, .BMP, .JPEG2000]
  374. self.dataSourcesArray = arr
  375. var needShowAdvance = true
  376. var needShowDefault = true
  377. #if VERSION_FREE
  378. // 桌机版
  379. if IAPProductsManager.default().isAvailableAllFunction() {
  380. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  381. needShowAdvance = true
  382. needShowDefault = false
  383. }
  384. }
  385. #else
  386. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  387. needShowAdvance = true
  388. needShowDefault = false
  389. }
  390. #endif
  391. if needShowDefault {
  392. var wordType: KMConvertWithPDFType = .WordStandard
  393. self.dataSourcesArray?.insert(wordType, at: 0)
  394. }
  395. if needShowAdvance {
  396. var wordType: KMConvertWithPDFType = .WordAdvance
  397. self.dataSourcesArray?.insert(wordType, at: 0)
  398. }
  399. }
  400. @IBAction func buttonClicked_Convert(_ sender: NSButton) {
  401. if !self.haveFiles { return }
  402. self.view.window?.makeFirstResponder(nil)
  403. for i in 0..<self.files!.count {
  404. let file = self.files?[i]
  405. file?.excelParameter.allInOneSheet = (self.convertEveryPageButton.state == .on) ? false : true
  406. file?.advanceWordParameter.isRetainLayout = (self.wordButtonTwo.state == .on) ? true : false
  407. file?.excelParameter.isExtreactTabel = (self.extractButton.state == .on) ? true : false
  408. file?.CSVParameter.isExtreactTabel = (self.csvExtractButton.state == .on) ? true : false
  409. file?.excelParameter.excelContentOption = self.excelContentOption
  410. file?.excelParameter.excelWorksheetOption = self.excelWorksheetOption
  411. }
  412. if sender.tag == 1 {
  413. self.beginBatchOperation()
  414. } else {
  415. self.cancelBatchOperation()
  416. }
  417. }
  418. @IBAction func moreButtonAction(_ sender: NSButton) {
  419. #if VERSION_DMG
  420. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  421. #else
  422. if IAPProductsManager.default().isAvailableAllFunction() {
  423. self.convertActionVC()
  424. } else {
  425. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  426. }
  427. #endif
  428. self.buttonClicked_Cancel("")
  429. }
  430. @IBAction func layoutButtonAction(_ sender: Any) {
  431. }
  432. @IBAction func exctractTableButtonAction(_ sender: Any) {
  433. if extractButton.state == .on {
  434. convertEveryPageButton.state = .off
  435. convertEveryPageButtonLabel.isEnabled = false
  436. convertEveryPageButton.isEnabled = false
  437. convertEveryPageButtonLabel.textColor = KMAppearance.Layout.b15_1Color()
  438. } else {
  439. convertEveryPageButton.state = .on
  440. convertEveryPageButtonLabel.isEnabled = true
  441. convertEveryPageButton.isEnabled = true
  442. convertEveryPageButtonLabel.textColor = NSColor.labelColor
  443. }
  444. }
  445. @IBAction func buttonClicked_Cancel(_ sender: Any) {
  446. let basePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? ""
  447. let newPath = (basePath as NSString).appendingPathComponent(Bundle.main.bundleIdentifier ?? "")
  448. let filePath = newPath.stringByAppendingPathComponent("convert.pdf")
  449. if FileManager.default.fileExists(atPath: filePath) {
  450. try? FileManager.default.removeItem(atPath: filePath)
  451. }
  452. }
  453. func transform(withString string: String) {
  454. if string == NSLocalizedString("Create Sheet for each Table", comment: "") {
  455. self.excelWorksheetOption = .forEachTable
  456. } else if string == NSLocalizedString("Create Sheet for each Page", comment: "") {
  457. self.excelWorksheetOption = .forEachPage
  458. } else if string == NSLocalizedString("Create single Sheet for File", comment: "") {
  459. self.excelWorksheetOption = .forTheDocument
  460. }
  461. }
  462. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  463. if sender == onlyTextBtn {
  464. onlyTextBtn.state = NSControl.StateValue.on
  465. onlyTableBtn.state = NSControl.StateValue.off
  466. allContentBtn.state = NSControl.StateValue.off
  467. tableMenu.isEnabled = false
  468. allContentMenu.isEnabled = false
  469. excelContentOption = .onlyText
  470. excelWorksheetOption = .forEachTable
  471. } else if sender == onlyTableBtn {
  472. onlyTableBtn.state = NSControl.StateValue.on
  473. onlyTextBtn.state = NSControl.StateValue.off
  474. allContentBtn.state = NSControl.StateValue.off
  475. tableMenu.isEnabled = true
  476. allContentMenu.isEnabled = false
  477. excelContentOption = .onlyTable
  478. transform(withString: tableMenu.selectedItem?.title ?? "")
  479. } else if sender == allContentBtn {
  480. allContentBtn.state = NSControl.StateValue.on
  481. onlyTextBtn.state = NSControl.StateValue.off
  482. onlyTableBtn.state = NSControl.StateValue.off
  483. tableMenu.isEnabled = false
  484. allContentMenu.isEnabled = true
  485. excelContentOption = .allContent
  486. transform(withString: allContentMenu.selectedItem?.title ?? "")
  487. }
  488. }
  489. @IBAction func buttonClicked_tableMenu(_ sender: NSPopUpButton) {
  490. transform(withString: self.tableMenu.selectedItem!.title)
  491. }
  492. @IBAction func buttonClickec_allContentMenu(_ sender: NSButton) {
  493. transform(withString: self.allContentMenu.selectedItem!.title)
  494. }
  495. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  496. return dataSourcesArray!.count
  497. }
  498. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  499. if let indexPath = indexPaths.first {
  500. convertType = dataSourcesArray![indexPath.item]
  501. updateBottomView()
  502. }
  503. }
  504. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  505. return NSSize(width: 10000, height: 50)
  506. }
  507. func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  508. let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier:NSUserInterfaceItemIdentifier(rawValue: "convertHeader") , for: indexPath) as? KMConvertCollectionViewHeader
  509. view?.refreshData()
  510. view?.convertHeaderClickedCallBack = { [weak self] in
  511. self?.moreConvertInfoAction()
  512. }
  513. return view ?? NSView()
  514. }
  515. func intConvertType(num: Int) -> KMConvertWithPDFType {
  516. var type: KMConvertWithPDFType?
  517. switch num {
  518. case 0:
  519. type = .WordAdvance
  520. case 1:
  521. type = .WordStandard
  522. case 2:
  523. type = .Excel
  524. case 3:
  525. type = .PowerPoint
  526. case 4:
  527. type = .RTF
  528. case 5:
  529. type = .CSV
  530. case 6:
  531. type = .HTML
  532. case 7:
  533. type = .Text
  534. case 8:
  535. type = .JPEG
  536. case 9:
  537. type = .JPG
  538. case 10:
  539. type = .PNG
  540. case 11:
  541. type = .GIF
  542. case 12:
  543. type = .TIFF
  544. case 13:
  545. type = .TGA
  546. case 14:
  547. type = .BMP
  548. case 15:
  549. type = .JPEG2000
  550. default:
  551. type = .WordAdvance
  552. }
  553. return type!
  554. }
  555. func moreConvertInfoAction() {
  556. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  557. #if VERSION_DMG
  558. if IAPProductsManager.default().isAvailableAllFunction() && IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  559. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  560. limitWC.continueBlock = { windowController in
  561. }
  562. limitWC.window?.center()
  563. limitWC.showWindow(nil)
  564. }else{
  565. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  566. }
  567. #else
  568. if IAPProductsManager.default().isAvailableAllFunction() {
  569. self.convertActionVC()
  570. } else {
  571. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  572. }
  573. #endif
  574. }
  575. }
  576. }
  577. extension KMBatchOperateConvertViewController: NSCollectionViewDelegateFlowLayout {
  578. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  579. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchoperateConvertCollectionViewItem"), for: indexPath) as! KMBatchoperateConvertCollectionViewItem
  580. item.updateInterface(dataSourcesArray![indexPath.item])
  581. return item
  582. }
  583. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  584. return NSSize(width: 74, height: 80)
  585. }
  586. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  587. return NSEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
  588. }
  589. }