Browse Source

rss : work to improve algorithm & memory

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

69
src/rss.h

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

63
src/rss_imp.cpp

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

5
src/rss_imp.h

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

Loading…
Cancel
Save