radioapp/radiouiengine/src/radiostationmodel.cpp
changeset 19 afea38384506
parent 16 f54ebcfc1b80
child 37 451b2e1545b2
--- a/radioapp/radiouiengine/src/radiostationmodel.cpp	Mon May 03 12:31:41 2010 +0300
+++ b/radioapp/radiouiengine/src/radiostationmodel.cpp	Fri May 14 15:52:32 2010 +0300
@@ -26,11 +26,6 @@
 #include "radiouiengine_p.h"
 #include "radiostation.h"
 #include "radiostation_p.h"
-#ifndef BUILD_WIN32
-#   include "radiomonitorservice.h"
-#else
-#   include "radiomonitorservice_win32.h"
-#endif
 #include "radiologger.h"
 
 /*!
@@ -185,12 +180,11 @@
 
         RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() );
         if ( d->mPresetStorage->readPreset( index, *preset ) ) {
-            RADIO_ASSERT( station.isValid(), "RadioStationModelPrivate::initialize", "Invalid station" );
-			// TODO: Remove this if when new Preset utility is taken into use
-            if ( station.frequency() != 87500000 )
-                {
+            if ( station.isValid() ) {
                 d->mStations.insert( station.frequency(), station );
-                }
+            } else {
+                LOG( "RadioStationModelPrivate::initialize: Invalid station!" );
+            }
         }
 
 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
@@ -260,21 +254,6 @@
 }
 
 /*!
- * Finds number of favorite stations
- */
-int RadioStationModel::favoriteCount()
-{
-    Q_D( const RadioStationModel );
-    int count = 0;
-    foreach( const RadioStation& tempStation, d->mStations ) {
-        if ( tempStation.isFavorite() ) {
-            ++count;
-        }
-    }
-    return count;
-}
-
-/*!
  * Finds a station by preset index
  */
 int RadioStationModel::findPresetIndex( int presetIndex )
@@ -305,6 +284,42 @@
 }
 
 /*!
+ * Finds the closest station from the given frequency
+ */
+RadioStation RadioStationModel::findClosest( const uint frequency, StationSkip::Mode mode )
+{
+    Q_D( RadioStationModel );
+    const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite;
+    const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite;
+    QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values();
+
+    // Find the previous and next station from current frequency
+    RadioStation previous;
+    RadioStation next;
+    foreach( const RadioStation& station, list ) {
+        const uint testFreq = station.frequency();
+        if ( testFreq == frequency ) {
+            continue;
+        }
+
+        if ( testFreq > frequency ) {
+            next = station;
+            break;
+        }
+        previous = station;
+    }
+
+    // Check if we need to loop around
+    if ( findNext && !next.isValid() ) {
+        next = list.first();
+    } else if ( !findNext && !previous.isValid() ) {
+        previous = list.last();
+    }
+
+    return findNext ? next : previous;
+}
+
+/*!
  * Removes a station by frequency
  */
 void RadioStationModel::removeByFrequency( uint frequency )
@@ -352,17 +367,55 @@
         d->mPresetStorage->deletePreset( tempStation.presetIndex() );
         d->mStations.remove( frequency );
 
-        endRemoveRows();
-
         d->mCurrentStation = NULL;
         d->setCurrentStation( d->mWrapper->currentFrequency() );
 
