MythTV  master
rominfo.cpp
Go to the documentation of this file.
1 #include <QFile>
2 
3 #include <mythdb.h>
4 #include <mythcontext.h>
5 
6 #include "rominfo.h"
7 #include "romedit.h"
8 
9 #define LOC_ERR QString("MythGame:ROMINFO Error: ")
10 #define LOC QString("MythGame:ROMINFO: ")
11 
12 bool operator==(const RomInfo& a, const RomInfo& b)
13 {
14  return a.Romname() == b.Romname();
15 }
16 
18 {
20 
21  bool inserting = false;
22 
23  if (m_id == 0)
24  inserting = true;
25 
26  if (inserting)
27  {
28  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Adding %1 - %2").arg(Rompath())
29  .arg(Romname()));
30 
31  query.prepare("INSERT INTO gamemetadata "
32  "(`system`, romname, gamename, genre, year, gametype, "
33  "rompath, country, crc_value, diskcount, display, plot, "
34  "publisher, version, fanart, boxart, screenshot) "
35  "VALUES (:SYSTEM, :ROMNAME, :GAMENAME, :GENRE, :YEAR, "
36  ":GAMETYPE, :ROMPATH, :COUNTRY, :CRC32, '1', '1', :PLOT, "
37  ":PUBLISHER, :VERSION, :FANART, :BOXART, :SCREENSHOT)");
38 
39  query.bindValue(":SYSTEM",System());
40  query.bindValue(":ROMNAME",Romname());
41  query.bindValue(":GAMENAME",Gamename());
42  query.bindValue(":GENRE",Genre());
43  query.bindValue(":YEAR",Year());
44  query.bindValue(":GAMETYPE",GameType());
45  query.bindValue(":ROMPATH",Rompath());
46  query.bindValue(":COUNTRY",Country());
47  query.bindValue(":CRC32", QString());
48  query.bindValue(":PLOT", Plot());
49  query.bindValue(":PUBLISHER", Publisher());
50  query.bindValue(":VERSION", Version());
51  query.bindValue(":FANART", Fanart());
52  query.bindValue(":BOXART", Boxart());
53  query.bindValue(":SCREENSHOT", Screenshot());
54  }
55  else
56  {
57  query.prepare("UPDATE gamemetadata "
58  "SET version = 'CUSTOM', "
59  " gamename = :GAMENAME,"
60  " genre = :GENRE,"
61  " year = :YEAR,"
62  " country = :COUNTRY,"
63  " plot = :PLOT,"
64  " publisher = :PUBLISHER,"
65  " favorite = :FAVORITE,"
66  " screenshot = :SCREENSHOT,"
67  " fanart = :FANART,"
68  " boxart = :BOXART, "
69  " inetref = :INETREF "
70  "WHERE gametype = :GAMETYPE AND "
71  " romname = :ROMNAME");
72  query.bindValue(":GAMENAME", Gamename());
73  query.bindValue(":GENRE", Genre());
74  query.bindValue(":YEAR", Year());
75  query.bindValue(":COUNTRY", Country());
76  query.bindValue(":PLOT", Plot());
77  query.bindValue(":PUBLISHER", Publisher());
78  query.bindValue(":FAVORITE", Favorite());
79  query.bindValue(":SCREENSHOT", Screenshot());
80  query.bindValue(":FANART", Fanart());
81  query.bindValue(":BOXART", Boxart());
82  query.bindValue(":INETREF", Inetref());
83  query.bindValue(":GAMETYPE", GameType());
84  query.bindValue(":ROMNAME", Romname());
85  }
86 
87  if (!query.exec())
88  {
89  MythDB::DBError("RomInfo::SaveToDatabase", query);
90  return;
91  }
92 }
93 
95 {
96  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Removing %1 - %2").arg(Rompath())
97  .arg(Romname()));
98 
100 
101  query.prepare("DELETE FROM gamemetadata WHERE "
102  "romname = :ROMNAME AND "
103  "rompath = :ROMPATH ");
104 
105  query.bindValue(":ROMNAME",Romname());
106  query.bindValue(":ROMPATH",Rompath());
107 
108  if (!query.exec())
109  MythDB::DBError("purgeGameDB", query);
110 
111 }
112 
113 // Return the count of how many times this appears in the db already
114 int romInDB(const QString& rom, const QString& gametype)
115 {
117 
118  int count = 0;
119 
120  query.prepare("SELECT count(*) FROM gamemetadata "
121  "WHERE gametype = :GAMETYPE "
122  "AND romname = :ROMNAME");
123 
124  query.bindValue(":GAMETYPE", gametype);
125  query.bindValue(":ROMNAME", rom);
126 
127  if (!query.exec())
128  {
129  MythDB::DBError("romInDB", query);
130  return -1;
131  }
132 
133 
134  if (query.next())
135  count = query.value(0).toInt();
136 
137  return count;
138 }
139 
140 
141 bool RomInfo::FindImage(QString BaseFileName, QString *result)
142 {
143  QStringList graphic_formats;
144  graphic_formats.append("png");
145  graphic_formats.append("gif");
146  graphic_formats.append("jpg");
147  graphic_formats.append("jpeg");
148  graphic_formats.append("xpm");
149  graphic_formats.append("bmp");
150  graphic_formats.append("pnm");
151  graphic_formats.append("tif");
152  graphic_formats.append("tiff");
153 
154  int dotLocation = BaseFileName.lastIndexOf('.');
155  if (dotLocation == -1)
156  {
157  BaseFileName.append('.');
158  dotLocation = BaseFileName.length();
159  }
160 
161 
162  BaseFileName.truncate(dotLocation + 1);
163  return std::any_of(graphic_formats.cbegin(), graphic_formats.cend(),
164  [BaseFileName,result](const auto & format)
165  { *result = BaseFileName + format;
166  return QFile::exists(*result); } );
167 }
168 
169 void RomInfo::setField(const QString& field, const QString& data)
170 {
171  if (field == "system")
172  m_system = data;
173  else if (field == "gamename")
174  m_gamename = data;
175  else if (field == "genre")
176  m_genre = data;
177  else if (field == "year")
178  m_year = data;
179  else if (field == "favorite")
180  m_favorite = (data.toInt() != 0);
181  else if (field == "rompath")
182  m_rompath = data;
183  else if (field == "screenshot")
184  m_screenshot = data;
185  else if (field == "fanart")
186  m_fanart = data;
187  else if (field == "boxart")
188  m_boxart = data;
189  else if (field == "country")
190  m_country = data;
191  else if (field == "plot")
192  m_plot = data;
193  else if (field == "publisher")
194  m_publisher = data;
195  else if (field == "crc_value")
196  m_crcValue = data;
197  else if (field == "inetref")
198  m_inetref = data;
199  else if (field == "diskcount")
200  m_diskcount = data.toInt();
201  else if (field == "gametype")
202  m_gametype = data;
203  else if (field == "romcount")
204  m_romcount = data.toInt();
205  else
206  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Invalid field %1").arg(field));
207 
208 }
209 
210 void RomInfo::setFavorite(bool updateDatabase)
211 {
213 
214  if (updateDatabase)
215  {
217 
218  query.prepare("UPDATE gamemetadata SET favorite = :FAV "
219  "WHERE romname = :ROMNAME");
220 
221  query.bindValue(":FAV", m_favorite);
222  query.bindValue(":ROMNAME",m_romname);
223 
224  if (!query.exec())
225  {
226  MythDB::DBError("RomInfo::setFavorite", query);
227  }
228  }
229 }
230 
231 QString RomInfo::getExtension() const
232 {
233  int pos = Romname().lastIndexOf(".");
234  if (pos == -1)
235  return nullptr;
236 
237  pos = Romname().length() - pos;
238  pos--;
239 
240  QString ext = Romname().right(pos);
241  return ext;
242 }
243 
245 {
246  if (m_gamename == "")
247  {
248  return;
249  }
250 
252 
253  QString systemtype;
254  if (m_system != "") {
255  systemtype += " AND `system` = :SYSTEM ";
256  }
257 
258  QString thequery = "SELECT `system`,gamename,genre,year,romname,favorite,"
259  "rompath,country,crc_value,diskcount,gametype,plot,publisher,"
260  "version,screenshot,fanart,boxart,inetref,intid FROM gamemetadata "
261  "WHERE gamename = :GAMENAME "
262  + systemtype + " ORDER BY diskcount DESC";
263 
264  query.prepare(thequery);
265  query.bindValue(":SYSTEM", m_system);
266  query.bindValue(":GAMENAME", m_gamename);
267 
268  if (query.exec() && query.next())
269  {
270  setSystem(query.value(0).toString());
271  setGamename(query.value(1).toString());
272  setGenre(query.value(2).toString());
273  setYear(query.value(3).toString());
274  setRomname(query.value(4).toString());
275  setField("favorite",query.value(5).toString());
276  setRompath(query.value(6).toString());
277  setCountry(query.value(7).toString());
278  setCRC_VALUE(query.value(8).toString());
279  setDiskCount(query.value(9).toInt());
280  setGameType(query.value(10).toString());
281  setPlot(query.value(11).toString());
282  setPublisher(query.value(12).toString());
283  setVersion(query.value(13).toString());
284  setScreenshot(query.value(14).toString());
285  setFanart(query.value(15).toString());
286  setBoxart(query.value(16).toString());
287  setInetref(query.value(17).toString());
288  setId(query.value(18).toInt());
289  }
290 
292 
293  // If we have more than one instance of this rom in the DB fill in all
294  // systems available to play it.
295  if (RomCount() > 1)
296  {
297  query.prepare("SELECT DISTINCT `system` FROM gamemetadata "
298  "WHERE romname = :ROMNAME");
299  query.bindValue(":ROMNAME", Romname());
300  if (!query.exec())
301  MythDB::DBError("RomInfo::fillData - selecting systems", query);
302 
303  while (query.next())
304  {
305  if (m_allsystems.isEmpty())
306  m_allsystems = query.value(0).toString();
307  else
308  m_allsystems += "," + query.value(0).toString();
309  }
310  }
311  else
312  {
314  }
315 }
316 
317 QList<RomInfo*> RomInfo::GetAllRomInfo()
318 {
319  QList<RomInfo*> ret;
320 
322 
323  QString querystr = "SELECT intid,`system`,romname,gamename,genre,year,publisher,"
324  "favorite,rompath,screenshot,fanart,plot,boxart,"
325  "gametype,diskcount,country,crc_value,inetref,display,"
326  "version FROM gamemetadata ORDER BY diskcount DESC";
327 
328  query.prepare(querystr);
329 
330  if (!query.exec())
331  {
332  MythDB::DBError("GetAllRomInfo", query);
333  return ret;
334  }
335 
336  while (query.next())
337  {
338  auto *add = new RomInfo(
339  query.value(0).toInt(),
340  query.value(2).toString(),
341  query.value(1).toString(),
342  query.value(3).toString(),
343  query.value(4).toString(),
344  query.value(5).toString(),
345  query.value(7).toBool(),
346  query.value(8).toString(),
347  query.value(15).toString(),
348  query.value(16).toString(),
349  query.value(14).toInt(),
350  query.value(13).toString(),
351  0, QString(),
352  query.value(11).toString(),
353  query.value(6).toString(),
354  query.value(19).toString(),
355  query.value(9).toString(),
356  query.value(10).toString(),
357  query.value(12).toString(),
358  query.value(17).toString());
359  ret.append(add);
360  }
361 
362  return ret;
363 }
364 
366 {
367  RomInfo *ret = nullptr;
368 
370 
371  QString querystr = "SELECT intid,`system`,romname,gamename,genre,year,publisher,"
372  "favorite,rompath,screenshot,fanart,plot,boxart,"
373  "gametype,diskcount,country,crc_value,inetref,display,"
374  "version FROM gamemetadata WHERE intid = :INTID";
375 
376  query.prepare(querystr);
377  query.bindValue(":INTID", id);
378 
379  if (!query.exec())
380  MythDB::DBError("GetRomInfoById", query);
381 
382  if (query.next())
383  {
384  ret = new RomInfo(
385  query.value(0).toInt(),
386  query.value(2).toString(),
387  query.value(1).toString(),
388  query.value(3).toString(),
389  query.value(4).toString(),
390  query.value(5).toString(),
391  query.value(7).toBool(),
392  query.value(8).toString(),
393  query.value(15).toString(),
394  query.value(16).toString(),
395  query.value(14).toInt(),
396  query.value(13).toString(),
397  0, QString(),
398  query.value(11).toString(),
399  query.value(6).toString(),
400  query.value(19).toString(),
401  query.value(9).toString(),
402  query.value(10).toString(),
403  query.value(12).toString(),
404  query.value(17).toString());
405  }
406 
407  if (!ret)
408  ret = new RomInfo();
409 
410  return ret;
411 }
412 
413 QString RomInfo::toString() const
414 {
415  return QString ("Rom Info:\n"
416  "ID: %1\n"
417  "Game Name: %2\n"
418  "Rom Name: %3\n"
419  "Rom Path: %4")
420  .arg(Id()).arg(Gamename())
421  .arg(Romname()).arg(Rompath());
422 }
RomInfo::m_favorite
bool m_favorite
Definition: rominfo.h:143
RomInfo::Boxart
QString Boxart() const
Definition: rominfo.h:67
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
RomInfo::setGameType
void setGameType(const QString &lgametype)
Definition: rominfo.h:86
RomInfo::m_id
int m_id
Definition: rominfo.h:128
RomInfo::m_publisher
QString m_publisher
Definition: rominfo.h:138
mythdb.h
RomInfo::Id
int Id() const
Definition: rominfo.h:55
RomInfo::Plot
QString Plot() const
Definition: rominfo.h:100
RomInfo::FindImage
static bool FindImage(QString BaseFileName, QString *result)
Definition: rominfo.cpp:141
RomInfo::Country
QString Country() const
Definition: rominfo.h:82
RomInfo::Gamename
QString Gamename() const
Definition: rominfo.h:76
RomInfo::Favorite
bool Favorite() const
Definition: rominfo.h:115
RomInfo::RomCount
int RomCount() const
Definition: rominfo.h:88
RomInfo::setBoxart
void setBoxart(const QString &lboxart)
Definition: rominfo.h:68
romedit.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
arg
arg(title).arg(filename).arg(doDelete))
RomInfo::setCountry
void setCountry(const QString &lcountry)
Definition: rominfo.h:83
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
RomInfo::Genre
QString Genre() const
Definition: rominfo.h:79
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
RomInfo::Version
QString Version() const
Definition: rominfo.h:106
RomInfo::setSystem
void setSystem(const QString &lsystem)
Definition: rominfo.h:74
RomInfo::System
QString System() const
Definition: rominfo.h:73
RomInfo::m_country
QString m_country
Definition: rominfo.h:133
RomInfo::setId
void setId(int lid)
Definition: rominfo.h:56
RomInfo::m_crcValue
QString m_crcValue
Definition: rominfo.h:134
RomInfo::m_plot
QString m_plot
Definition: rominfo.h:137
RomInfo::m_romname
QString m_romname
Definition: rominfo.h:129
RomInfo::Screenshot
QString Screenshot() const
Definition: rominfo.h:61
RomInfo::m_inetref
QString m_inetref
Definition: rominfo.h:148
RomInfo::fillData
void fillData()
Definition: rominfo.cpp:244
RomInfo::m_screenshot
QString m_screenshot
Definition: rominfo.h:145
RomInfo::setInetref
void setInetref(const QString &linetref)
Definition: rominfo.h:113
RomInfo::setGamename
void setGamename(const QString &lgamename)
Definition: rominfo.h:77
RomInfo::Rompath
QString Rompath() const
Definition: rominfo.h:58
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
RomInfo::setVersion
void setVersion(const QString &lversion)
Definition: rominfo.h:107
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:178
RomInfo::m_rompath
QString m_rompath
Definition: rominfo.h:144
RomInfo::setPublisher
void setPublisher(const QString &lpublisher)
Definition: rominfo.h:104
RomInfo::setFavorite
void setFavorite(bool updateDatabase=false)
Definition: rominfo.cpp:210
RomInfo::m_year
QString m_year
Definition: rominfo.h:142
rominfo.h
RomInfo::setFanart
void setFanart(const QString &lfanart)
Definition: rominfo.h:65
RomInfo::m_gamename
QString m_gamename
Definition: rominfo.h:131
RomInfo::setPlot
void setPlot(const QString &lplot)
Definition: rominfo.h:101
RomInfo::m_genre
QString m_genre
Definition: rominfo.h:132
RomInfo::GetRomInfoById
static RomInfo * GetRomInfoById(int id)
Definition: rominfo.cpp:365
RomInfo::setDiskCount
void setDiskCount(int ldiskcount)
Definition: rominfo.h:95
RomInfo::Publisher
QString Publisher() const
Definition: rominfo.h:103
RomInfo::setRomname
void setRomname(const QString &lromname)
Definition: rominfo.h:71
RomInfo::setRompath
void setRompath(const QString &lrompath)
Definition: rominfo.h:59
operator==
bool operator==(const RomInfo &a, const RomInfo &b)
Definition: rominfo.cpp:12
RomInfo
Definition: rominfo.h:13
RomInfo::setCRC_VALUE
void setCRC_VALUE(const QString &lcrc_value)
Definition: rominfo.h:98
LOC
#define LOC
Definition: rominfo.cpp:10
RomInfo::m_gametype
QString m_gametype
Definition: rominfo.h:135
RomInfo::m_boxart
QString m_boxart
Definition: rominfo.h:147
RomInfo::Fanart
QString Fanart() const
Definition: rominfo.h:64
RomInfo::setRomCount
void setRomCount(int lromcount)
Definition: rominfo.h:89
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
RomInfo::getExtension
QString getExtension() const
Definition: rominfo.cpp:231
RomInfo::setYear
void setYear(const QString &lyear)
Definition: rominfo.h:110
RomInfo::toString
QString toString() const
Definition: rominfo.cpp:413
RomInfo::DeleteFromDatabase
void DeleteFromDatabase() const
Definition: rominfo.cpp:94
mythcontext.h
RomInfo::SaveToDatabase
void SaveToDatabase() const
Definition: rominfo.cpp:17
romInDB
int romInDB(const QString &rom, const QString &gametype)
Definition: rominfo.cpp:114
RomInfo::RomInfo
RomInfo(int lid=0, QString lromname="", QString lsystem="", QString lgamename="", QString lgenre="", QString lyear="", bool lfavorite=false, QString lrompath="", QString lcountry="", QString lcrc_value="", int ldiskcount=0, QString lgametype="", int lromcount=0, QString lallsystems="", QString lplot="", QString lpublisher="", QString lversion="", QString lscreenshot="", QString lfanart="", QString lboxart="", QString linetref="")
Definition: rominfo.h:18
RomInfo::setField
void setField(const QString &field, const QString &data)
Definition: rominfo.cpp:169
RomInfo::m_allsystems
QString m_allsystems
Definition: rominfo.h:136
RomInfo::m_diskcount
int m_diskcount
Definition: rominfo.h:141
RomInfo::setScreenshot
void setScreenshot(const QString &lscreenshot)
Definition: rominfo.h:62
RomInfo::GameType
QString GameType() const
Definition: rominfo.h:85
query
MSqlQuery query(MSqlQuery::InitCon())
RomInfo::m_fanart
QString m_fanart
Definition: rominfo.h:146
RomInfo::Inetref
QString Inetref() const
Definition: rominfo.h:112
RomInfo::Year
QString Year() const
Definition: rominfo.h:109
RomInfo::Romname
QString Romname() const
Definition: rominfo.h:70
RomInfo::m_system
QString m_system
Definition: rominfo.h:130
RomInfo::setGenre
void setGenre(const QString &lgenre)
Definition: rominfo.h:80
RomInfo::m_romcount
int m_romcount
Definition: rominfo.h:140
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808