Skip to content

Commit

Permalink
MythNews: remove the need for the depreciated HttpComms
Browse files Browse the repository at this point in the history
This changes the downloads to use the MythDownloadManager and removes some
redunant code after StuartM's changes in 9bec233.

I can't test it but this should also fix MythNews on Qt5.
  • Loading branch information
Paul Harrison committed Jun 15, 2013
1 parent bf9fa6d commit a1b4b60
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 362 deletions.
276 changes: 10 additions & 266 deletions mythplugins/mythnews/mythnews/mythnews.cpp
Expand Up @@ -13,7 +13,6 @@
#include <QUrl>

// MythTV headers
#include <mythprogressdialog.h>
#include <mythuibuttonlist.h>
#include <mythmainwindow.h>
#include <mythdialogbox.h>
Expand All @@ -24,10 +23,7 @@
#include <mythdate.h>
#include <mythdirs.h>
#include <mythdb.h>

#if QT_VERSION < 0x050000
#include <httpcomms.h>
#endif
#include <mythdownloadmanager.h>

// MythNews headers
#include "mythnews.h"
Expand All @@ -52,9 +48,6 @@ MythNews::MythNews(MythScreenStack *parent, const QString &name) :
m_zoom(gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0")),
m_browser(gCoreContext->GetSetting("WebBrowserCommand", "")),
m_menuPopup(NULL),
m_progressPopup(NULL),
m_httpGrabber(NULL),
m_abortHttp(false),
m_sitesList(NULL),
m_articlesList(NULL),
m_nositesText(NULL),
Expand Down Expand Up @@ -298,35 +291,9 @@ void MythNews::updateInfoView(MythUIButtonListItem *selected)

if (!article.thumbnail().isEmpty())
{
QString fileprefix = GetConfDir();

QDir dir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);

fileprefix += "/MythNews/tcache";

dir = QDir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);

QString url = article.thumbnail();
QString sFilename = QString("%1/%2")
.arg(fileprefix)
.arg(qChecksum(url.toLocal8Bit().constData(),
url.toLocal8Bit().size()));

#if QT_VERSION < 0x050000
bool exists = QFile::exists(sFilename);
if (!exists)
HttpComms::getHttpFile(sFilename, url, 20000, 1, 2);
#else
#warning MythNews::updateInfoView() has not yet been ported to Qt5
#endif

if (m_thumbnailImage)
{
m_thumbnailImage->SetFilename(sFilename);
m_thumbnailImage->SetFilename(article.thumbnail());
m_thumbnailImage->Load();

if (!m_thumbnailImage->IsVisible())
Expand All @@ -335,48 +302,22 @@ void MythNews::updateInfoView(MythUIButtonListItem *selected)
}
else
{
if (m_thumbnailImage)
m_thumbnailImage->Hide();

if (site && !site->imageURL().isEmpty())
{
QString fileprefix = GetConfDir();

QDir dir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);

fileprefix += "/MythNews/scache";

dir = QDir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);

QString url = site->imageURL();
QString extension = url.section('.', -1);

QString sitename = site->name();
QString sFilename = QString("%1/%2.%3").arg(fileprefix)
.arg(sitename)
.arg(extension);

#if QT_VERSION < 0x050000
bool exists = QFile::exists(sFilename);
if (!exists)
HttpComms::getHttpFile(sFilename, url, 20000, 1, 2);
#else
#warning MythNews::updateInfoView() has not yet been ported to Qt5
#endif

if (m_thumbnailImage)
{
m_thumbnailImage->SetFilename(sFilename);
m_thumbnailImage->SetFilename(site->imageURL());
m_thumbnailImage->Load();

if (!m_thumbnailImage->IsVisible())
m_thumbnailImage->Show();
}
}
else
{
if (m_thumbnailImage)
m_thumbnailImage->Hide();
}
}

if (m_downloadImage)
Expand Down Expand Up @@ -432,37 +373,9 @@ void MythNews::updateInfoView(MythUIButtonListItem *selected)

if (!site->imageURL().isEmpty())
{
QString fileprefix = GetConfDir();

QDir dir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);

fileprefix += "/MythNews/scache";

dir = QDir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);

QString url = site->imageURL();
QString extension = url.section('.', -1);

QString sitename = site->name();
QString sFilename = QString("%1/%2.%3").arg(fileprefix)
.arg(sitename)
.arg(extension);

#if QT_VERSION < 0x050000
bool exists = QFile::exists(sFilename);
if (!exists)
HttpComms::getHttpFile(sFilename, url, 20000, 1, 2);
#else
#warning MythNews::updateInfoView() has not yet been ported to Qt5
#endif

if (m_thumbnailImage)
{
m_thumbnailImage->SetFilename(sFilename);
m_thumbnailImage->SetFilename(site->imageURL());
m_thumbnailImage->Load();

if (!m_thumbnailImage->IsVisible())
Expand All @@ -481,31 +394,12 @@ void MythNews::updateInfoView(MythUIButtonListItem *selected)
QDateTime updated(site->lastUpdated());
if (updated.toTime_t() != 0) {
text += MythDate::toString(site->lastUpdated(),
MythDate::kDateTimeFull | MythDate::kSimplify);
MythDate::kDateTimeFull | MythDate::kSimplify);
}
else
text += tr("Unknown");
m_updatedText->SetText(text);
}

