From 0daa7a1cf98c6281dd7ba09baf0bf36cd1e154f7 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Sat, 5 Jan 2013 17:42:57 +0100 Subject: [PATCH] Add a minimal JubChatClient class and refactor other code to use it --- src/core/JubChatClient.h | 19 ++++++++++++ src/core/JubChatClient.m | 57 ++++++++++++++++++++++++++++++++++++ src/core/Makefile | 1 + src/core/main.m | 44 ++++++---------------------- src/gui/common/JubUI.h | 4 ++- src/gui/gtk/JubGtkRosterUI.h | 4 ++- src/gui/gtk/JubGtkRosterUI.m | 7 +++-- src/gui/gtk/JubGtkUI.h | 2 -- src/gui/gtk/JubGtkUI.m | 9 ++---- src/gui/gtk/Makefile | 4 +-- 10 files changed, 101 insertions(+), 50 deletions(-) create mode 100644 src/core/JubChatClient.h create mode 100644 src/core/JubChatClient.m diff --git a/src/core/JubChatClient.h b/src/core/JubChatClient.h new file mode 100644 index 0000000..bd418f4 --- /dev/null +++ b/src/core/JubChatClient.h @@ -0,0 +1,19 @@ +#import +#import + +#import "JubUI.h" +#import "JubConfig.h" + +@interface JubChatClient : OFObject +{ + XMPPConnection *connection; + XMPPRoster *roster; + XMPPStreamManagement *streamManagement; + id ui; +} +@property (readonly) XMPPConnection *connection; +@property (readonly) XMPPRoster *roster; +@property (assign) id ui; + +- initWithConfig: (JubConfig*)config; +@end diff --git a/src/core/JubChatClient.m b/src/core/JubChatClient.m new file mode 100644 index 0000000..a31bdb5 --- /dev/null +++ b/src/core/JubChatClient.m @@ -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 diff --git a/src/core/Makefile b/src/core/Makefile index 9cf5923..34b97e1 100644 --- a/src/core/Makefile +++ b/src/core/Makefile @@ -1,5 +1,6 @@ STATIC_LIB_NOINST = core.a SRCS = main.m \ + JubChatClient.m \ JubConfig.m include ../../buildsys.mk diff --git a/src/core/main.m b/src/core/main.m index 9ec8495..659414b 100644 --- a/src/core/main.m +++ b/src/core/main.m @@ -3,12 +3,11 @@ #import "JubGtkUI.h" #import "JubConfig.h" +#import "JubChatClient.h" -@interface AppDelegate: OFObject - +@interface AppDelegate: OFObject { - XMPPConnection *connection; - XMPPRoster *roster; + JubChatClient *client; id ui; } @end @@ -18,44 +17,19 @@ OF_APPLICATION_DELEGATE(AppDelegate) @implementation AppDelegate - (void)applicationDidFinishLaunching { - id 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 { diff --git a/src/gui/common/JubUI.h b/src/gui/common/JubUI.h index 2ec2cf7..05cacc1 100644 --- a/src/gui/common/JubUI.h +++ b/src/gui/common/JubUI.h @@ -1,6 +1,8 @@ #import +@class JubChatClient; + @protocol JubUI +- initWithClient: (JubChatClient*)client; - (void)startUIThread; -- (id)rosterDelegate; @end diff --git a/src/gui/gtk/JubGtkRosterUI.h b/src/gui/gtk/JubGtkRosterUI.h index 5aa41b3..cfcdd47 100644 --- a/src/gui/gtk/JubGtkRosterUI.h +++ b/src/gui/gtk/JubGtkRosterUI.h @@ -2,6 +2,8 @@ #import #include +#import "JubChatClient.h" + @class JubGtkChatUI; @interface JubGtkRosterUI: OFObject @@ -16,6 +18,6 @@ XMPPConnection *connection; } -- initWithConnection: (XMPPConnection*)connection; +- initWithClient: (JubChatClient*)client; - (JubGtkChatUI*)chatForJID: (XMPPJID*)jid; @end diff --git a/src/gui/gtk/JubGtkRosterUI.m b/src/gui/gtk/JubGtkRosterUI.m index b3899e3..c27142f 100644 --- a/src/gui/gtk/JubGtkRosterUI.m +++ b/src/gui/gtk/JubGtkRosterUI.m @@ -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); diff --git a/src/gui/gtk/JubGtkUI.h b/src/gui/gtk/JubGtkUI.h index afec54e..acf813a 100644 --- a/src/gui/gtk/JubGtkUI.h +++ b/src/gui/gtk/JubGtkUI.h @@ -10,6 +10,4 @@ { JubGtkRosterUI *rosterUI; } - -- initWithConnection: (XMPPConnection*)connection; @end diff --git a/src/gui/gtk/JubGtkUI.m b/src/gui/gtk/JubGtkUI.m index b6110f4..b70e3df 100644 --- a/src/gui/gtk/JubGtkUI.m +++ b/src/gui/gtk/JubGtkUI.m @@ -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)rosterDelegate -{ - return rosterUI; -} @end diff --git a/src/gui/gtk/Makefile b/src/gui/gtk/Makefile index 56c27e5..90ff0ca 100644 --- a/src/gui/gtk/Makefile +++ b/src/gui/gtk/Makefile @@ -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 -- 2.39.2