2007-01-31 18:12:33 +01:00
|
|
|
/* NSString+Utilities.m - this file is part of SOGo
|
2006-07-29 00:43:28 +02:00
|
|
|
*
|
2008-08-22 15:52:24 +02:00
|
|
|
* Copyright (C) 2006-2008 Inverse inc.
|
2006-07-29 00:43:28 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-04-11 21:51:45 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2007-05-15 22:52:01 +02:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
2007-02-08 17:30:29 +01:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
|
|
|
|
2008-08-21 21:17:38 +02:00
|
|
|
#import <EOControl/EOQualifier.h>
|
|
|
|
|
2007-09-04 17:38:25 +02:00
|
|
|
#import <NGExtensions/NGQuotedPrintableCoding.h>
|
|
|
|
|
2007-05-30 22:02:27 +02:00
|
|
|
#import "NSArray+Utilities.h"
|
2006-07-29 00:43:28 +02:00
|
|
|
#import "NSDictionary+URL.h"
|
|
|
|
|
2007-05-30 22:02:27 +02:00
|
|
|
#import "NSString+Utilities.h"
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
static NSMutableCharacterSet *urlNonEndingChars = nil;
|
|
|
|
static NSMutableCharacterSet *urlAfterEndingChars = nil;
|
2007-12-13 01:00:55 +01:00
|
|
|
static NSMutableCharacterSet *urlStartChars = nil;
|
2007-05-15 22:52:01 +02:00
|
|
|
|
2006-07-29 00:43:28 +02:00
|
|
|
@implementation NSString (SOGoURLExtension)
|
|
|
|
|
|
|
|
- (NSString *) composeURLWithAction: (NSString *) action
|
|
|
|
parameters: (NSDictionary *) urlParameters
|
|
|
|
andHash: (BOOL) useHash
|
|
|
|
{
|
|
|
|
NSMutableString *completeURL;
|
|
|
|
|
|
|
|
completeURL = [NSMutableString new];
|
|
|
|
[completeURL autorelease];
|
|
|
|
|
2006-10-03 18:09:43 +02:00
|
|
|
[completeURL appendString: [self urlWithoutParameters]];
|
2006-07-29 00:43:28 +02:00
|
|
|
if (![completeURL hasSuffix: @"/"])
|
|
|
|
[completeURL appendString: @"/"];
|
|
|
|
[completeURL appendString: action];
|
|
|
|
[completeURL appendString: [urlParameters asURLParameters]];
|
|
|
|
if (useHash)
|
|
|
|
[completeURL appendString: @"#"];
|
|
|
|
|
|
|
|
return completeURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) hostlessURL
|
|
|
|
{
|
|
|
|
NSString *newURL;
|
|
|
|
NSRange hostR, locationR;
|
|
|
|
|
|
|
|
if ([self hasPrefix: @"/"])
|
2006-12-14 21:17:46 +01:00
|
|
|
{
|
|
|
|
newURL = [self copy];
|
|
|
|
[newURL autorelease];
|
|
|
|
}
|
2006-07-29 00:43:28 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hostR = [self rangeOfString: @"://"];
|
|
|
|
locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
|
|
|
|
rangeOfString: @"/"];
|
2007-07-09 22:46:36 +02:00
|
|
|
newURL = [self substringFromIndex: (hostR.location + hostR.length
|
|
|
|
+ locationR.location)];
|
2006-07-29 00:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newURL;
|
|
|
|
}
|
|
|
|
|
2006-10-03 18:09:43 +02:00
|
|
|
- (NSString *) urlWithoutParameters;
|
|
|
|
{
|
|
|
|
NSRange r;
|
|
|
|
NSString *newUrl;
|
|
|
|
|
|
|
|
r = [self rangeOfString:@"?" options: NSBackwardsSearch];
|
|
|
|
if (r.length > 0)
|
|
|
|
newUrl = [self substringToIndex: NSMaxRange(r) - 1];
|
|
|
|
else
|
|
|
|
newUrl = self;
|
|
|
|
|
|
|
|
return newUrl;
|
|
|
|
}
|
|
|
|
|
2006-12-14 21:17:46 +01:00
|
|
|
- (NSString *) davMethodToObjC
|
|
|
|
{
|
|
|
|
NSMutableString *newName;
|
|
|
|
NSEnumerator *components;
|
|
|
|
NSString *component;
|
|
|
|
|
|
|
|
newName = [NSMutableString stringWithString: @"dav"];
|
|
|
|
components = [[self componentsSeparatedByString: @"-"] objectEnumerator];
|
2008-07-10 23:31:15 +02:00
|
|
|
while ((component = [components nextObject]))
|
|
|
|
[newName appendString: [component capitalizedString]];
|
2006-12-14 21:17:46 +01:00
|
|
|
|
|
|
|
return newName;
|
|
|
|
}
|
|
|
|
|
2008-04-22 01:03:48 +02:00
|
|
|
- (NSString *) davSetterName
|
|
|
|
{
|
|
|
|
unichar firstLetter;
|
|
|
|
NSString *firstString;
|
|
|
|
|
|
|
|
firstLetter = [self characterAtIndex: 0];
|
|
|
|
firstString = [[NSString stringWithCharacters: &firstLetter length: 1]
|
|
|
|
uppercaseString];
|
|
|
|
return [NSString stringWithFormat: @"set%@%@:",
|
|
|
|
firstString, [self substringFromIndex: 1]];
|
|
|
|
}
|
|
|
|
|
2007-07-22 22:32:16 +02:00
|
|
|
- (NSDictionary *) asDavInvocation
|
|
|
|
{
|
|
|
|
NSMutableDictionary *davInvocation;
|
|
|
|
NSRange nsEnclosing, methodEnclosing;
|
|
|
|
unsigned int length;
|
|
|
|
|
|
|
|
davInvocation = nil;
|
|
|
|
if ([self hasPrefix: @"{"])
|
|
|
|
{
|
|
|
|
nsEnclosing = [self rangeOfString: @"}"];
|
|
|
|
length = [self length];
|
|
|
|
if (nsEnclosing.length > 0
|
|
|
|
&& nsEnclosing.location < (length - 1))
|
|
|
|
{
|
|
|
|
methodEnclosing = NSMakeRange(nsEnclosing.location + 1,
|
|
|
|
length - nsEnclosing.location - 1);
|
|
|
|
nsEnclosing.length = nsEnclosing.location - 1;
|
|
|
|
nsEnclosing.location = 1;
|
|
|
|
davInvocation = [NSMutableDictionary dictionaryWithCapacity: 2];
|
|
|
|
[davInvocation setObject: [self substringWithRange: nsEnclosing]
|
|
|
|
forKey: @"ns"];
|
|
|
|
[davInvocation setObject: [self substringWithRange: methodEnclosing]
|
|
|
|
forKey: @"method"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return davInvocation;
|
|
|
|
}
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
- (NSRange) _rangeOfURLInRange: (NSRange) refRange
|
|
|
|
{
|
|
|
|
int start, length;
|
2007-05-16 18:30:04 +02:00
|
|
|
NSRange workRange;
|
2007-05-15 22:52:01 +02:00
|
|
|
|
2007-09-07 17:38:34 +02:00
|
|
|
// [urlNonEndingChars addCharactersInString: @">&=,.:;\t \r\n"];
|
|
|
|
// [urlAfterEndingChars addCharactersInString: @"()[]{}&;<\t \r\n"];
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
if (!urlNonEndingChars)
|
|
|
|
{
|
|
|
|
urlNonEndingChars = [NSMutableCharacterSet new];
|
2008-08-25 22:52:48 +02:00
|
|
|
[urlNonEndingChars addCharactersInString: @"=,.:;&()\t \r\n"];
|
2007-05-15 22:52:01 +02:00
|
|
|
}
|
|
|
|
if (!urlAfterEndingChars)
|
|
|
|
{
|
|
|
|
urlAfterEndingChars = [NSMutableCharacterSet new];
|
2008-08-25 22:52:48 +02:00
|
|
|
[urlAfterEndingChars addCharactersInString: @"()[]\t \r\n"];
|
2007-05-15 22:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start = refRange.location;
|
|
|
|
while (start > -1
|
2007-05-16 18:30:04 +02:00
|
|
|
&& ![urlAfterEndingChars characterIsMember:
|
|
|
|
[self characterAtIndex: start]])
|
2007-05-15 22:52:01 +02:00
|
|
|
start--;
|
|
|
|
start++;
|
|
|
|
length = [self length] - start;
|
2007-10-10 20:44:26 +02:00
|
|
|
workRange = NSMakeRange(start, length);
|
2007-05-16 18:30:04 +02:00
|
|
|
workRange = [self rangeOfCharacterFromSet: urlAfterEndingChars
|
|
|
|
options: NSLiteralSearch range: workRange];
|
|
|
|
if (workRange.location != NSNotFound)
|
|
|
|
length = workRange.location - start;
|
2007-05-15 22:52:01 +02:00
|
|
|
while
|
2007-05-16 17:56:53 +02:00
|
|
|
(length > 0
|
|
|
|
&& [urlNonEndingChars characterIsMember:
|
|
|
|
[self characterAtIndex: (start + length - 1)]])
|
2007-05-15 22:52:01 +02:00
|
|
|
length--;
|
|
|
|
|
|
|
|
return NSMakeRange (start, length);
|
|
|
|
}
|
|
|
|
|
2007-05-16 23:42:47 +02:00
|
|
|
- (void) _handleURLs: (NSMutableString *) selfCopy
|
|
|
|
textToMatch: (NSString *) match
|
|
|
|
prefix: (NSString *) prefix
|
2007-05-30 22:02:27 +02:00
|
|
|
inRanges: (NSMutableArray *) ranges
|
2007-05-15 22:52:01 +02:00
|
|
|
{
|
2008-08-22 01:35:34 +02:00
|
|
|
NSEnumerator *enumRanges;
|
|
|
|
NSMutableArray *newRanges;
|
|
|
|
NSRange matchRange, currentUrlRange, rest;
|
|
|
|
NSString *urlText, *newUrlText, *range;
|
2008-08-22 02:49:23 +02:00
|
|
|
unsigned int length, matchLength, offset;
|
2007-12-13 01:00:55 +01:00
|
|
|
int startLocation;
|
2007-05-15 22:52:01 +02:00
|
|
|
|
2007-12-13 01:00:55 +01:00
|
|
|
if (!urlStartChars)
|
|
|
|
{
|
|
|
|
urlStartChars = [NSMutableCharacterSet new];
|
|
|
|
[urlStartChars addCharactersInString: @"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
@"0123456789:@"];
|
|
|
|
}
|
2008-08-22 01:35:34 +02:00
|
|
|
newRanges = [NSMutableArray new];
|
2007-05-16 23:42:47 +02:00
|
|
|
matchLength = [match length];
|
2008-08-22 01:35:34 +02:00
|
|
|
rest.location = -1;
|
|
|
|
|
|
|
|
matchRange = [selfCopy rangeOfString: match];
|
|
|
|
while (matchRange.location != NSNotFound)
|
2007-12-13 01:00:55 +01:00
|
|
|
{
|
2008-08-22 01:35:34 +02:00
|
|
|
startLocation = matchRange.location;
|
|
|
|
while (startLocation > rest.location
|
2007-12-13 01:00:55 +01:00
|
|
|
&& [urlStartChars characterIsMember:
|
|
|
|
[selfCopy characterAtIndex: startLocation]])
|
|
|
|
startLocation--;
|
2008-08-22 01:35:34 +02:00
|
|
|
matchRange.location = startLocation + 1;
|
|
|
|
|
|
|
|
currentUrlRange = [selfCopy _rangeOfURLInRange: matchRange];
|
|
|
|
if ([ranges hasRangeIntersection: currentUrlRange])
|
|
|
|
rest.location = NSMaxRange(currentUrlRange);
|
2007-05-30 22:02:27 +02:00
|
|
|
else
|
2007-05-16 23:42:47 +02:00
|
|
|
{
|
2008-08-22 01:35:34 +02:00
|
|
|
if (currentUrlRange.length > matchLength)
|
|
|
|
[newRanges addRange: currentUrlRange];
|
|
|
|
rest.location = NSMaxRange(currentUrlRange);
|
2007-05-16 23:42:47 +02:00
|
|
|
}
|
2007-10-10 20:44:26 +02:00
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
length = [selfCopy length];
|
|
|
|
rest.length = length - rest.location;
|
2008-08-22 01:35:34 +02:00
|
|
|
matchRange = [selfCopy rangeOfString: match
|
|
|
|
options: 0 range: rest];
|
|
|
|
}
|
|
|
|
|
2008-08-22 02:49:23 +02:00
|
|
|
// Make the substitutions, keep track of the new offset
|
|
|
|
offset = 0;
|
|
|
|
enumRanges = [newRanges objectEnumerator];
|
2008-08-22 01:35:34 +02:00
|
|
|
range = [enumRanges nextObject];
|
|
|
|
while (range)
|
|
|
|
{
|
2008-08-22 02:49:23 +02:00
|
|
|
currentUrlRange = NSRangeFromString(range);
|
|
|
|
currentUrlRange.location += offset;
|
2008-08-22 01:35:34 +02:00
|
|
|
urlText = [selfCopy substringFromRange: currentUrlRange];
|
|
|
|
if ([urlText hasPrefix: prefix]) prefix = @"";
|
|
|
|
newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
|
|
|
|
prefix, urlText, urlText];
|
|
|
|
[selfCopy replaceCharactersInRange: currentUrlRange
|
|
|
|
withString: newUrlText];
|
2008-08-22 02:49:23 +02:00
|
|
|
offset += ([newUrlText length] - [urlText length]);
|
2008-08-22 01:35:34 +02:00
|
|
|
|
|
|
|
// Add range for further substitutions
|
|
|
|
currentUrlRange = NSMakeRange (currentUrlRange.location, [newUrlText length]);
|
|
|
|
[ranges addRange: currentUrlRange];
|
|
|
|
|
|
|
|
range = [enumRanges nextObject];
|
2007-05-15 22:52:01 +02:00
|
|
|
}
|
2007-05-16 23:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringByDetectingURLs
|
|
|
|
{
|
|
|
|
NSMutableString *selfCopy;
|
2007-05-30 22:02:27 +02:00
|
|
|
NSMutableArray *ranges;
|
2007-05-16 23:42:47 +02:00
|
|
|
|
2007-05-30 22:02:27 +02:00
|
|
|
ranges = [NSMutableArray new];
|
2007-05-16 23:42:47 +02:00
|
|
|
selfCopy = [NSMutableString stringWithString: self];
|
2007-05-30 22:02:27 +02:00
|
|
|
[self _handleURLs: selfCopy
|
|
|
|
textToMatch: @"://"
|
|
|
|
prefix: @""
|
|
|
|
inRanges: ranges];
|
|
|
|
[self _handleURLs: selfCopy
|
|
|
|
textToMatch: @"@"
|
|
|
|
prefix: @"mailto:"
|
|
|
|
inRanges: ranges];
|
|
|
|
[ranges release];
|
2007-05-15 22:52:01 +02:00
|
|
|
|
|
|
|
return selfCopy;
|
|
|
|
}
|
|
|
|
|
2009-01-07 17:45:15 +01:00
|
|
|
- (NSString *) doubleQuotedString
|
2007-05-25 19:30:24 +02:00
|
|
|
{
|
|
|
|
NSMutableString *representation;
|
|
|
|
|
|
|
|
representation = [NSMutableString stringWithString: self];
|
|
|
|
[representation replaceString: @"\\" withString: @"\\\\"];
|
|
|
|
[representation replaceString: @"\"" withString: @"\\\""];
|
|
|
|
[representation replaceString: @"/" withString: @"\\/"];
|
|
|
|
[representation replaceString: @"\b" withString: @"\\b"];
|
|
|
|
[representation replaceString: @"\f" withString: @"\\f"];
|
|
|
|
[representation replaceString: @"\n" withString: @"\\n"];
|
|
|
|
[representation replaceString: @"\r" withString: @"\\r"];
|
|
|
|
[representation replaceString: @"\t" withString: @"\\t"];
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"\"%@\"", representation];
|
|
|
|
}
|
|
|
|
|
2009-01-07 17:45:15 +01:00
|
|
|
- (NSString *) jsonRepresentation
|
|
|
|
{
|
|
|
|
return [self doubleQuotedString];
|
|
|
|
}
|
|
|
|
|
2009-01-16 16:33:07 +01:00
|
|
|
- (NSString *) asCSSIdentifier
|
|
|
|
{
|
|
|
|
NSMutableString *cssIdentifier;
|
|
|
|
|
|
|
|
cssIdentifier = [NSMutableString stringWithString: self];
|
2009-01-21 18:01:06 +01:00
|
|
|
[cssIdentifier replaceString: @"_" withString: @"_U_"];
|
2009-01-16 16:33:07 +01:00
|
|
|
[cssIdentifier replaceString: @"." withString: @"_D_"];
|
|
|
|
[cssIdentifier replaceString: @"#" withString: @"_H_"];
|
|
|
|
[cssIdentifier replaceString: @"@" withString: @"_A_"];
|
|
|
|
[cssIdentifier replaceString: @"*" withString: @"_S_"];
|
|
|
|
[cssIdentifier replaceString: @":" withString: @"_C_"];
|
|
|
|
[cssIdentifier replaceString: @"," withString: @"_CO_"];
|
|
|
|
[cssIdentifier replaceString: @" " withString: @"_SP_"];
|
|
|
|
|
|
|
|
return cssIdentifier;
|
|
|
|
}
|
|
|
|
|
2007-08-09 22:59:30 +02:00
|
|
|
- (NSString *) pureEMailAddress
|
|
|
|
{
|
|
|
|
NSString *pureAddress;
|
|
|
|
NSRange delimiter;
|
|
|
|
|
|
|
|
delimiter = [self rangeOfString: @"<"];
|
|
|
|
if (delimiter.location == NSNotFound)
|
|
|
|
pureAddress = self;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pureAddress = [self substringFromIndex: NSMaxRange (delimiter)];
|
|
|
|
delimiter = [pureAddress rangeOfString: @">"];
|
|
|
|
if (delimiter.location != NSNotFound)
|
|
|
|
pureAddress = [pureAddress substringToIndex: delimiter.location];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pureAddress;
|
|
|
|
}
|
|
|
|
|
2007-08-24 00:10:50 +02:00
|
|
|
- (NSString *) asQPSubjectString: (NSString *) encoding
|
|
|
|
{
|
|
|
|
NSString *qpString, *subjectString;
|
|
|
|
NSData *subjectData, *destSubjectData;
|
|
|
|
|
|
|
|
subjectData = [self dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
destSubjectData = [subjectData dataByEncodingQuotedPrintable];
|
|
|
|
|
|
|
|
qpString = [[NSString alloc] initWithData: destSubjectData
|
|
|
|
encoding: NSASCIIStringEncoding];
|
|
|
|
[qpString autorelease];
|
|
|
|
if ([qpString length] > [self length])
|
2008-09-22 22:31:57 +02:00
|
|
|
{
|
|
|
|
qpString = [qpString stringByReplacingString: @" " withString: @"_"];
|
|
|
|
subjectString = [NSString stringWithFormat: @"=?%@?Q?%@?=",
|
|
|
|
encoding, qpString];
|
|
|
|
}
|
2007-08-24 00:10:50 +02:00
|
|
|
else
|
|
|
|
subjectString = self;
|
|
|
|
|
|
|
|
return subjectString;
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:17:38 +02:00
|
|
|
- (BOOL) caseInsensitiveMatches: (NSString *) match
|
|
|
|
{
|
|
|
|
EOQualifier *sq;
|
|
|
|
NSString *format;
|
|
|
|
|
|
|
|
format = [NSString stringWithFormat:
|
|
|
|
@"(description isCaseInsensitiveLike: '%@')",
|
|
|
|
match];
|
|
|
|
sq = [EOQualifier qualifierWithQualifierFormat: format];
|
|
|
|
|
|
|
|
return [sq evaluateWithObject: self];
|
|
|
|
}
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
#if LIB_FOUNDATION_LIBRARY
|
2007-04-11 21:33:25 +02:00
|
|
|
- (BOOL) boolValue
|
|
|
|
{
|
|
|
|
return !([self isEqualToString: @"0"]
|
|
|
|
|| [self isEqualToString: @"NO"]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-29 00:43:28 +02:00
|
|
|
@end
|