#if QT_VERSION < 0x050000
if (m_httpGrabber != NULL)
{
int progress = m_httpGrabber->getProgress();
int total = m_httpGrabber->getTotal();
if ((progress > 0) && (total > 0) && (progress < total))
{
float fProgress = (float)progress/total;
QString text = tr("%1 of %2 (%3 percent)")
.arg(formatSize(progress, 2))
.arg(formatSize(total, 2))
.arg(floor(fProgress*100));
m_updatedText->SetText(text);
}
}
#else
#warning MythNews::updateInfoView() has not yet been ported to Qt5
#endif
}
}

Expand Down Expand Up @@ -556,25 +450,6 @@ bool MythNews::keyPressEvent(QKeyEvent *event)
ShowEditDialog(true);
else if (action == "DELETE")
deleteNewsSite();
else if (action == "ESCAPE")
{
{
QMutexLocker locker(&m_lock);

if (m_progressPopup)
{
m_progressPopup->Close();
m_progressPopup = NULL;
}

m_RetrieveTimer->stop();

if (m_httpGrabber)
m_abortHttp = true;
}

Close();
}
else
handled = false;
}
Expand Down Expand Up @@ -696,137 +571,6 @@ void MythNews::slotSiteSelected(MythUIButtonListItem *item)
updateInfoView(item);
}

void MythNews::slotProgressCancelled(void)
{
QMutexLocker locker(&m_lock);

m_abortHttp = true;
}

void MythNews::createProgress(const QString &title)
{
QMutexLocker locker(&m_lock);

if (m_progressPopup)
return;

QString message = title;

MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");

m_progressPopup = new MythUIProgressDialog(message, popupStack,
"mythnewsprogressdialog");

if (m_progressPopup->Create())
popupStack->AddScreen(m_progressPopup, false);
else
{
delete m_progressPopup;
m_progressPopup = NULL;
}
}

bool MythNews::getHttpFile(const QString &sFilename, const QString &cmdURL)
{
#if QT_VERSION < 0x050000
QMutexLocker locker(&m_lock);

int redirectCount = 0;
QByteArray data(0);
bool res = false;
m_httpGrabber = NULL;
QString hostname;
QString fileUrl = cmdURL;

createProgress(tr("Downloading media..."));
while (1)
{
QUrl qurl(fileUrl);
if (hostname.isEmpty())
hostname = qurl.host(); // hold onto original host

if (qurl.host().isEmpty()) // can occur on redirects to partial paths
qurl.setHost(hostname);

if (m_httpGrabber != NULL)
delete m_httpGrabber;

m_httpGrabber = new HttpComms;
m_abortHttp = false;

m_httpGrabber->request(qurl, -1, true);

while ((!m_httpGrabber->isDone()) && (!m_abortHttp))
{
int progress = m_httpGrabber->getProgress();
int total = m_httpGrabber->getTotal();
if ((progress > 0) && (total > 5120) && (progress < total)) // Ignore total less than 5kb as we're probably looking at a redirect page or similar
{
m_progressPopup->SetTotal(total);
m_progressPopup->SetProgress(progress);
float fProgress = (float)progress/total;
QString text = tr("%1 of %2 (%3 percent)")
.arg(formatSize(progress, 2))
.arg(formatSize(total, 2))
.arg(floor(fProgress*100));
if (m_updatedText)
m_updatedText->SetText(text);
}
qApp->processEvents();
usleep(100000);
}

if (m_abortHttp)
break;

// Check for redirection
if (!m_httpGrabber->getRedirectedURL().isEmpty())
{
if (redirectCount++ < 3)
fileUrl = m_httpGrabber->getRedirectedURL();

// Try again
continue;
}

if (m_httpGrabber->getStatusCode() > 400)
{
// Error, request failed
break;
}

data = m_httpGrabber->getRawData();

if (data.size() > 0)
{
QFile file(sFilename);
if (file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
res = true;
}
}
break;
}

if (m_progressPopup)
{
m_progressPopup->Close();
m_progressPopup = NULL;
}

delete m_httpGrabber;
m_httpGrabber = NULL;
return res;
#else
#warning MythNews::getHttpFile() has not yet been ported to Qt5
(void) sFilename;
(void) cmdURL;
return false;
#endif
}

void MythNews::slotViewArticle(MythUIButtonListItem *articlesListItem)
{
QMutexLocker locker(&m_lock);
Expand Down
9 changes: 0 additions & 9 deletions mythplugins/mythnews/mythnews/mythnews.h
Expand Up @@ -14,7 +14,6 @@ class MythUIImage;
class MythDialogBox;
class MythUIButtonList;
class MythUIButtonListItem;
class MythUIProgressDialog;

/** \class MythNews
* \brief Plugin for browsing RSS news feeds.
Expand All @@ -37,8 +36,6 @@ class MythNews : public MythScreenType
void cancelRetrieve(void);
void processAndShowNews(NewsSite *site);

bool getHttpFile(const QString &sFilename, const QString &cmdURL);
void createProgress(const QString &title);
QString formatSize(long long bytes, int prec);
void playVideo(const NewsArticle &article);

Expand All @@ -59,10 +56,6 @@ class MythNews : public MythScreenType
QString m_browser;
MythDialogBox *m_menuPopup;

MythUIProgressDialog *m_progressPopup;
HttpComms *m_httpGrabber;
bool m_abortHttp;

MythUIButtonList *m_sitesList;
MythUIButtonList *m_articlesList;
QMap<MythUIButtonListItem*,NewsArticle> m_articles;
Expand All @@ -84,8 +77,6 @@ class MythNews : public MythScreenType
void slotRetrieveNews(void);
void slotNewsRetrieved(NewsSite*);
void slotSiteSelected(MythUIButtonListItem*);

void slotProgressCancelled();
};

#endif /* MYTHNEWS_H */

0 comments on commit a1b4b60

Please sign in to comment.