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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "bookmarkswidget.h"
#include "bookmarkmodel.h"
#include "editbookmarkdialog.h"
#include "mainwindow/mainwindow.h"
#include "subwindow/subwindow.h"
#include "ui_bookmarksform.h"
#include "webengine/webprofilemanager.h"
#include "webengine/webview.h"
#include <QFileDialog>
#include <QMenu>
#include <QTreeView>
#include <QUrl>
inline void expandChildren(QTreeView *view, BookmarkModel *m_model, const QModelIndex &rootIndex)
{
for(int i = 0; i < m_model->rowCount(rootIndex); ++i) {
QModelIndex idx = m_model->index(i, 0, rootIndex);
if(m_model->isItemExpanded(idx))
view->expand(idx);
// check if index has children
if(m_model->rowCount(idx) > 0)
expandChildren(view, m_model, idx);
}
}
inline void loadModel(const QString &path, BookmarkModel *m_model, QTreeView *view)
{
view->setModel(nullptr);
if(path.endsWith(".xbel")) {
QFile f(path);
if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
BookmarkFormat<XbelFormat>(&f) >> m_model;
f.close();
}
} else if(path.endsWith(".json")) {
QFile f(path);
if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
BookmarkFormat<FirefoxJsonFormat>(&f) >> m_model;
f.close();
}
}
view->setModel(m_model);
expandChildren(view, m_model, QModelIndex());
}
BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
: QWidget(parent)
, ui(new Ui::BookmarksDialog)
{
// make sure this dialog does not get deleted on close
setAttribute(Qt::WA_DeleteOnClose, false);
ui->setupUi(this);
ui->bookmark_groupBox->setVisible(false);
ui->folder_groupBox->setVisible(false);
ui->addFolder_toolButton->setIcon(style()->standardPixmap(QStyle::SP_DirIcon));
ui->addBookmark_toolButton->setIcon(style()->standardPixmap(QStyle::SP_FileIcon));
ui->deleteItem_toolButton->setIcon(style()->standardPixmap(QStyle::SP_TrashIcon));
ui->deleteItem_toolButton->setShortcut(QKeySequence::Delete);
m_model = new BookmarkModel(this);
m_bookmarksPath = path;
loadModel(path, m_model, ui->treeView);
// item activated
connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
if(index.column() == 1)
emit openUrl(index.data(Qt::DisplayRole).toUrl());
else
editBookmark(index);
});
connect(ui->treeView, &QTreeView::expanded, this, [this](const QModelIndex &index) {
m_model->setItemExpanded(index, true);
});
connect(ui->treeView, &QTreeView::collapsed, this, [this](const QModelIndex &index) {
m_model->setItemExpanded(index, false);
});
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeView, &QTreeView::customContextMenuRequested, this, [this](const QPoint &pos) {
const QModelIndex idx = ui->treeView->indexAt(pos);
if(idx.isValid()) {
const QUrl url = m_model->data(idx, 1, Qt::DisplayRole).toUrl();
// get parent main window
MainWindow *window = nullptr;
for(QWidget *w = this; w != nullptr && window == nullptr; w = w->parentWidget()) {
window = qobject_cast<MainWindow *>(w);
}
if(window == nullptr)
return;
auto *subwindow = window->currentSubWindow();
if(subwindow == nullptr)
return;
auto *menu = new QMenu(this);
menu->addAction(tr("Open link in current tab"), subwindow, [url, subwindow]() {
subwindow->currentView()->load(url);
});
auto *openInCurrentTabWithProfile = menu->addMenu(tr("Open link in current tab with profile"));
profileMenu(openInCurrentTabWithProfile, [url, subwindow](WebProfile *profile) {
subwindow->currentView()->setProfile(profile);
subwindow->currentView()->load(url);
});
menu->addAction(tr("Open link in new tab"), subwindow, [url, subwindow]() {
subwindow->addTab(url);
});
auto *openInNewTabWithProfile = menu->addMenu(tr("Open link in new tab with profile"));
profileMenu(openInNewTabWithProfile, [url, subwindow](WebProfile *profile) {
subwindow->addTab(url, profile);
});
menu->addSeparator();
menu->addAction(tr("Clear selection"), this, [this]() {
ui->treeView->setCurrentIndex(QModelIndex());
ui->treeView->clearSelection();
});
menu->exec(ui->treeView->viewport()->mapToGlobal(pos));
}
});
// addBookmark
connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() {
const QModelIndex idx = m_model->parentFolder(ui->treeView->currentIndex());
const QModelIndex childIdx = m_model->appendBookmark(tr("Title"), QString(), idx);
ui->treeView->setCurrentIndex(childIdx);
editBookmark(childIdx);
});
// addFolder
connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() {
const QModelIndex idx = m_model->parentFolder(ui->treeView->currentIndex());
const QModelIndex childIdx = m_model->appendFolder(tr("Title"), idx);
ui->treeView->setCurrentIndex(childIdx);
editBookmark(childIdx);
});
// deleteItem
connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() {
const QModelIndex idx = ui->treeView->currentIndex();
m_model->removeRow(idx.row(), idx.parent());
});
// import button
connect(ui->import_toolButton, &QToolButton::clicked, this, [this]() {
const auto import_path = QFileDialog::getOpenFileName(this, tr("Open bookmarks file"), QDir::homePath(), tr("Firefox bookmarks backup (*.json)"));
if(!import_path.isEmpty()) {
loadModel(import_path, m_model, ui->treeView);
}
});
}
BookmarksWidget::~BookmarksWidget()
{
delete ui;
}
void BookmarksWidget::editBookmark(const QModelIndex &index)
{
auto *dlg = new EditBookmarkDialog(m_model, index, this);
dlg->exec();
}
void BookmarksWidget::save()
{
if(!m_model->isModified())
return;
QFile bookmarksFile(m_bookmarksPath);
if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
BookmarkFormat<XbelFormat> f(&bookmarksFile);
m_model >> f;
bookmarksFile.flush();
}
}
void BookmarksWidget::addBookmark(const QString &title, const QString &url)
{
m_model->appendBookmark(title, url, QModelIndex());
}
void BookmarksWidget::search(const QString &term, const std::function<void(QStringList &)> &callback) const
{
QStringList ret = m_model->search(term);
callback(ret);
}
|