diff --git a/src/persistence/smileypack.cpp b/src/persistence/smileypack.cpp index e3d80a735..b56facea5 100644 --- a/src/persistence/smileypack.cpp +++ b/src/persistence/smileypack.cpp @@ -33,10 +33,12 @@ #include #include #include +#include SmileyPack::SmileyPack() { - load(Settings::getInstance().getSmileyPack()); + loadingMutex.lock(); + QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack()); connect(&Settings::getInstance(), &Settings::smileyPackChanged, this, &SmileyPack::onSmileyPackChanged); } @@ -101,7 +103,10 @@ bool SmileyPack::load(const QString& filename) // open emoticons.xml QFile xmlFile(filename); if (!xmlFile.open(QIODevice::ReadOnly)) + { + loadingMutex.unlock(); return false; // cannot open file + } /* parse the cfg file * sample: @@ -151,11 +156,14 @@ bool SmileyPack::load(const QString& filename) } // success! + loadingMutex.unlock(); return true; } QString SmileyPack::smileyfied(QString msg) { + QMutexLocker locker(&loadingMutex); + QRegExp exp("\\S+"); // matches words int index = msg.indexOf(exp); @@ -179,6 +187,7 @@ QString SmileyPack::smileyfied(QString msg) QList SmileyPack::getEmoticons() const { + QMutexLocker locker(&loadingMutex); return emoticons; } @@ -189,6 +198,7 @@ QString SmileyPack::getAsRichText(const QString &key) QIcon SmileyPack::getAsIcon(const QString &key) { + QMutexLocker locker(&loadingMutex); return getCachedSmiley(key); } @@ -217,5 +227,6 @@ QIcon SmileyPack::getCachedSmiley(const QString &key) void SmileyPack::onSmileyPackChanged() { - load(Settings::getInstance().getSmileyPack()); + loadingMutex.lock(); + QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack()); } diff --git a/src/persistence/smileypack.h b/src/persistence/smileypack.h index 262be026a..9281dca3c 100644 --- a/src/persistence/smileypack.h +++ b/src/persistence/smileypack.h @@ -25,6 +25,7 @@ #include #include #include +#include #define SMILEYPACK_SEARCH_PATHS \ { \ @@ -40,7 +41,6 @@ public: static QList > listSmileyPacks(const QStringList& paths = SMILEYPACK_SEARCH_PATHS); static bool isValid(const QString& filename); - bool load(const QString& filename); QString smileyfied(QString msg); QList getEmoticons() const; QString getAsRichText(const QString& key); @@ -54,6 +54,7 @@ private: SmileyPack(SmileyPack&) = delete; SmileyPack& operator=(const SmileyPack&) = delete; + bool load(const QString& filename); ///< The caller must lock loadingMutex and should run it in a thread void cacheSmiley(const QString& name); QIcon getCachedSmiley(const QString& key); @@ -61,6 +62,7 @@ private: QHash iconCache; // representation of a smiley ie. "happy.png" -> data QList emoticons; // {{ ":)", ":-)" }, {":(", ...}, ... } QString path; // directory containing the cfg and image files + mutable QMutex loadingMutex; }; #endif // SMILEYPACK_H