MythTV  master
gamescan.cpp
Go to the documentation of this file.
1 #include <QApplication>
2 #include <QImageReader>
3 #include <QStringList>
4 #include <QThread>
5 #include <QUrl>
6 #include <utility>
7 
8 #include <mythcontext.h>
9 #include <mythmainwindow.h>
10 #include <mythscreenstack.h>
11 #include <mythprogressdialog.h>
12 #include <mythdialogbox.h>
13 #include <mythevent.h>
14 #include <remoteutil.h>
15 #include <mythlogging.h>
16 
17 #include "gamescan.h"
18 #include "gamehandler.h"
19 #include "rominfo.h"
20 
22 
24  MThread("GameScanner"),
25  m_hasGUI(gCoreContext->HasGUI())
26 {
27 }
28 
30 {
31  RunProlog();
32 
33  LOG(VB_GENERAL, LOG_INFO, QString("Beginning Game Scan."));
34 
35  m_files.clear();
36  m_remove.clear();
38 
39  buildFileList();
40  verifyFiles();
41  updateDB();
42 
43  LOG(VB_GENERAL, LOG_INFO, QString("Finished Game Scan."));
44 
45  RunEpilog();
46 }
47 
48 
50 {
51  RomInfo *info = RomInfo::GetRomInfoById(id);
52  if (info)
53  {
54  info->DeleteFromDatabase();
55  delete info;
56  info = nullptr;
57  }
58 }
59 
61 {
62  int counter = 0;
63 
64  if (m_hasGUI)
65  SendProgressEvent(counter, (uint)m_dbgames.count(),
66  GameScanner::tr("Verifying game files..."));
67 
68  // For every file we know about, check to see if it still exists.
69  for (const auto *info : qAsConst(m_dbgames))
70  {
71  QString romfile = info->Romname();
72  QString gametype = info->GameType();
73  if (!romfile.isEmpty())
74  {
75  bool found = false;
76  // NOLINTNEXTLINE(modernize-loop-convert)
77  for (auto p2 = m_files.begin(); p2 != m_files.end(); ++p2)
78  {
79  if ((*p2).romfile == romfile &&
80  (*p2).gametype == gametype)
81  {
82  // We're done here, this file matches one in the DB
83  (*p2).indb = true;
84  found = true;
85  continue;
86  }
87  }
88  if (!found)
89  {
90  m_remove.append(info->Id());
91  }
92  }
93  if (m_hasGUI)
94  SendProgressEvent(++counter);
95 
96  delete info;
97  info = nullptr;
98  }
99 }
100 
102 {
103  uint counter = 0;
104  if (m_hasGUI)
105  SendProgressEvent(counter, (uint)(m_files.size() + m_remove.size()),
106  GameScanner::tr("Updating game database..."));
107 
108  for (const auto & file : qAsConst(m_files))
109  {
110  if (!file.indb)
111  {
112  RomInfo add(0, file.romfile, file.system,
113  file.romname, "", "", false, file.rompath,
114  "", "", 0, file.gametype, 0, "", "", "",
115  "", "", "", "", "");
116  add.SaveToDatabase();
117  m_dbDataChanged = true;
118  }
119  if (m_hasGUI)
120  SendProgressEvent(++counter);
121  }
122 
123  for (const uint & p : qAsConst(m_remove))
124  {
125  removeOrphan(p);
126  m_dbDataChanged = true;
127  }
128 }
129 
131 {
132  if (m_handlers.empty())
133  return false;
134 
135  int counter = 0;
136 
137  if (m_hasGUI)
138  SendProgressEvent(counter, (uint)m_handlers.size(),
139  GameScanner::tr("Searching for games..."));
140 
141  for (auto * handler : qAsConst(m_handlers))
142  {
143  QDir dir(handler->SystemRomPath());
144  QStringList extensions = handler->ValidExtensions();
145  QStringList filters;
146  for (const auto & ext : qAsConst(extensions))
147  {
148  filters.append(QString("*.%1").arg(ext));
149  }
150 
151  dir.setNameFilters(filters);
152  dir.setFilter(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
153 
154  QStringList files = dir.entryList();
155  for (const auto & file : qAsConst(files))
156  {
157  RomFileInfo info;
158  info.system = handler->SystemName();
159  info.gametype = handler->GameType();
160  info.romfile = file;
161  info.rompath = handler->SystemRomPath();
162  info.romname = QFileInfo(file).baseName();
163  info.indb = false;
164  m_files.append(info);
165  }
166 
167  if (m_hasGUI)
168  SendProgressEvent(++counter);
169  }
170 
171  return true;
172 }
173 
175  QString message)
176 {
177  if (!m_dialog)
178  return;
179 
180  auto *pue = new ProgressUpdateEvent(progress, total, std::move(message));
181  QApplication::postEvent(m_dialog, pue);
182 }
183 
185 {
187 }
188 
190 {
191  if (m_scanThread && m_scanThread->wait())
192  delete m_scanThread;
193 }
194 
195 void GameScanner::doScan(QList<GameHandler*> handlers)
196 {
197  if (m_scanThread->isRunning())
198  return;
199 
200  if (gCoreContext->HasGUI())
201  {
202  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
203 
204  auto *progressDlg = new MythUIProgressDialog("",
205  popupStack, "gamescanprogressdialog");
206 
207  if (progressDlg->Create())
208  {
209  popupStack->AddScreen(progressDlg, false);
210  connect(m_scanThread->qthread(), &QThread::finished,
211  progressDlg, &MythScreenType::Close);
212  connect(m_scanThread->qthread(), &QThread::finished,
214  }
215  else
216  {
217  delete progressDlg;
218  progressDlg = nullptr;
219  }
220  m_scanThread->SetProgressDialog(progressDlg);
221  }
222 
223  m_scanThread->SetHandlers(std::move(handlers));
224  m_scanThread->start();
225 }
226 
228 {
229  QList<GameHandler*> hlist;
230 
231  MSqlQuery query(MSqlQuery::InitCon());
232  query.prepare("SELECT DISTINCT playername FROM gameplayers "
233  "WHERE playername <> '';");
234 
235  if (!query.exec())
236  MythDB::DBError("doScanAll - selecting playername", query);
237 
238  while (query.next())
239  {
240  QString name = query.value(0).toString();
242  if (hnd)
243  hlist.append(hnd);
244  }
245 
246  doScan(hlist);
247 }
248 
250 {
252 }
253 
GameScanner::doScanAll
void doScanAll(void)
Definition: gamescan.cpp:227
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:802
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:124
mythevent.h
GameScannerThread::buildFileList
bool buildFileList()
Definition: gamescan.cpp:130
RomFileInfo
Definition: gamescan.h:18
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:286
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:385
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:303
mythscreenstack.h
GameScannerThread::getDataChanged
bool getDataChanged() const
Definition: gamescan.h:41
RomFileInfo::gametype
QString gametype
Definition: gamescan.h:21
GameHandler
Definition: gamehandler.h:60
progress
bool progress
Definition: mythtv/programs/mythcommflag/main.cpp:71
GameScannerThread::m_remove
QList< uint > m_remove
Definition: gamescan.h:60
RomFileInfo::romname
QString romname
Definition: gamescan.h:24
mythdialogbox.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:201
MythScreenStack
Definition: mythscreenstack.h:16
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
RomInfo::GetAllRomInfo
static QList< RomInfo * > GetAllRomInfo()
Definition: rominfo.cpp:317
GameScanner::GameScanner
GameScanner()
Definition: gamescan.cpp:184
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MThread::RunProlog
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:196
GameScannerThread::SetHandlers
void SetHandlers(QList< GameHandler * > handlers)
Definition: gamescan.h:38
build_compdb.file
file
Definition: build_compdb.py:55
remoteutil.h
GameScannerThread::m_handlers
QList< GameHandler * > m_handlers
Definition: gamescan.h:57
gamescan.h
mythprogressdialog.h
GameScannerThread::run
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: gamescan.cpp:29
MythUIProgressDialog::MythUIProgressDialog
MythUIProgressDialog(QString message, MythScreenStack *parent, const char *name)
Definition: mythprogressdialog.h:63
GameHandler::GetHandlerByName
static GameHandler * GetHandlerByName(const QString &systemname)
Definition: gamehandler.cpp:799
ProgressUpdateEvent
Definition: mythprogressdialog.h:16
mythlogging.h
hardwareprofile.config.p
p
Definition: config.py:33
MythUIProgressDialog
Definition: mythprogressdialog.h:59
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:200
p2
static guint32 * p2
Definition: goom_core.cpp:31
GameScanner::finished
void finished(bool)
GameScannerThread::GameScannerThread
GameScannerThread(void)
Definition: gamescan.cpp:23
MThread::qthread
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:236
GameScanner::finishedScan
void finishedScan()
Definition: gamescan.cpp:249
GameScannerThread::SendProgressEvent
void SendProgressEvent(uint progress, uint total=0, QString message=QString())
Definition: gamescan.cpp:174
RomFileInfo::romfile
QString romfile
Definition: gamescan.h:22
MThread::RunEpilog
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:209
GameScanner::~GameScanner
~GameScanner() override
Definition: gamescan.cpp:189
rominfo.h
handlers
static QList< GameHandler * > * handlers
Definition: gamehandler.cpp:23
GameScannerThread::m_files
RomFileInfoList m_files
Definition: gamescan.h:59
RomInfo::GetRomInfoById
static RomInfo * GetRomInfoById(int id)
Definition: rominfo.cpp:365
uint
unsigned int uint
Definition: compat.h:140
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
GameScannerThread::m_dbgames
QList< RomInfo * > m_dbgames
Definition: gamescan.h:61
GameScannerThread::removeOrphan
static void removeOrphan(int id)
Definition: gamescan.cpp:49
RomFileInfo::system
QString system
Definition: gamescan.h:20
RomInfo
Definition: rominfo.h:12
GameScannerThread::SetProgressDialog
void SetProgressDialog(MythUIProgressDialog *dialog)
Definition: gamescan.h:39
GameScannerThread::m_hasGUI
bool m_hasGUI
Definition: gamescan.h:55
GameScannerThread::updateDB
void updateDB()
Definition: gamescan.cpp:101
GameScannerThread
Definition: gamescan.h:30
GameScannerThread::m_dbDataChanged
bool m_dbDataChanged
Definition: gamescan.h:65
RomInfo::DeleteFromDatabase
void DeleteFromDatabase() const
Definition: rominfo.cpp:94
mythcontext.h
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
MThread::isRunning
bool isRunning(void) const
Definition: mthread.cpp:266
RomInfo::SaveToDatabase
void SaveToDatabase() const
Definition: rominfo.cpp:17
RomFileInfo::rompath
QString rompath
Definition: gamescan.h:23
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &Stackname)
Definition: mythmainwindow.cpp:322
MythCoreContext::HasGUI
bool HasGUI(void) const
Definition: mythcorecontext.cpp:1756
GameScannerThread::verifyFiles
void verifyFiles()
Definition: gamescan.cpp:60
RomFileInfo::indb
bool indb
Definition: gamescan.h:25
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:50
gamehandler.h
GameScanner::m_scanThread
GameScannerThread * m_scanThread
Definition: gamescan.h:86
GameScanner::doScan
void doScan(QList< GameHandler * > handlers)
Definition: gamescan.cpp:195
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:827
GameScannerThread::m_dialog
MythUIProgressDialog * m_dialog
Definition: gamescan.h:63