mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
perf(smileys): Cleanup smileys icons by timer
This commit is contained in:
parent
c640007775
commit
fa21594902
|
@ -25,6 +25,7 @@
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QtConcurrent/QtConcurrentRun>
|
#include <QtConcurrent/QtConcurrentRun>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
#if defined(Q_OS_FREEBSD)
|
#if defined(Q_OS_FREEBSD)
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
@ -58,6 +59,8 @@ static const QString RICH_TEXT_PATTERN = QStringLiteral("<img title=\"%1\" src=\
|
||||||
|
|
||||||
static const QString EMOTICONS_FILE_NAME = QStringLiteral("emoticons.xml");
|
static const QString EMOTICONS_FILE_NAME = QStringLiteral("emoticons.xml");
|
||||||
|
|
||||||
|
static constexpr int CLEANUP_TIMEOUT = 5 * 60 * 1000; // 5 minutes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Construct list of standard directories with "emoticons" sub dir, whether these directories
|
* @brief Construct list of standard directories with "emoticons" sub dir, whether these directories
|
||||||
* exist or not
|
* exist or not
|
||||||
|
@ -101,13 +104,34 @@ QString getAsRichText(const QString& key)
|
||||||
return RICH_TEXT_PATTERN.arg(key);
|
return RICH_TEXT_PATTERN.arg(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SmileyPack::SmileyPack()
|
SmileyPack::SmileyPack()
|
||||||
|
: cleanupTimer{new QTimer(this)}
|
||||||
{
|
{
|
||||||
loadingMutex.lock();
|
loadingMutex.lock();
|
||||||
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
|
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
|
||||||
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this,
|
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this,
|
||||||
&SmileyPack::onSmileyPackChanged);
|
&SmileyPack::onSmileyPackChanged);
|
||||||
|
connect(cleanupTimer, &QTimer::timeout, this, &SmileyPack::cleanup);
|
||||||
|
cleanupTimer->start(CLEANUP_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
SmileyPack::~SmileyPack()
|
||||||
|
{
|
||||||
|
delete cleanupTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SmileyPack::cleanup()
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&loadingMutex);
|
||||||
|
for (auto it = emoticonToIcon.begin(); it != emoticonToIcon.end();) {
|
||||||
|
std::shared_ptr<QIcon>& icon = it->second;
|
||||||
|
if (icon.use_count() == 1) {
|
||||||
|
it = emoticonToIcon.erase(it);
|
||||||
|
icon.reset();
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -242,7 +266,7 @@ QString SmileyPack::smileyfied(const QString& msg)
|
||||||
QString key = match.captured();
|
QString key = match.captured();
|
||||||
int startPos = match.capturedStart();
|
int startPos = match.capturedStart();
|
||||||
int keyLength = key.length();
|
int keyLength = key.length();
|
||||||
if (emoticonToPath.contains(key)) {
|
if (emoticonToPath.find(key) != emoticonToPath.end()) {
|
||||||
QString imgRichText = getAsRichText(key);
|
QString imgRichText = getAsRichText(key);
|
||||||
result.replace(startPos + replaceDiff, keyLength, imgRichText);
|
result.replace(startPos + replaceDiff, keyLength, imgRichText);
|
||||||
replaceDiff += imgRichText.length() - keyLength;
|
replaceDiff += imgRichText.length() - keyLength;
|
||||||
|
@ -269,7 +293,7 @@ QList<QStringList> SmileyPack::getEmoticons() const
|
||||||
std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
|
std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&loadingMutex);
|
QMutexLocker locker(&loadingMutex);
|
||||||
if (emoticonToIcon.contains(emoticon))
|
if (emoticonToIcon.find(emoticon) != emoticonToIcon.end())
|
||||||
return emoticonToIcon[emoticon];
|
return emoticonToIcon[emoticon];
|
||||||
|
|
||||||
const auto iconPathIt = emoticonToPath.find(emoticon);
|
const auto iconPathIt = emoticonToPath.find(emoticon);
|
||||||
|
@ -279,7 +303,7 @@ std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
|
||||||
|
|
||||||
const QString& iconPath = iconPathIt.value();
|
const QString& iconPath = iconPathIt.value();
|
||||||
auto icon = std::make_shared<QIcon>(iconPath);
|
auto icon = std::make_shared<QIcon>(iconPath);
|
||||||
emoticonToIcon.insert(emoticon, icon);
|
emoticonToIcon[emoticon] = icon;
|
||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
class QTimer;
|
||||||
|
|
||||||
class SmileyPack : public QObject
|
class SmileyPack : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -41,18 +43,21 @@ public:
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onSmileyPackChanged();
|
void onSmileyPackChanged();
|
||||||
|
void cleanup();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SmileyPack();
|
SmileyPack();
|
||||||
SmileyPack(SmileyPack&) = delete;
|
SmileyPack(SmileyPack&) = delete;
|
||||||
SmileyPack& operator=(const SmileyPack&) = delete;
|
SmileyPack& operator=(const SmileyPack&) = delete;
|
||||||
|
~SmileyPack() override;
|
||||||
|
|
||||||
bool load(const QString& filename);
|
bool load(const QString& filename);
|
||||||
|
|
||||||
QMap<QString, std::shared_ptr<QIcon>> emoticonToIcon;
|
std::map<QString, std::shared_ptr<QIcon>> emoticonToIcon;
|
||||||
QHash<QString, QString> emoticonToPath;
|
QHash<QString, QString> emoticonToPath;
|
||||||
QList<QStringList> emoticons;
|
QList<QStringList> emoticons;
|
||||||
QString path;
|
QString path;
|
||||||
|
QTimer* cleanupTimer;
|
||||||
mutable QMutex loadingMutex;
|
mutable QMutex loadingMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user