mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Refactor OfflineMsgEngine, fix potential segfault
(@apprb take note please)
This commit is contained in:
parent
eff76fd491
commit
11d4ec2599
@ -23,19 +23,16 @@
|
||||
#include <QTimer>
|
||||
|
||||
const int OfflineMsgEngine::offlineTimeout = 2000;
|
||||
QSet<OfflineMsgEngine*> OfflineMsgEngine::engines;
|
||||
QMutex OfflineMsgEngine::globalMutex;
|
||||
|
||||
OfflineMsgEngine::OfflineMsgEngine(Friend *frnd) :
|
||||
mutex(QMutex::Recursive),
|
||||
f(frnd)
|
||||
{
|
||||
engines.insert(this);
|
||||
}
|
||||
|
||||
OfflineMsgEngine::~OfflineMsgEngine()
|
||||
{
|
||||
engines.remove(this);
|
||||
}
|
||||
|
||||
void OfflineMsgEngine::dischargeReceipt(int receipt)
|
||||
@ -106,16 +103,3 @@ void OfflineMsgEngine::removeAllReciepts()
|
||||
receipts.clear();
|
||||
undeliveredMsgs.clear();
|
||||
}
|
||||
|
||||
void OfflineMsgEngine::processAllMsgs()
|
||||
{
|
||||
if (globalMutex.tryLock())
|
||||
{
|
||||
for (auto &it : engines)
|
||||
{
|
||||
it->deliverOfflineMsgs();
|
||||
}
|
||||
|
||||
globalMutex.unlock();
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ class OfflineMsgEngine : public QObject
|
||||
public:
|
||||
OfflineMsgEngine(Friend *);
|
||||
virtual ~OfflineMsgEngine();
|
||||
static QMutex globalMutex;
|
||||
|
||||
void dischargeReceipt(int receipt);
|
||||
void registerReceipt(int receipt, int messageID, MessageActionPtr msg, const QDateTime ×tamp = QDateTime::currentDateTime());
|
||||
@ -39,7 +40,6 @@ public:
|
||||
public slots:
|
||||
void deliverOfflineMsgs();
|
||||
void removeAllReciepts();
|
||||
static void processAllMsgs();
|
||||
|
||||
private:
|
||||
struct MsgPtr {
|
||||
@ -53,9 +53,7 @@ private:
|
||||
QHash<int, int> receipts;
|
||||
QMap<int, MsgPtr> undeliveredMsgs;
|
||||
|
||||
static QSet<OfflineMsgEngine*> engines;
|
||||
static const int offlineTimeout;
|
||||
static QMutex globalMutex;
|
||||
};
|
||||
|
||||
#endif // OFFLINEMSGENGINE_H
|
||||
|
@ -105,6 +105,7 @@ ChatForm::~ChatForm()
|
||||
{
|
||||
delete netcam;
|
||||
delete callConfirm;
|
||||
delete offlineEngine;
|
||||
}
|
||||
|
||||
void ChatForm::setStatusMessage(QString newMessage)
|
||||
|
@ -225,7 +225,7 @@ void Widget::init()
|
||||
connect(addFriendForm, SIGNAL(friendRequested(QString, QString)), this, SIGNAL(friendRequested(QString, QString)));
|
||||
connect(timer, &QTimer::timeout, this, &Widget::onUserAwayCheck);
|
||||
connect(timer, &QTimer::timeout, this, &Widget::onEventIconTick);
|
||||
connect(offlineMsgTimer, &QTimer::timeout, &OfflineMsgEngine::processAllMsgs);
|
||||
connect(offlineMsgTimer, &QTimer::timeout, this, &Widget::processOfflineMsgs);
|
||||
|
||||
addFriendForm->show(*ui);
|
||||
|
||||
@ -1125,6 +1125,20 @@ void Widget::onSplitterMoved(int pos, int index)
|
||||
saveSplitterGeometry();
|
||||
}
|
||||
|
||||
void Widget::processOfflineMsgs()
|
||||
{
|
||||
if (OfflineMsgEngine::globalMutex.tryLock())
|
||||
{
|
||||
QList<Friend*> frnds = FriendList::getAllFriends();
|
||||
for (Friend *f : frnds)
|
||||
{
|
||||
f->getChatForm()->getOfflineMsgEngine()->deliverOfflineMsgs();
|
||||
}
|
||||
|
||||
OfflineMsgEngine::globalMutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void Widget::clearAllReceipts()
|
||||
{
|
||||
QList<Friend*> frnds = FriendList::getAllFriends();
|
||||
|
@ -132,6 +132,7 @@ private slots:
|
||||
void onEventIconTick();
|
||||
void onSetShowSystemTray(bool newValue);
|
||||
void onSplitterMoved(int pos, int index);
|
||||
void processOfflineMsgs();
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
Loading…
x
Reference in New Issue
Block a user