1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: Use loaded icons as cache, make getAsIcon const

This commit is contained in:
Diadlo 2017-11-01 20:33:28 +03:00
parent fa21594902
commit 47da91d74f
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 13 additions and 12 deletions

View File

@ -111,7 +111,7 @@ SmileyPack::SmileyPack()
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this,
&SmileyPack::onSmileyPackChanged);
connect(cleanupTimer, &QTimer::timeout, this, &SmileyPack::cleanup);
connect(cleanupTimer, &QTimer::timeout, this, &SmileyPack::cleanupIconsCache);
cleanupTimer->start(CLEANUP_TIMEOUT);
}
@ -120,13 +120,13 @@ SmileyPack::~SmileyPack()
delete cleanupTimer;
}
void SmileyPack::cleanup()
void SmileyPack::cleanupIconsCache()
{
QMutexLocker locker(&loadingMutex);
for (auto it = emoticonToIcon.begin(); it != emoticonToIcon.end();) {
for (auto it = cachedIcon.begin(); it != cachedIcon.end();) {
std::shared_ptr<QIcon>& icon = it->second;
if (icon.use_count() == 1) {
it = emoticonToIcon.erase(it);
it = cachedIcon.erase(it);
icon.reset();
} else {
++it;
@ -226,8 +226,8 @@ bool SmileyPack::load(const QString& filename)
const QString childName = QStringLiteral("string");
const int iconsCount = emoticonElements.size();
emoticons.clear();
emoticonToIcon.clear();
emoticonToPath.clear();
cachedIcon.clear();
for (int i = 0; i < iconsCount; ++i) {
QDomNode node = emoticonElements.at(i);
@ -290,11 +290,12 @@ QList<QStringList> SmileyPack::getEmoticons() const
* @param emoticon Passed emoticon
* @return Returns cached icon according to passed emoticon, null if no icon mapped to this emoticon
*/
std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon) const
{
QMutexLocker locker(&loadingMutex);
if (emoticonToIcon.find(emoticon) != emoticonToIcon.end())
return emoticonToIcon[emoticon];
if (cachedIcon.find(emoticon) != cachedIcon.end()) {
return cachedIcon[emoticon];
}
const auto iconPathIt = emoticonToPath.find(emoticon);
if (iconPathIt == emoticonToPath.end()) {
@ -303,7 +304,7 @@ std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon)
const QString& iconPath = iconPathIt.value();
auto icon = std::make_shared<QIcon>(iconPath);
emoticonToIcon[emoticon] = icon;
cachedIcon[emoticon] = icon;
return icon;
}

View File

@ -39,11 +39,11 @@ public:
QString smileyfied(const QString& msg);
QList<QStringList> getEmoticons() const;
std::shared_ptr<QIcon> getAsIcon(const QString& key);
std::shared_ptr<QIcon> getAsIcon(const QString& key) const;
private slots:
void onSmileyPackChanged();
void cleanup();
void cleanupIconsCache();
private:
SmileyPack();
@ -53,7 +53,7 @@ private:
bool load(const QString& filename);
std::map<QString, std::shared_ptr<QIcon>> emoticonToIcon;
mutable std::map<QString, std::shared_ptr<QIcon>> cachedIcon;
QHash<QString, QString> emoticonToPath;
QList<QStringList> emoticons;
QString path;