MythTV  0.26-pre
rominfo.cpp
Go to the documentation of this file.
00001 #include <QFile>
00002 
00003 #include <mythdb.h>
00004 #include <mythcontext.h>
00005 
00006 #include "rominfo.h"
00007 #include "romedit.h"
00008 
00009 #define LOC_ERR QString("MythGame:ROMINFO Error: ")
00010 #define LOC QString("MythGame:ROMINFO: ")
00011 
00012 bool operator==(const RomInfo& a, const RomInfo& b)
00013 {
00014     if (a.Romname() == b.Romname())
00015         return true;
00016     return false;
00017 }
00018 
00019 void RomInfo::SaveToDatabase()
00020 {
00021     MSqlQuery query(MSqlQuery::InitCon());
00022 
00023     bool inserting = false;
00024 
00025     if (id == 0)
00026         inserting = true;
00027 
00028     if (inserting)
00029     {
00030         LOG(VB_GENERAL, LOG_INFO, LOC + QString("Adding %1 - %2").arg(Rompath())
00031             .arg(Romname()));
00032 
00033         query.prepare("INSERT INTO gamemetadata "
00034                       "(system, romname, gamename, genre, year, gametype, "
00035                       "rompath, country, crc_value, diskcount, display, plot, "
00036                       "publisher, version, fanart, boxart, screenshot) "
00037                       "VALUES (:SYSTEM, :ROMNAME, :GAMENAME, :GENRE, :YEAR, "
00038                       ":GAMETYPE, :ROMPATH, :COUNTRY, :CRC32, '1', '1', :PLOT, "
00039                       ":PUBLISHER, :VERSION, :FANART, :BOXART, :SCREENSHOT)");
00040 
00041         query.bindValue(":SYSTEM",System());
00042         query.bindValue(":ROMNAME",Romname());
00043         query.bindValue(":GAMENAME",Gamename());
00044         query.bindValue(":GENRE",Genre());
00045         query.bindValue(":YEAR",Year());
00046         query.bindValue(":GAMETYPE",GameType());
00047         query.bindValue(":ROMPATH",Rompath());
00048         query.bindValue(":COUNTRY",Country());
00049         query.bindValue(":CRC32", QString());
00050         query.bindValue(":PLOT", Plot());
00051         query.bindValue(":PUBLISHER", Publisher());
00052         query.bindValue(":VERSION", Version());
00053         query.bindValue(":FANART", Fanart());
00054         query.bindValue(":BOXART", Boxart());
00055         query.bindValue(":SCREENSHOT", Screenshot());
00056     }
00057     else
00058     {
00059         query.prepare("UPDATE gamemetadata "
00060                       "SET version = 'CUSTOM', "
00061                       "    gamename = :GAMENAME,"
00062                       "    genre = :GENRE,"
00063                       "    year = :YEAR,"
00064                       "    country = :COUNTRY,"
00065                       "    plot = :PLOT,"
00066                       "    publisher = :PUBLISHER,"
00067                       "    favorite = :FAVORITE,"
00068                       "    screenshot = :SCREENSHOT,"
00069                       "    fanart = :FANART,"
00070                       "    boxart = :BOXART, "
00071                       "    inetref = :INETREF "
00072                       "WHERE gametype = :GAMETYPE AND "
00073                       "      romname  = :ROMNAME");
00074         query.bindValue(":GAMENAME", Gamename());
00075         query.bindValue(":GENRE", Genre());
00076         query.bindValue(":YEAR", Year());
00077         query.bindValue(":COUNTRY", Country());
00078         query.bindValue(":PLOT", Plot());
00079         query.bindValue(":PUBLISHER", Publisher());
00080         query.bindValue(":FAVORITE", Favorite());
00081         query.bindValue(":SCREENSHOT", Screenshot());
00082         query.bindValue(":FANART", Fanart());
00083         query.bindValue(":BOXART", Boxart());
00084         query.bindValue(":INETREF", Inetref());
00085         query.bindValue(":GAMETYPE", GameType());
00086         query.bindValue(":ROMNAME", Romname());
00087     }
00088 
00089     if (!query.exec())
00090     {
00091         MythDB::DBError("RomInfo::SaveToDatabase", query);
00092         return;
00093     }
00094 }
00095 
00096 void RomInfo::DeleteFromDatabase()
00097 {
00098     LOG(VB_GENERAL, LOG_INFO, LOC + QString("Removing %1 - %2").arg(Rompath())
00099             .arg(Romname()));
00100 
00101     MSqlQuery query(MSqlQuery::InitCon());
00102 
00103     query.prepare("DELETE FROM gamemetadata WHERE "
00104                   "romname = :ROMNAME AND "
00105                   "rompath = :ROMPATH ");
00106 
00107     query.bindValue(":ROMNAME",Romname());
00108     query.bindValue(":ROMPATH",Rompath());
00109 
00110     if (!query.exec())
00111         MythDB::DBError("purgeGameDB", query);
00112 
00113 }
00114 
00115 // Return the count of how many times this appears in the db already
00116 int romInDB(QString rom, QString gametype)
00117 {
00118     MSqlQuery query(MSqlQuery::InitCon());
00119 
00120     int count = 0;
00121 
00122     query.prepare("SELECT count(*) FROM gamemetadata "
00123                   "WHERE gametype = :GAMETYPE "
00124                   "AND romname = :ROMNAME");
00125 
00126     query.bindValue(":GAMETYPE", gametype);
00127     query.bindValue(":ROMNAME", rom);
00128 
00129     if (!query.exec())
00130     {
00131         MythDB::DBError("romInDB", query);
00132         return -1;
00133     }
00134 
00135 
00136     if (query.next())
00137         count = query.value(0).toInt();
00138 
00139     return count;
00140 }
00141 
00142 
00143 bool RomInfo::FindImage(QString BaseFileName, QString *result)
00144 {
00145     QStringList graphic_formats;
00146     graphic_formats.append("png");
00147     graphic_formats.append("gif");
00148     graphic_formats.append("jpg");
00149     graphic_formats.append("jpeg");
00150     graphic_formats.append("xpm");
00151     graphic_formats.append("bmp");
00152     graphic_formats.append("pnm");
00153     graphic_formats.append("tif");
00154     graphic_formats.append("tiff");
00155 
00156     int dotLocation = BaseFileName.lastIndexOf('.');
00157     if (dotLocation == -1)
00158     {
00159         BaseFileName.append('.');
00160         dotLocation = BaseFileName.length();
00161     }
00162 
00163 
00164     BaseFileName.truncate(dotLocation + 1);
00165     for (QStringList::Iterator i = graphic_formats.begin(); i != graphic_formats.end(); i++)
00166     {
00167         *result = BaseFileName + *i;
00168         if (QFile::exists(*result))
00169             return true;
00170     }
00171     return false;
00172 }
00173 
00174 void RomInfo::setField(QString field, QString data)
00175 {
00176     if (field == "system")
00177         system = data;
00178     else if (field == "gamename")
00179         gamename = data;
00180     else if (field == "genre")
00181         genre = data;
00182     else if (field == "year")
00183         year = data;
00184     else if (field == "favorite")
00185         favorite = data.toInt();
00186     else if (field == "rompath")
00187         rompath = data;
00188     else if (field == "screenshot")
00189         screenshot = data;
00190     else if (field == "fanart")
00191         fanart = data;
00192     else if (field == "boxart")
00193         boxart = data;
00194     else if (field == "country")
00195         country = data;
00196     else if (field == "plot")
00197         plot = data;
00198     else if (field == "publisher")
00199         publisher = data;
00200     else if (field == "crc_value")
00201         crc_value = data;
00202     else if (field == "inetref")
00203         inetref = data;
00204     else if (field == "diskcount")
00205         diskcount = data.toInt();
00206     else if (field == "gametype")
00207         gametype = data;
00208     else if (field == "romcount")
00209         romcount = data.toInt();
00210     else
00211         LOG(VB_GENERAL, LOG_ERR, LOC + QString("Invalid field %1").arg(field));
00212 
00213 }
00214 
00215 void RomInfo::setFavorite(bool updateDatabase)
00216 {
00217     favorite = 1 - favorite;
00218 
00219     if (updateDatabase)
00220     {
00221         MSqlQuery query(MSqlQuery::InitCon());
00222 
00223         query.prepare("UPDATE gamemetadata SET favorite = :FAV "
00224                       "WHERE romname = :ROMNAME");
00225 
00226         query.bindValue(":FAV", favorite);
00227         query.bindValue(":ROMNAME",romname);
00228 
00229         if (!query.exec())
00230         {
00231             MythDB::DBError("RomInfo::setFavorite", query);
00232         }
00233     }
00234 }
00235 
00236 QString RomInfo::getExtension()
00237 {
00238     int pos = Romname().lastIndexOf(".");
00239     if (pos == -1)
00240         return NULL;
00241 
00242     pos = Romname().length() - pos;
00243     pos--;
00244 
00245     QString ext = Romname().right(pos);
00246     return ext;
00247 }
00248 
00249 void RomInfo::fillData()
00250 {
00251     if (gamename == "")
00252     {
00253         return;
00254     }
00255 
00256     MSqlQuery query(MSqlQuery::InitCon());
00257 
00258     QString systemtype;
00259     if (system != "") {
00260         systemtype  += " AND system = :SYSTEM ";
00261     }
00262 
00263     QString thequery = "SELECT system,gamename,genre,year,romname,favorite,"
00264                        "rompath,country,crc_value,diskcount,gametype,plot,publisher,"
00265                        "version,screenshot,fanart,boxart,inetref,intid FROM gamemetadata "
00266                        "WHERE gamename = :GAMENAME "
00267                        + systemtype + " ORDER BY diskcount DESC";
00268 
00269     query.prepare(thequery);
00270     query.bindValue(":SYSTEM", system);
00271     query.bindValue(":GAMENAME", gamename);
00272 
00273     if (query.exec() && query.next())
00274     {
00275         setSystem(query.value(0).toString());
00276         setGamename(query.value(1).toString());
00277         setGenre(query.value(2).toString());
00278         setYear(query.value(3).toString());
00279         setRomname(query.value(4).toString());
00280         setField("favorite",query.value(5).toString());
00281         setRompath(query.value(6).toString());
00282         setCountry(query.value(7).toString());
00283         setCRC_VALUE(query.value(8).toString());
00284         setDiskCount(query.value(9).toInt());
00285         setGameType(query.value(10).toString());
00286         setPlot(query.value(11).toString());
00287         setPublisher(query.value(12).toString());
00288         setVersion(query.value(13).toString());
00289         setScreenshot(query.value(14).toString());
00290         setFanart(query.value(15).toString());
00291         setBoxart(query.value(16).toString());
00292         setInetref(query.value(17).toString());
00293         setId(query.value(18).toInt());
00294     }
00295 
00296     setRomCount(romInDB(romname,gametype));
00297 
00298     // If we have more than one instance of this rom in the DB fill in all
00299     // systems available to play it.
00300     if (RomCount() > 1)
00301     {
00302         query.prepare("SELECT DISTINCT system FROM gamemetadata "
00303                       "WHERE romname = :ROMNAME");
00304         query.bindValue(":ROMNAME", Romname());
00305         if (!query.exec())
00306             MythDB::DBError("RomInfo::fillData - selecting systems", query);
00307 
00308         while (query.next())
00309         {
00310             if (allsystems.isEmpty())
00311                 allsystems = query.value(0).toString();
00312             else
00313                 allsystems += "," + query.value(0).toString();
00314         }
00315     }
00316     else
00317     {
00318         allsystems = system;
00319     }
00320 }
00321 
00322 QList<RomInfo*> RomInfo::GetAllRomInfo()
00323 {
00324     QList<RomInfo*> ret;
00325 
00326     MSqlQuery query(MSqlQuery::InitCon());
00327 
00328     QString querystr = "SELECT intid,system,romname,gamename,genre,year,publisher,"
00329                        "favorite,rompath,screenshot,fanart,plot,boxart,"
00330                        "gametype,diskcount,country,crc_value,inetref,display,"
00331                        "version FROM gamemetadata ORDER BY diskcount DESC";
00332 
00333     query.prepare(querystr);
00334 
00335     if (!query.exec())
00336     {
00337         MythDB::DBError("GetAllRomInfo", query);
00338         return ret;
00339     }
00340 
00341     while (query.next())
00342     {
00343         RomInfo *add = new RomInfo(
00344                            query.value(0).toInt(),
00345                            query.value(2).toString(),
00346                            query.value(1).toString(),
00347                            query.value(3).toString(),
00348                            query.value(4).toString(),
00349                            query.value(5).toString(),
00350                            query.value(7).toBool(),
00351                            query.value(8).toString(),
00352                            query.value(15).toString(),
00353                            query.value(16).toString(),
00354                            query.value(14).toInt(),
00355                            query.value(13).toString(),
00356                            0, QString(),
00357                            query.value(11).toString(),
00358                            query.value(6).toString(),
00359                            query.value(19).toString(),
00360                            query.value(9).toString(),
00361                            query.value(10).toString(),
00362                            query.value(12).toString(),
00363                            query.value(17).toString());
00364         ret.append(add);
00365     }
00366 
00367     return ret;
00368 }
00369 
00370 RomInfo *RomInfo::GetRomInfoById(int id)
00371 {
00372     RomInfo *ret = new RomInfo();
00373 
00374     MSqlQuery query(MSqlQuery::InitCon());
00375 
00376     QString querystr = "SELECT intid,system,romname,gamename,genre,year,publisher,"
00377                        "favorite,rompath,screenshot,fanart,plot,boxart,"
00378                        "gametype,diskcount,country,crc_value,inetref,display,"
00379                        "version FROM gamemetadata WHERE intid = :INTID";
00380 
00381     query.prepare(querystr);
00382     query.bindValue(":INTID", id);
00383 
00384     if (!query.exec())
00385     {
00386         MythDB::DBError("GetRomInfoById", query);
00387         return ret;
00388     }
00389 
00390     if (query.next())
00391     {
00392         ret = new RomInfo(
00393                            query.value(0).toInt(),
00394                            query.value(2).toString(),
00395                            query.value(1).toString(),
00396                            query.value(3).toString(),
00397                            query.value(4).toString(),
00398                            query.value(5).toString(),
00399                            query.value(7).toBool(),
00400                            query.value(8).toString(),
00401                            query.value(15).toString(),
00402                            query.value(16).toString(),
00403                            query.value(14).toInt(),
00404                            query.value(13).toString(),
00405                            0, QString(),
00406                            query.value(11).toString(),
00407                            query.value(6).toString(),
00408                            query.value(19).toString(),
00409                            query.value(9).toString(),
00410                            query.value(10).toString(),
00411                            query.value(12).toString(),
00412                            query.value(17).toString());
00413     }
00414 
00415     return ret;
00416 }
00417 
00418 QString RomInfo::toString()
00419 {
00420     return QString ("Rom Info:\n"
00421                "ID: %1\n"
00422                "Game Name: %2\n"
00423                "Rom Name: %3\n"
00424                "Rom Path: %4")
00425                .arg(Id()).arg(Gamename())
00426                .arg(Romname()).arg(Rompath());
00427 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends