1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "guilib/GUIDialog.h"
12 
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 
18 class CMediaSource;
19 
20 enum CONTEXT_BUTTON { CONTEXT_BUTTON_CANCELLED = 0,
21                       CONTEXT_BUTTON_RENAME,
22                       CONTEXT_BUTTON_DELETE,
23                       CONTEXT_BUTTON_MOVE,
24                       CONTEXT_BUTTON_SETTINGS,
25                       CONTEXT_BUTTON_RIP_CD,
26                       CONTEXT_BUTTON_CANCEL_RIP_CD,
27                       CONTEXT_BUTTON_RIP_TRACK,
28                       CONTEXT_BUTTON_EJECT_DISC,
29                       CONTEXT_BUTTON_EJECT_DRIVE,
30                       CONTEXT_BUTTON_EDIT_SOURCE,
31                       CONTEXT_BUTTON_REMOVE_SOURCE,
32                       CONTEXT_BUTTON_SET_DEFAULT,
33                       CONTEXT_BUTTON_CLEAR_DEFAULT,
34                       CONTEXT_BUTTON_SET_THUMB,
35                       CONTEXT_BUTTON_ADD_LOCK,
36                       CONTEXT_BUTTON_REMOVE_LOCK,
37                       CONTEXT_BUTTON_CHANGE_LOCK,
38                       CONTEXT_BUTTON_RESET_LOCK,
39                       CONTEXT_BUTTON_REACTIVATE_LOCK,
40                       CONTEXT_BUTTON_VIEW_SLIDESHOW,
41                       CONTEXT_BUTTON_RECURSIVE_SLIDESHOW,
42                       CONTEXT_BUTTON_REFRESH_THUMBS,
43                       CONTEXT_BUTTON_SWITCH_MEDIA,
44                       CONTEXT_BUTTON_MOVE_ITEM,
45                       CONTEXT_BUTTON_MOVE_HERE,
46                       CONTEXT_BUTTON_CANCEL_MOVE,
47                       CONTEXT_BUTTON_MOVE_ITEM_UP,
48                       CONTEXT_BUTTON_MOVE_ITEM_DOWN,
49                       CONTEXT_BUTTON_CLEAR,
50                       CONTEXT_BUTTON_QUEUE_ITEM,
51                       CONTEXT_BUTTON_PLAY_ITEM,
52                       CONTEXT_BUTTON_PLAY_WITH,
53                       CONTEXT_BUTTON_PLAY_PARTYMODE,
54                       CONTEXT_BUTTON_PLAY_PART,
55                       CONTEXT_BUTTON_RESUME_ITEM,
56                       CONTEXT_BUTTON_EDIT,
57                       CONTEXT_BUTTON_EDIT_SMART_PLAYLIST,
58                       CONTEXT_BUTTON_INFO,
59                       CONTEXT_BUTTON_INFO_ALL,
60                       CONTEXT_BUTTON_CDDB,
61                       CONTEXT_BUTTON_SCAN,
62                       CONTEXT_BUTTON_SCAN_TO_LIBRARY,
63                       CONTEXT_BUTTON_SET_ARTIST_THUMB,
64                       CONTEXT_BUTTON_SET_SEASON_ART,
65                       CONTEXT_BUTTON_CANCEL_PARTYMODE,
66                       CONTEXT_BUTTON_MARK_WATCHED,
67                       CONTEXT_BUTTON_MARK_UNWATCHED,
68                       CONTEXT_BUTTON_SET_CONTENT,
69                       CONTEXT_BUTTON_EDIT_PARTYMODE,
70                       CONTEXT_BUTTON_LINK_MOVIE,
71                       CONTEXT_BUTTON_UNLINK_MOVIE,
72                       CONTEXT_BUTTON_GO_TO_ARTIST,
73                       CONTEXT_BUTTON_GO_TO_ALBUM,
74                       CONTEXT_BUTTON_PLAY_OTHER,
75                       CONTEXT_BUTTON_SET_ACTOR_THUMB,
76                       CONTEXT_BUTTON_UNLINK_BOOKMARK,
77                       CONTEXT_BUTTON_ACTIVATE,
78                       CONTEXT_BUTTON_GROUP_MANAGER,
79                       CONTEXT_BUTTON_CHANNEL_MANAGER,
80                       CONTEXT_BUTTON_SET_MOVIESET_ART,
81                       CONTEXT_BUTTON_PLAY_AND_QUEUE,
82                       CONTEXT_BUTTON_PLAY_ONLY_THIS,
83                       CONTEXT_BUTTON_UPDATE_EPG,
84                       CONTEXT_BUTTON_TAGS_ADD_ITEMS,
85                       CONTEXT_BUTTON_TAGS_REMOVE_ITEMS,
86                       CONTEXT_BUTTON_SET_MOVIESET,
87                       CONTEXT_BUTTON_MOVIESET_ADD_REMOVE_ITEMS,
88                       CONTEXT_BUTTON_BROWSE_INTO,
89                       CONTEXT_BUTTON_EDIT_SORTTITLE,
90                       CONTEXT_BUTTON_DELETE_ALL,
91                       CONTEXT_BUTTON_HELP,
92                       CONTEXT_BUTTON_PLAY_NEXT,
93                       CONTEXT_BUTTON_NAVIGATE,
94                     };
95 
96 class CContextButtons : public std::vector< std::pair<size_t, std::string> >
97 {
98 public:
99   void Add(unsigned int, const std::string &label);
100   void Add(unsigned int, int label);
101 };
102 
103 class CGUIDialogContextMenu :
104       public CGUIDialog
105 {
106 public:
107   CGUIDialogContextMenu(void);
108   ~CGUIDialogContextMenu(void) override;
109   bool OnMessage(CGUIMessage &message) override;
110   bool OnAction(const CAction& action) override;
111   void SetPosition(float posX, float posY) override;
112 
113   static bool SourcesMenu(const std::string &strType, const CFileItemPtr& item, float posX, float posY);
114   static void SwitchMedia(const std::string& strType, const std::string& strPath);
115 
116   static void GetContextButtons(const std::string &type, const CFileItemPtr& item, CContextButtons &buttons);
117   static bool OnContextButton(const std::string &type, const CFileItemPtr& item, CONTEXT_BUTTON button);
118 
119   /*! Show the context menu with the given choices and return the index of the selected item,
120     or -1 if cancelled.
121    */
122   static int Show(const CContextButtons& choices, int focusedButton = 0);
123 
124   /*! Legacy method that returns the context menu id, or -1 on cancel */
125   static int ShowAndGetChoice(const CContextButtons &choices);
126 
127 protected:
128   void SetupButtons();
129 
130   /*! \brief Position the context menu in the middle of the focused control.
131    If no control is available it is positioned in the middle of the screen.
132    */
133   void PositionAtCurrentFocus();
134 
135   float GetWidth() const override;
136   float GetHeight() const override;
137   void OnInitWindow() override;
138   void OnWindowLoaded() override;
139   void OnDeinitWindow(int nextWindowID) override;
140   static std::string GetDefaultShareNameByType(const std::string &strType);
141   static void SetDefault(const std::string &strType, const std::string &strDefault);
142   static void ClearDefault(const std::string &strType);
143   static CMediaSource *GetShare(const std::string &type, const CFileItem *item);
144 
145 private:
146   float m_coordX, m_coordY;
147   /// \brief Stored size of background image (height or width depending on grouplist orientation)
148   float m_backgroundImageSize;
149   int m_initiallyFocusedButtonIdx = 0;
150   int m_clickedButton;
151   CContextButtons m_buttons;
152   const CGUIControl *m_backgroundImage = nullptr;
153 };
154