KBookmarks

kbookmarkcontextmenu.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 1998, 1999 Torben Weis <[email protected]>
3  Copyright (C) 2006 Daniel Teske <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library 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 GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kbookmarkcontextmenu.h"
22 #include "kbookmarkowner.h"
23 #include "kbookmarkmanager.h"
24 #include "kbookmarkdialog.h"
25 
26 #include <QApplication>
27 #include <QMessageBox>
28 #include <QMimeData>
29 #include <QClipboard>
30 
31 KBookmarkContextMenu::KBookmarkContextMenu(const KBookmark &bk, KBookmarkManager *manager, KBookmarkOwner *owner, QWidget *parent)
32  : QMenu(parent), bm(bk), m_pManager(manager), m_pOwner(owner)
33 {
34  connect(this, &QMenu::aboutToShow, this, &KBookmarkContextMenu::slotAboutToShow);
35 }
36 
37 void KBookmarkContextMenu::slotAboutToShow()
38 {
39  addActions();
40 }
41 
43 {
44  if (bm.isGroup()) {
45  addOpenFolderInTabs();
46  addBookmark();
47  addFolderActions();
48  } else {
49  addBookmark();
50  addBookmarkActions();
51  }
52 }
53 
54 KBookmarkContextMenu::~KBookmarkContextMenu()
55 {
56 
57 }
58 
59 void KBookmarkContextMenu::addBookmark()
60 {
61  if (m_pOwner && m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark)) {
62  addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), tr("Add Bookmark Here", "@action:inmenu"), this, &KBookmarkContextMenu::slotInsert);
63  }
64 }
65 
66 void KBookmarkContextMenu::addFolderActions()
67 {
68  addAction(tr("Open Folder in Bookmark Editor", "@action:inmenu"), this, &KBookmarkContextMenu::slotEditAt);
69  addProperties();
70  addSeparator();
71  addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete Folder", "@action:inmenu"), this, &KBookmarkContextMenu::slotRemove);
72 }
73 
74 void KBookmarkContextMenu::addProperties()
75 {
76  addAction(tr("Properties", "@action:inmenu"), this, &KBookmarkContextMenu::slotProperties);
77 }
78 
79 void KBookmarkContextMenu::addBookmarkActions()
80 {
81  addAction(tr("Copy Link Address", "@action:inmenu"), this, &KBookmarkContextMenu::slotCopyLocation);
82  addProperties();
83  addSeparator();
84  addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete Bookmark", "@action:inmenu"), this, &KBookmarkContextMenu::slotRemove);
85 }
86 
87 void KBookmarkContextMenu::addOpenFolderInTabs()
88 {
89  if (m_pOwner->supportsTabs()) {
90  addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open Folder in Tabs", "@action:inmenu"), this, &KBookmarkContextMenu::slotOpenFolderInTabs);
91  }
92 }
93 
94 void KBookmarkContextMenu::slotEditAt()
95 {
96  // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotEditAt" << m_highlightedAddress;
97  m_pManager->slotEditBookmarksAtAddress(bm.address());
98 }
99 
100 void KBookmarkContextMenu::slotProperties()
101 {
102  // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotProperties" << m_highlightedAddress;
103 
104  KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow());
105  dlg->editBookmark(bm);
106  delete dlg;
107 }
108 
109 void KBookmarkContextMenu::slotInsert()
110 {
111  // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotInsert" << m_highlightedAddress;
112 
113  QUrl url = m_pOwner->currentUrl();
114  if (url.isEmpty()) {
116  tr("Cannot add bookmark with empty URL.", "@info"));
117  return;
118  }
119  QString title = m_pOwner->currentTitle();
120  if (title.isEmpty()) {
121  title = url.toDisplayString();
122  }
123 
124  if (bm.isGroup()) {
125  KBookmarkGroup parentBookmark = bm.toGroup();
126  Q_ASSERT(!parentBookmark.isNull());
127  parentBookmark.addBookmark(title, url, m_pOwner->currentIcon());
128  m_pManager->emitChanged(parentBookmark);
129  } else {
130  KBookmarkGroup parentBookmark = bm.parentGroup();
131  Q_ASSERT(!parentBookmark.isNull());
132  KBookmark newBookmark = parentBookmark.addBookmark(title, m_pOwner->currentUrl(), m_pOwner->currentIcon());
133  parentBookmark.moveBookmark(newBookmark, parentBookmark.previous(bm));
134  m_pManager->emitChanged(parentBookmark);
135  }
136 }
137 
138 void KBookmarkContextMenu::slotRemove()
139 {
140  // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotRemove" << m_highlightedAddress;
141 
142  bool folder = bm.isGroup();
143 
146  folder ? tr("Bookmark Folder Deletion", "@title:window")
147  : tr("Bookmark Deletion", "@title:window"),
148  folder ? tr("Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(bm.text())
149  : tr("Are you sure you wish to remove the bookmark\n\"%1\"?").arg(bm.text()),
152  ) {
153  return;
154  }
155 
156  KBookmarkGroup parentBookmark = bm.parentGroup();
157  parentBookmark.deleteBookmark(bm);
158  m_pManager->emitChanged(parentBookmark);
159 }
160 
161 void KBookmarkContextMenu::slotCopyLocation()
162 {
163  // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotCopyLocation" << m_highlightedAddress;
164 
165  if (!bm.isGroup()) {
166  QMimeData *mimeData = new QMimeData;
167  bm.populateMimeData(mimeData);
169  mimeData = new QMimeData;
170  bm.populateMimeData(mimeData);
172  }
173 }
174 
175 void KBookmarkContextMenu::slotOpenFolderInTabs()
176 {
177  owner()->openFolderinTabs(bookmark().toGroup());
178 }
179 
180 KBookmarkManager *KBookmarkContextMenu::manager() const
181 {
182  return m_pManager;
183 }
184 
185 KBookmarkOwner *KBookmarkContextMenu::owner() const
186 {
187  return m_pOwner;
188 }
189 
190 KBookmark KBookmarkContextMenu::bookmark() const
191 {
192  return bm;
193 }
bool moveBookmark(const KBookmark &bookmark, const KBookmark &after)
Moves bookmark after after (which should be a child of ours).
Definition: kbookmark.cpp:187
QString toDisplayString(QUrl::FormattingOptions options) const const
KBookmark previous(const KBookmark &current) const
Return the prevous sibling of a child bookmark of this group.
Definition: kbookmark.cpp:122
void addActions(QList< QAction * > actions)
void deleteBookmark(const KBookmark &bk)
Delete a bookmark - it has to be one of our children ! Don&#39;t forget to use KBookmarkManager::self()->...
Definition: kbookmark.cpp:238
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 QUrl currentUrl() const
This function is called whenever the user wants to add the current page to the bookmarks list...
This class implements the reading/writing of bookmarks in XML.
virtual QString currentTitle() const
This function is called whenever the user wants to add the current page to the bookmarks list...
bool isEmpty() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QWidget * activeWindow()
void aboutToShow()
KBookmark addBookmark(const KBookmark &bm)
Create a new bookmark, as the last child of this group Don&#39;t forget to use KBookmarkManager::self()->...
Definition: kbookmark.cpp:212
bool isEmpty() const const
KBookmark editBookmark(const KBookmark &bm)
Shows a properties dialog Note: this updates the bookmark and calls KBookmarkManager::emitChanged.
A group of bookmarks.
Definition: kbookmark.h:327
The KBookmarkMenu and KBookmarkBar classes gives the user the ability to either edit bookmarks or add...
void setMimeData(QMimeData *src, QClipboard::Mode mode)
virtual QString currentIcon() const
This function is called whenever the user wants to add the current page to the bookmarks list...
const QList< QKeySequence > & addBookmark()
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
virtual bool supportsTabs() const
This function returns whether the owner supports tabs.
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QClipboard * clipboard()
This class provides a Dialog for editing properties, adding Bookmarks and creating new folders...
QString applicationName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 22:40:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.