Skip to content

Commit

Permalink
NALLY-9 Add a blocking window let TinyURL and Imgur block user input …
Browse files Browse the repository at this point in the history
…when generating url

Signed-off-by: Killercat <[email protected]>
  • Loading branch information
Rayer committed Mar 16, 2019
1 parent be8d7dc commit 5f9faca
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ language: objective-c
osx_image: xcode10
script:
- xcodebuild -workspace Nally.xcworkspace -list
- xcodebuild -workspace Nally.xcworkspace -scheme 'Nally-Release'
- xcodebuild -workspace Nally.xcworkspace -scheme Nally
3 changes: 3 additions & 0 deletions Code/YLContextualMenuManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
{
NSArray *_urlsToOpen;
}

@property bool anotherPasteStillOnGoing;

+ (YLContextualMenuManager *) sharedInstance ;
- (id) init ;
- (NSArray *) availableMenuItemForSelectionString: (NSString *)selectedString;
Expand Down
75 changes: 53 additions & 22 deletions Code/YLContextualMenuManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ - (NSString *) _extractShortURLFromString: (NSString *)s
NSMutableString *result = [NSMutableString string];
int i;
for (i = 0; i < [s length]; i++) {
unichar c = [s characterAtIndex: i];
unichar c = [s characterAtIndex:(NSUInteger) i];
if (c >= '!' && c <= '~')
[result appendString: [NSString stringWithCharacters: &c length: 1]];
}
Expand All @@ -47,17 +47,13 @@ @implementation NSString (UJStringUrlCategory)
- (BOOL) UJ_isUrlLike
{
NSURL *url = [NSURL URLWithString:self];
if (url && url.scheme && url.host) {
return YES;
}
return NO;
return url && url.scheme && url.host;
}


