Skip to content

Commit

Permalink
Merge pull request #348 from nico-izo/historyHook
Browse files Browse the repository at this point in the history
[core][chatsession] Added HistoryHook.
  • Loading branch information
Ruslan Nigmatullin committed Apr 17, 2015
2 parents d0b37fe + a015820 commit ca841a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
42 changes: 33 additions & 9 deletions core/libqutim/chatsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,43 @@ class MessageHandlerHook : public MessageHandler
{
public:
MessageHandlerHook()
{

}

MessageHandlerAsyncResult doHandle(Message &message) override
{
ChatSession *session = messageHookMap()->value(originalMessageId());
if (session) {
session->doAppendMessage(message);
}
return makeAsyncResult(Accept, QString());
}

};

class HistoryHook : public MessageHandler
{
public:
HistoryHook()
{
Config cfg(QLatin1String("appearance"));
cfg.beginGroup(QLatin1String("chat"));
cfg.beginGroup(QLatin1String("history"));
m_storeMessages = cfg.value(QLatin1String("storeMessages"), true);
m_storeServiceMessages = cfg.value(QLatin1String("storeServiceMessages"), true);
}

MessageHandlerAsyncResult doHandle(Message &message) override
{
ChatSession *session = messageHookMap()->value(originalMessageId());
if (session) {
session->doAppendMessage(message);
if (m_storeMessages && message.property("store", true)
&& (m_storeServiceMessages || !message.property("service", false))) {
History::instance()->store(message);
}

if (m_storeMessages && message.property("store", true)
&& (m_storeServiceMessages || !message.property("service", false))) {
History::instance()->store(message);
}
return makeAsyncResult(Accept, QString());
}

private:
bool m_storeMessages;
bool m_storeServiceMessages;
Expand Down Expand Up @@ -100,6 +116,7 @@ struct ChatLayerData
ServicePointer<ChatLayer> self;
QScopedPointer<MessageHandlerHook> handlerHook;
QScopedPointer<ChatUnitSenderMessageHandler> senderHook;
QScopedPointer<HistoryHook> historyHook;
};

Q_GLOBAL_STATIC(ChatLayerData, p)
Expand Down Expand Up @@ -203,6 +220,8 @@ ChatLayer::ChatLayer() : d_ptr(new ChatLayerPrivate)
qRegisterMetaType<qutim_sdk_0_3::MessageList>("qutim_sdk_0_3::MessageList");
p()->handlerHook.reset(new MessageHandlerHook);
p()->senderHook.reset(new ChatUnitSenderMessageHandler);
p()->historyHook.reset(new HistoryHook);

MessageHandler::registerHandler(p()->handlerHook.data(),
QLatin1String("HandlerHook"),
MessageHandler::ChatInPriority,
Expand All @@ -211,11 +230,16 @@ ChatLayer::ChatLayer() : d_ptr(new ChatLayerPrivate)
QLatin1String("SenderHook"),
MessageHandler::NormalPriortity,
MessageHandler::SenderPriority);
MessageHandler::registerHandler(p()->historyHook.data(),
QLatin1String("HistoryHook"),
MessageHandler::HistoryPriority,
MessageHandler::HistoryPriority);
}

ChatLayer::~ChatLayer()
{
p()->handlerHook.reset(0);
p()->historyHook.reset(0);
}

ChatLayer *ChatLayer::instance()
Expand Down
1 change: 1 addition & 0 deletions core/libqutim/messagehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class LIBQUTIM_EXPORT MessageHandler
NormalPriortity = 0x00010000,
ChatOutPriority = 0x00020000,
HighPriority = 0x01000000,
HistoryPriority = 0x01010000,
SenderPriority = 0x02000000
};

Expand Down

0 comments on commit ca841a6

Please sign in to comment.