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 "dialogs/GUIDialogContextMenu.h"
12 #include "filesystem/DirectoryHistory.h"
13 #include "filesystem/VirtualDirectory.h"
14 #include "guilib/GUIWindow.h"
15 #include "playlists/SmartPlayList.h"
16 #include "view/GUIViewControl.h"
17 
18 #include <atomic>
19 
20 class CFileItemList;
21 class CGUIViewState;
22 namespace
23 {
24 class CGetDirectoryItems;
25 }
26 
27 // base class for all media windows
28 class CGUIMediaWindow : public CGUIWindow
29 {
30 public:
31   CGUIMediaWindow(int id, const char *xmlFile);
32   ~CGUIMediaWindow(void) override;
33 
34   // specializations of CGUIControl
35   bool OnAction(const CAction &action) override;
36   bool OnBack(int actionID) override;
37   bool OnMessage(CGUIMessage& message) override;
38 
39   // specializations of CGUIWindow
40   void OnWindowLoaded() override;
41   void OnWindowUnload() override;
42   void OnInitWindow() override;
IsMediaWindow()43   bool IsMediaWindow() const  override { return true; }
GetViewContainerID()44   int GetViewContainerID() const  override { return m_viewControl.GetCurrentControl(); }
GetViewCount()45   int GetViewCount() const  override { return m_viewControl.GetViewModeCount(); };
HasListItems()46   bool HasListItems() const  override { return true; }
47   CFileItemPtr GetCurrentListItem(int offset = 0) override;
48 
49   // custom methods
CanFilterAdvanced()50   virtual bool CanFilterAdvanced() { return m_canFilterAdvanced; }
51   virtual bool IsFiltered();
52   virtual bool IsSameStartFolder(const std::string &dir);
53 
GetRootPath()54   virtual std::string GetRootPath() const { return ""; }
55 
56   const CFileItemList &CurrentDirectory() const;
57   const CGUIViewState *GetViewState() const;
UseFileDirectories()58   virtual bool UseFileDirectories() { return true; }
59 
60 protected:
61   // specializations of CGUIControlGroup
62   CGUIControl *GetFirstFocusableControl(int id) override;
63 
64   bool Load(TiXmlElement *pRootElement) override;
65 
66   // custom methods
67   virtual void SetupShares();
68   virtual bool GoParentFolder();
69   virtual bool OnClick(int iItem, const std::string &player = "");
70 
71   /* \brief React to a "Select" action on an item in a view.
72    \param item selected item.
73    \return true if the action is handled, false otherwise.
74    */
75   virtual bool OnSelect(int item);
76   virtual bool OnPopupMenu(int iItem);
77 
78   virtual void GetContextButtons(int itemNumber, CContextButtons &buttons);
79   virtual bool OnContextButton(int itemNumber, CONTEXT_BUTTON button);
OnAddMediaSource()80   virtual bool OnAddMediaSource() { return false; };
81 
82   virtual void FormatItemLabels(CFileItemList &items, const LABEL_MASKS &labelMasks);
83   virtual void UpdateButtons();
84   void SaveControlStates() override;
85   void RestoreControlStates() override;
86 
87   virtual bool GetDirectory(const std::string &strDirectory, CFileItemList &items);
88   /*! \brief Retrieves the items from the given path and updates the list
89    \param strDirectory The path to the directory to get the items from
90    \param updateFilterPath Whether to update the filter path in m_strFilterPath or not
91    \return true if the list was successfully updated otherwise false
92    \sa GetDirectory
93    \sa m_vecItems
94    \sa m_strFilterPath
95    */
96   virtual bool Update(const std::string &strDirectory, bool updateFilterPath = true);
97   /*! \brief Refreshes the current list by retrieving the lists's path
98    \return true if the list was successfully refreshed otherwise false
99    \sa Update
100    \sa GetDirectory
101    */
102   virtual bool Refresh(bool clearCache = false);
103 
104   virtual void FormatAndSort(CFileItemList &items);
105   virtual void OnPrepareFileItems(CFileItemList &items);
106   virtual void OnCacheFileItems(CFileItemList &items);
GetGroupedItems(CFileItemList & items)107   virtual void GetGroupedItems(CFileItemList &items) { }
108 
109   void ClearFileItems();
110   virtual void SortItems(CFileItemList &items);
111 
112   /*! \brief Check if the given list can be advance filtered or not
113    \param items List of items to check
114    \return true if the list can be advance filtered otherwise false
115    */
CheckFilterAdvanced(CFileItemList & items)116   virtual bool CheckFilterAdvanced(CFileItemList &items) const { return false; }
117   /*! \brief Check if the given path can contain a "filter" parameter
118    \param strDirectory Path to check
119    \return true if the given path can contain a "filter" parameter otherwise false
120    */
CanContainFilter(const std::string & strDirectory)121   virtual bool CanContainFilter(const std::string &strDirectory) const { return false; }
122   virtual void UpdateFilterPath(const std::string &strDirector, const CFileItemList &items, bool updateFilterPath);
123   virtual bool Filter(bool advanced = true);
124 
125   /* \brief Called on response to a GUI_MSG_FILTER_ITEMS message
126    Filters the current list with the given filter using FilterItems()
127    \param filter the filter to use.
128    \sa FilterItems
129    */
130   void OnFilterItems(const std::string &filter);
131 
132   /* \brief Retrieve the filtered item list
133    \param filter filter to apply
134    \param items CFileItemList to filter
135    \sa OnFilterItems
136    */
137   virtual bool GetFilteredItems(const std::string &filter, CFileItemList &items);
138 
139   /* \brief Retrieve the advance filtered item list
140   \param items CFileItemList to filter
141   \param hasNewItems Whether the filtered item list contains new items
142                      which were not present in the original list
143   \sa GetFilteredItems
144   */
145   virtual bool GetAdvanceFilteredItems(CFileItemList &items);
146 
147   // check for a disc or connection
148   virtual bool HaveDiscOrConnection(const std::string& strPath, int iDriveType);
149   void ShowShareErrorMessage(CFileItem* pItem) const;
150 
151   void SaveSelectedItemInHistory();
152   void RestoreSelectedItemFromHistory();
153   void GetDirectoryHistoryString(const CFileItem* pItem, std::string& strHistoryString) const;
154   void SetHistoryForPath(const std::string& strDirectory);
LoadPlayList(const std::string & strFileName)155   virtual void LoadPlayList(const std::string& strFileName) {}
156   virtual bool OnPlayMedia(int iItem, const std::string &player = "");
157   virtual bool OnPlayAndQueueMedia(const CFileItemPtr& item, const std::string& player = "");
158   void UpdateFileList();
159   virtual void OnDeleteItem(int iItem);
160   void OnRenameItem(int iItem);
161   bool WaitForNetwork() const;
162   bool GetDirectoryItems(CURL &url, CFileItemList &items, bool useDir);
163   bool WaitGetDirectoryItems(CGetDirectoryItems &items);
164   void CancelUpdateItems();
165 
166   /*! \brief Translate the folder to start in from the given quick path
167    \param url the folder the user wants
168    \return the resulting path */
169   virtual std::string GetStartFolder(const std::string &url);
170 
171   /*! \brief Utility method to remove the given parameter from a path/URL
172    \param strDirectory Path/URL from which to remove the given parameter
173    \param strParameter Parameter to remove from the given path/URL
174    \return Path/URL without the given parameter
175    */
176   static std::string RemoveParameterFromPath(const std::string &strDirectory, const std::string &strParameter);
177 
178   bool ProcessRenderLoop(bool renderOnly);
179 
180   XFILE::CVirtualDirectory m_rootDir;
181   CGUIViewControl m_viewControl;
182 
183   // current path and history
184   CFileItemList* m_vecItems;
185   CFileItemList* m_unfilteredItems;        ///< \brief items prior to filtering using FilterItems()
186   CDirectoryHistory m_history;
187   std::unique_ptr<CGUIViewState> m_guiState;
188   std::atomic_bool m_vecItemsUpdating = {false};
189   class CUpdateGuard
190   {
191   public:
CUpdateGuard(std::atomic_bool & update)192     CUpdateGuard(std::atomic_bool &update) : m_update(update)
193     {
194       m_update = true;
195     }
~CUpdateGuard()196     ~CUpdateGuard()
197     {
198       m_update = false;
199     }
200   protected:
201     std::atomic_bool &m_update;
202   };
203   CEvent m_updateEvent;
204   std::atomic_bool m_updateAborted = {false};
205   std::atomic_bool m_updateJobActive = {false};
206 
207   // save control state on window exit
208   int m_iLastControl;
209   std::string m_startDirectory;
210 
211   CSmartPlaylist m_filter;
212   bool m_canFilterAdvanced;
213   /*! \brief Contains the path used for filtering (including any active filter)
214 
215    When Update() is called with a path to e.g. a smartplaylist or
216    a library node filter, that "original" path will be stored in
217    m_vecItems->m_strPath. But the path used by XBMC to retrieve
218    those items from the database (Videodb:// or musicdb://)
219    is stored in this member variable to still have access to it
220    because it is used for filtering by appending the currently active
221    filter as a "filter" parameter to the filter path/URL.
222 
223    \sa Update
224    */
225   std::string m_strFilterPath;
226   bool m_backgroundLoad = false;
227 };
228