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 MythCoreContext::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;
138  QString fanart;
139  QString banner;
140 
141  QUrl coverurl(map.value(kArtworkCoverart).url);
142  if (!coverurl.path().isEmpty())
143  {
144  coverart = coverurl.path();
145  coverart = coverart.remove(0,1);
146  }
147 
148  QUrl fanarturl(map.value(kArtworkFanart).url);
149  if (!fanarturl.path().isEmpty())
150  {
151  fanart = fanarturl.path();
152  fanart = fanart.remove(0,1);
153  }
154 
155  QUrl bannerurl(map.value(kArtworkBanner).url);
156  if (!bannerurl.path().isEmpty())
157  {
158  banner = bannerurl.path();
159  banner = banner.remove(0,1);
160  }
161 
162  // Have to delete the old row for this item
163 
164  MSqlQuery prequery(MSqlQuery::InitCon());
165  prequery.prepare("DELETE FROM recordedartwork WHERE "
166  "inetref = :INETREF AND season = :SEASON;");
167 
168  prequery.bindValue(":INETREF", inetref);
169  prequery.bindValue(":SEASON", season);
170 
171  if (!prequery.exec())
172  {
173  MythDB::DBError("SetArtwork DELETE FROM", prequery);
174  return false;
175  }
176 
177  // Now we can insert the new
178  MSqlQuery query(MSqlQuery::InitCon());
179  query.prepare("INSERT INTO recordedartwork(inetref,"
180  "season,host,coverart,fanart,banner) VALUES( "
181  ":INETREF, :SEASON, :HOST, :COVERART, "
182  ":FANART, :BANNER);");
183 
184  query.bindValue(":INETREF", inetref);
185  query.bindValue(":SEASON", season);
186  query.bindValue(":HOST", host);
187  query.bindValue(":COVERART", coverart.isNull() ? "" : coverart);
188  query.bindValue(":FANART", fanart.isNull() ? "" : fanart);
189  query.bindValue(":BANNER", banner.isNull() ? "" : banner);
190 
191  if (!query.exec())
192  {
193  MythDB::DBError("SetArtwork INSERT INTO", query);
194  return false;
195  }
196 
197  return true;
198 }
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)
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
static QString GenMythURL(const QString &host=QString(), int port=0, QString path=QString(), const QString &storageGroup=QString())
unsigned int uint
Definition: compat.h:140
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)