MythTV  master
metadataimagehelper.cpp
Go to the documentation of this file.
1 
2 #include "metadataimagehelper.h"
3 
4 #include <QUrl>
5 
6 #include "mythdirs.h"
7 #include "mythdbcon.h"
8 #include "mythcorecontext.h"
9 #include "storagegroup.h"
10 
11 // a helper functions that is used only in this file
12 namespace {
13  QString generate_myth_url(const QString &storage_group, const QString &host,
14  const QString &path)
15  {
17 
18  return gCoreContext->GenMythURL(host, port, path,
19  StorageGroup::GetGroupToUse(host, storage_group));
20  }
21 }
22 
23 ArtworkMap GetArtwork(const QString& inetref,
24  uint season,
25  bool strict)
26 {
27  ArtworkMap map;
28 
29  if (inetref.isEmpty())
30  return map;
31 
33 
34  QString querystring = "SELECT host, coverart, fanart, banner "
35  "FROM recordedartwork WHERE inetref = :INETREF ";
36 
37  if (strict)
38  querystring += "AND season = :SEASON;";
39  else
40  {
41  if (season > 0)
42  {
43  querystring += "ORDER BY season = :SEASON DESC, season DESC;";
44  }
45  else
46  querystring += "ORDER BY season DESC;";
47  }
48 
49  query.prepare(querystring);
50 
51  query.bindValue(":INETREF", inetref);
52  if (strict || season > 0)
53  query.bindValue(":SEASON", season);
54 
55  if (!query.exec())
56  {
57  MythDB::DBError("GetArtwork SELECT", query);
58  return map;
59  }
60 
61  if (query.next())
62  {
63  QString host = query.value(0).toString();
64  QString coverart = query.value(1).toString();
65  QString fanart = query.value(2).toString();
66  QString banner = query.value(3).toString();
67 
68  if (!coverart.isEmpty())
69  {
70  ArtworkInfo coverartinfo;
71  coverartinfo.url = generate_myth_url("Coverart", host, coverart);
72  map.insert(kArtworkCoverart, coverartinfo);
73  }
74 
75  if (!fanart.isEmpty())
76  {
77  ArtworkInfo fanartinfo;
78  fanartinfo.url = generate_myth_url("Fanart", host, fanart);
79  map.insert(kArtworkFanart, fanartinfo);
80  }
81 
82  if (!banner.isEmpty())
83  {
84  ArtworkInfo bannerinfo;
85  bannerinfo.url = generate_myth_url("Banners", host, banner);
86  map.insert(kArtworkBanner, bannerinfo);
87  }
88  }
89 
90  return map;
91 }
92 
93 bool SetArtwork(const QString &inetref,
94  uint season,
95  const QString &host,
96  const QString &coverart,
97  const QString &fanart,
98  const QString &banner)
99 {
100  bool ret = false;
101  ArtworkMap map;
102 
103  if (!coverart.isEmpty())
104  {
105  ArtworkInfo coverartinfo;
106  coverartinfo.url = generate_myth_url("Coverart", host, coverart);
107  map.insert(kArtworkCoverart, coverartinfo);
108  }
109 
110  if (!fanart.isEmpty())
111  {
112  ArtworkInfo fanartinfo;
113  fanartinfo.url = generate_myth_url("Fanart", host, fanart);
114  map.insert(kArtworkFanart, fanartinfo);
115  }
116 
117  if (!banner.isEmpty())
118  {
119  ArtworkInfo bannerinfo;
120  bannerinfo.url = generate_myth_url("Banners", host, banner);
121  map.insert(kArtworkBanner, bannerinfo);
122  }
123 
124  ret = SetArtwork(inetref, season, host, map);
125 
126  return ret;
127 }
128 
129 bool SetArtwork(const QString &inetref,
130  uint season,
131  const QString &host,
132  const ArtworkMap& map)
133 {
134  if (inetref.isEmpty())
135  return false;
136 
137  QString coverart, fanart, banner;
138 
139  QUrl coverurl(map.value(kArtworkCoverart).url);
140  if (!coverurl.path().isEmpty())
141  {
142  coverart = coverurl.path();
143  coverart = coverart.remove(0,1);
144  }
145 
146  QUrl fanarturl(map.value(kArtworkFanart).url);
147  if (!fanarturl.path().isEmpty())
148  {
149  fanart = fanarturl.path();
150  fanart = fanart.remove(0,1);
151  }
152 
153  QUrl bannerurl(map.value(kArtworkBanner).url);
154  if (!bannerurl.path().isEmpty())
155  {
156  banner = bannerurl.path();
157  banner = banner.remove(0,1);
158  }
159 
160  // Have to delete the old row for this item
161 
162  MSqlQuery prequery(MSqlQuery::InitCon());
163  prequery.prepare("DELETE FROM recordedartwork WHERE "
164  "inetref = :INETREF AND season = :SEASON;");
165 
166  prequery.bindValue(":INETREF", inetref);
167  prequery.bindValue(":SEASON", season);
168 
169  if (!prequery.exec())
170  {
171  MythDB::DBError("SetArtwork DELETE FROM", prequery);
172  return false;
173  }
174 
175  // Now we can insert the new
176  MSqlQuery query(MSqlQuery::InitCon());
177  query.prepare("INSERT INTO recordedartwork(inetref,"
178  "season,host,coverart,fanart,banner) VALUES( "
179  ":INETREF, :SEASON, :HOST, :COVERART, "
180  ":FANART, :BANNER);");
181 
182  query.bindValue(":INETREF", inetref);
183  query.bindValue(":SEASON", season);
184  query.bindValue(":HOST", host);
185  query.bindValue(":COVERART", coverart.isNull() ? "" : coverart);
186  query.bindValue(":FANART", fanart.isNull() ? "" : fanart);
187  query.bindValue(":BANNER", banner.isNull() ? "" : banner);
188 
189  if (!query.exec())
190  {
191  MythDB::DBError("SetArtwork INSERT INTO", query);
192  return false;
193  }
194 
195  return true;
196 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
bool SetArtwork(const QString &inetref, uint season, const QString &host, const QString &coverart, const QString &fanart, const QString &banner)
unsigned int uint
Definition: compat.h:140
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
int GetBackendServerPort(void)
Returns the locally defined backend control port.
QVariant value(int i) const
Definition: mythdbcon.h:198
QString GenMythURL(const QString &host=QString(), int port=0, QString path=QString(), const QString &storageGroup=QString())
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
static QString GetGroupToUse(const QString &host, const QString &sgroup)
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
QMultiMap< VideoArtworkType, ArtworkInfo > ArtworkMap
ArtworkMap GetArtwork(const QString &inetref, uint season, bool strict)