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

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 "StationListParser.h"
0008 
0009 // Marble
0010 #include "MarbleGlobal.h"
0011 #include "BBCStation.h"
0012 #include "GeoDataCoordinates.h"
0013 #include "MarbleDebug.h"
0014 
0015 // Qt
0016 #include <QFile>
0017 #include <QString>
0018 
0019 using namespace Marble;
0020 
0021 StationListParser::StationListParser( QObject *parent )
0022     : QThread( parent ),
0023       QXmlStreamReader()
0024 {
0025 }
0026 
0027 StationListParser::~StationListParser()
0028 {
0029     wait( 1000 );
0030 }
0031 
0032 void StationListParser::read()
0033 {
0034     m_list.clear();
0035 
0036     while ( !atEnd() ) {
0037         readNext();
0038 
0039         if ( isStartElement() ) {
0040             if (name() == QLatin1String("StationList"))
0041                 readStationList();
0042             else
0043                 raiseError( QObject::tr("The file is not a valid file.") );
0044         }
0045     }
0046 }
0047 
0048 QList<BBCStation> StationListParser::stationList() const
0049 {
0050     return m_list;
0051 }
0052 
0053 void StationListParser::setPath( const QString& path )
0054 {
0055     m_path = path;
0056 }
0057 
0058 void StationListParser::run()
0059 {
0060     QFile file( m_path );
0061 
0062     if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
0063         return;
0064     }
0065 
0066     setDevice( &file );
0067     read();
0068 }
0069 
0070 void StationListParser::readUnknownElement()
0071 {
0072     Q_ASSERT( isStartElement() );
0073 
0074     while ( !atEnd() ) {
0075         readNext();
0076 
0077         if ( isEndElement() )
0078             break;
0079 
0080         if ( isStartElement() )
0081             readUnknownElement();
0082     }
0083 }
0084 
0085 void StationListParser::readStationList()
0086 {
0087     Q_ASSERT( isStartElement()
0088               && name() == QLatin1String("StationList"));
0089               
0090     while( !atEnd() ) {
0091         readNext();
0092         
0093         if( isEndElement() )
0094             break;
0095         
0096         if( isStartElement() ) {
0097             if (name() == QLatin1String("Station"))
0098                 readStation();
0099             else
0100                 readUnknownElement();
0101         }
0102     }
0103 }
0104     
0105 void StationListParser::readStation()
0106 {
0107     Q_ASSERT( isStartElement()
0108               && name() == QLatin1String("Station"));
0109     
0110     BBCStation station;
0111     
0112     while ( !atEnd() ) {
0113         readNext();
0114         
0115         if( isEndElement() )
0116             break;
0117         
0118         if( isStartElement() ) {
0119             if (name() == QLatin1String("name"))
0120                 station.setName( readCharacters() );
0121             else if (name() == QLatin1String("id"))
0122                 station.setBbcId( readCharacters().toLong() );
0123             else if (name() == QLatin1String("priority"))
0124                 station.setPriority( readCharacters().toInt() );
0125             else if (name() == QLatin1String("Point"))
0126                 readPoint( &station );
0127             else
0128                 readUnknownElement();
0129         }
0130     }
0131 
0132     // This find the right position in the sorted to insert the new item
0133     QList<BBCStation>::iterator i = std::lower_bound( m_list.begin(),
0134                                                  m_list.end(),
0135                                                  station );
0136     // Insert the item on the right position in the list
0137     m_list.insert( i, station );
0138 }
0139 
0140 QString StationListParser::readCharacters()
0141 {
0142     Q_ASSERT( isStartElement() );
0143     
0144     QString string;
0145     
0146     while ( !atEnd() ) {
0147         readNext();
0148         
0149         if ( isEndElement() )
0150             break;
0151         
0152         if ( isStartElement() ) {
0153             readUnknownElement();
0154         }
0155         
0156         if ( isCharacters() ) {
0157             string = text().toString();
0158         }
0159     }
0160     
0161     return string;
0162 }
0163 
0164 void StationListParser::readPoint( BBCStation *station )
0165 {
0166     Q_ASSERT( isStartElement()
0167               && name() == QLatin1String("Point"));
0168     
0169     while ( !atEnd() ) {
0170         readNext();
0171         
0172         if ( isEndElement() )
0173             break;
0174         
0175         if ( isStartElement() ) {
0176             if (name() == QLatin1String("coordinates")) {
0177                 QString coorString = readCharacters();
0178                 QStringList coorList = coorString.split(QLatin1Char(','));
0179                 
0180                 if ( coorList.size() >= 2 ) {
0181                     GeoDataCoordinates coordinates( coorList.at( 0 ).toFloat() * DEG2RAD,
0182                                                     coorList.at( 1 ).toFloat() * DEG2RAD );
0183                     station->setCoordinate( coordinates );
0184                 }
0185             }
0186             else
0187                 readUnknownElement();
0188         }
0189     }
0190 }
0191 
0192 #include "moc_StationListParser.cpp"