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