mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Async smiley loading for fast start
This can be a ~1s win on startup time with a HDD, now we load and cache the smileys in the background, blocking only if we try to use them while they are still loading
This commit is contained in:
parent
24bccb7bdd
commit
94f3e6d6e4
|
@ -33,10 +33,12 @@
|
||||||
#include <QDomElement>
|
#include <QDomElement>
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
#include <QStringBuilder>
|
#include <QStringBuilder>
|
||||||
|
#include <QtConcurrent/QtConcurrentRun>
|
||||||
|
|
||||||
SmileyPack::SmileyPack()
|
SmileyPack::SmileyPack()
|
||||||
{
|
{
|
||||||
load(Settings::getInstance().getSmileyPack());
|
loadingMutex.lock();
|
||||||
|
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
|
||||||
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this, &SmileyPack::onSmileyPackChanged);
|
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this, &SmileyPack::onSmileyPackChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +103,10 @@ bool SmileyPack::load(const QString& filename)
|
||||||
// open emoticons.xml
|
// open emoticons.xml
|
||||||
QFile xmlFile(filename);
|
QFile xmlFile(filename);
|
||||||
if (!xmlFile.open(QIODevice::ReadOnly))
|
if (!xmlFile.open(QIODevice::ReadOnly))
|
||||||
|
{
|
||||||
|
loadingMutex.unlock();
|
||||||
return false; // cannot open file
|
return false; // cannot open file
|
||||||
|
}
|
||||||
|
|
||||||
/* parse the cfg file
|
/* parse the cfg file
|
||||||
* sample:
|
* sample:
|
||||||
|
@ -151,11 +156,14 @@ bool SmileyPack::load(const QString& filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
// success!
|
// success!
|
||||||
|
loadingMutex.unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SmileyPack::smileyfied(QString msg)
|
QString SmileyPack::smileyfied(QString msg)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&loadingMutex);
|
||||||
|
|
||||||
QRegExp exp("\\S+"); // matches words
|
QRegExp exp("\\S+"); // matches words
|
||||||
|
|
||||||
int index = msg.indexOf(exp);
|
int index = msg.indexOf(exp);
|
||||||
|
@ -179,6 +187,7 @@ QString SmileyPack::smileyfied(QString msg)
|
||||||
|
|
||||||
QList<QStringList> SmileyPack::getEmoticons() const
|
QList<QStringList> SmileyPack::getEmoticons() const
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&loadingMutex);
|
||||||
return emoticons;
|
return emoticons;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,6 +198,7 @@ QString SmileyPack::getAsRichText(const QString &key)
|
||||||
|
|
||||||
QIcon SmileyPack::getAsIcon(const QString &key)
|
QIcon SmileyPack::getAsIcon(const QString &key)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&loadingMutex);
|
||||||
return getCachedSmiley(key);
|
return getCachedSmiley(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,5 +227,6 @@ QIcon SmileyPack::getCachedSmiley(const QString &key)
|
||||||
|
|
||||||
void SmileyPack::onSmileyPackChanged()
|
void SmileyPack::onSmileyPackChanged()
|
||||||
{
|
{
|
||||||
load(Settings::getInstance().getSmileyPack());
|
loadingMutex.lock();
|
||||||
|
QtConcurrent::run(this, &SmileyPack::load, Settings::getInstance().getSmileyPack());
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
#define SMILEYPACK_SEARCH_PATHS \
|
#define SMILEYPACK_SEARCH_PATHS \
|
||||||
{ \
|
{ \
|
||||||
|
@ -40,7 +41,6 @@ public:
|
||||||
static QList<QPair<QString, QString> > listSmileyPacks(const QStringList& paths = SMILEYPACK_SEARCH_PATHS);
|
static QList<QPair<QString, QString> > listSmileyPacks(const QStringList& paths = SMILEYPACK_SEARCH_PATHS);
|
||||||
static bool isValid(const QString& filename);
|
static bool isValid(const QString& filename);
|
||||||
|
|
||||||
bool load(const QString& filename);
|
|
||||||
QString smileyfied(QString msg);
|
QString smileyfied(QString msg);
|
||||||
QList<QStringList> getEmoticons() const;
|
QList<QStringList> getEmoticons() const;
|
||||||
QString getAsRichText(const QString& key);
|
QString getAsRichText(const QString& key);
|
||||||
|
@ -54,6 +54,7 @@ private:
|
||||||
SmileyPack(SmileyPack&) = delete;
|
SmileyPack(SmileyPack&) = delete;
|
||||||
SmileyPack& operator=(const 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);
|
void cacheSmiley(const QString& name);
|
||||||
QIcon getCachedSmiley(const QString& key);
|
QIcon getCachedSmiley(const QString& key);
|
||||||
|
|
||||||
|
@ -61,6 +62,7 @@ private:
|
||||||
QHash<QString, QIcon> iconCache; // representation of a smiley ie. "happy.png" -> data
|
QHash<QString, QIcon> iconCache; // representation of a smiley ie. "happy.png" -> data
|
||||||
QList<QStringList> emoticons; // {{ ":)", ":-)" }, {":(", ...}, ... }
|
QList<QStringList> emoticons; // {{ ":)", ":-)" }, {":(", ...}, ... }
|
||||||
QString path; // directory containing the cfg and image files
|
QString path; // directory containing the cfg and image files
|
||||||
|
mutable QMutex loadingMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SMILEYPACK_H
|
#endif // SMILEYPACK_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user