diff --git a/src/rss.h b/src/rss.h index 0a76a9d35..d658c85ec 100644 --- a/src/rss.h +++ b/src/rss.h @@ -22,9 +22,12 @@ #ifndef RSS_H #define RSS_H -#define STREAM_MAX_ITEM 15 +// MAX ITEM A STREAM +#define STREAM_MAX_ITEM 18 // FIXME: not used yet #define GLOBAL_MAX_ITEM 150 +// avoid crash if too many refresh +#define REFRESH_FREQ_MAX 5000 #include #include @@ -32,11 +35,14 @@ #include #include #include +#include #include "misc.h" #include "downloadThread.h" +class RssManager; class RssStream; +class RssItem; // Item of a rss stream, single information class RssItem{ @@ -47,14 +53,12 @@ class RssItem{ QString description; QString image; bool read; - RssStream* parent; QString downloadLink; public: // public constructor - RssItem(const QDomElement& properties, RssStream* _parent){ + RssItem(const QDomElement& properties){ read = false; - parent = _parent; downloadLink = "none"; QDomElement property = properties.firstChild().toElement(); while(!property.isNull()) { @@ -68,7 +72,6 @@ class RssItem{ image = property.text(); property = property.nextSibling().toElement(); } - //displayItem(); } ~RssItem(){ @@ -101,14 +104,6 @@ class RssItem{ void setRead(){ read = true; } - - RssStream* getParent() const{ - return parent; - } - - void displayItem(){ - qDebug(" - "+title.toUtf8()+" - "+link.toUtf8()); - } }; // Rss stream, loaded form an xml file @@ -125,7 +120,11 @@ class RssStream : public QObject{ QString filePath; QList listItem; downloadThread* downloader; + QTime lastRefresh; + signals: + void refreshFinished(const QString& msg); + public slots : // read and store the downloaded rss' informations void processDownloadedFile(const QString&, const QString& file_path, int return_code, const QString&) { @@ -143,6 +142,7 @@ class RssStream : public QObject{ return; } openRss(); + emit refreshFinished("plop"); } public: @@ -152,6 +152,7 @@ class RssStream : public QObject{ 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); + lastRefresh.start(); } ~RssStream(){ @@ -169,6 +170,12 @@ class RssStream : public QObject{ } } + void refresh() { + connect(downloader, SIGNAL(downloadFinished(const QString&, const QString&, int, const QString&)), this, SLOT(processDownloadedFile(const QString&, const QString&, int, const QString&))); + downloader->downloadUrl(url); + lastRefresh.start(); + } + QString getTitle() const{ return title; } @@ -214,13 +221,9 @@ class RssStream : public QObject{ return listItem; } - void displayStream(){ - qDebug(" # "+getTitle().toUtf8()+" - "+getUrl().toUtf8()+" - "+getAlias().toUtf8()); - unsigned int listItemSize = listItem.size(); - for(unsigned int i=0; idisplayItem(); - } - } + int getLastRefreshElapsed() const{ + return lastRefresh.elapsed(); + } private: short read(const QDomDocument& doc) { @@ -236,6 +239,7 @@ class RssStream : public QObject{ } QDomNode rss = root.firstChild(); QDomElement channel = root.firstChild().toElement(); + while(!channel.isNull()) { // we are reading the rss'main info if (channel.tagName() == "channel") { @@ -253,10 +257,10 @@ class RssStream : public QObject{ else if (property.tagName() == "image") image = property.text(); else if(property.tagName() == "item") { - if(getListSize() < STREAM_MAX_ITEM) { + if(getListSize() < 3*STREAM_MAX_ITEM) { //TODO: find a way to break here //add it to a list - listItem.append(new RssItem(property, this)); + listItem.append(new RssItem(property)); } } property = property.nextSibling().toElement(); @@ -264,9 +268,28 @@ class RssStream : public QObject{ } channel = channel.nextSibling().toElement(); } + // resize stream listItem + resizeList(); return 0; } + void resizeList() { + unsigned short lastindex = 0; + QString firstTitle = getItem(0)->getTitle(); + unsigned short listsize = getListSize(); + for(unsigned short i=0; igetTitle() == firstTitle) + lastindex = i; + } + for(unsigned short i=0; iSTREAM_MAX_ITEM) { + listItem.removeLast(); + } + + } + // existing and opening test after download short openRss(){ QDomDocument doc("Rss Seed"); @@ -297,13 +320,24 @@ class RssStream : public QObject{ }; // global class, manage the whole rss stream -class RssManager{ - +class RssManager : public QObject{ + Q_OBJECT + private : QList streamList; QStringList streamListUrl; QStringList streamListAlias; + signals: + void streamNeedRefresh(); + + public slots : + // read and store the downloaded rss' informations + void refreshFinished(const QString&) { + + qDebug("*******************************************************"); + } + public : RssManager(){ loadStreamList(); @@ -331,6 +365,7 @@ class RssManager{ RssStream *stream = new RssStream(streamListUrl.at(i)); stream->setAlias(streamListAlias.at(i)); streamList.append(stream); + connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh())); } } @@ -349,6 +384,7 @@ class RssManager{ streamList.append(stream); streamListUrl.append(stream->getUrl()); streamListAlias.append(stream->getUrl()); + connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh())); }else{ qDebug("Not adding the Rss stream because it is already in the list"); } @@ -410,15 +446,20 @@ class RssManager{ RssStream *stream = new RssStream(streamListUrl.at(i)); stream->setAlias(streamListAlias.at(i)); streamList.append(stream); + connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh())); } } void refresh(int index) { if(index>=0 && indexsetAlias(streamListAlias.at(index)); - streamList.replace(index, stream); + if(getStream(index)->getLastRefreshElapsed()>REFRESH_FREQ_MAX) { + //delete getStream(index); + //RssStream *stream = new RssStream(streamListUrl.at(index)); + //stream->setAlias(streamListAlias.at(index)); + //streamList.replace(index, stream); + getStream(index)->refresh(); + connect(getStream(index), SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh())); + } } } @@ -435,13 +476,6 @@ class RssManager{ return streamList.at(index); } - void displayManager(){ - unsigned int streamListSize = streamList.size(); - for(unsigned int i=0; idisplayStream(); - } - } - int getNbStream() { return streamList.size(); } diff --git a/src/rss_imp.cpp b/src/rss_imp.cpp index 8776d9879..f31740780 100644 --- a/src/rss_imp.cpp +++ b/src/rss_imp.cpp @@ -114,25 +114,21 @@ //right-clik on stream : refresh it void RSSImp::refreshStream() { - if(rssmanager.getNbStream()>0 && lastRefresh.elapsed()>REFRESH_FREQ_MAX) { - int index = listStreams->currentRow(); + int index = listStreams->currentRow(); + if(rssmanager.getNbStream()>0) { textBrowser->clear(); listNews->clear(); rssmanager.refresh(index); refreshStreamList(); - lastRefresh.start(); } } //right-click somewhere, refresh all the streams void RSSImp::refreshAllStreams() { - if(lastRefresh.elapsed()>REFRESH_FREQ_MAX) { - textBrowser->clear(); - listNews->clear(); - rssmanager.refreshAll(); - refreshStreamList(); - lastRefresh.start(); - } + textBrowser->clear(); + listNews->clear(); + rssmanager.refreshAll(); + refreshStreamList(); } //right-click, register a new stream @@ -180,7 +176,7 @@ } // show the number of news for each stream - void RSSImp::updateStreamNbNews() { + void RSSImp::updateStreamsName() { for(int i=0; iitem(i)->setText(rssmanager.getStream(i)->getAlias()+" ("+QString::number(rssmanager.getStream(i)->getListSize(),10).toUtf8()+")"); } @@ -200,9 +196,11 @@ refreshStreamList(); refreshTextBrowser(); timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(updateStreamNbNews())); + //connect(timer, SIGNAL(timeout()), this, SLOT(updateStreamsName())); timer->start(5000); - lastRefresh.start(); + //for(int i=0; i -#include #include "ui_rss.h" #include "rss.h" #include "GUI.h" @@ -36,7 +32,6 @@ class RSSImp : public QWidget, public Ui::RSS{ private: RssManager rssmanager; QTimer* timer; - QTime lastRefresh; protected slots: void on_addStream_button_clicked(); @@ -50,7 +45,7 @@ class RSSImp : public QWidget, public Ui::RSS{ void renameStream(); void refreshStream(); void createStream(); - void updateStreamNbNews(); + void updateStreamsName(); void refreshAllStreams(); void refreshStreamList(); void refreshNewsList();