diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 23c2fc0..e30ce57 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,12 +1,13 @@ remove_definitions(-DQT_NO_CAST_FROM_ASCII) find_package(Qt5Test ${REQUIRED_QT_VERSION} CONFIG QUIET) if(NOT Qt5Test_FOUND) message(STATUS "Qt5Test not found, autotests will not be built.") return() endif() include(ECMAddTests) ecm_add_test(kbookmarktest.cpp TEST_NAME kbookmarktest LINK_LIBRARIES KF5::Bookmarks Qt5::Test) +ecm_add_test(kbookmarkmenutest.cpp TEST_NAME kbookmarkmenutest LINK_LIBRARIES KF5::Bookmarks KF5::XmlGui Qt5::Test) diff --git a/autotests/kbookmarkmenutest.cpp b/autotests/kbookmarkmenutest.cpp new file mode 100644 index 0000000..fda3d34 --- /dev/null +++ b/autotests/kbookmarkmenutest.cpp @@ -0,0 +1,138 @@ +/*************************************************************************** + * Copyright (C) 2019 by David Hallas * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include +#include +#include +#include + +#include +#include +#include +#include + +class TestKBookmarkOwner; + +class KBookmarkMenuTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void tabsOpen_data(); + void tabsOpen(); + void tabsOpenChanges(); +}; + +class TestKBookmarkOwner final : public KBookmarkOwner +{ +public: + explicit TestKBookmarkOwner(bool supportsTabs_); + void openBookmark(const KBookmark &bm, Qt::MouseButtons mb, Qt::KeyboardModifiers km) override; + bool supportsTabs() const override; +private: + const bool m_supportsTabs; +}; + +TestKBookmarkOwner::TestKBookmarkOwner(bool supportsTabs_) : + m_supportsTabs(supportsTabs_) +{ +} + +void TestKBookmarkOwner::openBookmark(const KBookmark&, Qt::MouseButtons, Qt::KeyboardModifiers) +{ +} + +bool TestKBookmarkOwner::supportsTabs() const +{ + return m_supportsTabs; +} + +#define VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER() \ + do { \ + QCOMPARE(menu->actions().count(), 3); \ + QCOMPARE(menu->actions().at(0)->text(), QStringLiteral("&Add Bookmark")); \ + QCOMPARE(menu->actions().at(1)->text(), QStringLiteral("New Bookmark Folder...")); \ + QCOMPARE(menu->actions().at(2)->text(), QStringLiteral("&Edit Bookmarks...")); \ + } while (false) + +#define VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER() \ + do { \ + QCOMPARE(menu->actions().count(), 4); \ + QCOMPARE(menu->actions().at(0)->text(), QStringLiteral("&Add Bookmark")); \ + QCOMPARE(menu->actions().at(1)->text(), QStringLiteral("Bookmark Tabs as Folder...")); \ + QCOMPARE(menu->actions().at(2)->text(), QStringLiteral("New Bookmark Folder...")); \ + QCOMPARE(menu->actions().at(3)->text(), QStringLiteral("&Edit Bookmarks...")); \ + } while (false) + +void KBookmarkMenuTest::tabsOpen_data() +{ + QTest::addColumn("supportsTabs"); + QTest::addColumn("numberOfOpenTabs"); + + const bool TabsNotSupported(false); + QTest::newRow("Tabs not supported - 0 tabs open") << TabsNotSupported << 0; + QTest::newRow("Tabs not supported - 1 tabs open") << TabsNotSupported << 1; + QTest::newRow("Tabs not supported - 2 tabs open") << TabsNotSupported << 2; + const bool TabsSupported(true); + QTest::newRow("Tabs supported - 0 tabs open") << TabsSupported << 0; + QTest::newRow("Tabs supported - 1 tabs open") << TabsSupported << 1; + QTest::newRow("Tabs supported - 2 tabs open") << TabsSupported << 2; +} + +void KBookmarkMenuTest::tabsOpen() +{ + QFETCH(bool, supportsTabs); + QFETCH(int, numberOfOpenTabs); + auto manager = KBookmarkManager::createTempManager(); + QScopedPointer bookmarkOwner(new TestKBookmarkOwner(supportsTabs)); + QMenu* menu = new QMenu; + QScopedPointer actionCollection(new KActionCollection(nullptr, QString())); + QScopedPointer testObject(new KBookmarkMenu(manager, bookmarkOwner.get(), menu, actionCollection.get())); + testObject->setNumberOfOpenTabs(numberOfOpenTabs); + testObject->ensureUpToDate(); + if (supportsTabs && numberOfOpenTabs > 1) { + VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); + } else { + VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER(); + } +} + +void KBookmarkMenuTest::tabsOpenChanges() +{ + auto manager = KBookmarkManager::createTempManager(); + QScopedPointer bookmarkOwner(new TestKBookmarkOwner(true)); + QMenu* menu = new QMenu; + QScopedPointer actionCollection(new KActionCollection(nullptr, QString())); + QScopedPointer testObject(new KBookmarkMenu(manager, bookmarkOwner.get(), menu, actionCollection.get())); + testObject->ensureUpToDate(); + // If the number of open tabs has not been set it will default to 2 + const int DefaultNumberOfOpenTabs = 2; + QCOMPARE(testObject->numberOfOpenTabs(), DefaultNumberOfOpenTabs); + testObject->ensureUpToDate(); + VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); + testObject->setNumberOfOpenTabs(1); + testObject->ensureUpToDate(); + VERIFY_MENU_WITHOUT_BOOKMARK_TABS_AS_FOLDER(); + testObject->setNumberOfOpenTabs(2); + testObject->ensureUpToDate(); + VERIFY_MENU_WITH_BOOKMARK_TABS_AS_FOLDER(); +} + +QTEST_MAIN(KBookmarkMenuTest) + +#include "kbookmarkmenutest.moc" diff --git a/src/kbookmarkmenu.cpp b/src/kbookmarkmenu.cpp index 056303d..a558394 100644 --- a/src/kbookmarkmenu.cpp +++ b/src/kbookmarkmenu.cpp @@ -1,536 +1,552 @@ /* This file is part of the KDE project Copyright (C) 1998, 1999 Torben Weis Copyright (C) 2006 Daniel Teske This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kbookmarkmenu.h" #include "kbookmarkmenu_p.h" #include "kbookmarkaction.h" #include "kbookmarkactionmenu.h" #include "kbookmarkcontextmenu.h" #include "kbookmarkdialog.h" #include "kbookmarkowner.h" #include #include #include #include #include #include "kbookmarks_debug.h" #include #include #include /********************************************************************/ /********************************************************************/ /********************************************************************/ class KBookmarkMenuPrivate { public: KBookmarkMenuPrivate() : newBookmarkFolder(nullptr), addAddBookmark(nullptr), - bookmarksToFolder(nullptr) + bookmarksToFolder(nullptr), + numberOfOpenTabs(2) { } QAction *newBookmarkFolder; QAction *addAddBookmark; QAction *bookmarksToFolder; + int numberOfOpenTabs; }; KBookmarkMenu::KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *_owner, QMenu *_parentMenu, KActionCollection *actionCollection) : QObject(), m_actionCollection(actionCollection), d(new KBookmarkMenuPrivate()), m_bIsRoot(true), m_pManager(mgr), m_pOwner(_owner), m_parentMenu(_parentMenu), m_parentAddress(QLatin1String("")) //TODO KBookmarkAdress::root { // TODO KDE5 find a QMenu equvalnet for this one //m_parentMenu->setKeyboardShortcutsEnabled( true ); // qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::KBookmarkMenu " << this << " address : " << m_parentAddress; connect(_parentMenu, &QMenu::aboutToShow, this, &KBookmarkMenu::slotAboutToShow); if (KBookmarkSettings::self()->m_contextmenu) { m_parentMenu->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_parentMenu, &QWidget::customContextMenuRequested, this, &KBookmarkMenu::slotCustomContextMenu); } connect(m_pManager, &KBookmarkManager::changed, this, &KBookmarkMenu::slotBookmarksChanged); m_bDirty = true; addActions(); } void KBookmarkMenu::addActions() { if (m_bIsRoot) { addAddBookmark(); addAddBookmarksList(); addNewFolder(); addEditBookmarks(); } else { if (!m_parentMenu->actions().isEmpty()) { m_parentMenu->addSeparator(); } addOpenInTabs(); addAddBookmark(); addAddBookmarksList(); addNewFolder(); } } KBookmarkMenu::KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *_owner, QMenu *_parentMenu, const QString &parentAddress) : QObject(), m_actionCollection(new KActionCollection(this)), d(new KBookmarkMenuPrivate()), m_bIsRoot(false), m_pManager(mgr), m_pOwner(_owner), m_parentMenu(_parentMenu), m_parentAddress(parentAddress) { // TODO KDE5 find a QMenu equvalnet for this one //m_parentMenu->setKeyboardShortcutsEnabled( true ); connect(_parentMenu, &QMenu::aboutToShow, this, &KBookmarkMenu::slotAboutToShow); if (KBookmarkSettings::self()->m_contextmenu) { m_parentMenu->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_parentMenu, &QWidget::customContextMenuRequested, this, &KBookmarkMenu::slotCustomContextMenu); } m_bDirty = true; } KBookmarkMenu::~KBookmarkMenu() { qDeleteAll(m_lstSubMenus); qDeleteAll(m_actions); delete d; } void KBookmarkMenu::ensureUpToDate() { slotAboutToShow(); } +void KBookmarkMenu::setNumberOfOpenTabs(int numberOfOpenTabs) +{ + if (numberOfOpenTabs == d->numberOfOpenTabs) { + return; + } + m_bDirty = (d->numberOfOpenTabs < 2) != (numberOfOpenTabs < 2); + d->numberOfOpenTabs = numberOfOpenTabs; +} + +int KBookmarkMenu::numberOfOpenTabs() const +{ + return d->numberOfOpenTabs; +} + void KBookmarkMenu::slotAboutToShow() { // Did the bookmarks change since the last time we showed them ? if (m_bDirty) { m_bDirty = false; clear(); refill(); m_parentMenu->adjustSize(); } } void KBookmarkMenu::slotCustomContextMenu(const QPoint &pos) { QAction *action = m_parentMenu->actionAt(pos); QMenu *menu = contextMenu(action); if (!menu) { return; } menu->setAttribute(Qt::WA_DeleteOnClose); menu->popup(m_parentMenu->mapToGlobal(pos)); } QMenu *KBookmarkMenu::contextMenu(QAction *action) { KBookmarkActionInterface *act = dynamic_cast(action); if (!act) { return nullptr; } return new KBookmarkContextMenu(act->bookmark(), m_pManager, m_pOwner); } bool KBookmarkMenu::isRoot() const { return m_bIsRoot; } bool KBookmarkMenu::isDirty() const { return m_bDirty; } QString KBookmarkMenu::parentAddress() const { return m_parentAddress; } KBookmarkManager *KBookmarkMenu::manager() const { return m_pManager; } KBookmarkOwner *KBookmarkMenu::owner() const { return m_pOwner; } QMenu *KBookmarkMenu::parentMenu() const { return m_parentMenu; } /********************************************************************/ /********************************************************************/ /********************************************************************/ /********************************************************************/ /********************************************************************/ /********************************************************************/ void KBookmarkMenu::slotBookmarksChanged(const QString &groupAddress) { qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotBookmarksChanged groupAddress: " << groupAddress; if (groupAddress == m_parentAddress) { //qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::slotBookmarksChanged -> setting m_bDirty on " << groupAddress; m_bDirty = true; } else { // Iterate recursively into child menus for (QList::iterator it = m_lstSubMenus.begin(), end = m_lstSubMenus.end(); it != end; ++it) { (*it)->slotBookmarksChanged(groupAddress); } } } void KBookmarkMenu::clear() { qDeleteAll(m_lstSubMenus); m_lstSubMenus.clear(); for (QList::iterator it = m_actions.begin(), end = m_actions.end(); it != end; ++it) { m_parentMenu->removeAction(*it); delete *it; } m_parentMenu->clear(); m_actions.clear(); } void KBookmarkMenu::refill() { //qCDebug(KBOOKMARKS_LOG) << "KBookmarkMenu::refill()"; if (m_bIsRoot) { addActions(); } fillBookmarks(); if (!m_bIsRoot) { addActions(); } } void KBookmarkMenu::addOpenInTabs() { if (!m_pOwner || !m_pOwner->supportsTabs() || !KAuthorized::authorizeAction(QStringLiteral("bookmarks"))) { return; } QString title = tr("Open Folder in Tabs"); QAction *paOpenFolderInTabs = new QAction(title, this); paOpenFolderInTabs->setIcon(QIcon::fromTheme(QStringLiteral("tab-new"))); paOpenFolderInTabs->setToolTip(tr("Open all bookmarks in this folder as a new tab.")); paOpenFolderInTabs->setStatusTip(paOpenFolderInTabs->toolTip()); connect(paOpenFolderInTabs, &QAction::triggered, this, &KBookmarkMenu::slotOpenFolderInTabs); m_parentMenu->addAction(paOpenFolderInTabs); m_actions.append(paOpenFolderInTabs); } void KBookmarkMenu::addAddBookmarksList() { if (!m_pOwner || !m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark) || !m_pOwner->supportsTabs() || - !KAuthorized::authorizeAction(QStringLiteral("bookmarks"))) { + (d->numberOfOpenTabs < 2) || !KAuthorized::authorizeAction(QStringLiteral("bookmarks"))) { return; } if (d->bookmarksToFolder == nullptr) { QString title = tr("Bookmark Tabs as Folder..."); d->bookmarksToFolder = new QAction(title, this); m_actionCollection->addAction(m_bIsRoot ? QStringLiteral("add_bookmarks_list") : QString(), d->bookmarksToFolder); d->bookmarksToFolder->setIcon(QIcon::fromTheme(QStringLiteral("bookmark-new-list"))); d->bookmarksToFolder->setToolTip(tr("Add a folder of bookmarks for all open tabs.")); d->bookmarksToFolder->setStatusTip(d->bookmarksToFolder->toolTip()); connect(d->bookmarksToFolder, &QAction::triggered, this, &KBookmarkMenu::slotAddBookmarksList); } m_parentMenu->addAction(d->bookmarksToFolder); } void KBookmarkMenu::addAddBookmark() { if (!m_pOwner || !m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark) || !KAuthorized::authorizeAction(QStringLiteral("bookmarks"))) { return; } if (d->addAddBookmark == nullptr) { d->addAddBookmark = m_actionCollection->addAction( KStandardAction::AddBookmark, m_bIsRoot ? QStringLiteral("add_bookmark") : QString(), this, SLOT(slotAddBookmark())); if (!m_bIsRoot) { d->addAddBookmark->setShortcut(QKeySequence()); } } m_parentMenu->addAction(d->addAddBookmark); } void KBookmarkMenu::addEditBookmarks() { if ((m_pOwner && !m_pOwner->enableOption(KBookmarkOwner::ShowEditBookmark)) || QStandardPaths::findExecutable(QStringLiteral(KEDITBOOKMARKS_BINARY)).isEmpty() || !KAuthorized::authorizeAction(QStringLiteral("bookmarks"))) { return; } QAction *m_paEditBookmarks = m_actionCollection->addAction(KStandardAction::EditBookmarks, QStringLiteral("edit_bookmarks"), m_pManager, SLOT(slotEditBookmarks())); m_parentMenu->addAction(m_paEditBookmarks); m_paEditBookmarks->setToolTip(tr("Edit your bookmark collection in a separate window")); m_paEditBookmarks->setStatusTip(m_paEditBookmarks->toolTip()); } void KBookmarkMenu::addNewFolder() { if (!m_pOwner || !m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark) || !KAuthorized::authorizeAction(QStringLiteral("bookmarks"))) { return; } if (d->newBookmarkFolder == nullptr) { d->newBookmarkFolder = new QAction(tr("New Bookmark Folder..."), this); d->newBookmarkFolder->setIcon(QIcon::fromTheme(QStringLiteral("folder-new"))); d->newBookmarkFolder->setToolTip(tr("Create a new bookmark folder in this menu")); d->newBookmarkFolder->setStatusTip(d->newBookmarkFolder->toolTip()); connect(d->newBookmarkFolder, &QAction::triggered, this, &KBookmarkMenu::slotNewFolder); } m_parentMenu->addAction(d->newBookmarkFolder); } void KBookmarkMenu::fillBookmarks() { KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); Q_ASSERT(!parentBookmark.isNull()); if (m_bIsRoot && !parentBookmark.first().isNull()) { // at least one bookmark m_parentMenu->addSeparator(); } for (KBookmark bm = parentBookmark.first(); !bm.isNull(); bm = parentBookmark.next(bm)) { m_parentMenu->addAction(actionForBookmark(bm)); } } QAction *KBookmarkMenu::actionForBookmark(const KBookmark &bm) { if (bm.isGroup()) { //qCDebug(KBOOKMARKS_LOG) << "Creating bookmark submenu named " << bm.text(); KActionMenu *actionMenu = new KBookmarkActionMenu(bm, this); m_actions.append(actionMenu); KBookmarkMenu *subMenu = new KBookmarkMenu(m_pManager, m_pOwner, actionMenu->menu(), bm.address()); m_lstSubMenus.append(subMenu); return actionMenu; } else if (bm.isSeparator()) { QAction *sa = new QAction(this); sa->setSeparator(true); m_actions.append(sa); return sa; } else { //qCDebug(KBOOKMARKS_LOG) << "Creating bookmark menu item for " << bm.text(); QAction *action = new KBookmarkAction(bm, m_pOwner, this); m_actions.append(action); return action; } } void KBookmarkMenu::slotAddBookmarksList() { if (!m_pOwner || !m_pOwner->supportsTabs()) { return; } KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow()); dlg->addBookmarks(m_pOwner->currentBookmarkList(), QLatin1String(""), parentBookmark); delete dlg; } void KBookmarkMenu::slotAddBookmark() { if (!m_pOwner) { return; } if (m_pOwner->currentTitle().isEmpty() && m_pOwner->currentUrl().isEmpty()) { return; } KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); if (KBookmarkSettings::self()->m_advancedaddbookmark) { KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow()); dlg->addBookmark(m_pOwner->currentTitle(), m_pOwner->currentUrl(), m_pOwner->currentIcon(), parentBookmark); delete dlg; } else { parentBookmark.addBookmark(m_pOwner->currentTitle(), m_pOwner->currentUrl(), m_pOwner->currentIcon()); m_pManager->emitChanged(parentBookmark); } } void KBookmarkMenu::slotOpenFolderInTabs() { m_pOwner->openFolderinTabs(m_pManager->findByAddress(m_parentAddress).toGroup()); } void KBookmarkMenu::slotNewFolder() { if (!m_pOwner) { return; // this view doesn't handle bookmarks... } KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); Q_ASSERT(!parentBookmark.isNull()); KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow()); dlg->createNewFolder(QLatin1String(""), parentBookmark); delete dlg; } void KImportedBookmarkMenu::slotNSLoad() { // qCDebug(KBOOKMARKS_LOG)<<"**** slotNSLoad ****"<setFilename(location); connectToImporter(*importer); importer->parse(); delete importer; } void KBookmarkMenuImporter::connectToImporter(const QObject &importer) { connect(&importer, SIGNAL(newBookmark(QString,QString,QString)), SLOT(newBookmark(QString,QString,QString))); connect(&importer, SIGNAL(newFolder(QString,bool,QString)), SLOT(newFolder(QString,bool,QString))); connect(&importer, SIGNAL(newSeparator()), SLOT(newSeparator())); connect(&importer, SIGNAL(endFolder()), SLOT(endFolder())); } void KBookmarkMenuImporter::newBookmark(const QString &text, const QString &url, const QString &) { KBookmark bm = KBookmark::standaloneBookmark(text, QUrl(url), QStringLiteral("html")); QAction *action = new KBookmarkAction(bm, mstack.top()->owner(), this); mstack.top()->parentMenu()->addAction(action); mstack.top()->m_actions.append(action); } void KBookmarkMenuImporter::newFolder(const QString &text, bool, const QString &) { QString _text = KStringHandler::csqueeze(text).replace(QLatin1Char('&'), QLatin1String("&&")); KActionMenu *actionMenu = new KImportedBookmarkActionMenu(QIcon::fromTheme(QStringLiteral("folder")), _text, this); mstack.top()->parentMenu()->addAction(actionMenu); mstack.top()->m_actions.append(actionMenu); KImportedBookmarkMenu *subMenu = new KImportedBookmarkMenu(m_pManager, m_menu->owner(), actionMenu->menu()); mstack.top()->m_lstSubMenus.append(subMenu); mstack.push(subMenu); } void KBookmarkMenuImporter::newSeparator() { mstack.top()->parentMenu()->addSeparator(); } void KBookmarkMenuImporter::endFolder() { mstack.pop(); } #include "moc_kbookmarkmenu.cpp" #include "moc_kbookmarkmenu_p.cpp" diff --git a/src/kbookmarkmenu.h b/src/kbookmarkmenu.h index 08bf09b..1166d04 100644 --- a/src/kbookmarkmenu.h +++ b/src/kbookmarkmenu.h @@ -1,170 +1,187 @@ /* This file is part of the KDE project Copyright (C) 1998, 1999 Torben Weis Copyright (C) 2006 Daniel Teske This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef __kbookmarkmenu_h__ #define __kbookmarkmenu_h__ #include #include class QAction; class QMenu; class KBookmark; class KActionCollection; class KBookmarkManager; class KBookmarkOwner; class KBookmarkMenu; class KBookmarkActionInterface; class KBookmarkMenuPrivate; // Not implemented /** * This class provides a bookmark menu. It is typically used in * cooperation with KActionMenu but doesn't have to be. * * If you use this class by itself, then it will use KDE defaults for * everything -- the bookmark path, bookmark editor, bookmark launcher.. * everything. These defaults reside in the classes * KBookmarkOwner (editing bookmarks) and KBookmarkManager * (almost everything else). If you wish to change the defaults in * any way, you must reimplement either this class or KBookmarkOwner. * * Using this class is very simple: * * 1) Create a popup menu (either KActionMenu or KMenu will do) * 2) Instantiate a new KBookmarkMenu object using the above popup * menu as a parameter * 3) Insert your (now full) popup menu wherever you wish * * The functionality of this class can be disabled with the "action/bookmarks" * Kiosk action (see the KAuthorized namespace). */ class KBOOKMARKS_EXPORT KBookmarkMenu : public QObject { Q_OBJECT public: /** * Fills a bookmark menu * (one instance of KBookmarkMenu is created for the toplevel menu, * but also one per submenu). * * @param mgr The bookmark manager to use (i.e. for reading and writing) * @param owner implementation of the KBookmarkOwner callback interface. * Note: If you pass a null KBookmarkOwner to the constructor, the * openBookmark signal is not emitted, instead QDesktopServices::openUrl is used to open the bookmark. * @param parentMenu menu to be filled * @param collec parent collection for the KActions. * * @todo KDE 5: give ownership of the bookmarkmenu to another qobject, e.g. parentMenu. * Currently this is a QObject without a parent, use setParent to benefit from automatic deletion. */ KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, KActionCollection *collec); /** * Creates a bookmark submenu * * @todo KF6: give ownership of the bookmarkmenu to another qobject, e.g. parentMenu. * Currently this is a QObject without a parent, use setParent to benefit from automatic deletion. */ KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, const QString &parentAddress); ~KBookmarkMenu(); /** * Call ensureUpToDate() if you need KBookmarkMenu to adjust to its * final size before it is executed. **/ void ensureUpToDate(); + /** + * @brief Sets the number of currently open tabs + * @param numberOfOpenTabs The number of currently open tabs + * @see numberOfOpenTabs() + * @since 5.58 + */ + void setNumberOfOpenTabs(int numberOfOpenTabs); + /** + * This function returns how many (if any) tabs the application has open. + * This is used to determine if the Add a folder for all open + * tabs should be added to the menu, so if the application has more than + * one tab open, then the menu will be added. + * Default returns @c 2. + * @since 5.58 + */ + int numberOfOpenTabs() const; + public Q_SLOTS: // public for KonqBookmarkBar void slotBookmarksChanged(const QString &); protected Q_SLOTS: void slotAboutToShow(); void slotAddBookmarksList(); void slotAddBookmark(); void slotNewFolder(); void slotOpenFolderInTabs(); protected: virtual void clear(); virtual void refill(); virtual QAction *actionForBookmark(const KBookmark &bm); virtual QMenu *contextMenu(QAction *action); void addActions(); void fillBookmarks(); void addAddBookmark(); void addAddBookmarksList(); void addEditBookmarks(); void addNewFolder(); void addOpenInTabs(); bool isRoot() const; bool isDirty() const; /** * Parent bookmark for this menu. */ QString parentAddress() const; KBookmarkManager *manager() const; KBookmarkOwner *owner() const; /** * The menu in which we insert our actions * Supplied in the constructor. */ QMenu *parentMenu() const; /** * List of our sub menus */ QList m_lstSubMenus; // This is used to "export" our actions into an actionlist // we got in the constructor. So that the program can show our // actions in their shortcut dialog KActionCollection *m_actionCollection; /** * List of our actions. */ QList m_actions; private Q_SLOTS: void slotCustomContextMenu(const QPoint &); private: KBookmarkMenuPrivate *d; bool m_bIsRoot; bool m_bDirty; KBookmarkManager *m_pManager; KBookmarkOwner *m_pOwner; QMenu *m_parentMenu; private: QString m_parentAddress; }; #endif