| 1 |
/* |
| 2 |
* Copyright (C) 2020-2021 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 "DialogGameSaves.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "addons/Addon.h" |
| 14 |
#include "addons/AddonManager.h" |
| 15 |
#include "cores/RetroPlayer/savestates/ISavestate.h" |
| 16 |
#include "cores/RetroPlayer/savestates/SavestateDatabase.h" |
| 17 |
#include "dialogs/GUIDialogContextMenu.h" |
| 18 |
#include "dialogs/GUIDialogOK.h" |
| 19 |
#include "dialogs/GUIDialogYesNo.h" |
| 20 |
#include "games/addons/GameClient.h" |
| 21 |
#include "games/dialogs/DialogGameDefines.h" |
| 22 |
#include "guilib/GUIBaseContainer.h" |
| 23 |
#include "guilib/GUIComponent.h" |
| 24 |
#include "guilib/GUIKeyboardFactory.h" |
| 25 |
#include "guilib/GUIMessage.h" |
| 26 |
#include "guilib/GUIWindowManager.h" |
| 27 |
#include "guilib/LocalizeStrings.h" |
| 28 |
#include "guilib/WindowIDs.h" |
| 29 |
#include "input/actions/ActionIDs.h" |
| 30 |
#include "utils/FileUtils.h" |
| 31 |
#include "utils/Variant.h" |
| 32 |
#include "view/GUIViewControl.h" |
| 33 |
#include "view/ViewState.h" |
| 34 |
|
| 35 |
using namespace KODI; |
| 36 |
using namespace GAME; |
| 37 |
|
| 38 |
CDialogGameSaves::CDialogGameSaves() |
| 39 |
: CGUIDialog(WINDOW_DIALOG_GAME_SAVES, "DialogSelect.xml"), |
| 40 |
m_viewControl(std::make_unique<CGUIViewControl>()), |
| 41 |
m_vecList(std::make_unique<CFileItemList>()) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
bool CDialogGameSaves::OnMessage(CGUIMessage& message) |
| 46 |
{ |
| 47 |
switch (message.GetMessage()) |
| 48 |
{ |
| 49 |
case GUI_MSG_CLICKED: |
| 50 |
{ |
| 51 |
const int actionId = message.GetParam1(); |
| 52 |
|
| 53 |
switch (actionId) |
| 54 |
{ |
| 55 |
case ACTION_SELECT_ITEM: |
| 56 |
case ACTION_MOUSE_LEFT_CLICK: |
| 57 |
{ |
| 58 |
int selectedId = m_viewControl->GetSelectedItem(); |
| 59 |
if (0 <= selectedId && selectedId < m_vecList->Size()) |
| 60 |
{ |
| 61 |
CFileItemPtr item = m_vecList->Get(selectedId); |
| 62 |
if (item) |
| 63 |
{ |
| 64 |
for (int i = 0; i < m_vecList->Size(); i++) |
| 65 |
m_vecList->Get(i)->Select(false); |
| 66 |
|
| 67 |
item->Select(true); |
| 68 |
|
| 69 |
OnSelect(*item); |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
break; |
| 75 |
} |
| 76 |
case ACTION_CONTEXT_MENU: |
| 77 |
case ACTION_MOUSE_RIGHT_CLICK: |
| 78 |
{ |
| 79 |
int selectedItem = m_viewControl->GetSelectedItem(); |
| 80 |
if (selectedItem >= 0 && selectedItem < m_vecList->Size()) |
| 81 |
{ |
| 82 |
CFileItemPtr item = m_vecList->Get(selectedItem); |
| 83 |
if (item) |
| 84 |
{ |
| 85 |
OnContextMenu(*item); |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
break; |
| 90 |
} |
| 91 |
case ACTION_RENAME_ITEM: |
| 92 |
{ |
| 93 |
const int controlId = message.GetSenderId(); |
| 94 |
if (m_viewControl->HasControl(controlId)) |
| 95 |
{ |
| 96 |
int selectedItem = m_viewControl->GetSelectedItem(); |
| 97 |
if (selectedItem >= 0 && selectedItem < m_vecList->Size()) |
| 98 |
{ |
| 99 |
CFileItemPtr item = m_vecList->Get(selectedItem); |
| 100 |
if (item) |
| 101 |
{ |
| 102 |
OnRename(*item); |
| 103 |
return true; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
break; |
| 108 |
} |
| 109 |
case ACTION_DELETE_ITEM: |
| 110 |
{ |
| 111 |
const int controlId = message.GetSenderId(); |
| 112 |
if (m_viewControl->HasControl(controlId)) |
| 113 |
{ |
| 114 |
int selectedItem = m_viewControl->GetSelectedItem(); |
| 115 |
if (selectedItem >= 0 && selectedItem < m_vecList->Size()) |
| 116 |
{ |
| 117 |
CFileItemPtr item = m_vecList->Get(selectedItem); |
| 118 |
if (item) |
| 119 |
{ |
| 120 |
OnDelete(*item); |
| 121 |
return true; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
default: |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
const int controlId = message.GetSenderId(); |
| 132 |
switch (controlId) |
| 133 |
{ |
| 134 |
case CONTROL_SAVES_NEW_BUTTON: |
| 135 |
{ |
| 136 |
m_bNewPressed = true; |
| 137 |
Close(); |
| 138 |
break; |
| 139 |
} |
| 140 |
case CONTROL_SAVES_CANCEL_BUTTON: |
| 141 |
{ |
| 142 |
m_selectedItem.reset(); |
| 143 |
m_vecList->Clear(); |
| 144 |
m_bConfirmed = false; |
| 145 |
Close(); |
| 146 |
break; |
| 147 |
} |
| 148 |
default: |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
case GUI_MSG_SETFOCUS: |
| 156 |
{ |
| 157 |
const int controlId = message.GetControlId(); |
| 158 |
if (m_viewControl->HasControl(controlId)) |
| 159 |
{ |
| 160 |
if (m_vecList->IsEmpty()) |
| 161 |
{ |
| 162 |
SET_CONTROL_FOCUS(CONTROL_SAVES_NEW_BUTTON, 0); |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
if (m_viewControl->GetCurrentControl() != controlId) |
| 167 |
{ |
| 168 |
m_viewControl->SetFocused(); |
| 169 |
return true; |
| 170 |
} |
| 171 |
} |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
default: |
| 176 |
break; |
| 177 |
} |
| 178 |
|
| 179 |
return CGUIDialog::OnMessage(message); |
| 180 |
} |
| 181 |
|
| 182 |
void CDialogGameSaves::FrameMove() |
| 183 |
{ |
| 184 |
CGUIControl* itemContainer = GetControl(CONTROL_SAVES_DETAILED_LIST); |
| 185 |
if (itemContainer != nullptr) |
| 186 |
{ |
| 187 |
if (itemContainer->HasFocus()) |
| 188 |
{ |
| 189 |
int selectedItem = m_viewControl->GetSelectedItem(); |
| 190 |
if (selectedItem >= 0 && selectedItem < m_vecList->Size()) |
| 191 |
{ |
| 192 |
CFileItemPtr item = m_vecList->Get(selectedItem); |
| 193 |
if (item) |
| 194 |
OnFocus(*item); |
| 195 |
} |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
OnFocusLost(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
CGUIDialog::FrameMove(); |
| 204 |
} |
| 205 |
|
| 206 |
void CDialogGameSaves::OnInitWindow() |
| 207 |
{ |
| 208 |
m_viewControl->SetItems(*m_vecList); |
| 209 |
m_viewControl->SetCurrentView(CONTROL_SAVES_DETAILED_LIST); |
| 210 |
|
| 211 |
CGUIDialog::OnInitWindow(); |
| 212 |
|
| 213 |
// Select the first item |
| 214 |
m_viewControl->SetSelectedItem(0); |
| 215 |
|
| 216 |
// There's a race condition where the item's focus sends the update message |
| 217 |
// before the window is fully initialized, so explicitly set the info now. |
| 218 |
if (!m_vecList->IsEmpty()) |
| 219 |
{ |
| 220 |
CFileItemPtr item = m_vecList->Get(0); |
| 221 |
if (item) |
| 222 |
{ |
| 223 |
const std::string gameClientId = item->GetProperty(SAVESTATE_GAME_CLIENT).asString(); |
| 224 |
if (!gameClientId.empty()) |
| 225 |
{ |
| 226 |
std::string emulatorName; |
| 227 |
std::string emulatorIcon; |
| 228 |
|
| 229 |
using namespace ADDON; |
| 230 |
|
| 231 |
AddonPtr addon; |
| 232 |
CAddonMgr& addonManager = CServiceBroker::GetAddonMgr(); |
| 233 |
if (addonManager.GetAddon(m_currentGameClient, addon, OnlyEnabled::CHOICE_NO)) |
| 234 |
{ |
| 235 |
std::shared_ptr<CGameClient> gameClient = std::dynamic_pointer_cast<CGameClient>(addon); |
| 236 |
if (gameClient) |
| 237 |
{ |
| 238 |
m_currentGameClient = gameClient->ID(); |
| 239 |
|
| 240 |
emulatorName = gameClient->GetEmulatorName(); |
| 241 |
emulatorIcon = gameClient->Icon(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (!emulatorName.empty()) |
| 246 |
{ |
| 247 |
CGUIMessage message(GUI_MSG_LABEL_SET, GetID(), CONTROL_SAVES_EMULATOR_NAME); |
| 248 |
message.SetLabel(emulatorName); |
| 249 |
OnMessage(message); |
| 250 |
} |
| 251 |
if (!emulatorIcon.empty()) |
| 252 |
{ |
| 253 |
CGUIMessage message(GUI_MSG_SET_FILENAME, GetID(), CONTROL_SAVES_EMULATOR_ICON); |
| 254 |
message.SetLabel(emulatorIcon); |
| 255 |
OnMessage(message); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
const std::string caption = item->GetProperty(SAVESTATE_CAPTION).asString(); |
| 260 |
if (!caption.empty()) |
| 261 |
{ |
| 262 |
m_currentCaption = caption; |
| 263 |
|
| 264 |
CGUIMessage message(GUI_MSG_LABEL_SET, GetID(), CONTROL_SAVES_DESCRIPTION); |
| 265 |
message.SetLabel(m_currentCaption); |
| 266 |
OnMessage(message); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void CDialogGameSaves::OnDeinitWindow(int nextWindowID) |
| 273 |
{ |
| 274 |
m_viewControl->Clear(); |
| 275 |
|
| 276 |
CGUIDialog::OnDeinitWindow(nextWindowID); |
| 277 |
|
| 278 |
// Get selected item |
| 279 |
for (int i = 0; i < m_vecList->Size(); ++i) |
| 280 |
{ |
| 281 |
CFileItemPtr item = m_vecList->Get(i); |
| 282 |
if (item->IsSelected()) |
| 283 |
{ |
| 284 |
m_selectedItem = item; |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
m_vecList->Clear(); |
| 290 |
} |
| 291 |
|
| 292 |
void CDialogGameSaves::OnWindowLoaded() |
| 293 |
{ |
| 294 |
CGUIDialog::OnWindowLoaded(); |
| 295 |
|
| 296 |
m_viewControl->Reset(); |
| 297 |
m_viewControl->SetParentWindow(GetID()); |
| 298 |
m_viewControl->AddView(GetControl(CONTROL_SAVES_DETAILED_LIST)); |
| 299 |
} |
| 300 |
|
| 301 |
void CDialogGameSaves::OnWindowUnload() |
| 302 |
{ |
| 303 |
CGUIDialog::OnWindowUnload(); |
| 304 |
m_viewControl->Reset(); |
| 305 |
} |
| 306 |
|
| 307 |
void CDialogGameSaves::Reset() |
| 308 |
{ |
| 309 |
m_bConfirmed = false; |
| 310 |
m_bNewPressed = false; |
| 311 |
|
| 312 |
m_vecList->Clear(); |
| 313 |
m_selectedItem.reset(); |
| 314 |
} |
| 315 |
|
| 316 |
bool CDialogGameSaves::Open(const std::string& gamePath) |
| 317 |
{ |
| 318 |
CFileItemList items; |
| 319 |
|
| 320 |
RETRO::CSavestateDatabase db; |
| 321 |
if (!db.GetSavestatesNav(items, gamePath)) |
| 322 |
return false; |
| 323 |
|
| 324 |
if (items.IsEmpty()) |
| 325 |
return false; |
| 326 |
|
| 327 |
items.Sort(SortByDate, SortOrderDescending); |
| 328 |
|
| 329 |
SetItems(items); |
| 330 |
|
| 331 |
CGUIDialog::Open(); |
| 332 |
|
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
std::string CDialogGameSaves::GetSelectedItemPath() |
| 337 |
{ |
| 338 |
if (m_selectedItem) |
| 339 |
return m_selectedItem->GetPath(); |
| 340 |
|
| 341 |
return ""; |
| 342 |
} |
| 343 |
|
| 344 |
void CDialogGameSaves::SetItems(const CFileItemList& itemList) |
| 345 |
{ |
| 346 |
m_vecList->Clear(); |
| 347 |
|
| 348 |
// Need to make internal copy of list to be sure dialog is owner of it |
| 349 |
m_vecList->Copy(itemList); |
| 350 |
|
| 351 |
m_viewControl->SetItems(*m_vecList); |
| 352 |
} |
| 353 |
|
| 354 |
void CDialogGameSaves::OnSelect(const CFileItem& item) |
| 355 |
{ |
| 356 |
m_bConfirmed = true; |
| 357 |
Close(); |
| 358 |
} |
| 359 |
|
| 360 |
void CDialogGameSaves::OnFocus(const CFileItem& item) |
| 361 |
{ |
| 362 |
const std::string caption = item.GetProperty(SAVESTATE_CAPTION).asString(); |
| 363 |
const std::string gameClientId = item.GetProperty(SAVESTATE_GAME_CLIENT).asString(); |
| 364 |
|
| 365 |
HandleCaption(caption); |
| 366 |
HandleGameClient(gameClientId); |
| 367 |
} |
| 368 |
|
| 369 |
void CDialogGameSaves::OnFocusLost() |
| 370 |
{ |
| 371 |
HandleCaption(""); |
| 372 |
HandleGameClient(""); |
| 373 |
} |
| 374 |
|
| 375 |
void CDialogGameSaves::OnContextMenu(CFileItem& item) |
| 376 |
{ |
| 377 |
CContextButtons buttons; |
| 378 |
|
| 379 |
buttons.Add(0, 118); // "Rename" |
| 380 |
buttons.Add(1, 117); // "Delete" |
| 381 |
|
| 382 |
const int index = CGUIDialogContextMenu::Show(buttons); |
| 383 |
|
| 384 |
if (index == 0) |
| 385 |
OnRename(item); |
| 386 |
else if (index == 1) |
| 387 |
OnDelete(item); |
| 388 |
} |
| 389 |
|
| 390 |
void CDialogGameSaves::OnRename(CFileItem& item) |
| 391 |
{ |
| 392 |
const std::string& savestatePath = item.GetPath(); |
| 393 |
|
| 394 |
// Get savestate properties |
| 395 |
RETRO::CSavestateDatabase db; |
| 396 |
std::unique_ptr<RETRO::ISavestate> savestate = RETRO::CSavestateDatabase::AllocateSavestate(); |
| 397 |
db.GetSavestate(savestatePath, *savestate); |
| 398 |
|
| 399 |
std::string label(savestate->Label()); |
| 400 |
|
| 401 |
// "Enter new filename" |
| 402 |
if (CGUIKeyboardFactory::ShowAndGetInput(label, CVariant{g_localizeStrings.Get(16013)}, true) && |
| 403 |
label != savestate->Label()) |
| 404 |
{ |
| 405 |
std::unique_ptr<RETRO::ISavestate> newSavestate = db.RenameSavestate(savestatePath, label); |
| 406 |
if (newSavestate) |
| 407 |
{ |
| 408 |
RETRO::CSavestateDatabase::GetSavestateItem(*newSavestate, savestatePath, item); |
| 409 |
|
| 410 |
// Refresh thumbnails |
| 411 |
m_viewControl->SetItems(*m_vecList); |
| 412 |
} |
| 413 |
else |
| 414 |
{ |
| 415 |
// "Error" |
| 416 |
// "An unknown error has occurred." |
| 417 |
CGUIDialogOK::ShowAndGetInput(257, 24071); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
void CDialogGameSaves::OnDelete(CFileItem& item) |
| 423 |
{ |
| 424 |
// "Confirm delete" |
| 425 |
// "Would you like to delete the selected file(s)?[CR]Warning - this action can't be undone!" |
| 426 |
if (CGUIDialogYesNo::ShowAndGetInput(CVariant{122}, CVariant{125})) |
| 427 |
{ |
| 428 |
const std::string& savestatePath = item.GetPath(); |
| 429 |
|
| 430 |
RETRO::CSavestateDatabase db; |
| 431 |
if (db.DeleteSavestate(savestatePath)) |
| 432 |
{ |
| 433 |
m_vecList->Remove(&item); |
| 434 |
|
| 435 |
// Refresh thumbnails |
| 436 |
m_viewControl->SetItems(*m_vecList); |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
// "Error" |
| 441 |
// "An unknown error has occurred." |
| 442 |
CGUIDialogOK::ShowAndGetInput(257, 24071); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
void CDialogGameSaves::HandleCaption(const std::string& caption) |
| 448 |
{ |
| 449 |
if (caption != m_currentCaption) |
| 450 |
{ |
| 451 |
m_currentCaption = caption; |
| 452 |
|
| 453 |
// Update the GUI label |
| 454 |
CGUIMessage msg(GUI_MSG_LABEL_SET, GetID(), CONTROL_SAVES_DESCRIPTION); |
| 455 |
msg.SetLabel(m_currentCaption); |
| 456 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg, GetID()); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
void CDialogGameSaves::HandleGameClient(const std::string& gameClientId) |
| 461 |
{ |
| 462 |
if (gameClientId == m_currentGameClient) |
| 463 |
return; |
| 464 |
|
| 465 |
m_currentGameClient = gameClientId; |
| 466 |
if (m_currentGameClient.empty()) |
| 467 |
return; |
| 468 |
|
| 469 |
// Get game client properties |
| 470 |
std::shared_ptr<CGameClient> gameClient; |
| 471 |
std::string emulatorName; |
| 472 |
std::string iconPath; |
| 473 |
|
| 474 |
using namespace ADDON; |
| 475 |
|
| 476 |
AddonPtr addon; |
| 477 |
CAddonMgr& addonManager = CServiceBroker::GetAddonMgr(); |
| 478 |
if (addonManager.GetAddon(m_currentGameClient, addon, OnlyEnabled::CHOICE_NO)) |
| 479 |
gameClient = std::dynamic_pointer_cast<CGameClient>(addon); |
| 480 |
|
| 481 |
if (gameClient) |
| 482 |
{ |
| 483 |
emulatorName = gameClient->GetEmulatorName(); |
| 484 |
iconPath = gameClient->Icon(); |
| 485 |
} |
| 486 |
|
| 487 |
// Update the GUI elements |
| 488 |
if (!emulatorName.empty()) |
| 489 |
{ |
| 490 |
CGUIMessage message(GUI_MSG_LABEL_SET, GetID(), CONTROL_SAVES_EMULATOR_NAME); |
| 491 |
message.SetLabel(emulatorName); |
| 492 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(message, GetID()); |
| 493 |
} |
| 494 |
if (!iconPath.empty()) |
| 495 |
{ |
| 496 |
CGUIMessage message(GUI_MSG_SET_FILENAME, GetID(), CONTROL_SAVES_EMULATOR_ICON); |
| 497 |
message.SetLabel(iconPath); |
| 498 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(message, GetID()); |
| 499 |
} |
| 500 |
} |