summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-06-01 08:48:42 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-06-01 09:24:28 (GMT)
commit2812bddd81f8a1b804c7460f4e14cd0aa393d129 (patch)
treed46294f35150c4f0f43deaf2d31fceaf945ae715 /Swiften/Controllers
downloadswift-contrib-2812bddd81f8a1b804c7460f4e14cd0aa393d129.zip
swift-contrib-2812bddd81f8a1b804c7460f4e14cd0aa393d129.tar.bz2
Import.
Diffstat (limited to 'Swiften/Controllers')
-rw-r--r--Swiften/Controllers/ChatController.cpp38
-rw-r--r--Swiften/Controllers/ChatController.h24
-rw-r--r--Swiften/Controllers/ChatControllerBase.cpp171
-rw-r--r--Swiften/Controllers/ChatControllerBase.h57
-rw-r--r--Swiften/Controllers/ChatWindow.h37
-rw-r--r--Swiften/Controllers/ChatWindowFactory.h20
-rw-r--r--Swiften/Controllers/EventController.cpp21
-rw-r--r--Swiften/Controllers/EventController.h24
-rw-r--r--Swiften/Controllers/LoginWindow.h22
-rw-r--r--Swiften/Controllers/LoginWindowFactory.h22
-rw-r--r--Swiften/Controllers/MUCController.cpp76
-rw-r--r--Swiften/Controllers/MUCController.h43
-rw-r--r--Swiften/Controllers/MainController.cpp259
-rw-r--r--Swiften/Controllers/MainController.h85
-rw-r--r--Swiften/Controllers/MainWindow.h26
-rw-r--r--Swiften/Controllers/MainWindowFactory.h20
-rw-r--r--Swiften/Controllers/Makefile.inc11
-rw-r--r--Swiften/Controllers/NickResolver.cpp22
-rw-r--r--Swiften/Controllers/NickResolver.h24
-rw-r--r--Swiften/Controllers/RosterController.cpp83
-rw-r--r--Swiften/Controllers/RosterController.h48
-rw-r--r--Swiften/Controllers/UnitTest/Makefile.inc4
-rw-r--r--Swiften/Controllers/UnitTest/NickResolverTest.cpp62
-rw-r--r--Swiften/Controllers/UnitTest/XMPPRosterControllerTest.cpp83
-rw-r--r--Swiften/Controllers/XMPPRosterController.cpp54
-rw-r--r--Swiften/Controllers/XMPPRosterController.h28
26 files changed, 1364 insertions, 0 deletions
diff --git a/Swiften/Controllers/ChatController.cpp b/Swiften/Controllers/ChatController.cpp
new file mode 100644
index 0000000..bac73d4
--- /dev/null
+++ b/Swiften/Controllers/ChatController.cpp
@@ -0,0 +1,38 @@
+#include "Swiften/Controllers/ChatController.h"
+
+#include "Swiften/Controllers/ChatWindow.h"
+#include "Swiften/Controllers/ChatWindowFactory.h"
+#include "Swiften/Controllers/NickResolver.h"
+
+namespace Swift {
+
+/**
+ * The controller does not gain ownership of the stanzaChannel, nor the factory.
+ */
+ChatController::ChatController(StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle)
+ : ChatControllerBase(stanzaChannel, iqRouter, chatWindowFactory, contact, presenceOracle) {
+ nickResolver_ = nickResolver;
+}
+
+bool ChatController::isIncomingMessageFromMe(boost::shared_ptr<Message>) {
+ return false;
+}
+
+void ChatController::preHandleIncomingMessage(boost::shared_ptr<Message> message) {
+ JID from = message->getFrom();
+ if (!from.equals(toJID_, JID::WithResource)) {
+ if (toJID_.equals(from, JID::WithoutResource) && toJID_.isBare()){
+ toJID_ = from;
+ }
+ }
+}
+
+void ChatController::postSendMessage(const String& body) {
+ chatWindow_->addMessage(body, "me", true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel() : boost::optional<SecurityLabel>());
+}
+
+String ChatController::senderDisplayNameFromMessage(JID from) {
+ return nickResolver_->jidToNick(from);
+}
+
+}
diff --git a/Swiften/Controllers/ChatController.h b/Swiften/Controllers/ChatController.h
new file mode 100644
index 0000000..314bd70
--- /dev/null
+++ b/Swiften/Controllers/ChatController.h
@@ -0,0 +1,24 @@
+#ifndef SWIFTEN_ChatController_H
+#define SWIFTEN_ChatController_H
+
+#include "Swiften/Controllers/ChatControllerBase.h"
+
+namespace Swift {
+ class NickResolver;
+ class ChatController : public ChatControllerBase {
+ public:
+ ChatController(StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &contact, NickResolver* nickResolver, PresenceOracle* presenceOracle);
+ ~ChatController() {};
+ //boost::signal<void (const JID&, const JID&)> onJIDChanged;
+ protected:
+ bool isIncomingMessageFromMe(boost::shared_ptr<Message> message);
+ void postSendMessage(const String &body);
+ void preHandleIncomingMessage(boost::shared_ptr<Message> message);
+ String senderDisplayNameFromMessage(JID from);
+ private:
+ NickResolver* nickResolver_;
+ JID contact_;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/ChatControllerBase.cpp b/Swiften/Controllers/ChatControllerBase.cpp
new file mode 100644
index 0000000..5f8535e
--- /dev/null
+++ b/Swiften/Controllers/ChatControllerBase.cpp
@@ -0,0 +1,171 @@
+#include "Swiften/Controllers/ChatControllerBase.h"
+
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Client/StanzaChannel.h"
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Controllers/ChatWindow.h"
+#include "Swiften/Controllers/ChatWindowFactory.h"
+#include "Swiften/Queries/Requests/GetSecurityLabelsCatalogRequest.h"
+
+namespace Swift {
+
+ChatControllerBase::ChatControllerBase(StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &toJID, PresenceOracle* presenceOracle) : stanzaChannel_(stanzaChannel), iqRouter_(iqRouter), chatWindowFactory_(chatWindowFactory), toJID_(toJID), labelsEnabled_(false), presenceOracle_(presenceOracle) {
+ chatWindow_ = chatWindowFactory_->createChatWindow(toJID);
+ chatWindow_->onAllMessagesRead.connect(boost::bind(&ChatControllerBase::handleAllMessagesRead, this));
+ chatWindow_->onSendMessageRequest.connect(boost::bind(&ChatControllerBase::handleSendMessageRequest, this, _1));
+ presenceOracle_->onPresenceChange.connect(boost::bind(&ChatControllerBase::handlePresenceChange, this, _1, _2));
+}
+
+ChatControllerBase::~ChatControllerBase() {
+ delete chatWindow_;
+}
+
+void ChatControllerBase::setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info) {
+ if (info->hasFeature(DiscoInfo::SecurityLabels)) {
+ chatWindow_->setSecurityLabelsEnabled(true);
+ chatWindow_->setSecurityLabelsError();
+ GetSecurityLabelsCatalogRequest* request = new GetSecurityLabelsCatalogRequest(JID(toJID_.toBare()), iqRouter_, Request::AutoDeleteAfterResponse);
+ request->onResponse.connect(boost::bind(&ChatControllerBase::handleSecurityLabelsCatalogResponse, this, _1, _2));
+ request->send();
+ labelsEnabled_ = true;
+ } else {
+ chatWindow_->setSecurityLabelsEnabled(false);
+ labelsEnabled_ = false;
+ }
+}
+
+String ChatControllerBase::getStatusChangeString(boost::shared_ptr<Presence> presence) {
+ String nick = senderDisplayNameFromMessage(presence->getFrom());
+ if (presence->getType() == Presence::Unavailable) {
+ return nick + " has gone offline.";
+ } else if (presence->getType() == Presence::Available) {
+ StatusShow::Type show = presence->getShow();
+ if (show == StatusShow::Online || show == StatusShow::FFC) {
+ return nick + " has become available.";
+ } else if (show == StatusShow::Away || show == StatusShow::XA) {
+ return nick + " has gone away.";
+ } else if (show == StatusShow::DND) {
+ return nick + " is now busy.";
+ }
+ }
+
+ return "";
+}
+
+void ChatControllerBase::handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> previousPresence) {
+ if (!(toJID_.isBare() && newPresence->getFrom().equals(toJID_, JID::WithoutResource)) && newPresence->getFrom() != toJID_) {
+ return;
+ }
+ String newStatusChangeString = getStatusChangeString(newPresence);
+ if (previousPresence.get() == NULL || newStatusChangeString != getStatusChangeString(previousPresence)) {
+ chatWindow_->addSystemMessage(newStatusChangeString);
+ }
+}
+
+void ChatControllerBase::handleAllMessagesRead() {
+ foreach (boost::shared_ptr<MessageEvent> messageEvent, unreadMessages_) {
+ messageEvent->read();
+ }
+ unreadMessages_.clear();
+ chatWindow_->setUnreadMessageCount(0);
+}
+
+void ChatControllerBase::handleSendMessageRequest(const String &body) {
+ if (body.isEmpty()) {
+ return;
+ }
+ boost::shared_ptr<Message> message(new Message());
+ message->setTo(toJID_);
+ message->setType(Swift::Message::Chat);
+ message->setBody(body);
+ boost::optional<SecurityLabel> label;
+ if (labelsEnabled_) {
+ message->addPayload(boost::shared_ptr<SecurityLabel>(new SecurityLabel(chatWindow_->getSelectedSecurityLabel())));
+ label = boost::optional<SecurityLabel>(chatWindow_->getSelectedSecurityLabel());
+ }
+ preSendMessageRequest(message);
+ stanzaChannel_->sendMessage(message);
+ postSendMessage(message->getBody());
+}
+
+void ChatControllerBase::handleSecurityLabelsCatalogResponse(boost::shared_ptr<SecurityLabelsCatalog> catalog, const boost::optional<Error>& error) {
+ if (!error) {
+ if (catalog->getLabels().size() == 0) {
+ chatWindow_->setSecurityLabelsEnabled(false);
+ labelsEnabled_ = false;
+ } else {
+ chatWindow_->setAvailableSecurityLabels(catalog->getLabels());
+ chatWindow_->setSecurityLabelsEnabled(true);
+ }
+ } else {
+ chatWindow_->setSecurityLabelsError();
+ }
+}
+
+void ChatControllerBase::showChatWindow() {
+ chatWindow_->show();
+}
+
+String ChatControllerBase::senderDisplayNameFromMessage(JID from) {
+ return from;
+}
+
+
+
+void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> messageEvent) {
+ unreadMessages_.push_back(messageEvent);
+ chatWindow_->setUnreadMessageCount(unreadMessages_.size());
+
+ boost::shared_ptr<Message> message = messageEvent->getStanza();
+ preHandleIncomingMessage(message);
+ String body = message->getBody();
+ if (message->isError()) {
+ String errorMessage = getErrorMessage(message->getPayload<Error>());
+ chatWindow_->addErrorMessage(errorMessage);
+ }
+ else {
+ showChatWindow();
+ boost::shared_ptr<SecurityLabel> label = message->getPayload<SecurityLabel>();
+ boost::optional<SecurityLabel> maybeLabel = label ? boost::optional<SecurityLabel>(*label) : boost::optional<SecurityLabel>();
+ JID from = message->getFrom();
+ chatWindow_->addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), maybeLabel);
+ }
+}
+
+String ChatControllerBase::getErrorMessage(boost::shared_ptr<Error> error) {
+ String defaultMessage = "Error sending message";
+ if (!error->getText().isEmpty()) {
+ return error->getText();
+ }
+ else {
+ switch (error->getCondition()) {
+ case Error::BadRequest: return defaultMessage; break;
+ case Error::Conflict: return defaultMessage; break;
+ case Error::FeatureNotImplemented: return defaultMessage; break;
+ case Error::Forbidden: return defaultMessage; break;
+ case Error::Gone: return "Recipient can no longer be contacted"; break;
+ case Error::InternalServerError: return "Internal server error"; break;
+ case Error::ItemNotFound: return defaultMessage; break;
+ case Error::JIDMalformed: return defaultMessage; break;
+ case Error::NotAcceptable: return "Message was rejected"; break;
+ case Error::NotAllowed: return defaultMessage; break;
+ case Error::NotAuthorized: return defaultMessage; break;
+ case Error::PaymentRequired: return defaultMessage; break;
+ case Error::RecipientUnavailable: return "Recipient is unavailable."; break;
+ case Error::Redirect: return defaultMessage; break;
+ case Error::RegistrationRequired: return defaultMessage; break;
+ case Error::RemoteServerNotFound: return "Recipient's server not found."; break;
+ case Error::RemoteServerTimeout: return defaultMessage; break;
+ case Error::ResourceConstraint: return defaultMessage; break;
+ case Error::ServiceUnavailable: return defaultMessage; break;
+ case Error::SubscriptionRequired: return defaultMessage; break;
+ case Error::UndefinedCondition: return defaultMessage; break;
+ case Error::UnexpectedRequest: return defaultMessage; break;
+ }
+ }
+ return defaultMessage;
+}
+
+}
diff --git a/Swiften/Controllers/ChatControllerBase.h b/Swiften/Controllers/ChatControllerBase.h
new file mode 100644
index 0000000..1967977
--- /dev/null
+++ b/Swiften/Controllers/ChatControllerBase.h
@@ -0,0 +1,57 @@
+#ifndef SWIFTEN_ChatControllerBase_H
+#define SWIFTEN_ChatControllerBase_H
+
+#include <map>
+#include <vector>
+#include <boost/shared_ptr.hpp>
+#include <boost/signals.hpp>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/DiscoInfo.h"
+#include "Swiften/Events/MessageEvent.h"
+#include "Swiften/JID/JID.h"
+#include "Swiften/Elements/SecurityLabelsCatalog.h"
+#include "Swiften/Elements/Error.h"
+#include "Swiften/Presence/PresenceOracle.h"
+
+namespace Swift {
+ class IQRouter;
+ class StanzaChannel;
+ class ChatWindow;
+ class ChatWindowFactory;
+
+ class ChatControllerBase {
+ public:
+ virtual ~ChatControllerBase();
+ void showChatWindow();
+ void setAvailableServerFeatures(boost::shared_ptr<DiscoInfo> info);
+ void handleIncomingMessage(boost::shared_ptr<MessageEvent> message);
+
+ protected:
+ ChatControllerBase(StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, const JID &toJID, PresenceOracle* presenceOracle);
+ virtual void postSendMessage(const String&) {};
+ virtual String senderDisplayNameFromMessage(JID from);
+ void handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> previousPresence);
+ virtual bool isIncomingMessageFromMe(boost::shared_ptr<Message>) = 0;
+ virtual void preHandleIncomingMessage(boost::shared_ptr<Message>) {};
+ virtual void preSendMessageRequest(boost::shared_ptr<Message>) {};
+
+ std::vector<boost::shared_ptr<MessageEvent> > unreadMessages_;
+ StanzaChannel* stanzaChannel_;
+ IQRouter* iqRouter_;
+ ChatWindowFactory* chatWindowFactory_;
+ ChatWindow* chatWindow_;
+ JID toJID_;
+ bool labelsEnabled_;
+ PresenceOracle* presenceOracle_;
+
+ private:
+ void handleSendMessageRequest(const String &body);
+ String getStatusChangeString(boost::shared_ptr<Presence> presence);
+ void handleAllMessagesRead();
+ void handleSecurityLabelsCatalogResponse(boost::shared_ptr<SecurityLabelsCatalog>, const boost::optional<Error>& error);
+ String getErrorMessage(boost::shared_ptr<Error>);
+ };
+}
+
+#endif
diff --git a/Swiften/Controllers/ChatWindow.h b/Swiften/Controllers/ChatWindow.h
new file mode 100644
index 0000000..04d0007
--- /dev/null
+++ b/Swiften/Controllers/ChatWindow.h
@@ -0,0 +1,37 @@
+#ifndef SWIFTEN_CHATWINDOW_H
+#define SWIFTEN_CHATWINDOW_H
+
+#include <boost/optional.hpp>
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+#include <vector>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/SecurityLabel.h"
+
+namespace Swift {
+ class TreeWidget;
+ class ChatWindow {
+ public:
+ virtual ~ChatWindow() {};
+
+ virtual void addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional<SecurityLabel>& label) = 0;
+ virtual void addSystemMessage(const String& message) = 0;
+ virtual void addErrorMessage(const String& message) = 0;
+
+ virtual void show() = 0;
+ virtual void setAvailableSecurityLabels(const std::vector<SecurityLabel>& labels) = 0;
+ virtual void setSecurityLabelsEnabled(bool enabled) = 0;
+ virtual void setUnreadMessageCount(int count) = 0;
+ virtual void convertToMUC() = 0;
+ virtual TreeWidget *getTreeWidget() = 0;
+ virtual void setSecurityLabelsError() = 0;
+ virtual SecurityLabel getSelectedSecurityLabel() = 0;
+
+ boost::signal<void ()> onClosed;
+ boost::signal<void ()> onAllMessagesRead;
+ boost::signal<void (const String&)> onSendMessageRequest;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/ChatWindowFactory.h b/Swiften/Controllers/ChatWindowFactory.h
new file mode 100644
index 0000000..c55ddba
--- /dev/null
+++ b/Swiften/Controllers/ChatWindowFactory.h
@@ -0,0 +1,20 @@
+#ifndef SWIFTEN_CHATWINDOWFACTORY_H
+#define SWIFTEN_CHATWINDOWFACTORY_H
+
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ class ChatWindow;
+
+ class ChatWindowFactory {
+ public:
+ virtual ~ChatWindowFactory() {};
+ /**
+ * Transfers ownership of result.
+ */
+ virtual ChatWindow* createChatWindow(const JID &contact) = 0;
+
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/EventController.cpp b/Swiften/Controllers/EventController.cpp
new file mode 100644
index 0000000..a87f79a
--- /dev/null
+++ b/Swiften/Controllers/EventController.cpp
@@ -0,0 +1,21 @@
+#include "Swiften/Controllers/EventController.h"
+
+#include <boost/bind.hpp>
+#include <algorithm>
+
+namespace Swift {
+
+void EventController::handleIncomingEvent(boost::shared_ptr<MessageEvent> event) {
+ if (event->isReadable()) {
+ events_.push_back(event);
+ event->onRead.connect(boost::bind(&EventController::handleEventRead, this, event));
+ onEventQueueLengthChange(events_.size());
+ }
+}
+
+void EventController::handleEventRead(boost::shared_ptr<MessageEvent> event) {
+ events_.erase(std::remove(events_.begin(), events_.end(), event), events_.end());
+ onEventQueueLengthChange(events_.size());
+}
+
+}
diff --git a/Swiften/Controllers/EventController.h b/Swiften/Controllers/EventController.h
new file mode 100644
index 0000000..ab161af
--- /dev/null
+++ b/Swiften/Controllers/EventController.h
@@ -0,0 +1,24 @@
+#ifndef SWIFTEN_EventController_H
+#define SWIFTEN_EventController_H
+
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+#include <vector>
+
+#include "Swiften/Events/MessageEvent.h"
+
+namespace Swift {
+ class EventController {
+ public:
+ void handleIncomingEvent(boost::shared_ptr<MessageEvent> event);
+ boost::signal<void (int)> onEventQueueLengthChange;
+
+ private:
+ void handleEventRead(boost::shared_ptr<MessageEvent> event);
+ std::vector<boost::shared_ptr<MessageEvent> > events_;
+ };
+}
+#endif
+
+
diff --git a/Swiften/Controllers/LoginWindow.h b/Swiften/Controllers/LoginWindow.h
new file mode 100644
index 0000000..44855c0
--- /dev/null
+++ b/Swiften/Controllers/LoginWindow.h
@@ -0,0 +1,22 @@
+#ifndef SWIFTEN_LoginWindow_H
+#define SWIFTEN_LoginWindow_H
+
+#include "Swiften/Base/String.h"
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+ class MainWindow;
+ class LoginWindow {
+ public:
+ virtual ~LoginWindow() {};
+ virtual void morphInto(MainWindow *mainWindow) = 0;
+ virtual void loggedOut() = 0;
+ virtual void setMessage(const String&) = 0;
+
+ boost::signal<void (const String&, const String&, const String& /* certificateFile */, bool)> onLoginRequest;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/LoginWindowFactory.h b/Swiften/Controllers/LoginWindowFactory.h
new file mode 100644
index 0000000..d52325e
--- /dev/null
+++ b/Swiften/Controllers/LoginWindowFactory.h
@@ -0,0 +1,22 @@
+#ifndef SWIFTEN_LoginWindowFactory_H
+#define SWIFTEN_LoginWindowFactory_H
+
+
+
+namespace Swift {
+ class LoginWindow;
+ class String;
+
+ class LoginWindowFactory {
+ public:
+ virtual ~LoginWindowFactory() {};
+
+ /**
+ * Transfers ownership of result.
+ */
+ virtual LoginWindow* createLoginWindow(const String& defaultJID, const String& defaultPassword, const String& defaultCertificate) = 0;
+
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/MUCController.cpp b/Swiften/Controllers/MUCController.cpp
new file mode 100644
index 0000000..8137fe1
--- /dev/null
+++ b/Swiften/Controllers/MUCController.cpp
@@ -0,0 +1,76 @@
+#include "Swiften/Controllers/MUCController.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Controllers/ChatWindow.h"
+#include "Swiften/Controllers/ChatWindowFactory.h"
+#include "Swiften/MUC/MUC.h"
+#include "Swiften/Client/StanzaChannel.h"
+#include "Swiften/Roster/Roster.h"
+#include "Swiften/Roster/SetPresence.h"
+#include "Swiften/Roster/TreeWidgetFactory.h"
+
+namespace Swift {
+
+/**
+ * The controller does not gain ownership of the stanzaChannel, nor the factory.
+ */
+MUCController::MUCController (
+ const JID &muc,
+ const String &nick,
+ StanzaChannel* stanzaChannel,
+ IQRouter* iqRouter,
+ ChatWindowFactory* chatWindowFactory,
+ TreeWidgetFactory *treeWidgetFactory,
+ PresenceOracle* presenceOracle) :
+ ChatControllerBase(stanzaChannel, iqRouter, chatWindowFactory, muc, presenceOracle),
+ muc_(new MUC(stanzaChannel, muc)),
+ nick_(nick),
+ treeWidgetFactory_(treeWidgetFactory) {
+ roster_ = new Roster(chatWindow_->getTreeWidget(), treeWidgetFactory_);
+ chatWindow_->onClosed.connect(boost::bind(&MUCController::handleWindowClosed, this));
+ muc_->joinAs(nick);
+ muc_->onOccupantJoined.connect(boost::bind(&MUCController::handleOccupantJoined, this, _1));
+ muc_->onOccupantPresenceChange.connect(boost::bind(&MUCController::handleOccupantPresenceChange, this, _1));
+ muc_->onOccupantLeft.connect(boost::bind(&MUCController::handleOccupantLeft, this, _1, _2, _3));
+ chatWindow_->convertToMUC();
+ chatWindow_->show();
+}
+
+MUCController::~MUCController() {
+ delete muc_;
+ //don't crash on exit by masking this. FIXME.
+ //delete roster_;
+}
+
+void MUCController::handleWindowClosed() {
+ muc_->part();
+}
+
+void MUCController::handleOccupantJoined(const MUCOccupant& occupant) {
+ roster_->addContact(JID(toJID_.getNode(), toJID_.getDomain(), occupant.getNick()), occupant.getNick(), "Occupants");
+}
+
+void MUCController::handleOccupantLeft(const MUCOccupant& occupant, MUC::LeavingType, const String& /*reason*/) {
+ roster_->removeContact(JID(toJID_.getNode(), toJID_.getDomain(), occupant.getNick()));
+}
+
+void MUCController::handleOccupantPresenceChange(boost::shared_ptr<Presence> presence) {
+ roster_->applyOnItems(SetPresence(presence, JID::WithResource));
+}
+
+bool MUCController::isIncomingMessageFromMe(boost::shared_ptr<Message> message) {
+ JID from = message->getFrom();
+ return nick_ == from.getResource();
+}
+
+String MUCController::senderDisplayNameFromMessage(JID from) {
+ return from.getResource();
+}
+
+void MUCController::preSendMessageRequest(boost::shared_ptr<Message> message) {
+ message->setType(Swift::Message::Groupchat);
+}
+
+}
diff --git a/Swiften/Controllers/MUCController.h b/Swiften/Controllers/MUCController.h
new file mode 100644
index 0000000..a7859cf
--- /dev/null
+++ b/Swiften/Controllers/MUCController.h
@@ -0,0 +1,43 @@
+#ifndef SWIFTEN_MUCController_H
+#define SWIFTEN_MUCController_H
+
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Controllers/ChatControllerBase.h"
+#include "Swiften/Elements/Message.h"
+#include "Swiften/Elements/DiscoInfo.h"
+#include "Swiften/JID/JID.h"
+#include "Swiften/MUC/MUC.h"
+#include "Swiften/MUC/MUCOccupant.h"
+
+namespace Swift {
+ class StanzaChannel;
+ class IQRouter;
+ class ChatWindow;
+ class ChatWindowFactory;
+ class Roster;
+ class TreeWidgetFactory;
+
+ class MUCController : public ChatControllerBase {
+ public:
+ MUCController(const JID &muc, const String &nick, StanzaChannel* stanzaChannel, IQRouter* iqRouter, ChatWindowFactory* chatWindowFactory, TreeWidgetFactory *treeWidgetFactory, PresenceOracle* presenceOracle);
+ ~MUCController();
+
+ protected:
+ void preSendMessageRequest(boost::shared_ptr<Message> message);
+ bool isIncomingMessageFromMe(boost::shared_ptr<Message> message);
+ String senderDisplayNameFromMessage(JID from);
+ private:
+ void handleWindowClosed();
+ void handleOccupantJoined(const MUCOccupant& occupant);
+ void handleOccupantLeft(const MUCOccupant& occupant, MUC::LeavingType type, const String& reason);
+ void handleOccupantPresenceChange(boost::shared_ptr<Presence> presence);
+ MUC *muc_;
+ String nick_;
+ TreeWidgetFactory *treeWidgetFactory_;
+ Roster *roster_;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/MainController.cpp b/Swiften/Controllers/MainController.cpp
new file mode 100644
index 0000000..e0d0fe7
--- /dev/null
+++ b/Swiften/Controllers/MainController.cpp
@@ -0,0 +1,259 @@
+#include "Swiften/Controllers/MainController.h"
+
+#include <boost/bind.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/shared_ptr.hpp>
+#include <stdlib.h>
+
+#include "Swiften/Application/Application.h"
+#include "Swiften/Application/ApplicationMessageDisplay.h"
+#include "Swiften/Controllers/ChatController.h"
+#include "Swiften/Controllers/ChatWindowFactory.h"
+#include "Swiften/Controllers/EventController.h"
+#include "Swiften/Controllers/LoginWindow.h"
+#include "Swiften/Controllers/LoginWindowFactory.h"
+#include "Swiften/Controllers/MainWindow.h"
+#include "Swiften/Controllers/MainWindowFactory.h"
+#include "Swiften/Controllers/MUCController.h"
+#include "Swiften/Controllers/NickResolver.h"
+#include "Swiften/Controllers/RosterController.h"
+#include "Swiften/Controllers/XMPPRosterController.h"
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/Client/Client.h"
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/Roster/XMPPRoster.h"
+#include "Swiften/Queries/Responders/SoftwareVersionResponder.h"
+#include "Swiften/Roster/TreeWidgetFactory.h"
+#include "Swiften/Settings/SettingsProvider.h"
+#include "Swiften/Elements/DiscoInfo.h"
+#include "Swiften/Queries/Responders/DiscoInfoResponder.h"
+#include "Swiften/Disco/CapsInfoGenerator.h"
+#include "Swiften/Queries/Requests/GetDiscoInfoRequest.h"
+
+namespace Swift {
+
+static const String CLIENT_NAME = "Swift";
+static const String CLIENT_VERSION = "0.3";
+static const String CLIENT_NODE = "http://swift.im";
+
+typedef std::pair<JID, ChatController*> JIDChatControllerPair;
+typedef std::pair<JID, MUCController*> JIDMUCControllerPair;
+
+MainController::MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory *treeWidgetFactory, SettingsProvider *settings, Application* application)
+ : client_(NULL), chatWindowFactory_(chatWindowFactory), mainWindowFactory_(mainWindowFactory), loginWindowFactory_(loginWindowFactory), treeWidgetFactory_(treeWidgetFactory), settings_(settings),
+ xmppRosterController_(NULL), rosterController_(NULL), loginWindow_(NULL), clientVersionResponder_(NULL), nickResolver_(NULL), discoResponder_(NULL),
+ serverDiscoInfo_(new DiscoInfo()), presenceOracle_(NULL) {
+ application_ = application;
+ eventController_ = new EventController();
+ eventController_->onEventQueueLengthChange.connect(boost::bind(&MainController::handleEventQueueLengthChange, this, _1));
+ loginWindow_ = loginWindowFactory_->createLoginWindow(settings->getStringSetting("jid"), settings->getStringSetting("pass"), settings->getStringSetting("certificate"));
+ loginWindow_->onLoginRequest.connect(boost::bind(&MainController::handleLoginRequest, this, _1, _2, _3, _4));
+}
+
+MainController::~MainController() {
+ delete discoResponder_;
+ delete clientVersionResponder_;
+ delete xmppRosterController_;
+ delete rosterController_;
+ foreach (JIDChatControllerPair controllerPair, chatControllers_) {
+ delete controllerPair.second;
+ }
+ foreach (JIDMUCControllerPair controllerPair, mucControllers_) {
+ delete controllerPair.second;
+ }
+ delete presenceOracle_;
+ delete nickResolver_;
+ delete client_;
+}
+
+void MainController::handleConnected() {
+ delete presenceOracle_;
+ presenceOracle_ = new PresenceOracle(client_);
+
+ client_->onPresenceReceived.connect(boost::bind(&MainController::handleIncomingPresence, this, _1));
+
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+
+
+ delete nickResolver_;
+ nickResolver_ = new NickResolver(xmppRoster);
+
+ delete rosterController_;
+ rosterController_ = new RosterController(xmppRoster, mainWindowFactory_, treeWidgetFactory_);
+ rosterController_->onStartChatRequest.connect(boost::bind(&MainController::handleChatRequest, this, _1));
+ rosterController_->onJoinMUCRequest.connect(boost::bind(&MainController::handleJoinMUCRequest, this, _1, _2));
+ rosterController_->onChangeStatusRequest.connect(boost::bind(&MainController::handleChangeStatusRequest, this, _1, _2));
+
+ delete xmppRosterController_;
+ xmppRosterController_ = new XMPPRosterController(client_, xmppRoster);
+
+ delete clientVersionResponder_;
+ clientVersionResponder_ = new SoftwareVersionResponder(CLIENT_NAME, CLIENT_VERSION, client_);
+ loginWindow_->morphInto(rosterController_->getWindow());
+
+ DiscoInfo discoInfo;
+ discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc"));
+ capsInfo_ = boost::shared_ptr<CapsInfo>(new CapsInfo(CapsInfoGenerator(CLIENT_NODE).generateCapsInfo(discoInfo)));
+ discoResponder_ = new DiscoInfoResponder(client_);
+ discoResponder_->setDiscoInfo(discoInfo);
+ discoResponder_->setDiscoInfo(capsInfo_->getNode() + "#" + capsInfo_->getVersion(), discoInfo);
+
+ serverDiscoInfo_ = boost::shared_ptr<DiscoInfo>(new DiscoInfo());
+ GetDiscoInfoRequest* discoInfoRequest = new GetDiscoInfoRequest(JID(), client_, Request::AutoDeleteAfterResponse);
+ discoInfoRequest->onResponse.connect(boost::bind(&MainController::handleServerDiscoInfoResponse, this, _1, _2));
+ discoInfoRequest->send();
+
+ //Send presence last to catch all the incoming presences.
+ boost::shared_ptr<Presence> initialPresence(new Presence());
+ initialPresence->addPayload(capsInfo_);
+ client_->sendPresence(initialPresence);
+}
+
+void MainController::handleEventQueueLengthChange(int count) {
+ application_->getApplicationMessageDisplay()->setMessage(count == 0 ? "" : boost::lexical_cast<std::string>(count).c_str());
+}
+
+void MainController::handleChangeStatusRequest(StatusShow::Type show, const String &statusText) {
+ boost::shared_ptr<Presence> presence(new Presence());
+ presence->addPayload(capsInfo_);
+ presence->setShow(show);
+ presence->setStatus(statusText);
+ // FIXME: This is wrong. None doesn't mean unavailable
+ if (show == StatusShow::None) {
+ presence->setType(Presence::Unavailable);
+ }
+ client_->sendPresence(presence);
+ if (presence->getType() == Presence::Unavailable) {
+ logout();
+ }
+}
+
+void MainController::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
+ rosterController_->handleIncomingPresence(presence);
+}
+
+void MainController::handleLoginRequest(const String &username, const String &password, const String& certificateFile, bool remember) {
+ loginWindow_->setMessage("");
+
+ settings_->storeString("jid", username);
+ settings_->storeString("certificate", certificateFile);
+ settings_->storeString("pass", remember ? password : "");
+
+ delete client_;
+ client_ = new Swift::Client(JID(username), password);
+ if (!certificateFile.isEmpty()) {
+ client_->setCertificate(certificateFile);
+ }
+ client_->onError.connect(boost::bind(&MainController::handleError, this, _1));
+ client_->onConnected.connect(boost::bind(&MainController::handleConnected, this));
+ client_->onMessageReceived.connect(boost::bind(&MainController::handleIncomingMessage, this, _1));
+ client_->connect();
+}
+
+void MainController::handleError(const ClientError& error) {
+ String message;
+ switch(error.getType()) {
+ case ClientError::NoError: assert(false); break;
+ case ClientError::DomainNameResolveError: message = "Unable to find server"; break;
+ case ClientError::ConnectionError: message = "Error connecting to server"; break;
+ case ClientError::ConnectionReadError: message = "Error while receiving server data"; break;
+ case ClientError::XMLError: message = "Error parsing server data"; break;
+ case ClientError::AuthenticationFailedError: message = "Login/password invalid"; break;
+ case ClientError::NoSupportedAuthMechanismsError: message = "Authentication mechanisms not supported"; break;
+ case ClientError::UnexpectedElementError: message = "Unexpected response"; break;
+ case ClientError::ResourceBindError: message = "Error binding resource"; break;
+ case ClientError::SessionStartError: message = "Error starting session"; break;
+ case ClientError::TLSError: message = "Encryption error"; break;
+ case ClientError::ClientCertificateLoadError: message = "Error loading certificate (Invalid password?)"; break;
+ case ClientError::ClientCertificateError: message = "Certificate not authorized"; break;
+ }
+ loginWindow_->setMessage(message);
+ logout();
+}
+
+void MainController::logout() {
+ loginWindow_->loggedOut();
+
+ delete discoResponder_;
+ discoResponder_ = NULL;
+ delete clientVersionResponder_;
+ clientVersionResponder_ = NULL;
+ foreach (JIDChatControllerPair controllerPair, chatControllers_) {
+ delete controllerPair.second;
+ }
+ client_->disconnect();
+ chatControllers_.clear();
+ foreach (JIDMUCControllerPair controllerPair, mucControllers_) {
+ delete controllerPair.second;
+ }
+ mucControllers_.clear();
+}
+
+
+void MainController::handleChatRequest(const String &contact) {
+ getChatController(JID(contact))->showChatWindow();
+}
+
+ChatController* MainController::getChatController(const JID &contact) {
+ JID lookupContact(contact);
+ if (chatControllers_.find(lookupContact) == chatControllers_.end()) {
+ lookupContact = JID(contact.toBare());
+ }
+ if (chatControllers_.find(lookupContact) == chatControllers_.end()) {
+ chatControllers_[contact] = new ChatController(client_, client_, chatWindowFactory_, contact, nickResolver_, presenceOracle_);
+ chatControllers_[contact]->setAvailableServerFeatures(serverDiscoInfo_);
+ lookupContact = contact;
+ }
+ return chatControllers_[lookupContact];
+}
+
+void MainController::handleChatControllerJIDChanged(const JID& from, const JID& to) {
+ chatControllers_[to] = chatControllers_[from];
+ chatControllers_.erase(from);
+}
+
+void MainController::handleJoinMUCRequest(const JID &muc, const String &nick) {
+ mucControllers_[muc] = new MUCController(muc, nick, client_, client_, chatWindowFactory_, treeWidgetFactory_, presenceOracle_);
+ mucControllers_[muc]->setAvailableServerFeatures(serverDiscoInfo_);
+}
+
+void MainController::handleIncomingMessage(boost::shared_ptr<Message> message) {
+ JID jid = message->getFrom();
+ boost::shared_ptr<MessageEvent> event(new MessageEvent(message));
+
+ // Try to deliver it to a MUC
+ if (message->getType() == Message::Groupchat || message->getType() == Message::Error) {
+ std::map<JID, MUCController*>::iterator i = mucControllers_.find(jid.toBare());
+ if (i != mucControllers_.end()) {
+ i->second->handleIncomingMessage(event);
+ return;
+ }
+ else if (message->getType() == Message::Groupchat) {
+ //FIXME: Error handling - groupchat messages from an unknown muc.
+ return;
+ }
+ }
+
+ //if not a mucroom
+ eventController_->handleIncomingEvent(event);
+
+ // FIXME: This logic should go into a chat manager
+ if (event->isReadable()) {
+ getChatController(jid)->handleIncomingMessage(event);
+ }
+}
+
+void MainController::handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo> info, const boost::optional<Error>& error) {
+ if (!error) {
+ serverDiscoInfo_ = info;
+ foreach (JIDChatControllerPair pair, chatControllers_) {
+ pair.second->setAvailableServerFeatures(info);
+ }
+ foreach (JIDMUCControllerPair pair, mucControllers_) {
+ pair.second->setAvailableServerFeatures(info);
+ }
+ }
+}
+
+}
diff --git a/Swiften/Controllers/MainController.h b/Swiften/Controllers/MainController.h
new file mode 100644
index 0000000..e09d4fa
--- /dev/null
+++ b/Swiften/Controllers/MainController.h
@@ -0,0 +1,85 @@
+#ifndef SWIFTEN_MainController_H
+#define SWIFTEN_MainController_H
+
+
+#include "Swiften/Base/String.h"
+#include "Swiften/Client/ClientError.h"
+#include "Swiften/JID/JID.h"
+#include "Swiften/Elements/DiscoInfo.h"
+#include "Swiften/Elements/Error.h"
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/Elements/Message.h"
+#include "Swiften/Settings/SettingsProvider.h"
+#include "Swiften/Elements/CapsInfo.h"
+
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <vector>
+
+namespace Swift {
+ class Application;
+ class Client;
+ class ChatWindowFactory;
+ class ChatController;
+ class EventController;
+ class MainWindowFactory;
+ class MainWindow;
+ class NickResolver;
+ class RosterController;
+ class XMPPRosterController;
+ class DiscoInfoResponder;
+ class LoginWindow;
+ class EventLoop;
+ class SoftwareVersionResponder;
+ class LoginWindowFactory;
+ class TreeWidgetFactory;
+ class MUCController;
+ class PresenceOracle;
+
+ class MainController {
+ public:
+ MainController(ChatWindowFactory* chatWindowFactory, MainWindowFactory *mainWindowFactory, LoginWindowFactory *loginWindowFactory, TreeWidgetFactory* treeWidgetFactory, SettingsProvider *settings, Application* application);
+ ~MainController();
+
+
+ private:
+ void handleConnected();
+ void handleLoginRequest(const String& username, const String& password, const String& certificateFile, bool remember);
+ void handleChatRequest(const String& contact);
+ void handleJoinMUCRequest(const JID& muc, const String& nick);
+ void handleIncomingPresence(boost::shared_ptr<Presence> presence);
+ void handleChatControllerJIDChanged(const JID& from, const JID& to);
+ void handleIncomingMessage(boost::shared_ptr<Message> message);
+ void handleChangeStatusRequest(StatusShow::Type show, const String &statusText);
+ void handleError(const ClientError& error);
+ void handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo>, const boost::optional<Error>&);
+ void handleEventQueueLengthChange(int count);
+ ChatController* getChatController(const JID &contact);
+ void logout();
+
+ Client* client_;
+ ChatWindowFactory* chatWindowFactory_;
+ MainWindowFactory* mainWindowFactory_;
+ LoginWindowFactory* loginWindowFactory_;
+ TreeWidgetFactory* treeWidgetFactory_;
+ SettingsProvider *settings_;
+ Application* application_;
+ ChatController* chatController_;
+ XMPPRosterController* xmppRosterController_;
+ RosterController* rosterController_;
+ EventController* eventController_;
+ LoginWindow* loginWindow_;
+ SoftwareVersionResponder* clientVersionResponder_;
+ NickResolver* nickResolver_;
+ DiscoInfoResponder* discoResponder_;
+ boost::shared_ptr<CapsInfo> capsInfo_;
+ std::map<JID, MUCController*> mucControllers_;
+ std::map<JID, ChatController*> chatControllers_;
+ boost::shared_ptr<DiscoInfo> serverDiscoInfo_;
+ PresenceOracle* presenceOracle_;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/MainWindow.h b/Swiften/Controllers/MainWindow.h
new file mode 100644
index 0000000..081fe6e
--- /dev/null
+++ b/Swiften/Controllers/MainWindow.h
@@ -0,0 +1,26 @@
+#ifndef SWIFTEN_MainWindow_H
+#define SWIFTEN_MainWindow_H
+
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+#include "Swiften/Elements/StatusShow.h"
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+ class TreeWidget;
+
+ class MainWindow {
+ public:
+ virtual ~MainWindow() {};
+ virtual TreeWidget* getTreeWidget() = 0;
+
+ boost::signal<void (const JID&)> onStartChatRequest;
+ boost::signal<void (const JID&, const String&)> onJoinMUCRequest;
+ boost::signal<void (StatusShow::Type, const String&)> onChangeStatusRequest;
+ boost::signal<void (bool)> onShowOfflineToggled;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/MainWindowFactory.h b/Swiften/Controllers/MainWindowFactory.h
new file mode 100644
index 0000000..cf5a061
--- /dev/null
+++ b/Swiften/Controllers/MainWindowFactory.h
@@ -0,0 +1,20 @@
+#ifndef SWIFTEN_MainWindowFactory_H
+#define SWIFTEN_MainWindowFactory_H
+
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ class MainWindow;
+
+ class MainWindowFactory {
+ public:
+ virtual ~MainWindowFactory() {};
+ /**
+ * Transfers ownership of result.
+ */
+ virtual MainWindow* createMainWindow() = 0;
+
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/Makefile.inc b/Swiften/Controllers/Makefile.inc
new file mode 100644
index 0000000..c51b0f3
--- /dev/null
+++ b/Swiften/Controllers/Makefile.inc
@@ -0,0 +1,11 @@
+SWIFTEN_SOURCES += \
+ Swiften/Controllers/ChatController.cpp \
+ Swiften/Controllers/ChatControllerBase.cpp \
+ Swiften/Controllers/MainController.cpp \
+ Swiften/Controllers/NickResolver.cpp \
+ Swiften/Controllers/RosterController.cpp \
+ Swiften/Controllers/XMPPRosterController.cpp \
+ Swiften/Controllers/MUCController.cpp \
+ Swiften/Controllers/EventController.cpp
+
+include Swiften/Controllers/UnitTest/Makefile.inc
diff --git a/Swiften/Controllers/NickResolver.cpp b/Swiften/Controllers/NickResolver.cpp
new file mode 100644
index 0000000..1c51cb7
--- /dev/null
+++ b/Swiften/Controllers/NickResolver.cpp
@@ -0,0 +1,22 @@
+#include "Swiften/Controllers/NickResolver.h"
+
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Roster/XMPPRoster.h"
+
+namespace Swift {
+
+NickResolver::NickResolver(boost::shared_ptr<XMPPRoster> xmppRoster) {
+ xmppRoster_ = xmppRoster;
+}
+
+String NickResolver::jidToNick(const JID& jid) {
+ if (xmppRoster_->containsJID(jid)) {
+ return xmppRoster_->getNameForJID(jid);
+ }
+ std::map<JID, String>::iterator it = map_.find(jid);
+ return (it == map_.end()) ? jid.toBare() : it->second;
+}
+
+}
+
diff --git a/Swiften/Controllers/NickResolver.h b/Swiften/Controllers/NickResolver.h
new file mode 100644
index 0000000..b7dc005
--- /dev/null
+++ b/Swiften/Controllers/NickResolver.h
@@ -0,0 +1,24 @@
+#ifndef SWIFTEN_NickResolver_H
+#define SWIFTEN_NickResolver_H
+
+#include <map>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ class XMPPRoster;
+ class NickResolver {
+ public:
+ NickResolver(boost::shared_ptr<XMPPRoster> xmppRoster);
+ String jidToNick(const JID& jid);
+
+ private:
+ std::map<JID, String> map_;
+ boost::shared_ptr<XMPPRoster> xmppRoster_;
+ };
+}
+#endif
+
+
diff --git a/Swiften/Controllers/RosterController.cpp b/Swiften/Controllers/RosterController.cpp
new file mode 100644
index 0000000..1efbeea
--- /dev/null
+++ b/Swiften/Controllers/RosterController.cpp
@@ -0,0 +1,83 @@
+#include "Swiften/Controllers/RosterController.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Controllers/MainWindow.h"
+#include "Swiften/Controllers/MainWindowFactory.h"
+#include "Swiften/Queries/Requests/GetRosterRequest.h"
+#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/Roster/Roster.h"
+#include "Swiften/Roster/SetPresence.h"
+#include "Swiften/Roster/OfflineRosterFilter.h"
+#include "Swiften/Roster/OpenChatRosterAction.h"
+#include "Swiften/Roster/TreeWidgetFactory.h"
+#include "Swiften/Roster/XMPPRoster.h"
+
+namespace Swift {
+
+/**
+ * The controller does not gain ownership of these parameters.
+ */
+RosterController::RosterController(boost::shared_ptr<XMPPRoster> xmppRoster, MainWindowFactory* mainWindowFactory, TreeWidgetFactory* treeWidgetFactory)
+ : xmppRoster_(xmppRoster), mainWindowFactory_(mainWindowFactory), treeWidgetFactory_(treeWidgetFactory), mainWindow_(mainWindowFactory_->createMainWindow()), roster_(new Roster(mainWindow_->getTreeWidget(), treeWidgetFactory_)), offlineFilter_(new OfflineRosterFilter()) {
+ roster_->addFilter(offlineFilter_);
+ mainWindow_->onStartChatRequest.connect(boost::bind(&RosterController::handleStartChatRequest, this, _1));
+ mainWindow_->onJoinMUCRequest.connect(boost::bind(&RosterController::handleJoinMUCRequest, this, _1, _2));
+ mainWindow_->onChangeStatusRequest.connect(boost::bind(&RosterController::handleChangeStatusRequest, this, _1, _2));
+ mainWindow_->onShowOfflineToggled.connect(boost::bind(&RosterController::handleShowOfflineToggled, this, _1));
+ roster_->onUserAction.connect(boost::bind(&RosterController::handleUserAction, this, _1));
+ xmppRoster_->onJIDAdded.connect(boost::bind(&RosterController::handleOnJIDAdded, this, _1));
+}
+
+RosterController::~RosterController() {
+ delete offlineFilter_;
+
+}
+
+void RosterController::handleShowOfflineToggled(bool state) {
+ if (state) {
+ roster_->removeFilter(offlineFilter_);
+ } else {
+ roster_->addFilter(offlineFilter_);
+ }
+}
+
+void RosterController::handleChangeStatusRequest(StatusShow::Type show, const String &statusText) {
+ onChangeStatusRequest(show, statusText);
+}
+
+void RosterController::handleUserAction(boost::shared_ptr<UserRosterAction> action) {
+ boost::shared_ptr<OpenChatRosterAction> chatAction = boost::dynamic_pointer_cast<OpenChatRosterAction>(action);
+ if (chatAction.get() != NULL) {
+ ContactRosterItem *contactItem = dynamic_cast<ContactRosterItem*>(chatAction->getRosterItem());
+ assert(contactItem);
+ onStartChatRequest(contactItem->getJID().toBare());
+ }
+}
+
+void RosterController::handleOnJIDAdded(const JID& jid) {
+ std::vector<String> groups = xmppRoster_->getGroupsForJID(jid);
+ String name = xmppRoster_->getNameForJID(jid);
+ if (!groups.empty()) {
+ foreach(const String& group, groups) {
+ roster_->addContact(jid, name, group);
+ }
+ } else {
+ roster_->addContact(jid, name, "Contacts");
+ }
+}
+
+void RosterController::handleIncomingPresence(boost::shared_ptr<Presence> presence) {
+ roster_->applyOnItems(SetPresence(presence));
+}
+
+void RosterController::handleStartChatRequest(const JID& contact) {
+ onStartChatRequest(contact);
+}
+
+void RosterController::handleJoinMUCRequest(const JID &muc, const String &nick) {
+ onJoinMUCRequest(JID(muc), nick);
+}
+
+}
diff --git a/Swiften/Controllers/RosterController.h b/Swiften/Controllers/RosterController.h
new file mode 100644
index 0000000..945b068
--- /dev/null
+++ b/Swiften/Controllers/RosterController.h
@@ -0,0 +1,48 @@
+#ifndef SWIFTEN_RosterController_H
+#define SWIFTEN_RosterController_H
+
+#include "Swiften/JID/JID.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/Roster/UserRosterAction.h"
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+ class IQRouter;
+ class Roster;
+ class XMPPRoster;
+ class MainWindow;
+ class MainWindowFactory;
+ class TreeWidgetFactory;
+ class OfflineRosterFilter;
+
+ class RosterController {
+ public:
+ RosterController(boost::shared_ptr<XMPPRoster> xmppRoster, MainWindowFactory *mainWindowFactory, TreeWidgetFactory *treeWidgetFactory);
+ ~RosterController();
+ void showRosterWindow();
+ MainWindow* getWindow() {return mainWindow_;};
+ boost::signal<void (const JID&)> onStartChatRequest;
+ boost::signal<void (const JID&, const String&)> onJoinMUCRequest;
+ boost::signal<void (StatusShow::Type, const String&)> onChangeStatusRequest;
+ void handleIncomingPresence(boost::shared_ptr<Presence> presence);
+
+ private:
+ void handleOnJIDAdded(const JID &jid);
+ void handleStartChatRequest(const JID& contact);
+ void handleJoinMUCRequest(const JID &muc, const String &nick);
+ void handleUserAction(boost::shared_ptr<UserRosterAction> action);
+ void handleChangeStatusRequest(StatusShow::Type show, const String &statusText);
+ void handleShowOfflineToggled(bool state);
+ boost::shared_ptr<XMPPRoster> xmppRoster_;
+ MainWindowFactory* mainWindowFactory_;
+ TreeWidgetFactory* treeWidgetFactory_;
+ MainWindow* mainWindow_;
+ Roster* roster_;
+ OfflineRosterFilter* offlineFilter_;
+ };
+}
+#endif
+
diff --git a/Swiften/Controllers/UnitTest/Makefile.inc b/Swiften/Controllers/UnitTest/Makefile.inc
new file mode 100644
index 0000000..163da01
--- /dev/null
+++ b/Swiften/Controllers/UnitTest/Makefile.inc
@@ -0,0 +1,4 @@
+UNITTEST_SOURCES += \
+ Swiften/Controllers/UnitTest/NickResolverTest.cpp \
+ Swiften/Controllers/UnitTest/XMPPRosterControllerTest.cpp
+
diff --git a/Swiften/Controllers/UnitTest/NickResolverTest.cpp b/Swiften/Controllers/UnitTest/NickResolverTest.cpp
new file mode 100644
index 0000000..9c89d4d
--- /dev/null
+++ b/Swiften/Controllers/UnitTest/NickResolverTest.cpp
@@ -0,0 +1,62 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Controllers/NickResolver.h"
+#include "Swiften/Roster/XMPPRoster.h"
+
+using namespace Swift;
+
+class NickResolverTest : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(NickResolverTest);
+ CPPUNIT_TEST(testNoMatch);
+ CPPUNIT_TEST(testMatch);
+ CPPUNIT_TEST(testOverwrittenMatch);
+ CPPUNIT_TEST(testRemovedMatch);
+ CPPUNIT_TEST_SUITE_END();
+
+ std::vector<String> groups_;
+
+ public:
+ NickResolverTest() {}
+
+ void testNoMatch() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ NickResolver resolver(xmppRoster);
+ JID testling("foo@bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testling));
+ }
+
+ void testMatch() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ NickResolver resolver(xmppRoster);
+ JID testling("foo@bar/baz");
+ xmppRoster->addContact(testling, "Test", groups_);
+
+ CPPUNIT_ASSERT_EQUAL(String("Test"), resolver.jidToNick(testling));
+ }
+
+ void testOverwrittenMatch() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ NickResolver resolver(xmppRoster);
+ JID testling("foo@bar/baz");
+ xmppRoster->addContact(testling, "FailTest", groups_);
+ xmppRoster->addContact(testling, "Test", groups_);
+
+ CPPUNIT_ASSERT_EQUAL(String("Test"), resolver.jidToNick(testling));
+ }
+
+ void testRemovedMatch() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ NickResolver resolver(xmppRoster);
+ JID testling("foo@bar/baz");
+ xmppRoster->addContact(testling, "FailTest", groups_);
+ xmppRoster->removeContact(testling);
+ CPPUNIT_ASSERT_EQUAL(String("foo@bar"), resolver.jidToNick(testling));
+ }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(NickResolverTest);
+
diff --git a/Swiften/Controllers/UnitTest/XMPPRosterControllerTest.cpp b/Swiften/Controllers/UnitTest/XMPPRosterControllerTest.cpp
new file mode 100644
index 0000000..3bff8f3
--- /dev/null
+++ b/Swiften/Controllers/UnitTest/XMPPRosterControllerTest.cpp
@@ -0,0 +1,83 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Controllers/XMPPRosterController.h"
+#include "Swiften/Elements/Payload.h"
+#include "Swiften/Elements/RosterItemPayload.h"
+#include "Swiften/Elements/RosterPayload.h"
+#include "Swiften/Queries/DummyIQChannel.h"
+#include "Swiften/Queries/IQRouter.h"
+#include "Swiften/Roster/XMPPRoster.h"
+
+using namespace Swift;
+
+class XMPPRosterControllerTest : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(XMPPRosterControllerTest);
+ CPPUNIT_TEST(testAdd);
+ CPPUNIT_TEST(testModify);
+ CPPUNIT_TEST(testRemove);
+ CPPUNIT_TEST_SUITE_END();
+
+ DummyIQChannel* channel_;
+ IQRouter* router_;
+ public:
+ XMPPRosterControllerTest() : channel_(new DummyIQChannel()), router_(new IQRouter(channel_)) {}
+
+ ~XMPPRosterControllerTest() {
+ delete channel_;
+ delete router_;
+ }
+
+ void testAdd() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ XMPPRosterController controller(router_, xmppRoster);
+ JID testling("foo@bar");
+ CPPUNIT_ASSERT(!xmppRoster->containsJID(testling));
+ boost::shared_ptr<Payload> payload(new RosterPayload());
+ RosterItemPayload item(testling, "Bob", RosterItemPayload::Both);
+ dynamic_cast<RosterPayload*>(payload.get())->addItem(item);
+ controller.handleIQ(IQ::createRequest(IQ::Set, JID(), "eou", payload));
+ CPPUNIT_ASSERT(xmppRoster->containsJID(testling));
+ }
+
+ void testModify() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ XMPPRosterController controller(router_, xmppRoster);
+ JID testling("foo@bar");
+ CPPUNIT_ASSERT(!xmppRoster->containsJID(testling));
+ boost::shared_ptr<Payload> payload1(new RosterPayload());
+ RosterItemPayload item1(testling, "Bob", RosterItemPayload::Both);
+ dynamic_cast<RosterPayload*>(payload1.get())->addItem(item1);
+ controller.handleIQ(IQ::createRequest(IQ::Set, JID(), "eou", payload1));
+ CPPUNIT_ASSERT(xmppRoster->containsJID(testling));
+ CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster->getNameForJID(testling));
+ boost::shared_ptr<Payload> payload2(new RosterPayload());
+ RosterItemPayload item2(testling, "Bob2", RosterItemPayload::Both);
+ dynamic_cast<RosterPayload*>(payload2.get())->addItem(item2);
+ controller.handleIQ(IQ::createRequest(IQ::Set, JID(), "eou", payload2));
+ CPPUNIT_ASSERT_EQUAL(String("Bob2"), xmppRoster->getNameForJID(testling));
+ }
+
+ void testRemove() {
+ boost::shared_ptr<XMPPRoster> xmppRoster(new XMPPRoster());
+ XMPPRosterController controller(router_, xmppRoster);
+ JID testling("foo@bar");
+ CPPUNIT_ASSERT(!xmppRoster->containsJID(testling));
+ boost::shared_ptr<Payload> payload1(new RosterPayload());
+ RosterItemPayload item1(testling, "Bob", RosterItemPayload::Both);
+ dynamic_cast<RosterPayload*>(payload1.get())->addItem(item1);
+ controller.handleIQ(IQ::createRequest(IQ::Set, JID(), "eou", payload1));
+ CPPUNIT_ASSERT(xmppRoster->containsJID(testling));
+ boost::shared_ptr<Payload> payload2(new RosterPayload());
+ RosterItemPayload item2(testling, "Bob", RosterItemPayload::Remove);
+ dynamic_cast<RosterPayload*>(payload2.get())->addItem(item2);
+ controller.handleIQ(IQ::createRequest(IQ::Set, JID(), "eou", payload2));
+ CPPUNIT_ASSERT(!xmppRoster->containsJID(testling));
+ }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(XMPPRosterControllerTest);
+
+
diff --git a/Swiften/Controllers/XMPPRosterController.cpp b/Swiften/Controllers/XMPPRosterController.cpp
new file mode 100644
index 0000000..b6c36d7
--- /dev/null
+++ b/Swiften/Controllers/XMPPRosterController.cpp
@@ -0,0 +1,54 @@
+#include "Swiften/Controllers/XMPPRosterController.h"
+
+#include <boost/bind.hpp>
+
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Controllers/MainWindow.h"
+#include "Swiften/Controllers/MainWindowFactory.h"
+#include "Swiften/Elements/RosterItemPayload.h"
+#include "Swiften/Queries/IQRouter.h"
+#include "Swiften/Queries/Requests/GetRosterRequest.h"
+#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/Roster/Roster.h"
+#include "Swiften/Roster/SetPresence.h"
+#include "Swiften/Roster/OfflineRosterFilter.h"
+#include "Swiften/Roster/OpenChatRosterAction.h"
+#include "Swiften/Roster/TreeWidgetFactory.h"
+#include "Swiften/Roster/XMPPRoster.h"
+
+namespace Swift {
+
+/**
+ * The controller does not gain ownership of these parameters.
+ */
+XMPPRosterController::XMPPRosterController(IQRouter* iqRouter, boost::shared_ptr<XMPPRoster> xmppRoster)
+ : IQHandler(iqRouter), xmppRoster_(xmppRoster) {
+ GetRosterRequest* rosterRequest = new GetRosterRequest(iqRouter, Request::AutoDeleteAfterResponse);
+ rosterRequest->onResponse.connect(boost::bind(&XMPPRosterController::handleRosterReceived, this, _1));
+ rosterRequest->send();
+}
+
+XMPPRosterController::~XMPPRosterController() {
+
+}
+
+void XMPPRosterController::handleRosterReceived(boost::shared_ptr<RosterPayload> rosterPayload) {
+ foreach(const RosterItemPayload& item, rosterPayload->getItems()) {
+ if (item.getSubscription() == RosterItemPayload::Remove) {
+ xmppRoster_->removeContact(item.getJID());
+ } else {
+ xmppRoster_->addContact(item.getJID(), item.getName(), item.getGroups());
+ }
+ }
+}
+
+bool XMPPRosterController::handleIQ(boost::shared_ptr<IQ> iq) {
+ if (iq->getType() != IQ::Set || iq->getPayload<RosterPayload>().get() == NULL || iq->getFrom().isValid()) {
+ return false;
+ }
+ handleRosterReceived(iq->getPayload<RosterPayload>());
+ return true;
+}
+
+}
+
diff --git a/Swiften/Controllers/XMPPRosterController.h b/Swiften/Controllers/XMPPRosterController.h
new file mode 100644
index 0000000..2eaa6f5
--- /dev/null
+++ b/Swiften/Controllers/XMPPRosterController.h
@@ -0,0 +1,28 @@
+#pragma once
+
+#include "Swiften/JID/JID.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/Elements/IQ.h"
+#include "Swiften/Elements/RosterPayload.h"
+#include "Swiften/Queries/IQHandler.h"
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+namespace Swift {
+ class IQRouter;
+ class XMPPRoster;
+
+ class XMPPRosterController : public IQHandler {
+ public:
+ XMPPRosterController(IQRouter *iqRouter, boost::shared_ptr<XMPPRoster> xmppRoster);
+ ~XMPPRosterController();
+ boost::shared_ptr<XMPPRoster> getXMPPRoster() {return xmppRoster_;};
+ bool handleIQ(boost::shared_ptr<IQ>);
+
+ private:
+ void handleRosterReceived(boost::shared_ptr<RosterPayload> rosterPayload);
+ boost::shared_ptr<XMPPRoster> xmppRoster_;
+ };
+}
+