MythTV  master
mythuimetadataresults.cpp
Go to the documentation of this file.
1 
3 
4 #include "libmythbase/mythdate.h"
5 #include "libmythbase/mythdirs.h"
11 #include "libmythui/mythuitext.h"
12 
13 #include "metadatadownload.h"
14 #include "metadataimagedownload.h"
15 
17  MythScreenStack *lparent,
18  const MetadataLookupList &results) :
19 
20  MythScreenType(lparent, "metadataresultspopup"),
21  m_results(results)
22 {
24 }
25 
27 {
28  cleanCacheDir();
29 
30  if (m_imageDownload)
31  {
32  delete m_imageDownload;
33  m_imageDownload = nullptr;
34  }
35 }
36 
38 {
39  if (!LoadWindowFromXML("base.xml", "MythMetadataResults", this))
40  return false;
41 
42  bool err = false;
43  UIUtilE::Assign(this, m_resultsList, "results", &err);
44  if (err)
45  {
46  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'MythMetadataResults'");
47  return false;
48  }
49 
50  for (int i = 0;
51  i != m_results.count(); ++i)
52  {
53  auto *button = new MythUIButtonListItem(m_resultsList,
54  m_results[i]->GetTitle());
55  InfoMap metadataMap;
56  m_results[i]->toMap(metadataMap);
57 
58  QString coverartfile;
59  ArtworkList art = m_results[i]->GetArtwork(kArtworkCoverart);
60  if (art.count() > 0)
61  coverartfile = art.takeFirst().thumbnail;
62 
63  if (coverartfile.isEmpty())
64  {
65  art = m_results[i]->GetArtwork(kArtworkBanner);
66  if (art.count() > 0)
67  coverartfile = art.takeFirst().thumbnail;
68  }
69 
70  if (coverartfile.isEmpty())
71  {
72  art = m_results[i]->GetArtwork(kArtworkScreenshot);
73  if (art.count() > 0)
74  coverartfile = art.takeFirst().thumbnail;
75  }
76 
77  QString dlfile = getDownloadFilename(m_results[i]->GetTitle(),
78  coverartfile);
79 
80  if (!coverartfile.isEmpty())
81  {
82  int pos = m_resultsList->GetItemPos(button);
83 
84  if (QFile::exists(dlfile))
85  button->SetImage(dlfile);
86  else
87  {
88  m_imageDownload->addThumb(m_results[i]->GetTitle(),
89  coverartfile,
90  QVariant::fromValue<uint>(pos));
91  }
92  }
93 
94  button->SetTextFromMap(metadataMap);
95  button->SetData(QVariant::fromValue<uint>(i));
96  }
97 
100 
101  BuildFocusList();
102 
103  return true;
104 }
105 
107 {
108  QString cache = QString("%1/cache/metadata-thumbcache")
109  .arg(GetConfDir());
110  QDir cacheDir(cache);
111  QStringList thumbs = cacheDir.entryList(QDir::Files);
112 
113  for (auto i = thumbs.crbegin(); i != thumbs.crend(); ++i)
114  {
115  QString filename = QString("%1/%2").arg(cache, *i);
116  QFileInfo fi(filename);
117  QDateTime lastmod = fi.lastModified();
118  if (lastmod.addDays(2) < MythDate::current())
119  {
120  LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting old cache file %1")
121  .arg(filename));
122  QFile::remove(filename);
123  }
124  }
125 }
126 
128 {
129  if (event->type() == ThumbnailDLEvent::kEventType)
130  {
131  auto *tde = dynamic_cast<ThumbnailDLEvent *>(event);
132  if (tde == nullptr)
133  return;
134 
135  ThumbnailData *data = tde->m_thumb;
136 
137  QString file = data->url;
138  uint pos = data->data.value<uint>();
139 
140  if (file.isEmpty())
141  return;
142 
143  if (!((uint)m_resultsList->GetCount() >= pos))
144  return;
145 
146  MythUIButtonListItem *item =
147  m_resultsList->GetItemAt(pos);
148 
149  if (item)
150  {
151  item->SetImage(file);
152  }
153  }
154 }
155 
157 {
159  m_results.clear();
160  emit haveResult(lookup);
161  Close();
162 }
MythUIButtonList::GetItemAt
MythUIButtonListItem * GetItemAt(int pos) const
Definition: mythuibuttonlist.cpp:1677
mythuimetadataresults.h
RefCountHandler
Definition: referencecounterlist.h:17
mythuitext.h
MetadataImageDownload
Definition: metadataimagedownload.h:87
MetadataResultsDialog::customEvent
void customEvent(QEvent *event) override
Definition: mythuimetadataresults.cpp:127
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:386
ArtworkList
QList< ArtworkInfo > ArtworkList
Definition: metadataimagehelper.h:30
ThumbnailDLEvent::kEventType
static const Type kEventType
Definition: metadataimagedownload.h:84
mythdialogbox.h
MythScreenStack
Definition: mythscreenstack.h:16
MetadataResultsDialog::m_results
MetadataLookupList m_results
Definition: mythuimetadataresults.h:25
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
MetadataImageDownload::addThumb
void addThumb(QString title, QString url, QVariant data)
Definition: metadataimagedownload.cpp:36
mythuistatetype.h
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
mythuiimage.h
MythUIButtonList::GetCount
int GetCount() const
Definition: mythuibuttonlist.cpp:1656
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MythUIButtonList::GetItemPos
int GetItemPos(MythUIButtonListItem *item) const
Definition: mythuibuttonlist.cpp:1699
hardwareprofile.os_detect.results
results
Definition: os_detect.py:295
getDownloadFilename
QString getDownloadFilename(const QString &title, const QString &url)
Definition: metadataimagedownload.cpp:335
MythUIButtonListItem
Definition: mythuibuttonlist.h:41
mythdate.h
mythlogging.h
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:256
MythUIButtonList::itemClicked
void itemClicked(MythUIButtonListItem *item)
MetadataResultsDialog::Create
bool Create() override
Definition: mythuimetadataresults.cpp:37
ThumbnailData::url
QString url
Definition: metadataimagedownload.h:15
metadatadownload.h
metadataimagedownload.h
RefCountedList< MetadataLookup >
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:206
RefCountedList::takeAtAndDecr
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
Definition: referencecounterlist.h:102
ThumbnailDLEvent
Definition: metadataimagedownload.h:70
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3660
RefCountedList::takeFirst
T * takeFirst(void)
Removes the first item in the list and returns it.
Definition: referencecounterlist.h:78
uint
unsigned int uint
Definition: compat.h:81
ThumbnailData::data
QVariant data
Definition: metadataimagedownload.h:14
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
MetadataResultsDialog::m_imageDownload
MetadataImageDownload * m_imageDownload
Definition: mythuimetadataresults.h:27
MetadataResultsDialog::haveResult
void haveResult(RefCountHandler< MetadataLookup >)
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:687
MythUIButtonListItem::SetImage
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
Definition: mythuibuttonlist.cpp:3424
kArtworkBanner
@ kArtworkBanner
Definition: metadataimagehelper.h:13
MetadataResultsDialog::cleanCacheDir
static void cleanCacheDir()
Definition: mythuimetadataresults.cpp:106
kArtworkScreenshot
@ kArtworkScreenshot
Definition: metadataimagehelper.h:14
ThumbnailData
Definition: metadataimagedownload.h:12
MetadataResultsDialog::sendResult
void sendResult(MythUIButtonListItem *item)
Definition: mythuimetadataresults.cpp:156
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
MetadataResultsDialog::~MetadataResultsDialog
~MetadataResultsDialog() override
Definition: mythuimetadataresults.cpp:26
kArtworkCoverart
@ kArtworkCoverart
Definition: metadataimagehelper.h:11
MetadataResultsDialog::MetadataResultsDialog
MetadataResultsDialog(MythScreenStack *lparent, const MetadataLookupList &results)
Definition: mythuimetadataresults.cpp:16
MetadataResultsDialog::m_resultsList
MythUIButtonList * m_resultsList
Definition: mythuimetadataresults.h:26