diff --git a/src/macosx/UnrealIRCd/AppDelegate.swift b/src/macosx/UnrealIRCd/AppDelegate.swift index 9bce34bac..ea49f11d1 100644 --- a/src/macosx/UnrealIRCd/AppDelegate.swift +++ b/src/macosx/UnrealIRCd/AppDelegate.swift @@ -14,45 +14,35 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var mainMenu : NSMenu? var appModel : AppModel - var daemonModel : DaemonModel - var configurationModel : ConfigurationModel override init() { appModel = AppModel() - daemonModel = DaemonModel() - configurationModel = ConfigurationModel() super.init() } func applicationDidFinishLaunching(aNotification: NSNotification) { assert(mainMenu != nil, "Unable to load Menu from XIB") - appModel.setupStatusItem(mainMenu!) - if configurationModel.shouldAutoStart - { - daemonModel.start() - } + appModel.setupStatusItem(mainMenu!) + appModel.startupComplete() } func applicationWillTerminate(aNotification: NSNotification) { - daemonModel.stop() + appModel.shutdown() } @IBAction func startDaemon(sender: NSMenuItem) { - daemonModel.start() + appModel.startDaemon() } @IBAction func stopDaemon(sender: NSMenuItem) { - daemonModel.stop() + appModel.stopDaemon() } @IBAction func configureDaemon(sender: NSMenuItem) { - let storyboard = NSStoryboard(name: "Main", bundle:nil) - let controller = storyboard!.instantiateControllerWithIdentifier("Configuration") as! NSWindowController - - controller.showWindow(self) + appModel.showPreferences() } @IBAction func help(sender: NSMenuItem) { @@ -60,7 +50,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } @IBAction func quit(sender: NSMenuItem) { - exit(0) + appModel.shutdown() } diff --git a/src/macosx/UnrealIRCd/AppModel.swift b/src/macosx/UnrealIRCd/AppModel.swift index c96e71a18..8a46d51c8 100644 --- a/src/macosx/UnrealIRCd/AppModel.swift +++ b/src/macosx/UnrealIRCd/AppModel.swift @@ -14,14 +14,22 @@ class AppModel var menuItem : NSStatusItem static let logoName = "logo.png" static let helpURL = "https://www.unrealircd.org/docs/UnrealIRCd_3.4.x_documentation" + var daemonModel : DaemonModel + var configurationModel : ConfigurationModel + var windowController : NSWindowController? + var mainMenu : NSMenu init() { + + daemonModel = DaemonModel() + configurationModel = ConfigurationModel() menuItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1/*NSVariableStatusItemLength*/) } func setupStatusItem(menu: NSMenu) { + mainMenu = menu menuItem.image = NSImage(named: AppModel.logoName) menuItem.menu = menu } @@ -34,4 +42,49 @@ class AppModel } } + func showPreferences() + { + windowController!.showWindow(self) + } + + func startupComplete() + { + if configurationModel.shouldAutoStartDaemon + { + daemonModel.start() + updateUIFromDaemon() + } + + + let storyboard = NSStoryboard(name: "Main", bundle:nil) + let controller = storyboard!.instantiateControllerWithIdentifier("Configuration") as! NSWindowController? + assert(controller != nil, "Unable to load window from XIB") + windowController = controller + } + + func shutdown() + { + daemonModel.stop() + exit(0) + } + + func startDaemon() + { + daemonModel.stop() + updateUIFromDaemon() + } + + func stopDaemon() + { + daemonModel.start() + updateUIFromDaemon() + } + + func updateUIFromDaemon() + { + let daemonStatus = daemonModel.isRunning + mainMenu.itemWithTitle("Start UnrealIRCd")?.enabled = !daemonStatus + mainMenu.itemWithTitle("Stop UnrealIRCd")?.enabled = !daemonStatus + } + } \ No newline at end of file diff --git a/src/macosx/UnrealIRCd/ConfigurationModel.swift b/src/macosx/UnrealIRCd/ConfigurationModel.swift index ad49ba0ad..8b67ed59b 100644 --- a/src/macosx/UnrealIRCd/ConfigurationModel.swift +++ b/src/macosx/UnrealIRCd/ConfigurationModel.swift @@ -9,7 +9,60 @@ import Foundation class ConfigurationModel { - var shouldAutoStart : Bool { - return true + let defaults = NSUserDefaults.standardUserDefaults() + var changeDelegates : Set = [] + static let autoStartDaemonKey = "IRCD_AUTOSTART" + static let autoStartAgentKey = "AGENT_AUTOSTART" + + init() + { + } + + func attachChangeDelegate(delegate: ConfigurationModelChangeDelegate) + { + changeDelegates.insert(delegate) + } + + func dettachChangeDelegate(delegate: ConfigurationModelChangeDelegate) + { + changeDelegates.remove(delegate) + } + + var shouldAutoStartAgent : Bool { + set(value) + { + defaults.setBool(value, forKey: ConfigurationModel.autoStartAgentKey) + notifyListeners(); + } + get + { + return defaults.boolForKey(ConfigurationModel.autoStartAgentKey) + } + } + + var shouldAutoStartDaemon : Bool { + set(value) + { + defaults.setBool(value, forKey: ConfigurationModel.autoStartDaemonKey) + notifyListeners(); + } + get + { + return defaults.boolForKey(ConfigurationModel.autoStartDaemonKey) + } + } + + func notifyListeners() + { + for listener in changeDelegates + { + listener.configurationModelChanged(self) + } + } +} + +protocol ConfigurationModelChangeDelegate : Hashable +{ + func configurationModelChanged(model: ConfigurationModel); } \ No newline at end of file diff --git a/src/macosx/UnrealIRCd/DaemonModel.swift b/src/macosx/UnrealIRCd/DaemonModel.swift index 72ea1fc99..fe7abcd6d 100644 --- a/src/macosx/UnrealIRCd/DaemonModel.swift +++ b/src/macosx/UnrealIRCd/DaemonModel.swift @@ -21,5 +21,10 @@ class DaemonModel return false } + var isRunning : Bool + { + return false; + } + } \ No newline at end of file diff --git a/src/macosx/UnrealIRCd/ViewController.swift b/src/macosx/UnrealIRCd/ViewController.swift index 3fa1d9c59..26273257a 100644 --- a/src/macosx/UnrealIRCd/ViewController.swift +++ b/src/macosx/UnrealIRCd/ViewController.swift @@ -7,12 +7,33 @@ // import Cocoa +import AppKit class ViewController: NSViewController { + @IBOutlet weak var autoStartAgentCheckbox : NSButton? + @IBOutlet weak var autoStartDaemonCheckbox : NSButton? + @IBOutlet weak var startStopButton : NSButton? + static let stopButtonString = "Stop" + static let startButtonString = "Start" + var configModel : ConfigurationModel? + + func updateInterface(model: ConfigurationModel, daemonStatus: Bool) + { + configModel = model + autoStartAgentCheckbox?.state = model.shouldAutoStartAgent ? NSOnState : NSOffState + autoStartDaemonCheckbox?.state = model.shouldAutoStartDaemon ? NSOnState : NSOffState + startStopButton?.title = daemonStatus ? ViewController.stopButtonString : ViewController.startButtonString + } + override func viewDidLoad() { super.viewDidLoad() } + + override func viewWillDisappear() { + configModel?.shouldAutoStartAgent = autoStartAgentCheckbox?.state == NSOnState ? true : false + configModel?.shouldAutoStartDaemon = autoStartAgentCheckbox?.state == NSOnState ? true : false + } override var representedObject: AnyObject? { didSet {