radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 16 f54ebcfc1b80
parent 14 63aabac4416d
child 19 afea38384506
equal deleted inserted replaced
14:63aabac4416d 16:f54ebcfc1b80
    21 #include "radiostationmodel.h"
    21 #include "radiostationmodel.h"
    22 #include "radiostationmodel_p.h"
    22 #include "radiostationmodel_p.h"
    23 #include "radiopresetstorage.h"
    23 #include "radiopresetstorage.h"
    24 #include "radioenginewrapper.h"
    24 #include "radioenginewrapper.h"
    25 #include "radiouiengine.h"
    25 #include "radiouiengine.h"
       
    26 #include "radiouiengine_p.h"
    26 #include "radiostation.h"
    27 #include "radiostation.h"
    27 #include "radiostation_p.h"
    28 #include "radiostation_p.h"
    28 #ifndef BUILD_WIN32
    29 #ifndef BUILD_WIN32
    29 #   include "radiomonitorservice.h"
    30 #   include "radiomonitorservice.h"
    30 #else
    31 #else
    36  *
    37  *
    37  */
    38  */
    38 static QString parseLine( const RadioStation& station )
    39 static QString parseLine( const RadioStation& station )
    39 {
    40 {
    40     QString line = "";
    41     QString line = "";
    41     line.append( RadioUiEngine::parseFrequency( station.frequency() ) );
    42     const QString parsedFrequency = qtTrId( "txt_rad_dblist_l1_mhz" ).arg( RadioStation::parseFrequency( station.frequency() ) );
       
    43     line.append( parsedFrequency );
    42 
    44 
    43     QString name = station.name();
    45     QString name = station.name();
    44     if ( !name.isEmpty() )
    46     if ( !name.isEmpty() )
    45     {
    47     {
    46         line.append( " - " );
    48         line.append( " - " );
    52 }
    54 }
    53 
    55 
    54 /*!
    56 /*!
    55  *
    57  *
    56  */
    58  */
    57 RadioStationModel::RadioStationModel( RadioUiEngine& uiEngine ) :
    59 RadioStationModel::RadioStationModel( RadioUiEnginePrivate& uiEngine ) :
    58     QAbstractListModel( &uiEngine ),
    60     QAbstractListModel( &uiEngine.api() ),
    59     d_ptr( new RadioStationModelPrivate( this, uiEngine ) )
    61     d_ptr( new RadioStationModelPrivate( this, uiEngine ) )
    60 {
    62 {
    61 }
    63 }
    62 
    64 
    63 /*!
    65 /*!
    64  *
    66  *
    65  */
    67  */
    66 RadioStationModel::~RadioStationModel()
    68 RadioStationModel::~RadioStationModel()
    67 {
    69 {
       
    70     delete d_ptr;
    68 }
    71 }
    69 
    72 
    70 /*!
    73 /*!
    71  *
    74  *
    72  */
    75  */
   104         if ( d->mDetailLevel.testFlag( RadioStationModel::ShowGenre ) ) {
   107         if ( d->mDetailLevel.testFlag( RadioStationModel::ShowGenre ) ) {
   105             QStringList list;
   108             QStringList list;
   106             list.append( firstLine );
   109             list.append( firstLine );
   107             QString genre = " "; // Empty space so that the listbox generates the second row
   110             QString genre = " "; // Empty space so that the listbox generates the second row
   108             if ( station.genre() != -1 ) {
   111             if ( station.genre() != -1 ) {
   109                 genre = d->mUiEngine.genreToString( station.genre() );
   112                 genre = d->mUiEngine.api().genreToString( station.genre(), GenreTarget::StationsList );
   110             }
   113             }
   111             list.append( genre );
   114             list.append( genre );
   112 
   115 
   113             return list;
   116             return list;
   114         }
   117         }
   157 
   160 
   158     return false;
   161     return false;
   159 }
   162 }
   160 
   163 
   161 /*!
   164 /*!
   162  * Called by the wrapper to initialize the list with given amount of presets
   165  * Called by the engine to initialize the list with given amount of presets
   163  */
   166  */
   164 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* engine )
   167 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper )
   165 {
   168 {
   166     Q_D( RadioStationModel );
   169     Q_D( RadioStationModel );
   167     d->mPresetStorage = storage;
   170     d->mPresetStorage = storage;
   168     d->mEngine = engine;
   171     d->mWrapper = wrapper;
   169     const int presetCount = d->mPresetStorage->presetCount();
   172     const int presetCount = d->mPresetStorage->presetCount();
   170     int index = d->mPresetStorage->firstPreset();
   173     int index = d->mPresetStorage->firstPreset();
   171     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", presetCount, index );
   174     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", presetCount, index );
   172 
   175 
   173 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   176 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   187             if ( station.frequency() != 87500000 )
   190             if ( station.frequency() != 87500000 )
   188                 {
   191                 {
   189                 d->mStations.insert( station.frequency(), station );
   192                 d->mStations.insert( station.frequency(), station );
   190                 }
   193                 }
   191         }
   194         }
   192         ++index;
   195 
   193 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   196 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   194         index = d->mPresetStorage->nextPreset( index );
   197         index = d->mPresetStorage->nextPreset( index );
   195 #endif
   198 #endif
   196     }
   199     }
   197 
   200 
   198     d->setCurrentStation( d->mEngine->currentFrequency() );
   201     d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   202 
       
   203     wrapper->addObserver( d );
   199 }
   204 }
   200 
   205 
   201 /*!
   206 /*!
   202  * Sets the icons to be used in the lists
   207  * Sets the icons to be used in the lists
   203  */
   208  */
   253     }
   258     }
   254     return false;
   259     return false;
   255 }
   260 }
   256 
   261 
   257 /*!
   262 /*!
       
   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 /*!
   258  * Finds a station by preset index
   278  * Finds a station by preset index
   259  */
   279  */
   260 int RadioStationModel::findPresetIndex( int presetIndex )
   280 int RadioStationModel::findPresetIndex( int presetIndex )
   261 {
   281 {
   262     Q_D( RadioStationModel );
   282     Q_D( RadioStationModel );
   332         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
   352         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
   333         d->mStations.remove( frequency );
   353         d->mStations.remove( frequency );
   334 
   354 
   335         endRemoveRows();
   355         endRemoveRows();
   336 
   356 
   337         d->setCurrentStation( d->mEngine->currentFrequency() );
   357         d->mCurrentStation = NULL;
       
   358         d->setCurrentStation( d->mWrapper->currentFrequency() );
   338 
   359 
   339         emit stationRemoved( tempStation );
   360         emit stationRemoved( tempStation );
   340 
   361 
       
   362         if ( tempStation.isFavorite() ) {
       
   363             d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
       
   364         }
   341     }
   365     }
   342 }
   366 }
   343 
   367 
   344 /*!
   368 /*!
   345  * Adds a new station to the list
   369  * Adds a new station to the list
   354     newStation.setPresetIndex( newIndex );
   378     newStation.setPresetIndex( newIndex );
   355     newStation.unsetType( RadioStation::Temporary );
   379     newStation.unsetType( RadioStation::Temporary );
   356 
   380 
   357     // We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where
   381     // We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where
   358     // the new station will go in the sorted frequency order
   382     // the new station will go in the sorted frequency order
   359 //    int row = 0;
   383     int row = 0;
   360 //    const int count = rowCount();
   384     const int count = rowCount();
   361 //    if ( count > 1 ) {
   385     if ( count > 1 ) {
   362 //        Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() );
   386         Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() );
   363 //        uint iterFreq = iter.key();
   387         uint iterFreq = iter.key();
   364 //        if ( d->mStations.contains( iter.key() ) ) {
   388         if ( d->mStations.contains( iter.key() ) ) {
   365 //            row = d->mStations.keys().indexOf( iter.key() );
   389             row = d->mStations.keys().indexOf( iter.key() );
   366 //        } else {
   390         } else {
   367 //            row = count;
   391             row = count;
   368 //        }
   392         }
   369 //    } else if ( count == 1 ) {
   393     } else if ( count == 1 ) {
   370 //        uint existingFreq = d->mStations.keys().first();
   394         uint existingFreq = d->mStations.keys().first();
   371 //        if ( station.frequency() > existingFreq ) {
   395         if ( station.frequency() > existingFreq ) {
   372 //            row = 1;
   396             row = 1;
   373 //        }
   397         }
   374 //    }
   398     }
   375 //
   399 
   376     emit layoutAboutToBeChanged();
   400 //    emit layoutAboutToBeChanged();
   377 //    beginInsertRows( QModelIndex(), row, row );
   401     beginInsertRows( QModelIndex(), row, row );
   378     // We must add the station here because saveStation() will only update an existing station
   402     // We must add the station here because saveStation() will only update an existing station
   379     d->mStations.insert( newStation.frequency(), newStation );
   403 //    d->mStations.insert( newStation.frequency(), newStation );
   380 
   404 
   381     saveStation( newStation );
   405     d->doSaveStation( newStation );
   382     d->setCurrentStation( d->mEngine->currentFrequency() );
   406 
   383 
   407     d->setCurrentStation( d->mWrapper->currentFrequency() );
   384 //    endInsertRows();
   408 
   385 
   409     endInsertRows();
   386     emit layoutChanged();
   410 
       
   411 //    emit layoutChanged();
   387     emit stationAdded( station );
   412     emit stationAdded( station );
   388 }
   413 }
   389 
   414 
   390 /*!
   415 /*!
   391  * Saves the given station. It is expected to already exist in the list
   416  * Saves the given station. It is expected to already exist in the list
   401 
   426 
   402         emitChangeSignals( station, changeFlags );
   427         emitChangeSignals( station, changeFlags );
   403 
   428 
   404     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   429     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
   405 
   430 
   406         d->mStations.insert( station.frequency(), station );
   431         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
   407 
       
   408         if ( changeFlags.testFlag( RadioStation::PersistentDataChanged ) ) {
       
   409             const bool success = d->mPresetStorage->savePreset( *station.data_ptr() );
       
   410             RADIO_ASSERT( success, "RadioStationModelPrivate::saveStation", "Failed to add station" );
       
   411         }
       
   412 
   432 
   413         emitChangeSignals( station, changeFlags );
   433         emitChangeSignals( station, changeFlags );
   414     }
   434     }
   415 }
   435 }
   416 
   436 
   419  * not yet exist, it is added.
   439  * not yet exist, it is added.
   420  */
   440  */
   421 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite )
   441 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite )
   422 {
   442 {
   423     Q_D( RadioStationModel );
   443     Q_D( RadioStationModel );
   424     if ( d->mEngine->isFrequencyValid( frequency ) )
   444     if ( d->mWrapper->isFrequencyValid( frequency ) ) {
   425     {
       
   426         LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByFrequency, frequency: %d", frequency );
   445         LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByFrequency, frequency: %d", frequency );
   427         RadioStation station;
   446         RadioStation station;
   428         if ( findFrequency( frequency, station ) )  // Update existing preset
   447         if ( findFrequency( frequency, station ) ) { // Update existing preset
   429         {
   448             if ( station.isFavorite() != favorite ) {
   430             if ( station.isFavorite() != favorite )
       
   431             {
       
   432                 station.setFavorite( favorite );
   449                 station.setFavorite( favorite );
   433                 saveStation( station );
   450                 saveStation( station );
   434             }
   451             }
   435         }
   452         } else if ( favorite ) {                    // Add new preset if setting as favorite
   436         else if ( favorite )                    // Add new preset if setting as favorite
       
   437         {
       
   438             RadioStation newStation;
   453             RadioStation newStation;
   439             if ( d->mCurrentStation->frequency() == frequency ) {
   454             if ( d->mCurrentStation->frequency() == frequency ) {
   440                 newStation = *d->mCurrentStation;
   455                 newStation = *d->mCurrentStation;
   441             } else {
   456             } else {
   442                 LOG( "CurrentStation frequency mismatch!" );
   457                 LOG( "CurrentStation frequency mismatch!" );
   453             // Emit the signals only after adding the preset and reinitializing the current station
   468             // Emit the signals only after adding the preset and reinitializing the current station
   454             // because the UI will probably query the current station in its slots that get called.
   469             // because the UI will probably query the current station in its slots that get called.
   455             addStation( newStation );
   470             addStation( newStation );
   456             d->setCurrentStation( frequency );
   471             d->setCurrentStation( frequency );
   457         }
   472         }
       
   473 
       
   474         Q_D( RadioStationModel );
       
   475         d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
   458     }
   476     }
   459 }
   477 }
   460 
   478 
   461 /*!
   479 /*!
   462  * Changes the favorite status of a station by its preset index
   480  * Changes the favorite status of a station by its preset index
   463  */
   481  */
   464 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite )
   482 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite )
   465 {
   483 {
   466     LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex );
   484     LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex );
   467     RadioStation station;
   485     RadioStation station;
   468     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound )
   486     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   469     {
       
   470         station.setFavorite( favorite );
   487         station.setFavorite( favorite );
   471         saveStation( station );
   488         saveStation( station );
       
   489 
       
   490         Q_D( RadioStationModel );
       
   491         d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
   472     }
   492     }
   473 }
   493 }
   474 
   494 
   475 /*!
   495 /*!
   476  * Renames a station by its preset index
   496  * Renames a station by its preset index
   477  */
   497  */
   478 void RadioStationModel::renameStation( int presetIndex, const QString& name )
   498 void RadioStationModel::renameStation( int presetIndex, const QString& name )
   479 {
   499 {
   480     LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   500     LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
   481     RadioStation station;
   501     RadioStation station;
   482     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound )
   502     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
   483     {
       
   484         station.setUserDefinedName( name );
   503         station.setUserDefinedName( name );
   485         saveStation( station );
   504         saveStation( station );
       
   505         Q_D( RadioStationModel );
       
   506         d->mUiEngine.api().monitor().notifyName( name );
   486     }
   507     }
   487 }
   508 }
   488 
   509 
   489 /*!
   510 /*!
   490  *
   511  *
   494     foreach ( const QModelIndex& index, favorites ) {
   515     foreach ( const QModelIndex& index, favorites ) {
   495         RadioStation station = stationAt( index.row() );
   516         RadioStation station = stationAt( index.row() );
   496         RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation");
   517         RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation");
   497         setFavoriteByPreset( station.presetIndex(), true );
   518         setFavoriteByPreset( station.presetIndex(), true );
   498     }
   519     }
       
   520 
       
   521     Q_D( RadioStationModel );
       
   522     d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
   499 }
   523 }
   500 
   524 
   501 /*!
   525 /*!
   502  * Returns the currently tuned station
   526  * Returns the currently tuned station
   503  */
   527  */
   565         return;
   589         return;
   566     }
   590     }
   567 
   591 
   568     QList<RadioStation> tempStations = d->mStations.values();
   592     QList<RadioStation> tempStations = d->mStations.values();
   569 
   593 
   570 
       
   571     beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
   594     beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
   572 
   595 
   573     // Preset utility deletes all presets with index -1
   596     // Preset utility deletes all presets with index -1
   574     bool success = d->mPresetStorage->deletePreset( -1 );
   597     bool success = d->mPresetStorage->deletePreset( -1 );
   575     RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
   598     RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
   576 
   599 
   577     d->mStations.clear();
   600     d->mStations.clear();
   578     d->setCurrentStation( d->mEngine->currentFrequency() );
   601     d->mCurrentStation = NULL;
       
   602     d->setCurrentStation( d->mWrapper->currentFrequency() );
   579 
   603 
   580     endRemoveRows();
   604     endRemoveRows();
   581 
   605 
   582     foreach( RadioStation station, tempStations ) {
   606     foreach( RadioStation station, tempStations ) {
   583         emit stationRemoved( station );
   607         emit stationRemoved( station );
   584     }
   608     }
       
   609 
       
   610     reset(); // TODO: Remove. this is a workaround to HbGridView update problem
       
   611 
       
   612     d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
   585 }
   613 }
   586 
   614 
   587 /*!
   615 /*!
   588  * Private slot
   616  * Private slot
   589  * Timer timeout slot to indicate that the dynamic PS check has ended
   617  * Timer timeout slot to indicate that the dynamic PS check has ended
   596     {
   624     {
   597         d->mCurrentStation->setPsType( RadioStation::Static );
   625         d->mCurrentStation->setPsType( RadioStation::Static );
   598         d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() );
   626         d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() );
   599         d->mCurrentStation->setDynamicPsText( "" );
   627         d->mCurrentStation->setDynamicPsText( "" );
   600         saveStation( *d->mCurrentStation );
   628         saveStation( *d->mCurrentStation );
   601         d->mUiEngine.monitor().notifyName( d->mCurrentStation->name() );
   629         d->mUiEngine.api().monitor().notifyName( d->mCurrentStation->name() );
   602     }
   630     }
   603 }
   631 }
   604 
   632 
   605 /*!
   633 /*!
   606  * Checks the given station and emits signals based on what member variables had been changed
   634  * Checks the given station and emits signals based on what member variables had been changed