File indexing completed on 2024-04-14 14:18:00

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1998, 1999 Torben Weis <weis@kde.org>
0004     SPDX-FileCopyrightText: 2006 Daniel Teske <teske@squorn.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kbookmarkcontextmenu.h"
0010 #include "kbookmarkdialog.h"
0011 #include "kbookmarkmanager.h"
0012 #include "kbookmarkowner.h"
0013 
0014 #include <QApplication>
0015 #include <QClipboard>
0016 #include <QMessageBox>
0017 #include <QMimeData>
0018 
0019 KBookmarkContextMenu::KBookmarkContextMenu(const KBookmark &bk, KBookmarkManager *manager, KBookmarkOwner *owner, QWidget *parent)
0020     : QMenu(parent)
0021     , bm(bk)
0022     , m_pManager(manager)
0023     , m_pOwner(owner)
0024 {
0025     connect(this, &QMenu::aboutToShow, this, &KBookmarkContextMenu::slotAboutToShow);
0026 }
0027 
0028 void KBookmarkContextMenu::slotAboutToShow()
0029 {
0030     addActions();
0031 }
0032 
0033 void KBookmarkContextMenu::addActions()
0034 {
0035     if (bm.isGroup()) {
0036         addOpenFolderInTabs();
0037         addBookmark();
0038         addFolderActions();
0039     } else {
0040         addBookmark();
0041         addBookmarkActions();
0042     }
0043 }
0044 
0045 KBookmarkContextMenu::~KBookmarkContextMenu()
0046 {
0047 }
0048 
0049 void KBookmarkContextMenu::addBookmark()
0050 {
0051     if (m_pOwner && m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark)) {
0052         addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), tr("Add Bookmark Here", "@action:inmenu"), this, &KBookmarkContextMenu::slotInsert);
0053     }
0054 }
0055 
0056 void KBookmarkContextMenu::addFolderActions()
0057 {
0058     addAction(tr("Open Folder in Bookmark Editor", "@action:inmenu"), this, &KBookmarkContextMenu::slotEditAt);
0059     addProperties();
0060     addSeparator();
0061     addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete Folder", "@action:inmenu"), this, &KBookmarkContextMenu::slotRemove);
0062 }
0063 
0064 void KBookmarkContextMenu::addProperties()
0065 {
0066     addAction(tr("Properties", "@action:inmenu"), this, &KBookmarkContextMenu::slotProperties);
0067 }
0068 
0069 void KBookmarkContextMenu::addBookmarkActions()
0070 {
0071     addAction(tr("Copy Link Address", "@action:inmenu"), this, &KBookmarkContextMenu::slotCopyLocation);
0072     addProperties();
0073     addSeparator();
0074     addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Delete Bookmark", "@action:inmenu"), this, &KBookmarkContextMenu::slotRemove);
0075 }
0076 
0077 void KBookmarkContextMenu::addOpenFolderInTabs()
0078 {
0079     if (m_pOwner->supportsTabs()) {
0080         addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open Folder in Tabs", "@action:inmenu"), this, &KBookmarkContextMenu::slotOpenFolderInTabs);
0081     }
0082 }
0083 
0084 void KBookmarkContextMenu::slotEditAt()
0085 {
0086     // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotEditAt" << m_highlightedAddress;
0087     m_pManager->slotEditBookmarksAtAddress(bm.address());
0088 }
0089 
0090 void KBookmarkContextMenu::slotProperties()
0091 {
0092     // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotProperties" << m_highlightedAddress;
0093 
0094     KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow());
0095     dlg->editBookmark(bm);
0096     delete dlg;
0097 }
0098 
0099 void KBookmarkContextMenu::slotInsert()
0100 {
0101     // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotInsert" << m_highlightedAddress;
0102 
0103     QUrl url = m_pOwner->currentUrl();
0104     if (url.isEmpty()) {
0105         QMessageBox::critical(QApplication::activeWindow(), QApplication::applicationName(), tr("Cannot add bookmark with empty URL.", "@info"));
0106         return;
0107     }
0108     QString title = m_pOwner->currentTitle();
0109     if (title.isEmpty()) {
0110         title = url.toDisplayString();
0111     }
0112 
0113     if (bm.isGroup()) {
0114         KBookmarkGroup parentBookmark = bm.toGroup();
0115         Q_ASSERT(!parentBookmark.isNull());
0116         parentBookmark.addBookmark(title, url, m_pOwner->currentIcon());
0117         m_pManager->emitChanged(parentBookmark);
0118     } else {
0119         KBookmarkGroup parentBookmark = bm.parentGroup();
0120         Q_ASSERT(!parentBookmark.isNull());
0121         KBookmark newBookmark = parentBookmark.addBookmark(title, m_pOwner->currentUrl(), m_pOwner->currentIcon());
0122         parentBookmark.moveBookmark(newBookmark, parentBookmark.previous(bm));
0123         m_pManager->emitChanged(parentBookmark);
0124     }
0125 }
0126 
0127 void KBookmarkContextMenu::slotRemove()
0128 {
0129     // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotRemove" << m_highlightedAddress;
0130 
0131     bool folder = bm.isGroup();
0132 
0133     if (QMessageBox::warning(QApplication::activeWindow(),
0134                              folder ? tr("Bookmark Folder Deletion", "@title:window") : tr("Bookmark Deletion", "@title:window"),
0135                              folder ? tr("Are you sure you wish to remove the bookmark folder\n\"%1\"?").arg(bm.text())
0136                                     : tr("Are you sure you wish to remove the bookmark\n\"%1\"?").arg(bm.text()),
0137                              QMessageBox::Yes | QMessageBox::Cancel)
0138         != QMessageBox::Yes) {
0139         return;
0140     }
0141 
0142     KBookmarkGroup parentBookmark = bm.parentGroup();
0143     parentBookmark.deleteBookmark(bm);
0144     m_pManager->emitChanged(parentBookmark);
0145 }
0146 
0147 void KBookmarkContextMenu::slotCopyLocation()
0148 {
0149     // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotCopyLocation" << m_highlightedAddress;
0150 
0151     if (!bm.isGroup()) {
0152         QMimeData *mimeData = new QMimeData;
0153         bm.populateMimeData(mimeData);
0154         QApplication::clipboard()->setMimeData(mimeData, QClipboard::Selection);
0155         mimeData = new QMimeData;
0156         bm.populateMimeData(mimeData);
0157         QApplication::clipboard()->setMimeData(mimeData, QClipboard::Clipboard);
0158     }
0159 }
0160 
0161 void KBookmarkContextMenu::slotOpenFolderInTabs()
0162 {
0163     owner()->openFolderinTabs(bookmark().toGroup());
0164 }
0165 
0166 KBookmarkManager *KBookmarkContextMenu::manager() const
0167 {
0168     return m_pManager;
0169 }
0170 
0171 KBookmarkOwner *KBookmarkContextMenu::owner() const
0172 {
0173     return m_pOwner;
0174 }
0175 
0176 KBookmark KBookmarkContextMenu::bookmark() const
0177 {
0178     return bm;
0179 }
0180 
0181 #include "moc_kbookmarkcontextmenu.cpp"