Skip to content

Commit

Permalink
Extended URL Shortener V1: Closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Somerset committed Oct 13, 2015
1 parent bd64c04 commit 7586690
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 12 deletions.
69 changes: 66 additions & 3 deletions FileShuttle/MainMenu.xib
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -925,18 +925,78 @@ CQ
<point key="canvasLocation" x="657.5" y="-116"/> <point key="canvasLocation" x="657.5" y="-116"/>
</view> </view>
<view id="zxt-BZ-cJf"> <view id="zxt-BZ-cJf">
<rect key="frame" x="0.0" y="0.0" width="343" height="338"/> <rect key="frame" x="0.0" y="0.0" width="343" height="105"/>
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>
<subviews> <subviews>
<popUpButton verticalHuggingPriority="750" id="8bW-nh-6NB">
<rect key="frame" x="123" y="39" width="170" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<animations/>
<popUpButtonCell key="cell" type="push" title="is.gd" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="EVk-EC-1CH" id="Msl-Oz-Tzv">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="Z6x-i1-zaI">
<items>
<menuItem title="is.gd" state="on" id="EVk-EC-1CH"/>
<menuItem title="v.gd" tag="1" id="dLF-PM-d0p"/>
<menuItem title="bit.ly" tag="2" id="Fcq-EN-785"/>
<menuItem title="j.mp" tag="3" id="KiW-Us-fRA"/>
</items>
</menu>
</popUpButtonCell>
<connections>
<action selector="shortenerSelectionPopUpButtonChanged:" target="861" id="ydh-Nu-9gv"/>
<binding destination="606" name="selectedTag" keyPath="values.selected_shortener" id="xc8-cA-RLh">
<dictionary key="options">
<bool key="NSValidatesImmediately" value="YES"/>
</dictionary>
</binding>
</connections>
</popUpButton>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="ukZ-IA-G1e">
<rect key="frame" x="54" y="45" width="69" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<animations/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Shortener:" id="7n7-Qa-gaB">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="4qB-6k-HbP">
<rect key="frame" x="54" y="20" width="54" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<animations/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="API Key:" id="gIP-po-RUF">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="DRB-h3-Ai5">
<rect key="frame" x="125" y="15" width="165" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<animations/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="eWb-Qk-7nz">
<font key="font" metaFont="system"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="shortenerApiTokenChanged:" target="861" id="zHZ-lv-cVR"/>
<binding destination="606" name="value" keyPath="values.api_shortener_token" id="ATX-Jd-XKy"/>
</connections>
</textField>
<button id="1016"> <button id="1016">
<rect key="frame" x="54" y="302" width="234" height="18"/> <rect key="frame" x="123" y="69" width="234" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<animations/> <animations/>
<buttonCell key="cell" type="check" title="Shorten URLs" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="1035"> <buttonCell key="cell" type="check" title="Shorten URLs" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="1035">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/> <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
</buttonCell> </buttonCell>
<connections> <connections>
<action selector="enableShortenerCheckboxChanged:" target="861" id="h3I-Dm-PcA"/>
<binding destination="606" name="value" keyPath="values.url_shortener" id="1038"> <binding destination="606" name="value" keyPath="values.url_shortener" id="1038">
<dictionary key="options"> <dictionary key="options">
<integer key="NSMultipleValuesPlaceholder" value="1"/> <integer key="NSMultipleValuesPlaceholder" value="1"/>
Expand All @@ -949,17 +1009,20 @@ CQ
</button> </button>
</subviews> </subviews>
<animations/> <animations/>
<point key="canvasLocation" x="248.5" y="-120"/> <point key="canvasLocation" x="248.5" y="-236.5"/>
</view> </view>
<customObject id="861" customClass="MVPreferencesController"> <customObject id="861" customClass="MVPreferencesController">
<connections> <connections>
<outlet property="changePermissionsCheckbox" destination="acV-LA-g0x" id="WQI-hd-39D"/> <outlet property="changePermissionsCheckbox" destination="acV-LA-g0x" id="WQI-hd-39D"/>
<outlet property="clipboardRecorderControl" destination="1062" id="1067"/> <outlet property="clipboardRecorderControl" destination="1062" id="1067"/>
<outlet property="connectionView" destination="1135" id="1134"/> <outlet property="connectionView" destination="1135" id="1134"/>
<outlet property="enableShortenerCheckbox" destination="1016" id="AFp-fb-Yjl"/>
<outlet property="generalView" destination="1070" id="1180"/> <outlet property="generalView" destination="1070" id="1180"/>
<outlet property="passwordTextField" destination="1139" id="1181"/> <outlet property="passwordTextField" destination="1139" id="1181"/>
<outlet property="permissionsTextField" destination="ga9-B4-SiG" id="Jg3-Jc-Kw5"/> <outlet property="permissionsTextField" destination="ga9-B4-SiG" id="Jg3-Jc-Kw5"/>
<outlet property="setFilenamePopUpButton" destination="1197" id="1204"/> <outlet property="setFilenamePopUpButton" destination="1197" id="1204"/>
<outlet property="shortenerApiTokenField" destination="DRB-h3-Ai5" id="IfA-lh-IDg"/>
<outlet property="shortenerSelectionPopUpButton" destination="8bW-nh-6NB" id="nbp-IE-hH3"/>
<outlet property="showInPopUpButton" destination="1020" id="1042"/> <outlet property="showInPopUpButton" destination="1020" id="1042"/>
<outlet property="toolbar" destination="1071" id="1086"/> <outlet property="toolbar" destination="1071" id="1086"/>
<outlet property="urlshorteningView" destination="zxt-BZ-cJf" id="DWx-jS-opf"/> <outlet property="urlshorteningView" destination="zxt-BZ-cJf" id="DWx-jS-opf"/>
Expand Down
7 changes: 7 additions & 0 deletions MVPreferencesController.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
NSPopUpButton *showInPopUpButton_; NSPopUpButton *showInPopUpButton_;
SRRecorderControl *clipboardRecorderControl_; SRRecorderControl *clipboardRecorderControl_;
NSPopUpButton *setFilenamePopUpButton; NSPopUpButton *setFilenamePopUpButton;
NSPopUpButton *shortenerSelectionPopUpButton;
} }


