diff --git a/Noti/AuthViewController.swift b/Noti/AuthViewController.swift index 543bb2e..bc665b1 100644 --- a/Noti/AuthViewController.swift +++ b/Noti/AuthViewController.swift @@ -17,7 +17,7 @@ class AuthViewController: NSViewController, WebFrameLoadDelegate { override func viewDidLoad() { super.viewDidLoad() - let req = NSURLRequest(URL:NSURL(string:"https://www.pushbullet.com/authorize?client_id=K5fHgHU2IipmPaMov3CkOHkJb0DKC6Oc&redirect_uri=https%3A%2F%2Fjari.io%2Fnoti%2Fredirect&response_type=token&scope=everything")!) + let req = NSURLRequest(URL:NSURL(string:"https://www.pushbullet.com/authorize?client_id=QTVK7zATuEcu4sME8TrwLBMuoW7vC7Wr&redirect_uri=about:blank&response_type=token&scope=everything")!) webView.frameLoadDelegate = self webView.mainFrame.loadRequest(req) } @@ -35,8 +35,8 @@ class AuthViewController: NSViewController, WebFrameLoadDelegate { if let ds = frame.dataSource { if let url = ds.response.URL { - if url.absoluteString.hasPrefix("https://jari.io/noti/redirect") { - let token = (url.absoluteString as NSString).substringFromIndex(43) + if url.absoluteString.hasPrefix("about:blank") { + let token = (url.absoluteString as NSString).substringFromIndex(27) let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate print("Got token!", token, "saving and restarting PushManager") diff --git a/Noti/Base.lproj/Main.storyboard b/Noti/Base.lproj/Main.storyboard index 1af4c71..9cb4b89 100644 --- a/Noti/Base.lproj/Main.storyboard +++ b/Noti/Base.lproj/Main.storyboard @@ -653,7 +653,7 @@ - + diff --git a/Noti/IntroViewController.swift b/Noti/IntroViewController.swift index a45da17..ebc3634 100644 --- a/Noti/IntroViewController.swift +++ b/Noti/IntroViewController.swift @@ -33,7 +33,7 @@ class IntroViewController: NSViewController { self.view.window!.styleMask -= NSClosableWindowMask NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = 0.25 + context.duration = 0.50 self.authTxt.animator().alphaValue = 0 self.authBtn.animator().alphaValue = 0 @@ -44,13 +44,13 @@ class IntroViewController: NSViewController { self.authImg.alphaValue = 0 NSAnimationContext.runAnimationGroup({ (context) -> Void in - context.duration = 0.25 + context.duration = 0.50 self.authImg.animator().alphaValue = 1 }, completionHandler: nil) }) - NSTimer.scheduledTimerWithTimeInterval(1.5, target: NSBlockOperation(block: self.view.window!.close), selector: #selector(NSOperation.main), userInfo: nil, repeats: false) + NSTimer.scheduledTimerWithTimeInterval(3, target: NSBlockOperation(block: self.view.window!.close), selector: #selector(NSOperation.main), userInfo: nil, repeats: false) } diff --git a/Noti/PushManager.swift b/Noti/PushManager.swift index cba216c..05021e8 100644 --- a/Noti/PushManager.swift +++ b/Noti/PushManager.swift @@ -36,7 +36,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate self.initCrypt() print("Getting user info...") - getUserInfo { () in + getUserInfo { self.connect() } } @@ -80,7 +80,11 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate NSNotificationCenter.defaultCenter().postNotificationName("StateChange", object: object) } + var _callback:(() -> Void)? = nil func getUserInfo(callback: (() -> Void)?) { + //todo: this is kinda dirty ... + self._callback = callback + let headers = [ "Access-Token": token ]; @@ -90,16 +94,36 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate if let info = response.result.value { debugPrint(info) self.userInfo = JSON.parse(info) - if callback != nil { - callback!() + + if self.userInfo!["error"].isExists() { + self.killed = true + self.disconnect() + self.setState("Disconnected: " + self.userInfo!["error"]["message"].string!, disabled: true) + } else { + if callback != nil { + callback!() + } } + } else if response.result.error != nil { - self.setState("I'm RIP") //todo + if callback == nil { + self.killed = true + self.disconnect() + self.setState("Failed to log in.") + } else { + self.setState("Failed to log in, retrying in 2 seconds...") + NSTimer.scheduledTimerWithTimeInterval(2, target: NSBlockOperation(block: self.retryUserInfo), selector: #selector(NSOperation.main), userInfo: nil, repeats: false) + } + } } - } + func retryUserInfo() { + getUserInfo(self._callback) + } + + func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) { switch notification.activationType { case .ActionButtonClicked: @@ -232,10 +256,17 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate if(!self.killed) { print("Reconnecting in 5 sec"); - setState("Disconnected, retrying in 5 seconds.", disabled: true) + if error != nil { + setState("Disconnected: \(error!.localizedDescription), retrying...", disabled: true) + } + else { + setState("Disconnected, retrying...", disabled: true) + } + NSTimer.scheduledTimerWithTimeInterval(5, target: NSBlockOperation(block: self.connect), selector: #selector(NSOperation.main), userInfo: nil, repeats: false) } else { print("Not going to reconnect: I'm killed") + setState("Disconnected. Please log in.", disabled: true) } } diff --git a/Noti/StatusMenuController.swift b/Noti/StatusMenuController.swift index b02c833..3bb0660 100644 --- a/Noti/StatusMenuController.swift +++ b/Noti/StatusMenuController.swift @@ -22,6 +22,7 @@ class StatusMenuController: NSObject, NSUserNotificationCenterDelegate { button.image = NSImage(named: "StatusBarButtonImage") statusItem.menu = menu; } + statusItem.button?.appearsDisabled = true menuItem.enabled = true NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(StatusMenuController.stateChange(_:)), name:"StateChange", object: nil) @@ -46,7 +47,6 @@ class StatusMenuController: NSObject, NSUserNotificationCenterDelegate { newImage.size = destSize let finalImage = NSImage(data: newImage.TIFFRepresentation!)! menuItem.image = finalImage - } else { menuItem.image = nil }