summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJavier S. Pedro <maemo@javispedro.com>2013-04-02 14:25:56 +0200
committerJavier S. Pedro <maemo@javispedro.com>2013-04-02 14:25:56 +0200
commit70afca89b21dda18dc5e94858bec472da9130cda (patch)
tree9158cd9979f3c41c704f86ee6ea0da4274994e54
parent226690b61e546294ae102c4a4b48b56555609c57 (diff)
downloadtapasboard-70afca89b21dda18dc5e94858bec472da9130cda.tar.gz
tapasboard-70afca89b21dda18dc5e94858bec472da9130cda.zip
preparing smilies support
-rw-r--r--board.cpp37
-rw-r--r--board.h12
-rw-r--r--global.h3
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<QString, QString>::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<QString, QString>::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 <QtCore/QDateTime>
+#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QPair>
#include <QtCore/QQueue>
@@ -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<Action*> _queue;
+ /** Configuration cache */
+ mutable QHash<QString, QString> _config;
QList< QPair<QRegExp, QString> > _bbcodes;
+ QList< QPair<QString, QString> > _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