summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/history/autosaver.cpp67
-rw-r--r--src/history/autosaver.h31
-rw-r--r--src/history/historymanager.cpp43
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 <QtCore/QMetaObject>
+#include <QtCore/QTimerEvent>
+#include <QtCore/QBasicTimer>
+#include <QtCore/QTime>
-#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 <QtCore/QObject>
-#include <QtCore/QBasicTimer>
-#include <QtCore/QTime>
-#include <QtCore/QTimerEvent>
+// 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<HistoryItem> &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<HistoryItem> 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;
}