- (NSString *) UJ_protocolPrefixAppendedUrlString
{
NSArray *protocols = [NSArray arrayWithObjects:@"http://", @"https://", @"ftp://", @"telnet://",
@"bbs://", @"ssh://", @"mailto:", nil];
NSArray *protocols = @[@"http://", @"https://", @"ftp://", @"telnet://",
@"bbs://", @"ssh://", @"mailto:"];
for (NSString *p in protocols)
{
if ([self hasPrefix:p])
Expand All @@ -71,18 +67,24 @@ - (NSString *) UJ_protocolPrefixAppendedUrlString

@implementation YLContextualMenuManager

+ (YLContextualMenuManager *) sharedInstance

@synthesize anotherPasteStillOnGoing = _anotherPasteStillOnGoing;

+ (YLContextualMenuManager *)sharedInstance
{
return gSharedInstance ?: [[[YLContextualMenuManager alloc] init] autorelease];
}

- (id) init
{

[self setAnotherPasteStillOnGoing:NO];
if (gSharedInstance) {
[self release];
} else if ((gSharedInstance = [[super init] retain])) {
} else if ((gSharedInstance = (YLContextualMenuManager *) [[super init] retain])) {
// ...
}

return gSharedInstance;
}

Expand Down Expand Up @@ -117,7 +119,7 @@ - (NSArray *) availableMenuItemForSelectionString: (NSString *)selectedString
if ([urls count] > 1)
title = NSLocalizedString(@"Open mutiple URLs", @"Open mutiple URLs");
else if ([urls count] == 1)
title = [urls objectAtIndex: 0];
title = urls[0];

if (_urlsToOpen)
[_urlsToOpen release];
Expand Down Expand Up @@ -160,9 +162,9 @@ - (NSArray *) availableMenuItemForSelectionString: (NSString *)selectedString
} else if (^(NSArray* searchImageSuffix) {
for(NSString* suffix in searchImageSuffix) {
if([shortURL hasSuffix:suffix])
return TRUE;
return YES;
}
return FALSE;
return NO;
}(searchImageSuffix))
{
addShortenedURLMenuItem(@"Image search by GOOGLE", [@"https://www.google.com/searchbyimage?&image_url=" stringByAppendingString: shortURL]);
Expand Down Expand Up @@ -195,12 +197,19 @@ - (NSArray *) availableMenuItemForSelectionString: (NSString *)selectedString
} else {
// selectedString length == 0
//NSPasteboardTypeUrl is supported after 10.13, so use String to compitiable 10.12

if ([self anotherPasteStillOnGoing]) {
item = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Unable to paste now...", @"Unable to paste now...") action:@selector(nilSymbol) keyEquivalent:@""] autorelease];
[item setEnabled:NO];
[items addObject:item];
return items;
}

NSPasteboard* pasteBoard = [NSPasteboard generalPasteboard];
NSString* clippedString = [pasteBoard stringForType:NSPasteboardTypeString];

if([clippedString UJ_isUrlLike]) {
NSLog(@"%@ is a valid URL", clippedString);
item = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Paste tinyurl", @"Menu") action: @selector(tinyurl:) keyEquivalent: @""] autorelease];
item = [[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Paste TinyURL", @"Menu") action:@selector(tinyurl:) keyEquivalent:@""] autorelease];
[item setTarget: self];
[item setRepresentedObject:clippedString];
[items addObject:item];
Expand Down Expand Up @@ -252,17 +261,24 @@ - (IBAction) lookupDictionary: (id)sender
{
NSString *u = [sender representedObject];
NSPasteboard *spb = [NSPasteboard pasteboardWithUniqueName];
[spb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: self];
[spb declareTypes:@[NSStringPboardType] owner:self];
[spb setString: u forType: NSStringPboardType];
NSPerformService(@"Look Up in Dictionary", spb);

}

- (IBAction) tinyurl: (id)sender
{
[self setAnotherPasteStillOnGoing:YES];
NSAlert *alert = [NSAlert new];
[alert setMessageText:NSLocalizedString(@"Converting URL to TinyURL...", @"Converting URL to TinyURL...")];
[alert setInformativeText:NSLocalizedString(@"It may take several seconds, please wait", @"It may take several seconds, please wait")];
[alert addButtonWithTitle:@""];
[alert setAlertStyle:NSAlertStyleInformational];
[alert beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];
NSString *u = [sender representedObject];
NSString* APIRequestString = [@"http://tinyurl.com/api-create.php?url=" stringByAppendingString:u];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:APIRequestString]];
[request setHTTPMethod:@"GET"];
NSError *error = nil;
Expand All @@ -274,23 +290,38 @@ - (IBAction) tinyurl: (id)sender
NSLog(@"Error getting %@, HTTP status code %li", APIRequestString, (long)[responseCode statusCode]);
return;
}

NSString* tinyurlResult = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

YLController *controller = [NSApp delegate];

//[[NSApp mainWindow] endSheet:alert.window];

NSString *tinyurlResult = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];

YLController *controller = (id) [NSApp delegate];
[[controller telnetView] insertText:tinyurlResult];
[[NSApp mainWindow] endSheet:alert.window];
[self setAnotherPasteStillOnGoing:NO];
}

-(IBAction)imgur:(id)sender {
[self setAnotherPasteStillOnGoing:YES];
NSData* data = [sender representedObject];
NSAlert *alert = [NSAlert new];
[alert setMessageText:NSLocalizedString(@"Pasting to imgur...", @"Pasting to imgur...")];
[alert setInformativeText:NSLocalizedString(@"It may take several seconds, please wait", @"It may take several seconds, please wait")];
[alert addButtonWithTitle:@""];
[alert setAlertStyle:NSAlertStyleInformational];
[alert beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];

[[ImgurAnonymousAPIClient sharedClient] uploadImageData:data withFilename:@"nally-uploaded" completionHandler:^(NSURL *imgurURL, NSError *error) {
if(error) {
NSLog(@"Error! %@", error);
}
YLController *controller = [NSApp delegate];
YLController *controller = (id) [NSApp delegate];
[[controller telnetView] insertText:[imgurURL absoluteString]];
[[NSApp mainWindow] endSheet:alert.window];
//[NSApp stopModal];
//It will clear contents so it won't be paste again
[[NSPasteboard generalPasteboard] clearContents];
[self setAnotherPasteStillOnGoing:NO];
}];
}

Expand Down
1 change: 0 additions & 1 deletion Nally.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@
ED20D02CD762BDA0BA20E70E /* Pods-TextSuiteTests.debug.xcconfig */,
6BBF85D09A0AF6BAF42E2DAF /* Pods-TextSuiteTests.release.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Code/YLContextualMenuManager.m"
timestampString = "574166507.330093"
timestampString = "574307394.773394"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "200"
Expand All @@ -19,69 +19,5 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Code/YLContextualMenuManager.m"
timestampString = "574166507.33061"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "277"
endingLineNumber = "277"
landmarkName = "-tinyurl:"
landmarkType = "7">
<Locations>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "-[YLContextualMenuManager imgur:]"
moduleName = "Nally"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/rayer/Develop/nally/Code/YLContextualMenuManager.m"
timestampString = "574139147.4291"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "277"
endingLineNumber = "277"
offsetFromSymbolStart = "41">
</Location>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "__33-[YLContextualMenuManager imgur:]_block_invoke"
moduleName = "Nally"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/rayer/Develop/nally/Code/YLContextualMenuManager.m"
timestampString = "574139147.433387"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "278"
endingLineNumber = "278"
offsetFromSymbolStart = "31">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Code/YLContextualMenuManager.m"
timestampString = "574166507.331396"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "285"
endingLineNumber = "285"
landmarkName = "-imgur:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Binary file modified Resources/English.lproj/Localizable.strings
Binary file not shown.
26 changes: 13 additions & 13 deletions Resources/English.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -872,21 +872,21 @@ Gw
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView autohidesScrollers="YES" horizontalLineScroll="22" horizontalPageScroll="10" verticalLineScroll="22" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="731">
<rect key="frame" x="0.0" y="0.0" width="158" height="299"/>
<rect key="frame" x="0.0" y="0.0" width="160" height="299"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxX="YES" heightSizable="YES"/>
<clipView key="contentView" id="6Jd-le-MDw">
<rect key="frame" x="1" y="1" width="156" height="297"/>
<autoresizingMask key="autoresizingMask"/>
<rect key="frame" x="1" y="1" width="158" height="297"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" alternatingRowBackgroundColors="YES" columnSelection="YES" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" rowHeight="20" id="734">
<rect key="frame" x="0.0" y="0.0" width="156" height="297"/>
<rect key="frame" x="0.0" y="0.0" width="158" height="297"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<tableViewGridLines key="gridStyleMask" horizontal="YES"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn editable="NO" width="153" minWidth="40" maxWidth="1000" id="736">
<tableColumn editable="NO" width="155" minWidth="40" maxWidth="1000" id="736">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
Expand Down Expand Up @@ -920,25 +920,25 @@ Gw
</scroller>
</scrollView>
<box autoresizesSubviews="NO" borderType="none" title="Box" titlePosition="noTitle" id="792">
<rect key="frame" x="167" y="0.0" width="241" height="299"/>
<rect key="frame" x="166" y="-2" width="245" height="305"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" id="1ux-oV-DXh">
<rect key="frame" x="0.0" y="0.0" width="241" height="299"/>
<rect key="frame" x="0.0" y="0.0" width="245" height="305"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="777">
<rect key="frame" x="4" y="1" width="238" height="298"/>
<rect key="frame" x="4" y="1" width="242" height="304"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<clipView key="contentView" drawsBackground="NO" id="00G-fG-OG0">
<rect key="frame" x="1" y="1" width="236" height="296"/>
<autoresizingMask key="autoresizingMask"/>
<rect key="frame" x="1" y="1" width="240" height="302"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView importsGraphics="NO" richText="NO" verticallyResizable="YES" continuousSpellChecking="YES" spellingCorrection="YES" id="780">
<rect key="frame" x="0.0" y="0.0" width="236" height="357"/>
<rect key="frame" x="0.0" y="0.0" width="240" height="340"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="236" height="296"/>
<size key="minSize" width="240" height="302"/>
<size key="maxSize" width="459" height="10000000"/>
<attributedString key="textStorage">
<fragment>
Expand Down Expand Up @@ -1061,7 +1061,7 @@ Gw
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="778">
<rect key="frame" x="221" y="1" width="16" height="296"/>
<rect key="frame" x="225" y="1" width="16" height="302"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
Expand Down
7 changes: 6 additions & 1 deletion Resources/zh_CN.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@
"Fonts" = "字体";

/* Preferences */
"Connection" = "连接";
"Connection" = "连接";
"Paste TinyURL" = "Paste TinyURL";
"It may take several seconds, please wait" = "It may take several seconds, please wait";
"Pasting to imgur..." = "Pasting to imgur...";
"Unable to paste now..." = "Unable to paste now...";
"Converting URL to TinyURL..." = "Converting URL to TinyURL...";
8 changes: 7 additions & 1 deletion Resources/zh_TW.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@
"Fonts" = "字形";

/* Preferences */
"Connection" = "連線";
"Connection" = "連線";

"Paste TinyURL" = "轉換成TinyURL";
"It may take several seconds, please wait" = "這可能會需要幾秒,請稍待";
"Pasting to imgur..." = "貼上imgur.com";
"Unable to paste now..." = "目前無法貼上,其他動作進行中";
"Converting URL to TinyURL..." = "正在轉換網址成TinyURL...";

0 comments on commit 5f9faca

Please sign in to comment.