FreeRDP/client/iOS/Models/ConnectionParams.m

259 lines
6.1 KiB
Mathematica
Raw Normal View History

/*
Connection Parameters abstraction
2019-11-06 17:24:51 +03:00
2013-12-04 14:37:57 +04:00
Copyright 2013 Thincast Technologies GmbH, Author: Dorian Johnson
2019-11-06 17:24:51 +03:00
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
*/
#import "ConnectionParams.h"
#import "GlobalDefaults.h"
#import "EncryptionController.h"
#import "Utils.h"
#import "TSXAdditions.h"
@interface ConnectionParams (Private)
2019-11-06 17:24:51 +03:00
- (id)initWithConnectionParams:(ConnectionParams *)params;
@end
@implementation ConnectionParams
// Designated initializer.
2019-11-06 17:24:51 +03:00
- (id)initWithDictionary:(NSDictionary *)dict
{
if (!(self = [super init]))
return nil;
2019-11-06 17:24:51 +03:00
_connection_params = [dict mutableDeepCopy];
2019-11-06 17:24:51 +03:00
[self decryptPasswordForKey:@"password"];
[self decryptPasswordForKey:@"tsg_password"];
return self;
}
2019-11-06 17:24:51 +03:00
- (void)decryptPasswordForKey:(NSString *)key
2013-05-08 19:29:52 +04:00
{
if ([[_connection_params objectForKey:key] isKindOfClass:[NSData class]])
{
2019-11-06 17:24:51 +03:00
NSString *plaintext_password = [[[EncryptionController sharedEncryptionController]
decryptor] decryptString:[_connection_params objectForKey:key]];
2013-05-08 19:29:52 +04:00
[self setValue:plaintext_password forKey:key];
}
}
- (id)initWithBaseDefaultParameters
{
2019-11-06 17:24:51 +03:00
return [self initWithDictionary:[[NSUserDefaults standardUserDefaults]
dictionaryForKey:@"TSXDefaultComputerBookmarkSettings"]];
}
- (id)init
{
return [self initWithDictionary:[NSDictionary dictionary]];
}
2019-11-06 17:24:51 +03:00
- (id)initWithConnectionParams:(ConnectionParams *)params
{
return [self initWithDictionary:params->_connection_params];
}
- (void)dealloc
{
2019-11-06 17:24:51 +03:00
[_connection_params release];
_connection_params = nil;
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
return [[ConnectionParams alloc] initWithDictionary:_connection_params];
}
2019-11-06 17:24:51 +03:00
- (NSString *)description
{
return [NSString stringWithFormat:@"ConnectionParams: %@", [_connection_params description]];
}
#pragma mark -
#pragma mark NSCoder
- (id)initWithCoder:(NSCoder *)decoder
{
if ([decoder containsValueForKey:@"connectionParams"])
return [self initWithDictionary:[decoder decodeObjectForKey:@"connectionParams"]];
2019-11-06 17:24:51 +03:00
return [self init];
}
- (void)encodeWithCoder:(NSCoder *)coder
2019-11-06 17:24:51 +03:00
{
NSSet *unserializable_keys = [NSSet setWithObjects:@"view", nil];
NSMutableDictionary *serializable_params =
[[NSMutableDictionary alloc] initWithCapacity:[_connection_params count]];
for (NSString *k in _connection_params)
if (([k characterAtIndex:0] != '_') && ![unserializable_keys containsObject:k])
[serializable_params setObject:[_connection_params objectForKey:k] forKey:k];
2019-11-06 17:24:51 +03:00
if ([serializable_params objectForKey:@"password"] != nil)
2019-11-06 17:24:51 +03:00
[self serializeDecryptedForKey:@"password" forParams:serializable_params];
2013-05-08 19:29:52 +04:00
if ([serializable_params objectForKey:@"tsg_password"] != nil)
2019-11-06 17:24:51 +03:00
[self serializeDecryptedForKey:@"tsg_password" forParams:serializable_params];
[coder encodeObject:serializable_params forKey:@"connectionParams"];
[serializable_params release];
}
2019-11-06 17:24:51 +03:00
- (void)serializeDecryptedForKey:(NSString *)key forParams:(NSMutableDictionary *)params
2013-05-08 19:29:52 +04:00
{
2019-11-06 17:24:51 +03:00
NSData *encrypted_password = [[[EncryptionController sharedEncryptionController] encryptor]
encryptString:[params objectForKey:key]];
if (encrypted_password)
[params setObject:encrypted_password forKey:key];
else
[params removeObjectForKey:key];
2013-05-08 19:29:52 +04:00
}
#pragma mark -
#pragma mark NSKeyValueCoding
- (void)setValue:(id)value forKey:(NSString *)key
{
[self willChangeValueForKey:key];
2019-11-06 17:24:51 +03:00
if (value == nil)
[self setNilValueForKey:key];
else
[_connection_params setValue:value forKey:key];
2019-11-06 17:24:51 +03:00
[self didChangeValueForKey:key];
}
- (void)setValue:(id)value forKeyPath:(NSString *)key
{
[self willChangeValueForKey:key];
2019-11-06 17:24:51 +03:00
if (value == nil)
[self setNilValueForKey:key];
else
[_connection_params setValue:value forKeyPath:key];
2019-11-06 17:24:51 +03:00
[self didChangeValueForKey:key];
}
- (void)setNilValueForKey:(NSString *)key
{
[_connection_params removeObjectForKey:key];
}
2019-11-06 17:24:51 +03:00
- (id)valueForKey:(NSString *)key
{
return [_connection_params valueForKey:key];
}
2019-11-06 17:24:51 +03:00
- (NSArray *)allKeys
{
return [_connection_params allKeys];
}
#pragma mark -
#pragma mark KV convenience
2019-11-06 17:24:51 +03:00
- (BOOL)hasValueForKey:(NSString *)key
{
return [_connection_params objectForKey:key] != nil;
}
2019-11-06 17:24:51 +03:00
- (void)setInt:(int)integer forKey:(NSString *)key
{
[self setValue:[NSNumber numberWithInteger:integer] forKey:key];
}
2019-11-06 17:24:51 +03:00
- (int)intForKey:(NSString *)key
{
return [[self valueForKey:key] intValue];
}
2019-11-06 17:24:51 +03:00
- (void)setBool:(BOOL)v forKey:(NSString *)key
{
[self setValue:[NSNumber numberWithBool:v] forKey:key];
}
2019-11-06 17:24:51 +03:00
- (BOOL)boolForKey:(NSString *)key
{
return [[_connection_params objectForKey:key] boolValue];
}
2019-11-06 17:24:51 +03:00
- (const char *)UTF8StringForKey:(NSString *)key
{
id val = [self valueForKey:key];
2019-11-06 17:24:51 +03:00
const char *str;
if ([val respondsToSelector:@selector(UTF8String)] && (str = [val UTF8String]))
return str;
2019-11-06 17:24:51 +03:00
return "";
}
2019-11-06 17:24:51 +03:00
- (NSString *)StringForKey:(NSString *)key
{
return [self valueForKey:key];
}
2019-11-06 17:24:51 +03:00
- (BOOL)hasValueForKeyPath:(NSString *)key
{
2019-11-06 17:24:51 +03:00
return [_connection_params valueForKeyPath:key] != nil;
}
2019-11-06 17:24:51 +03:00
- (void)setInt:(int)integer forKeyPath:(NSString *)key
{
[self setValue:[NSNumber numberWithInteger:integer] forKeyPath:key];
}
2019-11-06 17:24:51 +03:00
- (int)intForKeyPath:(NSString *)key
{
return [[self valueForKeyPath:key] intValue];
}
2019-11-06 17:24:51 +03:00
- (void)setBool:(BOOL)v forKeyPath:(NSString *)key
{
[self setValue:[NSNumber numberWithBool:v] forKeyPath:key];
}
2019-11-06 17:24:51 +03:00
- (BOOL)boolForKeyPath:(NSString *)key
{
return [[self valueForKeyPath:key] boolValue];
}
2019-11-06 17:24:51 +03:00
- (const char *)UTF8StringForKeyPath:(NSString *)key
{
id val = [self valueForKeyPath:key];
2019-11-06 17:24:51 +03:00
const char *str;
if ([val respondsToSelector:@selector(UTF8String)] && (str = [val UTF8String]))
return str;
2019-11-06 17:24:51 +03:00
return "";
}
2019-11-06 17:24:51 +03:00
- (NSString *)StringForKeyPath:(NSString *)key
{
return [self valueForKeyPath:key];
}
2019-11-06 17:24:51 +03:00
- (int)intForKey:(NSString *)key with3GEnabled:(BOOL)enabled
{
2019-11-06 17:24:51 +03:00
if (enabled && [self boolForKey:@"enable_3g_settings"])
return [self intForKeyPath:[NSString stringWithFormat:@"settings_3g.%@", key]];
return [self intForKeyPath:key];
}
2019-11-06 17:24:51 +03:00
- (BOOL)boolForKey:(NSString *)key with3GEnabled:(BOOL)enabled
{
2019-11-06 17:24:51 +03:00
if (enabled && [self boolForKey:@"enable_3g_settings"])
return [self boolForKeyPath:[NSString stringWithFormat:@"settings_3g.%@", key]];
return [self boolForKeyPath:key];
}
@end