1
0
mirror of https://github.com/unrealircd/unrealircd.git synced 2026-07-08 16:33:14 +02:00

Further revisions to MVC architecture

This commit is contained in:
Travis McArthur
2015-07-18 23:18:29 -07:00
parent 6b2fefeadc
commit 7e23713ace
5 changed files with 141 additions and 19 deletions
+7 -17
View File
@@ -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()
}
+53
View File
@@ -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
}
}
+55 -2
View File
@@ -9,7 +9,60 @@
import Foundation
class ConfigurationModel {
var shouldAutoStart : Bool {
return true
let defaults = NSUserDefaults.standardUserDefaults()
var changeDelegates : Set<ConfigurationModelChangeDelegate> = []
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);
}
+5
View File
@@ -21,5 +21,10 @@ class DaemonModel
return false
}
var isRunning : Bool
{
return false;
}
}
@@ -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 {