MythTV  master
videometadatautil.cpp
Go to the documentation of this file.
1 #include <QReadWriteLock>
2 #include <QHash>
3 #include <QUrl>
4 
5 #include "videometadatautil.h"
6 #include "mythlogging.h"
7 #include "mythdb.h"
8 
9 #define LOC QString("VideoMetaDataUtil: ")
10 
11 static QReadWriteLock art_path_map_lock;
12 using ArtPair = QPair< QString, QString >;
13 static QMultiHash<QString, ArtPair> art_path_map;
14 using ArtList = QList<ArtPair>;
15 
16 QString VideoMetaDataUtil::GetArtPath(const QString &pathname,
17  const QString &type)
18 {
19  QString basename = pathname.section('/', -1);
20 
21  if (basename == pathname)
22  {
23  LOG(VB_GENERAL, LOG_WARNING, LOC +
24  "Programmer Error: Cannot determine art path\n\t\t\t"
25  "until the ProgramInfo pathname has been fully resolved.");
26  return QString();
27  }
28 
29  art_path_map_lock.lockForRead();
30  ArtList ret(art_path_map.values(basename));
31  art_path_map_lock.unlock();
32  for (const auto & [arttype, artpath] : qAsConst(ret))
33  {
34  if (arttype == type)
35  return artpath;
36  }
37 
38  QString fn = basename;
39  fn.prepend("%");
40 
41  QString dbcolumn;
42  if (type == "Coverart")
43  dbcolumn = "coverfile";
44  else if (type == "Fanart")
45  dbcolumn = "fanart";
46  else if (type == "Banners")
47  dbcolumn = "banner";
48  else if (type == "Screenshots")
49  dbcolumn = "screenshot";
50 
51  QString querystr = QString("SELECT %1 "
52  "FROM videometadata WHERE filename "
53  "LIKE :FILENAME").arg(dbcolumn);
54 
56  query.prepare(querystr);
57  query.bindValue(":FILENAME", fn);
58 
59  QString artpath;
60  if (query.exec() && query.next())
61  artpath = query.value(0).toString();
62 
63  if (!artpath.startsWith('/') && pathname.startsWith("myth://"))
64  {
65  const QString& workURL = pathname;
66  QUrl baseURL(workURL);
67  baseURL.setUserName(type);
68  QString finalURL =
69  baseURL.toString(QUrl::RemovePath) + '/' + artpath;
70  artpath = finalURL;
71  }
72 
73  ArtPair ins(type, artpath);
74  art_path_map_lock.lockForWrite();
75  art_path_map.insert(basename, ins);
76  art_path_map_lock.unlock();
77 
78  return artpath;
79 }
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
mythdb.h
ArtList
QList< ArtPair > ArtList
Definition: videometadatautil.cpp:14
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythlogging.h
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
art_path_map
static QMultiHash< QString, ArtPair > art_path_map
Definition: videometadatautil.cpp:13
videometadatautil.h
ArtPair
QPair< QString, QString > ArtPair
Definition: videometadatautil.cpp:12
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
LOC
#define LOC
Definition: videometadatautil.cpp:9
VideoMetaDataUtil::GetArtPath
static QString GetArtPath(const QString &pathname, const QString &type)
Definition: videometadatautil.cpp:16
art_path_map_lock
static QReadWriteLock art_path_map_lock
Definition: videometadatautil.cpp:11
query
MSqlQuery query(MSqlQuery::InitCon())
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808