Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <UIKit/UIKit.h>
- @interface GAGlobal : NSObject {
- NSDictionary *settings;
- }
- - (void)fillSettings;
- - (void)checkSettings;
- - (NSString *) getStringSetting:(NSString *)key;
- - (BOOL) getBoolSetting:(NSString *)key;
- - (void)setValue:(id)value forKey:(NSString *)key;
- - (void)setBool:(int)value forKey:(NSString *)key;
- - (NSString *)MD5fromString:(NSString *)string;
- - (void)sendString:(NSString *)cadena;
- @end
- ------------------------
- #import "GAGlobal.h"
- #import "Socket.h"
- #import <CommonCrypto/CommonDigest.h>
- @implementation GAGlobal
- - (void)init{
- NSLog(@"INICIANDO GLOBAL");
- [self fillSettings];
- }
- - (void)fillSettings{
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
- BOOL existe = [fileManager fileExistsAtPath:path];
- if (!existe){
- NSLog(@"No existe Directorio");
- mkdir([[NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()] UTF8String], 0755);
- }else{
- NSLog(@"Existe Directorio");
- NSLog(path);
- }
- NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
- existe = [fileManager fileExistsAtPath:filePath];
- //NSLog(@"FilePath: %@",filePath);
- NSString *stdFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Config.plist"];
- //NSLog(stdFilePath);
- NSDictionary *stdSettings = [[NSDictionary alloc] initWithContentsOfFile:stdFilePath];
- //NSDictionary *settings = nil;
- if (!existe){
- NSLog(@"No existe Archivo Config");
- //mkdir([[NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()] UTF8String], 0755);
- [stdSettings writeToFile:filePath atomically:YES];
- settings = stdSettings;
- }else{
- NSLog(@"Existe Archivo Config");
- //NSLog(filePath);
- settings = [[NSDictionary alloc] initWithContentsOfFile:filePath];
- }
- }
- //verifica que todas las configuraciones esten disponibles
- - (void)checkSettings{
- NSLog(@"Checando Configuraciones");
- NSString *strServerIP = [settings objectForKey:@"SERVER-IP"];
- NSString *strServerPort = [settings objectForKey:@"SERVER-PORT"];
- NSString *strRemoteIP = [settings objectForKey:@"REMOTE-IP"];
- NSString *strRemotePort = [settings objectForKey:@"REMOTE-PORT"];
- NSString *strVideoURL = [settings objectForKey:@"VIDEO-URL"];
- NSString *strVideoPort = [settings objectForKey:@"VIDEO-PORT"];
- NSString *strVideoUser = [settings objectForKey:@"VIDEO-USER"];
- NSString *strVideoPass = [settings objectForKey:@"VIDEO-PASS"];
- if (strServerIP == nil || strServerPort == nil || strRemoteIP == nil || strRemotePort==nil || strVideoURL == nil || strVideoPort == nil || strVideoUser == nil || strVideoPass==nil){
- NSLog(@"Faltan Settings");
- UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Algunas de las configuraciones hacen falta, para el buen funcionamiento de la aplicacion, rellene los campos faltantes en la parte de Configuracion" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
- //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
- //[myTextField setBackgroundColor:[UIColor whiteColor]];
- //[myAlertView addSubview:testTextField];
- [myAlertView show];
- [myAlertView release];
- }
- //*/
- }
- - (NSString *)getStringSetting:(NSString *)key{
- NSString *setting = [settings objectForKey:key];
- return setting;
- }
- - (BOOL) getBoolSetting:(NSString *)key{
- BOOL setting = [[settings valueForKey:key] boolValue];
- return setting;
- }
- - (int) getIntegerSetting:(NSString *)key{
- int setting = [[settings valueForKey:key] intValue];
- return setting;
- }
- - (void)setValue:(id)value forKey:(NSString *)key{
- NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
- NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
- NSLog(@"Inicio de guardar valor %@,%@",value,key);
- [settings setValue:value forKey:key];
- NSLog(@"escribir %@",filePath);
- [settings writeToFile:filePath atomically:YES];
- NSLog(@"Fin Guardar");
- }
- - (void)setBool:(int)value forKey:(NSString *)key{
- NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
- NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
- NSLog(@"Inicio de guardar valor %d,%@",value,key);
- [settings setValue:[NSNumber numberWithInt:value] forKey:key];
- //NSLog(@"escribir %@",filePath);
- [settings writeToFile:filePath atomically:YES];//*/
- NSLog(@"Fin Guardar");
- }
- - (void)sendString:(NSString *)cadena
- {
- //NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
- BOOL demoMode = [self getBoolSetting:@"DEMO-MODE"];
- NSLog(@"Demo: %d",demoMode);
- if (!demoMode){
- @try{
- Socket *socket = [Socket socket];
- [socket connectToHostName:[self getStringSetting:@"SERVER-IP"] port:[self getIntegerSetting:@"SERVER-PORT"]];
- [socket writeString:cadena];
- NSLog(@"Envio la cadena %@\n",cadena);
- NSMutableData *response = [NSMutableData data];
- while ([socket readData:response] <= 0) {
- /* each read appends to data,
- returns number of bytes read or 0 on EOF*/
- }
- [[NSFileHandle fileHandleWithStandardOutput] writeData:response];
- NSString *respuesta = [[NSString alloc] initWithData:response encoding:4];
- NSLog(@"Recibo respuesta: %@\n", respuesta);
- [socket release];
- }
- @catch (NSException *exeption){
- NSLog(@"Error al intentar la conexion");
- UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Hubo un error al intentar conectarse al servidor" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
- //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
- //[myTextField setBackgroundColor:[UIColor whiteColor]];
- //[myAlertView addSubview:testTextField];
- [myAlertView show];
- [myAlertView release];
- }
- }
- }
- - (NSString *) MD5fromString:(NSString *)string{
- NSString *md5 = @"";
- const char *cStr = [string UTF8String];
- unsigned char result[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, strlen(cStr), result );
- md5 = [NSString stringWithFormat:
- @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
- result[0], result[1], result[2], result[3], result[4],
- result[5], result[6], result[7],
- result[8], result[9], result[10], result[11], result[12],
- result[13], result[14], result[15]
- ];
- return md5;
- }
- @end
Add Comment
Please, Sign In to add comment