Falkon Develop
Cross-platform Qt-based web browser
historymanager.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "historymanager.h"
19#include "ui_historymanager.h"
20#include "browserwindow.h"
21#include "mainapplication.h"
22#include "history.h"
23#include "tabwidget.h"
24#include "tabbedwebview.h"
25#include "headerview.h"
26#include "qzsettings.h"
27#include "iconprovider.h"
28
29#include <QMessageBox>
30#include <QClipboard>
31#include <QKeyEvent>
32
34 : QWidget(parent)
35 , ui(new Ui::HistoryManager)
36 , m_window(window)
37{
38 ui->setupUi(this);
39 ui->historyTree->setViewType(HistoryTreeView::HistoryManagerViewType);
40
41 connect(ui->historyTree, &HistoryTreeView::urlActivated, this, &HistoryManager::urlActivated);
42 connect(ui->historyTree, &HistoryTreeView::urlCtrlActivated, this, &HistoryManager::urlCtrlActivated);
43 connect(ui->historyTree, &HistoryTreeView::urlShiftActivated, this, &HistoryManager::urlShiftActivated);
44 connect(ui->historyTree, &HistoryTreeView::contextMenuRequested, this, &HistoryManager::createContextMenu);
45
46 connect(ui->deleteB, &QAbstractButton::clicked, ui->historyTree, &HistoryTreeView::removeSelectedItems);
47 connect(ui->clearAll, &QAbstractButton::clicked, this, &HistoryManager::clearHistory);
48
49 ui->historyTree->setFocus();
50}
51
52BrowserWindow* HistoryManager::getWindow()
53{
54 if (!m_window)
55 m_window = mApp->getWindow();
56 return m_window.data();
57}
58
60{
61 if (window) {
62 m_window = window;
63 }
64}
65
66void HistoryManager::restoreState(const QByteArray &state)
67{
68 ui->historyTree->header()->restoreState(state);
69}
70
72{
73 return ui->historyTree->header()->saveState();
74}
75
76void HistoryManager::clearHistory()
77{
78 QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
79 tr("Are you sure you want to delete all history?"), QMessageBox::Yes | QMessageBox::No);
80 if (button != QMessageBox::Yes) {
81 return;
82 }
83
84 mApp->history()->clearHistory();
85}
86
87void HistoryManager::keyPressEvent(QKeyEvent *event)
88{
89 switch (event->key()) {
90 case Qt::Key_Delete:
91 ui->historyTree->removeSelectedItems();
92 break;
93 }
94
95 QWidget::keyPressEvent(event);
96}
97
98void HistoryManager::search(const QString &searchText)
99{
100 ui->historyTree->search(searchText);
101}
102
103void HistoryManager::urlActivated(const QUrl &url)
104{
105 openUrl(url);
106}
107
108void HistoryManager::urlCtrlActivated(const QUrl &url)
109{
110 openUrlInNewTab(url);
111}
112
113void HistoryManager::urlShiftActivated(const QUrl &url)
114{
115 openUrlInNewWindow(url);
116}
117
118void HistoryManager::openUrl(const QUrl &url)
119{
120 const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
121 m_window->weView()->load(u);
122}
123
124void HistoryManager::openUrlInNewTab(const QUrl &url)
125{
126 const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
127 m_window->tabWidget()->addView(u, qzSettings->newTabPosition);
128}
129
130void HistoryManager::openUrlInNewWindow(const QUrl &url)
131{
132 const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
133 mApp->createWindow(Qz::BW_NewWindow, u);
134}
135
136void HistoryManager::openUrlInNewPrivateWindow(const QUrl &url)
137{
138 const QUrl u = !url.isEmpty() ? url : ui->historyTree->selectedUrl();
139 mApp->startPrivateBrowsing(u);
140}
141
142void HistoryManager::createContextMenu(const QPoint &pos)
143{
144 QMenu menu;
145 QAction* actNewTab = menu.addAction(IconProvider::newTabIcon(), tr("Open in new tab"));
146 QAction* actNewWindow = menu.addAction(IconProvider::newWindowIcon(), tr("Open in new window"));
147 QAction* actNewPrivateWindow = menu.addAction(IconProvider::privateBrowsingIcon(), tr("Open in new private window"));
148
149 menu.addSeparator();
150 QAction* actCopyUrl = menu.addAction(tr("Copy url"), this, &HistoryManager::copyUrl);
151 QAction* actCopyTitle = menu.addAction(tr("Copy title"), this, &HistoryManager::copyTitle);
152
153 menu.addSeparator();
154 QAction* actDelete = menu.addAction(QIcon::fromTheme(QSL("edit-delete")), tr("Delete"));
155
156 connect(actNewTab, SIGNAL(triggered()), this, SLOT(openUrlInNewTab()));
157 connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openUrlInNewWindow()));
158 connect(actNewPrivateWindow, SIGNAL(triggered()), this, SLOT(openUrlInNewPrivateWindow()));
159 connect(actDelete, &QAction::triggered, ui->historyTree, &HistoryTreeView::removeSelectedItems);
160
161 if (ui->historyTree->selectedUrl().isEmpty()) {
162 actNewTab->setDisabled(true);
163 actNewWindow->setDisabled(true);
164 actNewPrivateWindow->setDisabled(true);
165 actCopyTitle->setDisabled(true);
166 actCopyUrl->setDisabled(true);
167 }
168
169 menu.exec(pos);
170}
171
172void HistoryManager::copyUrl()
173{
174 QApplication::clipboard()->setText(ui->historyTree->selectedUrl().toString());
175}
176
177void HistoryManager::copyTitle()
178{
179 QApplication::clipboard()->setText(ui->historyTree->currentIndex().data().toString());
180}
181
183{
184 delete ui;
185}
void restoreState(const QByteArray &state)
QByteArray saveState()
HistoryManager(BrowserWindow *window, QWidget *parent=nullptr)
void search(const QString &searchText)
void setMainWindow(BrowserWindow *window)
~HistoryManager() override
void urlCtrlActivated(const QUrl &url)
void urlShiftActivated(const QUrl &url)
void contextMenuRequested(const QPoint &point)
void urlActivated(const QUrl &url)
static QIcon privateBrowsingIcon()
static QIcon newWindowIcon()
static QIcon newTabIcon()
#define mApp
@ BW_NewWindow
Definition: qzcommon.h:67
State state
#define QSL(x)
Definition: qzcommon.h:40
#define qzSettings
Definition: qzsettings.h:69