SettingViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // SettingViewController.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/21.
  6. //
  7. import Foundation
  8. import AppKit
  9. import ComPDFKit_Conversion
  10. enum PathType {
  11. case Origin;
  12. case Result;
  13. case Check;
  14. }
  15. public protocol SettingViewControllerDelegate : NSObjectProtocol {
  16. @MainActor func settingViewDidFinished()
  17. }
  18. class SettingViewController : NSViewController, NSTextFieldDelegate, NSWindowDelegate {
  19. static var vc : SettingViewController!
  20. @IBOutlet var window : NSWindow!
  21. public var delegate : SettingViewControllerDelegate!
  22. @IBOutlet var originPathTF : NSTextField!
  23. @IBOutlet var originPathBtn : NSButton!
  24. @IBOutlet var sdkPopBtn : NSPopUpButton!
  25. @IBOutlet var sdkChangeAlertLbl : NSTextField!
  26. class func shared() -> SettingViewController {
  27. if vc == nil {
  28. vc = SettingViewController()
  29. Bundle.main.loadNibNamed("SettingViewController", owner: vc, topLevelObjects: nil)
  30. }
  31. return vc
  32. }
  33. override func viewDidLoad() {
  34. }
  35. override func viewWillAppear() {
  36. self.originPathTF.stringValue = DataModel.shared.directoryPath() ;
  37. let items = NSMutableArray()
  38. let currentVersion = CPDFConvertKit.sharedInstance().versionNumber!
  39. items.add(currentVersion);
  40. let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks");
  41. NSLog("\(path)")
  42. let subpaths = NSArray(array: try! FileManager.default.contentsOfDirectory(atPath: path))
  43. for item in NSArray(array: subpaths) {
  44. var subPath = item as! String
  45. subPath = NSString(string: subPath).pathComponents.first ?? ""
  46. subPath = NSString(string: subPath).deletingPathExtension
  47. if (NSString(string: subPath).contains("ComPDFKit_Conversion_")) {
  48. let version = NSString(string: subPath).replacingOccurrences(of: "ComPDFKit_Conversion_", with: "")
  49. if (NSString(string: version).length > 0 && !items.contains(version)) {
  50. items.add(version)
  51. }
  52. }
  53. }
  54. self.sdkPopBtn.removeAllItems()
  55. self.sdkPopBtn.addItems(withTitles: (items as! [String]));
  56. self.sdkPopBtn.selectItem(withTitle: currentVersion)
  57. }
  58. // IBAction
  59. @IBAction func pathSelected(_ sender:NSButton) {
  60. let openPanel = NSOpenPanel()
  61. openPanel.canChooseFiles = false
  62. openPanel.canChooseDirectories = true
  63. if openPanel.runModal() == NSApplication.ModalResponse.OK {
  64. let path = openPanel.directoryURL?.relativePath
  65. DataModel.shared.setDirectoryPath(path!);
  66. self.originPathTF.stringValue = path!;
  67. }
  68. }
  69. @IBAction func sdkChangeAction(_ sender:NSButton) {
  70. let currentVersion = CPDFConvertKit.sharedInstance().versionNumber!
  71. self.sdkChangeAlertLbl.isHidden = NSString(string: currentVersion).isEqual(to: self.sdkPopBtn.titleOfSelectedItem)
  72. let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks");
  73. let originPath = path.appending("/ComPDFKit_Conversion.framework")
  74. let nPath = NSString(string: path).appendingFormat("/ComPDFKit_Conversion_%@.framework", self.sdkPopBtn.titleOfSelectedItem!) as String
  75. try? FileManager.default.removeItem(atPath: originPath);
  76. try? FileManager.default.copyItem(atPath: nPath, toPath: originPath)
  77. }
  78. // Show
  79. public func show() {
  80. window.setIsVisible(true);
  81. }
  82. //Window Delegate
  83. func windowShouldClose(_ sender: NSWindow) -> Bool {
  84. if delegate != nil {
  85. delegate.settingViewDidFinished()
  86. }
  87. sender.setIsVisible(false);
  88. return false;
  89. }
  90. }