// // AppDelegate.swift // Logger4 // // Created by Kilian Hofmann on 15.06.17. // Copyright © 2017 Kilian Hofmann. All rights reserved. // import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { // MARK: - Things that have to be kept alive for the application lifetime // Status Item var statusItem: NSStatusItem = NSStatusItem() // URL Session for DOCSIS data and Internet check let urlSession: URLSession = URLSession(configuration: .default) // Preferences let pref: NSWindowController = NSWindowController(windowNibName: "SettingsWindowController") // Timers var timerFreqs: Timer = Timer() var timerFails: Timer = Timer() // Document content list var content: NSMutableDictionary = NSMutableDictionary() // MARK: - Application Lifecycle func applicationDidFinishLaunching(_ aNotification: Notification) { // Show Preferneces if defaults are empty UserDefaults.standard.register(defaults: ["upstream" : "", "downstream" : ""]) if UserDefaults.standard.string(forKey: "upstream") == nil || UserDefaults.standard.string(forKey: "downstream") == nil { pref.showWindow(nil) } // Set status item statusItem = NSStatusBar.system().statusItem(withLength: -2) statusItem.title = "AL" // Create status item menu let menu: NSMenu = NSMenu.init() menu.autoenablesItems = false let menuAbout: NSMenuItem = NSMenuItem(title: "About Logger", action: #selector(NSApp.orderFrontStandardAboutPanel(_:)), keyEquivalent: "") let menuQuit: NSMenuItem = NSMenuItem.init(title: "Quit", action: #selector(NSApp.terminate(_:)), keyEquivalent: "q") let menuPref: NSMenuItem = NSMenuItem(title: "Preferences", action: #selector(self.preferences(_:)), keyEquivalent: "") menuPref.image = NSImage(named: NSImageNameActionTemplate) // Layout menu menu.addItem(menuAbout) menu.addItem(NSMenuItem.separator()) menu.addItem(menuPref) menu.addItem(NSMenuItem.separator()) menu.addItem(menuQuit) // Add menu statusItem.menu = menu // Check if directory exists, creat or exit let fileManager = FileManager.default var directory: ObjCBool = ObjCBool(false) let exists: Bool = fileManager.fileExists(atPath: ("~/KDLog" as NSString).expandingTildeInPath, isDirectory: &directory) if exists && directory.boolValue { } else if exists { NSLog("FILE WITH NAME KDLog EXISTS, REMOVE IT") exit(1); } else { do { try fileManager.createDirectory(atPath: ("~/KDLog" as NSString).expandingTildeInPath, withIntermediateDirectories: false, attributes: nil) } catch let error as NSError { NSLog("ERROR ON DIRECTORY CREATION: \(error.localizedDescription)") exit(1); } } // Init timers if UserDefaults.standard.string(forKey: "upstream") != nil || UserDefaults.standard.string(forKey: "downstream") != nil { initTimers() } } func applicationShouldTerminate(_ sender: NSApplication) -> NSApplicationTerminateReply { urlSession.invalidateAndCancel() return .terminateNow } // MARK: - Logging Functions func logFreqs(_: Timer) { // let frequencyOperation: FrequencyOperation = FrequencyOperation() // OperationQueue.main.addOperation(frequencyOperation) } func logFails(_: Timer) { // let connectionLossOperation: ConnectionLossOperation = ConnectionLossOperation() // OperationQueue.main.addOperation(connectionLossOperation) } // MARK: - General functions func preferences(_ sender: NSMenuItem) { timerFreqs.invalidate() timerFails.invalidate() pref.showWindow(sender) pref.window?.makeKeyAndOrderFront(self) } func initTimers() { timerFreqs.invalidate() timerFails.invalidate() timerFreqs = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(logFreqs), userInfo: nil, repeats: true) timerFails = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(logFails), userInfo: nil, repeats: true) } @IBAction func close(_ sender: NSButton){ initTimers() sender.superview?.window?.close() } }