MythTV  master
gamehandler.cpp
Go to the documentation of this file.
1 // C++
2 #include <utility>
3 
4 // Qt
5 #include <QDir>
6 #include <QList>
7 #include <QRegularExpression>
8 
9 // MythTV
10 #include <libmyth/mythcontext.h>
11 #include <libmythbase/mythdb.h>
12 #include <libmythbase/mythdbcon.h>
17 #include <libmythui/mythuihelper.h>
18 
19 // MythGame
20 #include "gamehandler.h"
21 #include "rominfo.h"
22 #include "rom_metadata.h"
23 
24 #define LOC_ERR QString("MythGame:GAMEHANDLER Error: ")
25 #define LOC QString("MythGame:GAMEHANDLER: ")
26 
27 static QList<GameHandler*> *handlers = nullptr;
28 
29 static void checkHandlers(void)
30 {
31  // If a handlers list doesn't currently exist create one. Otherwise
32  // clear the existing list so that we can regenerate a new one.
33  if (!handlers)
34  handlers = new QList<GameHandler*>;
35  else
36  {
37  while (!handlers->isEmpty())
38  delete handlers->takeFirst();
39  handlers->clear();
40  }
41 
43  if (!query.exec("SELECT DISTINCT playername FROM gameplayers "
44  "WHERE playername <> '';"))
45  MythDB::DBError("checkHandlers - selecting playername", query);
46 
47  while (query.next())
48  {
49  QString name = query.value(0).toString();
51  }
52 }
53 
55 {
56  return handlers->at(i);
57 }
58 
60 {
62 
63  query.prepare("SELECT rompath, workingpath, commandline, screenshots, "
64  "gameplayerid, gametype, extensions, spandisks "
65  "FROM gameplayers WHERE playername = :SYSTEM ");
66 
67  query.bindValue(":SYSTEM", handler->SystemName());
68 
69  if (query.exec() && query.next())
70  {
71  handler->m_rompath = query.value(0).toString();
72  handler->m_workingpath = query.value(1).toString();
73  handler->m_commandline = query.value(2).toString();
74  handler->m_screenshots = query.value(3).toString();
75  handler->m_gameplayerid = query.value(4).toInt();
76  handler->m_gametype = query.value(5).toString();
77 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
78  handler->m_validextensions = query.value(6).toString().trimmed()
79  .remove(" ").split(",", QString::SkipEmptyParts);
80 #else
81  handler->m_validextensions = query.value(6).toString().trimmed()
82  .remove(" ").split(",", Qt::SkipEmptyParts);
83 #endif
84  handler->m_spandisks = query.value(7).toBool();
85  }
86 }
87 
89 
91 {
92  s_newInstance = new GameHandler();
93  s_newInstance->m_systemname = std::move(name);
94 
96 
97  return s_newInstance;
98 }
99 
100 // Creates/rebuilds the handler list and then returns the count.
102 {
103  checkHandlers();
104  return handlers->count();
105 }
106 
108 {
109  QString key;
110 
111  MSqlQuery query(MSqlQuery::InitCon());
112  query.prepare("SELECT crc, category, year, country, name, "
113  "description, publisher, platform, version, "
114  "binfile FROM romdb WHERE platform = :GAMETYPE;");
115 
116  query.bindValue(":GAMETYPE",GameType);
117 
118  if (query.exec())
119  {
120  while (query.next())
121  {
122  key = QString("%1:%2")
123  .arg(query.value(0).toString(),
124  query.value(9).toString());
125  m_romDB[key] = RomData(
126  query.value(1).toString(),
127  query.value(2).toString(),
128  query.value(3).toString(),
129  query.value(4).toString(),
130  query.value(5).toString(),
131  query.value(6).toString(),
132  query.value(7).toString(),
133  query.value(8).toString());
134  }
135  }
136 
137  if (m_romDB.count() == 0)
138  {
139  LOG(VB_GENERAL, LOG_ERR, LOC + QString("No romDB data read from "
140  "database for gametype %1 . Not imported?").arg(GameType));
141  }
142  else
143  {
144  LOG(VB_GENERAL, LOG_INFO, LOC +
145  QString("Loaded %1 items from romDB Database") .arg(m_romDB.count()));
146  }
147 }
148 
149 void GameHandler::GetMetadata(GameHandler *handler, const QString& rom, QString* Genre, QString* Year,
150  QString* Country, QString* CRC32, QString* GameName,
151  QString *Plot, QString *Publisher, QString *Version,
152  QString* Fanart, QString* Boxart)
153 {
154  QString key;
155 
156  *CRC32 = crcinfo(rom, handler->GameType(), &key, &m_romDB);
157 
158 #if 0
159  LOG(VB_GENERAL, LOG_DEBUG, "Key = " + key);
160 #endif
161 
162  // Set our default values
163  *Year = tr("19xx", "Default game year");
164  *Country = tr("Unknown", "Unknown country");
165  *GameName = tr("Unknown", "Unknown game name");
166  *Genre = tr("Unknown", "Unknown genre");
167  *Plot = tr("Unknown", "Unknown plot");
168  *Publisher = tr("Unknown", "Unknown publisher");
169  *Version = tr("0", "Default game version");
170  (*Fanart).clear();
171  (*Boxart).clear();
172 
173  if (!(*CRC32).isEmpty())
174  {
175  if (m_romDB.contains(key))
176  {
177  LOG(VB_GENERAL, LOG_INFO, LOC + QString("ROMDB FOUND for %1 - %2")
178  .arg(m_romDB[key].GameName(), key));
179  *Year = m_romDB[key].Year();
180  *Country = m_romDB[key].Country();
181  *Genre = m_romDB[key].Genre();
182  *Publisher = m_romDB[key].Publisher();
183  *GameName = m_romDB[key].GameName();
184  *Version = m_romDB[key].Version();
185  }
186  else
187  {
188  LOG(VB_GENERAL, LOG_ERR, LOC + QString("NO ROMDB FOUND for %1 (%2)")
189  .arg(rom, *CRC32));
190  }
191 
192  };
193 
194  if ((*Genre == tr("Unknown", "Unknown genre")) || (*Genre).isEmpty())
195  *Genre = tr("Unknown %1", "Unknown genre")
196  .arg( handler->GameType() );
197 
198 }
199 
200 static void purgeGameDB(const QString& filename, const QString& RomPath)
201 {
202  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Purging %1 - %2")
203  .arg(RomPath, filename));
204 
205  MSqlQuery query(MSqlQuery::InitCon());
206 
207  // This should have the added benefit of removing the rom from
208  // other games of the same gametype so we wont be asked to remove it
209  // more than once.
210  query.prepare("DELETE FROM gamemetadata WHERE "
211  "romname = :ROMNAME AND "
212  "rompath = :ROMPATH ");
213 
214  query.bindValue(":ROMNAME",filename);
215  query.bindValue(":ROMPATH",RomPath);
216 
217  if (!query.exec())
218  MythDB::DBError("purgeGameDB", query);
219 
220 }
221 
223 {
224  QString filename = scan.Rom();
225  QString RomPath = scan.RomFullPath();
226 
227  if (m_removeAll)
229 
230  if (m_keepAll || m_removeAll)
231  return;
232 
233  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
234  auto *removalPopup = new MythDialogBox(
235  //: %1 is the file name
236  tr("%1 appears to be missing.\n"
237  "Remove it from the database?")
238  .arg(filename), popupStack, "chooseSystemPopup");
239 
240  if (removalPopup->Create())
241  {
242  removalPopup->SetReturnEvent(this, "removalPopup");
243 
244  removalPopup->AddButton(tr("No"));
245  removalPopup->AddButton(tr("No to all"));
246  removalPopup->AddButtonV(tr("Yes"), QVariant::fromValue(scan));
247  removalPopup->AddButtonV(tr("Yes to all"), QVariant::fromValue(scan));
248  popupStack->AddScreen(removalPopup);
249 }
250  else
251  delete removalPopup;
252 }
253 
254 static void updateDisplayRom(const QString& romname, int display, const QString& Systemname)
255 {
256  MSqlQuery query(MSqlQuery::InitCon());
257  query.prepare("UPDATE gamemetadata SET display = :DISPLAY "
258  "WHERE romname = :ROMNAME AND `system` = :SYSTEM");
259 
260  query.bindValue(":DISPLAY", display);
261  query.bindValue(":ROMNAME", romname);
262  query.bindValue(":SYSTEM", Systemname);
263 
264  if (!query.exec())
265  MythDB::DBError("updateDisplayRom", query);
266 
267 }
268 
269 static void updateDiskCount(const QString& romname, int diskcount, const QString& GameType)
270 {
271  MSqlQuery query(MSqlQuery::InitCon());
272  query.prepare("UPDATE gamemetadata SET diskcount = :DISKCOUNT "
273  "WHERE romname = :ROMNAME AND gametype = :GAMETYPE ");
274 
275  query.bindValue(":DISKCOUNT",diskcount);
276  query.bindValue(":ROMNAME", romname);
277  query.bindValue(":GAMETYPE",GameType);
278 
279  if (!query.exec())
280  MythDB::DBError("updateDiskCount", query);
281 
282 }
283 
284 static void updateGameName(const QString& romname, const QString& GameName, const QString& Systemname)
285 {
286  MSqlQuery query(MSqlQuery::InitCon());
287  query.prepare("UPDATE gamemetadata SET GameName = :GAMENAME "
288  "WHERE romname = :ROMNAME AND `system` = :SYSTEM ");
289 
290  query.bindValue(":GAMENAME", GameName);
291  query.bindValue(":ROMNAME", romname);
292  query.bindValue(":SYSTEM", Systemname);
293 
294  if (!query.exec())
295  MythDB::DBError("updateGameName", query);
296 
297 }
298 
299 
300 static void UpdateGameCounts(const QStringList& updatelist)
301 {
302  MSqlQuery query(MSqlQuery::InitCon());
303 
304  static const QRegularExpression multiDiskRGXP { "[0-4]$" };
305 
306  QString lastrom;
307  QString firstname;
308  QString basename;
309 
310  for (const auto & GameType : qAsConst(updatelist))
311  {
312  LOG(VB_GENERAL, LOG_NOTICE,
313  LOC + QString("Update gametype %1").arg(GameType));
314 
315  query.prepare("SELECT romname,`system`,spandisks,gamename FROM "
316  "gamemetadata,gameplayers WHERE "
317  "gamemetadata.gametype = :GAMETYPE AND "
318  "playername = `system` ORDER BY romname");
319 
320  query.bindValue(":GAMETYPE",GameType);
321 
322  if (query.exec())
323  {
324  while (query.next())
325  {
326  QString RomName = query.value(0).toString();
327  QString System = query.value(1).toString();
328  int spandisks = query.value(2).toInt();
329  QString GameName = query.value(3).toString();
330 
331  basename = RomName;
332 
333  if (spandisks)
334  {
335  int diskcount = 0;
336  int extlength = 0;
337  int pos = RomName.lastIndexOf(".");
338  if (pos > 1)
339  {
340  extlength = RomName.length() - pos;
341  pos--;
342 
343  basename = RomName.mid(pos,1);
344  }
345 
346  if (basename.contains(multiDiskRGXP))
347  {
348  pos = (RomName.length() - extlength) - 1;
349  basename = RomName.left(pos);
350 
351  if (basename.right(1) == ".")
352  basename = RomName.left(pos - 1);
353  }
354  else
355  basename = GameName;
356 
357  if (basename == lastrom)
358  {
359  updateDisplayRom(RomName,0,System);
360  diskcount++;
361  if (diskcount > 1)
362  updateDiskCount(firstname,diskcount,GameType);
363  }
364  else
365  {
366  firstname = RomName;
367  lastrom = basename;
368  }
369 
370  if (basename != GameName)
371  updateGameName(RomName,basename,System);
372  }
373  else
374  {
375  if (basename == lastrom)
376  updateDisplayRom(RomName,0,System);
377  else
378  lastrom = basename;
379  }
380  }
381  }
382  }
383 }
384 
386 {
387  int counter = 0;
388  MSqlQuery query(MSqlQuery::InitCon());
389 
390  //: %1 is the system name, %2 is the game type
391  QString message = tr("Updating %1 (%2) ROM database")
392  .arg(handler->SystemName(), handler->GameType());
393 
394  CreateProgress(message);
395 
396  if (m_progressDlg)
398 
399  QString GameName;
400  QString Genre;
401  QString Country;
402  QString CRC32;
403  QString Year;
404  QString Plot;
405  QString Publisher;
406  QString Version;
407  QString Fanart;
408  QString Boxart;
409  QString ScreenShot;
410 
411  int removalprompt = gCoreContext->GetSetting("GameRemovalPrompt").toInt();
412  int indepth = gCoreContext->GetSetting("GameDeepScan").toInt();
413  QString screenShotPath = gCoreContext->GetSetting("mythgame.screenshotdir");
414 
415  for (const auto & game : qAsConst(m_gameMap))
416  {
417 
418  if (game.FoundLoc() == inFileSystem)
419  {
420  if (indepth)
421  {
422  GetMetadata(handler, game.RomFullPath(), &Genre, &Year, &Country, &CRC32, &GameName,
423  &Plot, &Publisher, &Version, &Fanart, &Boxart);
424  }
425  else
426  {
427  /*: %1 is the game type, when we don't know the genre we use the
428  * game type */
429  Genre = tr("Unknown %1", "Unknown genre").arg(handler->GameType());
430  Country = tr("Unknown", "Unknown country");
431  CRC32.clear();
432  Year = tr("19xx", "Default game year");
433  GameName = tr("Unknown", "Unknown game name");
434  Plot = tr("Unknown", "Unknown plot");
435  Publisher = tr("Unknown", "Unknown publisher");
436  Version = tr("0", "Default game version");
437  Fanart.clear();
438  Boxart.clear();
439  }
440 
441  if (GameName == tr("Unknown", "Unknown game name"))
442  GameName = game.GameName();
443 
444  int suffixPos = game.Rom().lastIndexOf(QChar('.'));
445  QString baseName = game.Rom();
446 
447  if (suffixPos > 0)
448  baseName = game.Rom().left(suffixPos);
449 
450  baseName = screenShotPath + "/" + baseName;
451 
452  if (QFile(baseName + ".png").exists())
453  ScreenShot = baseName + ".png";
454  else if (QFile(baseName + ".jpg").exists())
455  ScreenShot = baseName + ".jpg";
456  else if (QFile(baseName + ".gif").exists())
457  ScreenShot = baseName + ".gif";
458  else
459  ScreenShot.clear();
460 
461 #if 0
462  LOG(VB_GENERAL, LOG_INFO, QString("file %1 - genre %2 ")
463  .arg(iter.data().Rom()).arg(Genre));
464  LOG(VB_GENERAL, LOG_INFO, QString("screenshot %1").arg(ScreenShot));
465 #endif
466 
467  query.prepare("INSERT INTO gamemetadata "
468  "(`system`, romname, gamename, genre, year, gametype, "
469  "rompath, country, crc_value, diskcount, display, plot, "
470  "publisher, version, fanart, boxart, screenshot) "
471  "VALUES (:SYSTEM, :ROMNAME, :GAMENAME, :GENRE, :YEAR, "
472  ":GAMETYPE, :ROMPATH, :COUNTRY, :CRC32, '1', '1', :PLOT, :PUBLISHER, :VERSION, "
473  ":FANART, :BOXART, :SCREENSHOT)");
474 
475  query.bindValueNoNull(":SYSTEM",handler->SystemName());
476  query.bindValueNoNull(":ROMNAME",game.Rom());
477  query.bindValueNoNull(":GAMENAME",GameName);
478  query.bindValueNoNull(":GENRE",Genre);
479  query.bindValueNoNull(":YEAR",Year);
480  query.bindValueNoNull(":GAMETYPE",handler->GameType());
481  query.bindValueNoNull(":ROMPATH",game.RomPath());
482  query.bindValueNoNull(":COUNTRY",Country);
483  query.bindValueNoNull(":CRC32", CRC32);
484  query.bindValueNoNull(":PLOT", Plot);
485  query.bindValueNoNull(":PUBLISHER", Publisher);
486  query.bindValueNoNull(":VERSION", Version);
487  query.bindValueNoNull(":FANART", Fanart);
488  query.bindValueNoNull(":BOXART", Boxart);
489  query.bindValueNoNull(":SCREENSHOT", ScreenShot);
490 
491  if (!query.exec())
492  MythDB::DBError("GameHandler::UpdateGameDB - "
493  "insert gamemetadata", query);
494  }
495  else if ((game.FoundLoc() == inDatabase) && (removalprompt))
496  {
497 
498  promptForRemoval( game );
499  }
500 
501  if (m_progressDlg)
502  m_progressDlg->SetProgress(++counter);
503  }
504 
505  if (m_progressDlg)
506  {
507  m_progressDlg->Close();
508  m_progressDlg = nullptr;
509 }
510 }
511 
513 {
514  int counter = 0;
515 
516  MSqlQuery query(MSqlQuery::InitCon());
517  query.prepare("SELECT romname,rompath,gamename FROM gamemetadata "
518  "WHERE `system` = :SYSTEM");
519 
520  query.bindValue(":SYSTEM",handler->SystemName());
521 
522  if (!query.exec())
523  MythDB::DBError("GameHandler::VerifyGameDB - "
524  "select", query);
525 
526  //: %1 is the system name
527  QString message = tr("Verifying %1 files...").arg(handler->SystemName());
528 
529  CreateProgress(message);
530 
531  if (m_progressDlg)
532  m_progressDlg->SetTotal(query.size());
533 
534  // For every file we know about, check to see if it still exists.
535  while (query.next())
536  {
537  QString RomName = query.value(0).toString();
538  QString RomPath = query.value(1).toString();
539  QString GameName = query.value(2).toString();
540  if (!RomName.isEmpty())
541  {
542 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
543  auto iter = m_gameMap.find(RomName);
544 #else
545  auto iter = m_gameMap.constFind(RomName);
546 #endif
547  if (iter != m_gameMap.end())
548  {
549  // If it's both on disk and in the database we're done with it.
550  m_gameMap.erase(iter);
551  }
552  else
553  {
554  // If it's only in the database add it to our list and mark it for
555  // removal.
556  m_gameMap[RomName] = GameScan(RomName,RomPath + "/" + RomName,inDatabase,
557  GameName,RomPath);
558  }
559  }
560  if (m_progressDlg)
561  m_progressDlg->SetProgress(++counter);
562  }
563 
564  if (m_progressDlg)
565  {
566  m_progressDlg->Close();
567  m_progressDlg = nullptr;
568  }
569 }
570 
571 // Recurse through the directory and gather a count on how many files there are to process.
572 // This is used for the progressbar info.
573 int GameHandler::buildFileCount(const QString& directory, GameHandler *handler)
574 {
575  int filecount = 0;
576  QDir RomDir(directory);
577 
578  // If we can't read it's contents move on
579  if (!RomDir.isReadable())
580  return 0;
581 
582  RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
583  QFileInfoList List = RomDir.entryInfoList();
584  for (const auto & Info : qAsConst(List))
585  {
586  if (Info.isDir())
587  {
588  filecount += buildFileCount(Info.filePath(), handler);
589  continue;
590  }
591 
592  if (handler->m_validextensions.count() > 0)
593  {
594  QRegularExpression r {
595  "^" + Info.suffix() + "$",
596  QRegularExpression::CaseInsensitiveOption };
597  QStringList result;
598  QStringList& exts = handler->m_validextensions;
599  std::copy_if(exts.cbegin(), exts.cend(), std::back_inserter(result),
600  [&r](const QString& extension){ return extension.contains(r); } );
601  if (result.isEmpty())
602  continue;
603  }
604 
605  filecount++;
606  }
607 
608  return filecount;
609 }
610 
612 {
613  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
614  auto *clearPopup = new MythDialogBox(
615  tr("This will clear all game metadata from the database. Are you sure "
616  "you want to do this?"), popupStack, "clearAllPopup");
617 
618  if (clearPopup->Create())
619  {
620  clearPopup->SetReturnEvent(this, "clearAllPopup");
621  clearPopup->AddButton(tr("No"));
622  clearPopup->AddButton(tr("Yes"));
623  popupStack->AddScreen(clearPopup);
624  }
625  else
626  delete clearPopup;
627 }
628 
629 void GameHandler::buildFileList(const QString& directory, GameHandler *handler,
630  int* filecount)
631 {
632  QDir RomDir(directory);
633 
634  // If we can't read its contents move on
635  if (!RomDir.isReadable())
636  return;
637 
638  RomDir.setSorting( QDir:: DirsFirst | QDir::Name );
639  RomDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
640  QFileInfoList List = RomDir.entryInfoList();
641  for (const auto & Info : qAsConst(List))
642  {
643  QString RomName = Info.fileName();
644  QString GameName = Info.completeBaseName();
645 
646  if (Info.isDir())
647  {
648  buildFileList(Info.filePath(), handler, filecount);
649  continue;
650  }
651 
652  if (handler->m_validextensions.count() > 0)
653  {
654  QRegularExpression r {
655  "^" + Info.suffix() + "$",
656  QRegularExpression::CaseInsensitiveOption };
657  QStringList result;
658  QStringList& exts = handler->m_validextensions;
659  std::copy_if(exts.cbegin(), exts.cend(), std::back_inserter(result),
660  [&r](const QString& extension){ return extension.contains(r); } );
661  if (result.isEmpty())
662  continue;
663  }
664 
665  m_gameMap[RomName] = GameScan(RomName,Info.filePath(),inFileSystem,
666  GameName, Info.absoluteDir().path());
667 
668  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found ROM : (%1) - %2")
669  .arg(handler->SystemName(), RomName));
670 
671  *filecount = *filecount + 1;
672  if (m_progressDlg)
673  m_progressDlg->SetProgress(*filecount);
674  }
675 }
676 
678 {
679  int maxcount = 0;
680  MSqlQuery query(MSqlQuery::InitCon());
681 
682  if ((!handler->SystemRomPath().isEmpty()) && (handler->GameType() != "PC"))
683  {
684  QDir d(handler->SystemRomPath());
685  if (d.exists())
686  maxcount = buildFileCount(handler->SystemRomPath(),handler);
687  else
688  {
689  LOG(VB_GENERAL, LOG_ERR, LOC +
690  QString("ROM Path does not exist: %1")
691  .arg(handler->SystemRomPath()));
692  return;
693  }
694  }
695  else
696  maxcount = 100;
697 
698  if (handler->GameType() == "PC")
699  {
700  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
701 
702  //: %1 is the system name
703  QString message = tr("Scanning for %1 games...")
704  .arg(handler->SystemName());
705  auto *busyDialog = new MythUIBusyDialog(message, popupStack,
706  "gamescanbusy");
707 
708  if (busyDialog->Create())
709  popupStack->AddScreen(busyDialog, false);
710  else
711  {
712  delete busyDialog;
713  busyDialog = nullptr;
714  }
715 
716  m_gameMap[handler->SystemCmdLine()] =
717  GameScan(handler->SystemCmdLine(),
718  handler->SystemCmdLine(),
719  inFileSystem,
720  handler->SystemName(),
721  handler->SystemCmdLine().left(handler->SystemCmdLine().lastIndexOf("/")));
722 
723  if (busyDialog)
724  busyDialog->Close();
725 
726  LOG(VB_GENERAL, LOG_INFO, LOC +
727  QString("PC Game %1").arg(handler->SystemName()));
728  }
729  else
730  {
731  QString message = tr("Scanning for %1 games...")
732  .arg(handler->SystemName());
733  CreateProgress(message);
734 
735  if (m_progressDlg)
736  m_progressDlg->SetTotal(maxcount);
737 
738  int filecount = 0;
739  buildFileList(handler->SystemRomPath(), handler, &filecount);
740 
741  if (m_progressDlg)
742  {
743  m_progressDlg->Close();
744  m_progressDlg = nullptr;
745  }
746  }
747 
748  VerifyGameDB(handler);
749 
750  // If we still have some games in the list then update the database
751  if (!m_gameMap.empty())
752  {
753  InitMetaDataMap(handler->GameType());
754 
755  UpdateGameDB(handler);
756 
757  m_romDB.clear();
758  handler->setRebuild(true);
759  }
760  else
761  handler->setRebuild(false);
762 }
763 
765 {
766  checkHandlers();
767  QStringList updatelist;
768 
769  for (auto *handler : qAsConst(*handlers))
770  {
771  if (handler)
772  {
773  updateSettings(handler);
774  handler->processGames(handler);
775 
776  if (handler->needRebuild())
777  updatelist.append(handler->GameType());
778  }
779  }
780 
781  if (!updatelist.isEmpty())
782  UpdateGameCounts(updatelist);
783 }
784 
786 {
787  if (!rominfo)
788  return nullptr;
789 
790  for (auto *handler : qAsConst(*handlers))
791  {
792  if (handler)
793  {
794  if (rominfo->System() == handler->SystemName())
795  return handler;
796  }
797  }
798 
799  return nullptr;
800 }
801 
802 GameHandler* GameHandler::GetHandlerByName(const QString& systemname)
803 {
804  if (systemname.isEmpty())
805  return nullptr;
806 
807  for (auto *handler : qAsConst(*handlers))
808  {
809  if (handler)
810  {
811  if (handler->SystemName() == systemname)
812  return handler;
813  }
814  }
815 
816  return nullptr;
817 }
818 
819 void GameHandler::Launchgame(RomInfo *romdata, const QString& systemname)
820 {
821  GameHandler *handler = nullptr;
822 
823  if (!systemname.isEmpty())
824  {
825  handler = GetHandlerByName(systemname);
826  }
827  else if (!(handler = GetHandler(romdata)))
828  {
829  // Couldn't get handler so abort.
830  return;
831  }
832  QString exec = handler->SystemCmdLine();
833 
834  if (exec.isEmpty())
835  return;
836 
837  if (handler->GameType() != "PC")
838  {
839  QString arg = "\"" + romdata->Rompath() +
840  "/" + romdata->Romname() + "\"";
841 
842  // If they specified a %s in the commandline place the romname
843  // in that location, otherwise tack it on to the end of
844  // the command.
845  if (exec.contains("%s") || handler->SpanDisks())
846  {
847  exec = exec.replace("%s",arg);
848 
849  if (handler->SpanDisks())
850  {
851  static const QRegularExpression rxp { "%d[0-4]" };
852 
853  if (exec.contains(rxp))
854  {
855  if (romdata->DiskCount() > 1)
856  {
857  // Chop off the extension, . and last character of the name which we are assuming is the disk #
858  QString basename = romdata->Romname().left(romdata->Romname().length() - (romdata->getExtension().length() + 2));
859  QString extension = romdata->getExtension();
860  QString rom;
861  std::array<QString,7> diskid { "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6" };
862 
863  for (int disk = 1; disk <= romdata->DiskCount(); disk++)
864  {
865  rom = QString("\"%1/%2%3.%4\"")
866  .arg(romdata->Rompath(), basename,
867  QString::number(disk), extension);
868  exec = exec.replace(diskid[disk],rom);
869  }
870  } else
871  { // If there is only one disk make sure we replace %d1 just like %s
872  exec = exec.replace("%d1",arg);
873  }
874  }
875  }
876  }
877  else
878  {
879  exec = exec + " \"" +
880  romdata->Rompath() + "/" +
881  romdata->Romname() + "\"";
882  }
883  }
884 
885  QString savedir = QDir::current().path();
886  QDir d;
887  if (!handler->SystemWorkingPath().isEmpty())
888  {
889  if (!d.cd(handler->SystemWorkingPath()))
890  {
891  LOG(VB_GENERAL, LOG_ERR, LOC +
892  QString("Failed to change to specified Working Directory: %1")
893  .arg(handler->SystemWorkingPath()));
894  }
895  }
896  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Launching Game : %1 : %2")
897  .arg(handler->SystemName(), exec));
898 
899  GetMythUI()->AddCurrentLocation(QString("MythGame %1 ( %2 )")
900  .arg(handler->SystemName(), exec));
901 
902  QStringList cmdlist = exec.split(";");
903  if (cmdlist.count() > 0)
904  {
905  for (const auto & cmd : qAsConst(cmdlist))
906  {
907  LOG(VB_GENERAL, LOG_INFO, LOC +
908  QString("Executing : %1").arg(cmd));
910  }
911  }
912  else
913  {
914  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Executing : %1").arg(exec));
916  }
917 
919 
920  (void)d.cd(savedir);
921 }
922 
924 {
925  if (!parent || !GetHandler(parent))
926  return nullptr;
927 
928  return new RomInfo(*parent);
929 }
930 
932 {
933  handlers->append(handler);
934 }
935 
936 void GameHandler::customEvent(QEvent *event)
937 {
938  if (auto *dce = dynamic_cast<DialogCompletionEvent*>(event))
939  {
940  QString resultid = dce->GetId();
941 // QString resulttext = dce->GetResultText();
942 
943  if (resultid == "removalPopup")
944  {
945  int buttonNum = dce->GetResult();
946  auto scan = dce->GetData().value<GameScan>();
947  switch (buttonNum)
948  {
949  case 1:
950  m_keepAll = true;
951  break;
952  case 2:
953  purgeGameDB(scan.Rom() , scan.RomFullPath());
954  break;
955  case 3:
956  m_removeAll = true;
957  purgeGameDB(scan.Rom() , scan.RomFullPath());
958  break;
959  default:
960  break;
961  };
962  }
963  else if (resultid == "clearAllPopup")
964  {
965  int buttonNum = dce->GetResult();
966  switch (buttonNum)
967  {
968  case 1:
970  break;
971  default:
972  break;
973  }
974  }
975  }
976 }
977 
979 {
980  MSqlQuery query(MSqlQuery::InitCon());
981  if (!query.exec("DELETE FROM gamemetadata;"))
982  MythDB::DBError("GameHandler::clearAllGameData - "
983  "delete gamemetadata", query);
984 }
985 
986 void GameHandler::CreateProgress(const QString& message)
987 {
988  if (m_progressDlg)
989  return;
990 
991  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
992 
993  m_progressDlg = new MythUIProgressDialog(message, popupStack,
994  "gameprogress");
995 
996  if (m_progressDlg->Create())
997  {
998  popupStack->AddScreen(m_progressDlg, false);
999  }
1000  else
1001  {
1002  delete m_progressDlg;
1003  m_progressDlg = nullptr;
1004  }
1005 }
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:807
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
MythUIProgressDialog::Create
bool Create(void) override
Definition: mythprogressdialog.cpp:130
updateDisplayRom
static void updateDisplayRom(const QString &romname, int display, const QString &Systemname)
Definition: gamehandler.cpp:254
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:215
MSqlQuery::bindValueNoNull
void bindValueNoNull(const QString &placeholder, const QVariant &val)
Add a single binding, taking care not to set a NULL value.
Definition: mythdbcon.cpp:888
MythUILocation::RemoveCurrentLocation
QString RemoveCurrentLocation()
Definition: mythuilocation.cpp:12
GameHandler::CreateProgress
void CreateProgress(const QString &message)
Definition: gamehandler.cpp:986
mythdb.h
GameHandler::setRebuild
void setRebuild(bool setrebuild)
Definition: gamehandler.h:100
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:386
RomPath
Definition: gamesettings.cpp:266
GameHandler::GetMetadata
void GetMetadata(GameHandler *handler, const QString &rom, QString *Genre, QString *Year, QString *Country, QString *CRC32, QString *GameName, QString *Plot, QString *Publisher, QString *Version, QString *Fanart, QString *Boxart)
Definition: gamehandler.cpp:149
GameHandler::clearAllGameData
void clearAllGameData(void)
Definition: gamehandler.cpp:611
GameHandler
Definition: gamehandler.h:64
mythdialogbox.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:205
MythScreenStack
Definition: mythscreenstack.h:16
GameHandler::SpanDisks
bool SpanDisks() const
Definition: gamehandler.h:104
mythdbcon.h
GameHandler::registerHandler
static void registerHandler(GameHandler *handler)
Definition: gamehandler.cpp:931
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:608
GameHandler::CreateRomInfo
static RomInfo * CreateRomInfo(RomInfo *parent)
Definition: gamehandler.cpp:923
GameHandler::customEvent
void customEvent(QEvent *event) override
Definition: gamehandler.cpp:936
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
GameHandler::UpdateGameDB
void UpdateGameDB(GameHandler *handler)
Definition: gamehandler.cpp:385
RomInfo::System
QString System() const
Definition: rominfo.h:75
myth_system
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
Definition: mythsystemlegacy.cpp:506
hardwareprofile.scan.scan
def scan(profile, smoonURL, gate)
Definition: scan.py:57
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
mythprogressdialog.h
GameHandler::clearAllMetadata
static void clearAllMetadata(void)
Definition: gamehandler.cpp:978
mythsystemlegacy.h
GameHandler::GetHandlerByName
static GameHandler * GetHandlerByName(const QString &systemname)
Definition: gamehandler.cpp:802
GameHandler::m_progressDlg
MythUIProgressDialog * m_progressDlg
Definition: gamehandler.h:143
UpdateGameCounts
static void UpdateGameCounts(const QStringList &updatelist)
Definition: gamehandler.cpp:300
LOC
#define LOC
Definition: gamehandler.cpp:25
MythUIProgressDialog
Definition: mythprogressdialog.h:59
GameScan
Definition: gamehandler.h:32
RomInfo::Rompath
QString Rompath() const
Definition: rominfo.h:60
MythDialogBox
Basic menu dialog, message and a list of options.
Definition: mythdialogbox.h:166
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:540
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:227
MythUIProgressDialog::SetProgress
void SetProgress(uint count)
Definition: mythprogressdialog.cpp:199
GameHandler::getHandler
static GameHandler * getHandler(uint i)
Definition: gamehandler.cpp:54
updateDiskCount
static void updateDiskCount(const QString &romname, int diskcount, const QString &GameType)
Definition: gamehandler.cpp:269
GameHandler::promptForRemoval
void promptForRemoval(const GameScan &scan)
Definition: gamehandler.cpp:222
GameHandler::processAllGames
static void processAllGames(void)
Definition: gamehandler.cpp:764
GameHandler::GameType
QString GameType() const
Definition: gamehandler.h:111
GameHandler::processGames
void processGames(GameHandler *handler)
Definition: gamehandler.cpp:677
rominfo.h
handlers
static QList< GameHandler * > * handlers
Definition: gamehandler.cpp:27
MythUIBusyDialog
Definition: mythprogressdialog.h:36
GameHandler::m_systemname
QString m_systemname
Definition: gamehandler.h:124
checkHandlers
static void checkHandlers(void)
Definition: gamehandler.cpp:29
uint
unsigned int uint
Definition: compat.h:79
inFileSystem
@ inFileSystem
Definition: gamehandler.h:27
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:54
inDatabase
@ inDatabase
Definition: gamehandler.h:28
GameHandler::m_screenshots
QString m_screenshots
Definition: gamehandler.h:128
GameHandler::newHandler
static GameHandler * newHandler(QString name)
Definition: gamehandler.cpp:90
GameHandler::m_keepAll
bool m_keepAll
Definition: gamehandler.h:137
GameHandler::GetHandler
static GameHandler * GetHandler(RomInfo *rominfo)
Definition: gamehandler.cpp:785
RomInfo
Definition: rominfo.h:14
mythuihelper.h
RomInfo::DiskCount
int DiskCount() const
Definition: rominfo.h:96
GameHandler::SystemCmdLine
QString SystemCmdLine() const
Definition: gamehandler.h:106
crcinfo
QString crcinfo(const QString &romname, const QString &GameType, QString *key, RomDBMap *romDB)
Definition: rom_metadata.cpp:59
GameHandler::m_gameplayerid
uint m_gameplayerid
Definition: gamehandler.h:129
GameHandler::SystemName
QString SystemName() const
Definition: gamehandler.h:105
GameHandler::m_commandline
QString m_commandline
Definition: gamehandler.h:126
GameHandler::updateSettings
static void updateSettings(GameHandler *handler)
Definition: gamehandler.cpp:59
kMSProcessEvents
@ kMSProcessEvents
process events while waiting
Definition: mythsystem.h:39
GameHandler::VerifyGameDB
void VerifyGameDB(GameHandler *handler)
Definition: gamehandler.cpp:512
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:883
MythUIProgressDialog::SetTotal
void SetTotal(uint total)
Definition: mythprogressdialog.cpp:193
RomInfo::getExtension
QString getExtension() const
Definition: rominfo.cpp:234
DialogCompletionEvent
Event dispatched from MythUI modal dialogs to a listening class containing a result of some form.
Definition: mythdialogbox.h:41
mythcontext.h
GameHandler::buildFileList
void buildFileList(const QString &directory, GameHandler *handler, int *filecount)
Definition: gamehandler.cpp:629
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:102
GameHandler::Launchgame
static void Launchgame(RomInfo *romdata, const QString &systemname)
Definition: gamehandler.cpp:819
purgeGameDB
static void purgeGameDB(const QString &filename, const QString &RomPath)
Definition: gamehandler.cpp:200
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &Stackname)
Definition: mythmainwindow.cpp:320
GameHandler::m_gameMap
GameScanMap m_gameMap
Definition: gamehandler.h:134
GameHandler::s_newInstance
static GameHandler * s_newInstance
Definition: gamehandler.h:141
GameType
Definition: gamesettings.cpp:246
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
MythUILocation::AddCurrentLocation
void AddCurrentLocation(const QString &Location)
Definition: mythuilocation.cpp:5
GetMythUI
MythUIHelper * GetMythUI()
Definition: mythuihelper.cpp:66
RomData
Definition: rom_metadata.h:11
rom_metadata.h
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
GameHandler::m_rompath
QString m_rompath
Definition: gamehandler.h:125
GameHandler::m_romDB
RomDBMap m_romDB
Definition: gamehandler.h:133
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:50
GameHandler::m_validextensions
QStringList m_validextensions
Definition: gamehandler.h:131
GameHandler::GameHandler
GameHandler()=default
GameHandler::m_spandisks
bool m_spandisks
Definition: gamehandler.h:123
updateGameName
static void updateGameName(const QString &romname, const QString &GameName, const QString &Systemname)
Definition: gamehandler.cpp:284
gamehandler.h
GameHandler::SystemRomPath
QString SystemRomPath() const
Definition: gamehandler.h:107
GameHandler::m_gametype
QString m_gametype
Definition: gamehandler.h:130
GameHandler::buildFileCount
static int buildFileCount(const QString &directory, GameHandler *handler)
Definition: gamehandler.cpp:573
GameHandler::m_removeAll
bool m_removeAll
Definition: gamehandler.h:136
GameHandler::m_workingpath
QString m_workingpath
Definition: gamehandler.h:127
RomInfo::Romname
QString Romname() const
Definition: rominfo.h:72
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:896
GameHandler::count
static uint count(void)
Definition: gamehandler.cpp:101
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:832
GameHandler::InitMetaDataMap
void InitMetaDataMap(const QString &GameType)
Definition: gamehandler.cpp:107
GameHandler::SystemWorkingPath
QString SystemWorkingPath() const
Definition: gamehandler.h:108