From 66fb43cdefd0de3946f8d6c931f4568384bdf57b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Ander=20Pe=C3=B1alba?= Date: Mon, 20 Sep 2010 22:35:58 +0200 Subject: History autosaver improved to be more general and less hackish --- src/history/autosaver.cpp | 67 +++++++++++++++++++++--------------------- src/history/autosaver.h | 31 +++++++++++-------- src/history/historymanager.cpp | 43 +++++++++++++-------------- 3 files changed, 72 insertions(+), 69 deletions(-) diff --git a/src/history/autosaver.cpp b/src/history/autosaver.cpp index 83b3f6b0..3b966b52 100644 --- a/src/history/autosaver.cpp +++ b/src/history/autosaver.cpp @@ -25,73 +25,72 @@ * ============================================================ */ - - // Self Includes #include "autosaver.h" #include "autosaver.moc" // Qt Includes #include +#include +#include +#include -#define AUTOSAVE_IN 1000 * 3 // seconds -#define MAXWAIT 1000 * 15 // seconds +const int AUTOSAVE_TIME = 1000 * 3; // seconds +const int MAX_TIME_LIMIT = 1000 * 15; // seconds -AutoSaver::AutoSaver(QObject *parent) : QObject(parent) +AutoSaver::AutoSaver(QObject *parent) + : QObject(parent) + , m_timer(new QBasicTimer) + , m_firstChange(new QTime) { - Q_ASSERT(parent); } AutoSaver::~AutoSaver() { - if (m_timer.isActive()) - { + if (m_timer->isActive()) kDebug() << "AutoSaver: still active when destroyed, changes not saved."; - } + + delete m_firstChange; + delete m_timer; +} + + +void AutoSaver::saveIfNeccessary() +{ + if (m_timer->isActive()) + save(); } void AutoSaver::changeOccurred() { - if (m_firstChange.isNull()) - m_firstChange.start(); + if (m_firstChange->isNull()) + m_firstChange->start(); - if (m_firstChange.elapsed() > MAXWAIT) - { - saveIfNeccessary(); - } + if (m_firstChange->elapsed() > MAX_TIME_LIMIT) + save(); else - { - m_timer.start(AUTOSAVE_IN, this); - } + m_timer->start(AUTOSAVE_TIME, this); } void AutoSaver::timerEvent(QTimerEvent *event) { - if (event->timerId() == m_timer.timerId()) - { - saveIfNeccessary(); - } + if (event->timerId() == m_timer->timerId()) + save(); else - { QObject::timerEvent(event); - } } -void AutoSaver::saveIfNeccessary() +void AutoSaver::save() { - if (!m_timer.isActive()) - return; - m_timer.stop(); - m_firstChange = QTime(); - if (!QMetaObject::invokeMethod(parent(), "save", Qt::DirectConnection)) - { - kDebug() << "AutoSaver: error invoking slot save() on parent"; - } -} + m_timer->stop(); + delete m_firstChange; + m_firstChange = new QTime; + emit saveNeeded(); +} diff --git a/src/history/autosaver.h b/src/history/autosaver.h index 6927dbe8..685783a8 100644 --- a/src/history/autosaver.h +++ b/src/history/autosaver.h @@ -34,37 +34,42 @@ // Qt Includes #include -#include -#include -#include +// Forward Declarations +class QBasicTimer; /** - * This class will call the save() slot on the parent object when the parent changes. - * It will wait several seconds after changed() to combining multiple changes and - * prevent continuous writing to disk. - * + * This class emits the saveNeeded() signal. + * It will wait several seconds after changeOccurred() to combine + * multiple changes preventing continuous writing to disk. */ - class REKONQ_TESTS_EXPORT AutoSaver : public QObject { Q_OBJECT public: - AutoSaver(QObject *parent); - ~AutoSaver(); + explicit AutoSaver(QObject *parent); + virtual ~AutoSaver(); + + /** + * Emits the saveNeeded() signal if there's been any change since we last saved. + */ void saveIfNeccessary(); +signals: + void saveNeeded(); + public slots: void changeOccurred(); protected: - void timerEvent(QTimerEvent *event); + virtual void timerEvent(QTimerEvent *event); private: - QBasicTimer m_timer; - QTime m_firstChange; + void save(); + QBasicTimer *m_timer; + QTime *m_firstChange; }; #endif // AUTOSAVER_H diff --git a/src/history/historymanager.cpp b/src/history/historymanager.cpp index 4a2c0459..92f9caee 100644 --- a/src/history/historymanager.cpp +++ b/src/history/historymanager.cpp @@ -72,6 +72,7 @@ HistoryManager::HistoryManager(QObject *parent) connect(this, SIGNAL(entryAdded(const HistoryItem &)), m_saveTimer, SLOT(changeOccurred())); connect(this, SIGNAL(entryRemoved(const HistoryItem &)), m_saveTimer, SLOT(changeOccurred())); + connect(m_saveTimer, SIGNAL(saveNeeded()), this, SLOT(save())); load(); @@ -88,11 +89,9 @@ HistoryManager::HistoryManager(QObject *parent) HistoryManager::~HistoryManager() { m_saveTimer->saveIfNeccessary(); - + delete m_historyFilterModel; delete m_historyTreeModel; - - delete m_saveTimer; } @@ -107,7 +106,7 @@ void HistoryManager::addHistoryEntry(const QString &url) QWebSettings *globalSettings = QWebSettings::globalSettings(); if (globalSettings->testAttribute(QWebSettings::PrivateBrowsingEnabled)) return; - + QUrl cleanUrl(url); // don't store about: urls (home page related) @@ -129,7 +128,7 @@ void HistoryManager::addHistoryEntry(const QString &url) void HistoryManager::setHistory(const QList &history, bool loadedAndSorted) { m_history = history; - + // verify that it is sorted by date if (!loadedAndSorted) qSort(m_history.begin(), m_history.end()); @@ -189,14 +188,14 @@ void HistoryManager::updateHistoryEntry(const KUrl &url, const QString &title) urlString.remove(QL1S("www.")); if(urlString.startsWith(QL1S("http")) && urlString.endsWith(QL1C('/'))) urlString.remove(urlString.length()-1,1); - + for (int i = 0; i < m_history.count(); ++i) { QString itemUrl = m_history.at(i).url; itemUrl.remove(QL1S("www.")); if(itemUrl.startsWith(QL1S("http")) && itemUrl.endsWith(QL1C('/'))) itemUrl.remove(itemUrl.length()-1,1); - + if (urlString == itemUrl) { m_history[i].title = title; @@ -204,7 +203,7 @@ void HistoryManager::updateHistoryEntry(const KUrl &url, const QString &title) m_saveTimer->changeOccurred(); if (m_lastSavedUrl.isEmpty()) m_lastSavedUrl = m_history.at(i).url; - + emit entryUpdated(i); break; } @@ -253,7 +252,7 @@ QList HistoryManager::find(const QString &text) if (matches) list << item; } - + return list; } @@ -274,25 +273,25 @@ void HistoryManager::loadSettings() int days; switch (historyExpire) { - case 0: - days = 1; + case 0: + days = 1; break; - case 1: - days = 7; + case 1: + days = 7; break; - case 2: - days = 14; + case 2: + days = 14; break; - case 3: - days = 30; + case 3: + days = 30; break; - case 4: - days = 365; + case 4: + days = 365; break; - case 5: - days = -1; + case 5: + days = -1; break; - default: + default: days = -1; break; } -- cgit v1.2.1