// // ShrtyPlatform.h // // Copyright (c) 2009, Robert R. Fahrni // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation and/or // other materials provided with the distribution. // 3. Neither the name of the author nor the names of its contributors may be // used to endorse or promote products derived from this software without specific // prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import "ShrtyAppController.h" #import "Shrty.h" #import "ShrtySvcConst.h" // ShrtyAppController Private Methods, @interface ShrtyAppController (Private) - (void)_createShrtyInstanceFromServiceName:(NSString*)serviceName; @end @implementation ShrtyAppController - (id) init { self = [super init]; return self; } // init - (void) applicationDidFinishLaunching:(NSNotification*)notice { // Fill the combo box with services. Yes, yes these strings are hard coded, hey, it's just a sample app. NSArray* urlServices = [NSArray arrayWithObjects:@"bit.ly", @"su.pr", @"j.mp", @"tr.im", @"ping.fm", @"YOURLS", @"tinyarro.ws", @"is.gd", @"u.nu", nil]; [shorteningServices addItemsWithObjectValues:urlServices]; } - (IBAction) shortenIt:(id)sender { // Start the action of shortening a URL, we'll be called back on this object, it's the delegate // for Shrty. NSString* serviceName = [shorteningServices objectValueOfSelectedItem]; if (nil != serviceName) { // Go create an instance based on the name of the service. [self _createShrtyInstanceFromServiceName:serviceName]; if (nil != _shrty) { NSString* urlToShorten = [urlText stringValue]; [_shrty shortenUrl:urlToShorten]; } else { NSString* outString = [NSString stringWithFormat:@"Whoops, sorry, %@ is not currently supported, or is commented out, see _createShrtyInstanceFromServiceName", serviceName]; NSLog(outString); [outputText setStringValue:outString]; [outString release]; } } else { NSLog(@"Whoops, something went wrong. Couldn't get selected item for some unknown reason."); } } // shortenIt - (void)_createShrtyInstanceFromServiceName:(NSString*)serviceName { [_shrty release]; NSString* name = nil; NSString* pwd = nil; if ([serviceName isEqual:kShrtyPingFmSvcName]) { // The ping.fm service requires an API key and an App key, so // you need to provide that before creating an instance ping.fm. name = @"PingFmApiKeyGoesHere"; pwd = @"PingFmAppKeyGoesHere"; } if ([serviceName isEqual:kShrtyYOURLSSvcName]) { // This is a special case. YOURLS isn't a service, it's a URL Shortening Service YOU provide // by installing YOURLS on your server, so you'll need to create an instance here that // makes sense for your needs. see http://www.yourls.com for more information about creating // your own shortening service using YOURLS. name = @"YourYOURLSNameGoesHere"; pwd = @"YourYOURLSPasswordGoesHere"; _shrty = [[YOURLS alloc] initWithURL:@"YourYOURLSServiceURLGoesHere" userName:name userPassword:pwd delegate:self]; } else { _shrty = [ShrtySvc shrtySvcWithName:serviceName userName:name userPassword:pwd delegate:self]; } } #pragma mark - ShrtyDelegateProtocol Methods - (void) didReceiveShortenedURL:(NSString*)shortenedURL resultCode:(NSString*)resultCode { NSString* url = shortenedURL; if (nil == url) { url = @"GOT A NIL RESULT, DOH!"; } [outputText setStringValue:url]; [url release]; } // didReceiveShortenedURL - (void) didReceiveExpandedURL:(NSString*)expandedURL resultCode:(NSString*)resultCode { NSString* url = expandedURL; if (nil == url) { url = @"GOT A NIL RESULT, DOH!"; } [outputText setStringValue:url]; [url release]; } // didReceiveExpandedURL - (void) didFailWithError:(NSError*)error { NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); } // didFailWithError - (void) didFailWithErrorMessage:(NSString*)errorMessage { NSLog(@"Action failed with error %@", errorMessage); NSString* errorMsg = (nil == errorMessage) ? @"GOT A NIL ERROR MESSAGE, DOH!" : [errorMessage copy]; [outputText setStringValue:errorMsg]; [errorMsg release]; } @end