SettingViewController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks");
  72. let originPath = NSString(string: path).appendingPathComponent("ComPDFKit_Conversion.framework")
  73. let nPath = NSString(string: path).appendingFormat("/ComPDFKit_Conversion_%@.framework", self.sdkPopBtn.titleOfSelectedItem!) as String
  74. NSLog("[KdanAuto]originPath - \(originPath)");
  75. NSLog("[KdanAuto]nPath - \(nPath)");
  76. try? FileManager.default.removeItem(atPath: originPath);
  77. do {
  78. try? FileManager.default.copyItem(atPath: nPath, toPath: originPath)
  79. }catch {
  80. NSLog("[KdanAuto]拷贝出错");
  81. let shPath = Bundle.main.path(forResource: "UpdateSDK.sh", ofType: nil)!
  82. let process = Process()
  83. process.launchPath = "/bin/sh";
  84. process.arguments = [shPath, path, "ComPDFKit_Conversion", self.sdkPopBtn.titleOfSelectedItem!]
  85. let pipe = Pipe()
  86. process.launch()
  87. process.waitUntilExit();
  88. }
  89. self.sdkChangeAlertLbl.isHidden = NSString(string: currentVersion).isEqual(to: self.sdkPopBtn.titleOfSelectedItem)
  90. }
  91. // Show
  92. public func show() {
  93. window.setIsVisible(true);
  94. }
  95. //Window Delegate
  96. func windowShouldClose(_ sender: NSWindow) -> Bool {
  97. if delegate != nil {
  98. delegate.settingViewDidFinished()
  99. }
  100. sender.setIsVisible(false);
  101. return false;
  102. }
  103. }