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