11 #import "ConnectionParams.h"
12 #import "GlobalDefaults.h"
13 #import "EncryptionController.h"
15 #import "TSXAdditions.h"
17 @interface ConnectionParams (Private)
24 - (id)initWithDictionary:(NSDictionary *)dict
26 if (!(
self = [super init]))
29 _connection_params = [dict mutableDeepCopy];
31 [
self decryptPasswordForKey:@"password"];
32 [
self decryptPasswordForKey:@"tsg_password"];
37 - (void)decryptPasswordForKey:(NSString *)key
39 if ([[_connection_params objectForKey:key] isKindOfClass:[NSData
class]])
42 decryptor] decryptString:[_connection_params objectForKey:key]];
43 [
self setValue:plaintext_password forKey:key];
47 - (id)initWithBaseDefaultParameters
49 return [
self initWithDictionary:[[NSUserDefaults standardUserDefaults]
50 dictionaryForKey:@"TSXDefaultComputerBookmarkSettings"]];
55 return [
self initWithDictionary:[NSDictionary dictionary]];
60 return [
self initWithDictionary:params->_connection_params];
64 [_connection_params release];
65 _connection_params = nil;
69 - (id)copyWithZone:(NSZone *)zone
74 - (NSString *)description
76 return [NSString stringWithFormat:@"ConnectionParams: %@", [_connection_params description]];
82 - (id)initWithCoder:(NSCoder *)decoder
84 if ([decoder containsValueForKey:
@"connectionParams"])
85 return [
self initWithDictionary:[decoder decodeObjectForKey:@"connectionParams"]];
90 - (void)encodeWithCoder:(NSCoder *)coder
92 NSSet *unserializable_keys = [NSSet setWithObjects:@"view", nil];
93 NSMutableDictionary *serializable_params =
94 [[NSMutableDictionary alloc] initWithCapacity:[_connection_params count]];
96 for (NSString *k in _connection_params)
97 if (([k characterAtIndex:0] !=
'_') && ![unserializable_keys containsObject:k])
98 [serializable_params setObject:[_connection_params objectForKey:k] forKey:k];
100 if ([serializable_params objectForKey:
@"password"] != nil)
101 [
self serializeDecryptedForKey:@"password" forParams:serializable_params];
102 if ([serializable_params objectForKey:
@"tsg_password"] != nil)
103 [
self serializeDecryptedForKey:@"tsg_password" forParams:serializable_params];
105 [coder encodeObject:serializable_params forKey:@"connectionParams"];
106 [serializable_params release];
109 - (void)serializeDecryptedForKey:(NSString *)key forParams:(NSMutableDictionary *)params
112 encryptString:[params objectForKey:key]];
114 if (encrypted_password)
115 [params setObject:encrypted_password forKey:key];
117 [params removeObjectForKey:key];
121 #pragma mark NSKeyValueCoding
123 - (void)setValue:(
id)value forKey:(NSString *)key
125 [
self willChangeValueForKey:key];
128 [
self setNilValueForKey:key];
130 [_connection_params setValue:value forKey:key];
132 [
self didChangeValueForKey:key];
135 - (void)setValue:(
id)value forKeyPath:(NSString *)key
137 [
self willChangeValueForKey:key];
140 [
self setNilValueForKey:key];
142 [_connection_params setValue:value forKeyPath:key];
144 [
self didChangeValueForKey:key];
147 - (void)setNilValueForKey:(NSString *)key
149 [_connection_params removeObjectForKey:key];
152 - (id)valueForKey:(NSString *)key
154 return [_connection_params valueForKey:key];
159 return [_connection_params allKeys];
163 #pragma mark KV convenience
165 - (BOOL)hasValueForKey:(NSString *)key
167 return [_connection_params objectForKey:key] != nil;
170 - (void)setInt:(
int)integer forKey:(NSString *)key
172 [
self setValue:[NSNumber numberWithInteger:integer] forKey:key];
174 - (int)intForKey:(NSString *)key
176 return [[
self valueForKey:key] intValue];
179 - (void)setBool:(BOOL)v forKey:(NSString *)key
181 [
self setValue:[NSNumber numberWithBool:v] forKey:key];
183 - (BOOL)boolForKey:(NSString *)key
185 return [[_connection_params objectForKey:key] boolValue];
188 - (const
char *)UTF8StringForKey:(NSString *)key
190 id val = [
self valueForKey:key];
193 if ([val respondsToSelector:
@selector(UTF8String)] && (str = [val UTF8String]))
199 - (NSString *)StringForKey:(NSString *)key
201 return [
self valueForKey:key];
204 - (BOOL)hasValueForKeyPath:(NSString *)key
206 return [_connection_params valueForKeyPath:key] != nil;
209 - (void)setInt:(
int)integer forKeyPath:(NSString *)key
211 [
self setValue:[NSNumber numberWithInteger:integer] forKeyPath:key];
213 - (int)intForKeyPath:(NSString *)key
215 return [[
self valueForKeyPath:key] intValue];
218 - (void)setBool:(BOOL)v forKeyPath:(NSString *)key
220 [
self setValue:[NSNumber numberWithBool:v] forKeyPath:key];
223 - (BOOL)boolForKeyPath:(NSString *)key
225 return [[
self valueForKeyPath:key] boolValue];
228 - (const
char *)UTF8StringForKeyPath:(NSString *)key
230 id val = [
self valueForKeyPath:key];
233 if ([val respondsToSelector:
@selector(UTF8String)] && (str = [val UTF8String]))
239 - (NSString *)StringForKeyPath:(NSString *)key
241 return [
self valueForKeyPath:key];
244 - (int)intForKey:(NSString *)key with3GEnabled:(BOOL)enabled
246 if (enabled && [
self boolForKey:
@"enable_3g_settings"])
247 return [
self intForKeyPath:[NSString stringWithFormat:@"settings_3g.%@", key]];
248 return [
self intForKeyPath:key];
251 - (BOOL)boolForKey:(NSString *)key with3GEnabled:(BOOL)enabled
253 if (enabled && [
self boolForKey:
@"enable_3g_settings"])
254 return [
self boolForKeyPath:[NSString stringWithFormat:@"settings_3g.%@", key]];
255 return [
self boolForKeyPath:key];