File indexing completed on 2024-05-19 04:58:48

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "historymanager.h"
0019 #include "ui_historymanager.h"
0020 #include "browserwindow.h"
0021 #include "mainapplication.h"
0022 #include "history.h"
0023 #include "tabwidget.h"
0024 #include "tabbedwebview.h"
0025 #include "headerview.h"
0026 #include "qzsettings.h"
0027 #include "iconprovider.h"
0028 
0029 #include <QMessageBox>
0030 #include <QClipboard>
0031 #include <QKeyEvent>
0032 
0033 HistoryManager::HistoryManager(BrowserWindow* window, QWidget* parent)
0034     : QWidget(parent)
0035     , ui(new Ui::HistoryManager)
0036     , m_window(window)
0037 {
0038     ui->setupUi(this);
0039     ui->historyTree->setViewType(HistoryTreeView::HistoryManagerViewType);
0040 
0041     connect(ui->historyTree, &HistoryTreeView::urlActivated, this, &HistoryManager::urlActivated);
0042     connect(ui->historyTree, &HistoryTreeView::urlCtrlActivated, this, &HistoryManager::urlCtrlActivated);
0043     connect(ui->historyTree, &HistoryTreeView::urlShiftActivated, this, &HistoryManager::urlShiftActivated);
0044     connect(ui->historyTree, &HistoryTreeView::contextMenuRequested, this, &HistoryManager::createContextMenu);
0045 
0046     connect(ui->deleteB, &QAbstractButton::clicked, ui->historyTree, &HistoryTreeView::removeSelectedItems);
0047     connect(ui->clearAll, &QAbstractButton::clicked, this, &HistoryManager::clearHistory);
0048 
0049     ui->historyTree->setFocus();
0050 }
0051 
0052 BrowserWindow* HistoryManager::getWindow()
0053 {
0054     if (!m_window)
0055         m_window = mApp->getWindow();
0056     return m_window.data();
0057 }
0058 
0059 void HistoryManager::setMainWindow(BrowserWindow* window)
0060 {
0061     if (window) {
0062         m_window = window;
0063     }
0064 }
0065 
0066 void HistoryManager::restoreState(const QByteArray &state)
0067 {
0068     ui->historyTree->header()->restoreState(state);
0069 }
0070 
0071 QByteArray HistoryManager::saveState()
0072 {
0073     return ui->historyTree->header()->saveState();
0074 }
0075 
0076 void HistoryManager::clearHistory()
0077 {
0078     QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
0079                                          tr("Are you sure you want to delete all history?"), QMessageBox::Yes | QMessageBox::No);
0080     if (button != QMessageBox::Yes) {
0081         return;
0082     }
0083 
0084     mApp->history()->clearHistory();
0085 }
0086 
0087 void HistoryManager::keyPressEvent(QKeyEvent *event)
0088 {
0089     switch (event->key()) {
0090     case Qt::Key_Delete:
0091         ui->historyTree->removeSelectedItems();
0092         break;
0093     }
0094 
0095     QWidget::keyPressEvent(event);
0096 }
0097 
0098 void HistoryManager::search(const QString &searchText)
0099 {
0100     ui->historyTree->search(searchText);
0101 }
0102 
0103 void HistoryManager::urlActivated(const QUrl &url)
0104 {
0105     openUrl(url);
0106 }
0107 
0108 void HistoryManager::urlCtrlActivated(const QUrl &url)
0109 {
0110     openUrlInNewTab(url);
0111 }
0112 
0113 void HistoryManager::urlShiftActivated(const QUrl &url)
0114 {
0115     openUrlInNewWindow(url);
0116 }
0117 
0118 void HistoryManager::openUrl(const QUrl &url)
0119 {
0120     const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
0121     m_window->weView()->load(u);
0122 }
0123 
0124 void HistoryManager::openUrlInNewTab(const QUrl &url)
0125 {
0126     const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
0127     m_window->tabWidget()->addView(u, qzSettings->newTabPosition);
0128 }
0129 
0130 void HistoryManager::openUrlInNewWindow(const QUrl &url)
0131 {
0132     const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
0133     mApp->createWindow(Qz::BW_NewWindow, u);
0134 }
0135 
0136 void HistoryManager::openUrlInNewPrivateWindow(const QUrl &url)
0137 {
0138     const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
0139     mApp->startPrivateBrowsing(u);
0140 }
0141 
0142 void HistoryManager::createContextMenu(const QPoint &pos)
0143 {
0144     QMenu menu;
0145     QAction* actNewTab = menu.addAction(IconProvider::newTabIcon(), tr("Open in new tab"));
0146     QAction* actNewWindow = menu.addAction(IconProvider::newWindowIcon(), tr("Open in new window"));
0147     QAction* actNewPrivateWindow = menu.addAction(IconProvider::privateBrowsingIcon(), tr("Open in new private window"));
0148 
0149     menu.addSeparator();
0150     QAction* actCopyUrl = menu.addAction(tr("Copy url"), this, &HistoryManager::copyUrl);
0151     QAction* actCopyTitle = menu.addAction(tr("Copy title"), this, &HistoryManager::copyTitle);
0152 
0153     menu.addSeparator();
0154     QAction* actDelete = menu.addAction(QIcon::fromTheme(QSL("edit-delete")), tr("Delete"));
0155 
0156     connect(actNewTab, SIGNAL(triggered()), this, SLOT(openUrlInNewTab()));
0157     connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openUrlInNewWindow()));
0158     connect(actNewPrivateWindow, SIGNAL(triggered()), this, SLOT(openUrlInNewPrivateWindow()));
0159     connect(actDelete, &QAction::triggered, ui->historyTree, &HistoryTreeView::removeSelectedItems);
0160 
0161     if (ui->historyTree->selectedUrl().isEmpty()) {
0162         actNewTab->setDisabled(true);
0163         actNewWindow->setDisabled(true);
0164         actNewPrivateWindow->setDisabled(true);
0165         actCopyTitle->setDisabled(true);
0166         actCopyUrl->setDisabled(true);
0167     }
0168 
0169     menu.exec(pos);
0170 }
0171 
0172 void HistoryManager::copyUrl()
0173 {
0174     QApplication::clipboard()->setText(ui->historyTree->selectedUrl().toString());
0175 }
0176 
0177 void HistoryManager::copyTitle()
0178 {
0179     QApplication::clipboard()->setText(ui->historyTree->currentIndex().data().toString());
0180 }
0181 
0182 HistoryManager::~HistoryManager()
0183 {
0184     delete ui;
0185 }