KBookmarks

kbookmarkcontextmenu.cpp
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(this, &QMenu::aboutToShow, this, &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(KBookmarkOwner::ShowAddBookmark)) {
53 addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), tr("Add Bookmark Here", "@action:inmenu"), this, &KBookmarkContextMenu::slotInsert);
54 }
55}
56
57void KBookmarkContextMenu::addFolderActions()
58{
59 addAction(tr("Open Folder in Bookmark Editor", "@action:inmenu"), this, &KBookmarkContextMenu::slotEditAt);
60 addProperties();
62 addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete Folder", "@action:inmenu"), this, &KBookmarkContextMenu::slotRemove);
63}
64
65void KBookmarkContextMenu::addProperties()
66{
67 addAction(tr("Properties", "@action:inmenu"), this, &KBookmarkContextMenu::slotProperties);
68}
69
70void KBookmarkContextMenu::addBookmarkActions()
71{
72 addAction(tr("Copy Link Address", "@action:inmenu"), this, &KBookmarkContextMenu::slotCopyLocation);
73 addProperties();
75 addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete Bookmark", "@action:inmenu"), this, &KBookmarkContextMenu::slotRemove);
76}
77
78void KBookmarkContextMenu::addOpenFolderInTabs()
79{
80 if (m_pOwner->supportsTabs()) {
81 addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open Folder in Tabs", "@action:inmenu"), this, &KBookmarkContextMenu::slotOpenFolderInTabs);
82 }
83}
84
85void KBookmarkContextMenu::slotEditAt()
86{
87 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotEditAt" << m_highlightedAddress;
89 editBookmarks.setBrowserMode(m_browserMode);
90 auto result = editBookmarks.openForFileAtAddress(m_pManager->path(), bm.address());
91
92 if (!result.sucess()) {
94 }
95}
96
97void KBookmarkContextMenu::slotProperties()
98{
99 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotProperties" << m_highlightedAddress;
100
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(QApplication::activeWindow(), QApplication::applicationName(), tr("Cannot add bookmark with empty URL.", "@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(title, url, m_pOwner->currentIcon());
124 m_pManager->emitChanged(parentBookmark);
125 } else {
126 KBookmarkGroup parentBookmark = bm.parentGroup();
127 Q_ASSERT(!parentBookmark.isNull());
128 KBookmark newBookmark = parentBookmark.addBookmark(title, m_pOwner->currentUrl(), m_pOwner->currentIcon());
129 parentBookmark.moveBookmark(newBookmark, parentBookmark.previous(bm));
130 m_pManager->emitChanged(parentBookmark);
131 }
132}
133
134void KBookmarkContextMenu::slotRemove()
135{
136 // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotRemove" << m_highlightedAddress;
137
138 bool folder = bm.isGroup();
139
141 folder ? tr("Bookmark Folder Deletion", "@title:window") : tr("Bookmark Deletion", "@title:window"),
142 folder ? tr("Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(bm.text())
143 : tr("Are you sure you wish to remove the bookmark\n\"%1\"?").arg(bm.text()),
145 != QMessageBox::Yes) {
146 return;
147 }
148
149 KBookmarkGroup parentBookmark = bm.parentGroup();
150 parentBookmark.deleteBookmark(bm);
151 m_pManager->emitChanged(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);
162 mimeData = new QMimeData;
163 bm.populateMimeData(mimeData);
165 }
166}
167
168void KBookmarkContextMenu::slotOpenFolderInTabs()
169{
170 owner()->openFolderinTabs(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
189{
190 m_browserMode = browserMode;
191}
192
194{
195 return m_browserMode;
196}
197
198#include "moc_kbookmarkcontextmenu.cpp"
void setBrowserMode(bool browserMode)
Set this to true to make any "Edit Bookmarks" dialog show UI elements that are specific to browsers.
bool browserMode() const
Whether any "Edit Bookmarks" dialog shows UI elements that are specific to browsers.
This class provides a Dialog for editing properties, adding Bookmarks and creating new folders.
A group of bookmarks.
Definition kbookmark.h:316
This class implements the reading/writing of bookmarks in XML.
QString path() const
This will return the path that this manager is using to read the bookmarks.
void emitChanged()
Saves the bookmark file and notifies everyone.
The KBookmarkMenu and KBookmarkBar classes gives the user the ability to either edit bookmarks or add...
virtual QString currentIcon() const
This function is called whenever the user wants to add the current page to the bookmarks list.
virtual bool enableOption(BookmarkOption option) const
Returns true if action should be shown in the menu The default is to show both a add and editBookmark...
virtual QString currentTitle() const
This function is called whenever the user wants to add the current page to the bookmarks list.
virtual bool supportsTabs() const
This function returns whether the owner supports tabs.
virtual void openFolderinTabs(const KBookmarkGroup &bm)
Called if the user wants to open every bookmark in this folder in a new tab.
virtual QUrl currentUrl() const
This function is called whenever the user wants to add the current page to the bookmarks list.
A class representing a bookmark.
Definition kbookmark.h:27
QAction * editBookmarks(const QObject *recvr, const char *slot, QObject *parent)
QWidget * activeWindow()
void setMimeData(QMimeData *src, Mode mode)
QClipboard * clipboard()
QIcon fromTheme(const QString &name)
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
void aboutToShow()
QAction * addSeparator()
StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, StandardButton defaultButton)
StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, StandardButton defaultButton)
QString tr(const char *sourceText, const char *disambiguation, int n)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isEmpty() const const
QString toDisplayString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.