123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- //
- // 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!
- self.sdkChangeAlertLbl.isHidden = NSString(string: currentVersion).isEqual(to: self.sdkPopBtn.titleOfSelectedItem)
-
- let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks");
- let originPath = path.appending("/ComPDFKit_Conversion.framework")
- let nPath = NSString(string: path).appendingFormat("/ComPDFKit_Conversion_%@.framework", self.sdkPopBtn.titleOfSelectedItem!) as String
-
- try? FileManager.default.removeItem(atPath: originPath);
- try? FileManager.default.copyItem(atPath: nPath, toPath: originPath)
- }
-
- // Show
- public func show() {
- window.setIsVisible(true);
- }
-
- //Window Delegate
- func windowShouldClose(_ sender: NSWindow) -> Bool {
- if delegate != nil {
- delegate.settingViewDidFinished()
- }
-
- sender.setIsVisible(false);
-
- return false;
- }
-
-
-
- }
|