]> git.babelmonkeys.de Git - jubjub.git/commitdiff
Add basic config file support
authorFlorian Zeitz <florob@babelmonkeys.de>
Thu, 3 Jan 2013 22:46:35 +0000 (23:46 +0100)
committerFlorian Zeitz <florob@babelmonkeys.de>
Thu, 3 Jan 2013 22:46:35 +0000 (23:46 +0100)
config.xml [new file with mode: 0644]
src/core/JubConfig.h [new file with mode: 0644]
src/core/JubConfig.m [new file with mode: 0644]
src/core/Makefile
src/core/main.m

diff --git a/config.xml b/config.xml
new file mode 100644 (file)
index 0000000..714054e
--- /dev/null
@@ -0,0 +1,6 @@
+<config xmlns="http://babelmonkeys.de/jubjub/config">
+       <domain>localhost</domain>
+       <!--<server>127.0.0.1</server>-->
+       <username>alice</username>
+       <password>test</password>
+</config>
diff --git a/src/core/JubConfig.h b/src/core/JubConfig.h
new file mode 100644 (file)
index 0000000..00173fa
--- /dev/null
@@ -0,0 +1,16 @@
+#import <ObjFW/ObjFW.h>
+
+#define CONFIG_NS @"http://babelmonkeys.de/jubjub/config"
+
+@interface JubConfig : OFObject <OFXMLElementBuilderDelegate>
+{
+       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 (file)
index 0000000..33fe2c5
--- /dev/null
@@ -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
index 2bfacde5ffea25da97192b3b86f7122cb95e5133..9cf5923818c3dd005dd52e11b9bd028644ba07d3 100644 (file)
@@ -1,5 +1,6 @@
 STATIC_LIB_NOINST = core.a
-SRCS = main.m
+SRCS = main.m          \
+       JubConfig.m
 
 include ../../buildsys.mk
 
index c01a5ccecfe3da16797eff965ab93177e74e0f93..9ec84951de2042dbace938b3e7411768e11e2efe 100644 (file)
@@ -2,6 +2,7 @@
 #import <ObjXMPP/ObjXMPP.h>
 
 #import "JubGtkUI.h"
+#import "JubConfig.h"
 
 @interface AppDelegate: OFObject
     <OFApplicationDelegate, XMPPConnectionDelegate, XMPPRosterDelegate>
@@ -18,13 +19,15 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 - (void)applicationDidFinishLaunching
 {
        id<XMPPRosterDelegate, XMPPConnectionDelegate> 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];