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  RunEpilog();
44 }
45 
46 
48 {
49  RomInfo *info = RomInfo::GetRomInfoById(id);
50  if (info)
51  {
52  info->DeleteFromDatabase();
53  delete info;
54  info = nullptr;
55  }
56 }
57 
59 {
60  int counter = 0;
61 
62  if (m_hasGUI)
63  SendProgressEvent(counter, (uint)m_dbgames.count(),
64  GameScanner::tr("Verifying game files..."));
65 
66  // For every file we know about, check to see if it still exists.
67  for (const auto *info : qAsConst(m_dbgames))
68  {
69  QString romfile = info->Romname();
70  QString system = info->System();
71  QString gametype = info->GameType();
72  if (!romfile.isEmpty())
73  {
74  bool found = false;
75  // NOLINTNEXTLINE(modernize-loop-convert)
76  for (auto p2 = m_files.begin(); p2 != m_files.end(); ++p2)
77  {
78  if ((*p2).romfile == romfile &&
79  (*p2).gametype == gametype)
80  {
81  // We're done here, this file matches one in the DB
82  (*p2).indb = true;
83  found = true;
84  continue;
85  }
86  }
87  if (!found)
88  {
89  m_remove.append(info->Id());
90  }
91  }
92  if (m_hasGUI)
93  SendProgressEvent(++counter);
94 
95  delete info;
96  info = nullptr;
97  }
98 }
99 
101 {
102  uint counter = 0;
103  if (m_hasGUI)
104  SendProgressEvent(counter, (uint)(m_files.size() + m_remove.size()),
105  GameScanner::tr("Updating game database..."));
106 
107  for (const auto & file : qAsConst(m_files))
108  {
109  if (!file.indb)
110  {
111  RomInfo add(0, file.romfile, file.system,
112  file.romname, "", "", false, file.rompath,
113  "", "", 0, file.gametype, 0, "", "", "",
114  "", "", "", "", "");
115  add.SaveToDatabase();
116  m_dbDataChanged = true;
117  }
118  if (m_hasGUI)
119  SendProgressEvent(++counter);
120  }
121 
122  for (const uint & p : qAsConst(m_remove))
123  {
124  removeOrphan(p);
125  m_dbDataChanged = true;
126  }
127 }
128 
130 {
131  if (m_handlers.empty())
132  return false;
133 
134  int counter = 0;
135 
136  if (m_hasGUI)
137  SendProgressEvent(counter, (uint)m_handlers.size(),
138  GameScanner::tr("Searching for games..."));
139 
140  for (QList<GameHandler*>::const_iterator iter = m_handlers.begin();
141  iter != m_handlers.end(); ++iter)
142  {
143  QDir dir((*iter)->SystemRomPath());
144  QStringList extensions = (*iter)->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 = (*iter)->SystemName();
159  info.gametype = (*iter)->GameType();
160  info.romfile = file;
161  info.rompath = (*iter)->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(), SIGNAL(finished()),
211  progressDlg, SLOT(Close()));
212  connect(m_scanThread->qthread(), SIGNAL(finished()),
213  SLOT(finishedScan()));
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 
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 
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
bool indb
Definition: gamescan.h:27
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
void doScan(QList< GameHandler * > handlers)
Definition: gamescan.cpp:195
void verifyFiles()
Definition: gamescan.cpp:58
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void DeleteFromDatabase() const
Definition: rominfo.cpp:94
void doScanAll(void)
Definition: gamescan.cpp:227
bool getDataChanged() const
Definition: gamescan.h:43
static guint32 * p2
Definition: goom_core.cpp:30
GameScannerThread * m_scanThread
Definition: gamescan.h:88
bool HasGUI(void) const
MythUIProgressDialog * m_dialog
Definition: gamescan.h:65
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
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
bool buildFileList()
Definition: gamescan.cpp:129
MythScreenStack * GetStack(const QString &stackname)
QList< uint > m_remove
Definition: gamescan.h:62
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void SetHandlers(QList< GameHandler * > handlers)
Definition: gamescan.h:40
QString romfile
Definition: gamescan.h:24
void finished(bool)
QList< GameHandler * > m_handlers
Definition: gamescan.h:59
QVariant value(int i) const
Definition: mythdbcon.h:198
virtual void Close()
QString romname
Definition: gamescan.h:26
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
QList< RomInfo * > m_dbgames
Definition: gamescan.h:63
MSqlQuery query(MSqlQuery::InitCon())
void finishedScan()
Definition: gamescan.cpp:249
RomFileInfoList m_files
Definition: gamescan.h:61
static GameHandler * GetHandlerByName(const QString &systemname)
bool isRunning(void) const
Definition: mthread.cpp:274
GameScannerThread(void)
Definition: gamescan.cpp:23
unsigned int uint
Definition: compat.h:140
~GameScanner() override
Definition: gamescan.cpp:189
void SendProgressEvent(uint progress, uint total=0, QString message=QString())
Definition: gamescan.cpp:174
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
static QList< GameHandler * > * handlers
Definition: gamehandler.cpp:23
QString rompath
Definition: gamescan.h:25
MythMainWindow * GetMythMainWindow(void)
static RomInfo * GetRomInfoById(int id)
Definition: rominfo.cpp:368
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
static QList< RomInfo * > GetAllRomInfo()
Definition: rominfo.cpp:320
void SaveToDatabase() const
Definition: rominfo.cpp:17
QString gametype
Definition: gamescan.h:23
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
static void removeOrphan(int id)
Definition: gamescan.cpp:47
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:244
void SetProgressDialog(MythUIProgressDialog *dialog)
Definition: gamescan.h:41
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
QString system
Definition: gamescan.h:22
bool m_dbDataChanged
Definition: gamescan.h:67
MythUIProgressDialog(QString message, MythScreenStack *parent, const char *name)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23