]> cgit.babelmonkeys.de Git - jubjub.git/commitdiff
Add a minimal JubChatClient class and refactor other code to use it
authorFlorian Zeitz <florob@babelmonkeys.de>
Sat, 5 Jan 2013 16:42:57 +0000 (17:42 +0100)
committerFlorian Zeitz <florob@babelmonkeys.de>
Sat, 5 Jan 2013 16:42:57 +0000 (17:42 +0100)
src/core/JubChatClient.h [new file with mode: 0644]
src/core/JubChatClient.m [new file with mode: 0644]
src/core/Makefile
src/core/main.m
src/gui/common/JubUI.h
src/gui/gtk/JubGtkRosterUI.h
src/gui/gtk/JubGtkRosterUI.m
src/gui/gtk/JubGtkUI.h
src/gui/gtk/JubGtkUI.m
src/gui/gtk/Makefile

diff --git a/src/core/JubChatClient.h b/src/core/JubChatClient.h
new file mode 100644 (file)
index 0000000..bd418f4
--- /dev/null
@@ -0,0 +1,19 @@
+#import <ObjXMPP/ObjXMPP.h>
+#import <ObjFW/ObjFW.h>
+
+#import "JubUI.h"
+#import "JubConfig.h"
+
+@interface JubChatClient : OFObject <XMPPConnectionDelegate, XMPPRosterDelegate>
+{
+       XMPPConnection *connection;
+       XMPPRoster *roster;
+       XMPPStreamManagement *streamManagement;
+       id<JubUI> ui;
+}
+@property (readonly) XMPPConnection *connection;
+@property (readonly) XMPPRoster *roster;
+@property (assign) id<JubUI> ui;
+
+- initWithConfig: (JubConfig*)config;
+@end
diff --git a/src/core/JubChatClient.m b/src/core/JubChatClient.m
new file mode 100644 (file)
index 0000000..a31bdb5
--- /dev/null
@@ -0,0 +1,57 @@
+#import "JubChatClient.h"
+
+@implementation JubChatClient
+@synthesize connection;
+@synthesize roster;
+@synthesize ui;
+
+- initWithConfig: (JubConfig*)config
+{
+       self = [super init];
+
+       @try {
+               connection = [XMPPConnection new];
+
+               connection.username = config.username;
+               connection.domain = config.domain;
+               connection.server = config.server;
+               connection.password = config.password;
+               [connection addDelegate: self];
+
+               roster = [[XMPPRoster alloc] initWithConnection: connection];
+               [roster addDelegate: self];
+
+               streamManagement = [[XMPPStreamManagement alloc]
+                   initWithConnection: connection];
+
+               [connection asyncConnectAndHandle];
+       } @catch (id e) {
+               [self release];
+               @throw e;
+       }
+
+       return self;
+}
+
+- (void)dealloc
+{
+       [roster release];
+       [streamManagement release];
+       [connection release];
+
+       [super dealloc];
+}
+
+- (void)connection: (XMPPConnection*)conn_
+     wasBoundToJID: (XMPPJID*)jid
+{
+       of_log(@"Bound to JID: %@", [jid fullJID]);
+
+       [roster requestRoster];
+}
+
+- (void)rosterWasReceived: (XMPPRoster*)roster
+{
+       [connection sendStanza: [XMPPPresence presence]];
+}
+@end
index 9cf5923818c3dd005dd52e11b9bd028644ba07d3..34b97e15e41702caba4effdb27891c3b7f0ac6e5 100644 (file)
@@ -1,5 +1,6 @@
 STATIC_LIB_NOINST = core.a
 SRCS = main.m          \
+       JubChatClient.m \
        JubConfig.m
 
 include ../../buildsys.mk
index 9ec84951de2042dbace938b3e7411768e11e2efe..659414bf349d4d2e886d0eb793b2956cbac51668 100644 (file)
@@ -3,12 +3,11 @@
 
 #import "JubGtkUI.h"
 #import "JubConfig.h"
+#import "JubChatClient.h"
 
-@interface AppDelegate: OFObject
-    <OFApplicationDelegate, XMPPConnectionDelegate, XMPPRosterDelegate>
+@interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
 {
-       XMPPConnection *connection;
-       XMPPRoster *roster;
+       JubChatClient *client;
        id<JubUI> ui;
 }
 @end
@@ -18,44 +17,19 @@ OF_APPLICATION_DELEGATE(AppDelegate)
 @implementation AppDelegate
 - (void)applicationDidFinishLaunching
 {
-       id<XMPPRosterDelegate, XMPPConnectionDelegate> rosterDelegate;
-       JubConfig *config = [[JubConfig alloc] initWithFile: @"config.xml"];
+       JubConfig *config = [[[JubConfig alloc] initWithFile: @"config.xml"]
+           autorelease];
 
-       connection = [[XMPPConnection alloc] init];
-       [connection addDelegate: self];
+       client = [[JubChatClient alloc] initWithConfig: config];
 
-       connection.domain = config.domain;
-       connection.server = config.server;
-       connection.username = config.username;
-       connection.password = config.password;
+       ui = [[JubGtkUI alloc] initWithClient: client];
 
-       ui = [[JubGtkUI alloc] initWithConnection: connection];
-       rosterDelegate = [ui rosterDelegate];
-
-       [connection addDelegate: rosterDelegate];
-
-       roster = [[XMPPRoster alloc] initWithConnection: connection];
-       [roster addDelegate: rosterDelegate];
-       [roster addDelegate: self];
-
-       [connection asyncConnectAndHandle];
+       client.ui = ui;
+       [client.connection addDelegate: self];
 
        [ui startUIThread];
 }
 
