radioapp/radiowidgets/src/radiofrequencyscanner.cpp
branchRCL_3
changeset 19 cce62ebc198e
equal deleted inserted replaced
18:1a6714c53019 19:cce62ebc198e
       
     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 <HbProgressDialog>
       
    20 #include <QTimer>
       
    21 #include <HbLabel>
       
    22 #include <HbPushButton>
       
    23 #include <HbMessageBox>
       
    24 
       
    25 // User includes
       
    26 #include "radiofrequencyscanner.h"
       
    27 #include "radioscannerengine.h"
       
    28 #include "radiouiengine.h"
       
    29 #include "radiologger.h"
       
    30 #include "radiostationmodel.h"
       
    31 #include "radiofrequencystrip.h"
       
    32 #include "radiostationcarousel.h"
       
    33 #include "radioutil.h"
       
    34 #include "radiomainview.h"
       
    35 
       
    36 // Constants
       
    37 const int EXTRA_TO_PROGRESS_MAX_VALUE = 100000;
       
    38 const int CAROUSEL_FINISH_SCROLL_TIME = 1000;
       
    39 const int STRIP_FINISH_SCROLL_TIME = 1100;
       
    40 const int CAROUSEL_SCROLL_TIME = 1000;
       
    41 const int STRIP_SCROLL_TIME = 1100;
       
    42 const int START_DELAY = 1000;
       
    43 
       
    44 /*!
       
    45  *
       
    46  */
       
    47 RadioFrequencyScanner::RadioFrequencyScanner( RadioUiEngine& uiEngine, QObject* parent ) :
       
    48     QObject( parent ),
       
    49     mUiEngine( uiEngine ),
       
    50     mInMainView( parent->metaObject()->className() == RadioMainView::staticMetaObject.className() ),
       
    51     mScannerEngine( mUiEngine.createScannerEngine() ),
       
    52     mStripScrollTime( 0 ),
       
    53     mCarouselScrollTime( 0 ),
       
    54     mIsAlive( false ),
       
    55     mUserCanceled( false )
       
    56 {
       
    57     RadioUtil::setFrequencyScanner( this );
       
    58 
       
    59     Radio::connect( mScannerEngine.data(),  SIGNAL(stationFound(RadioStation)),
       
    60                     this,                   SLOT(updateScanProgress(RadioStation)) );
       
    61 }
       
    62 
       
    63 /*!
       
    64  *
       
    65  */
       
    66 RadioFrequencyScanner::~RadioFrequencyScanner()
       
    67 {
       
    68     RadioUtil::setScanStatus( Scan::NotScanning );
       
    69 }
       
    70 
       
    71 /*!
       
    72  *
       
    73  */
       
    74 void RadioFrequencyScanner::startScanning()
       
    75 {
       
    76     mIsAlive = true;
       
    77     RadioFrequencyStrip* frequencyStrip = RadioUtil::frequencyStrip();
       
    78     RadioStationCarousel* carousel = RadioUtil::carousel();
       
    79 
       
    80     if ( mInMainView ) {
       
    81 
       
    82         // If this is the first time start, bring application to foreground
       
    83         if ( mUiEngine.isFirstTimeStart() ) {
       
    84             static_cast<RadioMainView*>( parent() )->bringToForeground();
       
    85         }
       
    86 
       
    87         RadioUtil::setScanStatus( Scan::ScanningInMainView );
       
    88         mStripScrollTime = frequencyStrip->autoScrollTime();
       
    89         mCarouselScrollTime = carousel->autoScrollTime();
       
    90 
       
    91         carousel->setScanningMode( true );
       
    92         carousel->setAutoScrollTime( CAROUSEL_SCROLL_TIME );
       
    93         frequencyStrip->setAutoScrollTime( STRIP_SCROLL_TIME );
       
    94 
       
    95         Radio::connect( carousel,               SIGNAL(scanAnimationFinished()),
       
    96                         this,                   SLOT(continueScanning()) );
       
    97 
       
    98         static_cast<RadioMainView*>( parent() )->setScanningMode( true );
       
    99         frequencyStrip->setScanningMode( true );
       
   100     } else {
       
   101         RadioUtil::setScanStatus( Scan::ScanningInStationsView );
       
   102         mScanningProgressNote = new HbProgressDialog( HbProgressDialog::ProgressDialog );
       
   103         mScanningProgressNote.data()->setModal( true );
       
   104         mScanningProgressNote.data()->setAutoClose( true );
       
   105 
       
   106         // Add some extra to the maximum value to allow room for the station at the low band edge
       
   107         mScanningProgressNote.data()->setRange( mUiEngine.minFrequency(), mUiEngine.maxFrequency() + EXTRA_TO_PROGRESS_MAX_VALUE );
       
   108         mScanningProgressNote.data()->setProgressValue( mUiEngine.minFrequency() );
       
   109         mScanningProgressNote.data()->setText( hbTrId( "txt_rad_info_searching_local_stations_please_wait" ) );
       
   110         mScanningProgressNote.data()->setAttribute( Qt::WA_DeleteOnClose, true );
       
   111         mScanningProgressNote.data()->open();
       
   112 
       
   113         Radio::connect( mScanningProgressNote.data(),   SIGNAL(cancelled()),
       
   114                         this,                           SLOT(cancelScanning()) );
       
   115     }
       
   116 
       
   117     QTimer::singleShot( START_DELAY, this, SLOT(delayedStart()) );
       
   118 }
       
   119 
       
   120 /*!
       
   121  *
       
   122  */
       
   123 bool RadioFrequencyScanner::isAlive() const
       
   124 {
       
   125     return mIsAlive;
       
   126 }
       
   127 
       
   128 /*!
       
   129  * Public slot
       
   130  *
       
   131  */
       
   132 void RadioFrequencyScanner::cancelScanning()
       
   133 {
       
   134     mUserCanceled = true;
       
   135     RadioUtil::carousel()->cancelAnimation();
       
   136     finishScanning();
       
   137 }
       
   138 
       
   139 /*!
       
   140  * Private slot
       
   141  *
       
   142  */
       
   143 void RadioFrequencyScanner::delayedStart()
       
   144 {
       
   145     mScannerEngine->startScanning();
       
   146 }
       
   147 
       
   148 /*!
       
   149  * Private slot
       
   150  *
       
   151  */
       
   152 void RadioFrequencyScanner::updateScanProgress( const RadioStation& station )
       
   153 {
       
   154     if ( !station.isValid() ) {
       
   155         finishScanning();
       
   156         return;
       
   157     }
       
   158 
       
   159     const uint frequency = station.frequency();
       
   160     LOG_FORMAT( "RadioFrequencyScanner::updateScanAndSaveProgress frequency: %d", frequency );
       
   161 
       
   162     if ( mInMainView ) {
       
   163 
       
   164         RadioUtil::frequencyStrip()->setFrequency( frequency, TuneReason::StationScan );
       
   165         RadioUtil::carousel()->animateNewStation( station );
       
   166 
       
   167     } else {
       
   168         // Check for special case that can happen during scanning.
       
   169         // If there is a valid radio station at the low frequency band edge it will be reported last after
       
   170         // all of the higher frequencies. We don't update the progress value here because the value would
       
   171         // be lower than the previous one. The progress value is set to maximum when the scanner finishes.
       
   172         if ( frequency != mUiEngine.minFrequency() ) {
       
   173             mScanningProgressNote.data()->setProgressValue( frequency );
       
   174         }
       
   175 
       
   176         mScannerEngine->continueScanning();
       
   177     }
       
   178 }
       
   179 
       
   180 /*!
       
   181  * Private slot
       
   182  *
       
   183  */
       
   184 void RadioFrequencyScanner::continueScanning()
       
   185 {
       
   186     if ( !mUserCanceled ) {
       
   187         RadioUtil::frequencyStrip()->addScannedStation( mUiEngine.stationModel().currentStation().frequency() );
       
   188         mScannerEngine->continueScanning();
       
   189     }
       
   190 }
       
   191 
       
   192 /*!
       
   193  * Private slot
       
   194  *
       
   195  */
       
   196 void RadioFrequencyScanner::restoreUiControls()
       
   197 {
       
   198     if ( mInMainView ) {
       
   199         disconnect( RadioUtil::carousel(), SIGNAL(scrollingEnded()), this, 0 );
       
   200 
       
   201         RadioUtil::frequencyStrip()->setScanningMode( false );
       
   202         static_cast<RadioMainView*>( parent() )->setScanningMode( false );
       
   203         RadioUtil::carousel()->setScanningMode( false );
       
   204     }
       
   205 
       
   206     deleteLater();
       
   207 }
       
   208 
       
   209 /*!
       
   210  *
       
   211  */
       
   212 void RadioFrequencyScanner::finishScanning()
       
   213 {
       
   214     mScannerEngine->cancel();
       
   215     RadioUtil::setScanStatus( Scan::NotScanning );
       
   216     RadioUtil::setFrequencyScanner( NULL );
       
   217     RadioFrequencyStrip* frequencyStrip = RadioUtil::frequencyStrip();
       
   218     RadioStationCarousel* carousel = RadioUtil::carousel();
       
   219 
       
   220     disconnect( mScannerEngine.data(), SIGNAL(stationFound(RadioStation)), this, 0 );
       
   221 
       
   222     RadioStationModel& model = mUiEngine.stationModel();
       
   223     const int stationCount = model.rowCount();
       
   224 
       
   225     if ( mInMainView ) {
       
   226 
       
   227         disconnect( carousel, SIGNAL(scanAnimationFinished()), this, 0 );
       
   228 
       
   229         // Scroll the carousel and frequency strip through all of the scanned stations
       
   230         if ( stationCount > 1 ) {
       
   231             frequencyStrip->setAutoScrollTime( STRIP_FINISH_SCROLL_TIME );
       
   232             carousel->setAutoScrollTime( CAROUSEL_FINISH_SCROLL_TIME );
       
   233 
       
   234             Radio::connect( carousel,   SIGNAL(scrollingEnded()),
       
   235                             this,       SLOT(restoreUiControls()) );
       
   236 
       
   237             const uint frequency = model.stationAt( 0 ).frequency();
       
   238 
       
   239             mUiEngine.setFrequency( frequency, TuneReason::StationScanFinalize );
       
   240             frequencyStrip->setFrequency( frequency, TuneReason::StationScanFinalize, Scroll::Right );
       
   241             carousel->setFrequency( frequency, TuneReason::StationScanFinalize, Scroll::Right );
       
   242 
       
   243             frequencyStrip->setAutoScrollTime( mStripScrollTime );
       
   244             carousel->setAutoScrollTime( mCarouselScrollTime );
       
   245         } else {
       
   246             const uint frequency = mUiEngine.minFrequency();
       
   247             frequencyStrip->setFrequency( frequency, TuneReason::Unspecified );
       
   248             carousel->setFrequency( frequency, TuneReason::Unspecified );
       
   249 
       
   250             if ( !mUserCanceled ) {
       
   251                 HbMessageBox* box = new HbMessageBox( HbMessageBox::MessageTypeInformation );
       
   252                 box->setTimeout( HbPopup::NoTimeout );
       
   253                 box->setText( hbTrId( "txt_rad_dpophead_no_stations_found_try_searching" ) );
       
   254                 box->setDismissPolicy( HbPopup::NoDismiss );
       
   255                 box->setAttribute( Qt::WA_DeleteOnClose, true );
       
   256                 box->open();
       
   257             }
       
   258 
       
   259             restoreUiControls();
       
   260         }
       
   261 
       
   262     } else {
       
   263         if ( !mUserCanceled ) {
       
   264             if ( mScanningProgressNote ) {
       
   265                 mScanningProgressNote.data()->setProgressValue( mScanningProgressNote.data()->maximum() );
       
   266                 mScanningProgressNote.data()->close();
       
   267             }
       
   268 
       
   269             if ( stationCount == 0 ) {
       
   270                 HbMessageBox* box = new HbMessageBox( HbMessageBox::MessageTypeInformation );
       
   271                 box->setTimeout( HbPopup::NoTimeout );
       
   272                 box->setText( hbTrId( "txt_rad_dpophead_no_stations_found_try_searching" ) );
       
   273                 box->setDismissPolicy( HbPopup::NoDismiss );
       
   274                 box->setAttribute( Qt::WA_DeleteOnClose, true );
       
   275                 box->open();
       
   276             }
       
   277         }
       
   278         const uint frequency = model.stationAt( 0 ).frequency();
       
   279         mUiEngine.setFrequency( frequency, TuneReason::StationScanFinalize );
       
   280     }
       
   281 
       
   282     mIsAlive = false;
       
   283     emit frequencyScannerFinished();
       
   284 
       
   285     if ( !mInMainView ) {
       
   286         if ( mUserCanceled ) {
       
   287             deleteLater();
       
   288         } else {
       
   289             delete this; //TODO: Remove this weird hack once the problem with deleteLater is clear
       
   290         }
       
   291     }
       
   292 }