File indexing completed on 2024-04-21 05:51:21

0001 /*  This file was part of the KDE libraries
0002 
0003     SPDX-FileCopyrightText: 2019 Tomaz Canabrava <tcanabrava@kde.org>
0004     SPDX-FileCopyrightText: 2019 Martin Sandsmark <martin.sandsmark@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Own
0010 #include "BookmarkMenu.h"
0011 
0012 // KDE
0013 #include <KActionCollection>
0014 #include <KBookmark>
0015 #include <KBookmarkManager>
0016 #include <KBookmarkOwner>
0017 
0018 // Qt
0019 #include <QAction>
0020 #include <QMenu>
0021 
0022 #include <algorithm> // std::any_of
0023 
0024 BookmarkMenu::BookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, KActionCollection *collection)
0025     : KBookmarkMenu(mgr, owner, parentMenu)
0026 {
0027     QAction *bookmarkAction;
0028     collection->addActions(parentMenu->actions());
0029 
0030     bookmarkAction = addBookmarkAction();
0031 
0032     Q_ASSERT(bookmarkAction);
0033 
0034     // We need to hijack the action - note this only hijacks top-level action
0035     disconnect(bookmarkAction, nullptr, this, nullptr);
0036     connect(bookmarkAction, &QAction::triggered, this, &BookmarkMenu::maybeAddBookmark);
0037 
0038 #ifndef Q_OS_MACOS // not needed on this platform, Cmd+B (shortcut) is different to Ctrl+B (^B in terminal)
0039     // replace Ctrl+B shortcut for bookmarks only if user hasn't already
0040     // changed the shortcut; however, if the user changed it to Ctrl+B
0041     // this will still get changed to Ctrl+Shift+B
0042     if (bookmarkAction->shortcut() == QKeySequence(Qt::CTRL | Qt::Key_B)) {
0043         collection->setDefaultShortcut(bookmarkAction, Qt::CTRL | Qt::SHIFT | Qt::Key_B);
0044     }
0045 #endif
0046 }
0047 
0048 void BookmarkMenu::maybeAddBookmark()
0049 {
0050     // Check for duplicates first
0051     // This only catches duplicates in top-level (ie not sub-folders);
0052     //  this is due to only the top-level add_bookmark is hijacked.
0053     const KBookmarkGroup rootGroup = manager()->root();
0054     const QUrl currUrl = owner()->currentUrl();
0055     const auto urlList = rootGroup.groupUrlList();
0056     if (std::any_of(urlList.constBegin(), urlList.constEnd(), [currUrl](const QUrl &url) {
0057             return url == currUrl;
0058         })) {
0059         return;
0060     }
0061 
0062     slotAddBookmark();
0063 }
0064 
0065 #include "moc_BookmarkMenu.cpp"