Browse Source

- Improved rss includes a little and fix a memory leak I just introduced

adaptive-webui-19844
Christophe Dumez 18 years ago
parent
commit
042323a1a8
  1. 74
      src/rss_imp.cpp
  2. 4
      src/rss_imp.h

74
src/rss_imp.cpp

@ -20,6 +20,8 @@
*/ */
#include "rss_imp.h" #include "rss_imp.h"
#include "rss.h"
#include <QDesktopServices> #include <QDesktopServices>
#include <QInputDialog> #include <QInputDialog>
#include <QMenu> #include <QMenu>
@ -50,7 +52,7 @@
// delete a stream by a button // delete a stream by a button
void RSSImp::on_delStream_button_clicked() { void RSSImp::on_delStream_button_clicked() {
if(getNumStreamSelected()<0 || rssmanager.getNbStreams()==0) { if(getNumStreamSelected()<0 || rssmanager->getNbStreams()==0) {
qDebug("no stream selected"); qDebug("no stream selected");
return; return;
}else { }else {
@ -60,7 +62,7 @@
if(ok==0) { if(ok==0) {
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
rssmanager.removeStream(rssmanager.getStream(getNumStreamSelected())); rssmanager->removeStream(rssmanager->getStream(getNumStreamSelected()));
refreshStreamList(); refreshStreamList();
} }
} }
@ -73,9 +75,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() {
if(rssmanager.getNbStreams()>0) { if(rssmanager->getNbStreams()>0) {
moveCurrentItem(); moveCurrentItem();
rssmanager.getStream(getNumStreamSelected())->setRead(); rssmanager->getStream(getNumStreamSelected())->setRead();
// update the color of the stream, is it old ? // update the color of the stream, is it old ?
updateStreamName(getNumStreamSelected(), LATENCY); updateStreamName(getNumStreamSelected(), LATENCY);
refreshNewsList(); refreshNewsList();
@ -91,8 +93,8 @@
// open the url of the news in a browser // open the url of the news in a browser
void RSSImp::on_listNews_doubleClicked() { void RSSImp::on_listNews_doubleClicked() {
if(getNumStreamSelected()>=0 && listNews->currentRow()>=0 && rssmanager.getStream(getNumStreamSelected())->getListSize()>0) { if(getNumStreamSelected()>=0 && listNews->currentRow()>=0 && rssmanager->getStream(getNumStreamSelected())->getListSize()>0) {
RssItem* currentItem = rssmanager.getStream(getNumStreamSelected())->getItem(listNews->currentRow()); RssItem* currentItem = rssmanager->getStream(getNumStreamSelected())->getItem(listNews->currentRow());
if(currentItem->getLink()!=NULL && currentItem->getLink().length()>5) if(currentItem->getLink()!=NULL && currentItem->getLink().length()>5)
QDesktopServices::openUrl(QUrl(currentItem->getLink())); QDesktopServices::openUrl(QUrl(currentItem->getLink()));
} }
@ -111,7 +113,7 @@
//right-clik on stream : delete it //right-clik on stream : delete it
void RSSImp::deleteStream() { void RSSImp::deleteStream() {
if(rssmanager.getNbStreams()==0) { if(rssmanager->getNbStreams()==0) {
qDebug("no stream selected"); qDebug("no stream selected");
return; return;
}else { }else {
@ -120,7 +122,7 @@
moveCurrentItem(); moveCurrentItem();
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
rssmanager.removeStream(rssmanager.getStream(getNumStreamSelected())); rssmanager->removeStream(rssmanager->getStream(getNumStreamSelected()));
refreshStreamList(); refreshStreamList();
} }
} }
@ -128,16 +130,16 @@
//right-clik on stream : give him an alias //right-clik on stream : give him an alias
void RSSImp::renameStream() { void RSSImp::renameStream() {
if(rssmanager.getNbStreams()==0) { if(rssmanager->getNbStreams()==0) {
qDebug("no stream selected"); qDebug("no stream selected");
return; return;
}else { }else {
moveCurrentItem(); moveCurrentItem();
bool ok; bool ok;
short index = getNumStreamSelected(); short index = getNumStreamSelected();
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, NEWS); updateStreamName(index, NEWS);
} }
} }
@ -146,13 +148,13 @@
//right-clik on stream : refresh it //right-clik on stream : refresh it
void RSSImp::refreshStream() { void RSSImp::refreshStream() {
if(rssmanager.getNbStreams()>0) { if(rssmanager->getNbStreams()>0) {
moveCurrentItem(); moveCurrentItem();
short index = getNumStreamSelected(); short index = getNumStreamSelected();
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
listStreams->topLevelItem(index)->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png"))); listStreams->topLevelItem(index)->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
rssmanager.refresh(index); rssmanager->refresh(index);
} }
updateLastRefreshedTimeForStreams(); updateLastRefreshedTimeForStreams();
} }
@ -161,10 +163,10 @@
void RSSImp::refreshAllStreams() { void RSSImp::refreshAllStreams() {
textBrowser->clear(); textBrowser->clear();
listNews->clear(); listNews->clear();
unsigned short nbstream = rssmanager.getNbStreams(); unsigned short nbstream = rssmanager->getNbStreams();
for(unsigned short i=0; i<nbstream; ++i) for(unsigned short i=0; i<nbstream; ++i)
listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png"))); listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/loading.png")));
rssmanager.refreshAll(); rssmanager->refreshAll();
updateLastRefreshedTimeForStreams(); updateLastRefreshedTimeForStreams();
} }
@ -175,7 +177,7 @@
if(ok) { if(ok) {
newUrl = newUrl.trimmed(); newUrl = newUrl.trimmed();
if(!newUrl.isEmpty() && newUrl != "http://"){ if(!newUrl.isEmpty() && newUrl != "http://"){
rssmanager.addStream(newUrl); rssmanager->addStream(newUrl);
refreshStreamList(); refreshStreamList();
} }
} }
@ -184,21 +186,21 @@
// fills the streamList // fills the streamList
void RSSImp::refreshStreamList() { void RSSImp::refreshStreamList() {
qDebug("Refreshing stream list"); qDebug("Refreshing stream list");
unsigned short nbstream = rssmanager.getNbStreams(); unsigned short nbstream = rssmanager->getNbStreams();
listStreams->clear(); listStreams->clear();
QList<QTreeWidgetItem *> streams; QList<QTreeWidgetItem *> streams;
for(unsigned short i=0; i<nbstream; ++i) { for(unsigned short i=0; i<nbstream; ++i) {
QTreeWidgetItem* stream = new QTreeWidgetItem(listStreams); QTreeWidgetItem* stream = new QTreeWidgetItem(listStreams);
updateStreamName(i, NEWS); updateStreamName(i, NEWS);
stream->setToolTip(0, QString("<b>")+tr("Description:")+QString("</b> ")+rssmanager.getStream(i)->getDescription()+QString("<br/><b>")+tr("url:")+QString("</b> ")+rssmanager.getStream(i)->getUrl()+QString("<br/><b>")+tr("Last refresh:")+QString("</b> ")+rssmanager.getStream(i)->getLastRefreshElapsedString()); stream->setToolTip(0, QString("<b>")+tr("Description:")+QString("</b> ")+rssmanager->getStream(i)->getDescription()+QString("<br/><b>")+tr("url:")+QString("</b> ")+rssmanager->getStream(i)->getUrl()+QString("<br/><b>")+tr("Last refresh:")+QString("</b> ")+rssmanager->getStream(i)->getLastRefreshElapsedString());
} }
qDebug("Stream list refreshed"); qDebug("Stream list refreshed");
} }
// fills the newsList // fills the newsList
void RSSImp::refreshNewsList() { void RSSImp::refreshNewsList() {
if(rssmanager.getNbStreams()>0) { if(rssmanager->getNbStreams()>0) {
RssStream* currentstream = rssmanager.getStream(getNumStreamSelected()); RssStream* currentstream = rssmanager->getStream(getNumStreamSelected());
listNews->clear(); listNews->clear();
unsigned short currentStreamSize = currentstream->getListSize(); unsigned short currentStreamSize = currentstream->getListSize();
for(unsigned short i=0; i<currentStreamSize; ++i) { for(unsigned short i=0; i<currentStreamSize; ++i) {
@ -219,16 +221,16 @@
// display a news // display a news
void RSSImp::refreshTextBrowser() { void RSSImp::refreshTextBrowser() {
if(getNumStreamSelected()>=0 && listNews->currentRow()>=0) { if(getNumStreamSelected()>=0 && listNews->currentRow()>=0) {
RssItem* currentitem = rssmanager.getStream(getNumStreamSelected())->getItem(listNews->currentRow()); RssItem* currentitem = rssmanager->getStream(getNumStreamSelected())->getItem(listNews->currentRow());
textBrowser->setHtml(currentitem->getTitle()+" : \n"+currentitem->getDescription()); textBrowser->setHtml(currentitem->getTitle()+" : \n"+currentitem->getDescription());
currentitem->setRead(); currentitem->setRead();
} }
} }
void RSSImp::updateLastRefreshedTimeForStreams() { void RSSImp::updateLastRefreshedTimeForStreams() {
unsigned int nbStreams = rssmanager.getNbStreams(); unsigned int nbStreams = rssmanager->getNbStreams();
for(unsigned int i=0; i<nbStreams; ++i){ for(unsigned int i=0; i<nbStreams; ++i){
listStreams->topLevelItem(i)->setToolTip(0, QString("<b>")+tr("Description:")+QString("</b> ")+rssmanager.getStream(i)->getDescription()+QString("<br/><b>")+tr("url:")+QString("</b> ")+rssmanager.getStream(i)->getUrl()+QString("<br/><b>")+tr("Last refresh:")+QString("</b> ")+rssmanager.getStream(i)->getLastRefreshElapsedString()); listStreams->topLevelItem(i)->setToolTip(0, QString("<b>")+tr("Description:")+QString("</b> ")+rssmanager->getStream(i)->getDescription()+QString("<br/><b>")+tr("url:")+QString("</b> ")+rssmanager->getStream(i)->getUrl()+QString("<br/><b>")+tr("Last refresh:")+QString("</b> ")+rssmanager->getStream(i)->getLastRefreshElapsedString());
} }
} }
@ -236,15 +238,15 @@
void RSSImp::updateStreamName(const unsigned short& i, const unsigned short& type) { void RSSImp::updateStreamName(const unsigned short& i, const unsigned short& type) {
// icon has just been download // icon has just been download
if(type == ICON) { if(type == ICON) {
listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath()))); listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(rssmanager->getStream(i)->getIconPath())));
} }
// on click, show the age of the stream // on click, show the age of the stream
if(type == LATENCY) { if(type == LATENCY) {
unsigned short nbitem = rssmanager.getStream(i)->getNbNonRead(); unsigned short nbitem = rssmanager->getStream(i)->getNbNonRead();
listStreams->topLevelItem(i)->setText(0,rssmanager.getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")"); listStreams->topLevelItem(i)->setText(0,rssmanager->getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0) if(nbitem==0)
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("red"))); listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY) else if(rssmanager->getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("orange"))); listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("orange")));
else else
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("green"))); listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("green")));
@ -252,24 +254,24 @@
} }
// when news are refreshed, update all informations // when news are refreshed, update all informations
if(type == NEWS) { if(type == NEWS) {
unsigned short nbitem = rssmanager.getStream(i)->getListSize(); unsigned short nbitem = rssmanager->getStream(i)->getListSize();
listStreams->topLevelItem(i)->setText(0,rssmanager.getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")"); listStreams->topLevelItem(i)->setText(0,rssmanager->getStream(i)->getAlias().toUtf8()+" ("+QString::number(nbitem,10).toUtf8()+")");
if(nbitem==0) if(nbitem==0)
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("red"))); listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("red")));
else if(rssmanager.getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY) else if(rssmanager->getStream(i)->getLastRefreshElapsed()>REFRESH_MAX_LATENCY)
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("orange"))); listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("orange")));
else else
listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("green"))); listStreams->topLevelItem(i)->setData(0,Qt::ForegroundRole, QVariant(QColor("green")));
if(!rssmanager.getStream(i)->isRead()) if(!rssmanager->getStream(i)->isRead())
listStreams->topLevelItem(i)->setData(0,Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20))); listStreams->topLevelItem(i)->setData(0,Qt::BackgroundRole, QVariant(QColor(0, 255, 0, 20)));
if(getNumStreamSelected()==i) { if(getNumStreamSelected()==i) {
listNews->clear(); listNews->clear();
refreshNewsList(); refreshNewsList();
} }
listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(rssmanager.getStream(i)->getIconPath()))); listStreams->topLevelItem(i)->setData(0,Qt::DecorationRole, QVariant(QIcon(rssmanager->getStream(i)->getIconPath())));
// update description and display last refresh // update description and display last refresh
listStreams->topLevelItem(i)->setToolTip(0, QString("<b>")+tr("Description:")+QString("</b> ")+rssmanager.getStream(i)->getDescription()+QString("<br/><b>")+tr("url:")+QString("</b> ")+rssmanager.getStream(i)->getUrl()+QString("<br/><b>")+tr("Last refresh:")+QString("</b> ")+rssmanager.getStream(i)->getLastRefreshElapsedString()); listStreams->topLevelItem(i)->setToolTip(0, QString("<b>")+tr("Description:")+QString("</b> ")+rssmanager->getStream(i)->getDescription()+QString("<br/><b>")+tr("url:")+QString("</b> ")+rssmanager->getStream(i)->getUrl()+QString("<br/><b>")+tr("Last refresh:")+QString("</b> ")+rssmanager->getStream(i)->getLastRefreshElapsedString());
} }
} }
@ -286,13 +288,15 @@
actionCreate->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png"))); actionCreate->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png")));
actionRefreshAll->setIcon(QIcon(QString::fromUtf8(":/Icons/refresh.png"))); actionRefreshAll->setIcon(QIcon(QString::fromUtf8(":/Icons/refresh.png")));
rssmanager = new RssManager();
connect(rssmanager, SIGNAL(streamNeedRefresh(const unsigned short&, const unsigned short&)), this, SLOT(updateStreamName(const unsigned short&, const unsigned short&)));
connect(listStreams, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRSSListMenu(const QPoint&))); connect(listStreams, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRSSListMenu(const QPoint&)));
connect(actionDelete, SIGNAL(triggered()), this, SLOT(deleteStream())); connect(actionDelete, SIGNAL(triggered()), this, SLOT(deleteStream()));
connect(actionRename, SIGNAL(triggered()), this, SLOT(renameStream())); connect(actionRename, SIGNAL(triggered()), this, SLOT(renameStream()));
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&, const unsigned short&)), this, SLOT(updateStreamName(const unsigned short&, const unsigned short&)));
refreshTimeTimer = new QTimer(this); refreshTimeTimer = new QTimer(this);
connect(refreshTimeTimer, SIGNAL(timeout()), this, SLOT(updateLastRefreshedTimeForStreams())); connect(refreshTimeTimer, SIGNAL(timeout()), this, SLOT(updateLastRefreshedTimeForStreams()));
refreshTimeTimer->start(60000); // 1min refreshTimeTimer->start(60000); // 1min
@ -302,6 +306,8 @@
} }
RSSImp::~RSSImp(){ RSSImp::~RSSImp(){
delete refreshTimeTimer;
delete rssmanager;
} }
short RSSImp::getNumStreamSelected(){ short RSSImp::getNumStreamSelected(){

4
src/rss_imp.h

@ -24,19 +24,19 @@
#define REFRESH_MAX_LATENCY 600000 #define REFRESH_MAX_LATENCY 600000
#include "ui_rss.h" #include "ui_rss.h"
#include "rss.h"
#define DESCRIPTION_CHILD 0 #define DESCRIPTION_CHILD 0
#define URL_CHILD 1 #define URL_CHILD 1
#define TIME_CHILD 2 #define TIME_CHILD 2
class QTimer; class QTimer;
class RssManager;
class RSSImp : public QWidget, public Ui::RSS{ class RSSImp : public QWidget, public Ui::RSS{
Q_OBJECT Q_OBJECT
private: private:
RssManager rssmanager; RssManager *rssmanager;
QTimer *refreshTimeTimer; QTimer *refreshTimeTimer;
protected slots: protected slots:

Loading…
Cancel
Save