diff options
Diffstat (limited to 'Swift/Controllers/HistoryViewController.cpp')
-rw-r--r-- | Swift/Controllers/HistoryViewController.cpp | 91 |
1 files changed, 70 insertions, 21 deletions
diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp index a67f6cd..18e27bd 100644 --- a/Swift/Controllers/HistoryViewController.cpp +++ b/Swift/Controllers/HistoryViewController.cpp @@ -48,6 +48,7 @@ HistoryViewController::~HistoryViewController() { } void HistoryViewController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { + // TODO: add new nick manager boost::shared_ptr<RequestHistoryUIEvent> event = boost::dynamic_pointer_cast<RequestHistoryUIEvent>(rawEvent); if (event != NULL) { if (historyWindow_ == NULL) { @@ -56,13 +57,31 @@ void HistoryViewController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { historyWindow_->setRosterModel(roster_); - historyController_->getAllContacts(selfJID_, rooms_, contacts_); + // MUCs + std::set<JID> rooms = historyController_->getContacts(selfJID_, HistoryMessage::Groupchat); + foreach (const JID& room, rooms) { + if ( !rooms_.count(room)) { + roster_->addContact(room, room, nickResolver_->jidToNick(room), "MUC", avatarManager_->getAvatarPath(room).string()); + rooms_.insert(room); + } + } - foreach (const JID& muc, rooms_) { - roster_->addContact(muc, muc, nickResolver_->jidToNick(muc), "MUC", byteArrayToString(avatarManager_->getAvatar(muc))); + // Contacts + std::set<JID> contacts = historyController_->getContacts(selfJID_, HistoryMessage::Chat); + foreach (const JID& contact, contacts) { + if (!contacts_.count(contact)) { + roster_->addContact(contact, contact, nickResolver_->jidToNick(contact), "Contacts", avatarManager_->getAvatarPath(contact).string()); + contacts_.insert(contact); + } } - foreach (const JID& contact, contacts_) { - roster_->addContact(contact, contact, nickResolver_->jidToNick(contact), "Contacts", byteArrayToString(avatarManager_->getAvatar(contact))); + + // MUC contacts + std::set<JID> roomPrivateContacts = historyController_->getContacts(selfJID_, HistoryMessage::PrivateMessage); + foreach (const JID& contact, roomPrivateContacts) { + if ( !roomPrivateContacts_.count(contact)) { + roster_->addContact(contact, contact, nickResolver_->jidToNick(contact), "Contacts", avatarManager_->getAvatarPath(contact).string()); + roomPrivateContacts_.insert(contact); + } } } @@ -72,38 +91,68 @@ void HistoryViewController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) { void HistoryViewController::handleSelectedContactChanged(RosterItem* newContact) { // FIXME: signal is triggerd twice. - selectedItem_ = dynamic_cast<ContactRosterItem*>(newContact); + ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(newContact); - if (newContact == NULL) { + if (contact && selectedItem_ != contact) { + selectedItem_ = contact; + historyWindow_->resetConversationView(); + } + else { return; } - ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(newContact); JID contactJID = contact->getJID(); - bool isRoom = rooms_.count(contactJID); - std::vector<HistoryMessage> messages = historyController_->getMessages(selfJID_, contactJID, isRoom); + std::vector<HistoryMessage> messages; + if (roomPrivateContacts_.count(contactJID)) { + messages = historyController_->getMessages(selfJID_, contactJID, HistoryMessage::PrivateMessage); + } + else if (contacts_.count(contactJID)) { + messages = historyController_->getMessages(selfJID_, contactJID, HistoryMessage::Chat); + } + else { + messages = historyController_->getMessages(selfJID_, contactJID, HistoryMessage::Groupchat); + } + 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()); + addNewMessage(message); } } void HistoryViewController::handleNewMessage(const HistoryMessage& message) { JID contactJID = message.getFromJID().toBare() == selfJID_ ? message.getToJID() : message.getFromJID(); - if (selectedItem_ && selectedItem_->getJID().toBare() == contactJID) { - bool senderIsSelf = message.getFromJID().toBare() == selfJID_; - std::string avatarPath = byteArrayToString(avatarManager_->getAvatar(message.getFromJID())); - historyWindow_->addMessage(message.getMessage(), nickResolver_->jidToNick(message.getFromJID()), senderIsSelf, avatarPath, message.getTime()); + JID displayJID; + if (message.getType() == HistoryMessage::PrivateMessage) { + displayJID = contactJID; + } + else { + displayJID = contactJID.toBare(); + } + + // check current conversation + if (selectedItem_ && selectedItem_->getJID() == displayJID) { + addNewMessage(message); } - else if (!rooms_.count(contactJID.toBare()) && message.isGroupChat()) { - roster_->addContact(contactJID.toBare(), contactJID.toBare(), nickResolver_->jidToNick(contactJID.toBare()), "MUC", byteArrayToString(avatarManager_->getAvatar(contactJID.toBare()))); + // add new contact + else if (message.getType() == HistoryMessage::Groupchat && !rooms_.count(displayJID)) { + roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), "MUC", avatarManager_->getAvatarPath(displayJID).string()); + rooms_.insert(displayJID); } - else if (!contacts_.count(contactJID) && !message.isGroupChat()) { - roster_->addContact(contactJID, contactJID, nickResolver_->jidToNick(contactJID), "Contacts", byteArrayToString(avatarManager_->getAvatar(contactJID))); + else if (message.getType() == HistoryMessage::Chat && !contacts_.count(displayJID)) { + roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), "Contacts", avatarManager_->getAvatarPath(displayJID).string()); + contacts_.insert(displayJID); } + else if (message.getType() == HistoryMessage::PrivateMessage && !roomPrivateContacts_.count(displayJID)) { + roster_->addContact(displayJID, displayJID, nickResolver_->jidToNick(displayJID), "Contacts", avatarManager_->getAvatarPath(displayJID).string()); + roomPrivateContacts_.insert(displayJID); + } +} + +void HistoryViewController::addNewMessage(const HistoryMessage& message) { + bool senderIsSelf = message.getFromJID().toBare() == selfJID_; + std::string avatarPath = avatarManager_->getAvatarPath(message.getFromJID()).string(); + historyWindow_->addMessage(message.getMessage(), nickResolver_->jidToNick(message.getFromJID()), senderIsSelf, avatarPath, message.getTime()); } } |