radioapp/radiowidgets/src/radiofrequencyscanner.cpp
changeset 28 075425b8d9a4
parent 24 6df133bd92e1
child 32 189d20c34778
child 34 bc10a61bd7d3
--- a/radioapp/radiowidgets/src/radiofrequencyscanner.cpp	Fri Jun 04 10:21:36 2010 +0100
+++ b/radioapp/radiowidgets/src/radiofrequencyscanner.cpp	Fri Jun 11 13:38:32 2010 +0300
@@ -20,6 +20,7 @@
 #include <QTimer>
 #include <HbLabel>
 #include <HbPushButton>
+#include <HbMessageBox>
 
 // User includes
 #include "radiofrequencyscanner.h"
@@ -29,11 +30,16 @@
 #include "radiostationmodel.h"
 #include "radiofrequencystrip.h"
 #include "radiostationcarousel.h"
-#include "radiouiutilities.h"
+#include "radioutil.h"
 #include "radiomainview.h"
 
 // Constants
-const int KExtraRoomToMaxValue = 100000;
+const int EXTRA_TO_PROGRESS_MAX_VALUE = 100000;
+const int CAROUSEL_FINISH_SCROLL_TIME = 1000;
+const int STRIP_FINISH_SCROLL_TIME = 1100;
+const int CAROUSEL_SCROLL_TIME = 1000;
+const int STRIP_SCROLL_TIME = 1100;
+const int START_DELAY = 1000;
 
 /*!
  *
@@ -42,12 +48,16 @@
     QObject( parent ),
     mUiEngine( uiEngine ),
     mInMainView( parent->metaObject()->className() == RadioMainView::staticMetaObject.className() ),
-    mScannerEngine( mUiEngine.scannerEngine() ),
+    mScannerEngine( mUiEngine.createScannerEngine() ),
     mStripScrollTime( 0 ),
     mCarouselScrollTime( 0 ),
-    mIsAlive( false )
+    mIsAlive( false ),
+    mUserCanceled( false )
 {
-    RadioUiUtilities::setFrequencyScanner( this );
+    RadioUtil::setFrequencyScanner( this );
+
+    Radio::connect( mScannerEngine.data(),  SIGNAL(stationFound(RadioStation)),
+                    this,                   SLOT(updateScanProgress(RadioStation)) );
 }
 
 /*!
@@ -55,6 +65,7 @@
  */
 RadioFrequencyScanner::~RadioFrequencyScanner()
 {
+    RadioUtil::setScanStatus( Scan::NotScanning );
 }
 
 /*!
@@ -63,43 +74,47 @@
 void RadioFrequencyScanner::startScanning()
 {
     mIsAlive = true;
-    RadioFrequencyStrip* frequencyStrip = RadioUiUtilities::frequencyStrip();
-    RadioStationCarousel* carousel = RadioUiUtilities::carousel();
+    RadioFrequencyStrip* frequencyStrip = RadioUtil::frequencyStrip();
+    RadioStationCarousel* carousel = RadioUtil::carousel();
 
     if ( mInMainView ) {
+
+        // If this is the first time start, bring application to foreground
+        if ( mUiEngine.isFirstTimeStart() ) {
+            static_cast<RadioMainView*>( parent() )->bringToForeground();
+        }
+
+        RadioUtil::setScanStatus( Scan::ScanningInMainView );
         mStripScrollTime = frequencyStrip->autoScrollTime();
         mCarouselScrollTime = carousel->autoScrollTime();
 
         carousel->setScanningMode( true );
-        carousel->setAutoScrollTime( 1000 );
-        frequencyStrip->setAutoScrollTime( 1100 );
+        carousel->setAutoScrollTime( CAROUSEL_SCROLL_TIME );
+        frequencyStrip->setAutoScrollTime( STRIP_SCROLL_TIME );
 
-        connectAndTest( carousel,               SIGNAL(scanAnimationFinished()),
+        Radio::connect( carousel,               SIGNAL(scanAnimationFinished()),
                         this,                   SLOT(continueScanning()) );
 
         static_cast<RadioMainView*>( parent() )->setScanningMode( true );
         frequencyStrip->setScanningMode( true );
     } else {
-        carousel->setCarouselModel( NULL );
-
-        mScanningProgressNote.reset( new HbProgressDialog( HbProgressDialog::ProgressDialog ) ),
+        RadioUtil::setScanStatus( Scan::ScanningInStationsView );
+        mScanningProgressNote = new HbProgressDialog( HbProgressDialog::ProgressDialog );
         mScanningProgressNote->setModal( true );
         mScanningProgressNote->setAutoClose( true );
 
         // Add some extra to the maximum value to allow room for the station at the low band edge
-        mScanningProgressNote->setRange( mUiEngine.minFrequency(), mUiEngine.maxFrequency() + KExtraRoomToMaxValue );
+        mScanningProgressNote->setRange( mUiEngine.minFrequency(), mUiEngine.maxFrequency() + EXTRA_TO_PROGRESS_MAX_VALUE );
         mScanningProgressNote->setProgressValue( mUiEngine.minFrequency() );
         mScanningProgressNote->setText( hbTrId( "txt_rad_info_searching_local_stations_please_wait" ) );
-        mScanningProgressNote->show();
+        mScanningProgressNote->setAttribute( Qt::WA_DeleteOnClose, true );
+        mScanningProgressNote->open();
 
-        connectAndTest( mScanningProgressNote.data(),   SIGNAL(cancelled()),
+        Radio::connect( mScanningProgressNote.data(),   SIGNAL(cancelled()),
                         this,                           SLOT(cancelScanning()) );
     }
 
-    connectAndTest( mScannerEngine.data(),  SIGNAL(stationFound(RadioStation)),
-                    this,                   SLOT(updateScanProgress(RadioStation)) );
-
-    QTimer::singleShot( 1000, this, SLOT(delayedStart()) );
+    QTimer::singleShot( START_DELAY, this, SLOT(delayedStart()) );
 }
 
 /*!
@@ -116,6 +131,8 @@
  */
 void RadioFrequencyScanner::cancelScanning()
 {
+    mUserCanceled = true;
+    RadioUtil::carousel()->cancelAnimation();
     finishScanning();
 }
 
