/* * Copyright (c) 2012 Catalin Badea * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include #include #include #include #include #include namespace Swift { HistoryViewController::HistoryViewController( const JID& selfJID, UIEventStream* uiEventStream, HistoryController* historyController, NickResolver* nickResolver, AvatarManager* avatarManager, HistoryWindowFactory* historyWindowFactory) : selfJID_(selfJID), uiEventStream_(uiEventStream), historyController_(historyController), nickResolver_(nickResolver), avatarManager_(avatarManager), historyWindowFactory_(historyWindowFactory), historyWindow_(NULL) { uiEventStream_->onUIEvent.connect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); } HistoryViewController::~HistoryViewController() { uiEventStream_->onUIEvent.disconnect(boost::bind(&HistoryViewController::handleUIEvent, this, _1)); if (historyWindow_) { historyWindow_->onSelectedContactChanged.disconnect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1)); delete historyWindow_; delete roster_; } } void HistoryViewController::handleUIEvent(boost::shared_ptr rawEvent) { boost::shared_ptr event = boost::dynamic_pointer_cast(rawEvent); if (event != NULL) { if (historyWindow_ == NULL) { historyWindow_ = historyWindowFactory_->createHistoryWindow(uiEventStream_); historyWindow_->onSelectedContactChanged.connect(boost::bind(&HistoryViewController::handleSelectedContactChanged, this, _1)); roster_ = new Roster(false, true); historyWindow_->setRosterModel(roster_); historyController_->getAllContacts(selfJID_, rooms_, contacts_); foreach (const JID& muc, rooms_) { roster_->addContact(muc, muc, nickResolver_->jidToNick(muc), "MUC", byteArrayToString(avatarManager_->getAvatar(muc))); } foreach (const JID& contact, contacts_) { roster_->addContact(contact, contact, nickResolver_->jidToNick(contact), "Contacts", byteArrayToString(avatarManager_->getAvatar(contact))); } } historyWindow_->activate(); } } void HistoryViewController::handleSelectedContactChanged(RosterItem* newContact) { // FIXME: signal is triggerd twice. if (newContact == NULL) { return; } ContactRosterItem* contact = dynamic_cast(newContact); JID contactJID = contact->getJID(); bool isRoom = rooms_.count(contactJID); std::vector messages = historyController_->getMessages(selfJID_, contactJID, isRoom); foreach (const HistoryMessage& message, messages) { bool senderIsSelf = message.getFromJID() == selfJID_; std::string avatarPath = byteArrayToString(avatarManager_->getAvatar(message.getFromJID())); historyWindow_->addMessage(message.getMessage(), nickResolver_->jidToNick(message.getFromJID()), senderIsSelf, avatarPath, message.getTime()); } } }