Debian Codegraph @

xbmc/games/windows/GUIWindowGames.cpp

raw
1 /*
2 * Copyright (C) 2012-2020 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 #include "GUIWindowGames.h"
10
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 "filesystem/FileDirectoryFactory.h"
22 #include "games/GameUtils.h"
23 #include "guilib/GUIComponent.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "guilib/WindowIDs.h"
26 #include "input/actions/ActionIDs.h"
27 #include "media/MediaLockState.h"
28 #include "playlists/PlayListTypes.h"
29 #include "settings/MediaSourceSettings.h"
30 #include "settings/Settings.h"
31 #include "settings/SettingsComponent.h"
32 #include "utils/StringUtils.h"
33 #include "utils/URIUtils.h"
34
35 #include <algorithm>
36
37 using namespace KODI;
38 using namespace GAME;
39
40 #define CONTROL_BTNVIEWASICONS 2
41 #define CONTROL_BTNSORTBY 3
42 #define CONTROL_BTNSORTASC 4
43
44 CGUIWindowGames::CGUIWindowGames() : CGUIMediaWindow(WINDOW_GAMES, "MyGames.xml")
45 {
46 }
47
48 bool CGUIWindowGames::OnMessage(CGUIMessage& message)
49 {
50 switch (message.GetMessage())
51 {
52 case GUI_MSG_WINDOW_INIT:
53 {
54 m_rootDir.AllowNonLocalSources(true); //! @todo
55
56 // Is this the first time the window is opened?
57 if (m_vecItems->GetPath() == "?" && message.GetStringParam().empty())
58 message.SetStringParam(CMediaSourceSettings::GetInstance().GetDefaultSource("games"));
59
60 //! @todo
61 m_dlgProgress = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(
62 WINDOW_DIALOG_PROGRESS);
63
64 break;
65 }
66 case GUI_MSG_CLICKED:
67 {
68 if (OnClickMsg(message.GetSenderId(), message.GetParam1()))
69 return true;
70 break;
71 }
72 default:
73 break;
74 }
75 return CGUIMediaWindow::OnMessage(message);
76 }
77
78 bool CGUIWindowGames::OnClickMsg(int controlId, int actionId)
79 {
80 if (!m_viewControl.HasControl(controlId)) // list/thumb control
81 return false;
82
83 const int iItem = m_viewControl.GetSelectedItem();
84
85 CFileItemPtr pItem = m_vecItems->Get(iItem);
86 if (!pItem)
87 return false;
88
89 switch (actionId)
90 {
91 case ACTION_DELETE_ITEM:
92 {
93 // Is delete allowed?
94 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
95 CSettings::SETTING_FILELISTS_ALLOWFILEDELETION))
96 {
97 OnDeleteItem(iItem);
98 return true;
99 }
100 break;
101 }
102 case ACTION_PLAYER_PLAY:
103 {
104 if (CanPlay(*pItem))
105 {
106 PlayGame(*pItem);
107 return true;
108 }
109 break;
110 }
111 case ACTION_SHOW_INFO:
112 {
113 if (!m_vecItems->IsPlugin())
114 {
115 if (pItem->HasAddonInfo())
116 {
117 CGUIDialogAddonInfo::ShowForItem(pItem);
118 return true;
119 }
120 }
121 break;
122 }
123 default:
124 break;
125 }
126
127 return false;
128 }
129
130 void CGUIWindowGames::SetupShares()
131 {
132 CGUIMediaWindow::SetupShares();
133
134 // Don't convert zip files to directories. Otherwise, the files will be
135 // opened and scanned for games with a valid extension. If none are found,
136 // the .zip won't be shown.
137 //
138 // This is a problem for MAME roms, because the files inside the .zip don't
139 // have standard extensions.
140 //
141 m_rootDir.SetFlags(XFILE::DIR_FLAG_NO_FILE_DIRS);
142 }
143
144 bool CGUIWindowGames::OnClick(int iItem, const std::string& player /* = "" */)
145 {
146 CFileItemPtr item = m_vecItems->Get(iItem);
147 if (item)
148 {
149 if (!item->m_bIsFolder)
150 {
151 PlayGame(*item);
152 return true;
153 }
154 }
155
156 return CGUIMediaWindow::OnClick(iItem, player);
157 }
158
159 void CGUIWindowGames::GetContextButtons(int itemNumber, CContextButtons& buttons)
160 {
161 CFileItemPtr item = m_vecItems->Get(itemNumber);
162
163 if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
164 {
165 if (m_vecItems->IsVirtualDirectoryRoot() || m_vecItems->IsSourcesPath())
166 {
167 // Context buttons for a sources path, like "Add Source", "Remove Source", etc.
168 CGUIDialogContextMenu::GetContextButtons("games", item, buttons);
169 }
170 else
171 {
172 if (CanPlay(*item))
173 {
174 buttons.Add(CONTEXT_BUTTON_PLAY_ITEM, 208); // Play
175 }
176
177 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
178 CSettings::SETTING_FILELISTS_ALLOWFILEDELETION) &&
179 !item->IsReadOnly())
180 {
181 buttons.Add(CONTEXT_BUTTON_DELETE, 117);
182 buttons.Add(CONTEXT_BUTTON_RENAME, 118);
183 }
184 }
185 }
186
187 CGUIMediaWindow::GetContextButtons(itemNumber, buttons);
188 }
189
190 bool CGUIWindowGames::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
191 {
192 CFileItemPtr item = m_vecItems->Get(itemNumber);
193 if (item)
194 {
195 if (m_vecItems->IsVirtualDirectoryRoot() || m_vecItems->IsSourcesPath())
196 {
197 if (CGUIDialogContextMenu::OnContextButton("games", item, button))
198 {
199 Update(m_vecItems->GetPath());
200 return true;
201 }
202 }
203 switch (button)
204 {
205 case CONTEXT_BUTTON_PLAY_ITEM:
206 PlayGame(*item);
207 return true;
208 case CONTEXT_BUTTON_INFO:
209 CGUIDialogAddonInfo::ShowForItem(item);
210 return true;
211 case CONTEXT_BUTTON_DELETE:
212 OnDeleteItem(itemNumber);
213 return true;
214 case CONTEXT_BUTTON_RENAME:
215 OnRenameItem(itemNumber);
216 return true;
217 default:
218 break;
219 }
220 }
221 return CGUIMediaWindow::OnContextButton(itemNumber, button);
222 }
223
224 bool CGUIWindowGames::OnAddMediaSource()
225 {
226 return CGUIDialogMediaSource::ShowAndAddMediaSource("games");
227 }
228
229 bool CGUIWindowGames::GetDirectory(const std::string& strDirectory, CFileItemList& items)
230 {
231 if (!CGUIMediaWindow::GetDirectory(strDirectory, items))
232 return false;
233
234 // Now we must account for file folders not handled by DIR_FLAG_NO_FILE_DIRS
235 for (int i = 0; i < items.Size(); ++i)
236 {
237 CFileItemPtr item = items[i];
238 if (item->m_bIsFolder || !item->IsFileFolder(EFILEFOLDER_TYPE_ALWAYS))
239 continue;
240
241 const std::string originalPath = item->GetPath();
242
243 // This will turn the item into a file folder as a side effect
244 std::unique_ptr<XFILE::IFileDirectory> pDirectory{
245 XFILE::CFileDirectoryFactory::Create(CURL{originalPath}, item.get())};
246 if (pDirectory)
247 {
248 // Check for empty file folders
249 CFileItemList fileFolderItems;
250 if (!pDirectory->GetDirectory(item->GetURL(), fileFolderItems) || fileFolderItems.IsEmpty())
251 {
252 items.Remove(i);
253 --i;
254 continue;
255 }
256
257 // Check if file folder contains games or subfolders
258 if (std::any_of(fileFolderItems.begin(), fileFolderItems.end(),
259 [](const CFileItemPtr& fileFolderItem) {
260 return fileFolderItem->m_bIsFolder ||
261 CGameUtils::HasGameExtension(fileFolderItem->GetPath());
262 }))
263 {
264 continue;
265 }
266
267 // If the file folder contains no games, turn it back into a regular file
268 item->m_bIsFolder = false;
269 item->SetPath(originalPath);
270 }
271 else
272 {
273 // File folder contains a single file and was collapsed down. Remove it
274 // if not a game.
275 if (!CGameUtils::HasGameExtension(item->GetPath()))
276 {
277 items.Remove(i);
278 --i;
279 }
280 }
281 }
282
283 // Set label
284 std::string label;
285 if (items.GetLabel().empty())
286 {
287 std::string source;
288 if (m_rootDir.IsSource(items.GetPath(), CMediaSourceSettings::GetInstance().GetSources("games"),
289 &source))
290 label = std::move(source);
291 }
292
293 if (!label.empty())
294 items.SetLabel(label);
295
296 // Set content
297 std::string content;
298 if (items.GetContent().empty())
299 {
300 if (!items.IsVirtualDirectoryRoot() && // Don't set content for root directory
301 !items.IsPlugin()) // Don't set content for plugins
302 {
303 content = "games";
304 }
305 }
306
307 if (!content.empty())
308 items.SetContent(content);
309
310 // Ensure a game info tag is created so that files are recognized as games
311 for (const CFileItemPtr& item : items)
312 {
313 if (!item->m_bIsFolder)
314 item->GetGameInfoTag();
315 }
316
317 return true;
318 }
319
320 std::string CGUIWindowGames::GetStartFolder(const std::string& dir)
321 {
322 // From CGUIWindowPictures::GetStartFolder()
323
324 if (StringUtils::EqualsNoCase(dir, "plugins") || StringUtils::EqualsNoCase(dir, "addons"))
325 {
326 return "addons://sources/game/";
327 }
328
329 SetupShares();
330 VECSOURCES shares;
331 m_rootDir.GetSources(shares);
332 bool bIsSourceName = false;
333 int iIndex = CUtil::GetMatchingSource(dir, shares, bIsSourceName);
334 if (iIndex >= 0)
335 {
336 if (iIndex < static_cast<int>(shares.size()) && shares[iIndex].m_iHasLock == LOCK_STATE_LOCKED)
337 {
338 CFileItem item(shares[iIndex]);
339 if (!g_passwordManager.IsItemUnlocked(&item, "games"))
340 return "";
341 }
342 if (bIsSourceName)
343 return shares[iIndex].strPath;
344 return dir;
345 }
346 return CGUIMediaWindow::GetStartFolder(dir);
347 }
348
349 void CGUIWindowGames::OnItemInfo(int itemNumber)
350 {
351 CFileItemPtr item = m_vecItems->Get(itemNumber);
352 if (!item)
353 return;
354
355 if (!m_vecItems->IsPlugin())
356 {
357 if (item->IsPlugin() || item->IsScript())
358 CGUIDialogAddonInfo::ShowForItem(item);
359 }
360
361 //! @todo
362 /*
363 CGUIDialogGameInfo* gameInfo =
364 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogGameInfo>(WINDOW_DIALOG_PICTURE_INFO);
365 if (gameInfo)
366 {
367 gameInfo->SetGame(item);
368 gameInfo->Open();
369 }
370 */
371 }
372
373 bool CGUIWindowGames::PlayGame(const CFileItem& item)
374 {
375 CFileItem itemCopy(item);
376
377 // Dereference file folders. The check here assumes all "is folder" items
378 // passed CanPlay(), e.g. are definitely file folders.
379 if (itemCopy.m_bIsFolder)
380 {
381 itemCopy.m_bIsFolder = false;
382 itemCopy.SetPath(itemCopy.GetURL().GetHostName());
383 itemCopy.GetGameInfoTag();
384 }
385
386 return g_application.PlayMedia(itemCopy, "", PLAYLIST::TYPE_NONE);
387 }
388
389 bool CGUIWindowGames::CanPlay(const CFileItem& item) const
390 {
391 if (item.IsGame())
392 return true;
393
394 if (item.m_bIsFolder)
395 {
396 // Check for file folders
397 CURL url{item.GetPath()};
398 if (url.GetFileName().empty() && URIUtils::IsZIP(url.GetHostName()))
399 return true;
400 }
401
402 return false;
403 }