Browse Source

rss : work to improve algorithm & memory

implements some color
adaptive-webui-19844
Arnaud Demaiziere 18 years ago
parent
commit
2ce51400aa
  1. 71
      src/rss.h
  2. 71
      src/rss_imp.cpp
  3. 5
      src/rss_imp.h

71
src/rss.h

@ -121,6 +121,7 @@ class RssStream : public QObject{
QList<RssItem*> listItem; QList<RssItem*> listItem;
downloadThread* downloader; downloadThread* downloader;
QTime lastRefresh; QTime lastRefresh;
bool read;
signals: signals:
void refreshFinished(const QString& msg); void refreshFinished(const QString& msg);
@ -139,6 +140,7 @@ class RssStream : public QObject{
if(QFile::exists(filePath)) { if(QFile::exists(filePath)) {
QFile::remove(file_path); QFile::remove(file_path);
} }
emit refreshFinished(url);
return; return;
} }
openRss(); openRss();
@ -149,6 +151,7 @@ class RssStream : public QObject{
RssStream(const QString& _url) { RssStream(const QString& _url) {
url = _url; url = _url;
alias = url; alias = url;
read = true;
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);
@ -213,7 +216,7 @@ class RssStream : public QObject{
return listItem.at(index); return listItem.at(index);
} }
unsigned int getListSize() const{ unsigned short getListSize() const{
return listItem.size(); return listItem.size();
} }
@ -221,13 +224,21 @@ class RssStream : public QObject{
return listItem; return listItem;
} }
int getLastRefreshElapsed() const{ unsigned int getLastRefreshElapsed() const{
return lastRefresh.elapsed(); return lastRefresh.elapsed();
} }
bool isRead() const {
return read;
}
void setRead() {
read = true;
}
private: private:
// read and create items from a rss document // read and create items from a rss document
short read(const QDomDocument& doc) { short readDoc(const QDomDocument& doc) {
// is it a rss file ? // is it a rss file ?
QDomElement root = doc.documentElement(); QDomElement root = doc.documentElement();
if(root.tagName() == "html"){ if(root.tagName() == "html"){
@ -268,6 +279,7 @@ class RssStream : public QObject{
} }
property = property.nextSibling().toElement(); property = property.nextSibling().toElement();
} }
read = false;
} }
channel = channel.nextSibling().toElement(); channel = channel.nextSibling().toElement();
} }
@ -312,7 +324,7 @@ class RssStream : public QObject{
return -1; return -1;
} }
// start reading the xml // start reading the xml
short return_lecture = read(doc); short return_lecture = readDoc(doc);
fileRss.close(); fileRss.close();
if(QFile::exists(filePath)) { if(QFile::exists(filePath)) {
fileRss.remove(); fileRss.remove();
@ -328,10 +340,9 @@ class RssManager : public QObject{
private : private :
QList<RssStream*> streamList; QList<RssStream*> streamList;
QStringList streamListUrl; QStringList streamListUrl;
QStringList streamListAlias;
signals: signals:
void streamNeedRefresh(const int&); void streamNeedRefresh(const unsigned short&);
public slots : public slots :
void streamNeedRefresh(const QString& _url) { void streamNeedRefresh(const QString& _url) {
@ -356,8 +367,12 @@ class RssManager : public QObject{
QSettings settings("qBittorrent", "qBittorrent"); QSettings settings("qBittorrent", "qBittorrent");
settings.beginGroup("Rss"); settings.beginGroup("Rss");
streamListUrl = settings.value("streamList").toStringList(); streamListUrl = settings.value("streamList").toStringList();
streamListAlias = settings.value("streamAlias").toStringList(); QStringList streamListAlias = settings.value("streamAlias").toStringList();
//XXX: Maybe check that both list have same size? //check that both list have same size
while(streamListUrl.size()>streamListAlias.size())
streamListUrl.removeLast();
while(streamListAlias.size()>streamListUrl.size())
streamListAlias.removeLast();
qDebug("NB RSS streams loaded: %d", streamListUrl.size()); qDebug("NB RSS streams loaded: %d", streamListUrl.size());
settings.endGroup(); settings.endGroup();
unsigned int streamListUrlSize = streamListUrl.size(); unsigned int streamListUrlSize = streamListUrl.size();
@ -371,6 +386,12 @@ class RssManager : public QObject{
// save the list of the rss stream for the next session // save the list of the rss stream for the next session
void saveStreamList(){ void saveStreamList(){
streamListUrl.clear();
QStringList streamListAlias;
for(unsigned short i=0; i<getNbStream(); i++) {
streamListUrl.append(getStream(i)->getUrl());
streamListAlias.append(getStream(i)->getAlias());
}
QSettings settings("qBittorrent", "qBittorrent"); QSettings settings("qBittorrent", "qBittorrent");
settings.beginGroup("Rss"); settings.beginGroup("Rss");
settings.setValue("streamList", streamListUrl); settings.setValue("streamList", streamListUrl);
@ -383,7 +404,6 @@ 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());
streamListAlias.append(stream->getUrl());
connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&))); connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
}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");
@ -392,20 +412,20 @@ class RssManager : public QObject{
// add a stream to the manager // add a stream to the manager
void addStream(QString url){ void addStream(QString url){
// XXX : is it useful ?
// completion of the address // completion of the address
if(!url.endsWith(".xml")) { /*if(!url.endsWith(".xml")) {
if(url.endsWith("/")) { if(url.endsWith("/")) {
url.append("rss.xml"); url.append("rss.xml");
} else { } else {
url.append("/rss.xml"); url.append("/rss.xml");
} }
} }*/
if(hasStream(url) < 0) { if(hasStream(url) < 0) {
RssStream* stream = new RssStream(url); RssStream* stream = new RssStream(url);
streamList.append(stream); streamList.append(stream);
streamListUrl.append(url); streamListUrl.append(url);
streamListAlias.append(url);
connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&))); connect(stream, SIGNAL(refreshFinished(const QString&)), this, SLOT(streamNeedRefresh(const QString&)));
}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");
@ -418,21 +438,19 @@ class RssManager : public QObject{
if(index != -1){ if(index != -1){
delete streamList.takeAt(index); delete streamList.takeAt(index);
streamListUrl.removeAt(index); streamListUrl.removeAt(index);
streamListAlias.removeAt(index);
} }
} }
// remove all the streams in the manager // remove all the streams in the manager
void removeAll(){ void removeAll(){
QList<RssStream*> newStreamList; QList<RssStream*> newStreamList;
QStringList newUrlList, newAliasList; QStringList newUrlList;
unsigned int streamListSize = streamList.size(); unsigned int streamListSize = streamList.size();
for(unsigned int i=0; i<streamListSize; ++i){ for(unsigned int i=0; i<streamListSize; ++i){
delete getStream(i); delete getStream(i);
} }
streamList = newStreamList; streamList = newStreamList;
streamListUrl = newUrlList; streamListUrl = newUrlList;
streamListAlias = newAliasList;
} }
// reload all the xml files from the web // reload all the xml files from the web
@ -454,30 +472,37 @@ class RssManager : public QObject{
} }
// return the position index of a stream, if the manager owns it // return the position index of a stream, if the manager owns it
int hasStream(RssStream* stream) const{ short hasStream(RssStream* stream) const{
return hasStream(stream->getUrl()); return hasStream(stream->getUrl());
} }
int hasStream(const QString& url) const{ short hasStream(const QString& url) const{
return streamListUrl.indexOf(url); return streamListUrl.indexOf(url);
} }
RssStream* getStream(const int& index) const{ RssStream* getStream(const unsigned short& index) const{
return streamList.at(index); return streamList.at(index);
} }
int getNbStream() { unsigned short getNbStream() {
return streamList.size(); return streamList.size();
} }
//set an alias to an stream and save it for later //set an alias to an stream and save it for later
void setAlias(int index, QString newAlias) { void setAlias(unsigned short index, QString newAlias) {
if(newAlias.length()>=2 && !streamListAlias.contains(newAlias, Qt::CaseInsensitive)) { if(newAlias.length()>=2 && !getListAlias().contains(newAlias, Qt::CaseInsensitive)) {
getStream(index)->setAlias(newAlias); getStream(index)->setAlias(newAlias);
streamListAlias.replace(index, newAlias);
} }
} }
QStringList getListAlias() {
QStringList listAlias;
for(unsigned short i=0; i<getNbStream(); i++) {
listAlias.append(getStream(i)->getAlias());
}
return listAlias;
}
}; };
#endif #endif

71
src/rss_imp.cpp

@ -64,6 +64,9 @@
// 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();
//streamNeedRefresh(listStreams->currentRow());
listStreams->item(listStreams->currentRow())->setData(Qt::BackgroundRole, QVariant(QColor("white")));
refreshNewsList(); refreshNewsList();
} }
@ -88,10 +91,9 @@
qDebug("no stream selected"); qDebug("no stream selected");
return; return;
}else { }else {
int index = listStreams->currentRow();
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
rssmanager.removeStream(rssmanager.getStream(index)); rssmanager.removeStream(rssmanager.getStream(listStreams->currentRow()));
refreshStreamList(); refreshStreamList();
} }
} }
@ -103,11 +105,11 @@
return; return;
}else { }else {
bool ok; bool ok;
int index = listStreams->currentRow(); short index = listStreams->currentRow();
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);
refreshStreamList(); updateStreamName(index);
} }
} }
@ -115,12 +117,11 @@
//right-clik on stream : refresh it //right-clik on stream : refresh it
void RSSImp::refreshStream() { void RSSImp::refreshStream() {
int index = listStreams->currentRow(); short index = listStreams->currentRow();
if(rssmanager.getNbStream()>0) { if(rssmanager.getNbStream()>0) {
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
rssmanager.refresh(index); rssmanager.refresh(index);
refreshStreamList();
} }
} }
@ -129,7 +130,6 @@
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
rssmanager.refreshAll(); rssmanager.refreshAll();
refreshStreamList();
} }
//right-click, register a new stream //right-click, register a new stream
@ -147,16 +147,18 @@
// fills the streamList // fills the streamList
void RSSImp::refreshStreamList() { void RSSImp::refreshStreamList() {
int currentStream = listStreams->currentRow(); short currentStream = listStreams->currentRow();
unsigned short nbstream = rssmanager.getNbStream();
listStreams->clear(); listStreams->clear();
for(unsigned short i=0; i<rssmanager.getNbStream(); i++) { for(unsigned short i=0; i<nbstream; i++) {
new QListWidgetItem(rssmanager.getStream(i)->getAlias()+" ("+QString::number(rssmanager.getStream(i)->getListSize(),10).toUtf8()+")", listStreams); new QListWidgetItem(rssmanager.getStream(i)->getAlias()+" ("+QString::number(rssmanager.getStream(i)->getListSize(),10).toUtf8()+")", listStreams);
} }
listStreams->setCurrentRow(currentStream); if(currentStream>=0 && currentStream<nbstream) {
if(currentStream>=0) { listStreams->setCurrentRow(currentStream);
listNews->clear(); listNews->clear();
refreshNewsList(); refreshNewsList();
} }
updateAllStreamsName();
} }
// fills the newsList // fills the newsList
@ -164,8 +166,8 @@
if(rssmanager.getNbStream()>0) { if(rssmanager.getNbStream()>0) {
RssStream* currentstream = rssmanager.getStream(listStreams->currentRow()); RssStream* currentstream = rssmanager.getStream(listStreams->currentRow());
listNews->clear(); listNews->clear();
unsigned int currentStreamSize = currentstream->getListSize(); unsigned short currentStreamSize = currentstream->getListSize();
for(unsigned int i=0; i<currentStreamSize; ++i) { for(unsigned short i=0; i<currentStreamSize; ++i) {
new QListWidgetItem(currentstream->getItem(i)->getTitle(), listNews); new QListWidgetItem(currentstream->getItem(i)->getTitle(), listNews);
if(currentstream->getItem(i)->isRead()) if(currentstream->getItem(i)->isRead())
listNews->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("grey"))); listNews->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("grey")));
@ -182,21 +184,42 @@
} }
} }
// show the number of news for each stream // show the number of news for a stream
void RSSImp::updateStreamsName(const int& i) { void RSSImp::updateStreamName(const unsigned short& i) {
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()+" ("+QString::number(nbitem,10).toUtf8()+")");
// FIXME : the 2st conditions are incorrect
if(nbitem==0) if(nbitem==0)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("grey")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
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)
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("orange")));
else else
listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green"))); listStreams->item(i)->setData(Qt::ForegroundRole, QVariant(QColor("green")));
//qDebug(QString::number(nbitem).toUtf8()+"//"+QString::number(rssmanager.getStream(i)->getLastRefreshElapsed()).toUtf8()); if(!rssmanager.getStream(i)->isRead())
listStreams->item(i)->setData(Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20)));
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(); int currentStream = listStreams->currentRow();
listStreams->setCurrentRow(currentStream); if(currentStream>=0 && currentStream<nbstream) {
if(currentStream>=0) { listStreams->setCurrentRow(currentStream);
listNews->clear(); listNews->clear();
refreshNewsList(); refreshNewsList();
} }
@ -213,11 +236,11 @@
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 int&)), this, SLOT(updateStreamsName(const int&))); connect(&rssmanager, SIGNAL(streamNeedRefresh(const unsigned short&)), this, SLOT(updateStreamName(const unsigned short&)));
refreshStreamList(); refreshStreamList();
refreshTextBrowser(); refreshTextBrowser();
// force the first alias-refresh // force the first alias-refresh
QTimer::singleShot(10000, this, SLOT(updateStreamsName())); QTimer::singleShot(10000, this, SLOT(updateAllStreamsName()));
} }
RSSImp::~RSSImp(){ RSSImp::~RSSImp(){

5
src/rss_imp.h

@ -21,7 +21,7 @@
#ifndef __RSS_IMP_H__ #ifndef __RSS_IMP_H__
#define __RSS_IMP_H__ #define __RSS_IMP_H__
#define REFRESH_MAX_LATENCY 6000 #define REFRESH_MAX_LATENCY 600000
#include <QTimer> #include <QTimer>
#include "ui_rss.h" #include "ui_rss.h"
@ -46,7 +46,8 @@ class RSSImp : public QWidget, public Ui::RSS{
void renameStream(); void renameStream();
void refreshStream(); void refreshStream();
void createStream(); void createStream();
void updateStreamsName(const int&); void updateStreamName(const unsigned short&);
void updateAllStreamsName();
void refreshAllStreams(); void refreshAllStreams();
void refreshStreamList(); void refreshStreamList();
void refreshNewsList(); void refreshNewsList();

Loading…
Cancel
Save