MythTV  master
mythuimetadataresults.cpp
Go to the documentation of this file.
1 
3 
4 #include "mythuiimage.h"
5 #include "mythuitext.h"
6 #include "mythuistatetype.h"
7 #include "mythmainwindow.h"
8 #include "mythdialogbox.h"
9 #include "mythdate.h"
10 #include "mythdirs.h"
11 #include "mythlogging.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  MythUIButtonListItem *button =
55  m_results[i]->GetTitle());
56  InfoMap metadataMap;
57  m_results[i]->toMap(metadataMap);
58 
59  QString coverartfile;
60  ArtworkList art = m_results[i]->GetArtwork(kArtworkCoverart);
61  if (art.count() > 0)
62  coverartfile = art.takeFirst().thumbnail;
63 
64  if (coverartfile.isEmpty())
65  {
66  art = m_results[i]->GetArtwork(kArtworkBanner);
67  if (art.count() > 0)
68  coverartfile = art.takeFirst().thumbnail;
69  }
70 
71  if (coverartfile.isEmpty())
72  {
73  art = m_results[i]->GetArtwork(kArtworkScreenshot);
74  if (art.count() > 0)
75  coverartfile = art.takeFirst().thumbnail;
76  }
77 
78  QString dlfile = getDownloadFilename(m_results[i]->GetTitle(),
79  coverartfile);
80 
81  if (!coverartfile.isEmpty())
82  {
83  int pos = m_resultsList->GetItemPos(button);
84 
85  if (QFile::exists(dlfile))
86  button->SetImage(dlfile);
87  else
88  m_imageDownload->addThumb(m_results[i]->GetTitle(),
89  coverartfile,
90  qVariantFromValue<uint>(pos));
91  }
92 
93  button->SetTextFromMap(metadataMap);
94  button->SetData(qVariantFromValue<uint>(i));
95  }
96 
97  connect(m_resultsList, SIGNAL(itemClicked(MythUIButtonListItem *)),
99 
100  BuildFocusList();
101 
102  return true;
103 }
104 
106 {
107  QString cache = QString("%1/cache/metadata-thumbcache")
108  .arg(GetConfDir());
109  QDir cacheDir(cache);
110  QStringList thumbs = cacheDir.entryList(QDir::Files);
111 
112  for (QStringList::const_iterator i = thumbs.end() - 1;
113  i != thumbs.begin() - 1; --i)
114  {
115  QString filename = QString("%1/%2").arg(cache).arg(*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  ThumbnailDLEvent *tde = (ThumbnailDLEvent *)event;
132 
133  ThumbnailData *data = tde->m_thumb;
134 
135  QString file = data->url;
136  uint pos = data->data.value<uint>();
137 
138  if (file.isEmpty())
139  return;
140 
141  if (!((uint)m_resultsList->GetCount() >= pos))
142  return;
143 
144  MythUIButtonListItem *item =
145  m_resultsList->GetItemAt(pos);
146 
147  if (item)
148  {
149  item->SetImage(file);
150  }
151  }
152 }
153 
155 {
157  m_results.clear();
158  emit haveResult(lookup);
159  Close();
160 }
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
void SetData(QVariant data)
MetadataResultsDialog(MythScreenStack *lparent, const MetadataLookupList &results)
int GetItemPos(MythUIButtonListItem *item) const
unsigned int uint
Definition: compat.h:140
MythUIButtonListItem * GetItemAt(int pos) const
void BuildFocusList(void)
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
QString GetConfDir(void)
Definition: mythdirs.cpp:224
virtual void Close()
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
void customEvent(QEvent *event) override
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
T * takeFirst(void)
Removes the first item in the list and returns it.
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MetadataLookupList m_results
ThumbnailData * m_thumb
MetadataImageDownload * m_imageDownload
QList< ArtworkInfo > ArtworkList
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void addThumb(QString title, QString url, QVariant data)
MythUIButtonList * m_resultsList
Screen in which all other widgets are contained and rendered.
void haveResult(RefCountHandler< MetadataLookup >)
void SetTextFromMap(const InfoMap &infoMap, const QString &state="")
QString getDownloadFilename(const QString &title, const QString &url)
void sendResult(MythUIButtonListItem *item)