-- (void)connection: (XMPPConnection*)conn_
-     wasBoundToJID: (XMPPJID*)jid
-{
-       of_log(@"Bound to JID: %@", [jid fullJID]);
-
-       [roster requestRoster];
-}
-
-- (void)rosterWasReceived: (XMPPRoster*)roster
-{
-       [connection sendStanza: [XMPPPresence presence]];
-}
-
 -  (void)connection: (XMPPConnection*)conn
   didReceiveElement: (OFXMLElement*)element
 {
index 2ec2cf717cad1aade76c43cec3b2f6262cc14c0f..05cacc1046f42066211a24b6344e483761c84dbd 100644 (file)
@@ -1,6 +1,8 @@
 #import <ObjXMPP/XMPPRoster.h>
 
+@class JubChatClient;
+
 @protocol JubUI
+- initWithClient: (JubChatClient*)client;
 - (void)startUIThread;
-- (id<XMPPRosterDelegate, XMPPConnectionDelegate>)rosterDelegate;
 @end
index 5aa41b30c461542f2453770c24608b47623719d8..cfcdd471f6cdc54bb714d968712d10a573fa830e 100644 (file)
@@ -2,6 +2,8 @@
 #import <ObjXMPP/ObjXMPP.h>
 #include <gtk/gtk.h>
 
+#import "JubChatClient.h"
+
 @class JubGtkChatUI;
 
 @interface JubGtkRosterUI: OFObject <XMPPRosterDelegate, XMPPConnectionDelegate>
@@ -16,6 +18,6 @@
        XMPPConnection *connection;
 }
 
-- initWithConnection: (XMPPConnection*)connection;
+- initWithClient: (JubChatClient*)client;
 - (JubGtkChatUI*)chatForJID: (XMPPJID*)jid;
 @end
index b3899e34a1d750edf1e57d0d6961d4a8f4baf326..c27142f2f68fa0bf4b697ae8c121fae2cbd3e704 100644 (file)
@@ -80,7 +80,7 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
 }
 
 @implementation JubGtkRosterUI
-- initWithConnection: (XMPPConnection*)connection_
+- initWithClient: (JubChatClient*)client;
 {
        self = [super init];
 
@@ -95,7 +95,10 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                contactMap = [[OFMutableDictionary alloc] init];
                chatMap = [[OFMutableDictionary alloc] init];
                presences = [[OFCountedSet alloc] init];
-               connection = [connection_ retain];
+               connection = [client.connection retain];
+
+               [connection addDelegate: self];
+               [client.roster addDelegate: self];
 
                builder = gtk_builder_new();
                gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
index afec54e2d1545dd7df4ba5f32f29bfd77cf4b83f..acf813a6c54901e90590b1f76b91113995f99065 100644 (file)
@@ -10,6 +10,4 @@
 {
        JubGtkRosterUI *rosterUI;
 }
-
-- initWithConnection: (XMPPConnection*)connection;
 @end
index b6110f4ea6d0d45912ac23e196b50c4b1db5446a..b70e3df923c0ea3c47fc5e3972bebf169de540bc 100644 (file)
@@ -10,7 +10,7 @@ void on_roster_window_destroy(GObject *object, gpointer user_data)
 }
 
 @implementation JubGtkUI
-- initWithConnection: (XMPPConnection*)connection
+- initWithClient: (JubChatClient*)client;
 {
        self = [super init];
 
@@ -24,7 +24,7 @@ void on_roster_window_destroy(GObject *object, gpointer user_data)
                gtk_init(argc, argv);
 
                rosterUI = [[JubGtkRosterUI alloc]
-                   initWithConnection: connection];
+                   initWithClient: client];
        } @catch (id e) {
                [self release];
                @throw e;
@@ -49,9 +49,4 @@ void on_roster_window_destroy(GObject *object, gpointer user_data)
                return nil;
        }] start];
 }
-
-- (id<XMPPRosterDelegate>)rosterDelegate
-{
-       return rosterUI;
-}
 @end
index 56c27e5732644c71274e366303b1b90d912cad52..90ff0ca2c2fee4502589442a8acc4eebccea9cf1 100644 (file)
@@ -2,9 +2,9 @@ STATIC_LIB_NOINST = gtk.a
 SRCS = JubGtkUI.m      \
        JubGtkChatUI.m   \
        JubGtkRosterUI.m \
-       JubGtkHelper.m \
+       JubGtkHelper.m   \
        JubGObjectMap.m
 
 include ../../../buildsys.mk
 
-CPPFLAGS += -I../common
+CPPFLAGS += -I../common -I../../core