// // SettingViewController.swift // KdanAuto // // Created by 朱东勇 on 2022/11/21. // import Foundation import AppKit import ComPDFKit_Conversion enum PathType { case Origin; case Result; case Check; } public protocol SettingViewControllerDelegate : NSObjectProtocol { @MainActor func settingViewDidFinished() } class SettingViewController : NSViewController, NSTextFieldDelegate, NSWindowDelegate { static var vc : SettingViewController! @IBOutlet var window : NSWindow! public var delegate : SettingViewControllerDelegate! @IBOutlet var originPathTF : NSTextField! @IBOutlet var originPathBtn : NSButton! @IBOutlet var sdkPopBtn : NSPopUpButton! @IBOutlet var sdkChangeAlertLbl : NSTextField! class func shared() -> SettingViewController { if vc == nil { vc = SettingViewController() Bundle.main.loadNibNamed("SettingViewController", owner: vc, topLevelObjects: nil) } return vc } override func viewDidLoad() { } override func viewWillAppear() { self.originPathTF.stringValue = DataModel.shared.directoryPath() ; let items = NSMutableArray() let currentVersion = CPDFConvertKit.sharedInstance().versionNumber! items.add(currentVersion); let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks"); NSLog("\(path)") let subpaths = NSArray(array: try! FileManager.default.contentsOfDirectory(atPath: path)) for item in NSArray(array: subpaths) { var subPath = item as! String subPath = NSString(string: subPath).pathComponents.first ?? "" subPath = NSString(string: subPath).deletingPathExtension if (NSString(string: subPath).contains("ComPDFKit_Conversion_")) { let version = NSString(string: subPath).replacingOccurrences(of: "ComPDFKit_Conversion_", with: "") if (NSString(string: version).length > 0 && !items.contains(version)) { items.add(version) } } } self.sdkPopBtn.removeAllItems() self.sdkPopBtn.addItems(withTitles: (items as! [String])); self.sdkPopBtn.selectItem(withTitle: currentVersion) } // IBAction @IBAction func pathSelected(_ sender:NSButton) { let openPanel = NSOpenPanel() openPanel.canChooseFiles = false openPanel.canChooseDirectories = true if openPanel.runModal() == NSApplication.ModalResponse.OK { let path = openPanel.directoryURL?.relativePath DataModel.shared.setDirectoryPath(path!); self.originPathTF.stringValue = path!; } } @IBAction func sdkChangeAction(_ sender:NSButton) { let currentVersion = CPDFConvertKit.sharedInstance().versionNumber! let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks"); let originPath = NSString(string: path).appendingPathComponent("ComPDFKit_Conversion.framework") let nPath = NSString(string: path).appendingFormat("/ComPDFKit_Conversion_%@.framework", self.sdkPopBtn.titleOfSelectedItem!) as String NSLog("[KdanAuto]originPath - \(originPath)"); NSLog("[KdanAuto]nPath - \(nPath)"); try? FileManager.default.removeItem(atPath: originPath); do { try? FileManager.default.copyItem(atPath: nPath, toPath: originPath) }catch { NSLog("[KdanAuto]拷贝出错"); let shPath = Bundle.main.path(forResource: "UpdateSDK.sh", ofType: nil)! let process = Process() process.launchPath = "/bin/sh"; process.arguments = [shPath, path, "ComPDFKit_Conversion", self.sdkPopBtn.titleOfSelectedItem!] let pipe = Pipe() process.launch() process.waitUntilExit(); } self.sdkChangeAlertLbl.isHidden = NSString(string: currentVersion).isEqual(to: self.sdkPopBtn.titleOfSelectedItem) } // Show public func show() { window.setIsVisible(true); } //Window Delegate func windowShouldClose(_ sender: NSWindow) -> Bool { if delegate != nil { delegate.settingViewDidFinished() } sender.setIsVisible(false); return false; } }