omlapp/Open Media Library/AppDelegate.swift

79 lines
2.3 KiB
Swift
Raw Permalink Normal View History

2015-12-14 19:29:03 +00:00
//
// 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!
2016-04-14 10:57:58 +00:00
let autostart = Process.arguments.count >= 2 ? Process.arguments[1] == "--autostart" : false
2015-12-14 19:29:03 +00:00
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
let basePath = NSString(string:"~/Library/Application Support/Open Media Library").stringByExpandingTildeInPath
2016-04-14 10:57:58 +00:00
2015-12-14 19:29:03 +00:00
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()
2016-04-14 10:57:58 +00:00
if (!autostart) {
load()
}
2015-12-14 19:29:03 +00:00
} 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"
2015-12-14 19:29:03 +00:00
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)
}
}