@@ -144,8 +161,8 @@
 
     if ( mInMainView ) {
 
-        RadioUiUtilities::frequencyStrip()->setFrequency( frequency, TuneReason::StationScan );
-        RadioUiUtilities::carousel()->animateNewStation( station );
+        RadioUtil::frequencyStrip()->setFrequency( frequency, TuneReason::StationScan );
+        RadioUtil::carousel()->animateNewStation( station );
 
     } else {
         // Check for special case that can happen during scanning.
@@ -157,7 +174,7 @@
         }
 
         mScannerEngine->continueScanning();
-    }    
+    }
 }
 
 /*!
@@ -166,7 +183,10 @@
  */
 void RadioFrequencyScanner::continueScanning()
 {
-    mScannerEngine->continueScanning();
+    if ( !mUserCanceled ) {
+        RadioUtil::frequencyStrip()->addScannedStation( mUiEngine.stationModel().currentStation() );
+        mScannerEngine->continueScanning();
+    }
 }
 
 /*!
@@ -176,9 +196,11 @@
 void RadioFrequencyScanner::restoreUiControls()
 {
     if ( mInMainView ) {
-        RadioUiUtilities::frequencyStrip()->setScanningMode( false );
+        disconnect( RadioUtil::carousel(), SIGNAL(scrollingEnded()), this, 0 );
+
+        RadioUtil::frequencyStrip()->setScanningMode( false );
         static_cast<RadioMainView*>( parent() )->setScanningMode( false );
-        RadioUiUtilities::carousel()->setScanningMode( false );
+        RadioUtil::carousel()->setScanningMode( false );
     }
 
     deleteLater();
@@ -190,38 +212,77 @@
 void RadioFrequencyScanner::finishScanning()
 {
     mScannerEngine->cancel();
-    RadioUiUtilities::setFrequencyScanner( NULL );
-    mIsAlive = false;
-    RadioFrequencyStrip* frequencyStrip = RadioUiUtilities::frequencyStrip();
-    RadioStationCarousel* carousel = RadioUiUtilities::carousel();
+    RadioUtil::setScanStatus( Scan::NotScanning );
+    RadioUtil::setFrequencyScanner( NULL );
+    RadioFrequencyStrip* frequencyStrip = RadioUtil::frequencyStrip();
+    RadioStationCarousel* carousel = RadioUtil::carousel();
+
+    disconnect( mScannerEngine.data(), SIGNAL(stationFound(RadioStation)), this, 0 );
+
+    RadioStationModel& model = mUiEngine.stationModel();
+    const int stationCount = model.rowCount();
 
     if ( mInMainView ) {
-        RadioStationModel& model = mUiEngine.stationModel();
+
+        disconnect( carousel, SIGNAL(scanAnimationFinished()), this, 0 );
 
         // Scroll the carousel and frequency strip through all of the scanned stations
-        const int stationCount = model.rowCount();
         if ( stationCount > 1 ) {
-            frequencyStrip->setAutoScrollTime( 1000 );
-            carousel->setAutoScrollTime( 1000 );
-            const uint frequency = model.data( model.index( 0, 0 ), RadioStationModel::RadioStationRole ).value<RadioStation>().frequency();
-            frequencyStrip->setFrequency( frequency, TuneReason::StationScan );
-            carousel->setFrequency( frequency, TuneReason::StationScan );
+            frequencyStrip->setAutoScrollTime( STRIP_FINISH_SCROLL_TIME );
+            carousel->setAutoScrollTime( CAROUSEL_FINISH_SCROLL_TIME );
+
+            Radio::connect( carousel,   SIGNAL(scrollingEnded()),
+                            this,       SLOT(restoreUiControls()) );
+
+            const uint frequency = model.stationAt( 0 ).frequency();
+            frequencyStrip->setFrequency( frequency, TuneReason::StationScanFinalize, Scroll::Right );
+            carousel->setFrequency( frequency, TuneReason::StationScanFinalize, Scroll::Right );
 
             frequencyStrip->setAutoScrollTime( mStripScrollTime );
             carousel->setAutoScrollTime( mCarouselScrollTime );
-        }
+        } else {
+            const uint frequency = mUiEngine.minFrequency();
+            frequencyStrip->setFrequency( frequency, TuneReason::Unspecified );
+            carousel->setFrequency( frequency, TuneReason::Unspecified );
 
-        QTimer::singleShot( 100, this, SLOT(restoreUiControls()) );
+            if ( !mUserCanceled ) {
+                HbMessageBox* box = new HbMessageBox( HbMessageBox::MessageTypeInformation );
+                box->setTimeout( HbPopup::NoTimeout );
+                box->setText( hbTrId( "txt_rad_dpophead_no_stations_found_try_searching" ) );
+                box->setDismissPolicy( HbPopup::NoDismiss );
+                box->setAttribute( Qt::WA_DeleteOnClose, true );
+                box->open();
+            }
+
+            restoreUiControls();
+        }
 
     } else {
-        mScanningProgressNote->setProgressValue( mScanningProgressNote->maximum() );
-        deleteLater();
+        if ( !mUserCanceled ) {
+            if ( mScanningProgressNote ) {
+                mScanningProgressNote->setProgressValue( mScanningProgressNote->maximum() );
+                mScanningProgressNote->close();
+            }
 
-        carousel->setCarouselModel( mUiEngine.carouselModel() );
+            if ( stationCount == 0 ) {
+                HbMessageBox* box = new HbMessageBox( HbMessageBox::MessageTypeInformation );
+                box->setTimeout( HbPopup::NoTimeout );
+                box->setText( hbTrId( "txt_rad_dpophead_no_stations_found_try_searching" ) );
+                box->setDismissPolicy( HbPopup::NoDismiss );
+                box->setAttribute( Qt::WA_DeleteOnClose, true );
+                box->open();
+            }
+        }
     }
 
-    disconnect( mScannerEngine.data(),  SIGNAL(stationFound(RadioStation)),
-                this,                   SLOT(updateScanAndSaveProgress(RadioStation)) );
+    mIsAlive = false;
+    emit frequencyScannerFinished();
 
-    emit frequencyScannerFinished();
+    if ( !mInMainView ) {
+        if ( mUserCanceled ) {
+            deleteLater();
+        } else {
+            delete this; //TODO: Remove this weird hack once the problem with deleteLater is clear
+        }
+    }
 }