1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 1998, 1999 Torben Weis <weis@kde.org>
4 SPDX-FileCopyrightText: 2006 Daniel Teske <teske@squorn.de>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kbookmarkcontextmenu.h"
10#include "kbookmarkdialog.h"
11#include "kbookmarkmanager.h"
12#include "kbookmarkowner.h"
13#include "keditbookmarks_p.h"
14
15#include <QApplication>
16#include <QClipboard>
17#include <QMessageBox>
18#include <QMimeData>
19
20KBookmarkContextMenu::KBookmarkContextMenu(const KBookmark &bk, KBookmarkManager *manager, KBookmarkOwner *owner, QWidget *parent)
21 : QMenu(parent)
22 , bm(bk)
23 , m_pManager(manager)
24 , m_pOwner(owner)
25{
26 connect(sender: this, signal: &QMenu::aboutToShow, context: this, slot: &KBookmarkContextMenu::slotAboutToShow);
27}
28
29void KBookmarkContextMenu::slotAboutToShow()
30{
31 addActions();
32}
33
34void KBookmarkContextMenu::addActions()
35{
36 if (bm.isGroup()) {
37 addOpenFolderInTabs();
38 addBookmark();
39 addFolderActions();
40 } else {
41 addBookmark();
42 addBookmarkActions();
43 }
44}
45
46KBookmarkContextMenu::~KBookmarkContextMenu()
47{
48}
49
50void KBookmarkContextMenu::addBookmark()
51{
52 if (m_pOwner && m_pOwner->enableOption(option: KBookmarkOwner::ShowAddBookmark)) {
53 addAction(icon: QIcon::fromTheme(QStringLiteral("bookmark-new")), text: tr(s: "Add Bookmark Here", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotInsert);
54 }
55}
56
57void KBookmarkContextMenu::addFolderActions()
58{
59 addAction(text: tr(s: "Open Folder in Bookmark Editor", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotEditAt);
60 addProperties();
61 addSeparator();
62 addAction(icon: QIcon::fromTheme(QStringLiteral("edit-delete")), text: tr(s: "Delete Folder", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotRemove);
63}
64
65void KBookmarkContextMenu::addProperties()
66{
67 addAction(text: tr(s: "Properties", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotProperties);
68}
69
70void KBookmarkContextMenu::addBookmarkActions()
71{
72 addAction(text: tr(s: "Copy Link Address", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotCopyLocation);
73 addProperties();
74 addSeparator();
75 addAction(icon: QIcon::fromTheme(QStringLiteral("edit-delete")), text: tr(s: "Delete Bookmark", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotRemove);
76}
77
78void KBookmarkContextMenu::addOpenFolderInTabs()
79{
80 if (m_pOwner->supportsTabs()) {
81 addAction(icon: QIcon::fromTheme(QStringLiteral("tab-new")), text: tr(s: "Open Folder in Tabs", c: "@action:inmenu"), args: this, args: &KBookmarkContextMenu::slotOpenFolderInTabs);
82 }
83}
84
85void KBookmarkContextMenu::slotEditAt()
86{
87 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotEditAt" << m_highlightedAddress;
88 KEditBookmarks editBookmarks;
89 editBookmarks.setBrowserMode(m_browserMode);
90 auto result = editBookmarks.openForFileAtAddress(file: m_pManager->path(), address: bm.address());
91
92 if (!result.sucess()) {
93 QMessageBox::critical(parent: QApplication::activeWindow(), title: QApplication::applicationDisplayName(), text: result.errorMessage());
94 }
95}
96
97void KBookmarkContextMenu::slotProperties()
98{
99 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotProperties" << m_highlightedAddress;
100
101 KBookmarkDialog *dlg = new KBookmarkDialog(m_pManager, QApplication::activeWindow());
102 dlg->editBookmark(bm);
103 delete dlg;
104}
105
106void KBookmarkContextMenu::slotInsert()
107{
108 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotInsert" << m_highlightedAddress;
109
110 QUrl url = m_pOwner->currentUrl();
111 if (url.isEmpty()) {
112 QMessageBox::critical(parent: QApplication::activeWindow(), title: QApplication::applicationName(), text: tr(s: "Cannot add bookmark with empty URL.", c: "@info"));
113 return;
114 }
115 QString title = m_pOwner->currentTitle();
116 if (title.isEmpty()) {
117 title = url.toDisplayString();
118 }
119
120 if (bm.isGroup()) {
121 KBookmarkGroup parentBookmark = bm.toGroup();
122 Q_ASSERT(!parentBookmark.isNull());
123 parentBookmark.addBookmark(text: title, url, icon: m_pOwner->currentIcon());
124 m_pManager->emitChanged(group: parentBookmark);
125 } else {
126 KBookmarkGroup parentBookmark = bm.parentGroup();
127 Q_ASSERT(!parentBookmark.isNull());
128 KBookmark newBookmark = parentBookmark.addBookmark(text: title, url: m_pOwner->currentUrl(), icon: m_pOwner->currentIcon());
129 parentBookmark.moveBookmark(bookmark: newBookmark, after: parentBookmark.previous(current: bm));
130 m_pManager->emitChanged(group: parentBookmark);
131 }
132}
133
134void KBookmarkContextMenu::slotRemove()
135{
136 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotRemove" << m_highlightedAddress;
137
138 bool folder = bm.isGroup();
139
140 if (QMessageBox::warning(parent: QApplication::activeWindow(),
141 title: folder ? tr(s: "Bookmark Folder Deletion", c: "@title:window") : tr(s: "Bookmark Deletion", c: "@title:window"),
142 text: folder ? tr(s: "Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(a: bm.text())
143 : tr(s: "Are you sure you wish to remove the bookmark\n\"%1\"?").arg(a: bm.text()),
144 buttons: QMessageBox::Yes | QMessageBox::Cancel)
145 != QMessageBox::Yes) {
146 return;
147 }
148
149 KBookmarkGroup parentBookmark = bm.parentGroup();
150 parentBookmark.deleteBookmark(bk: bm);
151 m_pManager->emitChanged(group: parentBookmark);
152}
153
154void KBookmarkContextMenu::slotCopyLocation()
155{
156 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotCopyLocation" << m_highlightedAddress;
157
158 if (!bm.isGroup()) {
159 QMimeData *mimeData = new QMimeData;
160 bm.populateMimeData(mimeData);
161 QApplication::clipboard()->setMimeData(data: mimeData, mode: QClipboard::Selection);
162 mimeData = new QMimeData;
163 bm.populateMimeData(mimeData);
164 QApplication::clipboard()->setMimeData(data: mimeData, mode: QClipboard::Clipboard);
165 }
166}
167
168void KBookmarkContextMenu::slotOpenFolderInTabs()
169{
170 owner()->openFolderinTabs(bm: bookmark().toGroup());
171}
172
173KBookmarkManager *KBookmarkContextMenu::manager() const
174{
175 return m_pManager;
176}
177
178KBookmarkOwner *KBookmarkContextMenu::owner() const
179{
180 return m_pOwner;
181}
182
183KBookmark KBookmarkContextMenu::bookmark() const
184{
185 return bm;
186}
187
188void KBookmarkContextMenu::setBrowserMode(bool browserMode)
189{
190 m_browserMode = browserMode;
191}
192
193bool KBookmarkContextMenu::browserMode() const
194{
195 return m_browserMode;
196}
197
198#include "moc_kbookmarkcontextmenu.cpp"
199

source code of kbookmarks/src/widgets/kbookmarkcontextmenu.cpp