1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-14 19:12:11 +01:00
falkonOfficial/src/lib/history/historymanager.cpp

340 lines
10 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* 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 "historymanager.h"
#include "ui_historymanager.h"
#include "qupzilla.h"
#include "mainapplication.h"
#include "historymodel.h"
#include "iconprovider.h"
#include "browsinglibrary.h"
#include "globalfunctions.h"
#include "tabwidget.h"
2011-03-02 16:57:41 +01:00
#include <QMenu>
#include <QMessageBox>
#include <QClipboard>
#include <QTimer>
#include <QSqlQuery>
HistoryManager::HistoryManager(QupZilla* mainClass, QWidget* parent)
: QWidget(parent)
, ui(new Ui::HistoryManager)
, p_QupZilla(mainClass)
, m_historyModel(mApp->history())
2011-03-02 16:57:41 +01:00
{
ui->setupUi(this);
ui->historyTree->setDefaultItemShowMode(TreeWidget::ItemsCollapsed);
ui->historyTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->deleteB->setShortcut(QKeySequence("Del"));
2011-03-02 16:57:41 +01:00
connect(ui->historyTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(itemDoubleClicked(QTreeWidgetItem*)));
connect(ui->historyTree, SIGNAL(itemMiddleButtonClicked(QTreeWidgetItem*)), this, SLOT(itemDoubleClicked(QTreeWidgetItem*)));
2011-03-02 16:57:41 +01:00
connect(ui->deleteB, SIGNAL(clicked()), this, SLOT(deleteItem()));
connect(ui->clearAll, SIGNAL(clicked()), this, SLOT(clearHistory()));
connect(ui->historyTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &)));
connect(m_historyModel, SIGNAL(historyEntryAdded(HistoryEntry)), this, SLOT(historyEntryAdded(HistoryEntry)));
connect(m_historyModel, SIGNAL(historyEntryDeleted(HistoryEntry)), this, SLOT(historyEntryDeleted(HistoryEntry)));
connect(m_historyModel, SIGNAL(historyEntryEdited(HistoryEntry, HistoryEntry)), this, SLOT(historyEntryEdited(HistoryEntry, HistoryEntry)));
connect(m_historyModel, SIGNAL(historyClear()), ui->historyTree, SLOT(clear()));
connect(ui->optimizeDb, SIGNAL(clicked(QPoint)), this, SLOT(optimizeDb()));
2011-03-02 16:57:41 +01:00
//QTimer::singleShot(0, this, SLOT(refreshTable()));
ui->historyTree->setFocus();
2011-03-02 16:57:41 +01:00
}
QupZilla* HistoryManager::getQupZilla()
{
if (!p_QupZilla) {
2011-03-04 13:59:07 +01:00
p_QupZilla = mApp->getWindow();
}
return p_QupZilla.data();
2011-03-02 16:57:41 +01:00
}
2011-03-17 17:03:04 +01:00
void HistoryManager::setMainWindow(QupZilla* window)
2011-03-02 16:57:41 +01:00
{
if (window) {
2011-03-02 16:57:41 +01:00
p_QupZilla = window;
}
2011-03-02 16:57:41 +01:00
}
2011-03-17 17:03:04 +01:00
void HistoryManager::itemDoubleClicked(QTreeWidgetItem* item)
2011-03-02 16:57:41 +01:00
{
if (!item || item->text(1).isEmpty()) {
2011-03-02 16:57:41 +01:00
return;
}
QUrl url = QUrl::fromEncoded(item->text(1).toUtf8());
getQupZilla()->tabWidget()->addView(url, item->text(0));
2011-03-02 16:57:41 +01:00
}
void HistoryManager::loadInNewTab()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
getQupZilla()->tabWidget()->addView(action->data().toUrl(), Qz::NT_NotSelectedTab);
}
2011-03-02 16:57:41 +01:00
}
void HistoryManager::contextMenuRequested(const QPoint &position)
{
if (!ui->historyTree->itemAt(position)) {
2011-03-02 16:57:41 +01:00
return;
}
QUrl link = QUrl::fromEncoded(ui->historyTree->itemAt(position)->text(1).toUtf8());
if (link.isEmpty()) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
QMenu menu;
menu.addAction(tr("Open link in current tab"), getQupZilla(), SLOT(loadActionUrl()))->setData(link);
2011-03-02 16:57:41 +01:00
menu.addAction(tr("Open link in new tab"), this, SLOT(loadInNewTab()))->setData(link);
menu.addSeparator();
menu.addAction(tr("Copy address"), this, SLOT(copyUrl()))->setData(link);
2011-03-02 16:57:41 +01:00
//Prevent choosing first option with double rightclick
QPoint pos = QCursor::pos();
QPoint p(pos.x(), pos.y() + 1);
2011-03-02 16:57:41 +01:00
menu.exec(p);
}
void HistoryManager::copyUrl()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
QApplication::clipboard()->setText(action->data().toUrl().toEncoded());
}
}
2011-03-02 16:57:41 +01:00
void HistoryManager::deleteItem()
{
QList<int> list;
foreach(QTreeWidgetItem * item, ui->historyTree->selectedItems()) {
if (!item) {
continue;
}
if (!item->parent()) {
QList<QTreeWidgetItem*> items;
for (int i = 0; i < item->childCount(); i++) {
QTreeWidgetItem* children = item->child(i);
if (children->isHidden()) {
continue;
}
int id = children->data(0, Qt::UserRole + 10).toInt();
list.append(id);
m_ignoredIds.append(id);
items.append(children);
}
if (item->childCount() == 0) {
items.append(item);
}
ui->historyTree->deleteItems(items);
}
else {
int id = item->data(0, Qt::UserRole + 10).toInt();
list.append(id);
m_ignoredIds.append(id);
ui->historyTree->deleteItem(item);
}
}
m_historyModel->deleteHistoryEntry(list);
}
void HistoryManager::historyEntryAdded(const HistoryEntry &entry)
{
QDate todayDate = QDate::currentDate();
QDate startOfWeekDate = todayDate.addDays(1 - todayDate.dayOfWeek());
QDate date = entry.date.date();
QString localDate;
if (date == todayDate) {
localDate = tr("Today");
}
else if (date >= startOfWeekDate) {
localDate = tr("This Week");
}
else if (date.month() == todayDate.month()) {
localDate = tr("This Month");
}
else {
localDate = QString("%1 %2").arg(HistoryModel::titleCaseLocalizedMonth(date.month()), QString::number(date.year()));
}
QTreeWidgetItem* item = new QTreeWidgetItem();
QTreeWidgetItem* parentItem;
QList<QTreeWidgetItem*> findParent = ui->historyTree->findItems(localDate, 0);
if (findParent.count() == 1) {
parentItem = findParent.at(0);
}
else {
parentItem = new QTreeWidgetItem();
parentItem->setText(0, localDate);
parentItem->setIcon(0, QIcon(":/icons/menu/history_entry.png"));
ui->historyTree->insertTopLevelItem(0, parentItem);
}
item->setText(0, entry.title);
item->setText(1, entry.url.toEncoded());
item->setToolTip(0, entry.title);
item->setToolTip(1, entry.url.toEncoded());
item->setData(0, Qt::UserRole + 10, entry.id);
item->setIcon(0, _iconForUrl(entry.url));
ui->historyTree->prependToParentItem(parentItem, item);
}
void HistoryManager::historyEntryDeleted(const HistoryEntry &entry)
{
if (m_ignoredIds.contains(entry.id)) {
m_ignoredIds.removeOne(entry.id);
return;
}
QList<QTreeWidgetItem*> list = ui->historyTree->allItems();
foreach(QTreeWidgetItem * item, list) {
if (!item) {
continue;
}
if (item->data(0, Qt::UserRole + 10).toInt() != entry.id) {
continue;
}
ui->historyTree->deleteItem(item);
return;
}
2011-03-02 16:57:41 +01:00
}
void HistoryManager::historyEntryEdited(const HistoryEntry &before, const HistoryEntry &after)
2011-07-31 13:33:44 +02:00
{
historyEntryDeleted(before);
historyEntryAdded(after);
}
2011-03-02 16:57:41 +01:00
void HistoryManager::clearHistory()
{
QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
tr("Are you sure to delete all history?"), QMessageBox::Yes | QMessageBox::No);
if (button != QMessageBox::Yes) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
m_historyModel->clearHistory();
m_historyModel->optimizeHistory();
2011-03-02 16:57:41 +01:00
}
void HistoryManager::refreshTable()
{
QTimer::singleShot(0, this, SLOT(slotRefreshTable()));
}
void HistoryManager::slotRefreshTable()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
2011-03-02 16:57:41 +01:00
ui->historyTree->clear();
QDate todayDate = QDate::currentDate();
QDate startOfWeekDate = todayDate.addDays(1 - todayDate.dayOfWeek());
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("SELECT title, url, id, date FROM history ORDER BY date DESC");
int counter = 0;
QHash<QString, QTreeWidgetItem*> hash;
while (query.next()) {
const QString &title = query.value(0).toString();
const QUrl &url = query.value(1).toUrl();
2011-03-02 16:57:41 +01:00
int id = query.value(2).toInt();
const QDate &date = QDateTime::fromMSecsSinceEpoch(query.value(3).toLongLong()).date();
QString localDate;
if (date == todayDate) {
localDate = tr("Today");
}
else if (date >= startOfWeekDate) {
localDate = tr("This Week");
}
else if (date.month() == todayDate.month()) {
localDate = tr("This Month");
}
else {
localDate = QString("%1 %2").arg(HistoryModel::titleCaseLocalizedMonth(date.month()), QString::number(date.year()));
}
2011-03-02 16:57:41 +01:00
QTreeWidgetItem* item;
QTreeWidgetItem* findParent = hash[localDate];
if (findParent) {
item = new QTreeWidgetItem(findParent);
}
else {
2011-03-02 16:57:41 +01:00
QTreeWidgetItem* newParent = new QTreeWidgetItem(ui->historyTree);
newParent->setText(0, localDate);
newParent->setIcon(0, QIcon(":/icons/menu/history_entry.png"));
ui->historyTree->addTopLevelItem(newParent);
hash[localDate] = newParent;
2011-03-02 16:57:41 +01:00
item = new QTreeWidgetItem(newParent);
}
item->setText(0, title);
item->setText(1, url.toEncoded());
item->setToolTip(0, title);
item->setToolTip(1, url.toEncoded());
item->setData(0, Qt::UserRole + 10, id);
item->setIcon(0, _iconForUrl(url));
2011-03-02 16:57:41 +01:00
ui->historyTree->addTopLevelItem(item);
++counter;
if (counter > 200) {
QApplication::processEvents();
counter = 0;
}
2011-03-02 16:57:41 +01:00
}
QApplication::restoreOverrideCursor();
2011-03-02 16:57:41 +01:00
}
void HistoryManager::search(const QString &searchText)
2011-03-02 16:57:41 +01:00
{
ui->historyTree->filterString(searchText);
2011-03-02 16:57:41 +01:00
}
void HistoryManager::optimizeDb()
{
BrowsingLibrary* b = qobject_cast<BrowsingLibrary*>(parentWidget()->parentWidget());
if (!b) {
return;
}
b->optimizeDatabase();
}
2011-03-02 16:57:41 +01:00
HistoryManager::~HistoryManager()
{
delete ui;
}