From 70afca89b21dda18dc5e94858bec472da9130cda Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Tue, 2 Apr 2013 14:25:56 +0200 Subject: preparing smilies support --- board.cpp | 37 +++++++++++++++++++++++++++++++++---- board.h | 12 ++++++++++-- global.h | 3 +++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/board.cpp b/board.cpp index fc9e0ad..4275047 100644 --- a/board.cpp +++ b/board.cpp @@ -72,6 +72,14 @@ void Board::enqueueAction(Action *action) QString Board::getConfig(const QString &key) const { + // Try config cache first + QHash::const_iterator i = _config.find(key); + if (i != _config.end()) { + // Cache hit + return i.value(); + } + + // Try database QSqlQuery query(_db); query.prepare("SELECT key, value FROM config WHERE key = :key"); query.bindValue(":key", key); @@ -80,13 +88,27 @@ QString Board::getConfig(const QString &key) const return QString(); } if (query.next()) { - return query.value(1).toString(); + QString value = query.value(1).toString(); + _config[key] = value; // Store in cache + return value; } return QString(); } void Board::setConfig(const QString &key, const QString &value) { + + // Try config cache first + QHash::const_iterator i = _config.find(key); + if (i != _config.end()) { + QString old_value = i.value(); + if (old_value == value) { + // It's in the cache, and it's the same value: don't change. + return; + } + } + + // Update value in DB QSqlQuery query(_db); query.prepare("INSERT OR REPLACE INTO config (key, value) VALUES (:key, :value)"); query.bindValue(":key", key); @@ -94,7 +116,8 @@ void Board::setConfig(const QString &key, const QString &value) if (!query.exec()) { qWarning() << "Could not set configuration key" << key << ":" << query.lastError().text(); } - notifyConfigChanged(); + _config.insert(key, value); + notifyConfigChanged(key); } QString Board::removeHtml(QString text) const @@ -141,9 +164,15 @@ QString Board::renderHumanTime(const QDateTime &dateTime) return dateTime.toLocalTime().time().toString(Qt::DefaultLocaleShortDate); } -void Board::notifyConfigChanged() +void Board::notifyConfigChanged(const QString& key) { - emit configChanged(); + if (!key.isEmpty()) { + _config.remove(key); + } else { + // Must assume all keys were changed + _config.clear(); + } + emit configChanged(key); } void Board::notifyForumsChanged() diff --git a/board.h b/board.h index facc339..78ebf8a 100644 --- a/board.h +++ b/board.h @@ -2,6 +2,7 @@ #define BOARD_H #include +#include #include #include #include @@ -38,14 +39,16 @@ public: QString renderHumanTime(const QDateTime& dateTime); // These functions wrap emitting the signals below - void notifyConfigChanged(); + void notifyConfigChanged(const QString& key = QString()); void notifyForumsChanged(); + void notifySmiliesChanged(); void notifyForumTopicsChanged(int forumId, int start, int end); void notifyTopicPostsChanged(int topicId, int start, int end); signals: - void configChanged(); + void configChanged(const QString& key); void forumsChanged(); + void smiliesChanged(); void forumTopicsChanged(int forumId, int start, int end); void topicPostsChanged(int topicId, int start, int end); @@ -60,8 +63,10 @@ private: bool removeFromActionQueue(Action *action); void executeActionFromQueue(); void initializeBbCode(); + void initializeSmilies(); void fetchConfigIfOutdated(); void fetchForumsIfOutdated(); + void fetchSmiliesIfOutdated(); private slots: void handleActionFinished(Action *action); @@ -73,7 +78,10 @@ private: QSqlDatabase _db; XmlRpcInterface *_iface; QQueue _queue; + /** Configuration cache */ + mutable QHash _config; QList< QPair > _bbcodes; + QList< QPair > _smilies; }; inline bool Board::busy() const diff --git a/global.h b/global.h index 4ecc890..3b1d6ae 100644 --- a/global.h +++ b/global.h @@ -6,6 +6,9 @@ /** Time the forum config settings should be considered up to date, in days. */ #define BOARD_CONFIG_TTL 2 +/** Time the forum smilies list should be considered up to date, in days. */ +#define SMILIES_LIST_TTL BOARD_CONFIG_TLL + /** Time the list of forums should be considered up to date, in days. */ #define BOARD_LIST_TTL 2 -- cgit v1.2.3