 //
 
 import Cocoa
+
+private let LastKnownDeviceKey = "DLSLastKnownDeviceKey"
 
 class ConsoleWindowController: NSWindowController {
     

    @IBOutlet private var sidebarTable : NSTableView!
    
    @IBOutlet private var bodyController : NSViewController!
    
    private let deviceController : DeviceController = DeviceController()
    private var currentConnection : DeviceConnection?

        viewGrouper.delegate = self
    }
    
    private func lastKnownConnection() -> String? {
        return NSUserDefaults.standardUserDefaults().objectForKey(LastKnownDeviceKey) as? String
    }
    
    private func saveLastKnownConnection(connection : String) {
        NSUserDefaults.standardUserDefaults().setObject(connection, forKey: LastKnownDeviceKey)
    }
    
    private func devicesChanged() {
        if let device = currentConnection?.device {
            itemsChangedBroadcaster.notifyListeners(.Active(current : device, all : deviceController.knownDevices))
        }
        else if deviceController.hasDevices {
            itemsChangedBroadcaster.notifyListeners(.Available(deviceController.knownDevices))
            if let lastKnown = lastKnownConnection() {
                for device in deviceController.knownDevices {
                    if device.displayName == lastKnown {
                        connectToDevice(device)
                        break
                    }
                }
            }
        }
        else {
            itemsChangedBroadcaster.notifyListeners(.None)
