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

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 "WeatherModel.h"
0008 
0009 // Qt
0010 #include <QTimer>
0011 #include <QUrl>
0012 
0013 // Marble
0014 #include "BBCWeatherService.h"
0015 #include "FakeWeatherService.h"
0016 #include "GeoNamesWeatherService.h"
0017 #include "AbstractDataPluginItem.h"
0018 #include "WeatherItem.h"
0019 #include "MarbleDebug.h"
0020 #include "MarbleModel.h"
0021 
0022 using namespace Marble;
0023 
0024 WeatherModel::WeatherModel( const MarbleModel *marbleModel, QObject *parent )
0025     : AbstractDataPluginModel( "weather", marbleModel, parent )
0026 {
0027     registerItemProperties( WeatherItem::staticMetaObject );
0028 
0029     // addService( new FakeWeatherService( marbleModel(), this ) );
0030     addService( new BBCWeatherService( marbleModel, this ) );
0031     addService( new GeoNamesWeatherService( marbleModel, this ) );
0032 
0033     m_timer = new QTimer();
0034     connect( m_timer, SIGNAL(timeout()), SLOT(clear()) );
0035 
0036     // Default interval = 3 hours
0037     setUpdateInterval( 3 );
0038 
0039     m_timer->start();
0040 }
0041     
0042 WeatherModel::~WeatherModel()
0043 {
0044 }
0045 
0046 void WeatherModel::setFavoriteItems( const QStringList& list )
0047 {
0048     if ( favoriteItems() != list ) {
0049         for ( AbstractWeatherService *service: m_services ) {
0050             service->setFavoriteItems( list );
0051         }
0052 
0053         AbstractDataPluginModel::setFavoriteItems( list );
0054     }
0055 }
0056 
0057 void WeatherModel::setUpdateInterval( quint32 hours )
0058 {
0059     quint32 msecs = hours * 60 * 60 * 1000;
0060     m_timer->setInterval( msecs );
0061 }
0062 
0063 void WeatherModel::downloadItemData( const QUrl& url,
0064                                      const QString& type,
0065                                      AbstractDataPluginItem *item )
0066 {
0067     AbstractDataPluginItem *existingItem = findItem( item->id() );
0068     if ( !existingItem ) {
0069         WeatherItem *weatherItem = qobject_cast<WeatherItem*>( item );
0070         if( weatherItem ) {
0071             weatherItem->request( type );
0072         }
0073 
0074         downloadItem( url, type, item );
0075         addItemToList( item );
0076     } else {
0077         if ( existingItem != item )
0078             item->deleteLater();
0079         
0080         WeatherItem *existingWeatherItem = qobject_cast<WeatherItem*>( existingItem );
0081         if( existingWeatherItem && existingWeatherItem->request( type ) ) {
0082             downloadItem( url, type, existingItem );
0083             addItemToList( existingItem );
0084         }
0085     }
0086 }
0087 
0088 void WeatherModel::getAdditionalItems( const GeoDataLatLonAltBox& box,
0089                                qint32 number )
0090 {
0091     for ( AbstractWeatherService *service: m_services ) {
0092         service->getAdditionalItems( box, number );
0093     }
0094 }
0095 
0096 void WeatherModel::getItem( const QString &id )
0097 {
0098     for( AbstractWeatherService* service: m_services ) {
0099         service->getItem( id );
0100     }
0101 }
0102 
0103 void WeatherModel::parseFile( const QByteArray& file )
0104 {
0105     for ( AbstractWeatherService *service: m_services ) {
0106         service->parseFile( file );
0107     }
0108 }
0109 
0110 void WeatherModel::downloadDescriptionFileRequested( const QUrl& url )
0111 {
0112     downloadDescriptionFile( url );
0113 }
0114 
0115 void WeatherModel::setMarbleWidget(MarbleWidget *widget)
0116 {
0117     for ( AbstractWeatherService* service: m_services ) {
0118         service->setMarbleWidget( widget );
0119     }
0120 }
0121 
0122 void WeatherModel::addService( AbstractWeatherService *service )
0123 {
0124     service->setFavoriteItems( favoriteItems() );
0125 
0126     connect( service, SIGNAL(createdItems(QList<AbstractDataPluginItem*>)),
0127              this, SLOT(addItemsToList(QList<AbstractDataPluginItem*>)) );
0128     connect( service, SIGNAL(requestedDownload(QUrl,QString,AbstractDataPluginItem*)),
0129              this, SLOT(downloadItemData(QUrl,QString,AbstractDataPluginItem*)) );
0130     connect( service, SIGNAL(downloadDescriptionFileRequested(QUrl)),
0131              this, SLOT(downloadDescriptionFileRequested(QUrl)) );
0132 
0133     m_services.append( service );
0134 }
0135 
0136 #include "moc_WeatherModel.cpp"