<commit_before>//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2009      <NAME> <<EMAIL>>
//

// Self
#include "WeatherModel.h"

// Qt
#include <QtCore/QTimer>
#include <QtCore/QUrl>

// Marble
#include "BBCWeatherService.h"
#include "FakeWeatherService.h"
#include "GeoNamesWeatherService.h"
#include "AbstractDataPluginItem.h"
#include "WeatherItem.h"
#include "MarbleDebug.h"
#include "MarbleModel.h"

using namespace Marble;

WeatherModel::WeatherModel( const MarbleModel *marbleModel, QObject *parent )
    : AbstractDataPluginModel( "weather", marbleModel, parent ),
      m_initialized( false )
{
    registerItemProperties( WeatherItem::staticMetaObject );
    createServices();

    m_timer = new QTimer();
    connect( m_timer, SIGNAL(timeout()), SLOT(updateItems()) );

    // Default interval = 3 hours
    setUpdateInterval( 3 );

    m_timer->start();
}
    
WeatherModel::~WeatherModel()
{
}

void WeatherModel::setFavoriteItems( const QStringList& list )
{
    if ( favoriteItems() != list ) {
        foreach ( AbstractWeatherService *service, m_services ) {
            service->setFavoriteItems( list );
        }

        AbstractDataPluginModel::setFavoriteItems( list );

        if ( m_initialized && isFavoriteItemsOnly() ) {
            updateItems();
        }
    }
}

void WeatherModel::setUpdateInterval( quint32 hours )
{
    quint32 msecs = hours * 60 * 60 * 1000;
    m_timer->setInterval( msecs );
}

void WeatherModel::downloadItemData( const QUrl& url,
                                     const QString& type,
                                     AbstractDataPluginItem *item )
{
    AbstractDataPluginItem *existingItem = findItem( item->id() );
    if ( !existingItem ) {
        WeatherItem *weatherItem = qobject_cast<WeatherItem*>( item );
        if( weatherItem ) {
            weatherItem->request( type );
        }

        AbstractDataPluginModel::downloadItemData( url, type, item );
    } else {
        if ( existingItem != item )
            item->deleteLater();
        
        WeatherItem *existingWeatherItem = qobject_cast<WeatherItem*>( existingItem );
        if( existingWeatherItem && existingWeatherItem->request( type ) ) {
            AbstractDataPluginModel::downloadItemData( url, type, existingWeatherItem );
        }
    }
}

void WeatherModel::getAdditionalItems( const GeoDataLatLonAltBox& box,
                               qint32 number )
{
    m_lastBox = box;
    m_lastNumber = number;

    m_initialized = true;

    emit additionalItemsRequested( box, marbleModel(), number );
}

void WeatherModel::getItem( const QString &id )
{
    foreach( AbstractWeatherService* service, m_services ) {
        service->getItem( id, marbleModel() );
    }
}

void WeatherModel::parseFile( const QByteArray& file )
{
    emit parseFileRequested( file );
}

void WeatherModel::updateItems()
{
    clear();
    emit additionalItemsRequested( m_lastBox, marbleModel(), m_lastNumber );
    emit itemsUpdated();
}

void WeatherModel::createServices()
{
    // addService( new FakeWeatherService( this ) );
    addService( new BBCWeatherService( this ) );
    addService( new GeoNamesWeatherService( this ) );
}

void WeatherModel::downloadDescriptionFileRequested( const QUrl& url )
{
    downloadDescriptionFile( url );
}

void WeatherModel::setMarbleWidget(MarbleWidget *widget)
{
    foreach ( AbstractWeatherService* service, m_services ) {
        service->setMarbleWidget( widget );
    }
}

void WeatherModel::addService( AbstractWeatherService *service )
{
    service->setFavoriteItems( favoriteItems() );

    connect( service, SIGNAL(createdItems(QList<AbstractDataPluginItem*>)),
             this, SLOT(addItemsToList(QList<AbstractDataPluginItem*>)) );
    connect( service, SIGNAL(requestedDownload(QUrl,
                                               QString, 
                                               AbstractDataPluginItem*)),
             this, SLOT(downloadItemData(QUrl,
                                         QString,
                                         AbstractDataPluginItem*)) );
    connect( service, SIGNAL(downloadDescriptionFileRequested(QUrl)),
             this, SLOT(downloadDescriptionFileRequested(QUrl)) );

    connect( this, SIGNAL(additionalItemsRequested(const GeoDataLatLonAltBox,
                                                   const MarbleModel*,
                                                   qint32)),
             service, SLOT(getAdditionalItems(const GeoDataLatLonAltBox,
                                              const MarbleModel*,
                                              qint32)) );
    connect( this, SIGNAL(parseFileRequested(QByteArray)),
             service, SLOT(parseFile(QByteArray)) );

    m_services.append( service );
}

#include "WeatherModel.moc"
<commit_msg>port away from deprecated method<commit_after>//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2009      <NAME> <<EMAIL>>
//

