Skip to content
Browse files

extract application-level URL handling into a new class

  • Loading branch information...
1 parent fad9319 commit 2497d09df55ea915f5f1a1ea4f6e5202749564b8 @aharren committed Apr 8, 2012
View
6 CardDecks.xcodeproj/project.pbxproj
@@ -134,6 +134,7 @@
E0D673E0151640F90040A2B5 /* CDXTimerKeyboardExtensionView~ipad.xib in Resources */ = {isa = PBXBuildFile; fileRef = E0D673DF151640F90040A2B5 /* CDXTimerKeyboardExtensionView~ipad.xib */; };
E0D8C7FE1172255F00356C2C /* lcl.m in Sources */ = {isa = PBXBuildFile; fileRef = E0D8C7FD1172255F00356C2C /* lcl.m */; };
E0D909281172275D00AD50DA /* CDXLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = E0D909271172275D00AD50DA /* CDXLogger.m */; };
+ E0DBA1821531F39B0049A26B /* CDXAppURL.m in Sources */ = {isa = PBXBuildFile; fileRef = E0DBA1811531F39B0049A26B /* CDXAppURL.m */; };
E0DD7D6511D6302B00ED23B6 /* Logo-Prerendered@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E0DD7D6411D6302B00ED23B6 /* Logo-Prerendered@2x.png */; };
E0DD7D9311D63A1D00ED23B6 /* Cell-Logo@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E0DD7D9211D63A1D00ED23B6 /* Cell-Logo@2x.png */; };
E0DD7E6B11D65CEA00ED23B6 /* CDXIndexDotsView.m in Sources */ = {isa = PBXBuildFile; fileRef = E0DD7E6A11D65CEA00ED23B6 /* CDXIndexDotsView.m */; };
@@ -358,6 +359,8 @@
E0D909271172275D00AD50DA /* CDXLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CDXLogger.m; path = Logging/CDXLogger.m; sourceTree = "<group>"; };
E0D9096811722AD700AD50DA /* qlog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = qlog.h; path = Logging/qlog.h; sourceTree = "<group>"; };
E0D9E159132EBA1E001435D0 /* MessageUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MessageUI.framework; path = System/Library/Frameworks/MessageUI.framework; sourceTree = SDKROOT; };
+ E0DBA1801531F3380049A26B /* CDXAppURL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = CDXAppURL.h; path = Classes/CDXAppURL.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
+ E0DBA1811531F39B0049A26B /* CDXAppURL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = CDXAppURL.m; path = Classes/CDXAppURL.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
E0DD7D6411D6302B00ED23B6 /* Logo-Prerendered@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Logo-Prerendered@2x.png"; path = "Images/Logo-Prerendered@2x.png"; sourceTree = "<group>"; };
E0DD7D9211D63A1D00ED23B6 /* Cell-Logo@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Cell-Logo@2x.png"; path = "Images/Cells/Cell-Logo@2x.png"; sourceTree = "<group>"; };
E0DD7E6911D65CEA00ED23B6 /* CDXIndexDotsView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDXIndexDotsView.h; path = Classes/Views/CDXIndexDotsView.h; sourceTree = "<group>"; };
@@ -600,6 +603,8 @@
E047CEF6119369430024A022 /* CDXAppWindowManager.m */,
E0620CF411BA9EA3006AABD2 /* CDXAppWindowNoticeView.h */,
E0620CF511BA9EA3006AABD2 /* CDXAppWindowNoticeView.m */,
+ E0DBA1801531F3380049A26B /* CDXAppURL.h */,
+ E0DBA1811531F39B0049A26B /* CDXAppURL.m */,
E0D01AEB1170EF9200004856 /* CDXImageFactory.h */,
E0D01AEC1170EF9200004856 /* CDXImageFactory.m */,
E0F2B2A9119550BC0092D11B /* CDXObjectCache.h */,
@@ -1033,6 +1038,7 @@
E04E7A1114E4717300D46037 /* CDXTimerKeyboardExtension.m in Sources */,
E030327F152BA1B500F42494 /* CDXCardViewImageRendering.m in Sources */,
E048D463152E3A1B0081B88F /* CDXCardViewDirectRendering.m in Sources */,
+ E0DBA1821531F39B0049A26B /* CDXAppURL.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
35 Classes/CDXAppDelegate.m
@@ -33,6 +33,7 @@
#import "CDXCardDecks.h"
#import "CDXDevice.h"
#import "CDXAppSettings.h"
+#import "CDXAppURL.h"
#undef ql_component
#define ql_component lcl_cApplication
@@ -261,39 +262,7 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application {
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
- qltrace();
- if (url == nil) {
- return NO;
- }
-
- NSString *host = [url host];
- if (!(host == nil || [@"" isEqualToString:host])) {
- return NO;
- }
-
- BOOL handled = NO;
- NSString *path = [url path];
- CDXCardDeck *deckToAdd = nil;
-
- if ([@"/add" isEqualToString:path]) {
- deckToAdd = [CDXCardDeckURLSerializer cardDeckFromVersion1String:[url query]];
- } else if ([@"/2/add" isEqualToString:path]) {
- deckToAdd = [CDXCardDeckURLSerializer cardDeckFromVersion2String:[url query]];
- }
-
- if (deckToAdd != nil) {
- handled = YES;
- [deckToAdd updateStorageObjectDeferred:YES];
- CDXCardDeckHolder *holder = [CDXCardDeckHolder cardDeckHolderWithCardDeck:deckToAdd];
- [cardDecks addPendingCardDeckAdd:holder];
- [appWindowManager popToInitialViewController];
- UIViewController *vc = [appWindowManager visibleViewController];
- if ([vc respondsToSelector:@selector(processPendingCardDeckAddsAtTopDelayed)]) {
- [vc performSelector:@selector(processPendingCardDeckAddsAtTopDelayed)];
- }
- }
-
- return handled;
+ return [CDXAppURL handleOpenURL:url cardDecks:cardDecks];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
View
39 Classes/CDXAppURL.h
@@ -0,0 +1,39 @@
+//
+//
+// CDXAppURL.h
+//
+//
+// Copyright (c) 2009-2012 Arne Harren <ah@0xc0.de>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import "CDXCardDecks.h"
+
+
+@interface CDXAppURL : NSObject {
+
+}
+
++ (BOOL)handleOpenURL:(NSURL *)url cardDecks:(CDXCardDecks *)cardDecks;
+
++ (NSString *)carddecksURLStringForVersion2AddActionFromCardDeck:(CDXCardDeck *)cardDeck;
++ (NSString *)httpURLStringForVersion2AddActionFromCardDeck:(CDXCardDeck *)cardDeck;
+
+@end
+
View
90 Classes/CDXAppURL.m
@@ -0,0 +1,90 @@
+//
+//
+// CDXAppURL.m
+//
+//
+// Copyright (c) 2009-2012 Arne Harren <ah@0xc0.de>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import "CDXAppURL.h"
+#import "CDXCardDeckURLSerializer.h"
+
+#undef ql_component
+#define ql_component lcl_cApplication
+
+
+#define CDXAppURLPath_v1_add @"/add"
+#define CDXAppURLPath_v2_add @"/2/add"
+
+#define CDXAppURLPrefix_carddecks_v2_add @"carddecks://" CDXAppURLPath_v2_add @"?"
+#define CDXAppURLPrefix_http_v2_add @"http://carddecks.protocol.0xc0.de" CDXAppURLPath_v2_add @"?"
+
+
+@implementation CDXAppURL
+
++ (BOOL)handleOpenURL:(NSURL *)url cardDecks:(CDXCardDecks *)cardDecks {
+ qltrace(@"%@", url);
+ if (url == nil) {
+ return NO;
+ }
+
+ NSString *host = [url host];
+ if (!(host == nil || [@"" isEqualToString:host])) {
+ return NO;
+ }
+
+ NSString *path = [url path];
+ CDXCardDeck *deckToAdd = nil;
+ if ([CDXAppURLPath_v1_add isEqualToString:path]) {
+ deckToAdd = [CDXCardDeckURLSerializer cardDeckFromVersion1String:[url query]];
+ } else if ([CDXAppURLPath_v2_add isEqualToString:path]) {
+ deckToAdd = [CDXCardDeckURLSerializer cardDeckFromVersion2String:[url query]];
+ }
+
+ if (deckToAdd == nil) {
+ return NO;
+ }
+
+ [deckToAdd updateStorageObjectDeferred:YES];
+ CDXCardDeckHolder *holder = [CDXCardDeckHolder cardDeckHolderWithCardDeck:deckToAdd];
+ [cardDecks addPendingCardDeckAdd:holder];
+ [[CDXAppWindowManager sharedAppWindowManager] popToInitialViewController];
+ UIViewController *vc = [[CDXAppWindowManager sharedAppWindowManager] visibleViewController];
+ if ([vc respondsToSelector:@selector(processPendingCardDeckAddsAtTopDelayed)]) {
+ [vc performSelector:@selector(processPendingCardDeckAddsAtTopDelayed)];
+ }
+
+ return YES;
+}
+
++ (NSString *)carddecksURLStringForVersion2AddActionFromCardDeck:(CDXCardDeck *)cardDeck {
+ NSString *urlString = [CDXAppURLPrefix_carddecks_v2_add stringByAppendingString:[CDXCardDeckURLSerializer version2StringFromCardDeck:cardDeck]];
+ qltrace(@"%@", urlString);
+ return urlString;
+}
+
++ (NSString *)httpURLStringForVersion2AddActionFromCardDeck:(CDXCardDeck *)cardDeck {
+ NSString *urlString = [CDXAppURLPrefix_http_v2_add stringByAppendingString:[CDXCardDeckURLSerializer version2StringFromCardDeck:cardDeck]];
+ qltrace(@"%@", urlString);
+ return urlString;
+}
+
+@end
+
View
21 Classes/Controllers/CDXCardDeckListViewController.m
@@ -29,7 +29,7 @@
#import "CDXSettingsViewController.h"
#import "CDXImageFactory.h"
#import "CDXCardDecks.h"
-#import "CDXCardDeckURLSerializer.h"
+#import "CDXAppURL.h"
#import "CDXAppSettings.h"
#import "CDXDevice.h"
#import <Twitter/Twitter.h>
@@ -395,19 +395,19 @@ - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger
default:
return;
case CDXCardDeckListViewControllerActionEmailDeck: {
- NSString *body = [@"carddecks:///2/add?" stringByAppendingString:[CDXCardDeckURLSerializer version2StringFromCardDeck:cardDeck]];
+ NSString *carddeckUrl = [CDXAppURL carddecksURLStringForVersion2AddActionFromCardDeck:cardDeck];
if ([[CDXAppSettings sharedAppSettings] useMailApplication] || ![MFMailComposeViewController canSendMail]) {
- NSString *urlString = [NSString stringWithFormat:@"mailto:?&subject=%@&body=%@",
- [[cardDeck.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
- stringByReplacingOccurrencesOfString:@"&" withString:@"%26"],
- [[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
- stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]];
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
+ NSString *mailUrl = [NSString stringWithFormat:@"mailto:?&subject=%@&body=%@",
+ [[cardDeck.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
+ stringByReplacingOccurrencesOfString:@"&" withString:@"%26"],
+ [[carddeckUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
+ stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]];
+ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailUrl]];
} else {
MFMailComposeViewController *vc = [[[MFMailComposeViewController alloc] init] autorelease];
[vc setMailComposeDelegate:self];
[vc setSubject:cardDeck.name];
- [vc setMessageBody:[body stringByAppendingString:@" "] isHTML:NO];
+ [vc setMessageBody:[carddeckUrl stringByAppendingString:@" "] isHTML:NO];
[[CDXKeyboardExtensions sharedKeyboardExtensions] setEnabled:NO];
[[CDXAppWindowManager sharedAppWindowManager] presentModalViewController:vc animated:YES];
}
@@ -419,8 +419,7 @@ - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger
// in order to get the 'No Twitter Accounts' system message if no
// account is configured yet
TWTweetComposeViewController *twc = [[[TWTweetComposeViewController alloc] init] autorelease];
- NSString *urltext = [@"http://carddecks.protocol.0xc0.de/2/add?" stringByAppendingString:[CDXCardDeckURLSerializer version2StringFromCardDeck:cardDeck]];
- NSURL *url = [NSURL URLWithString:urltext];
+ NSURL *url = [NSURL URLWithString:[CDXAppURL httpURLStringForVersion2AddActionFromCardDeck:cardDeck]];
if ([twc addURL:url] == NO) {
return;
}

0 comments on commit 2497d09

Please sign in to comment.
Something went wrong with that request. Please try again.