1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
4 //
5 
6 // Self
7 #include "BBCItemGetter.h"
8 #include "BBCStation.h"
9 #include "BBCWeatherItem.h"
10 #include "MarbleDebug.h"
11 
12 // Qt
13 #include <QMutexLocker>
14 
15 using namespace Marble;
16 
BBCItemGetter(QObject * parent)17 BBCItemGetter::BBCItemGetter( QObject *parent )
18         : AbstractWorkerThread( parent ),
19           m_scheduleMutex(),
20           m_scheduledNumber( 0 )
21 {
22 }
23 
~BBCItemGetter()24 BBCItemGetter::~BBCItemGetter()
25 {
26 }
27 
setSchedule(const GeoDataLatLonBox & box,qint32 number)28 void BBCItemGetter::setSchedule( const GeoDataLatLonBox& box,
29                                  qint32 number )
30 {
31     m_scheduleMutex.lock();
32     m_scheduledBox = box;
33     m_scheduledNumber = number;
34     m_scheduleMutex.unlock();
35     ensureRunning();
36 }
37 
setStationList(const QList<BBCStation> & items)38 void BBCItemGetter::setStationList( const QList<BBCStation>& items )
39 {
40     m_items = items;
41     ensureRunning();
42 }
43 
station(const QString & id)44 BBCStation BBCItemGetter::station( const QString &id )
45 {
46     QString const bbcIdTemplate = QString( "bbc%1" );
47     for( const BBCStation &station: m_items ) {
48         if ( bbcIdTemplate.arg( station.bbcId() ) == id ) {
49             return station;
50         }
51     }
52 
53     return BBCStation();
54 }
55 
workAvailable()56 bool BBCItemGetter::workAvailable()
57 {
58     return !m_scheduledBox.isNull()
59            && m_scheduledNumber;
60 
61 }
62 
work()63 void BBCItemGetter::work()
64 {
65     if ( m_items.isEmpty() ) {
66         sleep( 1 );
67         return;
68     }
69 
70     m_scheduleMutex.lock();
71     GeoDataLatLonBox box = m_scheduledBox;
72     qint32 number = m_scheduledNumber;
73     m_scheduledBox = GeoDataLatLonBox();
74     m_scheduledNumber = 0;
75     m_scheduleMutex.unlock();
76 
77     qint32 fetched = 0;
78     QList<BBCStation>::ConstIterator it = m_items.constBegin();
79     QList<BBCStation>::ConstIterator end = m_items.constEnd();
80 
81     while ( fetched < number && it != end ) {
82         if ( box.contains( it->coordinate() ) ) {
83             emit foundStation( (*it) );
84             fetched++;
85         }
86         ++it;
87     }
88 }
89 
90 #include "moc_BBCItemGetter.cpp"
91