MythTV  master
mythuiimageresults.cpp
Go to the documentation of this file.
1 
2 #include "mythuiimageresults.h"
3 
4 #include <QDir>
5 #include <QFile>
6 
8 #include "libmythbase/mythdate.h"
9 #include "libmythbase/mythdirs.h"
11 
12 #include "metadataimagedownload.h"
13 
15  MythScreenStack *lparent,
16  ArtworkList list,
17  const VideoArtworkType type) :
18 
19  MythScreenType(lparent, "videosearchresultspopup"),
20  m_list(std::move(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 (const auto & info : std::as_const(m_list))
51  {
52  auto *button = new MythUIButtonListItem(m_resultsList, QString());
53  button->SetText(info.label, "label");
54  button->SetText(info.thumbnail, "thumbnail");
55  button->SetText(info.url, "url");
56  QString width = QString::number(info.width);
57  QString height = QString::number(info.height);
58  button->SetText(width, "width");
59  button->SetText(height, "height");
60  if (info.width > 0 && info.height > 0)
61  button->SetText(QString("%1x%2").arg(width, height),
62  "resolution");
63 
64  QString artfile = info.thumbnail;
65 
66  if (artfile.isEmpty())
67  artfile = info.url;
68 
69  QString dlfile = getDownloadFilename(info.label,
70  artfile);
71 
72  if (!artfile.isEmpty())
73  {
74  int pos = m_resultsList->GetItemPos(button);
75 
76  if (QFile::exists(dlfile))
77  button->SetImage(dlfile);
78  else
79  {
80  m_imageDownload->addThumb(info.label,
81  artfile,
82  QVariant::fromValue<uint>(pos));
83  }
84  }
85 
86  button->SetData(QVariant::fromValue<ArtworkInfo>(info));
87  }
88 
91 
93 
94  return true;
95 }
96 
98 {
99  QString cache = QString("%1/cache/metadata-thumbcache")
100  .arg(GetConfDir());
101  QDir cacheDir(cache);
102  QStringList thumbs = cacheDir.entryList(QDir::Files);
103 
104  for (auto i = thumbs.crbegin(); i != thumbs.crend(); ++i)
105  {
106  QString filename = QString("%1/%2").arg(cache, *i);
107  QFileInfo fi(filename);
108  QDateTime lastmod = fi.lastModified();
109  if (lastmod.addDays(2) < MythDate::current())
110  {
111  LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting file %1")
112  .arg(filename));
113  QFile::remove(filename);
114  }
115  }
116 }
117 
119 {
120  if (event->type() == ThumbnailDLEvent::kEventType)
121  {
122  auto *tde = dynamic_cast<ThumbnailDLEvent *>(event);
123  if (tde == nullptr)
124  return;
125 
126  ThumbnailData *data = tde->m_thumb;
127 
128  QString file = data->url;
129  uint pos = data->data.value<uint>();
130 
131  if (file.isEmpty())
132  return;
133 
134  if (!((uint)m_resultsList->GetCount() >= pos))
135  return;
136 
137  MythUIButtonListItem *item =
138  m_resultsList->GetItemAt(pos);
139 
140  if (item)
141  {
142  item->SetImage(file);
143  }
144  }
145 }
146 
148 {
149  emit haveResult(item->GetData().value<ArtworkInfo>(),
150  m_type);
151  Close();
152 }
153 
MythUIButtonList::GetItemAt
MythUIButtonListItem * GetItemAt(int pos) const
Definition: mythuibuttonlist.cpp:1677
ImageSearchResultsDialog::m_type
VideoArtworkType m_type
Definition: mythuiimageresults.h:30
VideoArtworkType
VideoArtworkType
Definition: metadataimagehelper.h:10
MetadataImageDownload
Definition: metadataimagedownload.h:87
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
MythScreenStack
Definition: mythscreenstack.h:16
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
ImageSearchResultsDialog::m_list
ArtworkList m_list
Definition: mythuiimageresults.h:29
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
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
mythuibuttonlist.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
ImageSearchResultsDialog::ImageSearchResultsDialog
ImageSearchResultsDialog(MythScreenStack *lparent, ArtworkList list, VideoArtworkType type)
Definition: mythuiimageresults.cpp:14
MythUIButtonList::GetItemPos
int GetItemPos(MythUIButtonListItem *item) const
Definition: mythuibuttonlist.cpp:1699
getDownloadFilename
QString getDownloadFilename(const QString &title, const QString &url)
Definition: metadataimagedownload.cpp:335
MythUIButtonListItem
Definition: mythuibuttonlist.h:41
mythdate.h
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:256
MythUIButtonList::itemClicked
void itemClicked(MythUIButtonListItem *item)
ThumbnailData::url
QString url
Definition: metadataimagedownload.h:15
ArtworkInfo
Definition: metadataimagehelper.h:21
metadataimagedownload.h
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:206
ThumbnailDLEvent
Definition: metadataimagedownload.h:70
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3660
ImageSearchResultsDialog::m_imageDownload
MetadataImageDownload * m_imageDownload
Definition: mythuiimageresults.h:32
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
mythcorecontext.h
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
ImageSearchResultsDialog::haveResult
void haveResult(ArtworkInfo, VideoArtworkType)
ImageSearchResultsDialog::Create
bool Create() override
Definition: mythuiimageresults.cpp:37
ImageSearchResultsDialog::customEvent
void customEvent(QEvent *event) override
Definition: mythuiimageresults.cpp:118
std
Definition: mythchrono.h:23
mythuiimageresults.h
ImageSearchResultsDialog::m_resultsList
MythUIButtonList * m_resultsList
Definition: mythuiimageresults.h:31
ThumbnailData
Definition: metadataimagedownload.h:12
build_compdb.filename
filename
Definition: build_compdb.py:21
ImageSearchResultsDialog::sendResult
void sendResult(MythUIButtonListItem *item)
Definition: mythuiimageresults.cpp:147
ImageSearchResultsDialog::cleanCacheDir
static void cleanCacheDir()
Definition: mythuiimageresults.cpp:97
ImageSearchResultsDialog::~ImageSearchResultsDialog
~ImageSearchResultsDialog() override
Definition: mythuiimageresults.cpp:26