Standardize and update all copyright statements to name "ZXing authors" as suggested...
[zxing.git] / iphone / Classes / URIParsedResult.m
1 //
2 //  URIParsedResult.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 29/05/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #import "URIParsedResult.h"
23 #import "OpenUrlAction.h"
24 #import "EmailAction.h"
25 #import "SMSAction.h"
26
27 @implementation NSString (ZXingURLExtensions)
28
29 - (bool)looksLikeAURI {
30   if ([self rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location != NSNotFound) {
31     return false;
32   }
33   if ([self rangeOfString:@":"].location == NSNotFound) {
34     return false;
35   }
36   return true;
37 }
38
39 - (NSString *)massagedURLString {
40   NSRange colonRange = [self rangeOfString:@":"];
41   if (colonRange.location == NSNotFound) {
42     return [NSString stringWithFormat:@"http://%@", self];
43   } else {
44     return [NSString stringWithFormat:@"%@%@",
45             [[self substringToIndex:colonRange.location] lowercaseString],
46             [self substringFromIndex:colonRange.location]
47             ];
48   }
49 }
50
51 @end
52
53 @implementation URIParsedResult
54
55 #define PREFIX @"URL:"
56
57 @synthesize urlString;
58 @synthesize URL;
59
60 - (ResultAction *)createAction {
61   NSString *scheme = [self.URL scheme];
62   if (scheme) {
63     if ([@"mailto" isEqualToString:scheme]) {
64       return [EmailAction actionWithRecipient:[urlString substringFromIndex:7] 
65                                       subject:nil 
66                                          body:nil];
67     } else if ([@"sms" isEqualToString:scheme]) {
68       return [SMSAction actionWithNumber:[urlString substringFromIndex:4]];
69     }
70   }
71   return [OpenUrlAction actionWithURL:self.URL];
72 }
73
74 - initWithURLString:(NSString *)s URL:(NSURL *)url {
75   if ((self = [super init]) != nil) {
76     self.urlString = s;
77     self.URL = url;
78   }
79   return self;
80 }
81
82 - initWithURLString:(NSString *)s {
83   return [self initWithURLString:s URL:[NSURL URLWithString:s]];
84 }
85
86 + parsedResultForString:(NSString *)s {
87   NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
88   if (prefixRange.location == 0) {
89     int restStart = prefixRange.location + prefixRange.length;
90     return [[[self alloc] initWithURLString:[[s substringFromIndex:restStart] massagedURLString]]
91             autorelease];
92   }
93   
94   if ([s looksLikeAURI]) {
95     NSString *massaged = [s massagedURLString];
96     NSURL *url = [NSURL URLWithString:massaged];
97     if (url != nil) {
98       return [[[self alloc] initWithURLString:massaged URL:url] autorelease];
99     }
100   }
101   
102   return nil;
103 }
104
105 - (NSString *)stringForDisplay {
106   return self.urlString;
107 }
108
109 - (void)populateActions { 
110   NSLog(@"creating action to open URL '%@'", self.urlString);
111   [actions addObject:[self createAction]];
112 }
113
114 - (void)dealloc {
115   [URL release];
116   [urlString release];
117   [super dealloc];
118 }
119
120 @end