radioapp/radiouiengine/src/radiostationmodel_p.cpp
changeset 16 f54ebcfc1b80
parent 14 63aabac4416d
child 19 afea38384506
equal deleted inserted replaced
14:63aabac4416d 16:f54ebcfc1b80
    23 #include "radiostationmodel_p.h"
    23 #include "radiostationmodel_p.h"
    24 #include "radiologger.h"
    24 #include "radiologger.h"
    25 #include "radiopresetstorage.h"
    25 #include "radiopresetstorage.h"
    26 #include "radioenginewrapper.h"
    26 #include "radioenginewrapper.h"
    27 #include "radiouiengine.h"
    27 #include "radiouiengine.h"
       
    28 #include "radiouiengine_p.h"
    28 #include "radiostation.h"
    29 #include "radiostation.h"
    29 #include "radioplaylogmodel.h"
    30 #include "radiostation_p.h"
       
    31 #include "radiohistorymodel.h"
    30 #ifndef BUILD_WIN32
    32 #ifndef BUILD_WIN32
    31 #   include "radiomonitorservice.h"
    33 #   include "radiomonitorservice.h"
    32 #else
    34 #else
    33 #   include "radiomonitorservice_win32.h"
    35 #   include "radiomonitorservice_win32.h"
    34 #endif
    36 #endif
    41 
    43 
    42 /*!
    44 /*!
    43  *
    45  *
    44  */
    46  */
    45 RadioStationModelPrivate::RadioStationModelPrivate( RadioStationModel* model,
    47 RadioStationModelPrivate::RadioStationModelPrivate( RadioStationModel* model,
    46                                                     RadioUiEngine& uiEngine ) :
    48                                                     RadioUiEnginePrivate& uiEngine ) :
    47     q_ptr( model ),
    49     q_ptr( model ),
    48     mUiEngine( uiEngine ),
    50     mUiEngine( uiEngine ),
    49     mCurrentStation( &mManualStation ),
    51     mCurrentStation( &mManualStation ),
    50     mDynamicPsTimer( new QTimer() )
    52     mDynamicPsTimer( new QTimer() )
    51 {
    53 {
    83  * Sets the currently tuned frequency. Meant to be set by the engine wrapper
    85  * Sets the currently tuned frequency. Meant to be set by the engine wrapper
    84  */
    86  */
    85 void RadioStationModelPrivate::setCurrentStation( uint frequency )
    87 void RadioStationModelPrivate::setCurrentStation( uint frequency )
    86 {
    88 {
    87     LOG_METHOD;
    89     LOG_METHOD;
       
    90     RadioStation* oldStation = mCurrentStation;
    88     if ( mStations.contains( frequency ) ) {
    91     if ( mStations.contains( frequency ) ) {
    89         // We have to be careful to check that key exists before using operator[]
    92         // We have to be careful to check that key exists before using operator[]
    90         // with QMap since it will insert a default-constructed value if it doesn't exist yet.
    93         // with QMap since it will insert a default-constructed value if it doesn't exist yet.
    91         mCurrentStation = &mStations[ frequency ];
    94         mCurrentStation = &mStations[ frequency ];
    92     } else {
    95     } else {
    93         mManualStation.reset();
    96         mManualStation.reset();
    94         mManualStation.setFrequency( frequency );
    97         mManualStation.setFrequency( frequency );
    95         mCurrentStation = &mManualStation;
    98         mCurrentStation = &mManualStation;
    96     }
    99     }
    97 
   100 
    98     mUiEngine.monitor().notifyName( mCurrentStation->name().isEmpty() ? mCurrentStation->frequencyMhz() 
   101     Q_Q( RadioStationModel );
    99                                                                       : mCurrentStation->name() );
   102     if ( oldStation && oldStation->isValid() ) {
       
   103         q->emitDataChanged( *oldStation );
       
   104     }
       
   105 
       
   106     mUiEngine.api().monitor().notifyName( mCurrentStation->name().isEmpty() ? mCurrentStation->frequencyMhz()
       
   107                                                                             : mCurrentStation->name() );
   100 }
   108 }
   101 
   109 
   102 /*!
   110 /*!
   103  * \reimp
   111  * \reimp
   104  * Sets the genre to the currently tuned station
   112  * Sets the genre to the currently tuned station
   111         LOG( "Unable to find current station. Ignoring RDS" );
   119         LOG( "Unable to find current station. Ignoring RDS" );
   112         return;
   120         return;
   113     }
   121     }
   114     station.setGenre( genre );
   122     station.setGenre( genre );
   115     q->saveStation( station );
   123     q->saveStation( station );
   116     mUiEngine.monitor().notifyGenre( mUiEngine.genreToString( genre ) );
   124     mUiEngine.api().monitor().notifyGenre( mUiEngine.api().genreToString( genre, GenreTarget::HomeScreen ) );
       
   125 }
       
   126 
       
   127 /*!
       
   128  * \reimp
       
   129  *
       
   130  */
       
   131 void RadioStationModelPrivate::tunedToFrequency( uint frequency, int reason )
       
   132 {
       
   133     if ( reason == TuneReason::Seek ) {
       
   134         addScannedFrequency( frequency );
       
   135     }
       
   136 
       
   137     setCurrentStation( frequency );
       
   138     startDynamicPsCheck();
   117 }
   139 }
   118 
   140 
   119 /*!
   141 /*!
   120  * \reimp
   142  * \reimp
   121  * Checks if the given frequency exists in the list
   143  * Checks if the given frequency exists in the list
   179     foreach( const RadioStation& station, mStations ) {
   201     foreach( const RadioStation& station, mStations ) {
   180         if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) {
   202         if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) {
   181             q->removeStation( station );
   203             q->removeStation( station );
   182         }
   204         }
   183     }
   205     }
       
   206     q->reset();
   184 }
   207 }
   185 
   208 
   186 /*!
   209 /*!
   187  * \reimp
   210  * \reimp
   188  * Sets the PS name to the currently tuned station
   211  * Sets the PS name to the currently tuned station
   189  */
   212  */
   190 void RadioStationModelPrivate::setCurrentPsName( uint frequency, const QString& name )
   213 void RadioStationModelPrivate::setCurrentPsName( uint frequency, const QString& name )
   191 {
   214 {
   192     Q_Q( RadioStationModel );
   215     Q_Q( RadioStationModel );
   193     LOG_FORMAT( "RadioEngineWrapperPrivate::updateCurrentStationName: %s", GETSTRING( name ) );
   216     LOG_FORMAT( "void RadioStationModelPrivate::setCurrentPsName: %s", GETSTRING( name ) );
   194     RadioStation station = q->findCurrentStation( frequency );
   217     RadioStation station = q->findCurrentStation( frequency );
   195     if ( !station.isValid() ) {
   218     if ( !station.isValid() ) {
   196         LOG( "Unable to find current station. Ignoring RDS" );
   219         LOG( "Unable to find current station. Ignoring RDS" );
   197         return;
   220         return;
   198     }
   221     }
   200     if ( station.psType() == RadioStation::Static ) {
   223     if ( station.psType() == RadioStation::Static ) {
   201 
   224 
   202         if ( name.compare( station.name() ) != 0 && !station.isRenamed() ) {
   225         if ( name.compare( station.name() ) != 0 && !station.isRenamed() ) {
   203             station.setName( name );
   226             station.setName( name );
   204             q->saveStation( station );
   227             q->saveStation( station );
   205             mUiEngine.monitor().notifyName( name );
   228             mUiEngine.api().monitor().notifyName( name );
   206         }
   229         }
   207 
   230 
   208     } else {
   231     } else {
   209 
   232 
   210         if ( mDynamicPsTimer->isActive() ) {    // Dynamic PS check is ongoing
   233         if ( mDynamicPsTimer->isActive() ) {    // Dynamic PS check is ongoing
   217                 mDynamicPsTimer->stop();
   240                 mDynamicPsTimer->stop();
   218 
   241 
   219                 // Cleanup the station name if region is not America
   242                 // Cleanup the station name if region is not America
   220                 if ( !station.name().isEmpty()
   243                 if ( !station.name().isEmpty()
   221                      && !station.isRenamed()
   244                      && !station.isRenamed()
   222                      && mEngine->region() != RadioRegion::America )
   245                      && mWrapper->region() != RadioRegion::America )
   223                 {
   246                 {
   224                     LOG( "Station name cleanup" );
   247                     LOG( "Station name cleanup" );
   225                     station.setName( "" );
   248                     station.setName( "" );
   226                 }
   249                 }
   227             }
   250             }
   258         LOG( "Unable to find current station. Ignoring RDS" );
   281         LOG( "Unable to find current station. Ignoring RDS" );
   259         return;
   282         return;
   260     }
   283     }
   261     station.setRadioText( radioText );
   284     station.setRadioText( radioText );
   262     q->saveStation( station );
   285     q->saveStation( station );
   263     mUiEngine.playLogModel().clearRadioTextPlus();
   286     mUiEngine.api().historyModel().clearRadioTextPlus();
   264     mUiEngine.monitor().notifyRadioText( radioText );
   287     mUiEngine.api().monitor().notifyRadioText( radioText );
   265 }
   288 }
   266 
   289 
   267 /*!
   290 /*!
   268  * \reimp
   291  * \reimp
   269  * Sets the radio text plus to the currently tuned station
   292  * Sets the radio text plus to the currently tuned station
   276         LOG( "Unable to find current station. Ignoring RDS" );
   299         LOG( "Unable to find current station. Ignoring RDS" );
   277         return;
   300         return;
   278     }
   301     }
   279     station.setRadioTextPlus( rtClass, rtItem );
   302     station.setRadioTextPlus( rtClass, rtItem );
   280     q->saveStation( station );
   303     q->saveStation( station );
   281     mUiEngine.playLogModel().addRadioTextPlus( rtClass, rtItem, station );
   304     mUiEngine.api().historyModel().addRadioTextPlus( rtClass, rtItem, station );
   282 }
   305 }
   283 
   306 
   284 /*!
   307 /*!
   285  * \reimp
   308  * \reimp
   286  * Sets the PI code to the currently tuned station
   309  * Sets the PI code to the currently tuned station
   294         return;
   317         return;
   295     }
   318     }
   296 #ifdef SHOW_CALLSIGN_IN_ANY_REGION
   319 #ifdef SHOW_CALLSIGN_IN_ANY_REGION
   297     RadioRegion::Region region = RadioRegion::America;
   320     RadioRegion::Region region = RadioRegion::America;
   298 #else
   321 #else
   299     RadioRegion::Region region =  mEngine->region();
   322     RadioRegion::Region region =  mWrapper->region();
   300 #endif
   323 #endif
   301 
   324 
   302     station.setPiCode( piCode, region );
   325     station.setPiCode( piCode, region );
   303     q->saveStation( station );
   326     q->saveStation( station );
   304 }
   327 }
   305 
   328 
       
   329 /*!
       
   330  *
       
   331  */
       
   332 void RadioStationModelPrivate::doSaveStation( RadioStation& station, bool persistentSave )
       
   333 {
       
   334     mStations.insert( station.frequency(), station );
       
   335 
       
   336     if ( persistentSave ) {
       
   337         const bool success = mPresetStorage->savePreset( *station.data_ptr() );
       
   338         RADIO_ASSERT( success, "RadioStationModelPrivate::saveStation", "Failed to add station" );
       
   339     }
       
   340 }