radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 24 6df133bd92e1
child 28 075425b8d9a4
equal deleted inserted replaced
23:a2b50a479edf 24:6df133bd92e1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <QStringList>
       
    20 
       
    21 #include "radiostationmodel.h"
       
    22 #include "radiostationmodel_p.h"
       
    23 #include "radiopresetstorage.h"
       
    24 #include "radioenginewrapper.h"
       
    25 #include "radiouiengine.h"
       
    26 #include "radiouiengine_p.h"
       
    27 #include "radiostation.h"
       
    28 #include "radiostation_p.h"
       
    29 #include "radiologger.h"
       
    30 
       
    31 /*!
       
    32  *
       
    33  */
       
    34 static QString parseLine( const RadioStation& station )
       
    35 {
       
    36     QString line = "";
       
    37     const QString parsedFrequency = qtTrId( "txt_rad_dblist_l1_mhz" ).arg( RadioStation::parseFrequency( station.frequency() ) );
       
    38     line.append( parsedFrequency );
       
    39 
       
    40     QString name = station.name();
       
    41     if ( !name.isEmpty() )
       
    42     {
       
    43         line.append( " - " );
       
    44         line.append( name.trimmed() );
       
    45     }
       
    46 
       
    47     LOG_FORMAT( "RadioStationModel: Returning line %s", GETSTRING(line) );
       
    48     return line;
       
    49 }
       
    50 
       
    51 /*!
       
    52  *
       
    53  */
       
    54 RadioStationModel::RadioStationModel( RadioUiEnginePrivate& uiEngine ) :
       
    55     QAbstractListModel( &uiEngine.api() ),
       
    56     d_ptr( new RadioStationModelPrivate( this, uiEngine ) )
       
    57 {
       
    58 }
       
    59 
       
    60 /*!
       
    61  *
       
    62  */
       
    63 RadioStationModel::~RadioStationModel()
       
    64 {
       
    65     delete d_ptr;
       
    66 }
       
    67 
       
    68 /*!
       
    69  *
       
    70  */
       
    71 Qt::ItemFlags RadioStationModel::flags ( const QModelIndex& index ) const
       
    72 {
       
    73     Qt::ItemFlags flags = QAbstractListModel::flags( index );
       
    74     flags |= Qt::ItemIsEditable;
       
    75     return flags;
       
    76 }
       
    77 
       
    78 /*!
       
    79  *
       
    80  */
       
    81 int RadioStationModel::rowCount( const QModelIndex& parent ) const
       
    82 {
       
    83     Q_UNUSED( parent );
       
    84     Q_D( const RadioStationModel );
       
    85     const int count = d->mStations.keys().count();
       
    86     return count;
       
    87 }
       
    88 
       
    89 /*!
       
    90  * Checks the given station and emits signals based on what member variables had been changed
       
    91  */
       
    92 QVariant RadioStationModel::data( const QModelIndex& index, int role ) const
       
    93 {
       
    94     if ( !index.isValid() ) {
       
    95         return QVariant();
       
    96     }
       
    97 
       
    98     Q_D( const RadioStationModel );
       
    99     if ( role == Qt::DisplayRole ) {
       
   100         RadioStation station = stationAt( index.row() );
       
   101         QString firstLine = parseLine( station );
       
   102         if ( d->mDetailLevel.testFlag( RadioStationModel::ShowGenre ) ) {
       
   103             QStringList list;
       
   104             list.append( firstLine );
       
   105             QString genre = " "; // Empty space so that the listbox generates the second row
       
   106             if ( station.genre() != -1 ) {
       
   107                 genre = d->mUiEngine.api().genreToString( station.genre(), GenreTarget::StationsList );
       
   108             }
       
   109             list.append( genre );
       
   110 
       
   111             return list;
       
   112         }
       
   113 
       
   114         return firstLine;
       
   115     } else if ( role == RadioStationModel::RadioStationRole ) {
       
   116         QVariant variant;
       
   117         variant.setValue( stationAt( index.row() ) );
       
   118         return variant;
       
   119     } else if ( role == Qt::DecorationRole &&
       
   120                 d->mDetailLevel.testFlag( RadioStationModel::ShowIcons ) ) {
       
   121         RadioStation station = stationAt( index.row() );
       
   122         QVariantList list;
       
   123         if ( station.isFavorite() && !d->mFavoriteIcon.isNull() ) {
       
   124             list.append( d->mFavoriteIcon );
       
   125         } else {
       
   126             list.append( QIcon() );
       
   127         }
       
   128         if ( currentStation().frequency() == station.frequency() && !d->mNowPlayingIcon.isNull() ) {
       
   129             list.append( d->mNowPlayingIcon );
       
   130         }
       
   131         return list;
       
   132     }
       
   133 
       
   134     return QVariant();
       
   135 }
       
   136 
       
   137 /*!
       
   138  * Checks the given station and emits signals based on what member variables had been changed
       
   139  */
       
   140 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role )
       
   141 {
       
   142     Q_UNUSED( index );
       
   143 
       
   144     if ( role == RadioStationModel::ToggleFavoriteRole ) {
       
   145         const uint frequency = value.toUInt();
       
   146         RadioStation station;
       
   147         if ( findFrequency( frequency, station ) ) {
       
   148             setFavoriteByPreset( station.presetIndex(), !station.isFavorite() );
       
   149         } else {
       
   150             setFavoriteByFrequency( frequency, true );
       
   151         }
       
   152 
       
   153         return true;
       
   154     }
       
   155 
       
   156     return false;
       
   157 }
       
   158 
       
   159 /*!
       
   160  * Called by the engine to initialize the list with given amount of presets
       
   161  */
       
   162 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper )
       
   163 {
       
   164     Q_D( RadioStationModel );
       
   165     d->mPresetStorage = storage;
       
   166     d->mWrapper = wrapper;
       
   167     const int presetCount = d->mPresetStorage->presetCount();
       
   168     int index = d->mPresetStorage->firstPreset();
       
   169     LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", presetCount, index );
       
   170 
       
   171 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   172     while ( index >= 0 ) {
       
   173 #else
       
   174     index = 0;
       
   175     while ( index < presetCount ) {
       
   176 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   177 
       
   178         RadioStation station;
       
   179         station.detach();
       
   180 
       
   181         RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() );
       
   182         if ( d->mPresetStorage->readPreset( index, *preset ) ) {
       
   183             if ( station.isValid() ) {
       
   184                 d->mStations.insert( station.frequency(), station );
       
   185             } else {
       
   186                 LOG( "RadioStationModelPrivate::initialize: Invalid station!" );
       
   187             }
       
   188         }
       
   189 
       
   190 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   191         index = d->mPresetStorage->nextPreset( index );
       
   192 #endif
       
   193     }
       
   194 
       
   195     d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   196 
       
   197     wrapper->addObserver( d );
       
   198 }
       
   199 
       
   200 /*!
       
   201  * Sets the icons to be used in the lists
       
   202  */
       
   203 void RadioStationModel::setIcons( const QIcon& favoriteIcon, const QIcon& nowPlayingIcon )
       
   204 {
       
   205     Q_D( RadioStationModel );
       
   206     d->mFavoriteIcon = favoriteIcon;
       
   207     d->mNowPlayingIcon = nowPlayingIcon;
       
   208 }
       
   209 
       
   210 /*!
       
   211  * Returns a reference to the station handler interface
       
   212  */
       
   213 RadioStationHandlerIf& RadioStationModel::stationHandlerIf()
       
   214 {
       
   215     Q_D( RadioStationModel );
       
   216     return *d;
       
   217 }
       
   218 
       
   219 /*!
       
   220  * Returns a reference to the underlying QList so that it can be easily looped
       
   221  */
       
   222 const Stations& RadioStationModel::list() const
       
   223 {
       
   224     Q_D( const RadioStationModel );
       
   225     return d->mStations;
       
   226 }
       
   227 
       
   228 /*!
       
   229  * Returns the station at the given index.
       
   230  */
       
   231 RadioStation RadioStationModel::stationAt( int index ) const
       
   232 {
       
   233     // 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
       
   235     Q_D( const RadioStationModel );
       
   236     if ( index < d->mStations.keys().count() ) {
       
   237         uint frequency = d->mStations.keys().at( index );
       
   238         return d->mStations.value( frequency );
       
   239     }
       
   240     return RadioStation();
       
   241 }
       
   242 
       
   243 /*!
       
   244  * Finds a station by frequency
       
   245  */
       
   246 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station )
       
   247 {
       
   248     Q_D( RadioStationModel );
       
   249     if ( d->mStations.contains( frequency ) ) {
       
   250         station = d->mStations.value( frequency );
       
   251         return true;
       
   252     }
       
   253     return false;
       
   254 }
       
   255 
       
   256 /*!
       
   257  * Finds a station by preset index
       
   258  */
       
   259 int RadioStationModel::findPresetIndex( int presetIndex )
       
   260 {
       
   261     Q_D( RadioStationModel );
       
   262     int index = 0;
       
   263     foreach( const RadioStation& tempStation, d->mStations ) {
       
   264         if ( tempStation.presetIndex() == presetIndex ) {
       
   265             return index;
       
   266         }
       
   267         ++index;
       
   268     }
       
   269 
       
   270     return RadioStation::NotFound;
       
   271 }
       
   272 
       
   273 /*!
       
   274  * Finds a station by preset index
       
   275  */
       
   276 int RadioStationModel::findPresetIndex( int presetIndex, RadioStation& station )
       
   277 {
       
   278     Q_D( RadioStationModel );
       
   279     const int index = findPresetIndex( presetIndex );
       
   280     if ( index != RadioStation::NotFound ) {
       
   281         station = d->mStations.values().at( index );
       
   282     }
       
   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;
       
   320 }
       
   321 
       
   322 /*!
       
   323  * Removes a station by frequency
       
   324  */
       
   325 void RadioStationModel::removeByFrequency( uint frequency )
       
   326 {
       
   327     RadioStation station;
       
   328     if ( findFrequency( frequency, station ) ) {
       
   329         removeStation( station );
       
   330     }
       
   331 }
       
   332 
       
   333 /*!
       
   334  * Removes a station by preset index
       
   335  */
       
   336 void RadioStationModel::removeByPresetIndex( int presetIndex )
       
   337 {
       
   338     RadioStation station;
       
   339     const int index = findPresetIndex( presetIndex, station );
       
   340     if ( index >= 0 ) {
       
   341         removeStation( station );
       
   342     }
       
   343 }
       
   344 
       
   345 /*!
       
   346  * Removes the given station
       
   347  */
       
   348 void RadioStationModel::removeStation( const RadioStation& station )
       
   349 {
       
   350     Q_D( RadioStationModel );
       
   351     const uint frequency = station.frequency();
       
   352     if ( d->mStations.contains( frequency ) ) {
       
   353 
       
   354         // If we are removing the current station, copy its data to the current station pointer
       
   355         // to keep all of the received RDS data still available. They will be discarded when
       
   356         // the user tunes to another frequency, but they are available if the user decides to add it back.
       
   357         if ( d->mCurrentStation->frequency() == frequency ) {
       
   358             *d->mCurrentStation = station;
       
   359         }
       
   360 
       
   361         // Copy the station to a temporary variable that can be used as signal parameter
       
   362         RadioStation tempStation = station;
       
   363 
       
   364         const int row = modelIndexFromFrequency( tempStation.frequency() ).row();
       
   365         beginRemoveRows( QModelIndex(), row, row );
       
   366 
       
   367         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
       
   368         d->mStations.remove( frequency );
       
   369 
       
   370         d->mCurrentStation = NULL;
       
   371         d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   372 
       
   373         endRemoveRows();
       
   374     }
       
   375 }
       
   376 
       
   377 /*!
       
   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
       
   419 }
       
   420 
       
   421 /*!
       
   422  * Adds a new station to the list
       
   423  */
       
   424 void RadioStationModel::addStation( const RadioStation& station )
       
   425 {
       
   426     Q_D( RadioStationModel );
       
   427     const int newIndex = findUnusedPresetIndex();
       
   428     LOG_FORMAT( "RadioStationModelPrivate::addStation: Adding station to index %d", newIndex );
       
   429 
       
   430     RadioStation newStation = station;
       
   431     newStation.setPresetIndex( newIndex );
       
   432     newStation.unsetType( RadioStation::Temporary );
       
   433 
       
   434     // We have to call beginInsertRows() BEFORE the addition is actually done so we must figure out where
       
   435     // the new station will go in the sorted frequency order
       
   436     int row = 0;
       
   437     const int count = rowCount();
       
   438     if ( count > 1 ) {
       
   439         Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() );
       
   440         if ( d->mStations.contains( iter.key() ) ) {
       
   441             row = d->mStations.keys().indexOf( iter.key() );
       
   442         } else {
       
   443             row = count;
       
   444         }
       
   445     } else if ( count == 1 ) {
       
   446         uint existingFreq = d->mStations.keys().first();
       
   447         if ( station.frequency() > existingFreq ) {
       
   448             row = 1;
       
   449         }
       
   450     }
       
   451 
       
   452 //    emit layoutAboutToBeChanged();
       
   453     beginInsertRows( QModelIndex(), row, row );
       
   454 
       
   455     d->doSaveStation( newStation );
       
   456 
       
   457     d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   458 
       
   459     endInsertRows();
       
   460 
       
   461 //    emit layoutChanged();
       
   462 }
       
   463 
       
   464 /*!
       
   465  * Saves the given station. It is expected to already exist in the list
       
   466  */
       
   467 void RadioStationModel::saveStation( RadioStation& station )
       
   468 {
       
   469     Q_D( RadioStationModel );
       
   470     const bool stationHasChanged = station.hasChanged();
       
   471     RadioStation::Change changeFlags = station.changeFlags();
       
   472     station.resetChangeFlags();
       
   473 
       
   474     if ( station.isType( RadioStation::Temporary ) ) {
       
   475 
       
   476         emitChangeSignals( station, changeFlags );
       
   477 
       
   478     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
       
   479 
       
   480         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
       
   481         d->setCurrentStation( d->mWrapper->currentFrequency() );
       
   482 
       
   483         emitChangeSignals( station, changeFlags );
       
   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();
       
   494 }
       
   495 
       
   496 /*!
       
   497  * Changes the favorite status of a station by its frequency. If the station does
       
   498  * not yet exist, it is added.
       
   499  */
       
   500 void RadioStationModel::setFavoriteByFrequency( uint frequency, bool favorite )
       
   501 {
       
   502     Q_D( RadioStationModel );
       
   503     if ( d->mWrapper->isFrequencyValid( frequency ) ) {
       
   504         LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByFrequency, frequency: %d", frequency );
       
   505         RadioStation station;
       
   506         if ( findFrequency( frequency, station ) ) { // Update existing preset
       
   507             if ( station.isFavorite() != favorite ) {
       
   508                 station.setFavorite( favorite );
       
   509                 saveStation( station );
       
   510             }
       
   511         } else if ( favorite ) {                    // Add new preset if setting as favorite
       
   512             RadioStation newStation;
       
   513             if ( d->mCurrentStation->frequency() == frequency ) {
       
   514                 newStation = *d->mCurrentStation;
       
   515             } else {
       
   516                 LOG( "CurrentStation frequency mismatch!" );
       
   517                 newStation.setFrequency( frequency );
       
   518             }
       
   519 
       
   520             newStation.setType( RadioStation::LocalStation | RadioStation::Favorite );
       
   521 
       
   522             // If PI code has been received, it is a local station
       
   523             if ( newStation.hasPiCode() ) {
       
   524                 newStation.setType( RadioStation::LocalStation );
       
   525             }
       
   526 
       
   527             // Emit the signals only after adding the preset and reinitializing the current station
       
   528             // because the UI will probably query the current station in its slots that get called.
       
   529             addStation( newStation );
       
   530         }
       
   531     }
       
   532 }
       
   533 
       
   534 /*!
       
   535  * Changes the favorite status of a station by its preset index
       
   536  */
       
   537 void RadioStationModel::setFavoriteByPreset( int presetIndex, bool favorite )
       
   538 {
       
   539     LOG_FORMAT( "RadioStationModelPrivate::setFavoriteByPreset, presetIndex: %d", presetIndex );
       
   540     RadioStation station;
       
   541     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
       
   542         station.setFavorite( favorite );
       
   543         saveStation( station );
       
   544     }
       
   545 }
       
   546 
       
   547 /*!
       
   548  * Renames a station by its preset index
       
   549  */
       
   550 void RadioStationModel::renameStation( int presetIndex, const QString& name )
       
   551 {
       
   552     LOG_FORMAT( "RadioStationModelPrivate::renameStation, presetIndex: %d, name: %s", presetIndex, GETSTRING(name) );
       
   553     RadioStation station;
       
   554     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
       
   555         station.setUserDefinedName( name );
       
   556         saveStation( station );
       
   557     }
       
   558 }
       
   559 
       
   560 /*!
       
   561  *
       
   562  */
       
   563 void RadioStationModel::setFavorites( const QModelIndexList& favorites )
       
   564 {
       
   565     foreach ( const QModelIndex& index, favorites ) {
       
   566         RadioStation station = stationAt( index.row() );
       
   567         RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation");
       
   568         setFavoriteByPreset( station.presetIndex(), true );
       
   569     }
       
   570 }
       
   571 
       
   572 /*!
       
   573  * Returns the currently tuned station
       
   574  */
       
   575 RadioStation& RadioStationModel::currentStation()
       
   576 {
       
   577     Q_D( RadioStationModel );
       
   578     return *d->mCurrentStation;
       
   579 }
       
   580 
       
   581 /*!
       
   582  * Returns the currently tuned station
       
   583  */
       
   584 const RadioStation& RadioStationModel::currentStation() const
       
   585 {
       
   586     Q_D( const RadioStationModel );
       
   587     return *d->mCurrentStation;
       
   588 }
       
   589 
       
   590 /*!
       
   591  * Sets the model detail level
       
   592  */
       
   593 void RadioStationModel::setDetail( Detail level )
       
   594 {
       
   595     Q_D( RadioStationModel );
       
   596     d->mDetailLevel = level;
       
   597 }
       
   598 
       
   599 /*!
       
   600  * Returns a list of radio stations in the given frequency range
       
   601  */
       
   602 QList<RadioStation> RadioStationModel::stationsInRange( uint minFrequency, uint maxFrequency )
       
   603 {
       
   604     Q_D( RadioStationModel );
       
   605     QList<RadioStation> stations;
       
   606     foreach( const RadioStation& station, d->mStations ) {
       
   607         if ( station.frequency() >= minFrequency && station.frequency() <= maxFrequency ) {
       
   608             stations.append( station );
       
   609         }
       
   610     }
       
   611 
       
   612     return stations;
       
   613 }
       
   614 
       
   615 /*!
       
   616  * Returns the model index corresponding to the given frequency
       
   617  */
       
   618 QModelIndex RadioStationModel::modelIndexFromFrequency( uint frequency )
       
   619 {
       
   620     RadioStation station;
       
   621     if ( findFrequency( frequency, station ) ) {
       
   622         return index( findPresetIndex( station.presetIndex() ), 0 );
       
   623     }
       
   624     return QModelIndex();
       
   625 }
       
   626 
       
   627 /*!
       
   628  * Private slot
       
   629  * Timer timeout slot to indicate that the dynamic PS check has ended
       
   630  */
       
   631 void RadioStationModel::dynamicPsCheckEnded()
       
   632 {
       
   633     Q_D( RadioStationModel );
       
   634     LOG_TIMESTAMP( "Finished dynamic PS check." );
       
   635     if ( d->mCurrentStation->psType() != RadioStation::Dynamic && !d->mCurrentStation->dynamicPsText().isEmpty() )
       
   636     {
       
   637         d->mCurrentStation->setPsType( RadioStation::Static );
       
   638         d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() );
       
   639         d->mCurrentStation->setDynamicPsText( "" );
       
   640         saveStation( *d->mCurrentStation );
       
   641     }
       
   642 }
       
   643 
       
   644 /*!
       
   645  * Checks the given station and emits signals based on what member variables had been changed
       
   646  */
       
   647 void RadioStationModel::emitChangeSignals( const RadioStation& station, RadioStation::Change flags )
       
   648 {
       
   649     if ( flags.testFlag( RadioStation::NameChanged ) ||
       
   650          flags.testFlag( RadioStation::GenreChanged ) ||
       
   651          flags.testFlag( RadioStation::UrlChanged ) ||
       
   652          flags.testFlag( RadioStation::TypeChanged ) ||
       
   653          flags.testFlag( RadioStation::PiCodeChanged ) ) {
       
   654 
       
   655         // Create a temporary RadioStation for the duration of the signal-slot processing
       
   656         // The receivers can ask the station what data has changed and update accordingly
       
   657         RadioStation tempStation( station );
       
   658         tempStation.setChangeFlags( flags );
       
   659         emit stationDataChanged( tempStation );
       
   660 
       
   661         emitDataChanged( tempStation );
       
   662     }
       
   663 
       
   664     if ( flags.testFlag( RadioStation::RadioTextChanged ) ) {
       
   665         emit radioTextReceived( station );
       
   666         emitDataChanged( station );
       
   667     }
       
   668 
       
   669     if ( flags.testFlag( RadioStation::DynamicPsChanged ) ) {
       
   670         emit dynamicPsChanged( station );
       
   671         emitDataChanged( station );
       
   672     }
       
   673 
       
   674     if ( flags.testFlag( RadioStation::FavoriteChanged ) && station.isValid() ) {
       
   675         emit favoriteChanged( station );
       
   676         emitDataChanged( station );
       
   677     }
       
   678 }
       
   679 
       
   680 /*!
       
   681  *
       
   682  */
       
   683 void RadioStationModel::emitDataChanged( const RadioStation& station )
       
   684 {
       
   685     const int row = findPresetIndex( station.presetIndex() );
       
   686     QModelIndex top = index( row, 0, QModelIndex() );
       
   687     QModelIndex bottom = index( row, 0, QModelIndex() );
       
   688     emit dataChanged( top, bottom );
       
   689 }
       
   690 
       
   691 /*!
       
   692  * Finds an unused preset index
       
   693  */
       
   694 int RadioStationModel::findUnusedPresetIndex()
       
   695 {
       
   696     Q_D( RadioStationModel );
       
   697     QList<int> indexes;
       
   698     foreach( const RadioStation& station, d->mStations ) {
       
   699         if ( station.isValid() ) {
       
   700             indexes.append( station.presetIndex() );
       
   701         }
       
   702     }
       
   703 
       
   704     int index = 0;
       
   705     for ( ; indexes.contains( index ); ++index ) {
       
   706         // Nothing to do here
       
   707     }
       
   708 
       
   709     LOG_FORMAT( "RadioStationModelPrivate::findUnusedPresetIndex, index: %d", index );
       
   710     return index;
       
   711 }
       
   712 
       
   713 /*!
       
   714  * Used by the RDS data setters to find the correct station where the data is set
       
   715  */
       
   716 RadioStation RadioStationModel::findCurrentStation( uint frequency )
       
   717 {
       
   718     Q_D( RadioStationModel );
       
   719     RadioStation station = *d->mCurrentStation;
       
   720     if ( station.frequency() != frequency ) {
       
   721         if ( !findFrequency( frequency, station ) ) {
       
   722             return RadioStation();
       
   723         }
       
   724     }
       
   725     return station;
       
   726 }