2015-02-08 00:18:25 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project
|
|
|
|
|
2015-02-08 00:18:25 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2015-02-08 00:18:25 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2015-02-08 00:18:25 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2015-02-08 00:18:25 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-08 00:18:25 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "offlinemsgengine.h"
|
|
|
|
#include "src/friend.h"
|
2015-06-06 07:44:47 +08:00
|
|
|
#include "src/persistence/settings.h"
|
2015-04-24 08:32:09 +08:00
|
|
|
#include "src/core/core.h"
|
2015-12-17 18:24:01 +08:00
|
|
|
#include "src/nexus.h"
|
|
|
|
#include "src/persistence/profile.h"
|
2015-02-08 00:18:25 +08:00
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
|
|
|
@var static const int OfflineMsgEngine::offlineTimeout
|
|
|
|
@brief timeout after which faux offline messages get to be re-sent.
|
|
|
|
Originally was 2s, but since that was causing lots of duplicated
|
|
|
|
messages on receiving end, make qTox be more lazy about re-sending
|
|
|
|
should be 20s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-03-23 18:48:31 +08:00
|
|
|
const int OfflineMsgEngine::offlineTimeout = 20000;
|
2015-02-08 00:18:25 +08:00
|
|
|
QMutex OfflineMsgEngine::globalMutex;
|
|
|
|
|
|
|
|
OfflineMsgEngine::OfflineMsgEngine(Friend *frnd) :
|
|
|
|
mutex(QMutex::Recursive),
|
|
|
|
f(frnd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OfflineMsgEngine::~OfflineMsgEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void OfflineMsgEngine::dischargeReceipt(int receipt)
|
|
|
|
{
|
|
|
|
QMutexLocker ml(&mutex);
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
Profile* profile = Nexus::getProfile();
|
2015-02-08 00:18:25 +08:00
|
|
|
auto it = receipts.find(receipt);
|
|
|
|
if (it != receipts.end())
|
|
|
|
{
|
|
|
|
int mID = it.value();
|
|
|
|
auto msgIt = undeliveredMsgs.find(mID);
|
|
|
|
if (msgIt != undeliveredMsgs.end())
|
|
|
|
{
|
2015-12-17 18:24:01 +08:00
|
|
|
if (profile->isHistoryEnabled())
|
|
|
|
profile->getHistory()->markAsSent(mID);
|
2015-02-08 01:49:38 +08:00
|
|
|
msgIt.value().msg->markAsSent(QDateTime::currentDateTime());
|
2015-02-08 00:18:25 +08:00
|
|
|
undeliveredMsgs.erase(msgIt);
|
|
|
|
}
|
|
|
|
receipts.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
void OfflineMsgEngine::registerReceipt(int receipt, int64_t messageID, ChatMessage::Ptr msg, const QDateTime ×tamp)
|
2015-02-08 00:18:25 +08:00
|
|
|
{
|
|
|
|
QMutexLocker ml(&mutex);
|
|
|
|
|
|
|
|
receipts[receipt] = messageID;
|
|
|
|
undeliveredMsgs[messageID] = {msg, timestamp, receipt};
|
|
|
|
}
|
|
|
|
|
|
|
|
void OfflineMsgEngine::deliverOfflineMsgs()
|
|
|
|
{
|
|
|
|
QMutexLocker ml(&mutex);
|
|
|
|
|
|
|
|
if (!Settings::getInstance().getFauxOfflineMessaging())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (f->getStatus() == Status::Offline)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (undeliveredMsgs.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2015-12-17 18:24:01 +08:00
|
|
|
QMap<int64_t, MsgPtr> msgs = undeliveredMsgs;
|
2016-01-18 21:50:11 +08:00
|
|
|
removeAllReceipts();
|
2016-01-18 21:45:29 +08:00
|
|
|
undeliveredMsgs.clear();
|
2015-02-08 00:18:25 +08:00
|
|
|
|
2015-10-14 04:21:20 +08:00
|
|
|
for (auto iter = msgs.begin(); iter != msgs.end(); ++iter)
|
2015-12-17 18:24:01 +08:00
|
|
|
{
|
|
|
|
auto val = iter.value();
|
|
|
|
auto key = iter.key();
|
2015-10-14 04:21:20 +08:00
|
|
|
|
|
|
|
if (val.timestamp.msecsTo(QDateTime::currentDateTime()) < offlineTimeout)
|
2015-02-08 00:18:25 +08:00
|
|
|
{
|
2015-10-14 04:21:20 +08:00
|
|
|
registerReceipt(val.receipt, key, val.msg, val.timestamp);
|
2015-02-08 00:18:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-14 04:21:20 +08:00
|
|
|
QString messageText = val.msg->toString();
|
2015-02-08 00:18:25 +08:00
|
|
|
int rec;
|
2015-10-14 04:21:20 +08:00
|
|
|
if (val.msg->isAction())
|
2015-02-08 00:18:25 +08:00
|
|
|
rec = Core::getInstance()->sendAction(f->getFriendID(), messageText);
|
|
|
|
else
|
|
|
|
rec = Core::getInstance()->sendMessage(f->getFriendID(), messageText);
|
2015-04-24 19:01:50 +08:00
|
|
|
|
2015-10-14 04:21:20 +08:00
|
|
|
registerReceipt(rec, key, val.msg);
|
2015-02-08 00:18:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 21:50:11 +08:00
|
|
|
void OfflineMsgEngine::removeAllReceipts()
|
2015-02-08 00:18:25 +08:00
|
|
|
{
|
|
|
|
QMutexLocker ml(&mutex);
|
|
|
|
|
|
|
|
receipts.clear();
|
|
|
|
}
|