2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "historymodel.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "qupzilla.h"
|
2011-07-30 17:57:14 +02:00
|
|
|
#include "iconprovider.h"
|
2011-12-08 19:12:43 +01:00
|
|
|
#include "databasewriter.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-12-17 14:30:54 +01:00
|
|
|
HistoryModel::HistoryModel(QupZilla* mainClass)
|
|
|
|
: QObject()
|
2011-10-26 19:23:50 +02:00
|
|
|
, m_isSaving(true)
|
|
|
|
, p_QupZilla(mainClass)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
loadSettings();
|
2011-12-17 14:30:54 +01:00
|
|
|
|
2012-01-23 17:30:34 +01:00
|
|
|
qRegisterMetaType<HistoryEntry>("HistoryEntry");
|
2011-12-17 14:30:54 +01:00
|
|
|
|
|
|
|
QThread* t = new QThread(this);
|
|
|
|
t->start();
|
|
|
|
moveToThread(t);
|
|
|
|
|
2011-12-17 16:04:04 +01:00
|
|
|
connect(this, SIGNAL(signalAddHistoryEntry(QUrl, QString)), this, SLOT(slotAddHistoryEntry(QUrl, QString)));
|
2011-12-17 14:30:54 +01:00
|
|
|
connect(this, SIGNAL(signalDeleteHistoryEntry(int)), this, SLOT(slotDeleteHistoryEntry(int)));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryModel::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.beginGroup("Web-Browser-Settings");
|
2011-11-06 17:01:23 +01:00
|
|
|
m_isSaving = settings.value("allowHistory", true).toBool();
|
2011-04-26 19:47:12 +02:00
|
|
|
settings.endGroup();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 14:30:54 +01:00
|
|
|
// AddHistoryEntry
|
|
|
|
void HistoryModel::addHistoryEntry(WebView* view)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_isSaving) {
|
2011-12-17 14:30:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-04 19:43:43 +01:00
|
|
|
const QUrl &url = view->url();
|
|
|
|
const QString &title = view->title();
|
2011-12-17 14:30:54 +01:00
|
|
|
|
|
|
|
addHistoryEntry(url, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryModel::addHistoryEntry(const QUrl &url, QString title)
|
|
|
|
{
|
|
|
|
emit signalAddHistoryEntry(url, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryModel::slotAddHistoryEntry(const QUrl &url, QString title)
|
|
|
|
{
|
|
|
|
if (!m_isSaving) {
|
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-12-04 16:54:57 +01:00
|
|
|
if (url.scheme() == "file:" || url.scheme() == "qupzilla" || url.scheme() == "about" ||
|
|
|
|
title.contains(tr("Failed loading page")) || url.isEmpty()) {
|
2011-12-17 14:30:54 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (title == "") {
|
|
|
|
title = tr("No Named Page");
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM history WHERE url=?");
|
|
|
|
query.bindValue(0, url);
|
|
|
|
query.exec();
|
|
|
|
if (!query.next()) {
|
|
|
|
query.prepare("INSERT INTO history (count, date, url, title) VALUES (1,?,?,?)");
|
2011-03-18 18:35:09 +01:00
|
|
|
query.bindValue(0, QDateTime::currentMSecsSinceEpoch());
|
2011-03-02 16:57:41 +01:00
|
|
|
query.bindValue(1, url);
|
|
|
|
query.bindValue(2, title);
|
2011-12-17 14:30:54 +01:00
|
|
|
query.exec();
|
2011-04-25 20:56:45 +02:00
|
|
|
|
|
|
|
int id = query.lastInsertId().toInt();
|
|
|
|
HistoryEntry entry;
|
|
|
|
entry.id = id;
|
|
|
|
entry.count = 1;
|
|
|
|
entry.date = QDateTime::currentDateTime();
|
|
|
|
entry.url = url;
|
|
|
|
entry.title = title;
|
|
|
|
emit historyEntryAdded(entry);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-07-31 13:33:44 +02:00
|
|
|
int id = query.value(0).toInt();
|
2011-03-17 20:47:06 +01:00
|
|
|
query.prepare("UPDATE history SET count = count + 1, date=?, title=? WHERE url=?");
|
2011-03-18 18:35:09 +01:00
|
|
|
query.bindValue(0, QDateTime::currentMSecsSinceEpoch());
|
2011-03-17 20:47:06 +01:00
|
|
|
query.bindValue(1, title);
|
|
|
|
query.bindValue(2, url);
|
2011-12-17 14:30:54 +01:00
|
|
|
query.exec();
|
2011-07-31 13:33:44 +02:00
|
|
|
|
|
|
|
HistoryEntry before;
|
|
|
|
before.id = id;
|
|
|
|
|
|
|
|
HistoryEntry after;
|
|
|
|
after.id = id;
|
|
|
|
after.date = QDateTime::currentDateTime();
|
|
|
|
after.url = url;
|
|
|
|
after.title = title;
|
|
|
|
emit historyEntryEdited(before, after);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-12-17 14:30:54 +01:00
|
|
|
}
|
2011-12-04 16:54:57 +01:00
|
|
|
|
2011-12-17 14:30:54 +01:00
|
|
|
// DeleteHistoryEntry
|
|
|
|
void HistoryModel::deleteHistoryEntry(int index)
|
|
|
|
{
|
|
|
|
emit signalDeleteHistoryEntry(index);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 14:30:54 +01:00
|
|
|
void HistoryModel::deleteHistoryEntry(const QString &url, const QString &title)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-12-17 14:30:54 +01:00
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM history WHERE url=? AND title=?");
|
|
|
|
query.bindValue(0, url);
|
|
|
|
query.bindValue(1, title);
|
|
|
|
query.exec();
|
|
|
|
if (query.next()) {
|
|
|
|
int id = query.value(0).toInt();
|
|
|
|
deleteHistoryEntry(id);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 14:30:54 +01:00
|
|
|
void HistoryModel::slotDeleteHistoryEntry(int index)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QSqlQuery query;
|
2011-04-25 20:56:45 +02:00
|
|
|
query.prepare("SELECT id, count, date, url, title FROM history WHERE id=?");
|
|
|
|
query.bindValue(0, index);
|
|
|
|
query.exec();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!query.next()) {
|
2011-12-17 14:30:54 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-12-17 14:30:54 +01:00
|
|
|
|
2011-04-25 20:56:45 +02:00
|
|
|
HistoryEntry entry;
|
|
|
|
entry.id = query.value(0).toInt();
|
|
|
|
entry.count = query.value(1).toInt();
|
|
|
|
entry.date = QDateTime::fromMSecsSinceEpoch(query.value(2).toLongLong());
|
|
|
|
entry.url = query.value(3).toUrl();
|
|
|
|
entry.title = query.value(4).toString();
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
query.prepare("DELETE FROM history WHERE id=?");
|
|
|
|
query.bindValue(0, index);
|
2011-12-17 14:30:54 +01:00
|
|
|
query.exec();
|
2011-07-31 13:33:44 +02:00
|
|
|
query.prepare("DELETE FROM icons WHERE url=?");
|
|
|
|
query.bindValue(0, entry.url.toEncoded(QUrl::RemoveFragment));
|
2011-12-17 14:30:54 +01:00
|
|
|
query.exec();
|
2011-12-08 19:12:43 +01:00
|
|
|
|
|
|
|
emit historyEntryDeleted(entry);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-31 13:33:44 +02:00
|
|
|
bool HistoryModel::urlIsStored(const QString &url)
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM history WHERE url=?");
|
|
|
|
query.bindValue(0, url);
|
|
|
|
query.exec();
|
|
|
|
return query.next();
|
|
|
|
}
|
|
|
|
|
2012-01-23 17:30:34 +01:00
|
|
|
QList<HistoryEntry> HistoryModel::mostVisited(int count)
|
2011-05-06 20:05:49 +02:00
|
|
|
{
|
|
|
|
QList<HistoryEntry> list;
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec(QString("SELECT count, date, id, title, url FROM history ORDER BY count DESC LIMIT %1").arg(count));
|
2011-11-06 17:01:23 +01:00
|
|
|
while (query.next()) {
|
2011-05-06 20:05:49 +02:00
|
|
|
HistoryEntry entry;
|
|
|
|
entry.count = query.value(0).toInt();
|
|
|
|
entry.date = query.value(1).toDateTime();
|
|
|
|
entry.id = query.value(2).toInt();
|
|
|
|
entry.title = query.value(3).toString();
|
|
|
|
entry.url = query.value(4).toUrl();
|
|
|
|
list.append(entry);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
bool HistoryModel::optimizeHistory()
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
return query.exec("VACUUM");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryModel::clearHistory()
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
2011-04-25 20:56:45 +02:00
|
|
|
if (query.exec("DELETE FROM history")) {
|
|
|
|
emit historyClear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryModel::setSaving(bool state)
|
|
|
|
{
|
|
|
|
m_isSaving = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryModel::isSaving()
|
|
|
|
{
|
|
|
|
return m_isSaving;
|
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
|
|
|
QString HistoryModel::titleCaseLocalizedMonth(int month)
|
|
|
|
{
|
|
|
|
switch (month) {
|
|
|
|
case 1:
|
|
|
|
return tr("January");
|
|
|
|
case 2:
|
|
|
|
return tr("February");
|
|
|
|
case 3:
|
|
|
|
return tr("March");
|
|
|
|
case 4:
|
|
|
|
return tr("April");
|
|
|
|
case 5:
|
|
|
|
return tr("May");
|
|
|
|
case 6:
|
|
|
|
return tr("June");
|
|
|
|
case 7:
|
|
|
|
return tr("July");
|
|
|
|
case 8:
|
|
|
|
return tr("August");
|
|
|
|
case 9:
|
|
|
|
return tr("September");
|
|
|
|
case 10:
|
|
|
|
return tr("October");
|
|
|
|
case 11:
|
|
|
|
return tr("November");
|
|
|
|
case 12:
|
|
|
|
return tr("December");
|
|
|
|
default:
|
|
|
|
qWarning("Month number out of range!");
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|