MythTV  master
mythuiimageresults.cpp
Go to the documentation of this file.
1 
2 #include "mythuiimageresults.h"
3 
4 #include <QDir>
5 #include <QFile>
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  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 (ArtworkList::const_iterator i = m_list.begin();
51  i != m_list.end(); ++i)
52  {
53  ArtworkInfo info = (*i);
54  auto *button = new MythUIButtonListItem(m_resultsList, QString());
55  button->SetText(info.label, "label");
56  button->SetText(info.thumbnail, "thumbnail");
57  button->SetText(info.url, "url");
58  QString width = QString::number(info.width);
59  QString height = QString::number(info.height);
60  button->SetText(width, "width");
61  button->SetText(height, "height");
62  if (info.width > 0 && info.height > 0)
63  button->SetText(QString("%1x%2").arg(width).arg(height),
64  "resolution");
65 
66  QString artfile = info.thumbnail;
67 
68  if (artfile.isEmpty())
69  artfile = info.url;
70 
71  QString dlfile = getDownloadFilename(info.label,
72  artfile);
73 
74  if (!artfile.isEmpty())
75  {
76  int pos = m_resultsList->GetItemPos(button);
77 
78  if (QFile::exists(dlfile))
79  button->SetImage(dlfile);
80  else
81  {
83  artfile,
84  QVariant::fromValue<uint>(pos));
85  }
86  }
87 
88  button->SetData(QVariant::fromValue<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  auto *tde = dynamic_cast<ThumbnailDLEvent *>(event);
126  if (tde == nullptr)
127  return;
128 
129  ThumbnailData *data = tde->m_thumb;
130 
131  QString file = data->url;
132  uint pos = data->data.value<uint>();
133 
134  if (file.isEmpty())
135  return;
136 
137  if (!((uint)m_resultsList->GetCount() >= pos))
138  return;
139 
140  MythUIButtonListItem *item =
141  m_resultsList->GetItemAt(pos);
142 
143  if (item)
144  {
145  item->SetImage(file);
146  }
147  }
148 }
149 
151 {
152  emit haveResult(item->GetData().value<ArtworkInfo>(),
153  m_type);
154  Close();
155 }
156 
MythUIButtonList::GetItemAt
MythUIButtonListItem * GetItemAt(int pos) const
Definition: mythuibuttonlist.cpp:1676
ImageSearchResultsDialog::m_type
VideoArtworkType m_type
Definition: mythuiimageresults.h:29
VideoArtworkType
VideoArtworkType
Definition: metadataimagehelper.h:10
MetadataImageDownload
Definition: metadataimagedownload.h:88
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
ArtworkList
QList< ArtworkInfo > ArtworkList
Definition: metadataimagehelper.h:30
MythScreenStack
Definition: mythscreenstack.h:16
arg
arg(title).arg(filename).arg(doDelete))
ArtworkInfo::label
QString label
Definition: metadataimagehelper.h:23
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
ImageSearchResultsDialog::m_list
ArtworkList m_list
Definition: mythuiimageresults.h:28
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:35
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
ArtworkInfo::width
uint width
Definition: metadataimagehelper.h:26
mythuibuttonlist.h
MythUIButtonList::GetCount
int GetCount() const
Definition: mythuibuttonlist.cpp:1655
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
ImageSearchResultsDialog::ImageSearchResultsDialog
ImageSearchResultsDialog(MythScreenStack *lparent, ArtworkList list, VideoArtworkType type)
Definition: mythuiimageresults.cpp:14
MythUIButtonList::GetItemPos
int GetItemPos(MythUIButtonListItem *item) const
Definition: mythuibuttonlist.cpp:1698
getDownloadFilename
QString getDownloadFilename(const QString &title, const QString &url)
Definition: metadataimagedownload.cpp:334
MythUIButtonListItem
Definition: mythuibuttonlist.h:28
mythdate.h
ThumbnailDLEvent::kEventType
static Type kEventType
Definition: metadataimagedownload.h:84
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:224
ThumbnailData::url
QString url
Definition: metadataimagedownload.h:15
ArtworkInfo
Definition: metadataimagehelper.h:22
metadataimagedownload.h
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
ThumbnailDLEvent
Definition: metadataimagedownload.h:71
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3588
ImageSearchResultsDialog::m_imageDownload
MetadataImageDownload * m_imageDownload
Definition: mythuiimageresults.h:31
uint
unsigned int uint
Definition: compat.h:140
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:692
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:3406
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:121
ArtworkInfo::height
uint height
Definition: metadataimagehelper.h:27
mythuiimageresults.h
ImageSearchResultsDialog::m_resultsList
MythUIButtonList * m_resultsList
Definition: mythuiimageresults.h:30
ArtworkInfo::thumbnail
QString thumbnail
Definition: metadataimagehelper.h:24
ThumbnailData
Definition: metadataimagedownload.h:12
ImageSearchResultsDialog::sendResult
void sendResult(MythUIButtonListItem *item)
Definition: mythuiimageresults.cpp:150
ImageSearchResultsDialog::cleanCacheDir
static void cleanCacheDir()
Definition: mythuiimageresults.cpp:99
ImageSearchResultsDialog::~ImageSearchResultsDialog
~ImageSearchResultsDialog() override
Definition: mythuiimageresults.cpp:26
ArtworkInfo::url
QString url
Definition: metadataimagehelper.h:25