Changeset 2982

Show
Ignore:
Timestamp:
04/28/06 08:51:49 (3 years ago)
Author:
madcat
Message:

Feeds caching; no longer reloads feeds on startup.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hydranode/hngui/hometabs.cpp

    r2980 r2982  
    5252        setActive("Overview"); 
    5353 
    54         m_ui->devFeed->addFeed( 
    55                 "Hydranode Development News",  
    56                 QUrl("http://hydranode.com/blog/atom.xml"), 5 
    57         ); 
    58         m_ui->devFeed->setActive("Hydranode Development News"); 
    59         m_ui->devFeed->getUpdates(); 
    60  
    61         m_ui->newsFeed->addFeed( 
    62                 "Slashdot - Stuff that matters",  
    63                 QUrl("http://rss.slashdot.org/Slashdot/slashdot"), 10 
    64         ); 
    65         m_ui->newsFeed->addFeed( 
    66                 "BBC World News",  
    67                 QUrl( 
    68                         "http://newsrss.bbc.co.uk/rss/" 
    69                         "newsonline_world_edition/front_page/rss.xml" 
    70                 ), 10 
    71         ); 
    72         m_ui->newsFeed->addFeed( 
    73                 "CNN World News",  
    74                 QUrl("http://rss.cnn.com/rss/cnn_topstories.rss"), 10 
    75         ); 
    76         m_ui->newsFeed->addFeed( 
    77                 "Tom's Hardware News",  
    78                 QUrl("http://www.pheedo.com/f/toms_hardware"), 10 
    79         ); 
    80         m_ui->newsFeed->addFeed( 
    81                 "Slyck - File Sharing News",  
    82                 QUrl("http://slyck.com/slyckrss.xml"), 30 
    83         ); 
    84         m_ui->newsFeed->getUpdates(); 
    85  
    86         QSettings conf(confDir() + "gui.ini", QSettings::IniFormat); 
    87         if (conf.contains("ActiveFeed")) { 
    88                 m_ui->newsFeed->setActive(conf.value("ActiveFeed").toString()); 
    89         } 
    90  
    9154        // make text controls backgrounds 100% transparent to allow background 
    9255        // to be displayed below it 
     
    12790                SLOT(changePage()) 
    12891        ); 
     92 
     93        m_ui->devFeed->addFeed( 
     94                "Hydranode Development News",  
     95                QUrl("http://hydranode.com/blog/atom.xml"), 5 
     96        ); 
     97        m_ui->devFeed->setActive("Hydranode Development News"); 
     98 
     99        m_ui->newsFeed->addFeed( 
     100                "Slashdot - Stuff that matters",  
     101                QUrl("http://rss.slashdot.org/Slashdot/slashdot"), 10 
     102        ); 
     103        m_ui->newsFeed->addFeed( 
     104                "BBC World News",  
     105                QUrl( 
     106                        "http://newsrss.bbc.co.uk/rss/" 
     107                        "newsonline_world_edition/front_page/rss.xml" 
     108                ), 10 
     109        ); 
     110        m_ui->newsFeed->addFeed( 
     111                "CNN World News",  
     112                QUrl("http://rss.cnn.com/rss/cnn_topstories.rss"), 10 
     113        ); 
     114        m_ui->newsFeed->addFeed( 
     115                "Tom's Hardware News",  
     116                QUrl("http://www.pheedo.com/f/toms_hardware"), 10 
     117        ); 
     118        m_ui->newsFeed->addFeed( 
     119                "Slyck - File Sharing News",  
     120                QUrl("http://slyck.com/slyckrss.xml"), 30 
     121        ); 
     122        m_ui->devFeed->init(); 
     123        m_ui->newsFeed->init(); 
     124 
     125        QSettings conf(confDir() + "gui.ini", QSettings::IniFormat); 
     126        if (conf.contains("ActiveFeed")) { 
     127                m_ui->newsFeed->setActive(conf.value("ActiveFeed").toString()); 
     128        } 
     129 
    129130        m_actionBack = QPixmap(imgDir() + "actionback.png"); 
    130131        m_actionBackOrig = m_actionBack; 
  • hydranode/hngui/newsfeed.cpp

    r2914 r2982  
    1818  
    1919#include "newsfeed.h" 
     20#include "main.h" 
    2021#include <QHttp> 
    2122#include <QXmlDefaultHandler> 
     
    2526#include <QSettings> 
    2627#include <QTimer> 
     28#include <QDir> 
    2729 
    2830// RssReader class 
     
    187189 
    188190NewsFeed::NewsFeed(QWidget *parent) : QTextBrowser(parent) { 
     191        m_timer = new QTimer(this); 
     192 
    189193        connect( 
    190194                this, SIGNAL(anchorClicked(const QUrl&)),  
    191195                SLOT(linkClicked(const QUrl&)) 
    192196        ); 
    193         QTimer *timer = new QTimer(this); 
    194         timer->start(1000 * 60 * 60); 
    195         connect(timer, SIGNAL(timeout()), SLOT(getUpdates())); 
     197        connect(m_timer, SIGNAL(timeout()), SLOT(getUpdates())); 
     198
     199 
     200void NewsFeed::init() { 
     201        QSettings conf(confDir() + "gui.ini", QSettings::IniFormat); 
     202        QDateTime lastUpdate = QDateTime::fromString( 
     203                conf.value(objectName() + "Updated", "").toString(),  
     204                Qt::ISODate 
     205        ); 
     206        logDebug("Last update was on " + lastUpdate.toString()); 
     207        QDateTime current = QDateTime::currentDateTime(); 
     208        int timeDiff = lastUpdate.secsTo(current); 
     209        logDebug(QString("Time diff since last update is %1").arg(timeDiff)); 
     210        if (timeDiff >= 60 * 60) { 
     211                timeDiff = 1; 
     212        } 
     213        m_timer->setSingleShot(true); 
     214        m_timer->start(1000 * timeDiff); 
     215        logDebug(QString("Next feeds update in %1 seconds").arg(timeDiff)); 
    196216} 
    197217 
     
    201221        m_feeds[title] = QString(); 
    202222        if (m_currentActive.isEmpty()) { 
    203                 m_currentActive = title; 
     223                setActive(title); 
     224        } 
     225 
     226        QFile f(confDir() + "/feeds/" + title + ".xml"); 
     227        f.open(QIODevice::ReadOnly); 
     228        if (f.isOpen()) { 
     229                QString feed(f.readAll()); 
     230                m_feeds[title] = parseFeed(feed, limit); 
    204231        } 
    205232} 
     
    209236        while (it != m_urls.end()) { 
    210237                QString title = it.key(); 
     238                logDebug("Getting updates for feed " + title); 
    211239                QUrl url = it.value(); 
    212240                if (m_links[title]) { 
     
    225253                ++it; 
    226254        } 
     255        QSettings conf(confDir() + "gui.ini", QSettings::IniFormat); 
     256        conf.setValue( 
     257                objectName() + "Updated",  
     258                QDateTime::currentDateTime().toString(Qt::ISODate) 
     259        ); 
     260 
     261        if (!m_timer->isActive()) { 
     262                logDebug("Next feeds update in one hour."); 
     263                m_timer->start(1000 * 60 * 60); 
     264        } 
    227265} 
    228266 
     
    254292        feed.replace("“", "\""); 
    255293        feed.replace("”", "\""); 
    256         QXmlSimpleReader reader; 
    257         RssReader handler(m_limits[m_reqs[code]]); 
    258         reader.setContentHandler(&handler); 
    259         QXmlInputSource source; 
    260         source.setData(feed); 
    261         reader.parse(&source); 
    262         m_feeds[m_reqs[code]] = handler.getOutput(); 
     294 
     295        m_feeds[m_reqs[code]] = parseFeed(feed, m_limits[m_reqs[code]]); 
    263296        if (!m_feeds[m_reqs[code]].size()) { 
    264297                m_feeds[m_reqs[code]] = feed; 
     
    269302        } 
    270303 
     304        // save cache 
     305        QDir d(confDir()); 
     306        if (!d.exists("feeds")) { 
     307                d.mkdir("feeds"); 
     308        } 
     309        QFile f(d.absolutePath() + "/feeds/" + m_reqs[code] + ".xml"); 
     310        f.open(QIODevice::WriteOnly); 
     311        if (f.isOpen()) { 
     312                f.write(feed.toAscii()); 
     313        } 
     314 
     315        // cleanup 
    271316        m_links[m_reqs[code]]->deleteLater(); 
    272317        m_links.remove(m_reqs[code]); 
     
    296341#endif 
    297342} 
     343 
     344QString NewsFeed::parseFeed(const QString &feed, int limit) { 
     345        QXmlSimpleReader reader; 
     346        RssReader handler(limit); 
     347        reader.setContentHandler(&handler); 
     348        QXmlInputSource source; 
     349        source.setData(feed); 
     350        reader.parse(&source); 
     351        return handler.getOutput(); 
     352} 
  • hydranode/hngui/newsfeed.h

    r2864 r2982  
    3535public Q_SLOTS: 
    3636        void getUpdates(); 
     37        void init(); 
    3738        void setActive(QString title); 
    3839private Q_SLOTS: 
     
    4142        void linkClicked(const QUrl &link); 
    4243private: 
     44        QString parseFeed(const QString &feed, int limit); 
     45 
    4346        QMap<QString, QUrl>    m_urls; 
    4447        QMap<QString, int>     m_limits; 
     
    4750        QMap<QString, QString> m_feeds; 
    4851        QString                m_currentActive; 
     52        QTimer                *m_timer; 
    4953}; 
    5054 
  • hydranode/hngui/transfertabs.cpp

    r2968 r2982  
    615615void TransferPage::getMoreComments() { 
    616616        if (!m_currentActive || !m_currentActive->getData()) { 
    617                 logDebug("Next GetNames in 5 seconds"); 
    618617                m_commentTimer->stop(); 
    619618                m_commentTimer->start(5000);