KBookmarks

kbookmarkdialog.cpp
1// -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2/*
3 This file is part of the KDE libraries
4 SPDX-FileCopyrightText: 2007 Daniel Teske <teske@squorn.de>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kbookmarkdialog.h"
10#include "kbookmarkdialog_p.h"
11#include "kbookmarkmanager.h"
12#include "kbookmarkmenu_p.h"
13
14#include <QDialogButtonBox>
15#include <QFormLayout>
16#include <QHeaderView>
17#include <QIcon>
18#include <QInputDialog>
19#include <QLabel>
20#include <QLineEdit>
21#include <QPushButton>
22#include <QTreeWidget>
23
24#include <KGuiItem>
25
26KBookmarkDialogPrivate::KBookmarkDialogPrivate(KBookmarkDialog *qq)
27 : q(qq)
29 , layout(false)
30{
31}
32
33KBookmarkDialogPrivate::~KBookmarkDialogPrivate()
34{
35}
36
37void KBookmarkDialogPrivate::initLayout()
38{
39 QBoxLayout *vbox = new QVBoxLayout(q);
40
42 vbox->addLayout(form);
43
44 form->addRow(titleLabel, title);
45 form->addRow(urlLabel, url);
46 form->addRow(commentLabel, comment);
47
48 vbox->addWidget(folderTree);
49 vbox->addWidget(buttonBox);
50}
51
52void KBookmarkDialogPrivate::initLayoutPrivate()
53{
54 title = new QLineEdit(q);
55 title->setMinimumWidth(300);
56 titleLabel = new QLabel(KBookmarkDialog::tr("Name:", "@label:textbox"), q);
57 titleLabel->setBuddy(title);
58
59 url = new QLineEdit(q);
60 url->setMinimumWidth(300);
61 urlLabel = new QLabel(KBookmarkDialog::tr("Location:", "@label:textbox"), q);
62 urlLabel->setBuddy(url);
63
64 comment = new QLineEdit(q);
65 comment->setMinimumWidth(300);
66 commentLabel = new QLabel(KBookmarkDialog::tr("Comment:", "@label:textbox"), q);
67 commentLabel->setBuddy(comment);
68
69 folderTree = new QTreeWidget(q);
70 folderTree->setColumnCount(1);
71 folderTree->header()->hide();
72 folderTree->setSortingEnabled(false);
74 folderTree->setSelectionBehavior(QTreeWidget::SelectRows);
75 folderTree->setMinimumSize(60, 100);
77 fillGroup(root, mgr->root());
78
79 buttonBox = new QDialogButtonBox(q);
80 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
81 q->connect(buttonBox, &QDialogButtonBox::accepted, q, &KBookmarkDialog::accept);
82 q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
83
84 initLayout();
85 layout = true;
86}
87
88void KBookmarkDialogPrivate::fillGroup(QTreeWidgetItem *parentItem, const KBookmarkGroup &group, const KBookmarkGroup &selectGroup)
89{
90 for (KBookmark bk = group.first(); !bk.isNull(); bk = group.next(bk)) {
91 if (bk.isGroup()) {
92 const KBookmarkGroup bkGroup = bk.toGroup();
93 QTreeWidgetItem *item = new KBookmarkTreeItem(parentItem, folderTree, bkGroup);
94 if (selectGroup == bkGroup) {
95 folderTree->setCurrentItem(item);
96 }
98 }
99 }
100}
101
102void KBookmarkDialogPrivate::setParentBookmark(const KBookmark &bm)
103{
104 QString address = bm.address();
105 KBookmarkTreeItem *item = static_cast<KBookmarkTreeItem *>(folderTree->topLevelItem(0));
106 while (true) {
107 if (item->address() == bm.address()) {
108 folderTree->setCurrentItem(item);
109 return;
110 }
111 for (int i = 0; i < item->childCount(); ++i) {
112 KBookmarkTreeItem *child = static_cast<KBookmarkTreeItem *>(item->child(i));
113 if (KBookmark::commonParent(child->address(), address) == child->address()) {
114 item = child;
115 break;
116 }
117 }
118 }
119}
120
121KBookmarkGroup KBookmarkDialogPrivate::parentBookmark()
122{
123 KBookmarkTreeItem *item = dynamic_cast<KBookmarkTreeItem *>(folderTree->currentItem());
124 if (!item) {
125 return mgr->root();
126 }
127 const QString &address = item->address();
128 return mgr->findByAddress(address).toGroup();
129}
130
131void KBookmarkDialog::accept()
132{
133 if (d->mode == KBookmarkDialogPrivate::NewFolder) {
134 KBookmarkGroup parent = d->parentBookmark();
135 if (d->title->text().isEmpty()) {
136 d->title->setText(QStringLiteral("New Folder"));
137 }
138 d->bm = parent.createNewFolder(d->title->text());
139 d->bm.setDescription(d->comment->text());
140 d->mgr->emitChanged(parent);
141 } else if (d->mode == KBookmarkDialogPrivate::NewBookmark) {
142 KBookmarkGroup parent = d->parentBookmark();
143 if (d->title->text().isEmpty()) {
144 d->title->setText(QStringLiteral("New Bookmark"));
145 }
146 d->bm = parent.addBookmark(d->title->text(), QUrl(d->url->text()), d->icon);
147 d->bm.setDescription(d->comment->text());
148 d->mgr->emitChanged(parent);
149 } else if (d->mode == KBookmarkDialogPrivate::NewMultipleBookmarks) {
150 KBookmarkGroup parent = d->parentBookmark();
151 if (d->title->text().isEmpty()) {
152 d->title->setText(QStringLiteral("New Folder"));
153 }
154 d->bm = parent.createNewFolder(d->title->text());
155 d->bm.setDescription(d->comment->text());
157 d->bm.toGroup().addBookmark(fb.title(), fb.url(), fb.icon());
158 }
159 d->mgr->emitChanged(parent);
160 } else if (d->mode == KBookmarkDialogPrivate::EditBookmark) {
161 d->bm.setFullText(d->title->text());
162 d->bm.setUrl(QUrl(d->url->text()));
163 d->bm.setDescription(d->comment->text());
164 d->mgr->emitChanged(d->bm.parentGroup());
165 } else if (d->mode == d->SelectFolder) {
166 d->bm = d->parentBookmark();
167 }
169}
170
172{
173 if (!d->layout) {
174 d->initLayoutPrivate();
175 }
176
177 KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Update", "@action:button")));
178 setWindowTitle(tr("Bookmark Properties", "@title:window"));
179 d->url->setVisible(!bm.isGroup());
180 d->urlLabel->setVisible(!bm.isGroup());
181 d->bm = bm;
182 d->title->setText(bm.fullText());
183 d->url->setText(bm.url().toString());
184 d->comment->setVisible(true);
185 d->commentLabel->setVisible(true);
186 d->comment->setText(bm.description());
187 d->folderTree->setVisible(false);
188
189 d->mode = KBookmarkDialogPrivate::EditBookmark;
190
191 if (exec() == QDialog::Accepted) {
192 return d->bm;
193 } else {
194 return KBookmark();
195 }
196}
197
198KBookmark KBookmarkDialog::addBookmark(const QString &title, const QUrl &url, const QString &icon, KBookmark parent)
199{
200 if (!d->layout) {
201 d->initLayoutPrivate();
202 }
203 if (parent.isNull()) {
204 parent = d->mgr->root();
205 }
206
208 KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new")));
209 d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
210 connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton);
211
212 KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Add", "@action:button"), QStringLiteral("bookmark-new")));
213 setWindowTitle(tr("Add Bookmark", "@title:window"));
214 d->url->setVisible(true);
215 d->urlLabel->setVisible(true);
216 d->title->setText(title);
217 d->url->setText(url.toString());
218 d->comment->setText(QString());
219 d->comment->setVisible(true);
220 d->commentLabel->setVisible(true);
221 d->setParentBookmark(parent);
222 d->folderTree->setVisible(true);
223 d->icon = icon;
224
225 d->mode = KBookmarkDialogPrivate::NewBookmark;
226
227 if (exec() == QDialog::Accepted) {
228 return d->bm;
229 } else {
230 return KBookmark();
231 }
232}
233
235{
236 if (!d->layout) {
237 d->initLayoutPrivate();
238 }
239 if (parent.isNull()) {
240 parent = d->mgr->root();
241 }
242
243 d->list = list;
244
246 KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new")));
247 d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
248 connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton);
249
250 KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Add", "@action:button"), QStringLiteral("bookmark-new")));
251 setWindowTitle(tr("Add Bookmarks", "@title:window"));
252 d->url->setVisible(false);
253 d->urlLabel->setVisible(false);
254 d->title->setText(name);
255 d->comment->setVisible(true);
256 d->commentLabel->setVisible(true);
257 d->comment->setText(QString());
258 d->setParentBookmark(parent);
259 d->folderTree->setVisible(true);
260
261 d->mode = KBookmarkDialogPrivate::NewMultipleBookmarks;
262
263 if (exec() == QDialog::Accepted) {
264 return d->bm.toGroup();
265 } else {
266 return KBookmarkGroup();
267 }
268}
269
271{
272 if (!d->layout) {
273 d->initLayoutPrivate();
274 }
275 if (parent.isNull()) {
276 parent = d->mgr->root();
277 }
278
280 KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new")));
281 d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
282 connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton);
283
284 setWindowTitle(tr("Select Folder", "@title:window"));
285 d->url->setVisible(false);
286 d->urlLabel->setVisible(false);
287 d->title->setVisible(false);
288 d->titleLabel->setVisible(false);
289 d->comment->setVisible(false);
290 d->commentLabel->setVisible(false);
291 d->setParentBookmark(parent);
292 d->folderTree->setVisible(true);
293
294 d->mode = d->SelectFolder;
295
296 if (exec() == QDialog::Accepted) {
297 return d->bm.toGroup();
298 } else {
299 return KBookmarkGroup();
300 }
301}
302
304{
305 if (!d->layout) {
306 d->initLayoutPrivate();
307 }
308 if (parent.isNull()) {
309 parent = d->mgr->root();
310 }
311
312 setWindowTitle(tr("New Folder", "@title:window"));
313 d->url->setVisible(false);
314 d->urlLabel->setVisible(false);
315 d->comment->setVisible(true);
316 d->commentLabel->setVisible(true);
317 d->comment->setText(QString());
318 d->title->setText(name);
319 d->setParentBookmark(parent);
320 d->folderTree->setVisible(true);
321
322 d->mode = KBookmarkDialogPrivate::NewFolder;
323
324 if (exec() == QDialog::Accepted) {
325 return d->bm.toGroup();
326 } else {
327 return KBookmarkGroup();
328 }
329}
330
332 : QDialog(parent)
333 , d(new KBookmarkDialogPrivate(this))
334{
335 d->mgr = mgr;
336}
337
338KBookmarkDialog::~KBookmarkDialog() = default;
339
340void KBookmarkDialog::newFolderButton()
341{
342 QString caption = d->parentBookmark().fullText().isEmpty() ? tr("Create New Bookmark Folder", "@title:window")
343 : tr("Create New Bookmark Folder in %1", "@title:window").arg(d->parentBookmark().text());
344 bool ok;
345 QString text = QInputDialog::getText(this, caption, tr("New folder:", "@label:textbox"), QLineEdit::Normal, QString(), &ok);
346 if (!ok) {
347 return;
348 }
349
350 KBookmarkGroup group = d->parentBookmark().createNewFolder(text);
351 if (!group.isNull()) {
352 KBookmarkGroup parentGroup = group.parentGroup();
353 d->mgr->emitChanged(parentGroup);
354 d->folderTree->clear();
355 QTreeWidgetItem *root = new KBookmarkTreeItem(d->folderTree);
356 d->fillGroup(root, d->mgr->root(), group);
357 }
358}
359
360/********************************************************************/
361
365{
366 setText(0, KBookmarkDialog::tr("Bookmarks", "name of the container of all browser bookmarks"));
367 setIcon(0, QIcon::fromTheme(QStringLiteral("bookmarks")));
368 tree->expandItem(this);
369 tree->setCurrentItem(this);
370 setSelected(true);
371}
372
374 : QTreeWidgetItem(parent)
375{
376 setIcon(0, QIcon::fromTheme(bk.icon()));
377 setText(0, bk.fullText());
378 tree->expandItem(this);
379 m_address = bk.address();
380}
381
383{
384}
385
386QString KBookmarkTreeItem::address()
387{
388 return m_address;
389}
390
391#include "moc_kbookmarkdialog.cpp"
This class provides a Dialog for editing properties, adding Bookmarks and creating new folders.
KBookmark editBookmark(const KBookmark &bm)
Shows a properties dialog Note: this updates the bookmark and calls KBookmarkManager::emitChanged.
KBookmarkGroup selectFolder(KBookmark start=KBookmark())
Shows a dialog to select a folder.
KBookmarkGroup addBookmarks(const QList< KBookmarkOwner::FutureBookmark > &list, const QString &name=QString(), KBookmarkGroup parent=KBookmarkGroup())
Creates a folder from a list of bookmarks Note: this updates the bookmark and calls KBookmarkManager:...
KBookmarkGroup createNewFolder(const QString &name, KBookmark parent=KBookmark())
Shows a dialog to create a new folder.
KBookmark addBookmark(const QString &title, const QUrl &url, const QString &icon, KBookmark parent=KBookmark())
Shows a "Add Bookmark" dialog Note: this updates the bookmark and calls KBookmarkManager::emitChanged...
KBookmarkDialog(KBookmarkManager *manager, QWidget *parent=nullptr)
Creates a KBookmarkDialog instance.
A group of bookmarks.
Definition kbookmark.h:316
KBookmark next(const KBookmark &current) const
Return the next sibling of a child bookmark of this group.
KBookmark first() const
Return the first child bookmark of this group.
This class implements the reading/writing of bookmarks in XML.
Represents the data for a bookmark that will be added.
A class representing a bookmark.
Definition kbookmark.h:27
KBookmarkGroup parentGroup() const
QString description() const
bool isNull() const
bool isGroup() const
Whether the bookmark is a group or a normal bookmark.
QString fullText() const
Text shown for the bookmark, not truncated.
static QString commonParent(const QString &A, const QString &B)
QString address() const
Return the "address" of this bookmark in the whole tree.
QUrl url() const
URL contained by the bookmark.
static void assign(QPushButton *button, const KGuiItem &item)
PostalAddress address(const QVariant &location)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void clicked(bool checked)
virtual void accept()
virtual int exec()
virtual void reject()
QIcon fromTheme(const QString &name)
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
void clear()
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString toString(QUrl::FormattingOptions options) const const
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:40:46 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.