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  {
88  m_imageDownload->addThumb(m_results[i]->GetTitle(),
89  coverartfile,
90  QVariant::fromValue<uint>(pos));
91  }
92  }
93 
94  button->SetTextFromMap(metadataMap);
95  button->SetData(QVariant::fromValue<uint>(i));
96  }
97 
98  connect(m_resultsList, SIGNAL(itemClicked(MythUIButtonListItem *)),
100 
101  BuildFocusList();
102 
103  return true;
104 }
105 
107 {
108  QString cache = QString("%1/cache/metadata-thumbcache")
109  .arg(GetConfDir());
110  QDir cacheDir(cache);
111  QStringList thumbs = cacheDir.entryList(QDir::Files);
112 
113  for (QStringList::const_iterator i = thumbs.end() - 1;
114  i != thumbs.begin() - 1; --i)
115  {
116  QString filename = QString("%1/%2").arg(cache).arg(*i);
117  QFileInfo fi(filename);
118  QDateTime lastmod = fi.lastModified();
119  if (lastmod.addDays(2) < MythDate::current())
120  {
121  LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting old cache file %1")
122  .arg(filename));
123  QFile::remove(filename);
124  }
125  }
126 }
127 
129 {
130  if (event->type() == ThumbnailDLEvent::kEventType)
131  {
132  auto *tde = dynamic_cast<ThumbnailDLEvent *>(event);
133  if (tde == nullptr)
134  return;
135 
136  ThumbnailData *data = tde->m_thumb;
137 
138  QString file = data->url;
139  uint pos = data->data.value<uint>();
140 
141  if (file.isEmpty())
142  return;
143 
144  if (!((uint)m_resultsList->GetCount() >= pos))
145  return;
146 
147  MythUIButtonListItem *item =
148  m_resultsList->GetItemAt(pos);
149 
150  if (item)
151  {
152  item->SetImage(file);
153  }
154  }
155 }
156 
158 {
160  m_results.clear();
161  emit haveResult(lookup);
162  Close();
163 }
MythUIButtonList::GetItemAt
MythUIButtonListItem * GetItemAt(int pos) const
Definition: mythuibuttonlist.cpp:1676
mythuimetadataresults.h
RefCountHandler
Definition: referencecounterlist.h:17
mythuitext.h
MetadataImageDownload
Definition: metadataimagedownload.h:87
MetadataResultsDialog::customEvent
void customEvent(QEvent *event) override
Definition: mythuimetadataresults.cpp:128
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
ArtworkList
QList< ArtworkInfo > ArtworkList
Definition: metadataimagehelper.h:30
mythdialogbox.h
MythScreenStack
Definition: mythscreenstack.h:15
arg
arg(title).arg(filename).arg(doDelete))
MetadataResultsDialog::m_results
MetadataLookupList m_results
Definition: mythuimetadataresults.h:26
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:44
MetadataImageDownload::addThumb
void addThumb(QString title, QString url, QVariant data)
Definition: metadataimagedownload.cpp:35
mythuistatetype.h
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
mythuiimage.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
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MythUIButtonList::GetItemPos
int GetItemPos(MythUIButtonListItem *item) const
Definition: mythuibuttonlist.cpp:1698
hardwareprofile.os_detect.results
results
Definition: os_detect.py:296
getDownloadFilename
QString getDownloadFilename(const QString &title, const QString &url)
Definition: metadataimagedownload.cpp:334
MythUIButtonListItem
Definition: mythuibuttonlist.h:27
mythdate.h
ThumbnailDLEvent::kEventType
static Type kEventType
Definition: metadataimagedownload.h:84
mythlogging.h
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:224
MetadataResultsDialog::Create
bool Create() override
Definition: mythuimetadataresults.cpp:37
ThumbnailData::url
QString url
Definition: metadataimagedownload.h:15
metadatadownload.h
metadataimagedownload.h
RefCountedList< MetadataLookup >
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
RefCountedList::takeAtAndDecr
RefCountHandler< T > takeAtAndDecr(int i)
Removes the item at index position i and returns it.
Definition: referencecounterlist.h:102
ThumbnailDLEvent
Definition: metadataimagedownload.h:70
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3588
RefCountedList::takeFirst
T * takeFirst(void)
Removes the first item in the list and returns it.
Definition: referencecounterlist.h:78
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
MetadataResultsDialog::m_imageDownload
MetadataImageDownload * m_imageDownload
Definition: mythuimetadataresults.h:28
MetadataResultsDialog::haveResult
void haveResult(RefCountHandler< MetadataLookup >)
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
kArtworkBanner
@ kArtworkBanner
Definition: metadataimagehelper.h:13
MetadataResultsDialog::cleanCacheDir
static void cleanCacheDir()
Definition: mythuimetadataresults.cpp:106
kArtworkScreenshot
@ kArtworkScreenshot
Definition: metadataimagehelper.h:14
ThumbnailData
Definition: metadataimagedownload.h:12
MetadataResultsDialog::sendResult
void sendResult(MythUIButtonListItem *item)
Definition: mythuimetadataresults.cpp:157
mythmainwindow.h
MetadataResultsDialog::~MetadataResultsDialog
~MetadataResultsDialog() override
Definition: mythuimetadataresults.cpp:26
kArtworkCoverart
@ kArtworkCoverart
Definition: metadataimagehelper.h:11
MetadataResultsDialog::MetadataResultsDialog
MetadataResultsDialog(MythScreenStack *lparent, const MetadataLookupList &results)
Definition: mythuimetadataresults.cpp:16
MetadataResultsDialog::m_resultsList
MythUIButtonList * m_resultsList
Definition: mythuimetadataresults.h:27