From 68a17f7fc1f5fe8c2fee68ff35e672af4fd1e2f2 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Thu, 3 Jan 2013 23:46:35 +0100 Subject: [PATCH] Add basic config file support --- config.xml | 6 +++++ src/core/JubConfig.h | 16 +++++++++++++ src/core/JubConfig.m | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/core/Makefile | 3 ++- src/core/main.m | 9 ++++--- 5 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 config.xml create mode 100644 src/core/JubConfig.h create mode 100644 src/core/JubConfig.m diff --git a/config.xml b/config.xml new file mode 100644 index 0000000..714054e --- /dev/null +++ b/config.xml @@ -0,0 +1,6 @@ + + localhost + + alice + test + diff --git a/src/core/JubConfig.h b/src/core/JubConfig.h new file mode 100644 index 0000000..00173fa --- /dev/null +++ b/src/core/JubConfig.h @@ -0,0 +1,16 @@ +#import + +#define CONFIG_NS @"http://babelmonkeys.de/jubjub/config" + +@interface JubConfig : OFObject +{ + OFString *domain, *server; + OFString *username; + OFString *password; +} +@property (readonly) OFString *domain, *server; +@property (readonly) OFString *username; +@property (readonly) OFString *password; + +- initWithFile: (OFString*)file; +@end diff --git a/src/core/JubConfig.m b/src/core/JubConfig.m new file mode 100644 index 0000000..33fe2c5 --- /dev/null +++ b/src/core/JubConfig.m @@ -0,0 +1,56 @@ +#import "JubConfig.h" + +@implementation JubConfig +@synthesize domain, server; +@synthesize username; +@synthesize password; + +- initWithFile: (OFString*)file +{ + self = [super init]; + + @try { + OFAutoreleasePool *pool = [OFAutoreleasePool new]; + OFXMLParser *parser = [OFXMLParser parser]; + OFXMLElementBuilder *builder = + [OFXMLElementBuilder elementBuilder]; + + parser.delegate = builder; + builder.delegate = self; + + [parser parseFile: file]; + + [pool release]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [domain release]; + [server release]; + [username release]; + [password release]; + + [super dealloc]; +} + +- (void)elementBuilder: (OFXMLElementBuilder*)builder + didBuildElement: (OFXMLElement*)element +{ + // TODO: At error handling for missing elements + of_log(@"Parsed file: %@", element); + domain = [[[element elementForName: @"domain" + namespace: CONFIG_NS] stringValue] copy]; + server = [[[element elementForName: @"server" + namespace: CONFIG_NS] stringValue] copy]; + username = [[[element elementForName: @"username" + namespace: CONFIG_NS] stringValue] copy]; + password = [[[element elementForName: @"password" + namespace: CONFIG_NS] stringValue] copy]; +} +@end diff --git a/src/core/Makefile b/src/core/Makefile index 2bfacde..9cf5923 100644 --- a/src/core/Makefile +++ b/src/core/Makefile @@ -1,5 +1,6 @@ STATIC_LIB_NOINST = core.a -SRCS = main.m +SRCS = main.m \ + JubConfig.m include ../../buildsys.mk diff --git a/src/core/main.m b/src/core/main.m index c01a5cc..9ec8495 100644 --- a/src/core/main.m +++ b/src/core/main.m @@ -2,6 +2,7 @@ #import #import "JubGtkUI.h" +#import "JubConfig.h" @interface AppDelegate: OFObject @@ -18,13 +19,15 @@ OF_APPLICATION_DELEGATE(AppDelegate) - (void)applicationDidFinishLaunching { id rosterDelegate; + JubConfig *config = [[JubConfig alloc] initWithFile: @"config.xml"]; connection = [[XMPPConnection alloc] init]; [connection addDelegate: self]; - connection.domain = @"localhost"; - connection.username = @"alice"; - connection.password = @"test"; + connection.domain = config.domain; + connection.server = config.server; + connection.username = config.username; + connection.password = config.password; ui = [[JubGtkUI alloc] initWithConnection: connection]; rosterDelegate = [ui rosterDelegate]; -- 2.39.5