File indexing completed on 2024-05-12 03:53:50

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 <kbookmarkswidgets_export.h>
0013 
0014 #include <QObject>
0015 #include <memory>
0016 
0017 class QAction;
0018 class QMenu;
0019 class KBookmark;
0020 class KBookmarkManager;
0021 class KBookmarkOwner;
0022 class KBookmarkMenu;
0023 
0024 class KBookmarkMenuPrivate;
0025 
0026 /**
0027  * @class KBookmarkMenu kbookmarkmenu.h KBookmarkMenu
0028  *
0029  * This class provides a bookmark menu.  It is typically used in
0030  * cooperation with KActionMenu but doesn't have to be.
0031  *
0032  * If you use this class by itself, then it will use KDE defaults for
0033  * everything -- the bookmark path, bookmark editor, bookmark launcher..
0034  * everything.  These defaults reside in the classes
0035  * KBookmarkOwner (editing bookmarks) and KBookmarkManager
0036  * (almost everything else).  If you wish to change the defaults in
0037  * any way, you must reimplement either this class or KBookmarkOwner.
0038  *
0039  * Using this class is very simple:
0040  *
0041  * 1) Create a popup menu (either KActionMenu or QMenu will do)
0042  * 2) Instantiate a new KBookmarkMenu object using the above popup
0043  *    menu as a parameter
0044  * 3) Insert your (now full) popup menu wherever you wish
0045  *
0046  * The functionality of this class can be disabled with the "action/bookmarks"
0047  * Kiosk action (see the KAuthorized namespace).
0048  */
0049 class KBOOKMARKSWIDGETS_EXPORT KBookmarkMenu : public QObject
0050 {
0051     Q_OBJECT
0052 public:
0053     /**
0054      * Fills a bookmark menu
0055      * (one instance of KBookmarkMenu is created for the toplevel menu,
0056      *  but also one per submenu).
0057      *
0058      * @param manager the bookmark manager to use (i.e. for reading and writing)
0059      * @param owner implementation of the KBookmarkOwner callback interface.
0060      * @note If you pass a null KBookmarkOwner to the constructor, the
0061      * openBookmark signal is not emitted, instead QDesktopServices::openUrl is used to open the bookmark.
0062      * @param parentMenu menu to be filled
0063      * @since 5.69
0064      */
0065     KBookmarkMenu(KBookmarkManager *manager, KBookmarkOwner *owner, QMenu *parentMenu);
0066 
0067     /**
0068      * Creates a bookmark submenu
0069      *
0070      * @todo KF6: give ownership of the bookmarkmenu to another qobject, e.g. parentMenu.
0071      * Currently this is a QObject without a parent, use setParent to benefit from automatic deletion.
0072      */
0073     KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, const QString &parentAddress);
0074 
0075     ~KBookmarkMenu() override;
0076 
0077     /**
0078      * Call ensureUpToDate() if you need KBookmarkMenu to adjust to its
0079      * final size before it is executed.
0080      **/
0081     void ensureUpToDate();
0082 
0083     /**
0084      * Returns the action for adding a bookmark. If you are using KXmlGui, you can add it to your
0085      * action collection.
0086      * @code
0087      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0088      * QAction *addAction = menu->addBookmarkAction();
0089      * actionCollection()->addAction(addAction->objectName(), addAction);
0090      * @endcode
0091      * @return the action for adding a bookmark.
0092      * @since 5.69
0093      */
0094     QAction *addBookmarkAction() const;
0095 
0096     /**
0097      * Returns the action for adding all current tabs as bookmarks. If you are using KXmlGui, you can
0098      * add it to your action collection.
0099      * @code
0100      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0101      * QAction *bookmarkTabsAction = menu->bookmarkTabsAsFolderAction();
0102      * actionCollection()->addAction(bookmarkTabsAction->objectName(), bookmarkTabsAction);
0103      * @endcode
0104      * @return the action for adding all current tabs as bookmarks.
0105      * @since 5.69
0106      */
0107     QAction *bookmarkTabsAsFolderAction() const;
0108 
0109     /**
0110      * Returns the action for adding a new bookmarks folder. If you are using KXmlGui, you can add it
0111      * to your action collection.
0112      * @code
0113      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0114      * QAction *newBookmarkFolderAction = menu->bookmarkTabsAsFolderAction();
0115      * actionCollection()->addAction(newBookmarkFolderAction->objectName(), newBookmarkFolderAction);
0116      * @endcode
0117      * @return the action for adding a new bookmarks folder
0118      * @since 5.70
0119      */
0120     QAction *newBookmarkFolderAction() const;
0121 
0122     /**
0123      * Returns the action for editing bookmarks.  If you are using KXmlGui, you can add it to your
0124      * action collection.
0125      * @code
0126      * KBookmarkMenu *menu = new KBookmarkMenu(manager, owner, parentMenu);
0127      * QAction *editAction = menu->editBookmarksAction();
0128      * actionCollection()->addAction(editAction->objectName(), editAction);
0129      * @endcode
0130      * @return the action for editing bookmarks.
0131      * @since 5.69
0132      */
0133     QAction *editBookmarksAction() const;
0134 
0135     /**
0136      * Set this to true to make any "Edit Bookmarks" dialog
0137      * show UI elements that are specific to browsers.
0138      *
0139      * @since 6.0
0140      */
0141     void setBrowserMode(bool browserMode);
0142 
0143     /**
0144      * Whether any "Edit Bookmarks" dialog shows UI elements
0145      * that are specific to browsers.
0146      *
0147      * @since 6.0
0148      */
0149     bool browserMode() const;
0150 
0151 public Q_SLOTS:
0152     // public for KonqBookmarkBar
0153     void slotBookmarksChanged(const QString &);
0154 
0155 protected Q_SLOTS:
0156     void slotAboutToShow();
0157     void slotAddBookmarksList();
0158     void slotAddBookmark();
0159     void slotNewFolder();
0160     void slotOpenFolderInTabs();
0161 
0162 protected:
0163     virtual void clear();
0164     virtual void refill();
0165     virtual QAction *actionForBookmark(const KBookmark &bm);
0166     virtual QMenu *contextMenu(QAction *action);
0167 
0168     void addActions();
0169     void fillBookmarks();
0170     void addAddBookmark();
0171     void addAddBookmarksList();
0172     void addEditBookmarks();
0173     void addNewFolder();
0174     void addOpenInTabs();
0175 
0176     bool isRoot() const;
0177     bool isDirty() const;
0178 
0179     /**
0180      * Parent bookmark for this menu.
0181      */
0182     QString parentAddress() const;
0183 
0184     KBookmarkManager *manager() const;
0185     KBookmarkOwner *owner() const;
0186     /**
0187      * The menu in which we insert our actions
0188      * Supplied in the constructor.
0189      */
0190     QMenu *parentMenu() const;
0191 
0192     /**
0193      * List of our sub menus
0194      */
0195     QList<KBookmarkMenu *> m_lstSubMenus;
0196 
0197     /**
0198      * List of our actions.
0199      */
0200     QList<QAction *> m_actions;
0201 
0202 private Q_SLOTS:
0203     KBOOKMARKSWIDGETS_NO_EXPORT void slotCustomContextMenu(const QPoint &);
0204 
0205 private:
0206     void slotEditBookmarks();
0207     KBOOKMARKSWIDGETS_NO_EXPORT void init();
0208 
0209 private:
0210     std::unique_ptr<KBookmarkMenuPrivate> const d;
0211 };
0212 
0213 #endif