From 4224acb678319b23ee9fb1ac1b5a19829fa9131b Mon Sep 17 00:00:00 2001 From: johnste Date: Wed, 8 Jul 2015 23:45:02 +0200 Subject: [PATCH] Fix lots of trailing white space --- Finicky/Finicky/AppDelegate.swift | 38 ++++++++++++------------ Finicky/Finicky/FNAPI.swift | 32 ++++++++++---------- Finicky/Finicky/FNConfigLoader.swift | 24 +++++++-------- Finicky/Finicky/FNPathWatcher.swift | 14 ++++----- Finicky/Finicky/FNShortUrlResolver.swift | 31 +++++++++---------- 5 files changed, 70 insertions(+), 69 deletions(-) diff --git a/Finicky/Finicky/AppDelegate.swift b/Finicky/Finicky/AppDelegate.swift index 4c41f2d..1844686 100644 --- a/Finicky/Finicky/AppDelegate.swift +++ b/Finicky/Finicky/AppDelegate.swift @@ -15,22 +15,22 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet var statusItemMenu: NSMenu! - + var statusItem: NSStatusItem! var configLoader: FNConfigLoader! var shortUrlResolver: FNShortUrlResolver! var urlsToLoad = Array() - + static var defaultBrowser: String! = "com.google.Chrome" - + func applicationDidFinishLaunching(aNotification: NSNotification) { var bundleId = "net.kassett.Finicky" LSSetDefaultHandlerForURLScheme("http", bundleId) LSSetDefaultHandlerForURLScheme("https", bundleId) - + var img: NSImage! = NSImage(named: "statusitem") img.setTemplate(true) - + let bar = NSStatusBar.systemStatusBar() // Workaround for some bug: -1 instead of NSVariableStatusItemLength statusItem = bar.statusItemWithLength(CGFloat(-1)) @@ -39,11 +39,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { statusItem.image = img toggleDockIcon(showIcon: false) } - + @IBAction func reloadConfig(sender: NSMenuItem) { configLoader.reload() } - + @IBAction func showAboutPanel(sender: NSMenuItem) { NSApp.orderFrontStandardAboutPanel(sender) } @@ -58,21 +58,21 @@ class AppDelegate: NSObject, NSApplicationDelegate { } return result } - + func handleGetURLEvent(event: NSAppleEventDescriptor?, withReplyEvent: NSAppleEventDescriptor?) { var url : NSURL = NSURL(string: event!.paramDescriptorForKeyword(AEKeyword(keyDirectObject))!.stringValue!)! let pid = event!.attributeDescriptorForKeyword(AEKeyword(keySenderPIDAttr))!.int32Value let sourceBundleIdentifier = NSRunningApplication(processIdentifier: pid)?.bundleIdentifier - + let callback = callUrlHandlers(sourceBundleIdentifier!) - + if shortUrlResolver.isShortUrl(url) { shortUrlResolver.resolveUrl(url, callback: callback) } else { callback(url: url) } } - + func callUrlHandlers(sourceBundleIdentifier: String)(url: NSURL) { let flags = getFlags() var bundleIdentifier : String! = AppDelegate.defaultBrowser @@ -81,19 +81,19 @@ class AppDelegate: NSObject, NSApplicationDelegate { let strategy = FinickyAPI.callUrlHandlers(newUrl, sourceBundleIdentifier: sourceBundleIdentifier, flags: flags) if strategy["url"] != nil { newUrl = NSURL(string: strategy["url"]!)! - + let bundleId : String! = strategy["bundleIdentifier"] as String! - + if bundleId != nil && !bundleId.isEmpty { bundleIdentifier = strategy["bundleIdentifier"]! } - + if bundleIdentifier != nil && !bundleIdentifier.isEmpty { openUrlWithBrowser(newUrl, bundleIdentifier:bundleIdentifier) } } } - + func openUrlWithBrowser(url: NSURL, bundleIdentifier: String) { var eventDescriptor: NSAppleEventDescriptor? = NSAppleEventDescriptor() var errorInfo : NSDictionary? = nil @@ -101,7 +101,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { var urls = [url] NSWorkspace.sharedWorkspace().openURLs(urls, withAppBundleIdentifier: bundleIdentifier, options: NSWorkspaceLaunchOptions.Default, additionalEventParamDescriptor: nil, launchIdentifiers: nil) } - + func getFlags() -> Dictionary { return [ "cmd": NSEvent.modifierFlags() & .CommandKeyMask != nil, @@ -110,16 +110,16 @@ class AppDelegate: NSObject, NSApplicationDelegate { "alt": NSEvent.modifierFlags() & .AlternateKeyMask != nil ] } - + func applicationWillFinishLaunching(aNotification: NSNotification) { configLoader = FNConfigLoader() configLoader.reload() shortUrlResolver = FNShortUrlResolver() - + var appleEventManager:NSAppleEventManager = NSAppleEventManager.sharedAppleEventManager() appleEventManager.setEventHandler(self, andSelector: "handleGetURLEvent:withReplyEvent:", forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) } - + func applicationWillTerminate(aNotification: NSNotification) { } } diff --git a/Finicky/Finicky/FNAPI.swift b/Finicky/Finicky/FNAPI.swift index dc98018..49188f8 100644 --- a/Finicky/Finicky/FNAPI.swift +++ b/Finicky/Finicky/FNAPI.swift @@ -16,35 +16,35 @@ import JavaScriptCore } @objc class FinickyAPI : NSObject, FinickyAPIExports { - + private static var urlHandlers = Array() - + class func setDefaultBrowser(browser: String?) -> Void { AppDelegate.defaultBrowser = browser } - + static func log(message: String?) -> Void { if message != nil { NSLog(message!) } } - + class func onUrl(handler: JSValue) -> Void { urlHandlers.append(handler) } - + class func reset() -> Void { urlHandlers.removeAll(keepCapacity: true) } - + /** Get strategy from registered handlers - + @param originalUrl The original url that triggered finicky to start - + @param sourceBundleIdentifier Bundle identifier of the application that triggered the url to open - - @return A dictionary keyed with "url" and "bundleIdentifier" with + + @return A dictionary keyed with "url" and "bundleIdentifier" with the new url and bundle identifier to spawn */ @@ -53,12 +53,12 @@ import JavaScriptCore "url": originalUrl.absoluteString!, "bundleIdentifier": "" ] - + var options : Dictionary = [ "sourceBundleIdentifier": sourceBundleIdentifier, "flags": flags ] - + for handler in urlHandlers { let url = strategy["url"]! let val = handler.callWithArguments([url, options]) @@ -69,18 +69,18 @@ import JavaScriptCore if handlerStrategy["url"] != nil { strategy["url"] = (handlerStrategy["url"] as! String) } - + if handlerStrategy["bundleIdentifier"] != nil { strategy["bundleIdentifier"] = (handlerStrategy["bundleIdentifier"] as! String) } - + if handlerStrategy["last"] != nil { break } } } - } + } return strategy } - + } \ No newline at end of file diff --git a/Finicky/Finicky/FNConfigLoader.swift b/Finicky/Finicky/FNConfigLoader.swift index cd7572f..ec82e03 100644 --- a/Finicky/Finicky/FNConfigLoader.swift +++ b/Finicky/Finicky/FNConfigLoader.swift @@ -12,21 +12,21 @@ import JavaScriptCore var FNConfigPath: String = "~/.finicky.js" class FNConfigLoader { - + var configPaths: NSMutableSet; var configWatcher: FNPathWatcher?; var monitor : FNPathWatcher!; - + init() { self.configPaths = NSMutableSet() } - + func resetConfigPaths() { FinickyAPI.reset() configPaths.removeAllObjects() configPaths.addObject(FNConfigPath) } - + func setupConfigWatcher() { let url = NSURL(fileURLWithPath: FNConfigPath.stringByExpandingTildeInPath)! @@ -35,34 +35,34 @@ class FNConfigLoader { }) monitor.start() } - + func reload() { self.resetConfigPaths() var error:NSError? let filename: String = FNConfigPath.stringByStandardizingPath var config: String? = String(contentsOfFile: filename, encoding: NSUTF8StringEncoding, error: &error) - + if config == nil { - println("Config file could not be read or found") + println("Config file could not be read or found") return } - + if let theError = error { print("\(theError.localizedDescription)") } - + var ctx: JSContext = JSContext() - + ctx.exceptionHandler = { context, exception in println("JS Error: \(exception)") } - + self.setupAPI(ctx) ctx.evaluateScript(config!) setupConfigWatcher() } - + func setupAPI(ctx: JSContext) { ctx.setObject(FinickyAPI.self, forKeyedSubscript: "api") ctx.setObject(FinickyAPI.self, forKeyedSubscript: "finicky") diff --git a/Finicky/Finicky/FNPathWatcher.swift b/Finicky/Finicky/FNPathWatcher.swift index fc99407..fba3c63 100644 --- a/Finicky/Finicky/FNPathWatcher.swift +++ b/Finicky/Finicky/FNPathWatcher.swift @@ -9,18 +9,18 @@ import Foundation public class FNPathWatcher { - + enum State { case On, Off } - + private let source: dispatch_source_t private let descriptor: CInt private var state: State = .Off - + /// Creates a folder monitor object with monitoring enabled. public init(url: NSURL, handler: ()->Void) { - + state = .Off descriptor = open(url.fileSystemRepresentation, O_EVTONLY) let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) @@ -35,7 +35,7 @@ public class FNPathWatcher { //dispatch_source_set_cancel_handler({}) start() } - + /// Starts sending notifications if currently stopped public func start() { if state == .Off { @@ -43,7 +43,7 @@ public class FNPathWatcher { dispatch_resume(source) } } - + /// Stops sending notifications if currently enabled public func stop() { if state == .On { @@ -51,7 +51,7 @@ public class FNPathWatcher { dispatch_suspend(source) } } - + deinit { close(descriptor) dispatch_source_cancel(source) diff --git a/Finicky/Finicky/FNShortUrlResolver.swift b/Finicky/Finicky/FNShortUrlResolver.swift index 932e9b8..e75a783 100644 --- a/Finicky/Finicky/FNShortUrlResolver.swift +++ b/Finicky/Finicky/FNShortUrlResolver.swift @@ -11,12 +11,12 @@ import Foundation class ResolveShortUrls: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate { private var shortUrlResolver : FNShortUrlResolver? = nil - + init(shortUrlResolver: FNShortUrlResolver) { self.shortUrlResolver = shortUrlResolver super.init() } - + func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) { var newRequest : NSURLRequest? = request @@ -27,47 +27,48 @@ class ResolveShortUrls: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate } } } - + completionHandler(newRequest) - + } } class FNShortUrlResolver { - + private var shortUrlProviders = [ "bit.ly", "goo.gl", - "owl.ly", + "ow.ly", "deck.ly", "t.co", "su.pr", "spoti.fi", "fur.ly", - "tinyurl.com" + "tinyurl.com", + "tiny.cc" ] - + init() { } - + func isShortUrl(url: NSURL) -> Bool { return contains(shortUrlProviders, url.host!) } - + func resolveUrl(url: NSURL, callback: ((NSURL) -> Void)) -> Void { if !self.isShortUrl(url) { callback(url) return } - + var response: NSURLResponse? var error: NSError? - + var request = NSURLRequest(URL: url) let myDelegate = ResolveShortUrls(shortUrlResolver: self) let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: myDelegate, delegateQueue: nil) - + let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in if let responsy : NSHTTPURLResponse = response as? NSHTTPURLResponse { let newUrl = NSURL(string: (responsy.allHeaderFields["Location"] as? String)!)! @@ -77,8 +78,8 @@ class FNShortUrlResolver { } }) - - + + task.resume() return }