@property (assign) IBOutlet NSWindow *window; @property (assign) IBOutlet NSWindow *window;
Expand All @@ -32,12 +33,18 @@
@property (assign) IBOutlet NSButton *changePermissionsCheckbox; @property (assign) IBOutlet NSButton *changePermissionsCheckbox;
@property (assign) IBOutlet NSTextField *permissionsTextField; @property (assign) IBOutlet NSTextField *permissionsTextField;
@property (assign) IBOutlet SRRecorderControl *clipboardRecorderControl; @property (assign) IBOutlet SRRecorderControl *clipboardRecorderControl;
@property (assign) IBOutlet NSButton *enableShortenerCheckbox;
@property (assign) IBOutlet NSPopUpButton *shortenerSelectionPopUpButton;
@property (assign) IBOutlet NSTextField *shortenerApiTokenField;


- (IBAction)toolbarItemAction:(id)sender; - (IBAction)toolbarItemAction:(id)sender;
- (IBAction)protocolChanged:(id)sender; - (IBAction)protocolChanged:(id)sender;
- (IBAction)passwordChanged:(id)sender; - (IBAction)passwordChanged:(id)sender;
- (IBAction)showInPopUpButtonChanged:(id)sender; - (IBAction)showInPopUpButtonChanged:(id)sender;
- (IBAction)setFilenamePopUpButtonChanged:(id)sender; - (IBAction)setFilenamePopUpButtonChanged:(id)sender;
- (IBAction)changePermissionsCheckboxChanged:(id)sender; - (IBAction)changePermissionsCheckboxChanged:(id)sender;
- (IBAction)shortenerSelectionPopUpButtonChanged:(id)sender;
- (IBAction)shortenerApiTokenChanged:(id)sender;
- (IBAction)enableShortenerCheckboxChanged:(id)sender;


@end @end
59 changes: 59 additions & 0 deletions MVPreferencesController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ @implementation MVPreferencesController
urlshorteningView = urlshorteningView_, urlshorteningView = urlshorteningView_,
connectionView = connectionView_, connectionView = connectionView_,
showInPopUpButton = showInPopUpButton_, showInPopUpButton = showInPopUpButton_,
shortenerSelectionPopUpButton = shortenerSelectionPopUpButton_,
shortenerApiTokenField = shortenerApiTokenField_,
setFilenamePopUpButton = setFilenamePopUpButton_, setFilenamePopUpButton = setFilenamePopUpButton_,
clipboardRecorderControl = clipboardRecorderControl_, clipboardRecorderControl = clipboardRecorderControl_,
selectedIdentifier = selectedIdentifier_, selectedIdentifier = selectedIdentifier_,
Expand Down Expand Up @@ -122,6 +124,27 @@ - (void)awakeFromNib
} }
else else
[self.setFilenamePopUpButton selectItemWithTag:0]; [self.setFilenamePopUpButton selectItemWithTag:0];

if([defaults boolForKey:@"url_shortener"]) {
[self.shortenerSelectionPopUpButton setEnabled:YES];
} else {
[self.shortenerSelectionPopUpButton setEnabled:NO];
}

