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  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  m_imageDownload->addThumb(m_results[i]->GetTitle(),
88  coverartfile,
89  qVariantFromValue<uint>(pos));
90  }
91 
92  button->SetTextFromMap(metadataMap);
93  button->SetData(qVariantFromValue<uint>(i));
94  }
95 
96  connect(m_resultsList, SIGNAL(itemClicked(MythUIButtonListItem *)),
98 
100 
101  return true;
102 }
103 
105 {
106  QString cache = QString("%1/cache/metadata-thumbcache")
107  .arg(GetConfDir());
108  QDir cacheDir(cache);
109  QStringList thumbs = cacheDir.entryList(QDir::Files);
110 
111  for (QStringList::const_iterator i = thumbs.end() - 1;
112  i != thumbs.begin() - 1; --i)
113  {
114  QString filename = QString("%1/%2").arg(cache).arg(*i);
115  QFileInfo fi(filename);
116  QDateTime lastmod = fi.lastModified();
117  if (lastmod.addDays(2) < MythDate::current())
118  {
119  LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting old cache file %1")
120  .arg(filename));
121  QFile::remove(filename);
122  }
123  }
124 }
125 
127 {
128  if (event->type() == ThumbnailDLEvent::kEventType)
129  {
130  auto *tde = dynamic_cast<ThumbnailDLEvent *>(event);
131  if (tde == nullptr)
132  return;
133 
134  ThumbnailData *data = tde->m_thumb;
135 
136  QString file = data->url;
137  uint pos = data->data.value<uint>();
138 
139  if (file.isEmpty())
140  return;
141 
142  if (!((uint)m_resultsList->GetCount() >= pos))
143  return;
144 
145  MythUIButtonListItem *item =
146  m_resultsList->GetItemAt(pos);
147 
148  if (item)
149  {
150  item->SetImage(file);
151  }
152  }
153 }
154 
156 {
158  m_results.clear();
159  emit haveResult(lookup);
160  Close();
161 }
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MetadataResultsDialog(MythScreenStack *lparent, const MetadataLookupList &results)
int GetItemPos(MythUIButtonListItem *item) const
MythUIButtonListItem * GetItemAt(int pos) const
void BuildFocusList(void)
QList< ArtworkInfo > ArtworkList
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()
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.
unsigned int uint
Definition: compat.h:140
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MetadataLookupList m_results
MetadataImageDownload * m_imageDownload
#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 >)
QString getDownloadFilename(const QString &title, const QString &url)
void sendResult(MythUIButtonListItem *item)