Browse Source

rss : try to download streams' favicon

adaptive-webui-19844
Arnaud Demaiziere 18 years ago
parent
commit
7d8d4e7ee5
  1. BIN
      src/Icons/loading.gif
  2. BIN
      src/Icons/loading.png
  3. 5
      src/icons.qrc
  4. 62
      src/rss.h
  5. 49
      src/rss_imp.cpp
  6. 2
      src/rss_imp.h

BIN
src/Icons/loading.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 B

BIN
src/Icons/loading.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 B

5
src/icons.qrc

@ -20,12 +20,13 @@ @@ -20,12 +20,13 @@
<file>Icons/splash.png</file>
<file>Icons/home.png</file>
<file>Icons/uparrow.png</file>
<file>Icons/rss.png</file>
<file>Icons/downarrow.png</file>
<file>Icons/connection.png</file>
<file>Icons/add_file.png</file>
<file>Icons/refresh.png</file>
<file>Icons/rss.png</file>
<file>Icons/add_folder.png</file>
<file>Icons/add_file.png</file>
<file>Icons/loading.png</file>
<file>Icons/flags/portugal.png</file>
<file>Icons/flags/france.png</file>
<file>Icons/flags/ukraine.png</file>

62
src/rss.h

@ -29,6 +29,9 @@ @@ -29,6 +29,9 @@
// avoid crash if too many refresh
#define REFRESH_FREQ_MAX 5000
#define ICON 0
#define NEWS 1
#include <QFile>
#include <QList>
#include <curl/curl.h>
@ -36,6 +39,7 @@ @@ -36,6 +39,7 @@
#include <QSettings>
#include <QDomDocument>
#include <QTime>
#include <QUrl>
#include "misc.h"
#include "downloadThread.h"
@ -118,13 +122,14 @@ class RssStream : public QObject{ @@ -118,13 +122,14 @@ class RssStream : public QObject{
QString image;
QString url;
QString filePath;
QString iconPath;
QList<RssItem*> listItem;
downloadThread* downloader;
QTime lastRefresh;
bool read;
signals:
void refreshFinished(const QString& msg);
void refreshFinished(const QString& msg, const unsigned short& type);
public slots :
// read and store the downloaded rss' informations
@ -138,13 +143,31 @@ class RssStream : public QObject{ @@ -138,13 +143,31 @@ class RssStream : public QObject{
// Download failed
qDebug("(download failure) "+file_path.toUtf8());
if(QFile::exists(filePath)) {
QFile::remove(file_path);
QFile::remove(filePath);
}
emit refreshFinished(url);
emit refreshFinished(url, NEWS);
return;
}
openRss();
emit refreshFinished(url);
emit refreshFinished(url, NEWS);
}
void displayIcon(const QString&, const QString& file_path, int return_code, const QString&) {
if(QFile::exists(iconPath) && iconPath!=":/Icons/rss.png") {
QFile::remove(iconPath);
}
iconPath = file_path;
if(return_code){
// Download failed
qDebug("(download failure) "+iconPath.toUtf8());
if(QFile::exists(iconPath) && iconPath!=":/Icons/rss.png") {
QFile::remove(iconPath);
}
iconPath = ":/Icons/rss.png";
emit refreshFinished(url, ICON);
return;
}
emit refreshFinished(url, ICON);
}
public:
@ -152,9 +175,11 @@ class RssStream : public QObject{ @@ -152,9 +175,11 @@ class RssStream : public QObject{
url = _url;
alias = url;
read = true;
iconPath = ":/Icons/rss.png";
downloader = new downloadThread(this);
connect(downloader, SIGNAL(downloadFinished(const QString&, const QString&, int, const QString&)), this, SLOT(processDownloadedFile(const QString&, const QString&, int, const QString&)));
downloader->downloadUrl(url);
getIcon();
lastRefresh.start();
}
@ -163,6 +188,8 @@ class RssStream : public QObject{ @@ -163,6 +188,8 @@ class RssStream : public QObject{
delete downloader;
if(QFile::exists(filePath))
QFile::remove(filePath);
if(QFile::exists(iconPath) && iconPath!=":/Icons/rss.png")
QFile::remove(iconPath);
}
// delete all the items saved
@ -212,6 +239,10 @@ class RssStream : public QObject{ @@ -212,6 +239,10 @@ class RssStream : public QObject{
return filePath;
}
QString getIconPath() const{
return filePath;
}
RssItem* getItem(unsigned int index) const{
return listItem.at(index);
}
@ -236,6 +267,13 @@ class RssStream : public QObject{ @@ -236,6 +267,13 @@ class RssStream : public QObject{
read = true;
}
void getIcon() {
QUrl siteUrl(url);
QString iconUrl = "http://"+siteUrl.host()+"/favicon.ico";
connect(downloader, SIGNAL(downloadFinished(const QString&, const QString&, int, const QString&)), this, SLOT(displayIcon(const QString&, const QString&, int, const QString&)));
downloader->downloadUrl(iconUrl);
}
private:
// read and create items from a rss document
short readDoc(const QDomDocument& doc) {
@ -342,11 +380,11 @@ class RssManager : public QObject{ @@ -342,11 +380,11 @@ class RssManager : public QObject{
QStringList streamListUrl;
signals:
void streamNeedRefresh(const unsigned short&);
void streamNeedRefresh(const unsigned short&, const unsigned short&);
public slots :
void streamNeedRefresh(const QString& _url) {
emit(streamNeedRefresh(hasStream(_url)));
void streamNeedRefresh(const QString& _url, const unsigned short& type) {
emit(streamNeedRefresh(hasStream(_url), type));
}
public :
@ -380,7 +418,7 @@ class RssManager : public QObject{ @@ -380,7 +418,7 @@ class RssManager : public QObject{
RssStream *stream = new RssStream(streamListUrl.at(i));
stream->setAlias(streamListAlias.at(i));
streamList.append(stream);
connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
connect(stream, SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}
}
@ -404,7 +442,7 @@ class RssManager : public QObject{ @@ -404,7 +442,7 @@ class RssManager : public QObject{
if(hasStream(stream) < 0){
streamList.append(stream);
streamListUrl.append(stream->getUrl());
connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
connect(stream, SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}else{
qDebug("Not adding the Rss stream because it is already in the list");
}
@ -426,7 +464,7 @@ class RssManager : public QObject{ @@ -426,7 +464,7 @@ class RssManager : public QObject{
RssStream* stream = new RssStream(url);
streamList.append(stream);
streamListUrl.append(url);
connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
connect(stream, SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}else {
qDebug("Not adding the Rss stream because it is already in the list");
}
@ -458,7 +496,7 @@ class RssManager : public QObject{ @@ -458,7 +496,7 @@ class RssManager : public QObject{
unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){
getStream(i)->refresh();
connect(getStream(i), SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
connect(getStream(i), SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}
}
@ -466,7 +504,7 @@ class RssManager : public QObject{ @@ -466,7 +504,7 @@ class RssManager : public QObject{
if(index>=0 && index<getNbStream()) {
if(getStream(index)->getLastRefreshElapsed()>REFRESH_FREQ_MAX) {
getStream(index)->refresh();
connect(getStream(index), SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
connect(getStream(index), SIGNAL(refreshFinished(const QString&, const unsigned short&)), this, SLOT(streamNeedRefresh(const QString&, const unsigned short&)));
}
}
}

49
src/rss_imp.cpp

@ -65,7 +65,6 @@ @@ -65,7 +65,6 @@
// display the news of a stream when click on it
void RSSImp::on_listStreams_clicked() {
rssmanager.getStream(listStreams->currentRow())->setRead();
//streamNeedRefresh(listStreams->currentRow());
listStreams->item(listStreams->currentRow())->setData(Qt::BackgroundRole, QVariant(QColor("white")));
refreshNewsList();
}
@ -109,7 +108,7 @@ @@ -109,7 +108,7 @@
QString newAlias = QInputDialog::getText(this, tr("Please choose a new name for this stream"), tr("New stream name:"), QLineEdit::Normal, rssmanager.getStream(index)->getAlias(), &ok);
if(ok) {
rssmanager.setAlias(index, newAlias);
updateStreamName(index);
updateStreamName(index, NEWS);
}
}
@ -121,7 +120,7 @@ @@ -121,7 +120,7 @@
if(rssmanager.getNbStream()>0) {
textBrowser->clear();
listNews->clear();
listStreams->item(index)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/refresh.png")));
listStreams->item(index)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
rssmanager.refresh(index);
}
}
@ -132,7 +131,7 @@ @@ -132,7 +131,7 @@
listNews->clear();
unsigned short nbstream = rssmanager.getNbStream();
for(unsigned short i=0; i<nbstream; i++)
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/refresh.png")));
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
rssmanager.refreshAll();
}
@ -162,7 +161,6 @@ @@ -162,7 +161,6 @@
listNews->clear();
refreshNewsList();
}
//updateAllStreamsName();
}
// fills the newsList
@ -191,9 +189,13 @@ @@ -191,9 +189,13 @@
}
// show the number of news for a stream, his status and an icon
void RSSImp::updateStreamName(const unsigned short& i) {
void RSSImp::updateStreamName(const unsigned short& i, const unsigned short& type) {
if(type == ICON) {
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath())));
}
else if(type == NEWS) {
unsigned short nbitem = rssmanager.getStream(i)->getListSize();
listStreams->item(i)->setText(rssmanager.getStream(i)->getAlias()+" ("+QString::number(nbitem,10).toUtf8()+")");
listStreams->item(i)->setText(rssmanager.getStream(i)->getAlias().toUtf8()+" "+rssmanager.getStream(i)->getIconPath().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
@ -202,35 +204,13 @@ @@ -202,35 +204,13 @@
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green")));
if(!rssmanager.getStream(i)->isRead())
listStreams->item(i)->setData(Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20)));
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/rss.png")));
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath())));
if(listStreams->currentRow()==i) {
listNews->clear();
refreshNewsList();
}
}
// show the number of news for each stream
/*void RSSImp::updateAllStreamsName() {
unsigned short nbstream = rssmanager.getNbStream();
for(unsigned short i=0; i<nbstream; i++) {
unsigned short nbitem = rssmanager.getStream(i)->getListSize();
listStreams->item(i)->setText(rssmanager.getStream(i)->getAlias()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("orange")));
else
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green")));
if(!rssmanager.getStream(i)->isRead())
listStreams->item(i)->setData(Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20)));
}
int currentStream = listStreams->currentRow();
if(currentStream>=0 && currentStream<nbstream) {
listStreams->setCurrentRow(currentStream);
listNews->clear();
refreshNewsList();
}
}*/
RSSImp::RSSImp() : QWidget(){
setupUi(this);
@ -243,11 +223,14 @@ @@ -243,11 +223,14 @@
connect(actionRefresh, SIGNAL(triggered()), this, SLOT(refreshStream()));
connect(actionCreate, SIGNAL(triggered()), this, SLOT(createStream()));
connect(actionRefreshAll, SIGNAL(triggered()), this, SLOT(refreshAllStreams()));
connect(&rssmanager, SIGNAL(streamNeedRefresh(const unsigned short&)), this, SLOT(updateStreamName(const unsigned short&)));
connect(&rssmanager, SIGNAL(streamNeedRefresh(const unsigned short&, const unsigned short&)), this, SLOT(updateStreamName(const unsigned short&, const unsigned short&)));
refreshStreamList();
unsigned short nbstream = rssmanager.getNbStream();
for(unsigned short i=0; i<nbstream; i++) {
listStreams->item(i)->setData(Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red")));
}
refreshTextBrowser();
// force the first alias-refresh
//QTimer::singleShot(10000, this, SLOT(updateAllStreamsName()));
}
RSSImp::~RSSImp(){

2
src/rss_imp.h

@ -46,7 +46,7 @@ class RSSImp : public QWidget, public Ui::RSS{ @@ -46,7 +46,7 @@ class RSSImp : public QWidget, public Ui::RSS{
void renameStream();
void refreshStream();
void createStream();
void updateStreamName(const unsigned short&);
void updateStreamName(const unsigned short&, const unsigned short&);
//void updateAllStreamsName();
void refreshAllStreams();
void refreshStreamList();

Loading…
Cancel
Save