00001 #include <QFile>
00002 #include <QDir>
00003
00004 #include "mythdirs.h"
00005
00006 #include "mythuiimageresults.h"
00007
00008 ImageSearchResultsDialog::ImageSearchResultsDialog(
00009 MythScreenStack *lparent,
00010 const ArtworkList list,
00011 const VideoArtworkType type) :
00012
00013 MythScreenType(lparent, "videosearchresultspopup"),
00014 m_list(list),
00015 m_type(type),
00016 m_resultsList(0)
00017 {
00018 m_imageDownload = new MetadataImageDownload(this);
00019 }
00020
00021 ImageSearchResultsDialog::~ImageSearchResultsDialog()
00022 {
00023 cleanCacheDir();
00024
00025 if (m_imageDownload)
00026 {
00027 delete m_imageDownload;
00028 m_imageDownload = NULL;
00029 }
00030 }
00031
00032 bool ImageSearchResultsDialog::Create()
00033 {
00034 if (!LoadWindowFromXML("base.xml", "MythArtworkResults", this))
00035 return false;
00036
00037 bool err = false;
00038 UIUtilE::Assign(this, m_resultsList, "results", &err);
00039 if (err)
00040 {
00041 LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'MythArtworkResults'");
00042 return false;
00043 }
00044
00045 for (ArtworkList::const_iterator i = m_list.begin();
00046 i != m_list.end(); ++i)
00047 {
00048 ArtworkInfo info = (*i);
00049 MythUIButtonListItem *button =
00050 new MythUIButtonListItem(m_resultsList,
00051 QString());
00052 button->SetText(info.label, "label");
00053 button->SetText(info.thumbnail, "thumbnail");
00054 button->SetText(info.url, "url");
00055 QString width = QString::number(info.width);
00056 QString height = QString::number(info.height);
00057 button->SetText(width, "width");
00058 button->SetText(height, "height");
00059 if (info.width > 0 && info.height > 0)
00060 button->SetText(QString("%1x%2").arg(width).arg(height),
00061 "resolution");
00062
00063 QString artfile = info.thumbnail;
00064
00065 if (artfile.isEmpty())
00066 artfile = info.url;
00067
00068 QString dlfile = getDownloadFilename(info.label,
00069 artfile);
00070
00071 if (!artfile.isEmpty())
00072 {
00073 int pos = m_resultsList->GetItemPos(button);
00074
00075 if (QFile::exists(dlfile))
00076 button->SetImage(dlfile);
00077 else
00078 m_imageDownload->addThumb(info.label,
00079 artfile,
00080 qVariantFromValue<uint>(pos));
00081 }
00082
00083 button->SetData(qVariantFromValue<ArtworkInfo>(*i));
00084 }
00085
00086 connect(m_resultsList, SIGNAL(itemClicked(MythUIButtonListItem *)),
00087 SLOT(sendResult(MythUIButtonListItem *)));
00088
00089 BuildFocusList();
00090
00091 return true;
00092 }
00093
00094 void ImageSearchResultsDialog::cleanCacheDir()
00095 {
00096 QString cache = QString("%1/thumbcache")
00097 .arg(GetConfDir());
00098 QDir cacheDir(cache);
00099 QStringList thumbs = cacheDir.entryList(QDir::Files);
00100
00101 for (QStringList::const_iterator i = thumbs.end() - 1;
00102 i != thumbs.begin() - 1; --i)
00103 {
00104 QString filename = QString("%1/%2").arg(cache).arg(*i);
00105 QFileInfo fi(filename);
00106 QDateTime lastmod = fi.lastModified();
00107 if (lastmod.addDays(2) < QDateTime::currentDateTime())
00108 {
00109 LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting file %1")
00110 .arg(filename));
00111 QFile::remove(filename);
00112 }
00113 }
00114 }
00115
00116 void ImageSearchResultsDialog::customEvent(QEvent *event)
00117 {
00118 if (event->type() == ThumbnailDLEvent::kEventType)
00119 {
00120 ThumbnailDLEvent *tde = (ThumbnailDLEvent *)event;
00121
00122 ThumbnailData *data = tde->thumb;
00123
00124 QString file = data->url;
00125 uint pos = qVariantValue<uint>(data->data);
00126
00127 if (file.isEmpty())
00128 return;
00129
00130 if (!((uint)m_resultsList->GetCount() >= pos))
00131 return;
00132
00133 MythUIButtonListItem *item =
00134 m_resultsList->GetItemAt(pos);
00135
00136 if (item)
00137 {
00138 item->SetImage(file);
00139 }
00140 delete data;
00141 data = NULL;
00142 }
00143 }
00144
00145 void ImageSearchResultsDialog::sendResult(MythUIButtonListItem* item)
00146 {
00147 emit haveResult(qVariantValue<ArtworkInfo>(item->GetData()),
00148 m_type);
00149 Close();
00150 }
00151