[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / games / windows / GUIWindowGames.cpp
bloba74186db8423a7b3a0da24b3fe68b89428a5e1d6
1 /*
2 * Copyright (C) 2012-2020 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIWindowGames.h"
11 #include "FileItem.h"
12 #include "GUIPassword.h"
13 #include "ServiceBroker.h"
14 #include "URL.h"
15 #include "Util.h"
16 #include "addons/gui/GUIDialogAddonInfo.h"
17 #include "application/Application.h"
18 #include "dialogs/GUIDialogContextMenu.h"
19 #include "dialogs/GUIDialogMediaSource.h"
20 #include "dialogs/GUIDialogProgress.h"
21 #include "guilib/GUIComponent.h"
22 #include "guilib/GUIWindowManager.h"
23 #include "guilib/WindowIDs.h"
24 #include "input/actions/ActionIDs.h"
25 #include "media/MediaLockState.h"
26 #include "playlists/PlayListTypes.h"
27 #include "settings/MediaSourceSettings.h"
28 #include "settings/Settings.h"
29 #include "settings/SettingsComponent.h"
30 #include "utils/StringUtils.h"
31 #include "utils/URIUtils.h"
33 #include <algorithm>
35 using namespace KODI;
36 using namespace GAME;
38 #define CONTROL_BTNVIEWASICONS 2
39 #define CONTROL_BTNSORTBY 3
40 #define CONTROL_BTNSORTASC 4
42 CGUIWindowGames::CGUIWindowGames() : CGUIMediaWindow(WINDOW_GAMES, "MyGames.xml")
46 bool CGUIWindowGames::OnMessage(CGUIMessage& message)
48 switch (message.GetMessage())
50 case GUI_MSG_WINDOW_INIT:
52 m_rootDir.AllowNonLocalSources(true); //! @todo
54 // Is this the first time the window is opened?
55 if (m_vecItems->GetPath() == "?" && message.GetStringParam().empty())
56 message.SetStringParam(CMediaSourceSettings::GetInstance().GetDefaultSource("games"));
58 //! @todo
59 m_dlgProgress = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(
60 WINDOW_DIALOG_PROGRESS);
62 break;
64 case GUI_MSG_CLICKED:
66 if (OnClickMsg(message.GetSenderId(), message.GetParam1()))
67 return true;
68 break;
70 default:
71 break;
73 return CGUIMediaWindow::OnMessage(message);
76 bool CGUIWindowGames::OnClickMsg(int controlId, int actionId)
78 if (!m_viewControl.HasControl(controlId)) // list/thumb control
79 return false;
81 const int iItem = m_viewControl.GetSelectedItem();
83 CFileItemPtr pItem = m_vecItems->Get(iItem);
84 if (!pItem)
85 return false;
87 switch (actionId)
89 case ACTION_DELETE_ITEM:
91 // Is delete allowed?
92 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
93 CSettings::SETTING_FILELISTS_ALLOWFILEDELETION))
95 OnDeleteItem(iItem);
96 return true;
98 break;
100 case ACTION_PLAYER_PLAY:
102 if (OnClick(iItem))
103 return true;
104 break;
106 case ACTION_SHOW_INFO:
108 if (!m_vecItems->IsPlugin())
110 if (pItem->HasAddonInfo())
112 CGUIDialogAddonInfo::ShowForItem(pItem);
113 return true;
116 break;
118 default:
119 break;
122 return false;
125 void CGUIWindowGames::SetupShares()
127 CGUIMediaWindow::SetupShares();
129 // Don't convert zip files to directories. Otherwise, the files will be
130 // opened and scanned for games with a valid extension. If none are found,
131 // the .zip won't be shown.
133 // This is a problem for MAME roms, because the files inside the .zip don't
134 // have standard extensions.
136 m_rootDir.SetFlags(XFILE::DIR_FLAG_NO_FILE_DIRS);
139 bool CGUIWindowGames::OnClick(int iItem, const std::string& player /* = "" */)
141 CFileItemPtr item = m_vecItems->Get(iItem);
142 if (item)
144 // Compensate for DIR_FLAG_NO_FILE_DIRS flag
145 if (URIUtils::IsArchive(item->GetPath()))
147 bool bIsGame = false;
149 // If zip file contains no games, assume it is a game
150 CFileItemList items;
151 if (m_rootDir.GetDirectory(CURL(item->GetPath()), items))
153 if (items.Size() == 0)
154 bIsGame = true;
157 if (!bIsGame)
158 item->m_bIsFolder = true;
161 if (!item->m_bIsFolder)
163 PlayGame(*item);
164 return true;
168 return CGUIMediaWindow::OnClick(iItem, player);
171 void CGUIWindowGames::GetContextButtons(int itemNumber, CContextButtons& buttons)
173 CFileItemPtr item = m_vecItems->Get(itemNumber);
175 if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
177 if (m_vecItems->IsVirtualDirectoryRoot() || m_vecItems->IsSourcesPath())
179 // Context buttons for a sources path, like "Add Source", "Remove Source", etc.
180 CGUIDialogContextMenu::GetContextButtons("games", item, buttons);
182 else
184 if (item->IsGame())
186 buttons.Add(CONTEXT_BUTTON_PLAY_ITEM, 208); // Play
189 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
190 CSettings::SETTING_FILELISTS_ALLOWFILEDELETION) &&
191 !item->IsReadOnly())
193 buttons.Add(CONTEXT_BUTTON_DELETE, 117);
194 buttons.Add(CONTEXT_BUTTON_RENAME, 118);
199 CGUIMediaWindow::GetContextButtons(itemNumber, buttons);
202 bool CGUIWindowGames::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
204 CFileItemPtr item = m_vecItems->Get(itemNumber);
205 if (item)
207 if (m_vecItems->IsVirtualDirectoryRoot() || m_vecItems->IsSourcesPath())
209 if (CGUIDialogContextMenu::OnContextButton("games", item, button))
211 Update(m_vecItems->GetPath());
212 return true;
215 switch (button)
217 case CONTEXT_BUTTON_PLAY_ITEM:
218 PlayGame(*item);
219 return true;
220 case CONTEXT_BUTTON_INFO:
221 CGUIDialogAddonInfo::ShowForItem(item);
222 return true;
223 case CONTEXT_BUTTON_DELETE:
224 OnDeleteItem(itemNumber);
225 return true;
226 case CONTEXT_BUTTON_RENAME:
227 OnRenameItem(itemNumber);
228 return true;
229 default:
230 break;
233 return CGUIMediaWindow::OnContextButton(itemNumber, button);
236 bool CGUIWindowGames::OnAddMediaSource()
238 return CGUIDialogMediaSource::ShowAndAddMediaSource("games");
241 bool CGUIWindowGames::GetDirectory(const std::string& strDirectory, CFileItemList& items)
243 if (!CGUIMediaWindow::GetDirectory(strDirectory, items))
244 return false;
246 // Set label
247 std::string label;
248 if (items.GetLabel().empty())
250 std::string source;
251 if (m_rootDir.IsSource(items.GetPath(), CMediaSourceSettings::GetInstance().GetSources("games"),
252 &source))
253 label = std::move(source);
256 if (!label.empty())
257 items.SetLabel(label);
259 // Set content
260 std::string content;
261 if (items.GetContent().empty())
263 if (!items.IsVirtualDirectoryRoot() && // Don't set content for root directory
264 !items.IsPlugin()) // Don't set content for plugins
266 content = "games";
270 if (!content.empty())
271 items.SetContent(content);
273 // Ensure a game info tag is created so that files are recognized as games
274 for (const CFileItemPtr& item : items)
276 if (!item->m_bIsFolder)
277 item->GetGameInfoTag();
280 return true;
283 std::string CGUIWindowGames::GetStartFolder(const std::string& dir)
285 // From CGUIWindowPictures::GetStartFolder()
287 if (StringUtils::EqualsNoCase(dir, "plugins") || StringUtils::EqualsNoCase(dir, "addons"))
289 return "addons://sources/game/";
292 SetupShares();
293 VECSOURCES shares;
294 m_rootDir.GetSources(shares);
295 bool bIsSourceName = false;
296 int iIndex = CUtil::GetMatchingSource(dir, shares, bIsSourceName);
297 if (iIndex >= 0)
299 if (iIndex < static_cast<int>(shares.size()) && shares[iIndex].m_iHasLock == LOCK_STATE_LOCKED)
301 CFileItem item(shares[iIndex]);
302 if (!g_passwordManager.IsItemUnlocked(&item, "games"))
303 return "";
305 if (bIsSourceName)
306 return shares[iIndex].strPath;
307 return dir;
309 return CGUIMediaWindow::GetStartFolder(dir);
312 void CGUIWindowGames::OnItemInfo(int itemNumber)
314 CFileItemPtr item = m_vecItems->Get(itemNumber);
315 if (!item)
316 return;
318 if (!m_vecItems->IsPlugin())
320 if (item->IsPlugin() || item->IsScript())
321 CGUIDialogAddonInfo::ShowForItem(item);
324 //! @todo
326 CGUIDialogGameInfo* gameInfo =
327 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogGameInfo>(WINDOW_DIALOG_PICTURE_INFO);
328 if (gameInfo)
330 gameInfo->SetGame(item);
331 gameInfo->Open();
336 bool CGUIWindowGames::PlayGame(const CFileItem& item)
338 CFileItem itemCopy(item);
339 return g_application.PlayMedia(itemCopy, "", PLAYLIST::TYPE_NONE);