<commit_before>//
//  AppDelegate.swift
//  PingThing
//
//  Created by <NAME> on 2.6.2015.
//  Copyright (c) 2015 DiUS Computing Pty Ltd. All rights reserved.
//

import Cocoa

let TargetHostUserDefaultsKey = "target-host"
let PingIntervalUserDefaultsKey = "ping-interval"

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    
    //bug in XCode :( - NSSquareStatusItemLength == -2
    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
    
    let StatusMenuItemTag = 0
    let StartStopMenuItemTag = 1
    
    static let pingHelper = PingHelper()
    var host: String = "8.8.8.8"
    
    @IBOutlet weak var window: NSWindow!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        checkUserDefaultsAndLoad()
        
        if let button = statusItem.button {
            button.image = NSImage(named: "StatusBarPaused")
        }
        
        statusItem.menu = createMenu()
        listenToPings(onPingHelper: AppDelegate.pingHelper)
    }
    
    func checkUserDefaultsAndLoad() {
        let prefs = NSUserDefaults.standardUserDefaults()
        if let hostFromPrefs = prefs.objectForKey(TargetHostUserDefaultsKey) as? String {
            AppDelegate.pingHelper.host = hostFromPrefs
        }
    }
    
    func createMenu() -> NSMenu {
        let menu = NSMenu()
        
        let startStopMenuTitle = AppDelegate.pingHelper.running ? "Stop" : "Start"
        let status = AppDelegate.pingHelper.status
        
        let statusMenuItem = NSMenuItem(title: "Status: \(status)", action: nil, keyEquivalent: "")
        statusMenuItem.tag = StatusMenuItemTag
        
        let startStopMenuItem = NSMenuItem(title: startStopMenuTitle,  action: Selector("startStopPing:"), keyEquivalent: "")
        startStopMenuItem.tag = StartStopMenuItemTag
        
        menu.addItem(statusMenuItem)
        menu.addItem(NSMenuItem.separatorItem())
        menu.addItem(startStopMenuItem)
        menu.addItem(NSMenuItem(title: "Preferences…", action: Selector("openPrefs:"), keyEquivalent: ""))
        menu.addItem(NSMenuItem.separatorItem())
        menu.addItem(NSMenuItem(title: "Quit PingThing", action: Selector("terminate:"), keyEquivalent: ""))
        
        return menu
    }
    
    private func updateMenus() {
        if let button = statusItem.button {
            switch AppDelegate.pingHelper.status {
            case Status.Success:
                button.image = NSImage(named: "StatusBarTick")
            case Status.NotRunning:
                button.image = NSImage(named: "StatusBarPaused")
            case Status.Failure:
                button.image = NSImage(named: "StatusBarCross")
            default:
                button.image = NSImage(named: "StatusBarWarning")
            }
        }
        
        if let menu = statusItem.menu {
            if let statusMenu = menu.itemWithTag(StatusMenuItemTag) {
                statusMenu.title = "Status: \(AppDelegate.pingHelper.status.rawValue)"
            }
            
            if let startStopMenu = menu.itemWithTag(StartStopMenuItemTag) {
                startStopMenu.title = AppDelegate.pingHelper.running ? "Stop" : "Start"
            }
        }
    }
    
    private func listenToPings(onPingHelper pingHelper: PingHelper) {
        NSNotificationCenter.defaultCenter().addObserverForName(PingReceivedNotification,
            object: pingHelper,
            queue: NSOperationQueue.mainQueue()) { [weak self] notification in
                if let strongSelf = self {
                    strongSelf.updateMenus()
                }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PingStartedNotification,
            object: pingHelper,
            queue: NSOperationQueue.mainQueue()) { [weak self] notification in
                if let strongSelf = self {
                    strongSelf.updateMenus()
                }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PingStoppedNotification,<commit_msg>renamed notification<commit_after>//
//  AppDelegate.swift
//  PingThing
//
//  Created by <NAME> on 2.6.2015.
//  Copyright (c) 2015 DiUS Computing Pty Ltd. All rights reserved.
//

import Cocoa

let TargetHostUserDefaultsKey = "target-host"
let PingIntervalUserDefaultsKey = "ping-interval"

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    
    //bug in XCode :( - NSSquareStatusItemLength == -2
    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
    
    let StatusMenuItemTag = 0
    let StartStopMenuItemTag = 1
    
    static let pingHelper = PingHelper()
    var host: String = "8.8.8.8"
    
    @IBOutlet weak var window: NSWindow!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        checkUserDefaultsAndLoad()
        
        if let button = statusItem.button {
            button.image = NSImage(named: "StatusBarPaused")
        }
        
        statusItem.menu = createMenu()
        listenToPings(onPingHelper: AppDelegate.pingHelper)
    }
    
    func checkUserDefaultsAndLoad() {
        let prefs = NSUserDefaults.standardUserDefaults()
        if let hostFromPrefs = prefs.objectForKey(TargetHostUserDefaultsKey) as? String {
            AppDelegate.pingHelper.host = hostFromPrefs
        }
    }
    
    func createMenu() -> NSMenu {
        let menu = NSMenu()
        
        let startStopMenuTitle = AppDelegate.pingHelper.running ? "Stop" : "Start"
        let status = AppDelegate.pingHelper.status
        
        let statusMenuItem = NSMenuItem(title: "Status: \(status)", action: nil, keyEquivalent: "")
        statusMenuItem.tag = StatusMenuItemTag
        
        let startStopMenuItem = NSMenuItem(title: startStopMenuTitle,  action: Selector("startStopPing:"), keyEquivalent: "")
        startStopMenuItem.tag = StartStopMenuItemTag
        
        menu.addItem(statusMenuItem)
        menu.addItem(NSMenuItem.separatorItem())
        menu.addItem(startStopMenuItem)
        menu.addItem(NSMenuItem(title: "Preferences…", action: Selector("openPrefs:"), keyEquivalent: ""))
        menu.addItem(NSMenuItem.separatorItem())
        menu.addItem(NSMenuItem(title: "Quit PingThing", action: Selector("terminate:"), keyEquivalent: ""))
        
        return menu
    }
    
    private func updateMenus() {
        if let button = statusItem.button {
            switch AppDelegate.pingHelper.status {
            case Status.Success:
                button.image = NSImage(named: "StatusBarTick")
            case Status.NotRunning:
                button.image = NSImage(named: "StatusBarPaused")
            case Status.Failure:
                button.image = NSImage(named: "StatusBarCross")
            default:
                button.image = NSImage(named: "StatusBarWarning")
            }
        }
        
        if let menu = statusItem.menu {
            if let statusMenu = menu.itemWithTag(StatusMenuItemTag) {
                statusMenu.title = "Status: \(AppDelegate.pingHelper.status.rawValue)"
            }
            
            if let startStopMenu = menu.itemWithTag(StartStopMenuItemTag) {
                startStopMenu.title = AppDelegate.pingHelper.running ? "Stop" : "Start"
            }
        }
    }
    
    private func listenToPings(onPingHelper pingHelper: PingHelper) {
        NSNotificationCenter.defaultCenter().addObserverForName(StatusChangedNotification,
            object: pingHelper,
            queue: NSOperationQueue.mainQueue()) { [weak self] notification in
                if let strongSelf = self {
                    strongSelf.updateMenus()
                }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PingStartedNotification,
            object: pingHelper,
            queue: NSOperationQueue.mainQueue()) { [weak self] notification in
                if let strongSelf = self {
                    strongSelf.updateMenus()
                }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PingStoppedNotification,
{'fraction_non_alphanumeric': 0.0901941508970263, 'fraction_numerical': 0.004423691324649791, 'mean_word_length': 2.276570048309179, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7431116', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2141, 'n_words': 434}