if([defaults integerForKey:@"selected_shortener"]) {
[self.shortenerSelectionPopUpButton selectItemWithTag:[defaults integerForKey:@"selected_shortener"]];
NSInteger tag = [self.shortenerSelectionPopUpButton selectedTag];
// If shortener selected is bit.ly or j.mp ask for API key.
if(tag == 2 || tag == 3) {
[shortenerApiTokenField_ setEnabled:YES];
} else {
[shortenerApiTokenField_ setEnabled:NO];
}
}
if([defaults objectForKey:@"api_shortener_token"]) {
[self.shortenerApiTokenField setStringValue:[defaults objectForKey:@"api_shortener_token"]];
[self.shortenerApiTokenField setEnabled:YES];
}


NSString *protocol = [defaults valueForKey:@"protocol"]; NSString *protocol = [defaults valueForKey:@"protocol"];
if([protocol compare:@"SFTP"] == NSOrderedSame || [protocol compare:@"SCP"] == NSOrderedSame) { if([protocol compare:@"SFTP"] == NSOrderedSame || [protocol compare:@"SCP"] == NSOrderedSame) {
Expand Down Expand Up @@ -240,7 +263,43 @@ - (IBAction)setFilenamePopUpButtonChanged:(id)sender {
[defaults setBool:YES forKey:@"use_hash"]; [defaults setBool:YES forKey:@"use_hash"];
} }
} }
///////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)shortenerSelectionPopUpButtonChanged:(id)sender {
NSInteger tag = [self.shortenerSelectionPopUpButton selectedTag];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:tag forKey:@"selected_shortener"];
// If shortener selected is bit.ly or j.mp ask for API key.
if(tag == 2 || tag == 3) {
[shortenerApiTokenField_ setEnabled:YES];
} else {
[shortenerApiTokenField_ setEnabled:NO];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveApiKey {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *key = [shortenerApiTokenField_ stringValue];
[defaults setObject:key forKey:@"api_shortener_token"];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)shortenerApiTokenChanged:(id)sender {
[self saveApiKey];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)enableShortenerCheckboxChanged:(id)sender
{
NSButton *checkbox = sender;
BOOL enableShortener = checkbox.state == NSOnState;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:enableShortener forKey:@"url_shortener"];

self.shortenerSelectionPopUpButton.enabled = enableShortener;


if (enableShortener) {
[self.shortenerSelectionPopUpButton becomeFirstResponder];
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)changePermissionsCheckboxChanged:(id)sender - (IBAction)changePermissionsCheckboxChanged:(id)sender
{ {
Expand Down
41 changes: 32 additions & 9 deletions MVURLShortener.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,17 +14,40 @@ @implementation MVURLShortener


/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)shortenURL:(NSString*)url { - (NSString*)shortenURL:(NSString*)url {
NSString *shttleUrl = [NSString stringWithFormat: NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
@"http://is.gd/create.php?format=simple&url=%@",url]; NSInteger shortenerid = [defaults integerForKey:@"selected_shortener"];
NSString *doc = [NSString stringWithContentsOfURL:[NSURL URLWithString:shttleUrl] NSString *apikey = @"";
encoding:NSUTF8StringEncoding error:nil]; NSString *shortenerUrl = @"";
if(doc == nil) return nil;

if([defaults objectForKey:@"api_shortener_token"]) {
if([doc isEqual: @"Error: Please enter a valid URL to shorten"]) { apikey = [defaults objectForKey:@"api_shortener_token"];
}

// is.gd
if(shortenerid == 0) {
shortenerUrl = [NSString stringWithFormat:
@"http://is.gd/create.php?format=simple&url=%@",url];
}
// v.gd
else if(shortenerid == 1) {
shortenerUrl = [NSString stringWithFormat:@"http://v.gd/create.php?format=simple&url=%@",url];
}
// bit.ly
else if(shortenerid == 2) {
shortenerUrl = [NSString stringWithFormat:@"https://api-ssl.bitly.com/v3/shorten?access_token=%@&format=txt&longUrl=%@", apikey, url];
}
else if(shortenerid == 3) {
shortenerUrl = [NSString stringWithFormat:@"https://api-ssl.bitly.com/v3/shorten?access_token=%@&domain=j.mp&format=txt&longUrl=%@", apikey, url];
}

NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:shortenerUrl]
encoding:NSUTF8StringEncoding error:nil];
if(response == nil) return nil;
if([response isEqual: @"Error: Please enter a valid URL to shorten"] || [response isEqual: @"INVALID_URI"] || [response isEqual: @"MISSING_ARG_ACCESS_TOKEN"]) {
return nil; return nil;
} else { } else {
return doc; return response;
} }
} }


@end @end

0 comments on commit 7586690

Please sign in to comment.