From babda920a6c6efdd6464093b55c7ff752181a63b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sun, 6 Jun 2010 12:33:34 +0200 Subject: Use delay when printing MUC history. Resolves: #415 diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp index 2e1b1c8..7b52fba 100644 --- a/Swift/Controllers/Chat/ChatController.cpp +++ b/Swift/Controllers/Chat/ChatController.cpp @@ -70,7 +70,7 @@ void ChatController::preSendMessageRequest(boost::shared_ptr message) { } void ChatController::postSendMessage(const String& body) { - addMessage(body, "me", true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel() : boost::optional(), String(avatarManager_->getAvatarPath(selfJID_).string())); + addMessage(body, "me", true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel() : boost::optional(), String(avatarManager_->getAvatarPath(selfJID_).string()), boost::posix_time::microsec_clock::universal_time()); chatStateNotifier_->userSentMessage(); } @@ -107,5 +107,8 @@ void ChatController::handlePresenceChange(boost::shared_ptr newPresenc } } +boost::optional ChatController::getMessageTimestamp(boost::shared_ptr message) const { + return message->getTimestamp(); +} } diff --git a/Swift/Controllers/Chat/ChatController.h b/Swift/Controllers/Chat/ChatController.h index 7d2072d..26015a1 100644 --- a/Swift/Controllers/Chat/ChatController.h +++ b/Swift/Controllers/Chat/ChatController.h @@ -28,6 +28,7 @@ namespace Swift { void preHandleIncomingMessage(boost::shared_ptr message); void preSendMessageRequest(boost::shared_ptr); String senderDisplayNameFromMessage(const JID& from); + virtual boost::optional getMessageTimestamp(boost::shared_ptr) const; private: NickResolver* nickResolver_; diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp index d1452c7..adafff0 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.cpp +++ b/Swift/Controllers/Chat/ChatControllerBase.cpp @@ -105,11 +105,11 @@ void ChatControllerBase::activateChatWindow() { chatWindow_->activate(); } -void ChatControllerBase::addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath) { +void ChatControllerBase::addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time) { if (message.beginsWith("/me ")) { - chatWindow_->addAction(message.getSplittedAtFirst(' ').second, senderName, senderIsSelf, label, avatarPath); + chatWindow_->addAction(message.getSplittedAtFirst(' ').second, senderName, senderIsSelf, label, avatarPath, time); } else { - chatWindow_->addMessage(message, senderName, senderIsSelf, label, avatarPath); + chatWindow_->addMessage(message, senderName, senderIsSelf, label, avatarPath, time); } } @@ -148,7 +148,14 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr m } boost::shared_ptr label = message->getPayload(); boost::optional maybeLabel = label ? boost::optional(*label) : boost::optional(); - addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), maybeLabel, String(avatarManager_->getAvatarPath(from).string())); + + // TODO: determine the timestamp + boost::posix_time::ptime timeStamp = boost::posix_time::microsec_clock::universal_time(); + boost::optional messageTimeStamp = getMessageTimestamp(message); + if (messageTimeStamp) { + timeStamp = *messageTimeStamp; + } + addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), maybeLabel, String(avatarManager_->getAvatarPath(from).string()), timeStamp); } } diff --git a/Swift/Controllers/Chat/ChatControllerBase.h b/Swift/Controllers/Chat/ChatControllerBase.h index e4d14af..b851857 100644 --- a/Swift/Controllers/Chat/ChatControllerBase.h +++ b/Swift/Controllers/Chat/ChatControllerBase.h @@ -12,6 +12,8 @@ #include #include "Swiften/Base/boost_bsignals.h" #include +#include +#include #include "Swiften/Base/String.h" #include "Swiften/Elements/DiscoInfo.h" @@ -37,7 +39,7 @@ namespace Swift { void activateChatWindow(); void setAvailableServerFeatures(boost::shared_ptr info); void handleIncomingMessage(boost::shared_ptr message); - void addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath); + void addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time); void setEnabled(bool enabled); virtual void setToJID(const JID& jid) {toJID_ = jid;}; protected: @@ -49,6 +51,7 @@ namespace Swift { virtual void preHandleIncomingMessage(boost::shared_ptr) {}; virtual void preSendMessageRequest(boost::shared_ptr) {}; virtual bool isFromContact(const JID& from); + virtual boost::optional getMessageTimestamp(boost::shared_ptr) const = 0; private: void handleSendMessageRequest(const String &body); diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp index 65603d7..544043d 100644 --- a/Swift/Controllers/Chat/MUCController.cpp +++ b/Swift/Controllers/Chat/MUCController.cpp @@ -247,4 +247,8 @@ void MUCController::preSendMessageRequest(boost::shared_ptr message) { message->setType(Swift::Message::Groupchat); } +boost::optional MUCController::getMessageTimestamp(boost::shared_ptr message) const { + return message->getTimestampFrom(toJID_); +} + } diff --git a/Swift/Controllers/Chat/MUCController.h b/Swift/Controllers/Chat/MUCController.h index a3c72e7..e93af1e 100644 --- a/Swift/Controllers/Chat/MUCController.h +++ b/Swift/Controllers/Chat/MUCController.h @@ -40,6 +40,7 @@ namespace Swift { void preSendMessageRequest(boost::shared_ptr message); bool isIncomingMessageFromMe(boost::shared_ptr message); String senderDisplayNameFromMessage(const JID& from); + boost::optional getMessageTimestamp(boost::shared_ptr message) const; private: void handleWindowClosed(); diff --git a/Swift/Controllers/UIInterfaces/ChatWindow.h b/Swift/Controllers/UIInterfaces/ChatWindow.h index 7e97f0c..50a0b39 100644 --- a/Swift/Controllers/UIInterfaces/ChatWindow.h +++ b/Swift/Controllers/UIInterfaces/ChatWindow.h @@ -10,6 +10,7 @@ #include #include "Swiften/Base/boost_bsignals.h" #include +#include #include #include "Swiften/Base/String.h" @@ -27,8 +28,8 @@ namespace Swift { ChatWindow() {} virtual ~ChatWindow() {}; - virtual void addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath) = 0; - virtual void addAction(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath) = 0; + virtual void addMessage(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time) = 0; + virtual void addAction(const String& message, const String& senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time) = 0; virtual void addSystemMessage(const String& message) = 0; virtual void addErrorMessage(const String& message) = 0; diff --git a/Swift/Controllers/UnitTest/MockChatWindow.h b/Swift/Controllers/UnitTest/MockChatWindow.h index 78705ce..ff61bf6 100644 --- a/Swift/Controllers/UnitTest/MockChatWindow.h +++ b/Swift/Controllers/UnitTest/MockChatWindow.h @@ -14,8 +14,8 @@ namespace Swift { MockChatWindow() {}; virtual ~MockChatWindow(); - virtual void addMessage(const String& message, const String& /*senderName*/, bool /*senderIsSelf*/, const boost::optional& /*label*/, const String& /*avatarPath*/) {lastMessageBody_ = message;}; - virtual void addAction(const String& message, const String& /*senderName*/, bool /*senderIsSelf*/, const boost::optional& /*label*/, const String& /*avatarPath*/) {lastMessageBody_ = message;}; + virtual void addMessage(const String& message, const String& /*senderName*/, bool /*senderIsSelf*/, const boost::optional& /*label*/, const String& /*avatarPath*/, const boost::posix_time::ptime&) {lastMessageBody_ = message;}; + virtual void addAction(const String& message, const String& /*senderName*/, bool /*senderIsSelf*/, const boost::optional& /*label*/, const String& /*avatarPath*/, const boost::posix_time::ptime&) {lastMessageBody_ = message;}; virtual void addSystemMessage(const String& /*message*/) {}; virtual void addErrorMessage(const String& /*message*/) {}; @@ -31,7 +31,7 @@ namespace Swift { virtual SecurityLabel getSelectedSecurityLabel() {return SecurityLabel();}; virtual void setInputEnabled(bool /*enabled*/) {}; virtual void setRosterModel(Roster* /*roster*/) {}; - virtual void setTabComplete(TabComplete* complete) {}; + virtual void setTabComplete(TabComplete*) {}; boost::signal onClosed; boost::signal onAllMessagesRead; diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp index 6f49082..d5cadf4 100644 --- a/Swift/QtUI/QtChatWindow.cpp +++ b/Swift/QtUI/QtChatWindow.cpp @@ -224,11 +224,11 @@ void QtChatWindow::updateTitleWithUnreadCount() { emit titleUpdated(); } -void QtChatWindow::addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath) { - addMessage(message, senderName, senderIsSelf, label, avatarPath, ""); +void QtChatWindow::addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time) { + addMessage(message, senderName, senderIsSelf, label, avatarPath, "", time); } -void QtChatWindow::addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const QString& style) { +void QtChatWindow::addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const QString& style, const boost::posix_time::ptime& time) { if (isWidgetSelected()) { onAllMessagesRead(); } @@ -247,15 +247,15 @@ void QtChatWindow::addMessage(const String &message, const String &senderName, b bool appendToPrevious = !previousMessageWasSystem_ && ((senderIsSelf && previousMessageWasSelf_) || (!senderIsSelf && !previousMessageWasSelf_ && previousSenderName_ == P2QSTRING(senderName))); QString qAvatarPath = avatarPath.isEmpty() ? "qrc:/icons/avatar.png" : QUrl::fromLocalFile(P2QSTRING(avatarPath)).toEncoded(); - messageLog_->addMessage(MessageSnippet(htmlString, Qt::escape(P2QSTRING(senderName)), QDateTime::currentDateTime(), qAvatarPath, senderIsSelf, appendToPrevious)); + messageLog_->addMessage(MessageSnippet(htmlString, Qt::escape(P2QSTRING(senderName)), B2QDATE(time), qAvatarPath, senderIsSelf, appendToPrevious)); previousMessageWasSelf_ = senderIsSelf; previousSenderName_ = P2QSTRING(senderName); previousMessageWasSystem_ = false; } -void QtChatWindow::addAction(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath) { - addMessage(" *" + message + "*", senderName, senderIsSelf, label, avatarPath, "font-style:italic "); +void QtChatWindow::addAction(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time) { + addMessage(" *" + message + "*", senderName, senderIsSelf, label, avatarPath, "font-style:italic ", time); } void QtChatWindow::addErrorMessage(const String& errorMessage) { diff --git a/Swift/QtUI/QtChatWindow.h b/Swift/QtUI/QtChatWindow.h index ff2f1cb..44d79a1 100644 --- a/Swift/QtUI/QtChatWindow.h +++ b/Swift/QtUI/QtChatWindow.h @@ -27,8 +27,8 @@ namespace Swift { public: QtChatWindow(const QString &contact, UIEventStream* eventStream); ~QtChatWindow(); - void addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath); - void addAction(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath); + void addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time); + void addAction(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const boost::posix_time::ptime& time); void addSystemMessage(const String& message); void addErrorMessage(const String& errorMessage); void show(); @@ -61,7 +61,7 @@ namespace Swift { private: void updateTitleWithUnreadCount(); void tabComplete(); - void addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const QString& style); + void addMessage(const String &message, const String &senderName, bool senderIsSelf, const boost::optional& label, const String& avatarPath, const QString& style, const boost::posix_time::ptime& time); int unreadCount_; bool contactIsTyping_; diff --git a/Swift/QtUI/QtSwiftUtil.h b/Swift/QtUI/QtSwiftUtil.h index a2ad4db..3f1ad3b 100644 --- a/Swift/QtUI/QtSwiftUtil.h +++ b/Swift/QtUI/QtSwiftUtil.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Kevin Smith + * Copyright (c) 2010 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -10,4 +10,6 @@ #define P2QSTRING(a) QString::fromUtf8(a.getUTF8Data()) #define Q2PSTRING(a) Swift::String(a.toUtf8()) +#define B2QDATE(a) QDateTime::fromTime_t((a - boost::posix_time::from_time_t(0)).total_seconds()) + #endif diff --git a/Swiften/Elements/Delay.h b/Swiften/Elements/Delay.h index f59d729..3213037 100644 --- a/Swiften/Elements/Delay.h +++ b/Swiften/Elements/Delay.h @@ -16,7 +16,7 @@ namespace Swift { class Delay : public Payload { public: Delay() {}; - Delay(const boost::posix_time::ptime& time, const JID& from) : time_(time), from_(from) {}; + Delay(const boost::posix_time::ptime& time, const JID& from = JID()) : time_(time), from_(from) {}; const boost::posix_time::ptime& getStamp() const {return time_;}; void setStamp(const boost::posix_time::ptime& time) {time_ = time;}; diff --git a/Swiften/Elements/Stanza.cpp b/Swiften/Elements/Stanza.cpp index dd6021d..d15d778 100644 --- a/Swiften/Elements/Stanza.cpp +++ b/Swiften/Elements/Stanza.cpp @@ -5,6 +5,7 @@ */ #include "Swiften/Elements/Stanza.h" +#include "Swiften/Elements/Delay.h" #include @@ -33,5 +34,20 @@ boost::shared_ptr Stanza::getPayloadOfSameType(boost::shared_ptr(); } +boost::optional Stanza::getTimestamp() const { + boost::shared_ptr delay = getPayload(); + return delay ? delay->getStamp() : boost::optional(); +} + +boost::optional Stanza::getTimestampFrom(const JID& jid) const { + std::vector< boost::shared_ptr > delays = getPayloads(); + for (size_t i = 0; i < delays.size(); ++i) { + if (delays[i]->getFrom() == jid) { + return delays[i]->getStamp(); + } + } + return getTimestamp(); +} + } diff --git a/Swiften/Elements/Stanza.h b/Swiften/Elements/Stanza.h index f42048e..20fb557 100644 --- a/Swiften/Elements/Stanza.h +++ b/Swiften/Elements/Stanza.h @@ -4,11 +4,12 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#ifndef SWIFTEN_STANZAS_STANZA_H -#define SWIFTEN_STANZAS_STANZA_H +#pragma once #include #include +#include +#include #include "Swiften/Elements/Element.h" #include "Swiften/Elements/Payload.h" @@ -65,6 +66,11 @@ namespace Swift { const String& getID() const { return id_; } void setID(const String& id) { id_ = id; } + + boost::optional getTimestamp() const; + + // Falls back to any timestamp if no specific timestamp for the given JID is found. + boost::optional getTimestampFrom(const JID& jid) const; private: String id_; @@ -75,5 +81,3 @@ namespace Swift { Payloads payloads_; }; } - -#endif diff --git a/Swiften/Elements/UnitTest/StanzaTest.cpp b/Swiften/Elements/UnitTest/StanzaTest.cpp index 1e7e9c4..15f8629 100644 --- a/Swiften/Elements/UnitTest/StanzaTest.cpp +++ b/Swiften/Elements/UnitTest/StanzaTest.cpp @@ -11,6 +11,7 @@ #include "Swiften/Elements/Stanza.h" #include "Swiften/Elements/Payload.h" #include "Swiften/Elements/Message.h" +#include "Swiften/Elements/Delay.h" using namespace Swift; @@ -27,6 +28,11 @@ class StanzaTest : public CppUnit::TestFixture CPPUNIT_TEST(testUpdatePayload_NewPayload); CPPUNIT_TEST(testGetPayloadOfSameType); CPPUNIT_TEST(testGetPayloadOfSameType_NoSuchPayload); + CPPUNIT_TEST(testGetTimestamp); + CPPUNIT_TEST(testGetTimestamp_TimestampWithFrom); + CPPUNIT_TEST(testGetTimestamp_NoDelay); + CPPUNIT_TEST(testGetTimestampFrom); + CPPUNIT_TEST(testGetTimestampFrom_Fallsback); CPPUNIT_TEST_SUITE_END(); public: @@ -60,8 +66,6 @@ class StanzaTest : public CppUnit::TestFixture bool* alive_; }; - StanzaTest() {} - void testConstructor_Copy() { Message m; m.addPayload(boost::shared_ptr(new MyPayload1())); @@ -172,6 +176,55 @@ class StanzaTest : public CppUnit::TestFixture CPPUNIT_ASSERT(!m.getPayloadOfSameType(boost::shared_ptr(new MyPayload2("bar")))); } + + void testGetTimestamp() { + Message m; + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(1)))); + + boost::optional timestamp = m.getTimestamp(); + + CPPUNIT_ASSERT(timestamp); + CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:01"), boost::posix_time::to_simple_string(*timestamp)); + } + + void testGetTimestamp_TimestampWithFrom() { + Message m; + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(1), JID("foo@bar.com")))); + + boost::optional timestamp = m.getTimestamp(); + + CPPUNIT_ASSERT(timestamp); + CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:01"), boost::posix_time::to_simple_string(*timestamp)); + } + + void testGetTimestamp_NoDelay() { + Message m; + CPPUNIT_ASSERT(!m.getTimestamp()); + } + + void testGetTimestampFrom() { + Message m; + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(0)))); + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(1), JID("foo1@bar.com")))); + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(2), JID("foo2@bar.com")))); + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(3), JID("foo3@bar.com")))); + + boost::optional timestamp = m.getTimestampFrom(JID("foo2@bar.com")); + + CPPUNIT_ASSERT(timestamp); + CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:02"), boost::posix_time::to_simple_string(*timestamp)); + } + + void testGetTimestampFrom_Fallsback() { + Message m; + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(1), JID("foo1@bar.com")))); + m.addPayload(boost::shared_ptr(new Delay(boost::posix_time::from_time_t(3), JID("foo3@bar.com")))); + + boost::optional timestamp = m.getTimestampFrom(JID("foo2@bar.com")); + + CPPUNIT_ASSERT(timestamp); + CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:01"), boost::posix_time::to_simple_string(*timestamp)); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(StanzaTest); -- cgit v0.10.2-6-g49f6