2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-02-08 12:10:12 +01:00
|
|
|
* Copyright (C) 2010-2013 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-04-30 16:33:14 +02:00
|
|
|
#include "historyitem.h"
|
2011-07-30 17:57:14 +02:00
|
|
|
#include "iconprovider.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
#include <QApplication>
|
2012-03-10 12:21:45 +01:00
|
|
|
#include <QSqlQuery>
|
2012-04-30 16:33:14 +02:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTimer>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2013-01-22 18:12:21 +01:00
|
|
|
static QString dateTimeToString(const QDateTime &dateTime)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
const QDateTime ¤t = QDateTime::currentDateTime();
|
|
|
|
if (current.date() == dateTime.date()) {
|
|
|
|
return dateTime.time().toString("h:mm");
|
|
|
|
}
|
|
|
|
|
|
|
|
return dateTime.toString("d.M.yyyy h:mm");
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryModel::HistoryModel(History* history)
|
|
|
|
: QAbstractItemModel(history)
|
|
|
|
, m_rootItem(new HistoryItem(0))
|
|
|
|
, m_todayItem(0)
|
|
|
|
, m_history(history)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
init();
|
|
|
|
|
|
|
|
connect(m_history, SIGNAL(resetHistory()), this, SLOT(resetHistory()));
|
2013-03-06 09:05:41 +01:00
|
|
|
connect(m_history, SIGNAL(historyEntryAdded(HistoryEntry)), this, SLOT(historyEntryAdded(HistoryEntry)));
|
|
|
|
connect(m_history, SIGNAL(historyEntryDeleted(HistoryEntry)), this, SLOT(historyEntryDeleted(HistoryEntry)));
|
|
|
|
connect(m_history, SIGNAL(historyEntryEdited(HistoryEntry,HistoryEntry)), this, SLOT(historyEntryEdited(HistoryEntry,HistoryEntry)));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
QVariant HistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch (section) {
|
|
|
|
case 0:
|
|
|
|
return tr("Title");
|
|
|
|
case 1:
|
|
|
|
return tr("Address");
|
|
|
|
case 2:
|
|
|
|
return tr("Visit Date");
|
|
|
|
case 3:
|
|
|
|
return tr("Visit Count");
|
|
|
|
}
|
2011-12-17 14:30:54 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
return QAbstractItemModel::headerData(section, orientation, role);
|
|
|
|
}
|
2011-12-17 14:30:54 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
HistoryItem* item = itemFromIndex(index);
|
|
|
|
|
|
|
|
if (index.row() < 0 || !item) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->isTopLevel()) {
|
|
|
|
switch (role) {
|
|
|
|
case IsTopLevelRole:
|
|
|
|
return true;
|
|
|
|
case TimestampStartRole:
|
|
|
|
return item->startTimestamp();
|
|
|
|
case TimestampEndRole:
|
|
|
|
return item->endTimestamp();
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Qt::EditRole:
|
|
|
|
return index.column() == 0 ? item->title : QVariant();
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
return index.column() == 0 ? QIcon(":/icons/menu/history_entry.png") : QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-02-08 12:10:12 +01:00
|
|
|
const HistoryEntry entry = item->historyEntry;
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case IdRole:
|
|
|
|
return entry.id;
|
|
|
|
case TitleRole:
|
|
|
|
return entry.title;
|
|
|
|
case UrlRole:
|
|
|
|
return entry.url;
|
|
|
|
case UrlStringRole:
|
|
|
|
return entry.urlString;
|
|
|
|
case IconRole:
|
|
|
|
return item->icon();
|
|
|
|
case IconLoadedRole:
|
|
|
|
return item->iconLoaded();
|
|
|
|
case IsTopLevelRole:
|
|
|
|
return false;
|
|
|
|
case TimestampStartRole:
|
|
|
|
return -1;
|
|
|
|
case TimestampEndRole:
|
|
|
|
return -1;
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
if (index.column() == 0) {
|
|
|
|
return QString("%1\n%2").arg(entry.title, entry.urlString);
|
|
|
|
}
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Qt::EditRole:
|
|
|
|
switch (index.column()) {
|
|
|
|
case 0:
|
|
|
|
return entry.title;
|
|
|
|
case 1:
|
|
|
|
return entry.urlString;
|
|
|
|
case 2:
|
|
|
|
return dateTimeToString(entry.date);
|
|
|
|
case 3:
|
|
|
|
return entry.count;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
if (index.column() == 0) {
|
|
|
|
return item->icon().isNull() ? qIconProvider->emptyWebIcon() : item->icon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
2011-12-17 14:30:54 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
bool HistoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
2011-12-17 14:30:54 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* item = itemFromIndex(index);
|
|
|
|
|
|
|
|
if (index.row() < 0 || !item || item->isTopLevel()) {
|
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
if (role == IconRole) {
|
|
|
|
item->setIcon(value.value<QIcon>());
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex HistoryModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (!hasIndex(row, column, parent)) {
|
|
|
|
return QModelIndex();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* parentItem = itemFromIndex(parent);
|
|
|
|
HistoryItem* childItem = parentItem->child(row);
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
return childItem ? createIndex(row, column, childItem) : QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex HistoryModel::parent(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QModelIndex();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-31 13:33:44 +02:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* childItem = itemFromIndex(index);
|
|
|
|
HistoryItem* parentItem = childItem->parent();
|
2011-07-31 13:33:44 +02:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
if (!parentItem || parentItem == m_rootItem) {
|
|
|
|
return QModelIndex();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
return createIndex(parentItem->row(), 0, parentItem);
|
2011-12-17 14:30:54 +01:00
|
|
|
}
|
2011-12-04 16:54:57 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
Qt::ItemFlags HistoryModel::flags(const QModelIndex &index) const
|
2011-12-17 14:30:54 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
if (!index.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-10 12:21:45 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
2012-03-10 12:21:45 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
int HistoryModel::rowCount(const QModelIndex &parent) const
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
if (parent.column() > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-17 14:30:54 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* parentItem = itemFromIndex(parent);
|
|
|
|
|
|
|
|
return parentItem->childCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int HistoryModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryModel::hasChildren(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItem* item = itemFromIndex(parent);
|
|
|
|
|
|
|
|
return item ? item->isTopLevel() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItem* HistoryModel::itemFromIndex(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.isValid()) {
|
|
|
|
HistoryItem* item = static_cast<HistoryItem*>(index.internalPointer());
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_rootItem;
|
|
|
|
}
|
|
|
|
|
2012-07-04 00:21:08 +02:00
|
|
|
void HistoryModel::removeTopLevelIndexes(const QList<QPersistentModelIndex> &indexes)
|
|
|
|
{
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QPersistentModelIndex &index, indexes) {
|
2012-07-04 00:21:08 +02:00
|
|
|
if (index.parent().isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
HistoryItem* item = m_rootItem->child(row);
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
delete item;
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
if (item == m_todayItem) {
|
|
|
|
m_todayItem = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryModel::resetHistory()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
delete m_rootItem;
|
2012-11-20 09:49:23 +01:00
|
|
|
m_todayItem = 0;
|
2012-04-30 16:33:14 +02:00
|
|
|
m_rootItem = new HistoryItem(0);
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryModel::canFetchMore(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
HistoryItem* parentItem = itemFromIndex(parent);
|
|
|
|
|
|
|
|
return parentItem ? parentItem->canFetchMore : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryModel::fetchMore(const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
HistoryItem* parentItem = itemFromIndex(parent);
|
|
|
|
|
|
|
|
if (!parent.isValid() || !parentItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentItem->canFetchMore = false;
|
|
|
|
|
|
|
|
QList<int> idList;
|
|
|
|
for (int i = 0; i < parentItem->childCount(); ++i) {
|
|
|
|
idList.append(parentItem->child(i)->historyEntry.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id, count, title, url, date FROM history WHERE date BETWEEN ? AND ? ORDER BY date DESC");
|
|
|
|
query.addBindValue(parentItem->endTimestamp());
|
|
|
|
query.addBindValue(parentItem->startTimestamp());
|
|
|
|
query.exec();
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
QVector<HistoryEntry> list;
|
2011-04-25 20:56:45 +02:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
while (query.next()) {
|
2012-03-10 12:21:45 +01:00
|
|
|
HistoryEntry entry;
|
|
|
|
entry.id = query.value(0).toInt();
|
|
|
|
entry.count = query.value(1).toInt();
|
2012-04-30 16:33:14 +02:00
|
|
|
entry.title = query.value(2).toString();
|
2012-03-10 12:21:45 +01:00
|
|
|
entry.url = query.value(3).toUrl();
|
2012-04-30 16:33:14 +02:00
|
|
|
entry.date = QDateTime::fromMSecsSinceEpoch(query.value(4).toLongLong());
|
|
|
|
entry.urlString = entry.url.toEncoded();
|
2012-03-10 12:21:45 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
if (!idList.contains(entry.id)) {
|
|
|
|
list.append(entry);
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 12:21:45 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
if (list.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-10 12:21:45 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
beginInsertRows(parent, 0, list.size() - 1);
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const HistoryEntry &entry, list) {
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* newItem = new HistoryItem(parentItem);
|
|
|
|
newItem->historyEntry = entry;
|
2012-03-10 12:21:45 +01:00
|
|
|
}
|
2011-12-08 19:12:43 +01:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
endInsertRows();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryModel::historyEntryAdded(const HistoryEntry &entry)
|
2012-04-04 14:45:45 +02:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
if (!m_todayItem) {
|
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
|
|
|
|
|
|
|
m_todayItem = new HistoryItem(0);
|
|
|
|
m_todayItem->setStartTimestamp(-1);
|
|
|
|
m_todayItem->setEndTimestamp(QDateTime(QDate::currentDate()).toMSecsSinceEpoch());
|
|
|
|
m_todayItem->title = tr("Today");
|
|
|
|
|
|
|
|
m_rootItem->prependChild(m_todayItem);
|
|
|
|
|
|
|
|
endInsertRows();
|
2012-04-04 14:45:45 +02:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
beginInsertRows(createIndex(0, 0, m_todayItem), 0, 0);
|
|
|
|
|
|
|
|
HistoryItem* item = new HistoryItem();
|
|
|
|
item->historyEntry = entry;
|
|
|
|
|
|
|
|
m_todayItem->prependChild(item);
|
|
|
|
|
|
|
|
endInsertRows();
|
2012-04-04 14:45:45 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryModel::historyEntryDeleted(const HistoryEntry &entry)
|
2011-07-31 13:33:44 +02:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* item = findHistoryItem(entry);
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItem* parentItem = item->parent();
|
|
|
|
int row = item->row();
|
|
|
|
|
|
|
|
beginRemoveRows(createIndex(parentItem->row(), 0, parentItem), row, row);
|
|
|
|
delete item;
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
checkEmptyParentItem(parentItem);
|
2011-07-31 13:33:44 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryModel::historyEntryEdited(const HistoryEntry &before, const HistoryEntry &after)
|
2011-05-06 20:05:49 +02:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
#if 0
|
|
|
|
HistoryItem* item = findHistoryItem(before);
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
HistoryItem* parentItem = item->parent();
|
|
|
|
const QModelIndex &sourceParent = createIndex(parentItem->row(), 0, parentItem);
|
|
|
|
const QModelIndex &destinationParent = createIndex(m_todayItem->row(), 0, m_todayItem);
|
|
|
|
int row = item->row();
|
|
|
|
|
|
|
|
beginMoveRows(sourceParent, row, row, destinationParent, 0);
|
|
|
|
item->historyEntry = after;
|
|
|
|
item->refreshIcon();
|
|
|
|
item->changeParent(m_todayItem);
|
|
|
|
endMoveRows(); // This line sometimes throw "std::bad_alloc" ... I don't know why ?!
|
|
|
|
|
|
|
|
checkEmptyParentItem(parentItem);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
historyEntryAdded(after);
|
2011-05-06 20:05:49 +02:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
#endif
|
|
|
|
historyEntryDeleted(before);
|
|
|
|
historyEntryAdded(after);
|
2011-05-06 20:05:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* HistoryModel::findHistoryItem(const HistoryEntry &entry)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
HistoryItem* parentItem = 0;
|
|
|
|
qint64 timestamp = entry.date.toMSecsSinceEpoch();
|
|
|
|
|
|
|
|
for (int i = 0; i < m_rootItem->childCount(); ++i) {
|
|
|
|
HistoryItem* item = m_rootItem->child(i);
|
|
|
|
|
|
|
|
if (item->endTimestamp() < timestamp) {
|
|
|
|
parentItem = item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentItem) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < parentItem->childCount(); ++i) {
|
|
|
|
HistoryItem* item = parentItem->child(i);
|
|
|
|
if (item->historyEntry.id == entry.id) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryModel::checkEmptyParentItem(HistoryItem* item)
|
|
|
|
{
|
|
|
|
if (item->childCount() == 0 && item->isTopLevel()) {
|
|
|
|
int row = item->row();
|
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
delete item;
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
if (item == m_todayItem) {
|
|
|
|
m_todayItem = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryModel::init()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QSqlQuery query;
|
2012-04-30 16:33:14 +02:00
|
|
|
query.exec("SELECT MIN(date) FROM history");
|
2012-12-08 11:39:03 +01:00
|
|
|
if (!query.next()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-08 12:10:12 +01:00
|
|
|
const qint64 minTimestamp = query.value(0).toLongLong();
|
2012-12-08 11:39:03 +01:00
|
|
|
if (minTimestamp <= 0) {
|
2012-04-30 16:33:14 +02:00
|
|
|
return;
|
2011-04-25 20:56:45 +02:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
const QDate &today = QDate::currentDate();
|
|
|
|
const QDate &week = today.addDays(1 - today.dayOfWeek());
|
|
|
|
const QDate &month = QDate(today.year(), today.month(), 1);
|
2013-02-08 12:10:12 +01:00
|
|
|
const qint64 currentTimestamp = QDateTime::currentMSecsSinceEpoch();
|
2012-04-30 16:33:14 +02:00
|
|
|
|
|
|
|
qint64 timestamp = currentTimestamp;
|
|
|
|
while (timestamp > minTimestamp) {
|
|
|
|
QDate timestampDate = QDateTime::fromMSecsSinceEpoch(timestamp).date();
|
|
|
|
qint64 endTimestamp;
|
|
|
|
QString itemName;
|
|
|
|
|
|
|
|
if (timestampDate == today) {
|
|
|
|
endTimestamp = QDateTime(today).toMSecsSinceEpoch();
|
|
|
|
|
|
|
|
itemName = tr("Today");
|
|
|
|
}
|
|
|
|
else if (timestampDate >= week) {
|
|
|
|
endTimestamp = QDateTime(week).toMSecsSinceEpoch();
|
|
|
|
|
|
|
|
itemName = tr("This Week");
|
|
|
|
}
|
|
|
|
else if (timestampDate.month() == month.month() && timestampDate.year() == month.year()) {
|
|
|
|
endTimestamp = QDateTime(month).toMSecsSinceEpoch();
|
|
|
|
|
|
|
|
itemName = tr("This Month");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QDate startDate(timestampDate.year(), timestampDate.month(), timestampDate.daysInMonth());
|
|
|
|
QDate endDate(startDate.year(), startDate.month(), 1);
|
|
|
|
|
|
|
|
timestamp = QDateTime(startDate, QTime(23, 59, 59)).toMSecsSinceEpoch();
|
|
|
|
endTimestamp = QDateTime(endDate).toMSecsSinceEpoch();
|
|
|
|
itemName = QString("%1 %2").arg(History::titleCaseLocalizedMonth(timestampDate.month()), QString::number(timestampDate.year()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("SELECT id FROM history WHERE date BETWEEN ? AND ? LIMIT 1");
|
|
|
|
query.addBindValue(endTimestamp);
|
|
|
|
query.addBindValue(timestamp);
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.next()) {
|
|
|
|
HistoryItem* item = new HistoryItem(m_rootItem);
|
|
|
|
item->setStartTimestamp(timestamp == currentTimestamp ? -1 : timestamp);
|
|
|
|
item->setEndTimestamp(endTimestamp);
|
|
|
|
item->title = itemName;
|
|
|
|
item->canFetchMore = true;
|
|
|
|
|
|
|
|
if (timestamp == currentTimestamp) {
|
|
|
|
m_todayItem = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp = endTimestamp - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryFilterModel::HistoryFilterModel(QAbstractItemModel* parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
setSourceModel(parent);
|
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
m_filterTimer = new QTimer(this);
|
|
|
|
m_filterTimer->setSingleShot(true);
|
|
|
|
m_filterTimer->setInterval(500);
|
|
|
|
|
|
|
|
connect(m_filterTimer, SIGNAL(timeout()), this, SLOT(startFiltering()));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryFilterModel::setFilterFixedString(const QString &pattern)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
m_pattern = pattern;
|
|
|
|
|
|
|
|
m_filterTimer->stop();
|
|
|
|
m_filterTimer->start();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
void HistoryFilterModel::startFiltering()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
if (m_pattern.isEmpty()) {
|
|
|
|
emit collapseAllItems();
|
|
|
|
QSortFilterProxyModel::setFilterFixedString(m_pattern);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
|
|
|
|
// Expand all items also calls fetchmore
|
|
|
|
emit expandAllItems();
|
|
|
|
|
|
|
|
QSortFilterProxyModel::setFilterFixedString(m_pattern);
|
|
|
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
2012-04-30 16:33:14 +02:00
|
|
|
bool HistoryFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
2012-04-30 16:33:14 +02:00
|
|
|
const QModelIndex &index = sourceModel()->index(sourceRow, 0, sourceParent);
|
|
|
|
|
|
|
|
if (index.data(HistoryModel::IsTopLevelRole).toBool()) {
|
|
|
|
return true;
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|
2012-04-30 16:33:14 +02:00
|
|
|
|
2012-11-19 15:04:25 +01:00
|
|
|
return (index.data(HistoryModel::UrlStringRole).toString().contains(m_pattern, Qt::CaseInsensitive) ||
|
|
|
|
index.data(HistoryModel::TitleRole).toString().contains(m_pattern, Qt::CaseInsensitive));
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|