File indexing completed on 2024-05-12 15:31:19

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 // Self
0007 #include "BBCItemGetter.h"
0008 #include "BBCStation.h"
0009 #include "BBCWeatherItem.h"
0010 #include "MarbleDebug.h"
0011 
0012 // Qt
0013 #include <QMutexLocker>
0014 
0015 using namespace Marble;
0016 
0017 BBCItemGetter::BBCItemGetter( QObject *parent )
0018         : AbstractWorkerThread( parent ),
0019           m_scheduleMutex(),
0020           m_scheduledNumber( 0 )
0021 {
0022 }
0023 
0024 BBCItemGetter::~BBCItemGetter()
0025 {
0026 }
0027 
0028 void BBCItemGetter::setSchedule( const GeoDataLatLonBox& box,
0029                                  qint32 number )
0030 {
0031     m_scheduleMutex.lock();
0032     m_scheduledBox = box;
0033     m_scheduledNumber = number;
0034     m_scheduleMutex.unlock();
0035     ensureRunning();
0036 }
0037 
0038 void BBCItemGetter::setStationList( const QList<BBCStation>& items )
0039 {
0040     m_items = items;
0041     ensureRunning();
0042 }
0043 
0044 BBCStation BBCItemGetter::station( const QString &id )
0045 {
0046     QString const bbcIdTemplate = QString( "bbc%1" );
0047     for( const BBCStation &station: m_items ) {
0048         if ( bbcIdTemplate.arg( station.bbcId() ) == id ) {
0049             return station;
0050         }
0051     }
0052 
0053     return BBCStation();
0054 }
0055 
0056 bool BBCItemGetter::workAvailable()
0057 {
0058     return !m_scheduledBox.isNull()
0059            && m_scheduledNumber;
0060 
0061 }
0062 
0063 void BBCItemGetter::work()
0064 {
0065     if ( m_items.isEmpty() ) {
0066         sleep( 1 );
0067         return;
0068     }
0069 
0070     m_scheduleMutex.lock();
0071     GeoDataLatLonBox box = m_scheduledBox;
0072     qint32 number = m_scheduledNumber;
0073     m_scheduledBox = GeoDataLatLonBox();
0074     m_scheduledNumber = 0;
0075     m_scheduleMutex.unlock();
0076 
0077     qint32 fetched = 0;
0078     QList<BBCStation>::ConstIterator it = m_items.constBegin();
0079     QList<BBCStation>::ConstIterator end = m_items.constEnd();
0080 
0081     while ( fetched < number && it != end ) {
0082         if ( box.contains( it->coordinate() ) ) {
0083             emit foundStation( (*it) );
0084             fetched++;
0085         }
0086         ++it;
0087     }
0088 }
0089 
0090 #include "moc_BBCItemGetter.cpp"