radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 57 21be958eb3ce
parent 54 a8ba0c289b44
equal deleted inserted replaced
56:04837bf3a628 57:21be958eb3ce
    26 #include "radiouiengine.h"
    26 #include "radiouiengine.h"
    27 #include "radiouiengine_p.h"
    27 #include "radiouiengine_p.h"
    28 #include "radiostation.h"
    28 #include "radiostation.h"
    29 #include "radiostation_p.h"
    29 #include "radiostation_p.h"
    30 #include "radiologger.h"
    30 #include "radiologger.h"
       
    31 
       
    32 // Constants
    31 
    33 
    32 /*!
    34 /*!
    33  *
    35  *
    34  */
    36  */
    35 static QString parseLine( const RadioStation& station )
    37 static QString parseLine( const RadioStation& station )
   422  * Removes the given station
   424  * Removes the given station
   423  */
   425  */
   424 void RadioStationModel::removeStation( const RadioStation& station )
   426 void RadioStationModel::removeStation( const RadioStation& station )
   425 {
   427 {
   426     Q_D( RadioStationModel );
   428     Q_D( RadioStationModel );
   427     const uint frequency = station.frequency();
   429     d->doRemoveStation( station );   
   428     if ( d->mStations.contains( frequency ) ) {
   430 }
   429 
   431 
   430         // If we are removing the current station, copy its data to the current station pointer
       
   431         // to keep all of the received RDS data still available. They will be discarded when
       
   432         // the user tunes to another frequency, but they are available if the user decides to add it back.
       
   433         if ( d->mCurrentStation->frequency() == frequency ) {
       
   434             *d->mCurrentStation = station;
       
   435         }
       
   436 
       
   437         // Copy the station to a temporary variable that can be used as signal parameter
       
   438         RadioStation tempStation = station;
       
   439 
       
   440         const int row = indexFromFrequency( tempStation.frequency() );
       
   441         beginRemoveRows( QModelIndex(), row, row );
       
   442 
       
   443         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
       
   444         d->mStations.remove( frequency );
       
   445 
       
   446         d->mCurrentStation = NULL;
       
   447         d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   448 
       
   449         endRemoveRows();
       
   450     }
       
   451 }
       
   452 /*!
   432 /*!
   453  * Removes stations based on model indices
   433  * Removes stations based on model indices
   454  */
   434  */
   455 void RadioStationModel::removeByModelIndices( QModelIndexList& indices, bool removefavorite )
   435 void RadioStationModel::removeByModelIndices( QModelIndexList& indices, bool removefavorite )
   456 {
   436 {
       
   437     Q_D( RadioStationModel );
   457     // List needs to be sorted and indices needs to go throught from largest to smallest.
   438     // List needs to be sorted and indices needs to go throught from largest to smallest.
   458     // This is for keeping QmodelIndexing in sync after begin- and endremoverows, which
   439     // This is for keeping QmodelIndexing in sync after begin- and endremoverows, which
   459     // are needed for each item separately
   440     // are needed for each item separately
   460     qSort( indices );
   441     qSort( indices );
   461     QModelIndexList::const_iterator iter = indices.constEnd();
   442     QModelIndexList::const_iterator iter = indices.constEnd();
   462     QModelIndexList::const_iterator begin = indices.constBegin();
   443     QModelIndexList::const_iterator begin = indices.constBegin();
       
   444     QList<uint> frequencies;
   463     RadioStation station;  
   445     RadioStation station;  
   464     while( iter != begin ) {
   446     while( iter != begin ) {
   465        iter--;
   447         iter--;
   466        station = stationAt( (*iter).row() );
   448         station = stationAt( (*iter).row() );
   467        if( removefavorite ) {
   449         if( removefavorite ) {
   468           setFavoriteByPreset( station.presetIndex(), false );   
   450             setFavoriteByPreset( station.presetIndex(), false );   
   469       } else {                     
   451         } else {    
   470           removeStation( station );
   452             frequencies.append( station.frequency() );
   471       }                     
   453             d->doRemoveStation( station, false );
       
   454         }                     
       
   455     }
       
   456     if( !removefavorite && frequencies.count() ) {
       
   457         emit stationsRemoved( frequencies );
   472     }
   458     }
   473 }
   459 }
   474 
   460 
   475 /*!
   461 /*!
   476  * Public slot
   462  * Public slot
   482     if ( d->mStations.count() == 0 ) {
   468     if ( d->mStations.count() == 0 ) {
   483         return;
   469         return;
   484     }
   470     }
   485 
   471 
   486     if ( mode == RemoveAll ) {
   472     if ( mode == RemoveAll ) {
       
   473         
       
   474         // sace frequencies locally so that they can be used while emitting signal
       
   475         QList<uint> frequencies = list().keys();        
       
   476         
       
   477         // notify changes about current station for UI to update correctly
       
   478         d->mCurrentStation->setFavorite( false );
       
   479         d->mCurrentStation->setUserDefinedName( "" );
       
   480         emitChangeSignals( *d->mCurrentStation, d->mCurrentStation->changeFlags() );
       
   481         
   487         beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
   482         beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
   488 
   483 
   489         // Preset utility deletes all presets with index -1
   484         // Preset utility deletes all presets with index -1
   490         bool success = d->mPresetStorage->deletePreset( -1 );
   485         bool success = d->mPresetStorage->deletePreset( -1 );
   491         Q_UNUSED( success );
   486         Q_UNUSED( success );
   492         RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
   487         RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
   493 
   488         
   494         d->mStations.clear();
   489         d->mStations.clear();
   495         d->mCurrentStation = NULL;
   490         d->mCurrentStation = NULL;
   496         d->setCurrentStation( d->mWrapper->currentFrequency() );
   491         d->setCurrentStation( d->mWrapper->currentFrequency() );
   497 
   492 
   498         endRemoveRows();
   493         endRemoveRows();
       
   494         
       
   495         emit stationsRemoved( frequencies );
       
   496         
   499     } else {
   497     } else {
   500         foreach( const RadioStation& station, d->mStations ) {
   498         foreach( const RadioStation& station, d->mStations ) {
   501 
   499 
   502             if ( mode == RemoveLocalStations ) {
   500             if ( mode == RemoveLocalStations ) {
   503                 if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) {
   501                 if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) {
   653 void RadioStationModel::renameStation( int presetIndex, const QString& name )
   651 void RadioStationModel::renameStation( int presetIndex, const QString& name )
   654 {
   652 {
   655     LOG_FORMAT( "RadioStationModel::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   653     LOG_FORMAT( "RadioStationModel::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   656     RadioStation station;
   654     RadioStation station;
   657     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   655     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   658         station.setUserDefinedName( name.left(15) ); // Only 15 characters allowed
   656         station.setUserDefinedName( name.left( MAX_STATION_NAME_LENGTH ) );
   659         saveStation( station );
   657         saveStation( station );
   660     }
   658     }
   661 }
   659 }
   662 
   660 
   663 /*!
   661 /*!
   744         saveStation( *d->mCurrentStation );
   742         saveStation( *d->mCurrentStation );
   745     }
   743     }
   746 }
   744 }
   747 
   745 
   748 /*!
   746 /*!
       
   747  * Private slot
       
   748  * Clears the radiotext from a station after its timeout has passed
       
   749  */
       
   750 void RadioStationModel::clearRadiotext( int id )
       
   751 {
       
   752     uint frequency = static_cast<uint>( id );
       
   753     RadioStation station = findStation( frequency );
       
   754     if ( station.isValid() ) {
       
   755         station.setRadioText( "" );
       
   756         saveStation( station );
       
   757     }
       
   758 }
       
   759 
       
   760 /*!
       
   761  * Private slot
       
   762  * Handles the end of RT plus check
       
   763  */
       
   764 void RadioStationModel::rtPlusCheckEnd()
       
   765 {
       
   766     Q_D( RadioStationModel );
       
   767     if ( !d->mRadioTextHolder.isEmpty() ) {
       
   768         d->mCurrentStation->setRadioText( d->mRadioTextHolder );
       
   769         saveStation( *d->mCurrentStation );
       
   770         d->mRadioTextHolder.clear();
       
   771         d->startRadioTextClearTimer();
       
   772     }
       
   773 }
       
   774 
       
   775 /*!
   749  * Checks the given station and emits signals based on what member variables had been changed
   776  * Checks the given station and emits signals based on what member variables had been changed
   750  */
   777  */
   751 void RadioStationModel::emitChangeSignals( const RadioStation& station, RadioStation::Change flags )
   778 void RadioStationModel::emitChangeSignals( const RadioStation& station, RadioStation::Change flags )
   752 {
   779 {
       
   780     // Create a temporary RadioStation for the duration of the signal-slot processing
       
   781     // The receivers can ask the station what data has changed and update accordingly
       
   782     RadioStation tempStation( station );
       
   783     tempStation.setChangeFlags( flags );
       
   784 
   753     if ( flags.testFlag( RadioStation::NameChanged ) ||
   785     if ( flags.testFlag( RadioStation::NameChanged ) ||
   754          flags.testFlag( RadioStation::GenreChanged ) ||
   786          flags.testFlag( RadioStation::GenreChanged ) ||
   755          flags.testFlag( RadioStation::UrlChanged ) ||
   787          flags.testFlag( RadioStation::UrlChanged ) ||
   756          flags.testFlag( RadioStation::TypeChanged ) ||
   788          flags.testFlag( RadioStation::TypeChanged ) ||
   757          flags.testFlag( RadioStation::PiCodeChanged ) ) {
   789          flags.testFlag( RadioStation::PiCodeChanged ) ) {
   758 
   790 
   759         // Create a temporary RadioStation for the duration of the signal-slot processing
       
   760         // The receivers can ask the station what data has changed and update accordingly
       
   761         RadioStation tempStation( station );
       
   762         tempStation.setChangeFlags( flags );
       
   763         emit stationDataChanged( tempStation );
   791         emit stationDataChanged( tempStation );
   764 
   792 
   765         emitDataChanged( tempStation );
   793         emitDataChanged( tempStation );
   766     }
   794     }
   767 
   795 
   768     if ( flags.testFlag( RadioStation::RadioTextChanged ) ) {
   796     if ( flags.testFlag( RadioStation::RadioTextChanged ) ) {
   769         emit radioTextReceived( station );
   797 
   770         emitDataChanged( station );
   798         emit radioTextReceived( tempStation );
       
   799         emitDataChanged( tempStation );
   771     }
   800     }
   772 
   801 
   773     if ( flags.testFlag( RadioStation::DynamicPsChanged ) ) {
   802     if ( flags.testFlag( RadioStation::DynamicPsChanged ) ) {
   774         emit dynamicPsChanged( station );
   803         emit dynamicPsChanged( tempStation );
   775         emitDataChanged( station );
   804         emitDataChanged( tempStation );
   776     }
   805     }
   777 
   806 
   778     if ( flags.testFlag( RadioStation::FavoriteChanged ) && station.isValid() ) {
   807     if ( flags.testFlag( RadioStation::FavoriteChanged ) && station.isValid() ) {
   779         emit favoriteChanged( station );
   808         emit favoriteChanged( tempStation );
   780         emitDataChanged( station );
   809         emitDataChanged( tempStation );
   781     }
   810     }
   782 }
   811 }
   783 
   812 
   784 /*!
   813 /*!
   785  *
   814  *