KMBatchOperateImageToPDFViewController.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. //
  2. // KMBatchOperateImageToPDFViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/5.
  6. //
  7. import Cocoa
  8. class KMBatchOperateImageToPDFViewController: KMBatchOperateBaseViewController, KMImageToPDFMethodDelegate, NSPopoverDelegate{
  9. @IBOutlet var outputTypeLabel: NSTextField!
  10. @IBOutlet var createNewPDFBtn: NSButton!
  11. @IBOutlet var btnMerge: NSButton!
  12. @IBOutlet var appendPDFBtn: NSButton!
  13. @IBOutlet var appendTextField: NSTextField!
  14. @IBOutlet var appendOtherPDFBtn: NSButton!
  15. @IBOutlet var appendBackView: NSView!
  16. @IBOutlet var ocrLabel: NSTextField!
  17. @IBOutlet var ocrSelectBtn: NSButton!
  18. @IBOutlet var languaeBox: NSBox!
  19. @IBOutlet var languageButton: NSButton!
  20. @IBOutlet var saveAsButton: NSButton!
  21. @IBOutlet var planButton: NSButton!
  22. @IBOutlet var selectLanguageLabel: NSTextField!
  23. @IBOutlet var planBox: NSBox!
  24. @IBOutlet var actionButton: NSButton!
  25. var password: String = ""
  26. lazy var method: KMImageToPDFMethod = {
  27. let method = KMImageToPDFMethod()
  28. method.imageTopdfDelegate = self
  29. return method
  30. }()
  31. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  32. set{
  33. super.interfaceStatus = newValue
  34. if newValue == .PrepareProcess {
  35. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  36. var files = NSMutableArray()
  37. for url in self.successFilePathURLArray! {
  38. if FileManager.default.fileExists(atPath: url.path) {
  39. files.add(url)
  40. }
  41. }
  42. if files.count > 0 {
  43. let workspace = NSWorkspace.shared
  44. workspace.activateFileViewerSelecting(files as! [URL])
  45. }
  46. }
  47. self.actionButton.tag = 1
  48. self.actionButton.title = NSLocalizedString("Save as PDF", comment: "")
  49. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  50. self.actionButton.isEnabled = true
  51. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  52. } else {
  53. self.actionButton.tag = 0
  54. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  55. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  56. self.actionButton.isEnabled = false
  57. }
  58. }
  59. get{
  60. return super.interfaceStatus
  61. }
  62. }
  63. deinit {
  64. NotificationCenter.default.removeObserver(self)
  65. }
  66. override func viewDidLoad() {
  67. super.viewDidLoad()
  68. self.localizedLanguage()
  69. self.configuUI()
  70. NotificationCenter.default.addObserver(self, selector: #selector(OCRSelectedLanguagesChangeNotification(notification:)), name: NSNotification.Name("KMOCRSelectedLanguagesChangeNotification"), object: nil)
  71. NotificationCenter.default.addObserver(self, selector: #selector(OCRSelectedPlanChangeNotification(notification:)), name: NSNotification.Name("KMOCRSelectedPlanChangeNotification"), object: nil)
  72. NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  73. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  74. }
  75. func localizedLanguage() {
  76. self.outputTypeLabel.stringValue = KMLocalizedString("Output",nil)
  77. self.btnMerge.title = KMLocalizedString("Merge All", nil)
  78. self.createNewPDFBtn.title = KMLocalizedString("New PDF Document", nil)
  79. self.appendPDFBtn.title = KMLocalizedString("Append To Existing File", nil)
  80. self.appendTextField.placeholderString = KMLocalizedString("Select a File", nil)
  81. self.selectLanguageLabel.stringValue = KMLocalizedString("Select OCR Language:",nil)
  82. self.ocrSelectBtn.title = KMLocalizedString("OCR Plan",nil)
  83. self.updateLanguageButton((KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String]))
  84. self.actionButton.title = KMLocalizedString("Save as PDF", nil)
  85. self.saveAsButton.title = KMLocalizedString("Save as TXT", nil)
  86. self.OCRSelectedPlanChangeAction()
  87. }
  88. func configuUI() {
  89. self.view.wantsLayer = true
  90. appendOtherPDFBtn.wantsLayer = true
  91. appendBackView.wantsLayer = true
  92. appendBackView.layer?.borderWidth = 0.5
  93. self.actionButton.wantsLayer = true
  94. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  95. self.appendOtherPDFBtn.layer?.backgroundColor = KMAppearance.Interactive.s0Color().withAlphaComponent(0.4).cgColor
  96. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  97. self.actionButton.layer?.cornerRadius = 1.0
  98. self.createNewPDFBtn.setTitleColor(KMAppearance.Layout.h0Color())
  99. self.btnMerge.setTitleColor(KMAppearance.Layout.h0Color())
  100. self.appendPDFBtn.setTitleColor(KMAppearance.Layout.h0Color())
  101. self.ocrSelectBtn.setTitleColor(KMAppearance.Layout.h0Color())
  102. self.saveAsButton.setTitleColor(KMAppearance.Layout.h0Color())
  103. self.selectLanguageLabel.textColor = KMAppearance.Layout.h0Color()
  104. self.languageButton.isEnabled = false
  105. self.planButton.isEnabled = false
  106. self.saveAsButton.isEnabled = false
  107. appendTextField.backgroundColor = KMAppearance.Layout.l1Color()
  108. planButton.wantsLayer = true
  109. appendTextField.wantsLayer = true
  110. planButton.wantsLayer = true
  111. appendTextField.layer?.cornerRadius = 1.0
  112. languageButton.layer?.backgroundColor = NSColor.clear.cgColor
  113. languaeBox.borderColor = KMAppearance.Interactive.s0Color()
  114. planBox.borderColor = KMAppearance.Interactive.s0Color()
  115. languaeBox.fillColor = KMAppearance.Layout.l1Color()
  116. planBox.fillColor = KMAppearance.Layout.l1Color()
  117. self.updateViewColor()
  118. }
  119. func updateViewColor() {
  120. if KMAppearance.isDarkMode() {
  121. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  122. appendBackView.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1).cgColor
  123. appendBackView.layer?.backgroundColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1).cgColor
  124. } else {
  125. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  126. appendBackView.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1).cgColor
  127. appendBackView.layer?.backgroundColor = NSColor.white.cgColor;
  128. }
  129. }
  130. func updateLanguageButton(_ languages: [String]?) {
  131. if languages?.count ?? 0 < 1 {
  132. self.languageButton.title = " " + KMLocalizedString("Auto Detection", nil)
  133. return
  134. }
  135. var languageName: String = ""
  136. if languages?.count ?? 0 > 0 {
  137. for i in 0..<(languages?.count ?? 0) {
  138. let language = languages?[i]
  139. if i == 0 {
  140. languageName = language ?? ""
  141. } else {
  142. languageName = languageName.appendingFormat(",%@", language ?? "")
  143. }
  144. }
  145. } else {
  146. languageName = ""
  147. }
  148. self.languageButton.title = " " + languageName
  149. }
  150. func converArrType(arr: Array<KMBatchOperateFile>, keyString: String) -> [String] {
  151. let newArr = NSMutableArray()
  152. for item in arr {
  153. newArr.add(item.filePath)
  154. }
  155. return newArr as! [String]
  156. }
  157. func isConnectionAvailable() -> Bool {
  158. // var isExistenceNetwork = true
  159. // let reach = Reachability(hostname: "www.apple.com")
  160. // let status: NetworkStatus = NetworkStatus(rawValue: (reach?.currentReachabilityStatus())!.rawValue)
  161. // switch status.rawValue {
  162. // case 0:
  163. // isExistenceNetwork = false
  164. // case 1:
  165. // isExistenceNetwork = true
  166. // case 2:
  167. // isExistenceNetwork = true
  168. // default:
  169. // break
  170. // }
  171. if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 {
  172. return false
  173. }
  174. return true
  175. }
  176. func beginImageToPDF() {
  177. if self.files?.count ?? 0 < 1 {
  178. return
  179. }
  180. let photoArray = converArrType(arr: self.files!, keyString: "")
  181. var path: String = ""
  182. var isMerge = false
  183. var isCreatNewPDF = false
  184. var isOCR = false
  185. if self.ocrSelectBtn.state == .on {
  186. isOCR = true
  187. }
  188. var isSaveAs = false
  189. if self.saveAsButton.state == .on {
  190. isSaveAs = true
  191. }
  192. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  193. if isOCR && !self.isConnectionAvailable() && plan == 0 {
  194. let alert = NSAlert()
  195. alert.alertStyle = .critical
  196. alert.messageText = NSLocalizedString("Connection Error", comment: "")
  197. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  198. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  199. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  200. alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
  201. } else {
  202. alert.runModal()
  203. }
  204. return
  205. }
  206. if self.createNewPDFBtn.state == .on {
  207. if (self.choosePath.count < 1) {
  208. let alert = NSAlert()
  209. alert.alertStyle = .critical
  210. alert.messageText = String(format: NSLocalizedString("Output Folder cannot be empty.", comment: ""))
  211. alert.runModal()
  212. return
  213. }
  214. path = self.choosePath
  215. if self.btnMerge.state == .on {
  216. isMerge = true
  217. }
  218. isCreatNewPDF = true
  219. } else {
  220. let appenString = self.appendTextField.stringValue
  221. if appenString.isEmpty {
  222. let alert = NSAlert()
  223. alert.alertStyle = .critical
  224. alert.messageText = String(format: NSLocalizedString("Select a File", comment: ""))
  225. alert.runModal()
  226. return
  227. }
  228. path = self.appendTextField.stringValue
  229. isMerge = true
  230. isCreatNewPDF = false
  231. }
  232. self.languageButton.isEnabled = false
  233. self.planButton.isEnabled = false
  234. self.method.password = self.password
  235. self.interfaceStatus = .Processing
  236. self.method.exportPDFFile(fileArray: photoArray, savePath: path, isOCR: isOCR, isCreatPDF: isCreatNewPDF, isMerge: isMerge, isSaveAsText: isSaveAs) { [weak self] savePath, errorArr, errorOCRArray in
  237. self?.languageButton.isEnabled = true
  238. self?.planButton.isEnabled = true
  239. self?.interfaceStatus = .PrepareProcess
  240. if errorArr.count > 0 {
  241. let dict: [String: Any] = ["isMerge": false, "isSuccess": false]
  242. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateImageToPDFSuccessNotification"), object: self, userInfo: dict)
  243. let alert = NSAlert()
  244. alert.messageText = NSLocalizedString("Conversion Failed", comment: "")
  245. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  246. alert.informativeText = "\(errorArr)"
  247. alert.alertStyle = .informational
  248. alert.runModal()
  249. } else {
  250. if errorOCRArray.count > 0 {
  251. var contextString = NSLocalizedString("Some problems occurred during the last operation:", comment: "")
  252. for filePath in errorOCRArray {
  253. contextString += "\n" + (filePath as AnyObject).lastPathComponent
  254. }
  255. let alert = NSAlert()
  256. alert.messageText = NSLocalizedString("Converted Successfully", comment: "")
  257. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  258. alert.informativeText = contextString
  259. alert.alertStyle = .informational
  260. let response = alert.runModal()
  261. if response == .OK {
  262. self?.viewFileAtFinder(fileName: savePath)
  263. }
  264. } else {
  265. self?.viewFileAtFinder(fileName: savePath)
  266. }
  267. }
  268. }
  269. }
  270. func viewFileAtFinder(fileName: String) {
  271. let dict = ["isMerge": true, "isSuccess": true]
  272. NotificationCenter.default.post(name: Notification.Name("KMBatchOperateImageToPDFSuccessNotification"), object: self, userInfo: dict)
  273. let workspace = NSWorkspace.shared
  274. let url = URL(fileURLWithPath: fileName)
  275. workspace.activateFileViewerSelecting([url])
  276. }
  277. //MARK: Notification
  278. @objc func OCRSelectedLanguagesChangeNotification(notification: Notification) {
  279. let selectedLanguages = notification.object/* as? [KMBatchOperateFile]*/
  280. self.updateLanguageButton(selectedLanguages as? [String])
  281. }
  282. @objc func OCRSelectedPlanChangeNotification(notification: Notification) {
  283. self.OCRSelectedPlanChangeAction()
  284. }
  285. @objc func themeChanged(notification: Notification) {
  286. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  287. self.updateViewColor()
  288. }
  289. }
  290. func OCRSelectedPlanChangeAction() {
  291. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  292. if plan == 0 {
  293. self.planButton.title = " " + KMLocalizedString("Plan 1 (Online)", nil)
  294. } else {
  295. self.planButton.title = " " + KMLocalizedString("Plan 2 (Offline)", nil)
  296. }
  297. KMGOCRManager.default().selectedLanguages = NSMutableArray()
  298. self.updateLanguageButton(KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String])
  299. }
  300. @objc func batchFilesCountNotification(notification: Notification) {
  301. let files: Array? = notification.object as? [KMBatchOperateFile]
  302. self.files? = files ?? []
  303. // if files?.count ?? 0 > 0 {
  304. // haveFiles = true
  305. // } else {
  306. // haveFiles = false
  307. // }
  308. }
  309. @IBAction func buttonClicked_CreateNewPDF(_ sender: NSButton) {
  310. if (sender.state == .on) {
  311. self.btnMerge.isEnabled = true
  312. self.appendPDFBtn.state = .off
  313. self.appendOtherPDFBtn.isEnabled = false
  314. }
  315. }
  316. @IBAction func buttonClicked_AppendOtherPDF(_ sender: NSButton) {
  317. if (sender.state == .on) {
  318. self.createNewPDFBtn.state = .off
  319. self.btnMerge.isEnabled = false
  320. self.appendOtherPDFBtn.isEnabled = true
  321. }
  322. }
  323. @IBAction func buttonClicked_OCRSelect(_ sender: NSButton) {
  324. //MARK: 判断是否付费用户
  325. if (sender.state == .on) {
  326. self.languageButton.isEnabled = true
  327. self.planButton.isEnabled = true
  328. self.saveAsButton.isEnabled = true
  329. } else {
  330. self.languageButton.isEnabled = false
  331. self.planButton.isEnabled = false
  332. self.saveAsButton.isEnabled = false
  333. }
  334. }
  335. @IBAction func buttonClicked_ChooseLanguage(_ sender: NSButton) {
  336. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  337. if plan == 0 {
  338. KMGOCRManager.default().ocrType = .google
  339. } else {
  340. KMGOCRManager.default().ocrType = .apple
  341. }
  342. let popover = NSPopover()
  343. popover.delegate = self
  344. popover.contentViewController = KMLanguageViewController(nibName: "KMLanguageViewController", bundle: Bundle.main)
  345. popover.animates = true
  346. popover.behavior = .transient
  347. popover.show(relativeTo: sender.bounds, of: sender as NSView, preferredEdge: .minX)
  348. }
  349. @IBAction func buttonClicked_ChoosePlan(_ sender: NSButton) {
  350. let popover = NSPopover()
  351. popover.delegate = self
  352. popover.contentViewController = KMPlanViewController(nibName: "KMPlanViewController", bundle: Bundle.main)
  353. popover.animates = true
  354. popover.behavior = .transient
  355. popover.show(relativeTo: sender.bounds, of: sender as NSView, preferredEdge: .minX)
  356. }
  357. @IBAction func buttonClicked_ImageToPDF(_ sender: NSButton) {
  358. //MARK: 判断是否付费用户,展示iap界面
  359. if self.files?.count ?? 0 < 1 {
  360. return
  361. }
  362. if sender.tag == 1 {
  363. self.choosePath = ""
  364. var hasTask = false
  365. for i in 0..<(self.files?.count ?? 0) {
  366. let file = self.files?[i]
  367. file?.currentOperateInfo?.resetState()
  368. if file?.fileType == .Image {
  369. hasTask = true
  370. }
  371. }
  372. if !hasTask {
  373. NSSound.beep()
  374. return
  375. }
  376. var isOCR = false
  377. if self.ocrSelectBtn.state == .on {
  378. isOCR = true
  379. }
  380. var isSaveAs = false
  381. if self.saveAsButton.state == .on {
  382. isSaveAs = true
  383. }
  384. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  385. if isOCR && !isConnectionAvailable() && plan == 0 {
  386. let alert = NSAlert()
  387. alert.alertStyle = .critical
  388. alert.messageText = NSLocalizedString("Connection Error", comment: "")
  389. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  390. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  391. if let window = self.view.window {
  392. alert.beginSheetModal(for: window, completionHandler: nil)
  393. } else {
  394. alert.runModal()
  395. }
  396. return
  397. }
  398. if self.createNewPDFBtn.state == .off {
  399. let appenString = self.appendTextField.stringValue
  400. if appenString.isEmpty {
  401. let alert = NSAlert()
  402. alert.alertStyle = .critical
  403. alert.messageText = NSLocalizedString("Select a File", comment: "")
  404. alert.runModal()
  405. return
  406. }
  407. }
  408. let openPanel = NSOpenPanel()
  409. openPanel.canChooseFiles = false
  410. openPanel.canChooseDirectories = true
  411. openPanel.canCreateDirectories = true
  412. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  413. if result == .OK {
  414. for fileURL in openPanel.urls {
  415. self.choosePath = fileURL.path
  416. self.beginImageToPDF()
  417. }
  418. }
  419. }
  420. } else { // Do something else }
  421. }
  422. }
  423. @IBAction func buttonItemClicked_AppendOtherPDF(_ sender: NSButton) {
  424. let openPanel = NSOpenPanel()
  425. openPanel.allowedFileTypes = ["pdf"]
  426. openPanel.canChooseDirectories = false
  427. openPanel.allowsMultipleSelection = false
  428. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  429. if result == .OK {
  430. guard let url = openPanel.url else { return }
  431. guard let document = PDFDocument(url: url) else {
  432. let alert = NSAlert()
  433. alert.alertStyle = .critical
  434. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  435. alert.runModal()
  436. return
  437. }
  438. if !document.allowsCopying || !document.allowsPrinting {
  439. let alert = NSAlert()
  440. alert.alertStyle = .critical
  441. alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  442. alert.runModal()
  443. return
  444. }
  445. if document.isLocked {
  446. let com = PasswordWindowController(windowNibName: "PasswordWindowController")
  447. com.fileURL = url
  448. com.beginSheetModal(for: self.view.window!) { (password) in
  449. if password.count > 0 {
  450. self.password = password
  451. self.appendTextField.stringValue = url.path
  452. }
  453. }
  454. } else {
  455. self.appendTextField.stringValue = url.path
  456. }
  457. }
  458. }
  459. }
  460. @IBAction func buttonClicked_Help(_ sender: NSButton) {
  461. let helpController = NSViewController()
  462. let textView = NSTextView(frame: NSRect(x: 0, y: 0, width: 300.0, height: 100.0))
  463. textView.backgroundColor = NSColor.clear
  464. textView.isEditable = false
  465. textView.layer?.cornerRadius = 6
  466. let tStrAuto = NSLocalizedString("Choose automatic language detection for better OCR results.", comment: "")
  467. let tStrVPN = NSLocalizedString("The OCR service works via an internet connection. We would suggest you to perform OCR using a VPN connection while the service is limited.", comment: "")
  468. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  469. if plan == 0 {
  470. textView.string = "\(tStrAuto)\n\n\(tStrVPN)"
  471. } else {
  472. textView.frame = NSRect(x: 0, y: 0, width: 300.0, height: 40.0)
  473. textView.string = tStrAuto
  474. }
  475. helpController.view = textView
  476. let popover = NSPopover()
  477. popover.delegate = self
  478. popover.contentViewController = helpController
  479. popover.animates = true
  480. popover.behavior = .transient
  481. popover.show(relativeTo: sender.bounds, of: sender as NSView, preferredEdge: .minY)
  482. }
  483. //MARK: KMImageToPDFMethodDelegate
  484. func imageToPDFMethod(_ method: KMImageToPDFMethod, progress: Float) {
  485. }
  486. }