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 {
116  MSqlQuery query(MSqlQuery::InitCon());
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  for (QStringList::Iterator i = graphic_formats.begin(); i != graphic_formats.end(); i++)
164  {
165  *result = BaseFileName + *i;
166  if (QFile::exists(*result))
167  return true;
168  }
169  return false;
170 }
171 
172 void RomInfo::setField(const QString& field, const QString& data)
173 {
174  if (field == "system")
175  m_system = data;
176  else if (field == "gamename")
177  m_gamename = data;
178  else if (field == "genre")
179  m_genre = data;
180  else if (field == "year")
181  m_year = data;
182  else if (field == "favorite")
183  m_favorite = (data.toInt() != 0);
184  else if (field == "rompath")
185  m_rompath = data;
186  else if (field == "screenshot")
187  m_screenshot = data;
188  else if (field == "fanart")
189  m_fanart = data;
190  else if (field == "boxart")
191  m_boxart = data;
192  else if (field == "country")
193  m_country = data;
194  else if (field == "plot")
195  m_plot = data;
196  else if (field == "publisher")
197  m_publisher = data;
198  else if (field == "crc_value")
199  m_crc_value = data;
200  else if (field == "inetref")
201  m_inetref = data;
202  else if (field == "diskcount")
203  m_diskcount = data.toInt();
204  else if (field == "gametype")
205  m_gametype = data;
206  else if (field == "romcount")
207  m_romcount = data.toInt();
208  else
209  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Invalid field %1").arg(field));
210 
211 }
212 
213 void RomInfo::setFavorite(bool updateDatabase)
214 {
216 
217  if (updateDatabase)
218  {
219  MSqlQuery query(MSqlQuery::InitCon());
220 
221  query.prepare("UPDATE gamemetadata SET favorite = :FAV "
222  "WHERE romname = :ROMNAME");
223 
224  query.bindValue(":FAV", m_favorite);
225  query.bindValue(":ROMNAME",m_romname);
226 
227  if (!query.exec())
228  {
229  MythDB::DBError("RomInfo::setFavorite", query);
230  }
231  }
232 }
233 
235 {
236  int pos = Romname().lastIndexOf(".");
237  if (pos == -1)
238  return nullptr;
239 
240  pos = Romname().length() - pos;
241  pos--;
242 
243  QString ext = Romname().right(pos);
244  return ext;
245 }
246 
248 {
249  if (m_gamename == "")
250  {
251  return;
252  }
253 
254  MSqlQuery query(MSqlQuery::InitCon());
255 
256  QString systemtype;
257  if (m_system != "") {
258  systemtype += " AND system = :SYSTEM ";
259  }
260 
261  QString thequery = "SELECT system,gamename,genre,year,romname,favorite,"
262  "rompath,country,crc_value,diskcount,gametype,plot,publisher,"
263  "version,screenshot,fanart,boxart,inetref,intid FROM gamemetadata "
264  "WHERE gamename = :GAMENAME "
265  + systemtype + " ORDER BY diskcount DESC";
266 
267  query.prepare(thequery);
268  query.bindValue(":SYSTEM", m_system);
269  query.bindValue(":GAMENAME", m_gamename);
270 
271  if (query.exec() && query.next())
272  {
273  setSystem(query.value(0).toString());
274  setGamename(query.value(1).toString());
275  setGenre(query.value(2).toString());
276  setYear(query.value(3).toString());
277  setRomname(query.value(4).toString());
278  setField("favorite",query.value(5).toString());
279  setRompath(query.value(6).toString());
280  setCountry(query.value(7).toString());
281  setCRC_VALUE(query.value(8).toString());
282  setDiskCount(query.value(9).toInt());
283  setGameType(query.value(10).toString());
284  setPlot(query.value(11).toString());
285  setPublisher(query.value(12).toString());
286  setVersion(query.value(13).toString());
287  setScreenshot(query.value(14).toString());
288  setFanart(query.value(15).toString());
289  setBoxart(query.value(16).toString());
290  setInetref(query.value(17).toString());
291  setId(query.value(18).toInt());
292  }
293 
295 
296  // If we have more than one instance of this rom in the DB fill in all
297  // systems available to play it.
298  if (RomCount() > 1)
299  {
300  query.prepare("SELECT DISTINCT system FROM gamemetadata "
301  "WHERE romname = :ROMNAME");
302  query.bindValue(":ROMNAME", Romname());
303  if (!query.exec())
304  MythDB::DBError("RomInfo::fillData - selecting systems", query);
305 
306  while (query.next())
307  {
308  if (m_allsystems.isEmpty())
309  m_allsystems = query.value(0).toString();
310  else
311  m_allsystems += "," + query.value(0).toString();
312  }
313  }
314  else
315  {
317  }
318 }
319 
320 QList<RomInfo*> RomInfo::GetAllRomInfo()
321 {
322  QList<RomInfo*> ret;
323 
324  MSqlQuery query(MSqlQuery::InitCon());
325 
326  QString querystr = "SELECT intid,system,romname,gamename,genre,year,publisher,"
327  "favorite,rompath,screenshot,fanart,plot,boxart,"
328  "gametype,diskcount,country,crc_value,inetref,display,"
329  "version FROM gamemetadata ORDER BY diskcount DESC";
330 
331  query.prepare(querystr);
332 
333  if (!query.exec())
334  {
335  MythDB::DBError("GetAllRomInfo", query);
336  return ret;
337  }
338 
339  while (query.next())
340  {
341  RomInfo *add = new RomInfo(
342  query.value(0).toInt(),
343  query.value(2).toString(),
344  query.value(1).toString(),
345  query.value(3).toString(),
346  query.value(4).toString(),
347  query.value(5).toString(),
348  query.value(7).toBool(),
349  query.value(8).toString(),
350  query.value(15).toString(),
351  query.value(16).toString(),
352  query.value(14).toInt(),
353  query.value(13).toString(),
354  0, QString(),
355  query.value(11).toString(),
356  query.value(6).toString(),
357  query.value(19).toString(),
358  query.value(9).toString(),
359  query.value(10).toString(),
360  query.value(12).toString(),
361  query.value(17).toString());
362  ret.append(add);
363  }
364 
365  return ret;
366 }
367 
369 {
370  RomInfo *ret = nullptr;
371 
372  MSqlQuery query(MSqlQuery::InitCon());
373 
374  QString querystr = "SELECT intid,system,romname,gamename,genre,year,publisher,"
375  "favorite,rompath,screenshot,fanart,plot,boxart,"
376  "gametype,diskcount,country,crc_value,inetref,display,"
377  "version FROM gamemetadata WHERE intid = :INTID";
378 
379  query.prepare(querystr);
380  query.bindValue(":INTID", id);
381 
382  if (!query.exec())
383  MythDB::DBError("GetRomInfoById", query);
384 
385  if (query.next())
386  {
387  ret = new RomInfo(
388  query.value(0).toInt(),
389  query.value(2).toString(),
390  query.value(1).toString(),
391  query.value(3).toString(),
392  query.value(4).toString(),
393  query.value(5).toString(),
394  query.value(7).toBool(),
395  query.value(8).toString(),
396  query.value(15).toString(),
397  query.value(16).toString(),
398  query.value(14).toInt(),
399  query.value(13).toString(),
400  0, QString(),
401  query.value(11).toString(),
402  query.value(6).toString(),
403  query.value(19).toString(),
404  query.value(9).toString(),
405  query.value(10).toString(),
406  query.value(12).toString(),
407  query.value(17).toString());
408  }
409 
410  if (!ret)
411  ret = new RomInfo();
412 
413  return ret;
414 }
415 
417 {
418  return QString ("Rom Info:\n"
419  "ID: %1\n"
420  "Game Name: %2\n"
421  "Rom Name: %3\n"
422  "Rom Path: %4")
423  .arg(Id()).arg(Gamename())
424  .arg(Romname()).arg(Rompath());
425 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
QString m_year
Definition: rominfo.h:163
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
void setRomCount(const int &lromcount)
Definition: rominfo.h:110
void setPlot(const QString &lplot)
Definition: rominfo.h:122
int romInDB(const QString &rom, const QString &gametype)
Definition: rominfo.cpp:114
QString m_publisher
Definition: rominfo.h:159
QString Screenshot() const
Definition: rominfo.h:82
void fillData()
Definition: rominfo.cpp:247
void DeleteFromDatabase()
Definition: rominfo.cpp:94
void setGameType(const QString &lgametype)
Definition: rominfo.h:107
QString Plot() const
Definition: rominfo.h:121
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
QString Gamename() const
Definition: rominfo.h:97
QString m_gametype
Definition: rominfo.h:156
bool FindImage(QString BaseFileName, QString *result)
Definition: rominfo.cpp:141
QString Country() const
Definition: rominfo.h:103
QString Romname() const
Definition: rominfo.h:91
void setBoxart(const QString &lboxart)
Definition: rominfo.h:89
QString m_inetref
Definition: rominfo.h:169
QString m_romname
Definition: rominfo.h:150
QString m_genre
Definition: rominfo.h:153
void setId(const int &lid)
Definition: rominfo.h:77
void setInetref(const QString &linetref)
Definition: rominfo.h:134
QString Version() const
Definition: rominfo.h:127
void setVersion(const QString &lversion)
Definition: rominfo.h:128
unsigned char b
Definition: ParseText.cpp:329
QVariant value(int i) const
Definition: mythdbcon.h:198
void setPublisher(const QString &lpublisher)
Definition: rominfo.h:125
QString m_boxart
Definition: rominfo.h:168
void setField(const QString &field, const QString &data)
Definition: rominfo.cpp:172
QString m_allsystems
Definition: rominfo.h:157
int RomCount() const
Definition: rominfo.h:109
QString m_plot
Definition: rominfo.h:158
void setScreenshot(const QString &lscreenshot)
Definition: rominfo.h:83
QString m_crc_value
Definition: rominfo.h:155
int m_id
Definition: rominfo.h:149
void setRompath(const QString &lrompath)
Definition: rominfo.h:80
QString toString()
Definition: rominfo.cpp:416
void setDiskCount(const int &ldiskcount)
Definition: rominfo.h:116
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
bool operator==(const RomInfo &a, const RomInfo &b)
Definition: rominfo.cpp:12
int m_diskcount
Definition: rominfo.h:162
void setFavorite(bool updateDatabase=false)
Definition: rominfo.cpp:213
QString Year() const
Definition: rominfo.h:130
QString m_gamename
Definition: rominfo.h:152
static RomInfo * GetRomInfoById(int id)
Definition: rominfo.cpp:368
int m_romcount
Definition: rominfo.h:161
bool m_favorite
Definition: rominfo.h:164
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static QList< RomInfo * > GetAllRomInfo()
Definition: rominfo.cpp:320
QString Rompath() const
Definition: rominfo.h:79
void setRomname(const QString &lromname)
Definition: rominfo.h:92
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:16
QString Boxart() const
Definition: rominfo.h:88
QString Genre() const
Definition: rominfo.h:100
void setCountry(const QString &lcountry)
Definition: rominfo.h:104
QString Fanart() const
Definition: rominfo.h:85
void setFanart(const QString &lfanart)
Definition: rominfo.h:86
QString m_country
Definition: rominfo.h:154
QString Inetref() const
Definition: rominfo.h:133
QString GameType() const
Definition: rominfo.h:106
int Id() const
Definition: rominfo.h:76
void setYear(const QString &lyear)
Definition: rominfo.h:131
QString m_fanart
Definition: rominfo.h:167
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 Publisher() const
Definition: rominfo.h:124
QString m_system
Definition: rominfo.h:151
void setGenre(const QString &lgenre)
Definition: rominfo.h:101
#define LOC
Definition: rominfo.cpp:10
QString m_screenshot
Definition: rominfo.h:166
void SaveToDatabase()
Definition: rominfo.cpp:17
void setGamename(const QString &lgamename)
Definition: rominfo.h:98
QString System() const
Definition: rominfo.h:94
QString getExtension()
Definition: rominfo.cpp:234
int Favorite() const
Definition: rominfo.h:136
void setSystem(const QString &lsystem)
Definition: rominfo.h:95
void setCRC_VALUE(const QString &lcrc_value)
Definition: rominfo.h:119
QString m_rompath
Definition: rominfo.h:165