File indexing completed on 2024-04-21 14:53:28

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 #ifndef __kbookmarkmenu_h__
0010 #define __kbookmarkmenu_h__
0011 
0012 #include <kbookmarks_export.h>
0013 
0014 #include <QObject>
0015 #include <memory>
0016 
0017 class QAction;
0018 class QMenu;
0019 class KBookmark;
0020 class KActionCollection;
0021 class KBookmarkManager;
0022 class KBookmarkOwner;
0023 class KBookmarkMenu;
0024 
0025 class KBookmarkMenuPrivate;
0026 
0027 /**
0028  * @class KBookmarkMenu kbookmarkmenu.h KBookmarkMenu
0029  *
0030  * This class provides a bookmark menu.  It is typically used in
0031  * cooperation with KActionMenu but doesn't have to be.
0032  *
0033  * If you use this class by itself, then it will use KDE defaults for
0034  * everything -- the bookmark path, bookmark editor, bookmark launcher..
0035  * everything.  These defaults reside in the classes
0036  * KBookmarkOwner (editing bookmarks) and KBookmarkManager
0037  * (almost everything else).  If you wish to change the defaults in
0038  * any way, you must reimplement either this class or KBookmarkOwner.
0039  *
0040  * Using this class is very simple:
0041  *
0042  * 1) Create a popup menu (either KActionMenu or QMenu will do)
0043  * 2) Instantiate a new KBookmarkMenu object using the above popup
0044  *    menu as a parameter
0045  * 3) Insert your (now full) popup menu wherever you wish
0046  *
0047  * The functionality of this class can be disabled with the "action/bookmarks"
0048  * Kiosk action (see the KAuthorized namespace).
0049  */
0050 class KBOOKMARKS_EXPORT KBookmarkMenu : public QObject
0051 {
0052     Q_OBJECT
0053 public:
0054 #if KBOOKMARKS_ENABLE_DEPRECATED_SINCE(5, 69)
0055     /**
0056      * Fills a bookmark menu
0057      * (one instance of KBookmarkMenu is created for the toplevel menu,
0058      *  but also one per submenu).
0059      *
0060      * @param mgr The bookmark manager to use (i.e. for reading and writing)
0061      * @param owner implementation of the KBookmarkOwner callback interface.
0062      * Note: If you pass a null KBookmarkOwner to the constructor, the
0063      * openBookmark signal is not emitted, instead QDesktopServices::openUrl is used to open the bookmark.
0064      * @param parentMenu menu to be filled
0065      * @param collec parent collection for the KActions.
0066      *
0067      * @todo KDE 5: give ownership of the bookmarkmenu to another qobject, e.g. parentMenu.
0068      * Currently this is a QObject without a parent, use setParent to benefit from automatic deletion.
0069      *
0070      * @deprecated since 5.69. Use overload without KActionCollection and add actions manually to your actioncollection if desired.
0071      * @code
0072      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0073      *
0074      * QAction *addAction = menu->addBookmarkAction();
0075      * actionCollection()->addAction(addAction->objectName(), addAction);
0076      *
0077      * QAction *bookmarkTabsAction = menu->bookmarkTabsAsFolderAction();
0078      * actionCollection()->addAction(bookmarkTabsAction->objectName(), bookmarkTabsAction);
0079      *
0080      * QAction *editAction = menu->editBookmarksAction();
0081      * actionCollection()->addAction(editAction->objectName(), editAction);
0082      * @endcode
0083      *
0084      * Alternatively you can get a list of the actions from @p parentMenu, and add them to your
0085      * actionCollection:
0086      * @code
0087      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0088      * actionCollection()->addActions(parentMenu->actions());
0089      * @endcode
0090      *
0091      * Note that adding the actions to your KActionCollection will make them show up in the shortcuts editor
0092      * dialog.
0093      *
0094      */
0095     KBOOKMARKS_DEPRECATED_VERSION(5, 69, "Use overload without KActionCollection and add actions manually to your actionCollection if desired")
0096     KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, KActionCollection *collec);
0097 #endif
0098 
0099     /**
0100      * Fills a bookmark menu
0101      * (one instance of KBookmarkMenu is created for the toplevel menu,
0102      *  but also one per submenu).
0103      *
0104      * @param manager the bookmark manager to use (i.e. for reading and writing)
0105      * @param owner implementation of the KBookmarkOwner callback interface.
0106      * @note If you pass a null KBookmarkOwner to the constructor, the
0107      * openBookmark signal is not emitted, instead QDesktopServices::openUrl is used to open the bookmark.
0108      * @param parentMenu menu to be filled
0109      * @since 5.69
0110      */
0111     KBookmarkMenu(KBookmarkManager *manager, KBookmarkOwner *owner, QMenu *parentMenu);
0112 
0113     /**
0114      * Creates a bookmark submenu
0115      *
0116      * @todo KF6: give ownership of the bookmarkmenu to another qobject, e.g. parentMenu.
0117      * Currently this is a QObject without a parent, use setParent to benefit from automatic deletion.
0118      */
0119     KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, const QString &parentAddress);
0120 
0121     ~KBookmarkMenu() override;
0122 
0123     /**
0124      * Call ensureUpToDate() if you need KBookmarkMenu to adjust to its
0125      * final size before it is executed.
0126      **/
0127     void ensureUpToDate();
0128 
0129     /**
0130      * @brief Sets the number of currently open tabs
0131      * @param numberOfOpenTabs The number of currently open tabs
0132      * @see numberOfOpenTabs()
0133      * @since 5.58
0134      */
0135     void setNumberOfOpenTabs(int numberOfOpenTabs);
0136     /**
0137      * This function returns how many (if any) tabs the application has open.
0138      * This is used to determine if the Add a folder for all open
0139      * tabs should be added to the menu, so if the application has more than
0140      * one tab open, then the menu will be added.
0141      * Default returns @c 2.
0142      * @since 5.58
0143      */
0144     int numberOfOpenTabs() const;
0145 
0146     /**
0147      * Returns the action for adding a bookmark. If you are using KXmlGui, you can add it to your
0148      * action collection.
0149      * @code
0150      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0151      * QAction *addAction = menu->addBookmarkAction();
0152      * actionCollection()->addAction(addAction->objectName(), addAction);
0153      * @endcode
0154      * @return the action for adding a bookmark.
0155      * @since 5.69
0156      */
0157     QAction *addBookmarkAction() const;
0158 
0159     /**
0160      * Returns the action for adding all current tabs as bookmarks. If you are using KXmlGui, you can
0161      * add it to your action collection.
0162      * @code
0163      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0164      * QAction *bookmarkTabsAction = menu->bookmarkTabsAsFolderAction();
0165      * actionCollection()->addAction(bookmarkTabsAction->objectName(), bookmarkTabsAction);
0166      * @endcode
0167      * @return the action for adding all current tabs as bookmarks.
0168      * @since 5.69
0169      */
0170     QAction *bookmarkTabsAsFolderAction() const;
0171 
0172     /**
0173      * Returns the action for adding a new bookmarks folder. If you are using KXmlGui, you can add it
0174      * to your action collection.
0175      * @code
0176      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0177      * QAction *newBookmarkFolderAction = menu->bookmarkTabsAsFolderAction();
0178      * actionCollection()->addAction(newBookmarkFolderAction->objectName(), newBookmarkFolderAction);
0179      * @endcode
0180      * @return the action for adding a new bookmarks folder
0181      * @since 5.70
0182      */
0183     QAction *newBookmarkFolderAction() const;
0184 
0185     /**
0186      * Returns the action for editing bookmarks.  If you are using KXmlGui, you can add it to your
0187      * action collection.
0188      * @code
0189      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0190      * QAction *editAction = menu->editBookmarksAction();
0191      * actionCollection()->addAction(editAction->objectName(), editAction);
0192      * @endcode
0193      * @return the action for editing bookmarks.
0194      * @since 5.69
0195      */
0196     QAction *editBookmarksAction() const;
0197 
0198 public Q_SLOTS:
0199     // public for KonqBookmarkBar
0200     void slotBookmarksChanged(const QString &);
0201 
0202 protected Q_SLOTS:
0203     void slotAboutToShow();
0204     void slotAddBookmarksList();
0205     void slotAddBookmark();
0206     void slotNewFolder();
0207     void slotOpenFolderInTabs();
0208 
0209 protected:
0210     virtual void clear();
0211     virtual void refill();
0212     virtual QAction *actionForBookmark(const KBookmark &bm);
0213     virtual QMenu *contextMenu(QAction *action);
0214 
0215     void addActions();
0216     void fillBookmarks();
0217     void addAddBookmark();
0218     void addAddBookmarksList();
0219     void addEditBookmarks();
0220     void addNewFolder();
0221     void addOpenInTabs();
0222 
0223     bool isRoot() const;
0224     bool isDirty() const;
0225 
0226     /**
0227      * Parent bookmark for this menu.
0228      */
0229     QString parentAddress() const;
0230 
0231     KBookmarkManager *manager() const;
0232     KBookmarkOwner *owner() const;
0233     /**
0234      * The menu in which we insert our actions
0235      * Supplied in the constructor.
0236      */
0237     QMenu *parentMenu() const;
0238 
0239     /**
0240      * List of our sub menus
0241      */
0242     QList<KBookmarkMenu *> m_lstSubMenus;
0243 
0244     // This is used to "export" our actions into an actionlist
0245     // we got in the constructor. So that the program can show our
0246     // actions in their shortcut dialog
0247     KActionCollection *m_actionCollection;
0248     /**
0249      * List of our actions.
0250      */
0251     QList<QAction *> m_actions;
0252 
0253 private Q_SLOTS:
0254     KBOOKMARKS_NO_EXPORT void slotCustomContextMenu(const QPoint &);
0255 
0256 private:
0257     KBOOKMARKS_NO_EXPORT void init();
0258 
0259 private:
0260     std::unique_ptr<KBookmarkMenuPrivate> const d;
0261 
0262     // TODO KF6:: move into KBookmarkMenuPrivate
0263     bool m_bIsRoot;
0264     bool m_bDirty;
0265     KBookmarkManager *m_pManager;
0266     KBookmarkOwner *m_pOwner;
0267     QMenu *m_parentMenu;
0268     QString m_parentAddress;
0269 };
0270 
0271 #endif