radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 19 afea38384506
parent 16 f54ebcfc1b80
child 37 451b2e1545b2
equal deleted inserted replaced
16:f54ebcfc1b80 19:afea38384506
    24 #include "radioenginewrapper.h"
    24 #include "radioenginewrapper.h"
    25 #include "radiouiengine.h"
    25 #include "radiouiengine.h"
    26 #include "radiouiengine_p.h"
    26 #include "radiouiengine_p.h"
    27 #include "radiostation.h"
    27 #include "radiostation.h"
    28 #include "radiostation_p.h"
    28 #include "radiostation_p.h"
    29 #ifndef BUILD_WIN32
       
    30 #   include "radiomonitorservice.h"
       
    31 #else
       
    32 #   include "radiomonitorservice_win32.h"
       
    33 #endif
       
    34 #include "radiologger.h"
    29 #include "radiologger.h"
    35 
    30 
    36 /*!
    31 /*!
    37  *
    32  *
    38  */
    33  */
   183         RadioStation station;
   178         RadioStation station;
   184         station.detach();
   179         station.detach();
   185 
   180 
   186         RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() );
   181         RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() );
   187         if ( d->mPresetStorage->readPreset( index, *preset ) ) {
   182         if ( d->mPresetStorage->readPreset( index, *preset ) ) {
   188             RADIO_ASSERT( station.isValid(), "RadioStationModelPrivate::initialize", "Invalid station" );
   183             if ( station.isValid() ) {
   189 			// TODO: Remove this if when new Preset utility is taken into use
       
   190             if ( station.frequency() != 87500000 )
       
   191                 {
       
   192                 d->mStations.insert( station.frequency(), station );
   184                 d->mStations.insert( station.frequency(), station );
   193                 }
   185             } else {
       
   186                 LOG( "RadioStationModelPrivate::initialize: Invalid station!" );
       
   187             }
   194         }
   188         }
   195 
   189 
   196 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   190 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   197         index = d->mPresetStorage->nextPreset( index );
   191         index = d->mPresetStorage->nextPreset( index );
   198 #endif
   192 #endif
   258     }
   252     }
   259     return false;
   253     return false;
   260 }
   254 }
   261 
   255 
   262 /*!
   256 /*!
   263  * Finds number of favorite stations
       
   264  */
       
   265 int RadioStationModel::favoriteCount()
       
   266 {
       
   267     Q_D( const RadioStationModel );
       
   268     int count = 0;
       
   269     foreach( const RadioStation& tempStation, d->mStations ) {
       
   270         if ( tempStation.isFavorite() ) {
       
   271             ++count;
       
   272         }
       
   273     }
       
   274     return count;
       
   275 }
       
   276 
       
   277 /*!
       
   278  * Finds a station by preset index
   257  * Finds a station by preset index
   279  */
   258  */
   280 int RadioStationModel::findPresetIndex( int presetIndex )
   259 int RadioStationModel::findPresetIndex( int presetIndex )
   281 {
   260 {
   282     Q_D( RadioStationModel );
   261     Q_D( RadioStationModel );
   300     const int index = findPresetIndex( presetIndex );
   279     const int index = findPresetIndex( presetIndex );
   301     if ( index != RadioStation::NotFound ) {
   280     if ( index != RadioStation::NotFound ) {
   302         station = d->mStations.values().at( index );
   281         station = d->mStations.values().at( index );
   303     }
   282     }
   304     return index;
   283     return index;
       
   284 }
       
   285 
       
   286 /*!
       
   287  * Finds the closest station from the given frequency
       
   288  */
       
   289 RadioStation RadioStationModel::findClosest( const uint frequency, StationSkip::Mode mode )
       
   290 {
       
   291     Q_D( RadioStationModel );
       
   292     const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite;
       
   293     const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite;
       
   294     QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values();
       
   295 
       
   296     // Find the previous and next station from current frequency
       
   297     RadioStation previous;
       
   298     RadioStation next;
       
   299     foreach( const RadioStation& station, list ) {
       
   300         const uint testFreq = station.frequency();
       
   301         if ( testFreq == frequency ) {
       
   302             continue;
       
   303         }
       
   304 
       
   305         if ( testFreq > frequency ) {
       
   306             next = station;
       
   307             break;
       
   308         }
       
   309         previous = station;
       
   310     }
       
   311 
       
   312     // Check if we need to loop around
       
   313     if ( findNext && !next.isValid() ) {
       
   314         next = list.first();
       
   315     } else if ( !findNext && !previous.isValid() ) {
       
   316         previous = list.last();
       
   317     }
       
   318 
       
   319     return findNext ? next : previous;
   305 }
   320 }
   306 
   321 
   307 /*!
   322 /*!
   308  * Removes a station by frequency
   323  * Removes a station by frequency
   309  */
   324  */
   350         beginRemoveRows( QModelIndex(), row, row );
   365         beginRemoveRows( QModelIndex(), row, row );
   351 
   366 
   352         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
   367         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
   353         d->mStations.remove( frequency );
   368         d->mStations.remove( frequency );
   354 
   369 
   355         endRemoveRows();
       
   356 
       
   357         d->mCurrentStation = NULL;
   370         d->mCurrentStation = NULL;
   358         d->setCurrentStation( d->mWrapper->currentFrequency() );
   371         d->setCurrentStation( d->mWrapper->currentFrequency() );
   359 
   372 
   360         emit stationRemoved( tempStation );
   373         endRemoveRows();
   361 
   374     }
   362         if ( tempStation.isFavorite() ) {
   375 }
   363             d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
   376 
   364         }
   377 /*!
   365     }
   378  * Public slot
       
   379  * Removes all stations
       
   380  */
       
   381 void RadioStationModel::removeAll( RemoveMode mode )
       
   382 {
       
   383     Q_D( RadioStationModel );
       
   384     if ( d->mStations.count() == 0 ) {
       
   385         return;
       
   386     }
       
   387 
       
   388     if ( mode == RemoveAll ) {
       
   389         beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
       
   390 
       
   391         // Preset utility deletes all presets with index -1
       
   392         bool success = d->mPresetStorage->deletePreset( -1 );
       
   393         Q_UNUSED( success );
       
   394         RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
       
   395 
       
   396         d->mStations.clear();
       
   397         d->mCurrentStation = NULL;
       
   398         d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   399 
       
   400         endRemoveRows();
       
   401     } else {
       
   402         foreach( const RadioStation& station, d->mStations ) {
       
   403 
       
   404             if ( mode == RemoveLocalStations ) {
       
   405                 if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) {
       
   406                     removeStation( station );
       
   407                 }
       
   408             } else {
       
   409                 if ( station.isFavorite() ) {
       
   410                     RadioStation newStation( station );
       
   411                     newStation.setFavorite( false );
       
   412                     saveStation( newStation );
       
   413                 }
       
   414             }
       
   415         }
       
   416     }
       
   417 
       
   418     reset(); // TODO: Remove. this is a workaround to HbGridView update problem
   366 }
   419 }
   367 
   420 
   368 /*!
   421 /*!
   369  * Adds a new station to the list
   422  * Adds a new station to the list
   370  */
   423  */
   382     // the new station will go in the sorted frequency order
   435     // the new station will go in the sorted frequency order
   383     int row = 0;
   436     int row = 0;
   384     const int count = rowCount();
   437     const int count = rowCount();
   385     if ( count > 1 ) {
   438     if ( count > 1 ) {
   386         Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() );
   439         Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() );
   387         uint iterFreq = iter.key();
       
   388         if ( d->mStations.contains( iter.key() ) ) {
   440         if ( d->mStations.contains( iter.key() ) ) {
   389             row = d->mStations.keys().indexOf( iter.key() );
   441             row = d->mStations.keys().indexOf( iter.key() );
   390         } else {
   442         } else {
   391             row = count;
   443             row = count;
   392         }
   444         }
   397         }
   449         }
   398     }
   450     }
   399 
   451 
   400 //    emit layoutAboutToBeChanged();
   452 //    emit layoutAboutToBeChanged();
   401     beginInsertRows( QModelIndex(), row, row );
   453     beginInsertRows( QModelIndex(), row, row );
   402     // We must add the station here because saveStation() will only update an existing station
       
   403 //    d->mStations.insert( newStation.frequency(), newStation );
       
   404 
   454 
   405     d->doSaveStation( newStation );
   455     d->doSaveStation( newStation );
   406 
   456 
   407     d->setCurrentStation( d->mWrapper->currentFrequency() );
   457     d->setCurrentStation( d->mWrapper->currentFrequency() );
   408 
   458 
   409     endInsertRows();
   459     endInsertRows();
   410 
   460 
   411 //    emit layoutChanged();
   461 //    emit layoutChanged();
   412     emit stationAdded( station );
       
   413 }
   462 }
   414 
   463 
   415 /*!
   464 /*!
   416  * Saves the given station. It is expected to already exist in the list
   465  * Saves the given station. It is expected to already exist in the list
   417  */
   466  */
   427         emitChangeSignals( station, changeFlags );
   476         emitChangeSignals( station, changeFlags );
   428 
   477 
   429     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   478     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   430 
   479 
   431         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
   480         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
       
   481         d->setCurrentStation( d->mWrapper->currentFrequency() );
   432 
   482 
   433         emitChangeSignals( station, changeFlags );
   483         emitChangeSignals( station, changeFlags );
   434     }
   484     }
       
   485 }
       
   486 
       
   487 /*!
       
   488  * Finds number of favorite stations
       
   489  */
       
   490 int RadioStationModel::favoriteCount()
       
   491 {
       
   492     Q_D( const RadioStationModel );
       
   493     return d->favorites().count();
   435 }
   494 }
   436 
   495 
   437 /*!
   496 /*!
   438  * Changes the favorite status of a station by its frequency. If the station does
   497  * Changes the favorite status of a station by its frequency. If the station does
   439  * not yet exist, it is added.
   498  * not yet exist, it is added.
   466             }
   525             }
   467 
   526 
   468             // Emit the signals only after adding the preset and reinitializing the current station
   527             // Emit the signals only after adding the preset and reinitializing the current station
   469             // because the UI will probably query the current station in its slots that get called.
   528             // because the UI will probably query the current station in its slots that get called.
   470             addStation( newStation );
   529             addStation( newStation );
   471             d->setCurrentStation( frequency );
   530         }
   472         }
       
   473 
       
   474         Q_D( RadioStationModel );
       
   475         d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
       
   476     }
   531     }
   477 }
   532 }
   478 
   533 
   479 /*!
   534 /*!
   480  * Changes the favorite status of a station by its preset index
   535  * Changes the favorite status of a station by its preset index
   484     LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex );
   539     LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex );
   485     RadioStation station;
   540     RadioStation station;
   486     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   541     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   487         station.setFavorite( favorite );
   542         station.setFavorite( favorite );
   488         saveStation( station );
   543         saveStation( station );
   489 
       
   490         Q_D( RadioStationModel );
       
   491         d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
       
   492     }
   544     }
   493 }
   545 }
   494 
   546 
   495 /*!
   547 /*!
   496  * Renames a station by its preset index
   548  * Renames a station by its preset index
   500     LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   552     LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   501     RadioStation station;
   553     RadioStation station;
   502     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   554     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   503         station.setUserDefinedName( name );
   555         station.setUserDefinedName( name );
   504         saveStation( station );
   556         saveStation( station );
   505         Q_D( RadioStationModel );
       
   506         d->mUiEngine.api().monitor().notifyName( name );
       
   507     }
   557     }
   508 }
   558 }
   509 
   559 
   510 /*!
   560 /*!
   511  *
   561  *
   515     foreach ( const QModelIndex& index, favorites ) {
   565     foreach ( const QModelIndex& index, favorites ) {
   516         RadioStation station = stationAt( index.row() );
   566         RadioStation station = stationAt( index.row() );
   517         RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation");
   567         RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation");
   518         setFavoriteByPreset( station.presetIndex(), true );
   568         setFavoriteByPreset( station.presetIndex(), true );
   519     }
   569     }
   520 
       
   521     Q_D( RadioStationModel );
       
   522     d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
       
   523 }
   570 }
   524 
   571 
   525 /*!
   572 /*!
   526  * Returns the currently tuned station
   573  * Returns the currently tuned station
   527  */
   574  */
   573     RadioStation station;
   620     RadioStation station;
   574     if ( findFrequency( frequency, station ) ) {
   621     if ( findFrequency( frequency, station ) ) {
   575         return index( findPresetIndex( station.presetIndex() ), 0 );
   622         return index( findPresetIndex( station.presetIndex() ), 0 );
   576     }
   623     }
   577     return QModelIndex();
   624     return QModelIndex();
   578 }
       
   579 
       
   580 /*!
       
   581  * Public slot
       
   582  * Removes all stations
       
   583  */
       
   584 void RadioStationModel::removeAll()
       
   585 {
       
   586     Q_D( RadioStationModel );
       
   587 	
       
   588     if ( d->mStations.count() == 0 ) {
       
   589         return;
       
   590     }
       
   591 
       
   592     QList<RadioStation> tempStations = d->mStations.values();
       
   593 
       
   594     beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
       
   595 
       
   596     // Preset utility deletes all presets with index -1
       
   597     bool success = d->mPresetStorage->deletePreset( -1 );
       
   598     RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
       
   599 
       
   600     d->mStations.clear();
       
   601     d->mCurrentStation = NULL;
       
   602     d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   603 
       
   604     endRemoveRows();
       
   605 
       
   606     foreach( RadioStation station, tempStations ) {
       
   607         emit stationRemoved( station );
       
   608     }
       
   609 
       
   610     reset(); // TODO: Remove. this is a workaround to HbGridView update problem
       
   611 
       
   612     d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
       
   613 }
   625 }
   614 
   626 
   615 /*!
   627 /*!
   616  * Private slot
   628  * Private slot
   617  * Timer timeout slot to indicate that the dynamic PS check has ended
   629  * Timer timeout slot to indicate that the dynamic PS check has ended
   624     {
   636     {
   625         d->mCurrentStation->setPsType( RadioStation::Static );
   637         d->mCurrentStation->setPsType( RadioStation::Static );
   626         d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() );
   638         d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() );
   627         d->mCurrentStation->setDynamicPsText( "" );
   639         d->mCurrentStation->setDynamicPsText( "" );
   628         saveStation( *d->mCurrentStation );
   640         saveStation( *d->mCurrentStation );
   629         d->mUiEngine.api().monitor().notifyName( d->mCurrentStation->name() );
       
   630     }
   641     }
   631 }
   642 }
   632 
   643 
   633 /*!
   644 /*!
   634  * Checks the given station and emits signals based on what member variables had been changed
   645  * Checks the given station and emits signals based on what member variables had been changed