Skip to content

Commit

Permalink
added upload history; ability to copy and delete past uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
moshee committed Mar 30, 2014
1 parent b5513c7 commit d0cca8d
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 61 deletions.
6 changes: 6 additions & 0 deletions Airlift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
4E90AEE218E697E700A50247 /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4E90AEE118E697E700A50247 /* Sparkle.framework */; };
4E90AEE518E697FC00A50247 /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4E90AEE118E697E700A50247 /* Sparkle.framework */; };
4E90AEEB18E7CFE100A50247 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = 4E90AEEA18E7CFE100A50247 /* dsa_pub.pem */; };
4E90AEF318E7EB4A00A50247 /* ALUploadHistoryItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E90AEF218E7EB4A00A50247 /* ALUploadHistoryItem.m */; };
4EF567EA18BA8C5F0060A66A /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4EF567E918BA8C5F0060A66A /* Cocoa.framework */; };
4EF567F418BA8C5F0060A66A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4EF567F218BA8C5F0060A66A /* InfoPlist.strings */; };
4EF567F618BA8C5F0060A66A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4EF567F518BA8C5F0060A66A /* main.m */; };
Expand Down Expand Up @@ -40,6 +41,8 @@
/* Begin PBXFileReference section */
4E90AEE118E697E700A50247 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Sparkle.framework; path = Airlift/Sparkle.framework; sourceTree = "<group>"; };
4E90AEEA18E7CFE100A50247 /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = "<group>"; };
4E90AEF118E7EB4A00A50247 /* ALUploadHistoryItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ALUploadHistoryItem.h; sourceTree = "<group>"; };
4E90AEF218E7EB4A00A50247 /* ALUploadHistoryItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ALUploadHistoryItem.m; sourceTree = "<group>"; };
4EF567E618BA8C5F0060A66A /* Airlift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Airlift.app; sourceTree = BUILT_PRODUCTS_DIR; };
4EF567E918BA8C5F0060A66A /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
4EF567EC18BA8C5F0060A66A /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -130,6 +133,8 @@
4EF5683118BB03B30060A66A /* ALDropZoneView.m */,
4EF60A9918E3751B003AB75F /* ALUploadManager.h */,
4EF60A9A18E3751B003AB75F /* ALUploadManager.m */,
4E90AEF118E7EB4A00A50247 /* ALUploadHistoryItem.h */,
4E90AEF218E7EB4A00A50247 /* ALUploadHistoryItem.m */,
4EF5680118BA8C5F0060A66A /* Images.xcassets */,
4EF5682918BADF3D0060A66A /* Resources */,
4EF567F018BA8C5F0060A66A /* Supporting Files */,
Expand Down Expand Up @@ -261,6 +266,7 @@
buildActionMask = 2147483647;
files = (
4EF567F618BA8C5F0060A66A /* main.m in Sources */,
4E90AEF318E7EB4A00A50247 /* ALUploadHistoryItem.m in Sources */,
4EF567FD18BA8C5F0060A66A /* ALAppDelegate.m in Sources */,
4EF60A9B18E3751B003AB75F /* ALUploadManager.m in Sources */,
4EF5682518BA94000060A66A /* ALPreferenceViewController.m in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions Airlift/ALAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "ALDropZoneView.h"
#import "ALUploadManager.h"
#import "ALPreferenceViewController.h"
#import "ALUploadHistoryItem.h"
#import "Sparkle.framework/Headers/Sparkle.h"

@interface ALAppDelegate
Expand All @@ -28,6 +29,8 @@
title:(NSString*)title
subtitle:(NSString*)subtitle
additionalInfo:(NSDictionary*)info;
- (void)addUploadToHistory:(ALUploadHistoryItem*)upload;
- (void)removeUploadFromHistory:(ALUploadHistoryItem*)upload;

enum HotkeyAction { HotkeyTakeScreenshot = 0, HotkeyTakeFullScreenshot };

Expand Down
29 changes: 27 additions & 2 deletions Airlift/ALAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

@interface ALAppDelegate () {
NSMenu* menu;
NSMutableArray* uploadHistory;
}

@end

@implementation ALAppDelegate

@synthesize dropZone = _dropZone;
const NSUInteger MAX_UPLOAD_HISTORY = 10;

@synthesize dropZone;

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
menu = [[NSMenu alloc] init];
Expand All @@ -30,7 +33,7 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
[menu addItem:[NSMenuItem separatorItem]];
[menu addItemWithTitle:@"Quit" action:@selector(quit:) keyEquivalent:@""];

_dropZone = [[ALDropZoneView alloc] initWithMenu:menu];
dropZone = [[ALDropZoneView alloc] initWithMenu:menu];

[_window setContentSize:[[prefs view] frame].size];
[_window setContentView:[prefs view]];
Expand Down Expand Up @@ -94,6 +97,28 @@ - (void)showNotificationOfType:(ALNotificationType)notificationType
deliverNotification:notification];
}

- (void)addUploadToHistory:(ALUploadHistoryItem*)historyItem {
if (uploadHistory == nil) {
uploadHistory = [NSMutableArray array];
}
if ([uploadHistory count] >= MAX_UPLOAD_HISTORY) {
[uploadHistory removeLastObject];
}

[historyItem copyLink];
[uploadHistory insertObject:historyItem atIndex:0];
[dropZone setHistoryItems:uploadHistory];
}

- (void)removeUploadFromHistory:(ALUploadHistoryItem*)historyItem {
[uploadHistory removeObject:historyItem];
[dropZone setHistoryItems:uploadHistory];
}

- (NSArray*)uploadHistory {
return (NSArray*)uploadHistory;
}

#pragma mark - NSUserNotificationCenterDelegate

- (void)userNotificationCenter:(NSUserNotificationCenter*)center
Expand Down
2 changes: 2 additions & 0 deletions Airlift/ALDropZoneView.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import <Cocoa/Cocoa.h>
#import "ALUploadManager.h"
#import "ALUploadHistoryItem.h"

@interface ALDropZoneView : NSView <NSMenuDelegate, NSDraggingDestination>

Expand All @@ -14,6 +15,7 @@
- (void)addStatus:(ALDropZoneStatus)status;
- (BOOL)hasStatus:(ALDropZoneStatus)status;
- (void)removeStatus:(ALDropZoneStatus)status;
- (void)setHistoryItems:(NSArray*)historyItems;

- (void)setProgress:(CGFloat)progress;

Expand Down
53 changes: 52 additions & 1 deletion Airlift/ALDropZoneView.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ @interface ALDropZoneView () {
NSMenuItem* progressMenuItem;
NSMenuItem* cancelUploadMenuItem;
NSMenuItem* oopsMenuItem;
NSMenu* uploadHistoryMenu;
}

@end
Expand All @@ -25,6 +26,8 @@ @implementation ALDropZoneView
static NSImage* StatusIconSelected;
static NSImage* StatusIconUploading;

static NSMenuItem* emptyUploadHistoryItem;

- (id)initWithMenu:(NSMenu*)menu {
NSStatusItem* item =
[[NSStatusBar systemStatusBar] statusItemWithLength:28.0];
Expand All @@ -40,6 +43,12 @@ - (id)initWithMenu:(NSMenu*)menu {
StatusIconUploading = [NSImage imageNamed:@"StatusIconUploading"];
}

if (emptyUploadHistoryItem == nil) {
emptyUploadHistoryItem = [NSMenuItem new];
[emptyUploadHistoryItem setTitle:@"(No uploads)"];
[emptyUploadHistoryItem setEnabled:NO];
}

if (self) {
progressMenuItem = [NSMenuItem new];
[progressMenuItem setTitle:@"Uploading..."];
Expand All @@ -63,7 +72,16 @@ - (id)initWithMenu:(NSMenu*)menu {
@"delete the last file uploaded."];

[menu insertItem:oopsMenuItem atIndex:2];
[menu insertItem:[NSMenuItem separatorItem] atIndex:3];

NSMenuItem* uploadHistoryMenuItem = [NSMenuItem new];
[uploadHistoryMenuItem setTitle:@"Past uploads"];

uploadHistoryMenu = [NSMenu new];
[self setHistoryItems:nil];
[uploadHistoryMenuItem setSubmenu:uploadHistoryMenu];
[menu insertItem:uploadHistoryMenuItem atIndex:3];

[menu insertItem:[NSMenuItem separatorItem] atIndex:4];

statusItem = item;
[statusItem setView:self];
Expand Down Expand Up @@ -186,6 +204,39 @@ - (void)setProgress:(CGFloat)_progress {
}
}

- (void)setHistoryItems:(NSArray*)historyItems {
[uploadHistoryMenu removeAllItems];
if (historyItems == nil || [historyItems count] == 0) {
[uploadHistoryMenu addItem:emptyUploadHistoryItem];
return;
}

for (ALUploadHistoryItem* historyItem in historyItems) {
NSMenuItem* menuItem = [NSMenuItem new];
NSString* title = [[historyItem filePath] lastPathComponent];
[menuItem setTitle:title];

NSMenuItem* copyItem =
[[NSMenuItem alloc] initWithTitle:@"Copy link"
action:@selector(copyLink)
keyEquivalent:@""];
[copyItem setTarget:historyItem];

NSMenuItem* deleteItem =
[[NSMenuItem alloc] initWithTitle:@"Delete upload"
action:@selector(deleteUpload)
keyEquivalent:@""];
[deleteItem setTarget:historyItem];

NSMenu* submenu = [NSMenu new];
[submenu addItem:copyItem];
[submenu addItem:deleteItem];
[menuItem setSubmenu:submenu];

[uploadHistoryMenu addItem:menuItem];
}
}

#pragma mark - Menu actions

- (void)cancelUpload:(id)sender {
Expand Down
19 changes: 19 additions & 0 deletions Airlift/ALUploadHistoryItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 display: none;. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import <Foundation/Foundation.h>

@interface ALUploadHistoryItem : NSObject

// The originally returned URL, with no scheme or trailing extension.
@property (retain) NSString* URL;
// The local path of the file that was uploaded.
@property (retain) NSURL* filePath;
// The URL that the upload request was originally sent to.
@property (retain) NSURL* originalURL;

- (void)deleteUpload;
- (void)copyLink;

@end
76 changes: 76 additions & 0 deletions Airlift/ALUploadHistoryItem.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2014 display: none;. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import "ALUploadHistoryItem.h"
#import "ALAppDelegate.h"
#import "ALUploadManager.h"

@implementation ALUploadHistoryItem

@synthesize URL, filePath, originalURL;

- (void)deleteUpload {
[ALUploadManager deleteUploadAtURL:URL];
[[ALAppDelegate sharedAppDelegate] removeUploadFromHistory:self];
}

- (void)copyLink {
NSString* url = URL;

if ([[NSUserDefaults standardUserDefaults]
boolForKey:@"appendExtensions"]) {
NSString* ext = [[filePath path] pathExtension];
url = [url stringByAppendingPathExtension:ext];
}

NSString* linkableURL =
[NSString stringWithFormat:@"%@://%@", [originalURL scheme], url];

NSString* errMsg = [self copyString:linkableURL];
if (errMsg != nil) {
NSString* subtitle =
[NSString stringWithFormat:@"The upload worked, but couldn't copy "
@"the URL to clipboard: %@",
errMsg];
[[ALAppDelegate sharedAppDelegate]
showNotificationOfType:ALNotificationUploadAborted
title:[NSString
stringWithFormat:@"Error copying %@", linkableURL]
subtitle:subtitle
additionalInfo:nil];
} else {
NSDictionary* info =
[NSDictionary dictionaryWithObject:linkableURL forKey:@"url"];
[[ALAppDelegate sharedAppDelegate]
showNotificationOfType:ALNotificationURLCopied
title:linkableURL
subtitle:@"URL copied to clipboard"
additionalInfo:info];
}
}

- (NSString*)copyString:(NSString*)str {
NSPasteboard* pboard = [NSPasteboard generalPasteboard];

if (pboard == nil) {
return @"Failed to get handle on pasteboard";
}

[pboard declareTypes:@[NSPasteboardTypeString] owner:nil];
[pboard clearContents];

NSString* msg;

@try {
if (![pboard writeObjects:[NSArray arrayWithObject:str]]) {
msg = @"Pasteboard ownership changed";
}
}
@catch (NSException* e) {
msg = [NSString stringWithFormat:@"%@: %@", e.name, e.reason];
}
return msg;
}

@end
1 change: 1 addition & 0 deletions Airlift/ALUploadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#import <Foundation/Foundation.h>
#import "ALPreferenceViewController.h"
#import "ALUploadHistoryItem.h"

@interface ALUploadManager
: NSObject <NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
Expand Down
Loading

0 comments on commit d0cca8d

Please sign in to comment.