Skip to content

Commit

Permalink
cleanup: Stop loading smileypack in a separate thread.
Browse files Browse the repository at this point in the history
It takes 10ms to load the file. There's no need to do this in a separate
thread anymore.
  • Loading branch information
iphydf committed Dec 7, 2024
1 parent eaf7705 commit ae63ec9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
35 changes: 12 additions & 23 deletions src/persistence/smileypack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,14 @@ SmileyPack::SmileyPack(ISmileySettings& settings_)
: cleanupTimer{new QTimer(this)}
, settings{settings_}
{
loadingMutex.lock();
QThreadPool::globalInstance()->start([this]() { load(settings.getSmileyPack()); });
settings.connectTo_smileyPackChanged(this, [&](const QString&) { onSmileyPackChanged(); });
load(settings.getSmileyPack());

settings.connectTo_smileyPackChanged(this, [this](const QString& filename) { load(filename); });
connect(cleanupTimer, &QTimer::timeout, this, &SmileyPack::cleanupIconsCache);
cleanupTimer->start(CLEANUP_TIMEOUT);
}

SmileyPack::~SmileyPack()
{
delete cleanupTimer;
}
SmileyPack::~SmileyPack() = default;

/**
* @brief Wraps passed string into smiley HTML image reference
Expand Down Expand Up @@ -175,17 +172,18 @@ QList<QPair<QString, QString>> SmileyPack::listSmileyPacks(const QStringList& pa
}

/**
* @brief Load smile pack
* @note The caller must lock loadingMutex and should run it in a thread
* @param filename Filename of smilepack.
* @return False if cannot open file, true otherwise.
* @brief Load smiley pack.
*
* @param filename Filename of smiley pack.
*/
bool SmileyPack::load(const QString& filename)
void SmileyPack::load(const QString& filename)
{
QMutexLocker<QMutex> locker(&loadingMutex);

QFile xmlFile(filename);
if (!xmlFile.exists() || !xmlFile.open(QIODevice::ReadOnly)) {
loadingMutex.unlock();
return false;
qWarning() << "SmileyPack: Could not open file" << filename;
return;
}

QDomDocument doc;
Expand Down Expand Up @@ -233,9 +231,6 @@ bool SmileyPack::load(const QString& filename)
}

constructRegex();

loadingMutex.unlock();
return true;
}

/**
Expand Down Expand Up @@ -330,9 +325,3 @@ std::shared_ptr<QIcon> SmileyPack::getAsIcon(const QString& emoticon) const
cachedIcon[emoticon] = icon;
return icon;
}

void SmileyPack::onSmileyPackChanged()
{
loadingMutex.lock();
QThreadPool::globalInstance()->start([this]() { load(settings.getSmileyPack()); });
}
3 changes: 1 addition & 2 deletions src/persistence/smileypack.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ class SmileyPack : public QObject
static QString getAsRichText(const QString& key);

private slots:
void onSmileyPackChanged();
void load(const QString& filename);
void cleanupIconsCache();

private:
bool load(const QString& filename);
void constructRegex();

mutable std::map<QString, std::shared_ptr<QIcon>> cachedIcon;
Expand Down

0 comments on commit ae63ec9

Please sign in to comment.