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 @@
<file>Icons/splash.png</file> <file>Icons/splash.png</file>
<file>Icons/home.png</file> <file>Icons/home.png</file>
<file>Icons/uparrow.png</file> <file>Icons/uparrow.png</file>
<file>Icons/rss.png</file>
<file>Icons/downarrow.png</file> <file>Icons/downarrow.png</file>
<file>Icons/connection.png</file> <file>Icons/connection.png</file>
<file>Icons/add_file.png</file>
<file>Icons/refresh.png</file> <file>Icons/refresh.png</file>
<file>Icons/rss.png</file>
<file>Icons/add_folder.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/portugal.png</file>
<file>Icons/flags/france.png</file> <file>Icons/flags/france.png</file>
<file>Icons/flags/ukraine.png</file> <file>Icons/flags/ukraine.png</file>

62
src/rss.h

@ -29,6 +29,9 @@
// avoid crash if too many refresh // avoid crash if too many refresh
#define REFRESH_FREQ_MAX 5000 #define REFRESH_FREQ_MAX 5000
#define ICON 0
#define NEWS 1
#include <QFile> #include <QFile>
#include <QList> #include <QList>
#include <curl/curl.h> #include <curl/curl.h>
@ -36,6 +39,7 @@
#include <QSettings> #include <QSettings>
#include <QDomDocument> #include <QDomDocument>
#include <QTime> #include <QTime>
#include <QUrl>
#include "misc.h" #include "misc.h"
#include "downloadThread.h" #include "downloadThread.h"
@ -118,13 +122,14 @@ class RssStream : public QObject{
QString image; QString image;
QString url; QString url;
QString filePath; QString filePath;
QString iconPath;
QList<RssItem*> listItem; QList<RssItem*> listItem;
downloadThread* downloader; downloadThread* downloader;
QTime lastRefresh; QTime lastRefresh;
bool read; bool read;
signals: signals:
void refreshFinished(const QString& msg); void refreshFinished(const QString& msg, const unsigned short& type);
public slots : public slots :
// read and store the downloaded rss' informations // read and store the downloaded rss' informations
@ -138,13 +143,31 @@ class RssStream : public QObject{
// Download failed // Download failed
qDebug("(download failure) "+file_path.toUtf8()); qDebug("(download failure) "+file_path.toUtf8());
if(QFile::exists(filePath)) { if(QFile::exists(filePath)) {
QFile::remove(file_path); QFile::remove(filePath);
} }
emit refreshFinished(url); emit refreshFinished(url, NEWS);
return; return;
} }
openRss(); 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: public:
@ -152,9 +175,11 @@ class RssStream : public QObject{
url = _url; url = _url;
alias = url; alias = url;
read = true; read = true;
iconPath = ":/Icons/rss.png";
downloader = new downloadThread(this); 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&))); connect(downloader, SIGNAL(downloadFinished(const QString&, const QString&, int, const QString&)), this, SLOT(processDownloadedFile(const QString&, const QString&, int, const QString&)));
downloader->downloadUrl(url); downloader->downloadUrl(url);
getIcon();
lastRefresh.start(); lastRefresh.start();
} }
@ -163,6 +188,8 @@ class RssStream : public QObject{
delete downloader; delete downloader;
if(QFile::exists(filePath)) if(QFile::exists(filePath))
QFile::remove(filePath); QFile::remove(filePath);
if(QFile::exists(iconPath) && iconPath!=":/Icons/rss.png")
QFile::remove(iconPath);
} }
// delete all the items saved // delete all the items saved
@ -212,6 +239,10 @@ class RssStream : public QObject{
return filePath; return filePath;
} }
QString getIconPath() const{
return filePath;
}
RssItem* getItem(unsigned int index) const{ RssItem* getItem(unsigned int index) const{
return listItem.at(index); return listItem.at(index);
} }
@ -236,6 +267,13 @@ class RssStream : public QObject{
read = true; 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: private:
// read and create items from a rss document // read and create items from a rss document
short readDoc(const QDomDocument& doc) { short readDoc(const QDomDocument& doc) {
@ -342,11 +380,11 @@ class RssManager : public QObject{
QStringList streamListUrl; QStringList streamListUrl;
signals: signals:
void streamNeedRefresh(const unsigned short&); void streamNeedRefresh(const unsigned short&, const unsigned short&);
public slots : public slots :
void streamNeedRefresh(const QString& _url) { void streamNeedRefresh(const QString& _url, const unsigned short& type) {
emit(streamNeedRefresh(hasStream(_url))); emit(streamNeedRefresh(hasStream(_url), type));
} }
public : public :
@ -380,7 +418,7 @@ class RssManager : public QObject{
RssStream *stream = new RssStream(streamListUrl.at(i)); RssStream *stream = new RssStream(streamListUrl.at(i));
stream->setAlias(streamListAlias.at(i)); stream->setAlias(streamListAlias.at(i));
streamList.append(stream); 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{
if(hasStream(stream) < 0){ if(hasStream(stream) < 0){
streamList.append(stream); streamList.append(stream);
streamListUrl.append(stream->getUrl()); 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{ }else{
qDebug("Not adding the Rss stream because it is already in the list"); qDebug("Not adding the Rss stream because it is already in the list");
} }
@ -426,7 +464,7 @@ class RssManager : public QObject{
RssStream* stream = new RssStream(url); RssStream* stream = new RssStream(url);
streamList.append(stream); streamList.append(stream);
streamListUrl.append(url); 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 { }else {
qDebug("Not adding the Rss stream because it is already in the list"); qDebug("Not adding the Rss stream because it is already in the list");
} }
@ -458,7 +496,7 @@ class RssManager : public QObject{
unsigned int streamListUrlSize = streamListUrl.size(); unsigned int streamListUrlSize = streamListUrl.size();
for(unsigned int i=0; i<streamListUrlSize; ++i){ for(unsigned int i=0; i<streamListUrlSize; ++i){
getStream(i)->refresh(); 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{
if(index>=0 && index<getNbStream()) { if(index>=0 && index<getNbStream()) {
if(getStream(index)->getLastRefreshElapsed()>REFRESH_FREQ_MAX) { if(getStream(index)->getLastRefreshElapsed()>REFRESH_FREQ_MAX) {
getStream(index)->refresh(); 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 @@
// display the news of a stream when click on it // display the news of a stream when click on it
void RSSImp::on_listStreams_clicked() { void RSSImp::on_listStreams_clicked() {
rssmanager.getStream(listStreams->currentRow())->setRead(); rssmanager.getStream(listStreams->currentRow())->setRead();
//streamNeedRefresh(listStreams->currentRow());
listStreams->item(listStreams->currentRow())->setData(Qt::BackgroundRole, QVariant(QColor("white"))); listStreams->item(listStreams->currentRow())->setData(Qt::BackgroundRole, QVariant(QColor("white")));
refreshNewsList(); refreshNewsList();
} }
@ -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); 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) { if(ok) {
rssmanager.setAlias(index, newAlias); rssmanager.setAlias(index, newAlias);
updateStreamName(index); updateStreamName(index, NEWS);
} }
} }
@ -121,7 +120,7 @@
if(rssmanager.getNbStream()>0) { if(rssmanager.getNbStream()>0) {
textBrowser->clear(); textBrowser->clear();
listNews->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); rssmanager.refresh(index);
} }
} }
@ -132,7 +131,7 @@
listNews->clear(); listNews->clear();
unsigned short nbstream = rssmanager.getNbStream(); unsigned short nbstream = rssmanager.getNbStream();
for(unsigned short i=0; i<nbstream; i++) 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(); rssmanager.refreshAll();
} }
@ -162,7 +161,6 @@
listNews->clear(); listNews->clear();
refreshNewsList(); refreshNewsList();
} }
//updateAllStreamsName();
} }
// fills the newsList // fills the newsList
@ -191,9 +189,13 @@
} }
// show the number of news for a stream, his status and an icon // 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(); 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) if(nbitem==0)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red"))); listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY) else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
@ -202,35 +204,13 @@
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green"))); listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green")));
if(!rssmanager.getStream(i)->isRead()) if(!rssmanager.getStream(i)->isRead())
listStreams->item(i)->setData(Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20))); 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) { if(listStreams->currentRow()==i) {
listNews->clear(); listNews->clear();
refreshNewsList(); 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(){ RSSImp::RSSImp() : QWidget(){
setupUi(this); setupUi(this);
@ -243,11 +223,14 @@
connect(actionRefresh, SIGNAL(triggered()), this, SLOT(refreshStream())); connect(actionRefresh, SIGNAL(triggered()), this, SLOT(refreshStream()));
connect(actionCreate, SIGNAL(triggered()), this, SLOT(createStream())); connect(actionCreate, SIGNAL(triggered()), this, SLOT(createStream()));
connect(actionRefreshAll, SIGNAL(triggered()), this, SLOT(refreshAllStreams())); 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(); 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(); refreshTextBrowser();
// force the first alias-refresh
//QTimer::singleShot(10000, this, SLOT(updateAllStreamsName()));
} }
RSSImp::~RSSImp(){ RSSImp::~RSSImp(){

2
src/rss_imp.h

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

Loading…
Cancel
Save