radioapp/radiouiengine/src/radiostationmodel_p.cpp
changeset 57 21be958eb3ce
parent 47 74b7c6e79031
equal deleted inserted replaced
56:04837bf3a628 57:21be958eb3ce
    32 
    32 
    33 // Constants
    33 // Constants
    34 /**
    34 /**
    35  * Timeout period for checking if station is sending dynamic PS in milliseconds
    35  * Timeout period for checking if station is sending dynamic PS in milliseconds
    36  */
    36  */
    37 const int DYNAMIC_PS_CHECK_TIMEOUT = 10 * 1000;
    37 const int DYNAMIC_PS_CHECK_TIMEOUT = 10 * 1000; // 10 seconds
       
    38 
       
    39 const int RADIOTEXT_CLEAR_TIMEOUT = 1 * 60 * 1000; // One minute
       
    40 const int RTPLUS_CHECK_TIMEOUT = 700;
       
    41 
       
    42 enum TimerId{ RtPlusCheck = 999 };
       
    43 
       
    44 const QLatin1String HTML_TAG_START( "<font color='cyan'><u>" );
       
    45 const QLatin1String HTML_TAG_END( "</u></font>" );
    38 
    46 
    39 /*!
    47 /*!
    40  *
    48  *
    41  */
    49  */
    42 RadioStationModelPrivate::RadioStationModelPrivate( RadioStationModel* model,
    50 RadioStationModelPrivate::RadioStationModelPrivate( RadioStationModel* model,
   122  * \reimp
   130  * \reimp
   123  *
   131  *
   124  */
   132  */
   125 void RadioStationModelPrivate::tunedToFrequency( uint frequency, int reason )
   133 void RadioStationModelPrivate::tunedToFrequency( uint frequency, int reason )
   126 {
   134 {
   127     if ( reason == TuneReason::Seek ) {
   135     if ( !mRadioTextHolder.isEmpty() ) {
   128         addScannedFrequency( frequency );
   136         mRadioTextHolder.clear();
       
   137         Q_Q( RadioStationModel );
       
   138         mUiEngine.api().cancelTimer( RtPlusCheck, q );
   129     }
   139     }
   130 
   140 
   131     setCurrentStation( frequency );
   141     setCurrentStation( frequency );
   132     startDynamicPsCheck();
   142     startDynamicPsCheck();
   133 }
   143 }
   257     RadioStation station = q->findStation( frequency, FindCriteria::IncludeManualStation );
   267     RadioStation station = q->findStation( frequency, FindCriteria::IncludeManualStation );
   258     if ( !station.isValid() ) {
   268     if ( !station.isValid() ) {
   259         LOG( "Unable to find current station. Ignoring RDS" );
   269         LOG( "Unable to find current station. Ignoring RDS" );
   260         return;
   270         return;
   261     }
   271     }
   262     station.setRadioText( radioText );
   272 
   263     q->saveStation( station );
       
   264     mUiEngine.api().historyModel().clearRadioTextPlus();
   273     mUiEngine.api().historyModel().clearRadioTextPlus();
       
   274 
       
   275     mRadioTextHolder = radioText;
       
   276     mRadioTextPlusItems.clear();
       
   277 
       
   278     if ( !radioText.isEmpty() ) {
       
   279         mUiEngine.api().startTimer( RTPLUS_CHECK_TIMEOUT, RtPlusCheck,
       
   280                                     q, SLOT(rtPlusCheckEnd()) );
       
   281     } else {
       
   282         station.setRadioText( "" );
       
   283         q->saveStation( station );
       
   284     }
   265 }
   285 }
   266 
   286 
   267 /*!
   287 /*!
   268  * \reimp
   288  * \reimp
   269  * Sets the radio text plus to the currently tuned station
   289  * Sets the radio text plus to the currently tuned station
   270  */
   290  */
   271 void RadioStationModelPrivate::setCurrentRadioTextPlus( uint frequency, int rtClass, const QString& rtItem )
   291 void RadioStationModelPrivate::setCurrentRadioTextPlus( uint frequency, int rtClass, const QString& rtItem )
   272 {
   292 {
       
   293     LEVEL2( LOG_METHOD );
       
   294 
   273     Q_Q( RadioStationModel );
   295     Q_Q( RadioStationModel );
   274     RadioStation station = q->findStation( frequency, FindCriteria::IncludeManualStation );
   296     RadioStation station = q->findStation( frequency, FindCriteria::IncludeManualStation );
   275     if ( !station.isValid() ) {
   297     if ( !station.isValid() ) {
   276         LOG( "Unable to find current station. Ignoring RDS" );
   298         LOG( "Unable to find current station. Ignoring RDS" );
   277         return;
   299         return;
   278     }
   300     }
   279     station.setRadioTextPlus( rtClass, rtItem );
   301 
   280     q->saveStation( station );
   302     if ( mRadioTextPlusItems.isEmpty() ) { // First RT tag has arrived
   281     mUiEngine.api().historyModel().addRadioTextPlus( rtClass, rtItem, station );
   303 
       
   304         const RtPlus::Tag tag = recognizeRtPlusTag( rtClass );
       
   305         mRadioTextPlusItems.insert( tag, rtItem );
       
   306 
       
   307     } else { // Second RT tag has arrived
       
   308 
       
   309         const RtPlus::Tag tag = recognizeRtPlusTag( rtClass );
       
   310         mRadioTextPlusItems.insert( tag, rtItem );
       
   311 
       
   312         handleRadioTextPlus( mRadioTextPlusItems, station );
       
   313         mRadioTextPlusItems.clear();
       
   314 
       
   315     }
   282 }
   316 }
   283 
   317 
   284 /*!
   318 /*!
   285  * \reimp
   319  * \reimp
   286  * Sets the PI code to the currently tuned station
   320  * Sets the PI code to the currently tuned station
   318 }
   352 }
   319 
   353 
   320 /*!
   354 /*!
   321  *
   355  *
   322  */
   356  */
       
   357 void RadioStationModelPrivate::doRemoveStation( const RadioStation& station, bool signalFrequency )
       
   358 {
       
   359     Q_Q( RadioStationModel );
       
   360     const uint frequency = station.frequency();
       
   361     if ( mStations.contains( frequency ) ) {
       
   362 
       
   363         // If we are removing the current station, copy its data to the current station pointer
       
   364         // to keep all of the received RDS data still available. Name and favorite setting have  
       
   365         // to be cleared. Station data will be discarded when the user tunes to another frequency, 
       
   366         // but they are available if the user decides to add it back.
       
   367         if ( mCurrentStation->frequency() == frequency ) {
       
   368             *mCurrentStation = station;
       
   369             mCurrentStation->setFavorite( false );
       
   370             mCurrentStation->setUserDefinedName( "" );
       
   371             q->emitChangeSignals( *mCurrentStation, mCurrentStation->changeFlags() );
       
   372         }
       
   373 
       
   374         const int row = q->indexFromFrequency( station.frequency() );
       
   375         q->beginRemoveRows( QModelIndex(), row, row );
       
   376         
       
   377         mPresetStorage->deletePreset( station.presetIndex() );
       
   378         mStations.remove( frequency );
       
   379 
       
   380         mCurrentStation = NULL;
       
   381         setCurrentStation( mWrapper->currentFrequency() );
       
   382         
       
   383         q->endRemoveRows();
       
   384         if( signalFrequency ) {
       
   385             emit q->stationsRemoved( QList<uint>() << frequency );
       
   386         }
       
   387     }
       
   388 }
       
   389 
       
   390 /*!
       
   391  *
       
   392  */
   323 QList<RadioStation> RadioStationModelPrivate::favorites() const
   393 QList<RadioStation> RadioStationModelPrivate::favorites() const
   324 {
   394 {
   325     QList<RadioStation> favoriteList;
   395     QList<RadioStation> favoriteList;
   326     foreach( const RadioStation& tempStation, mStations ) {
   396     foreach( const RadioStation& tempStation, mStations ) {
   327         if ( tempStation.isFavorite() ) {
   397         if ( tempStation.isFavorite() ) {
   342             localList.append( tempStation );
   412             localList.append( tempStation );
   343         }
   413         }
   344     }
   414     }
   345     return localList;
   415     return localList;
   346 }
   416 }
       
   417 
       
   418 /*!
       
   419  *
       
   420  */
       
   421 void RadioStationModelPrivate::startRadioTextClearTimer()
       
   422 {
       
   423     Q_Q( RadioStationModel );
       
   424     mUiEngine.api().startTimer( RADIOTEXT_CLEAR_TIMEOUT,
       
   425                                 static_cast<int>( mCurrentStation->frequency() ),
       
   426                                 q,
       
   427                                 SLOT(clearRadiotext(int)) );
       
   428 }
       
   429 
       
   430 /*!
       
   431  *
       
   432  */
       
   433 RtPlus::Tag RadioStationModelPrivate::recognizeRtPlusTag( int rtClass )
       
   434 {
       
   435     switch ( rtClass ) {
       
   436 
       
   437         case RtPlus::Dummy:
       
   438             return RtPlus::Dummy;
       
   439 
       
   440         case RtPlus::Title:
       
   441             return RtPlus::Title;
       
   442 
       
   443         case RtPlus::Artist:
       
   444             return RtPlus::Artist;
       
   445 
       
   446         case RtPlus::Band:
       
   447             return RtPlus::Band;
       
   448 
       
   449         case RtPlus::Homepage:
       
   450             return RtPlus::Homepage;
       
   451 
       
   452         default:
       
   453             break;
       
   454     }
       
   455 
       
   456     return RtPlus::Unknown;
       
   457 }
       
   458 
       
   459 /*!
       
   460  *
       
   461  */
       
   462 void RadioStationModelPrivate::handleRadioTextPlus( const RtPlusMap& rtItems, RadioStation& station )
       
   463 {
       
   464     Q_Q( RadioStationModel );
       
   465     if ( rtItems.contains( RtPlus::Homepage ) ) {
       
   466 
       
   467         station.setUrl( rtItems.value( RtPlus::Homepage ) );
       
   468         q->saveStation( station );
       
   469 
       
   470     } else if ( rtItems.contains( RtPlus::Artist ) || rtItems.contains( RtPlus::Title ) ) {
       
   471 
       
   472         mUiEngine.api().cancelTimer( RtPlusCheck, q );
       
   473 
       
   474         QList<RtPlus::Tag> tags;
       
   475         tags << RtPlus::Artist << RtPlus::Title;
       
   476         foreach( RtPlus::Tag tag, tags ) {
       
   477             QString item = rtItems.value( tag );
       
   478             if ( !item.isEmpty() ) {
       
   479                 const QString replacement = HTML_TAG_START + item + HTML_TAG_END;
       
   480                 mRadioTextHolder.replace( item, replacement );
       
   481             }
       
   482         }
       
   483         station.setRadioText( mRadioTextHolder );
       
   484         mRadioTextHolder.clear();
       
   485         q->saveStation( station );
       
   486         startRadioTextClearTimer();
       
   487         mUiEngine.api().historyModel().addItem( rtItems.value( RtPlus::Artist ), rtItems.value( RtPlus::Title ), station );
       
   488 
       
   489     }
       
   490 }