// Self
#include "WeatherModel.h"

// Qt
#include <QtCore/QTimer>
#include <QtCore/QUrl>

// Marble
#include "BBCWeatherService.h"
#include "FakeWeatherService.h"
#include "GeoNamesWeatherService.h"
#include "AbstractDataPluginItem.h"
#include "WeatherItem.h"
#include "MarbleDebug.h"
#include "MarbleModel.h"

using namespace Marble;

WeatherModel::WeatherModel( const MarbleModel *marbleModel, QObject *parent )
    : AbstractDataPluginModel( "weather", marbleModel, parent ),
      m_initialized( false )
{
    registerItemProperties( WeatherItem::staticMetaObject );
    createServices();

    m_timer = new QTimer();
    connect( m_timer, SIGNAL(timeout()), SLOT(updateItems()) );

    // Default interval = 3 hours
    setUpdateInterval( 3 );

    m_timer->start();
}
    
WeatherModel::~WeatherModel()
{
}

void WeatherModel::setFavoriteItems( const QStringList& list )
{
    if ( favoriteItems() != list ) {
        foreach ( AbstractWeatherService *service, m_services ) {
            service->setFavoriteItems( list );
        }

        AbstractDataPluginModel::setFavoriteItems( list );

        if ( m_initialized && isFavoriteItemsOnly() ) {
            updateItems();
        }
    }
}

void WeatherModel::setUpdateInterval( quint32 hours )
{
    quint32 msecs = hours * 60 * 60 * 1000;
    m_timer->setInterval( msecs );
}

void WeatherModel::downloadItemData( const QUrl& url,
                                     const QString& type,
                                     AbstractDataPluginItem *item )
{
    AbstractDataPluginItem *existingItem = findItem( item->id() );
    if ( !existingItem ) {
        WeatherItem *weatherItem = qobject_cast<WeatherItem*>( item );
        if( weatherItem ) {
            weatherItem->request( type );
        }

        downloadItem( url, type, item );
        addItemToList( item );
    } else {
        if ( existingItem != item )
            item->deleteLater();
        
        WeatherItem *existingWeatherItem = qobject_cast<WeatherItem*>( existingItem );
        if( existingWeatherItem && existingWeatherItem->request( type ) ) {
            downloadItem( url, type, existingItem );
            addItemToList( existingItem );
        }
    }
}

void WeatherModel::getAdditionalItems( const GeoDataLatLonAltBox& box,
                               qint32 number )
{
    m_lastBox = box;
    m_lastNumber = number;

    m_initialized = true;

    emit additionalItemsRequested( box, marbleModel(), number );
}

void WeatherModel::getItem( const QString &id )
{
    foreach( AbstractWeatherService* service, m_services ) {
        service->getItem( id, marbleModel() );
    }
}

void WeatherModel::parseFile( const QByteArray& file )
{
    emit parseFileRequested( file );
}

void WeatherModel::updateItems()
{
    clear();
    emit additionalItemsRequested( m_lastBox, marbleModel(), m_lastNumber );
    emit itemsUpdated();
}

void WeatherModel::createServices()
{
    // addService( new FakeWeatherService( this ) );
    addService( new BBCWeatherService( this ) );
    addService( new GeoNamesWeatherService( this ) );
}

void WeatherModel::downloadDescriptionFileRequested( const QUrl& url )
{
    downloadDescriptionFile( url );
}

void WeatherModel::setMarbleWidget(MarbleWidget *widget)
{
    foreach ( AbstractWeatherService* service, m_services ) {
        service->setMarbleWidget( widget );
    }
}

void WeatherModel::addService( AbstractWeatherService *service )
{
    service->setFavoriteItems( favoriteItems() );

    connect( service, SIGNAL(createdItems(QList<AbstractDataPluginItem*>)),
             this, SLOT(addItemsToList(QList<AbstractDataPluginItem*>)) );
    connect( service, SIGNAL(requestedDownload(QUrl,
                                               QString, 
                                               AbstractDataPluginItem*)),
             this, SLOT(downloadItemData(QUrl,
                                         QString,
                                         AbstractDataPluginItem*)) );
    connect( service, SIGNAL(downloadDescriptionFileRequested(QUrl)),
             this, SLOT(downloadDescriptionFileRequested(QUrl)) );

    connect( this, SIGNAL(additionalItemsRequested(const GeoDataLatLonAltBox,
                                                   const MarbleModel*,
                                                   qint32)),
             service, SLOT(getAdditionalItems(const GeoDataLatLonAltBox,
                                              const MarbleModel*,
                                              qint32)) );
    connect( this, SIGNAL(parseFileRequested(QByteArray)),
             service, SLOT(parseFile(QByteArray)) );

    m_services.append( service );
}

#include "WeatherModel.moc"

{'fraction_non_alphanumeric': 0.10297345839153861, 'fraction_numerical': 0.004789463181001796, 'mean_word_length': 2.5008732099196647, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '967074', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2677, 'n_words': 608}