[Marble-commits] KDE/kdeedu/marble/src

Bastian Holst bastianholst at gmx.de
Wed Aug 19 14:37:49 CEST 2009


SVN commit 1013275 by bholst:

Work on Marble's weather plugin:
* Build an abstract class to do multiple jobs asynchronously (AbstractWorkerThread).
* BBCParser now using AbstractWorkerThread.
* Finding of the right items now using AbstractWorkerThread.


 AM            lib/AbstractWorkerThread.cpp   [License: LGPL]
 AM            lib/AbstractWorkerThread.h   [License: LGPL]
 M  +2 -0      lib/CMakeLists.txt  
 AM            plugins/render/weather/BBCItemGetter.cpp   [License: LGPL]
 AM            plugins/render/weather/BBCItemGetter.h   [License: LGPL]
 M  +22 -55    plugins/render/weather/BBCParser.cpp  
 M  +5 -7      plugins/render/weather/BBCParser.h  
 M  +8 -29     plugins/render/weather/BBCWeatherService.cpp  
 M  +5 -6      plugins/render/weather/BBCWeatherService.h  
 M  +1 -0      plugins/render/weather/CMakeLists.txt  


** trunk/KDE/kdeedu/marble/src/lib/AbstractWorkerThread.cpp #property svn:eol-style
   + native
** trunk/KDE/kdeedu/marble/src/lib/AbstractWorkerThread.h #property svn:eol-style
   + native
--- trunk/KDE/kdeedu/marble/src/lib/CMakeLists.txt #1013274:1013275
@@ -156,6 +156,7 @@
     AbstractDataPlugin.cpp
     AbstractDataPluginModel.cpp
     AbstractDataPluginItem.cpp
+    AbstractWorkerThread.cpp
     
     PluginInterface.cpp
     NetworkPluginInterface.cpp
@@ -316,6 +317,7 @@
     AbstractDataPlugin.h
     AbstractDataPluginModel.h
     AbstractDataPluginItem.h
+    AbstractWorkerThread.h
 
     LatLonEdit.h
     DESTINATION ${CMAKE_INSTALL_PREFIX}/include/marble
** trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCItemGetter.cpp #property svn:eol-style
   + native
** trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCItemGetter.h #property svn:eol-style
   + native
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCParser.cpp #1013274:1013275
@@ -39,24 +39,14 @@
 QHash<QString, int> BBCParser::monthNames
         = QHash<QString, int>();
 
-const int WAIT_ATTEMPTS = 20;
-const int WAIT_TIME = 100;
-
-BBCParser::BBCParser()
-    : QThread(),
-      m_running( false ),
-      m_end( false )
+BBCParser::BBCParser( QObject *parent )
+        : AbstractWorkerThread( parent )
 {
     BBCParser::setupHashes();
 }
 
 BBCParser::~BBCParser()
 {
-    m_schedule.clear();
-    if ( isRunning() ) {
-        m_end = true;
-        wait( 1000 );
-    }
 }
 
 BBCParser *BBCParser::instance()
@@ -76,60 +66,37 @@
 
     m_schedule.push( entry );
 
-    QMutexLocker locker( &m_runningMutex );
-    if ( !m_running ) {
-        if ( wait( 2 * WAIT_TIME ) ) {
-            m_running = true;
-            start( QThread::IdlePriority );
-        }
-    }
+    ensureRunning();
 }
 
-void BBCParser::run()
+bool BBCParser::workAvailable()
 {
-    int waitAttempts = WAIT_ATTEMPTS;
-    while( 1 ) {
-        m_runningMutex.lock();
-        if ( m_schedule.isEmpty() ) {
-            waitAttempts--;
-            if ( !waitAttempts || m_end ) {
-                m_running = false;
-                m_runningMutex.unlock();
-                break;
-            }
-            else {
-                m_runningMutex.unlock();
-                msleep( WAIT_TIME );
-            }
-        }
-        else {
-            m_runningMutex.unlock();
-            ScheduleEntry entry = m_schedule.pop();
+    return !m_schedule.isEmpty();
+}
 
-            QFile file( entry.path );
-            if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
-                return;
-            }
+void BBCParser::work()
+{
+    ScheduleEntry entry = m_schedule.pop();
 
-            QList<WeatherData> data = read( &file );
+    QFile file( entry.path );
+    if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
+        return;
+    }
 
-            if( !data.isEmpty() && !entry.item.isNull() ) {
-                if ( entry.type == "bbcobservation" ) {
-                    entry.item->setCurrentWeather( data.at( 0 ) );
-                }
-                else if ( entry.type == "bbcforecast" ) {
-                    entry.item->addForecastWeather( data );
-                }
+    QList<WeatherData> data = read( &file );
 
-                emit parsedFile();
-            }
-
-            waitAttempts = WAIT_ATTEMPTS;
+    if( !data.isEmpty() && !entry.item.isNull() ) {
+        if ( entry.type == "bbcobservation" ) {
+            entry.item->setCurrentWeather( data.at( 0 ) );
         }
+        else if ( entry.type == "bbcforecast" ) {
+            entry.item->addForecastWeather( data );
+        }
+
+        emit parsedFile();
     }
 }
 
