summaryrefslogtreecommitdiffstats
path: root/src/core/HistoryModel.cpp
blob: 830e7c6cfefc8d27f125102f93db132c8cb8c621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/****************************************************************************
 *   Copyright (C) 2012  Instituto Nokia de Tecnologia (INdT)               *
 *                                                                          *
 *   This file may be used under the terms of the GNU Lesser                *
 *   General Public License version 2.1 as published by the Free Software   *
 *   Foundation and appearing in the file LICENSE.LGPL included in the      *
 *   packaging of this file.  Please review the following information to    *
 *   ensure the GNU Lesser General Public License version 2.1 requirements  *
 *   will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.   *
 *                                                                          *
 *   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 Lesser General Public License for more details.                    *
 ****************************************************************************/

#include "HistoryModel.h"

#include <QtCore/QDateTime>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>

HistoryModel::HistoryModel(QSqlDatabase database, QObject *parent)
    : QSqlTableModel(parent,  database)
{
}

void HistoryModel::generateRoleNames()
{
    for (int i = 0; i < this->columnCount(); i++)
        m_roles[Qt::UserRole + i] = this->headerData(i, Qt::Horizontal).toByteArray();

    setRoleNames(m_roles);
}

QString HistoryModel::tableCreateQuery() const
{
    const QLatin1String historyQuery("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT,"
                                     "url VARCHAR, title VARCHAR, visitsCount INTEGER, lastVisit DATE);");
    return historyQuery;
}

void HistoryModel::populate()
{
    sortByRecentlyVisited();
    select();
}

void HistoryModel::insert(const QString& url, const QString& title)
{
    if (url.isEmpty())
        return;

    QSqlQuery query(database());
    int visits = visitsCount(url);
    if (visits) {
        static QString updateStatement = QLatin1String("UPDATE history SET visitsCount = ?, title = ?, lastVisit = ? WHERE url = ?");
        query.prepare(updateStatement);
    } else {
        static QString insertStatement = QLatin1String("INSERT INTO history (visitsCount, title, lastVisit, url) VALUES (?, ?, ?, ?)");
        query.prepare(insertStatement);
    }
    query.addBindValue(++visits);
    query.addBindValue(title);
    query.addBindValue(QDateTime::currentDateTime().toTime_t());
    query.addBindValue(url);
    query.exec();
    select();
}

int HistoryModel::visitsCount(const QString& url)
{
    QSqlQuery query(database());
    static QString selectStatement = QLatin1String("SELECT visitsCount FROM history WHERE url = '%1'");
    query.prepare(selectStatement.arg(url));
    query.exec();
    return query.first() ? query.value(0).toInt() : 0;
}

QVariant HistoryModel::data(const QModelIndex& index, int role) const
{
    if (role < Qt::UserRole)
        return QVariant();

    const int columnId = role - Qt::UserRole;
    const QModelIndex modelIndex = createIndex(index.row(), columnId);
    return QSqlTableModel::data(modelIndex, Qt::DisplayRole);
}


void HistoryModel::setFilterString(const QString& filterString)
{
    if (m_filterString == filterString)
        return;

    if (filterString.isEmpty()) {
        sortByRecentlyVisited();
        setFilter("");
    } else {
        static QString filterBase = QLatin1String("url like '%%1%' OR title like '%%1%'");
        sortByMostVisited();
        setFilter(filterBase.arg(filterString));
    }
    m_filterString = filterString;
    emit filterStringChanged();

    // FIXME: explicit call here to avoid not displaying results when user provides
    // a filter with a sql query injected. Injection wont work but for a bit the internal
    // select on setFilter won't be triggered.
    select();
}

void HistoryModel::sortByRecentlyVisited()
{
    setSort(HistoryModel::LastVisit, Qt::DescendingOrder);
}

void HistoryModel::sortByMostVisited()
{
    setSort(HistoryModel::VisitsCount, Qt::DescendingOrder);
}