MythTV  master
mythuiimageresults.cpp
Go to the documentation of this file.
1 
2 #include "mythuiimageresults.h"
3 
4 #include <QFile>
5 #include <QDir>
6 
7 #include "mythdirs.h"
8 #include "mythdate.h"
9 
10 #include "mythuibuttonlist.h"
11 #include "metadataimagedownload.h"
12 #include "mythcorecontext.h"
13 
15  MythScreenStack *lparent,
16  const ArtworkList& list,
17  const VideoArtworkType type) :
18 
19  MythScreenType(lparent, "videosearchresultspopup"),
20  m_list(list),
21  m_type(type)
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", "MythArtworkResults", 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 'MythArtworkResults'");
47  return false;
48  }
49 
50  for (ArtworkList::const_iterator i = m_list.begin();
51  i != m_list.end(); ++i)
52  {
53  ArtworkInfo info = (*i);
54  MythUIButtonListItem *button =
56  QString());
57  button->SetText(info.label, "label");
58  button->SetText(info.thumbnail, "thumbnail");
59  button->SetText(info.url, "url");
60  QString width = QString::number(info.width);
61  QString height = QString::number(info.height);
62  button->SetText(width, "width");
63  button->SetText(height, "height");
64  if (info.width > 0 && info.height > 0)
65  button->SetText(QString("%1x%2").arg(width).arg(height),
66  "resolution");
67 
68  QString artfile = info.thumbnail;
69 
70  if (artfile.isEmpty())
71  artfile = info.url;
72 
73  QString dlfile = getDownloadFilename(info.label,
74  artfile);
75 
76  if (!artfile.isEmpty())
77  {
78  int pos = m_resultsList->GetItemPos(button);
79 
80  if (QFile::exists(dlfile))
81  button->SetImage(dlfile);
82  else
84  artfile,
85  qVariantFromValue<uint>(pos));
86  }
87 
88  button->SetData(qVariantFromValue<ArtworkInfo>(*i));
89  }
90 
91  connect(m_resultsList, SIGNAL(itemClicked(MythUIButtonListItem *)),
93 
95 
96  return true;
97 }
98 
100 {
101  QString cache = QString("%1/cache/metadata-thumbcache")
102  .arg(GetConfDir());
103  QDir cacheDir(cache);
104  QStringList thumbs = cacheDir.entryList(QDir::Files);
105 
106  for (QStringList::const_iterator i = thumbs.end() - 1;
107  i != thumbs.begin() - 1; --i)
108  {
109  QString filename = QString("%1/%2").arg(cache).arg(*i);
110  QFileInfo fi(filename);
111  QDateTime lastmod = fi.lastModified();
112  if (lastmod.addDays(2) < MythDate::current())
113  {
114  LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting file %1")
115  .arg(filename));
116  QFile::remove(filename);
117  }
118  }
119 }
120 
122 {
123  if (event->type() == ThumbnailDLEvent::kEventType)
124  {
125  ThumbnailDLEvent *tde = (ThumbnailDLEvent *)event;
126 
127  ThumbnailData *data = tde->m_thumb;
128 
129  QString file = data->url;
130  uint pos = data->data.value<uint>();
131 
132  if (file.isEmpty())
133  return;
134 
135  if (!((uint)m_resultsList->GetCount() >= pos))
136  return;
137 
138  MythUIButtonListItem *item =
139  m_resultsList->GetItemAt(pos);
140 
141  if (item)
142  {
143  item->SetImage(file);
144  }
145  }
146 }
147 
149 {
150  emit haveResult(item->GetData().value<ArtworkInfo>(),
151  m_type);
152  Close();
153 }
154 
void SetData(QVariant data)
VideoArtworkType
int GetItemPos(MythUIButtonListItem *item) const
unsigned int uint
Definition: compat.h:140
ImageSearchResultsDialog(MythScreenStack *lparent, const ArtworkList &list, const VideoArtworkType type)
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()
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void SetText(const QString &text, const QString &name="", const QString &state="")
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
void customEvent(QEvent *event) override
ThumbnailData * m_thumb
QList< ArtworkInfo > ArtworkList
void sendResult(MythUIButtonListItem *item)
MetadataImageDownload * m_imageDownload
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void addThumb(QString title, QString url, QVariant data)
void haveResult(ArtworkInfo, VideoArtworkType)
Screen in which all other widgets are contained and rendered.
MythUIButtonList * m_resultsList
QString getDownloadFilename(const QString &title, const QString &url)