-
 QList<WeatherData> BBCParser::read( QIODevice *device )
 {
     m_list.clear();
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCParser.h #1013274:1013275
@@ -12,6 +12,7 @@
 #define BBCPARSER_H
 
 // Marble
+#include "AbstractWorkerThread.h"
 #include "WeatherData.h"
 
 // Qt
@@ -20,7 +21,6 @@
 #include <QtCore/QMutex>
 #include <QtCore/QPointer>
 #include <QtCore/QStack>
-#include <QtCore/QThread>
 #include <QtCore/QXmlStreamReader>
 
 class QByteArray;
@@ -38,18 +38,19 @@
     QString type;
 };
 
-class BBCParser : public QThread, public QXmlStreamReader
+class BBCParser : public AbstractWorkerThread, public QXmlStreamReader
 {
     Q_OBJECT
 public:
-    BBCParser();
+    BBCParser( QObject *parent = 0 );
     ~BBCParser();
 
     static BBCParser *instance();
     void scheduleRead( const QString& path, BBCWeatherItem *item, const QString& type );
 
 protected:
-    void run();
+    bool workAvailable();
+    void work();
 
 Q_SIGNALS:
     void parsedFile();
@@ -69,9 +70,6 @@
 
     QList<WeatherData> m_list;
     QStack<ScheduleEntry> m_schedule;
-    bool m_running;
-    QMutex m_runningMutex;
-    bool m_end;
     
     static QHash<QString, WeatherData::WeatherCondition> dayConditions;
     static QHash<QString, WeatherData::WeatherCondition> nightConditions;
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCWeatherService.cpp #1013274:1013275
@@ -12,6 +12,7 @@
 #include "BBCWeatherService.h"
 
 // Marble
+#include "BBCItemGetter.h"
 #include "BBCWeatherItem.h"
 #include "GeoDataCoordinates.h"
 #include "GeoDataLatLonAltBox.h"
@@ -34,9 +35,7 @@
     : AbstractWeatherService( parent ),
       m_parsingStarted( false ),
       m_parser( 0 ),
-      m_scheduledBox(),
-      m_scheduledNumber( 0 ),
-      m_scheduledFacade( 0 )
+      m_itemGetter( new BBCItemGetter( this ) )
 {
 }
 
@@ -54,38 +53,18 @@
         setupList();
     }
 
-    if ( m_items.isEmpty() ) {
-        m_scheduledBox = box;
-        m_scheduledNumber = number;
-        m_scheduledFacade = facade;
-        return;
-    }
-
-    qint32 fetched = 0;
-    QList<BBCWeatherItem *>::iterator it = m_items.begin();
-    
-    while ( fetched < number && it != m_items.end() ) {
-        if ( (*it) && box.contains( (*it)->coordinate() ) ) {
-            (*it)->setTarget( "earth" );
-            emit requestedDownload( (*it)->observationUrl(), "bbcobservation", (*it) );
-            emit requestedDownload( (*it)->forecastUrl(),    "bbcforecast",    (*it) );
-            fetched++;
-        }
-        ++it;
-    }
+    m_itemGetter->setSchedule( box, facade, number );
 }
 
 void BBCWeatherService::fetchStationList()
 {
-    m_items = m_parser->stationList();
+    connect( m_itemGetter,
+             SIGNAL( requestedDownload( QUrl, QString, AbstractDataPluginItem* ) ),
+             this,
+             SIGNAL( requestedDownload( QUrl, QString, AbstractDataPluginItem* ) ) );
+    m_itemGetter->setStationList( m_parser->stationList() );
     delete m_parser;
     m_parser = 0;
-
-    if ( m_scheduledNumber
-         && !m_scheduledBox.isNull()
-         && m_scheduledFacade ) {
-        getAdditionalItems( m_scheduledBox, m_scheduledFacade, m_scheduledNumber );
-    }
 }
 
 void BBCWeatherService::setupList()
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCWeatherService.h #1013274:1013275
@@ -13,12 +13,14 @@
 
 #include "AbstractWeatherService.h"
 
-#include "GeoDataLatLonAltBox.h"
+class QMutex;
 
 namespace Marble
 {
-    
+
+class BBCItemGetter;
 class BBCWeatherItem;
+class GeoDataLatLonAltBox;
 class StationListParser;
 
 class BBCWeatherService : public AbstractWeatherService
@@ -40,12 +42,9 @@
  private:
     void setupList();
 
-    QList<BBCWeatherItem*> m_items;
     bool m_parsingStarted;
     StationListParser *m_parser;
-    GeoDataLatLonAltBox m_scheduledBox;
-    qint32 m_scheduledNumber;
-    MarbleDataFacade *m_scheduledFacade;
+    BBCItemGetter *m_itemGetter;
 };
 
 } // namespace Marble
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/CMakeLists.txt #1013274:1013275
@@ -12,6 +12,7 @@
                   WeatherModel.cpp 
                   WeatherPlugin.cpp
                   AbstractWeatherService.cpp
+                  BBCItemGetter.cpp
                   BBCParser.cpp
                   BBCWeatherService.cpp
                   BBCWeatherItem.cpp


More information about the Marble-commits mailing list