KMConvertBaseWindowController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. //
  2. // KMConvertBaseWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/5.
  6. //
  7. import PDFKit
  8. import KMComponentLibrary
  9. class KMConvertBaseWindowController: KMNBaseWindowController {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var leftBox: NSBox!
  12. @IBOutlet weak var rightScrollView: NSScrollView!
  13. @IBOutlet weak var batchButton: NSButton!
  14. @IBOutlet weak var canelBox: NSBox!
  15. @IBOutlet weak var convertBox: NSBox!
  16. @IBOutlet weak var rightBoxHeightConst: NSLayoutConstraint!
  17. private var cancelButton_: ComponentButton?
  18. private var convertButton_: ComponentButton?
  19. private var batchButton_: ComponentButton?
  20. var documentModel: KMDocumentModel?
  21. var currentPageIndex: Int = 1
  22. var pageRange = KMPageRange.all
  23. var pageRangeString: String = ""
  24. var settingView: KMConvertSettingView?
  25. var subType: Int = 0
  26. var fileExtension: String {
  27. get {
  28. return ""
  29. }
  30. }
  31. var progressController: SKProgressController?
  32. var pdfPreView: KMNPreView?
  33. private var convert: KMPDFConvert?
  34. private var ocrLanguage_: COCRLanguage = .english
  35. var oriDocumentUrl: URL?
  36. deinit {
  37. KMPrint("KMConvertBaseWindowController 已释放")
  38. }
  39. convenience init() {
  40. self.init(windowNibName: "KMConvertBaseWindowController")
  41. }
  42. override func awakeFromNib() {
  43. super.awakeFromNib()
  44. DispatchQueue.main.async {
  45. self.window?.makeFirstResponder(nil)
  46. }
  47. }
  48. override func windowDidLoad() {
  49. super.windowDidLoad()
  50. let preView = KMNPreView.createFromNib()
  51. leftBox.contentView = preView
  52. pdfPreView = preView
  53. pdfPreView?.contentInset = .init(top: 6, left: 6, bottom: 16, right: 6)
  54. if (documentModel != nil) {
  55. pdfPreView?.setFileUrl(self.documentModel!.documentURL, password: self.documentModel?.password)
  56. pdfPreView?.pdfView?.go(toPageIndex: self.documentModel?.currentIndex ?? 0, animated: false)
  57. }
  58. if let pageCount = pdfPreView?.pageCount {
  59. settingView?.pageCount = Int(pageCount)
  60. }
  61. pdfPreView?.reloadUI()
  62. updateViewColor()
  63. }
  64. override func updateUILanguage() {
  65. super.updateUILanguage()
  66. KMMainThreadExecute {
  67. self.batchButton_?.properties.buttonText = KMLocalizedString("Batch")
  68. self.batchButton_?.reloadData()
  69. }
  70. }
  71. override func initContentView() {
  72. super.initContentView()
  73. canelBox.fillColor = .clear
  74. cancelButton_?.keyEquivalent = KMKeyEquivalent.esc.string()
  75. convertBox.fillColor = .clear
  76. convertButton_ = ComponentButton()
  77. convertBox.contentView = convertButton_
  78. convertButton_?.properties = ComponentButtonProperty(type: .primary,
  79. size: .s,
  80. state: .normal,
  81. isDisable: false,
  82. buttonText: KMLocalizedString("Convert"))
  83. convertButton_?.setTarget(self, action: #selector(convertButtonAction))
  84. convertButton_?.keyEquivalent = KMKeyEquivalent.enter
  85. rightScrollView.hasVerticalScroller = false
  86. rightScrollView.hasHorizontalScroller = false
  87. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  88. leftBox.borderWidth = 1
  89. leftBox.cornerRadius = ComponentLibrary.shared.getComponentValueFromKey("radius/m") as? CGFloat ?? 0
  90. let view = initSettingView()
  91. settingView = view
  92. rightScrollView.documentView = view
  93. rightScrollView.verticalScrollElasticity = .none
  94. rightScrollView.horizontalScrollElasticity = .none
  95. view?.km_add_inset_constraint()
  96. view?.currentLanguage = self.getCurrentLanguage()
  97. view?.lanugageDidSelected = { [weak self] value, _ in
  98. guard let index = value as? Int else {
  99. return
  100. }
  101. self?.saveLanugageSelectedIndex(index: index)
  102. }
  103. view?.pageRangeDidChange = { [weak self] pageRange in
  104. self?.pageRange = pageRange
  105. if (pageRange != .custom) {
  106. self?._updatePreView(pageRange: pageRange)
  107. }
  108. }
  109. view?.pageRangeDidInputFinishCallback = { [unowned self] string in
  110. self.pageRangeString = string
  111. let array = self.findSelectPage(pageRangeString: string)
  112. if array.count == 0 {
  113. let alert = NSAlert()
  114. alert.alertStyle = .warning
  115. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  116. alert.runModal()
  117. self.settingView?.pageRangeItemView?.pageSizeVC?.stringValue = ""
  118. } else {
  119. self._updatePreView(pageRange: .custom, pageString: string)
  120. }
  121. }
  122. settingView?.tipView?.isHidden = true
  123. cancelButton_ = ComponentButton()
  124. cancelButton_?.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, buttonText: KMLocalizedString("Cancel"))
  125. cancelButton_?.setTarget(self, action: #selector(cancelButtonAction))
  126. canelBox.contentView = cancelButton_
  127. batchButton.title = ""
  128. batchButton_ = ComponentButton()
  129. batchButton_?.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, state: .normal, buttonText: KMLocalizedString("Batch"))
  130. batchButton_?.setTarget(self, action: #selector(batchButtonAction))
  131. batchButton.addSubview(self.batchButton_!)
  132. batchButton_?.frame = batchButton.bounds
  133. batchButton_?.autoresizingMask = [.width, .height]
  134. }
  135. func initSettingView() -> KMConvertSettingView? {
  136. let settingView = KMConvertSettingView.createFromNib()
  137. return settingView
  138. }
  139. public func initConvert() -> KMPDFConvert {
  140. return KMPDFConvert()
  141. }
  142. func setupContentHeight(height: CGFloat, animated: Bool = false) { // 370
  143. if (animated) {
  144. rightBoxHeightConst.animator().constant = height
  145. } else {
  146. rightBoxHeightConst.constant = height
  147. }
  148. }
  149. func updateViewColor() {
  150. KMMainThreadExecute {
  151. self.titleLabel.textColor = KMNColorTools.colorText_1()
  152. self.leftBox.fillColor = KMNColorTools.colorFill_4()
  153. self.leftBox.borderColor = KMNColorTools.colorBorder_4()
  154. self.pdfPreView?.pdfView?.backgroundColor = KMNColorTools.colorFill_4()
  155. self.settingView?.updateViewColor()
  156. }
  157. }
  158. @objc func batchButtonAction() {
  159. self._clearData()
  160. own_closeEndSheet()
  161. }
  162. @objc func cancelButtonAction() {
  163. self._clearData()
  164. own_closeEndSheet()
  165. }
  166. @objc func convertButtonAction() {
  167. window?.makeFirstResponder(nil)
  168. if (pageRange == .custom) {
  169. let array = findSelectPage(pageRangeString: pageRangeString)
  170. if (array.count <= 0) {
  171. window?.makeFirstResponder(settingView?.pageRangeItemView?.pageSizeVC?.textField)
  172. return
  173. }
  174. }
  175. var pages: [Int] = []
  176. let filePath = getConvertFileSavePath()
  177. if (FileManager.default.fileExists(atPath: filePath)) {
  178. try?FileManager.default.removeItem(atPath: filePath)
  179. }
  180. let document = pdfPreView?.pdfPreView.pdfView.document
  181. if ((document?.writeDecrypt(to: URL(fileURLWithPath: filePath)))!) {
  182. for i in 0 ..< document!.pageCount {
  183. pages.append(Int(i)+1)
  184. }
  185. }
  186. if (pages.count <= 0) {
  187. let alert = NSAlert()
  188. alert.alertStyle = .warning
  189. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  190. alert.runModal()
  191. return
  192. }
  193. _convertButtonAction()
  194. }
  195. func convertModelAppendParams(convert: KMPDFConvert) -> () {
  196. if let isAllowOCR = settingView?.ocrEnabled {
  197. convert.isAllowOCR = isAllowOCR
  198. if let idx = settingView?.ocrItemView?.languageIndex {
  199. convert.ocrLanguage = _getOCRLanguage(idx: idx)
  200. }
  201. } else {
  202. convert.isAllowOCR = false
  203. }
  204. }
  205. func getConvertFileSavePath() -> String {
  206. var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
  207. path?.append("/")
  208. path?.append(Bundle.main.bundleIdentifier!)
  209. if (FileManager.default.fileExists(atPath: path!) == false) {
  210. try?FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: false)
  211. }
  212. path?.append("/")
  213. path?.append("convert.pdf")
  214. return path!
  215. }
  216. func isValidPagesString(pagesString: String)-> Bool {
  217. var valid = false
  218. for ch in pagesString {
  219. if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != "," && ch != "-" {
  220. valid = false
  221. break
  222. } else {
  223. valid = true
  224. }
  225. }
  226. return valid
  227. }
  228. func findSelectPage(pageRangeString: String) -> ([Int]) {
  229. if !isValidPagesString(pagesString: pageRangeString) {
  230. return []
  231. }
  232. var result: [Int] = []
  233. let array = pageRangeString.components(separatedBy: ",")
  234. for string in array {
  235. if string.isEmpty {
  236. return []
  237. } else {
  238. let pages = string .components(separatedBy: "-")
  239. if pages.count > 2 {
  240. return []
  241. } else if pages.count == 1 {
  242. let page = pages[0]
  243. if page.isEmpty || Int(page)! > documentModel!.pageCount || Int(page)! == 0 {
  244. return []
  245. } else {
  246. var hasSame: Bool = false
  247. for i in result {
  248. if i == Int(page)! {
  249. hasSame = true
  250. return []
  251. }
  252. }
  253. if !hasSame {
  254. result.append(Int(page)!)
  255. }
  256. }
  257. } else if pages.count == 2 {
  258. let page1 = pages[0]
  259. let page2 = pages[1]
  260. if page1.isEmpty || page2.isEmpty || Int(page1)! >= Int(page2)! || Int(page2)! > documentModel!.pageCount || Int(page1)! == 0 {
  261. return []
  262. } else {
  263. var hasSame: Bool = false
  264. for i in Int(page1)! ... Int(page2)! {
  265. for j in result {
  266. if j == i {
  267. hasSame = true
  268. return []
  269. }
  270. }
  271. }
  272. if !hasSame {
  273. for i in Int(page1)! ... Int(page2)! {
  274. result.append(i)
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. return result
  282. }
  283. override func mouseDown(with event: NSEvent) {
  284. super.mouseDown(with: event)
  285. window?.makeFirstResponder(nil)
  286. }
  287. // MARK: - Private Methods
  288. private func _updatePreView(pageRange: KMPageRange, pageString: String = "") {
  289. let pages = getPages(pageRange, pageString: pageString)
  290. if (pages == nil || pages!.isEmpty) {
  291. let alert = NSAlert()
  292. alert.alertStyle = .warning
  293. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  294. alert.runModal()
  295. return
  296. }
  297. var indexs = IndexSet()
  298. let document = pdfPreView?.pdfPreView.pdfView.document
  299. for i in 0 ..< document!.pageCount {
  300. indexs.insert(IndexSet.Element(i))
  301. }
  302. document?.removePage(at: indexs)
  303. for page in pages! {
  304. document?.insertPageObject(page, at: document?.pageCount ?? 0)
  305. }
  306. DispatchQueue.main.async { [self] in
  307. let prePDFView = pdfPreView?.pdfPreView.pdfView
  308. prePDFView?.layoutDocumentView()
  309. prePDFView?.setNeedsDisplayForVisiblePages()
  310. prePDFView?.goToFirstPage(nil)
  311. currentPageIndex = 1
  312. pdfPreView?.reloadUI()
  313. }
  314. }
  315. private func getPageIndexs(_ pageRange: KMPageRange, pageString: String = "") -> IndexSet? {
  316. guard let pageCount = documentModel?.pageCount, pageCount > 0 else {
  317. return nil
  318. }
  319. var indexs = IndexSet()
  320. if (pageRange == .all) {
  321. for i in 0 ..< pageCount {
  322. indexs.insert(i)
  323. }
  324. } else if (pageRange == .odd) {
  325. for i in 0 ..< pageCount {
  326. if ((i % 2) == 1) {
  327. continue
  328. }
  329. indexs.insert(i)
  330. }
  331. } else if (pageRange == .even) {
  332. for i in 0 ..< pageCount {
  333. if ((i % 2) == 0) {
  334. continue
  335. }
  336. indexs.insert(i)
  337. }
  338. } else if (pageRange == .current) {
  339. if (currentPageIndex >= 1 && currentPageIndex <= pageCount) {
  340. indexs.insert(currentPageIndex-1)
  341. }
  342. } else if (pageRange == .custom) {
  343. let array = findSelectPage(pageRangeString: pageString)
  344. if array.count == 0 {
  345. } else {
  346. for i in 0 ..< array.count {
  347. let index = array[i]
  348. if (index > pageCount || index < 1) {
  349. continue
  350. }
  351. indexs.insert(index-1)
  352. }
  353. }
  354. }
  355. return indexs
  356. }
  357. private func getPages(_ pageRange: KMPageRange, pageString: String = "") -> [CPDFPage]? {
  358. guard let pageCount = documentModel?.pageCount, pageCount > 0 else {
  359. return nil
  360. }
  361. var pages: [CPDFPage] = []
  362. if (pageRange == .all) {
  363. for i in 0 ..< pageCount {
  364. let page = documentModel!.document?.page(at: UInt(i))
  365. if (page == nil) {
  366. continue
  367. }
  368. pages.append(page!)
  369. }
  370. } else if (pageRange == .odd) {
  371. for i in 0 ..< pageCount {
  372. if ((i % 2) == 1) {
  373. continue
  374. }
  375. let page = documentModel!.document?.page(at: UInt(i))
  376. if (page == nil) {
  377. continue
  378. }
  379. pages.append(page!)
  380. }
  381. } else if (pageRange == .even) {
  382. for i in 0 ..< pageCount {
  383. if ((i % 2) == 0) {
  384. continue
  385. }
  386. let page = documentModel!.document?.page(at: UInt(i))
  387. if (page == nil) {
  388. continue
  389. }
  390. pages.append(page!)
  391. }
  392. } else if (pageRange == .current) {
  393. let page = pdfPreView?.pdfPreView.pdfView.currentPage()
  394. if (page != nil) {
  395. pages.append(page!)
  396. }
  397. } else if (pageRange == .custom) {
  398. let array = findSelectPage(pageRangeString: pageString)
  399. if array.count == 0 {
  400. } else {
  401. for i in 0 ..< array.count {
  402. let index = array[i]
  403. if (index > pageCount || index < 1) {
  404. continue
  405. }
  406. let page = documentModel?.document?.page(at: UInt(index-1))
  407. if (page == nil) {
  408. continue
  409. }
  410. pages.append(page!)
  411. }
  412. }
  413. }
  414. return pages
  415. }
  416. private func _convertButtonAction() {
  417. var pages: [Int] = []
  418. let filePath = getConvertFileSavePath()
  419. if (FileManager.default.fileExists(atPath: filePath)) {
  420. try?FileManager.default.removeItem(atPath: filePath)
  421. }
  422. let document = pdfPreView?.pdfPreView.pdfView.document
  423. if ((document?.writeDecrypt(to: URL(fileURLWithPath: filePath)))!) {
  424. for i in 0 ..< document!.pageCount {
  425. pages.append(Int(i)+1)
  426. }
  427. }
  428. if (pages.count <= 0) {
  429. let alert = NSAlert()
  430. alert.alertStyle = .warning
  431. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  432. alert.runModal()
  433. return
  434. }
  435. DispatchQueue.main.async {
  436. NSPanel.savePanel(self.window!) { panel in
  437. var url: URL = (self.pdfPreView?.pdfPreView.pdfView.document.documentURL)!
  438. if (self.oriDocumentUrl != nil) {
  439. url = self.oriDocumentUrl!
  440. }
  441. let name = url.deletingPathExtension().lastPathComponent
  442. panel.nameFieldStringValue = name
  443. panel.allowedFileTypes = [self.fileExtension]
  444. } completion: { response, url in
  445. if (response == .cancel) {
  446. return
  447. }
  448. let outputFolderPath = url!.deletingLastPathComponent().path
  449. let convert = self.initConvert()
  450. self.convert = convert
  451. convert.password = self.documentModel!.password
  452. convert.outputFolderPath = outputFolderPath
  453. convert.filePath = filePath
  454. convert.pages = pages
  455. var fileName = url!.deletingPathExtension().lastPathComponent
  456. if (self.fileExtension.isEmpty) {
  457. fileName = url!.lastPathComponent
  458. }
  459. convert.outputFileName = fileName
  460. self.convertModelAppendParams(convert: convert)
  461. DispatchQueue.main.async {
  462. self.showProgressWindow()
  463. self.progressController?.maxValue = Double(convert.pages.count)
  464. }
  465. DispatchQueue.global().async {
  466. KMPDFConvertManager.defaultManager.convert(convert: convert, progress: { index in
  467. DispatchQueue.main.async {
  468. self.progressController?.increment(by: 1.0)
  469. }
  470. }) { [unowned self] finished, error in
  471. self.hiddenProgressWindow()
  472. // 清除临时文件
  473. if (FileManager.default.fileExists(atPath: filePath)) {
  474. try?FileManager.default.removeItem(atPath: filePath)
  475. }
  476. if finished {
  477. self._clearData()
  478. cancelButtonAction()
  479. if FileManager.default.fileExists(atPath: convert.outputFilePath) {
  480. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: convert.outputFilePath)])
  481. }
  482. } else {
  483. var errorString = ""
  484. let myError: NSError? = error as? NSError
  485. if myError?.code == 1 {
  486. errorString = KMLocalizedString("Password required or incorrect password. Please re-enter your password and try again", comment: "")
  487. } else if myError?.code == 2 {
  488. errorString = KMLocalizedString("The license doesn't allow the permission", comment: "")
  489. } else if myError?.code == 3 {
  490. errorString = KMLocalizedString("Malloc failure", comment: "")
  491. } else if myError?.code == 4 {
  492. errorString = KMLocalizedString("Unknown error in processing conversion. Please try again later", comment: "")
  493. } else if myError?.code == 5 {
  494. errorString = KMLocalizedString("Unknown error in processing PDF. Please try again later", comment: "")
  495. } else if myError?.code == 6 {
  496. errorString = KMLocalizedString("File not found or could not be opened. Check if your file exists or choose another file to convert", comment: "")
  497. } else if myError?.code == 7 {
  498. errorString = KMLocalizedString("File not in PDF format or corruptead. Change a PDF file and try again", comment: "")
  499. } else if myError?.code == 8 {
  500. errorString = KMLocalizedString("Unsupported security scheme", comment: "")
  501. } else if myError?.code == 9 {
  502. errorString = KMLocalizedString("Page not found or content error", comment: "")
  503. } else {
  504. errorString = KMLocalizedString("Table not found", comment: "")
  505. }
  506. let alert = NSAlert()
  507. alert.alertStyle = .critical
  508. alert.messageText = KMLocalizedString("Conversion Failed", comment: "")
  509. alert.informativeText = errorString
  510. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  511. alert.runModal()
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. private func _getOCRLanguage(idx: Int) -> COCRLanguage {
  519. switch (idx) {
  520. case 0: //chinese
  521. self.ocrLanguage_ = .chinese
  522. break
  523. case 1: //chinese zht
  524. self.ocrLanguage_ = .chineseTraditional
  525. break
  526. case 2: //English
  527. self.ocrLanguage_ = .english
  528. break
  529. case 3:
  530. //Japanese
  531. self.ocrLanguage_ = .japanese
  532. break
  533. case 4:
  534. //Kroean
  535. self.ocrLanguage_ = .korean
  536. break
  537. case 5:
  538. //Serbian
  539. self.ocrLanguage_ = .serbian
  540. break
  541. case 6:
  542. //Occitan
  543. self.ocrLanguage_ = .occitan
  544. break
  545. case 7:
  546. //Danish
  547. self.ocrLanguage_ = .danish
  548. break
  549. case 8:
  550. //German
  551. self.ocrLanguage_ = .german
  552. break
  553. case 9:
  554. //French
  555. self.ocrLanguage_ = .french
  556. break
  557. case 10:
  558. //Italian
  559. self.ocrLanguage_ = .italian
  560. break
  561. case 11:
  562. //Spanish
  563. self.ocrLanguage_ = .spanish
  564. break
  565. case 12:
  566. //Portuguese
  567. self.ocrLanguage_ = .portuguese
  568. break
  569. case 13:
  570. //Maori
  571. self.ocrLanguage_ = .maori
  572. break
  573. case 14:
  574. //Malay
  575. self.ocrLanguage_ = .malay
  576. break
  577. case 15:
  578. //Malay
  579. self.ocrLanguage_ = .maltese
  580. break
  581. case 16:
  582. //Dutch
  583. self.ocrLanguage_ = .dutch
  584. break
  585. case 17:
  586. //Norwegian
  587. self.ocrLanguage_ = .norwegian
  588. break
  589. case 18:
  590. //Polish
  591. self.ocrLanguage_ = .polish
  592. break
  593. case 19:
  594. //Romanian
  595. self.ocrLanguage_ = .romanian
  596. break
  597. case 20:
  598. //Slovak
  599. self.ocrLanguage_ = .slovak
  600. break
  601. case 21:
  602. //Slovenian
  603. self.ocrLanguage_ = .slovenian
  604. break
  605. case 22:
  606. //Albanian
  607. self.ocrLanguage_ = .albanian
  608. break
  609. case 23:
  610. //Swedish
  611. self.ocrLanguage_ = .swedish
  612. break
  613. case 24:
  614. //Swahili
  615. self.ocrLanguage_ = .swahili
  616. break
  617. case 25:
  618. //Tagalog
  619. self.ocrLanguage_ = .tagalog
  620. break
  621. case 26:
  622. //Turish
  623. self.ocrLanguage_ = .turish
  624. break
  625. case 27:
  626. //Uzbek
  627. self.ocrLanguage_ = .uzbek
  628. break
  629. case 28:
  630. //Vietnamese
  631. self.ocrLanguage_ = .vietnamese
  632. break
  633. case 29:
  634. //Afrikaans
  635. self.ocrLanguage_ = .afrikaans
  636. break
  637. case 30:
  638. //Azerbaijani
  639. self.ocrLanguage_ = .azerbaijani
  640. break
  641. case 31:
  642. //Bosnian
  643. self.ocrLanguage_ = .bosnian
  644. break
  645. case 32:
  646. //Czech
  647. self.ocrLanguage_ = .czech
  648. break
  649. case 33: //Welsh
  650. self.ocrLanguage_ = .welsh
  651. break
  652. case 34: //Estonian
  653. self.ocrLanguage_ = .estonian
  654. break
  655. case 35: //Irish
  656. self.ocrLanguage_ = .irish
  657. break
  658. case 36: //Croatian
  659. self.ocrLanguage_ = .croatian
  660. break
  661. case 37: //Hungarian
  662. self.ocrLanguage_ = .hungarian
  663. break
  664. case 38: //Indonesian
  665. self.ocrLanguage_ = .indonesian
  666. break
  667. case 39: //Icelandic
  668. self.ocrLanguage_ = .icelandic
  669. break
  670. case 40: //Kurdish
  671. self.ocrLanguage_ = .kurdish
  672. break
  673. case 41: //Lithuanian
  674. self.ocrLanguage_ = .lithuanian
  675. break
  676. case 42: //Latvian
  677. self.ocrLanguage_ = .latvian
  678. break
  679. case 43: //Marathi
  680. self.ocrLanguage_ = .marathi
  681. break
  682. case 44: //Nepali
  683. self.ocrLanguage_ = .nepali
  684. break
  685. case 45: //Latvia
  686. self.ocrLanguage_ = .latvian
  687. break
  688. case 46: //Bihari
  689. self.ocrLanguage_ = .bihari
  690. break
  691. case 47: //Maithili
  692. self.ocrLanguage_ = .maithili
  693. break
  694. case 48: //Angika
  695. self.ocrLanguage_ = .angika
  696. break
  697. case 49: //Bhojpuri
  698. self.ocrLanguage_ = .bhojpuri
  699. break
  700. case 50: //Magahi
  701. self.ocrLanguage_ = .magahi
  702. break
  703. case 51: //Nagpur
  704. self.ocrLanguage_ = .nagpur
  705. break
  706. case 52: //Newari
  707. self.ocrLanguage_ = .newari
  708. break
  709. case 53: //GoanKonkani
  710. self.ocrLanguage_ = .goanKonkani
  711. break
  712. case 54: //SaudiArabia
  713. self.ocrLanguage_ = .saudiArabia
  714. break
  715. default:
  716. self.ocrLanguage_ = .english
  717. break
  718. }
  719. return self.ocrLanguage_
  720. }
  721. // MARK: - Notifation Methods
  722. // MARK: Progress
  723. func showProgressWindow() {
  724. self.hiddenProgressWindow()
  725. let progress = SKProgressController()
  726. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  727. progress.window?.contentView?.wantsLayer = true
  728. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  729. progress.progressField.textColor = NSColor.white
  730. progress.message = KMLocalizedString("Converting...", comment: "")
  731. progress.closeBlock = { [weak self] in
  732. if let convert = self?.convert {
  733. KMPDFConvertManager.defaultManager.cancel(convert: convert)
  734. }
  735. }
  736. self.progressController = progress
  737. self.window?.beginSheet(progress.window!)
  738. }
  739. func hiddenProgressWindow() {
  740. if (self.progressController != nil) {
  741. self.window?.endSheet((self.progressController?.window)!)
  742. self.progressController = nil
  743. }
  744. }
  745. func getCurrentLanguage() -> String {
  746. let array: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  747. return array.first!
  748. }
  749. /// 存储用户的选择的语言
  750. func saveLanugageSelectedIndex(index: Int) {
  751. UserDefaults.standard.setValue("\(index)", forKey: kKMConvertLanugageSelectedIndex)
  752. UserDefaults.standard.synchronize()
  753. }
  754. private func _clearData() {
  755. if let _ = self.oriDocumentUrl {
  756. if let data = self.pdfPreView?.pdfView?.document.documentURL.path, FileManager.default.fileExists(atPath: data) {
  757. try?FileManager.default.removeItem(atPath: data)
  758. }
  759. }
  760. }
  761. }