-        emit stationRemoved( tempStation );
+        endRemoveRows();
+    }
+}
+
+/*!
+ * Public slot
+ * Removes all stations
+ */
+void RadioStationModel::removeAll( RemoveMode mode )
+{
+    Q_D( RadioStationModel );
+    if ( d->mStations.count() == 0 ) {
+        return;
+    }
+
+    if ( mode == RemoveAll ) {
+        beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
 
-        if ( tempStation.isFavorite() ) {
-            d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
+        // Preset utility deletes all presets with index -1
+        bool success = d->mPresetStorage->deletePreset( -1 );
+        Q_UNUSED( success );
+        RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
+
+        d->mStations.clear();
+        d->mCurrentStation = NULL;
+        d->setCurrentStation( d->mWrapper->currentFrequency() );
+
+        endRemoveRows();
+    } else {
+        foreach( const RadioStation& station, d->mStations ) {
+
+            if ( mode == RemoveLocalStations ) {
+                if ( station.isType( RadioStation::LocalStation ) && !station.isFavorite() ) {
+                    removeStation( station );
+                }
+            } else {
+                if ( station.isFavorite() ) {
+                    RadioStation newStation( station );
+                    newStation.setFavorite( false );
+                    saveStation( newStation );
+                }
+            }
         }
     }
+
+    reset(); // TODO: Remove. this is a workaround to HbGridView update problem
 }
 
 /*!
@@ -384,7 +437,6 @@
     const int count = rowCount();
     if ( count > 1 ) {
         Stations::const_iterator iter = d->mStations.upperBound( newStation.frequency() );
-        uint iterFreq = iter.key();
         if ( d->mStations.contains( iter.key() ) ) {
             row = d->mStations.keys().indexOf( iter.key() );
         } else {
@@ -399,8 +451,6 @@
 
 //    emit layoutAboutToBeChanged();
     beginInsertRows( QModelIndex(), row, row );
-    // We must add the station here because saveStation() will only update an existing station
-//    d->mStations.insert( newStation.frequency(), newStation );
 
     d->doSaveStation( newStation );
 
@@ -409,7 +459,6 @@
     endInsertRows();
 
 //    emit layoutChanged();
-    emit stationAdded( station );
 }
 
 /*!
@@ -429,12 +478,22 @@
     } else if ( station.isValid() && stationHasChanged && d->mStations.contains( station.frequency() )) {
 
         d->doSaveStation( station, changeFlags.testFlag( RadioStation::PersistentDataChanged ) );
+        d->setCurrentStation( d->mWrapper->currentFrequency() );
 
         emitChangeSignals( station, changeFlags );
     }
 }
 
 /*!
+ * Finds number of favorite stations
+ */
+int RadioStationModel::favoriteCount()
+{
+    Q_D( const RadioStationModel );
+    return d->favorites().count();
+}
+
+/*!
  * Changes the favorite status of a station by its frequency. If the station does
  * not yet exist, it is added.
  */
@@ -468,11 +527,7 @@
             // Emit the signals only after adding the preset and reinitializing the current station
             // because the UI will probably query the current station in its slots that get called.
             addStation( newStation );
-            d->setCurrentStation( frequency );
         }
-
-        Q_D( RadioStationModel );
-        d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
     }
 }
 
@@ -486,9 +541,6 @@
     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
         station.setFavorite( favorite );
         saveStation( station );
-
-        Q_D( RadioStationModel );
-        d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
     }
 }
 
@@ -502,8 +554,6 @@
     if ( findPresetIndex( presetIndex, station ) != RadioStation::NotFound ) {
         station.setUserDefinedName( name );
         saveStation( station );
-        Q_D( RadioStationModel );
-        d->mUiEngine.api().monitor().notifyName( name );
     }
 }
 
@@ -517,9 +567,6 @@
         RADIO_ASSERT( station.isValid() , "RadioStationModel::setFavorites", "invalid RadioStation");
         setFavoriteByPreset( station.presetIndex(), true );
     }
-
-    Q_D( RadioStationModel );
-    d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
 }
 
 /*!
@@ -578,41 +625,6 @@
 }
 
 /*!
- * Public slot
- * Removes all stations
- */
-void RadioStationModel::removeAll()
-{
-    Q_D( RadioStationModel );
-	
-    if ( d->mStations.count() == 0 ) {
-        return;
-    }
-
-    QList<RadioStation> tempStations = d->mStations.values();
-
-    beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
-
-    // Preset utility deletes all presets with index -1
-    bool success = d->mPresetStorage->deletePreset( -1 );
-    RADIO_ASSERT( success, "FMRadio", "Failed to remove station" );
-
-    d->mStations.clear();
-    d->mCurrentStation = NULL;
-    d->setCurrentStation( d->mWrapper->currentFrequency() );
-
-    endRemoveRows();
-
-    foreach( RadioStation station, tempStations ) {
-        emit stationRemoved( station );
-    }
-
-    reset(); // TODO: Remove. this is a workaround to HbGridView update problem
-
-    d->mUiEngine.api().monitor().notifyFavoriteCount( favoriteCount() );
-}
-
-/*!
  * Private slot
  * Timer timeout slot to indicate that the dynamic PS check has ended
  */
@@ -626,7 +638,6 @@
         d->mCurrentStation->setName( d->mCurrentStation->dynamicPsText() );
         d->mCurrentStation->setDynamicPsText( "" );
         saveStation( *d->mCurrentStation );
-        d->mUiEngine.api().monitor().notifyName( d->mCurrentStation->name() );
     }
 }