radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 28 075425b8d9a4
parent 24 6df133bd92e1
child 32 189d20c34778
child 34 bc10a61bd7d3
equal deleted inserted replaced
24:6df133bd92e1 28:075425b8d9a4
    16 */
    16 */
    17 
    17 
    18 // System includes
    18 // System includes
    19 #include <QStringList>
    19 #include <QStringList>
    20 
    20 
       
    21 // User includes
    21 #include "radiostationmodel.h"
    22 #include "radiostationmodel.h"
    22 #include "radiostationmodel_p.h"
    23 #include "radiostationmodel_p.h"
    23 #include "radiopresetstorage.h"
    24 #include "radiopresetstorage.h"
    24 #include "radioenginewrapper.h"
    25 #include "radioenginewrapper.h"
    25 #include "radiouiengine.h"
    26 #include "radiouiengine.h"
   110 
   111 
   111             return list;
   112             return list;
   112         }
   113         }
   113 
   114 
   114         return firstLine;
   115         return firstLine;
   115     } else if ( role == RadioStationModel::RadioStationRole ) {
   116     } else if ( role == RadioRole::RadioStationRole ) {
   116         QVariant variant;
   117         QVariant variant;
   117         variant.setValue( stationAt( index.row() ) );
   118         variant.setValue( stationAt( index.row() ) );
   118         return variant;
   119         return variant;
   119     } else if ( role == Qt::DecorationRole &&
   120     } else if ( role == Qt::DecorationRole &&
   120                 d->mDetailLevel.testFlag( RadioStationModel::ShowIcons ) ) {
   121                 d->mDetailLevel.testFlag( RadioStationModel::ShowIcons ) ) {
   127         }
   128         }
   128         if ( currentStation().frequency() == station.frequency() && !d->mNowPlayingIcon.isNull() ) {
   129         if ( currentStation().frequency() == station.frequency() && !d->mNowPlayingIcon.isNull() ) {
   129             list.append( d->mNowPlayingIcon );
   130             list.append( d->mNowPlayingIcon );
   130         }
   131         }
   131         return list;
   132         return list;
       
   133     } else if ( role == RadioRole::IsFavoriteRole ) {
       
   134         QVariant variant;
       
   135         variant.setValue( stationAt( index.row() ).isFavorite() );
       
   136         return variant;
   132     }
   137     }
   133 
   138 
   134     return QVariant();
   139     return QVariant();
   135 }
   140 }
   136 
   141 
   139  */
   144  */
   140 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role )
   145 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role )
   141 {
   146 {
   142     Q_UNUSED( index );
   147     Q_UNUSED( index );
   143 
   148 
   144     if ( role == RadioStationModel::ToggleFavoriteRole ) {
   149     if ( role == RadioRole::ToggleFavoriteRole ) {
   145         const uint frequency = value.toUInt();
   150         const uint frequency = value.toUInt();
   146         RadioStation station;
   151         RadioStation station;
   147         if ( findFrequency( frequency, station ) ) {
   152         if ( findFrequency( frequency, station ) ) {
   148             setFavoriteByPreset( station.presetIndex(), !station.isFavorite() );
   153             setFavoriteByPreset( station.presetIndex(), !station.isFavorite() );
   149         } else {
   154         } else {
   162 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper )
   167 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper )
   163 {
   168 {
   164     Q_D( RadioStationModel );
   169     Q_D( RadioStationModel );
   165     d->mPresetStorage = storage;
   170     d->mPresetStorage = storage;
   166     d->mWrapper = wrapper;
   171     d->mWrapper = wrapper;
   167     const int presetCount = d->mPresetStorage->presetCount();
   172 
   168     int index = d->mPresetStorage->firstPreset();
   173     int index = d->mPresetStorage->firstPreset();
   169     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", presetCount, index );
   174     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d",
   170 
   175                                             d->mPresetStorage->presetCount(), index );
   171 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
   176 
   172     while ( index >= 0 ) {
   177     while ( index >= 0 ) {
   173 #else
       
   174     index = 0;
       
   175     while ( index < presetCount ) {
       
   176 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   177 
       
   178         RadioStation station;
   178         RadioStation station;
   179         station.detach();
   179         station.detach();
   180 
   180 
   181         RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() );
   181         RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() );
   182         if ( d->mPresetStorage->readPreset( index, *preset ) ) {
   182         if ( d->mPresetStorage->readPreset( index, *preset ) ) {
   185             } else {
   185             } else {
   186                 LOG( "RadioStationModelPrivate::initialize: Invalid station!" );
   186                 LOG( "RadioStationModelPrivate::initialize: Invalid station!" );
   187             }
   187             }
   188         }
   188         }
   189 
   189 
   190 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   191         index = d->mPresetStorage->nextPreset( index );
   190         index = d->mPresetStorage->nextPreset( index );
   192 #endif
       
   193     }
   191     }
   194 
   192 
   195     d->setCurrentStation( d->mWrapper->currentFrequency() );
   193     d->setCurrentStation( d->mWrapper->currentFrequency() );
   196 
   194 
   197     wrapper->addObserver( d );
   195     wrapper->addObserver( d );
   231 RadioStation RadioStationModel::stationAt( int index ) const
   229 RadioStation RadioStationModel::stationAt( int index ) const
   232 {
   230 {
   233     // Get the value from the keys list instead of directly accessing the values list
   231     // Get the value from the keys list instead of directly accessing the values list
   234     // because QMap may have added a default-constructed value to the values list
   232     // because QMap may have added a default-constructed value to the values list
   235     Q_D( const RadioStationModel );
   233     Q_D( const RadioStationModel );
   236     if ( index < d->mStations.keys().count() ) {
   234     if ( index >= 0 && index < d->mStations.keys().count() ) {
   237         uint frequency = d->mStations.keys().at( index );
   235         uint frequency = d->mStations.keys().at( index );
   238         return d->mStations.value( frequency );
   236         return d->mStations.value( frequency );
   239     }
   237     }
   240     return RadioStation();
   238     return RadioStation();
   241 }
   239 }
   242 
   240 
   243 /*!
   241 /*!
   244  * Finds a station by frequency
   242  * Finds a station by frequency
   245  */
   243  */
   246 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station )
   244 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station ) const
   247 {
   245 {
   248     Q_D( RadioStationModel );
   246     Q_D( const RadioStationModel );
   249     if ( d->mStations.contains( frequency ) ) {
   247     if ( d->mStations.contains( frequency ) ) {
   250         station = d->mStations.value( frequency );
   248         station = d->mStations.value( frequency );
   251         return true;
   249         return true;
   252     }
   250     }
   253     return false;
   251     return false;
   290 {
   288 {
   291     Q_D( RadioStationModel );
   289     Q_D( RadioStationModel );
   292     const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite;
   290     const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite;
   293     const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite;
   291     const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite;
   294     QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values();
   292     QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values();
       
   293 
       
   294     if ( list.isEmpty() ) {
       
   295         return RadioStation();
       
   296     }
   295 
   297 
   296     // Find the previous and next station from current frequency
   298     // Find the previous and next station from current frequency
   297     RadioStation previous;
   299     RadioStation previous;
   298     RadioStation next;
   300     RadioStation next;
   299     foreach( const RadioStation& station, list ) {
   301     foreach( const RadioStation& station, list ) {
   318 
   320 
   319     return findNext ? next : previous;
   321     return findNext ? next : previous;
   320 }
   322 }
   321 
   323 
   322 /*!
   324 /*!
       
   325  * Checks if the model contains the given frequency
       
   326  */
       
   327 bool RadioStationModel::contains( const uint frequency ) const
       
   328 {
       
   329     RadioStation unused;
       
   330     return findFrequency( frequency, unused );
       
   331 }
       
   332 
       
   333 /*!
   323  * Removes a station by frequency
   334  * Removes a station by frequency
   324  */
   335  */
   325 void RadioStationModel::removeByFrequency( uint frequency )
   336 void RadioStationModel::removeByFrequency( uint frequency )
   326 {
   337 {
   327     RadioStation station;
   338     RadioStation station;
   359         }
   370         }
   360 
   371 
   361         // Copy the station to a temporary variable that can be used as signal parameter
   372         // Copy the station to a temporary variable that can be used as signal parameter
   362         RadioStation tempStation = station;
   373         RadioStation tempStation = station;
   363 
   374 
   364         const int row = modelIndexFromFrequency( tempStation.frequency() ).row();
   375         const int row = indexFromFrequency( tempStation.frequency() );
   365         beginRemoveRows( QModelIndex(), row, row );
   376         beginRemoveRows( QModelIndex(), row, row );
   366 
   377 
   367         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
   378         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
   368         d->mStations.remove( frequency );
   379         d->mStations.remove( frequency );
   369 
   380 
   412                     saveStation( newStation );
   423                     saveStation( newStation );
   413                 }
   424                 }
   414             }
   425             }
   415         }
   426         }
   416     }
   427     }
   417 
       
   418     reset(); // TODO: Remove. this is a workaround to HbGridView update problem
       
   419 }
   428 }
   420 
   429 
   421 /*!
   430 /*!
   422  * Adds a new station to the list
   431  * Adds a new station to the list
   423  */
   432  */
   613 }
   622 }
   614 
   623 
   615 /*!
   624 /*!
   616  * Returns the model index corresponding to the given frequency
   625  * Returns the model index corresponding to the given frequency
   617  */
   626  */
   618 QModelIndex RadioStationModel::modelIndexFromFrequency( uint frequency )
   627 int RadioStationModel::indexFromFrequency( uint frequency )
   619 {
   628 {
   620     RadioStation station;
   629     RadioStation station;
   621     if ( findFrequency( frequency, station ) ) {
   630     if ( findFrequency( frequency, station ) ) {
   622         return index( findPresetIndex( station.presetIndex() ), 0 );
   631         return findPresetIndex( station.presetIndex() );
   623     }
   632     }
   624     return QModelIndex();
   633     return -1;
   625 }
   634 }
   626 
   635 
   627 /*!
   636 /*!
   628  * Private slot
   637  * Private slot
   629  * Timer timeout slot to indicate that the dynamic PS check has ended
   638  * Timer timeout slot to indicate that the dynamic PS check has ended