omlapp/Open Media Library/AppDelegate.swift

78 lines
2.3 KiB
Swift

//
// AppDelegate.swift
// Open Media Library
//
// Created on 14/12/15.
// GPL3 2015 Open Media Library. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var statusMenu: NSMenu!
let autostart = Process.arguments.count >= 2 ? Process.arguments[1] == "--autostart" : false
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
let basePath = NSString(string:"~/Library/Application Support/Open Media Library").stringByExpandingTildeInPath
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")
//invert icon in dark status bar mode
//icon!.template = true
statusItem.image = icon
statusItem.menu = statusMenu
if (NSFileManager.defaultManager().fileExistsAtPath(basePath)) {
let server = NSTask()
server.launchPath = basePath.stringByAppendingString("/ctl")
server.arguments = ["start"]
server.launch()
if (!autostart) {
load()
}
} else {
let resourcePath = NSBundle.mainBundle().resourcePath
let install_py = resourcePath?.stringByAppendingString("/install.py")
let install_html = resourcePath?.stringByAppendingString("/install.html")
let install = NSTask()
install.launchPath = "/usr/bin/python3"
install.arguments = [install_py!]
install.launch()
open(install_html!)
}
}
func applicationWillTerminate(aNotification: NSNotification) {
let task = NSTask()
task.launchPath = basePath.stringByAppendingString("/ctl")
task.arguments = ["stop"]
task.launch()
task.waitUntilExit()
}
func load() {
open(basePath.stringByAppendingString("/openmedialibrary/static/html/load.html"))
}
func open(path: String) {
let task = NSTask()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
}
@IBAction func onOpen(sender: NSMenuItem) {
load()
}
@IBAction func onQuit(sender: NSMenuItem) {
NSApplication.sharedApplication().terminate(self)
}
}