diff --git a/src/persistence/smileypack.cpp b/src/persistence/smileypack.cpp index d7d0c2edb..97c68cdeb 100644 --- a/src/persistence/smileypack.cpp +++ b/src/persistence/smileypack.cpp @@ -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& 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 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 SmileyPack::getAsIcon(const QString& emoticon) +std::shared_ptr 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 SmileyPack::getAsIcon(const QString& emoticon) const QString& iconPath = iconPathIt.value(); auto icon = std::make_shared(iconPath); - emoticonToIcon[emoticon] = icon; + cachedIcon[emoticon] = icon; return icon; } diff --git a/src/persistence/smileypack.h b/src/persistence/smileypack.h index f1cf63f4f..0f6c96e5a 100644 --- a/src/persistence/smileypack.h +++ b/src/persistence/smileypack.h @@ -39,11 +39,11 @@ public: QString smileyfied(const QString& msg); QList getEmoticons() const; - std::shared_ptr getAsIcon(const QString& key); + std::shared_ptr 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> emoticonToIcon; + mutable std::map> cachedIcon; QHash emoticonToPath; QList emoticons; QString path;