Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Delete screenshot" option && solved bug after deleting #23

Merged
merged 1 commit into from
Aug 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
18 changes: 16 additions & 2 deletions mac2imgur/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
monitor = ScreenshotMonitor(delegate: self)

NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self

}

func uploadAttemptCompleted(successful: Bool, link: String) {
func uploadAttemptCompleted(successful: Bool, link: String, pathToImage: String) {
if successful {
lastLink = link
copyLinkToClipboard()
displayNotification("Screenshot uploaded successfully!", informativeText: lastLink)

if imgurSession.deleteScreenshotAfterUpload! {
deleteScreenshot(pathToImage)
}

} else {
displayNotification("Screenshot upload failed...", informativeText: "")
}
Expand Down Expand Up @@ -87,6 +91,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
pasteBoard.setString(lastLink, forType: NSStringPboardType)
}

func deleteScreenshot(pathToImage: String!){
let fileManager = NSFileManager.defaultManager()
var error: NSError?
fileManager.removeItemAtPath(pathToImage, error: &error)
if error != nil {
NSLog(error!.localizedDescription)
}

}

func quit() {
NSApplication.sharedApplication().terminate(self)
}
Expand Down
27 changes: 18 additions & 9 deletions mac2imgur/ImgurClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class ImgurClient: NSObject {

let REFRESH_TOKEN_CONSTANT: NSString! = "refresh_token"
let USERNAME_CONSTANT: NSString! = "imgur_username"
let DELETE_SCREENSHOT_AFTER_UPLOAD_CONSTANT: NSString! = "delete_screenshot_after_upload"

var isUserLoggedIn: Bool! = false
var lastTokenExpiry: NSDate?
var deleteScreenshotAfterUpload: Bool! = false

var username: NSString?
var accessToken: NSString?
Expand All @@ -32,6 +34,7 @@ class ImgurClient: NSObject {
let userDefaults = NSUserDefaults.standardUserDefaults()
refreshToken = userDefaults.objectForKey(REFRESH_TOKEN_CONSTANT) as NSString?
username = userDefaults.objectForKey(USERNAME_CONSTANT) as NSString?
deleteScreenshotAfterUpload = userDefaults.boolForKey(DELETE_SCREENSHOT_AFTER_UPLOAD_CONSTANT)
if refreshToken != nil {
isUserLoggedIn = true
}
Expand Down Expand Up @@ -106,9 +109,9 @@ class ImgurClient: NSObject {
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
NSLog("Response: \(response)")
//NSLog("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
NSLog("Body: \(strData)")
//NSLog("Body: \(strData)")
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as NSDictionary

Expand All @@ -118,7 +121,7 @@ class ImgurClient: NSObject {
else {
if let access = json["access_token"] as NSString? {
self.setAccessToken(access)
NSLog("Succes: \(access)")
//NSLog("Succes: \(access)")
}
}
})
Expand All @@ -134,7 +137,7 @@ class ImgurClient: NSObject {

request.HTTPMethod = "POST"

NSLog("Refresh token (\(refreshToken))")
//NSLog("Refresh token (\(refreshToken))")

let params = ["client_id":imgurClientId, "client_secret":imgurClientSecret, "grant_type":"refresh_token", "refresh_token":self.refreshToken!] as Dictionary

Expand All @@ -144,7 +147,7 @@ class ImgurClient: NSObject {
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
//NSLog("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
NSLog("Body: \(strData)")
//NSLog("Body: \(strData)")
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as NSDictionary

Expand All @@ -155,7 +158,7 @@ class ImgurClient: NSObject {
if let access = json["access_token"] as NSString? {
self.setAccessToken(access)
closure()
NSLog("Succes: \(access)")
//NSLog("Succes: \(access)")
}
}
})
Expand All @@ -167,7 +170,7 @@ class ImgurClient: NSObject {
func isAccessTokenStillValid() -> Bool {

if self.accessToken == nil {
NSLog("Token was nil")
//NSLog("Token was nil")
return false
}

Expand All @@ -177,13 +180,13 @@ class ImgurClient: NSObject {

if comparison == NSComparisonResult.OrderedDescending {

NSLog("Token is still valid")
//NSLog("Token is still valid")

return true

} else {

NSLog("Token is no longer valid")
//NSLog("Token is no longer valid")

return false

Expand All @@ -205,6 +208,12 @@ class ImgurClient: NSObject {
NSUserDefaults.standardUserDefaults().setValue(token, forKey: REFRESH_TOKEN_CONSTANT)
}

func setDeleteScreenshotAfterUpload(delete: Bool!){
self.deleteScreenshotAfterUpload = delete

NSUserDefaults.standardUserDefaults().setBool(delete, forKey: DELETE_SCREENSHOT_AFTER_UPLOAD_CONSTANT)
}

func saveUserName(username: NSString?){
self.username = username
NSUserDefaults.standardUserDefaults().setValue(username, forKey: USERNAME_CONSTANT)
Expand Down
2 changes: 1 addition & 1 deletion mac2imgur/ImgurUploadDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
//

protocol ImgurUploadDelegate {
func uploadAttemptCompleted(successful: Bool, link: String) -> ()
func uploadAttemptCompleted(successful: Bool, link: String, pathToImage: String) -> ()
}
44 changes: 41 additions & 3 deletions mac2imgur/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class PreferencesWindowController : NSWindowController {
@IBOutlet weak var pinCodeField: NSTextField!
@IBOutlet weak var saveButton: NSButton!
@IBOutlet weak var accountLabel: NSTextField!
@IBOutlet weak var deleteScreenshotAfterUploadButton: NSButton!


override init() {

Expand All @@ -35,19 +37,31 @@ class PreferencesWindowController : NSWindowController {

}


required init(coder aDecoder: NSCoder!){

super.init(coder: aDecoder)

}


override func showWindow(sender: AnyObject!) {

super.showWindow(sender)

if imgurSession.isUserLoggedIn! {

setWindowForLoggedUser(imgurSession.username!)

}

if imgurSession.deleteScreenshotAfterUpload! {
deleteScreenshotAfterUploadButton.state = NSOnState
}

}


/*
* Disables the "Sign in" button, as the user won't be signing in again
* Enables the PIN Code field and the Save button
Expand All @@ -56,18 +70,37 @@ class PreferencesWindowController : NSWindowController {
@IBAction func signInButtonClick(sender: AnyObject) {

if imgurSession.isUserLoggedIn == true {

imgurSession.deleteCredentials()

} else {

signInButton!.enabled = false
pinCodeField!.enabled = true

saveButton!.enabled = true
imgurSession.openBrowserForAuth()
}


}

}


@IBAction func onDeleteScreenshotAfterUploadButtonPress(sender: AnyObject) {

if deleteScreenshotAfterUploadButton.state == NSOnState {

imgurSession.setDeleteScreenshotAfterUpload(true)

} else {

imgurSession.setDeleteScreenshotAfterUpload(false)

}

}


/*
* Loads the text written at the PIN Code field, and starts
* the authentication process
Expand All @@ -91,24 +124,29 @@ class PreferencesWindowController : NSWindowController {

}


func setWindowForLoggedUser(username: NSString){

self.signInButton!.title = "Sign out"
self.signInButton!.enabled = true

self.saveButton!.enabled = false

let labelMessage = "Logged in as \(username)"

self.accountLabel!.stringValue = labelMessage
self.accountLabel!.hidden = false

}

func setWindowForAnonymousUser(){

self.signInButton!.title = "Sign in"
self.signInButton!.enabled = true

self.pinCodeField!.enabled = false
self.saveButton!.enabled = false
self.accountLabel!.hidden = true

}

}
16 changes: 14 additions & 2 deletions mac2imgur/PreferencesWindowController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<customObject id="-2" userLabel="File's Owner" customClass="PreferencesWindowController" customModule="mac2imgur" customModuleProvider="target">
<connections>
<outlet property="accountLabel" destination="eE8-35-I4D" id="IrV-kr-tQD"/>
<outlet property="deleteScreenshotAfterUploadButton" destination="7HP-T3-upD" id="p6W-TJ-I8t"/>
<outlet property="pinCodeField" destination="39S-hc-U6b" id="oQV-qS-HEK"/>
<outlet property="saveButton" destination="pX9-ik-K1x" id="sWt-cf-MP3"/>
<outlet property="signInButton" destination="ZAf-d1-jN7" id="tsj-h7-dUL"/>
Expand All @@ -28,12 +29,12 @@
<font key="font" metaFont="system"/>
<tabViewItems>
<tabViewItem label="General" identifier="1" id="RSC-PG-McI">
<view key="view" id="ecH-Wg-cnj">
<view key="view" ambiguous="YES" id="ecH-Wg-cnj">
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</view>
</tabViewItem>
<tabViewItem label="Account" identifier="2" id="k9K-VK-YNr">
<view key="view" ambiguous="YES" id="Twc-0L-sC5">
<view key="view" id="Twc-0L-sC5">
<rect key="frame" x="10" y="33" width="434" height="208"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -110,8 +111,19 @@
</tabViewItem>
</tabViewItems>
</tabView>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="7HP-T3-upD">
<rect key="frame" x="49" y="190" width="215" height="18"/>
<buttonCell key="cell" type="check" title="Delete screenshot after upload" bezelStyle="regularSquare" imagePosition="left" inset="2" id="ntQ-kO-9c8">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onDeleteScreenshotAfterUploadButtonPress:" target="-2" id="J8h-ym-rhS"/>
</connections>
</button>
</subviews>
</view>
<point key="canvasLocation" x="-76" y="110"/>
</window>
</objects>
<resources>
Expand Down
Loading