1 /* This file is part of the KDE project
2     SPDX-FileCopyrightText: 1998-2016 David Faure <faure@kde.org>
3     SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@yahoo.com>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef __konqpopupmenu_h
9 #define __konqpopupmenu_h
10 
11 #include <QMenu>
12 
13 #include <libkonq_export.h>
14 
15 class KFileItemList;
16 class KNewFileMenu;
17 
18 class KActionCollection;
19 class KBookmarkManager;
20 class KonqPopupMenuPrivate;
21 class QUrl;
22 
23 /**
24  * This class implements the popup menu for URLs in file managers.
25  *
26  * The recommended usage is to use QMenu::popup() to avoid modal popupmenus.
27  *
28  * Either reuse the instance, or delete it after use with
29  *   connect(menu, &QMenu::aboutToHide, [menu]() { menu->deleteLater(); });
30  *
31  * Users of KonqPopupMenu include: konqueror, kfind, the folderview desktop plasmoid.
32  */
33 class LIBKONQ_EXPORT KonqPopupMenu : public QMenu
34 {
35     Q_OBJECT
36 public:
37 
38     /**
39      * Each action group is inserted into the context menu at a specific position.
40      * "editactions" for actions related text editing,
41      * "linkactions" for actions related to hyperlinks,
42      * "partactions" for any other actions provided by the part
43      */
44     enum ActionGroup {
45         TopActions,  ///< actions to be shown at the top of the context menu, like "show menu bar"
46         TabHandlingActions, ///< actions for tab handling, like "open in new tab"
47         EditActions, ///< move to trash, delete, etc.
48         PreviewActions, ///< actions related to viewing the selected file with an embedded viewer
49         CustomActions, ///< group for more custom actions, such as those provided by KParts components
50         LinkActions  ///< actions related to handling of hyperlinks, only shown if the IsLink flag is set
51     };
52 
53     typedef QMap<ActionGroup, QList<QAction *> > ActionGroupMap;
54 
55     /**
56      * Set of flags to ask for some items in the popup menu.
57      */
58     enum Flag {
59         DefaultPopupItems = 0x0000, ///< default value, no additional menu item
60         ShowBookmark = 0x0008, ///< show "add to bookmarks" (usually not done on the local filesystem)
61         ShowCreateDirectory = 0x0010, ///<  show "create directory" (usually only done on the background of the view, or
62                                       /// in hierarchical views like directory trees, where the new dir would be visible)
63         ShowTextSelectionItems = 0x0020, ///< set when selecting text, for a popup that only contains text-related items.
64         NoDeletion = 0x0040, ///< "cut" not allowed (e.g. parent dir not writeable).
65                              /// (this is only needed if the protocol itself supports deletion, unlike e.g. HTTP)
66         IsLink = 0x0080, ///< show "Bookmark This Link" and other link-related actions (LinkActions group)
67         ShowUrlOperations = 0x0100, ///< show copy, paste, as well as cut if NoDeletion is not set.
68         ShowProperties = 0x0200,   ///< show "Properties" action (usually done by directory views)
69         ShowNewWindow = 0x0400, ///< Show "Open in new window" action
70         NoPlugins = 0x0800      ///< Disable plugins - mostly for the unittest
71      };
72     Q_DECLARE_FLAGS(Flags, Flag)
73 
74     /**
75      * Constructor
76      * @param items the list of file items the popupmenu should be shown for
77      * @param viewURL the URL shown in the view, to test for RMB click on view background
78      * @param actions list of actions the caller wants to see in the menu
79      * @param flags flags which control which items to show in the popupmenu
80      * @param parentWidget the widget we're showing this popup for. Helps destroying
81      * the popup if the widget is destroyed before the popup.
82      *
83      * The actions to pass in the action collection are :
84      * reload, cut, copy, paste, pasteto
85      * The others items are automatically inserted.
86      */
87     KonqPopupMenu(const KFileItemList &items,
88                   const QUrl &viewURL,
89                   KActionCollection &actions,
90                   Flags flags,
91                   QWidget *parentWidget = nullptr);
92 
93     /**
94      * Don't forget to destroy the object
95      */
96     ~KonqPopupMenu() override;
97 
98     /**
99      * Sets the "New file" menu instance. This allows to share it with the menubar.
100      */
101     void setNewFileMenu(KNewFileMenu *newMenu);
102 
103     /**
104      * Sets the bookmark manager for the "add to bookmark" action.
105      * Only used if ShowBookmark is set
106      */
107     void setBookmarkManager(KBookmarkManager *manager);
108 
109     /**
110      * Sets the additional actions to show in the context menu
111      */
112     void setActionGroups(const ActionGroupMap &actionGroups);
113 
114     /**
115      * Set the title of the URL, when the popupmenu is opened for a single URL.
116      * This is used if the user chooses to add a bookmark for this URL.
117      */
118     void setURLTitle(const QString &urlTitle);
119 
120 Q_SIGNALS:
121     /**
122      * Emitted before the "Open With" dialog is shown
123      * This is used e.g in folderview to close the folder peek popups on invoking the "Open With" menu action
124      */
125     void openWithDialogAboutToBeShown();
126 
127 private:
128     KonqPopupMenuPrivate *d;
129 };
130 
131 #endif
132