radioapp/radiouiengine/src/radiouiengine.cpp
changeset 13 46974bebc798
child 14 63aabac4416d
equal deleted inserted replaced
0:f3d95d9c00ab 13:46974bebc798
       
     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 <qscopedpointer>
       
    20 #include <qprocess>
       
    21 #include <qfile>
       
    22 
       
    23 #ifdef BUILD_WIN32
       
    24 #   include <qsettings>
       
    25 #endif // WIN32_BUILD
       
    26 
       
    27 // User includes
       
    28 #include "radiouiengine.h"
       
    29 #include "radiouiengine_p.h"
       
    30 #include "radiologger.h"
       
    31 #include "radioenginewrapper.h"
       
    32 #include "radiostationmodel.h"
       
    33 #include "radioplaylogmodel.h"
       
    34 #include "radiosettings.h"
       
    35 #include "radiostationfiltermodel.h"
       
    36 #include "radiolocalization.h"
       
    37 
       
    38 #ifdef USE_MOBILE_EXTENSIONS_API
       
    39     #include "xqprofile"
       
    40 #endif
       
    41 
       
    42 // Constants
       
    43 const QString KPathFormatter = "%1:%2%3";
       
    44 const QString KApplicationDir = "\\sys\\bin\\";
       
    45 const QString KSongRecognitionApp = "Shazam_0x200265B3.exe";
       
    46 const QString KSongRecognitionAppParams = "-listen";
       
    47 
       
    48 /*!
       
    49  *
       
    50  */
       
    51 bool RadioUiEngine::isOfflineProfile()
       
    52 {
       
    53     bool offline = false;
       
    54 
       
    55 #ifdef USE_MOBILE_EXTENSIONS_API
       
    56     QScopedPointer<XQProfile> profile ( new XQProfile() );  // Deletes automatically when out of scope
       
    57     offline = profile->activeProfile() == XQProfile::ProfileOffLine;
       
    58 #elif BUILD_WIN32
       
    59     QScopedPointer<QSettings> settings( new QSettings( "Nokia", "QtFmRadio" ) );
       
    60     offline = settings->value( "Offline", false ).toBool();
       
    61 #endif
       
    62 
       
    63     return offline;
       
    64 }
       
    65 
       
    66 /*!
       
    67  *
       
    68  */
       
    69 QString RadioUiEngine::parseFrequency( uint frequency )
       
    70 {
       
    71     return QTRANSLATE( KFrequencyMhz ).arg( RadioStation::parseFrequency( frequency ) );
       
    72 }
       
    73 
       
    74 /*!
       
    75  *
       
    76  */
       
    77 QString RadioUiEngine::nameOrFrequency( const RadioStation& station, uint frequency )
       
    78 {
       
    79     if ( frequency == 0 )
       
    80     {
       
    81         frequency = station.frequency();
       
    82     }
       
    83 
       
    84     QString text = "";
       
    85     if ( station.isValid() && !station.name().isEmpty() )
       
    86     {
       
    87         text = station.name();
       
    88     }
       
    89     else
       
    90     {
       
    91         text = parseFrequency( frequency );
       
    92     }
       
    93 
       
    94     return text;
       
    95 }
       
    96 
       
    97 /*!
       
    98  *
       
    99  */
       
   100 RadioUiEngine::RadioUiEngine( QObject* parent ) :
       
   101     QObject( parent ),
       
   102     d_ptr( new RadioUiEnginePrivate( this ) )
       
   103 {
       
   104 }
       
   105 
       
   106 /*!
       
   107  *
       
   108  */
       
   109 RadioUiEngine::~RadioUiEngine()
       
   110 {
       
   111 }
       
   112 
       
   113 /*!
       
   114  *
       
   115  */
       
   116 bool RadioUiEngine::startRadio()
       
   117 {
       
   118     Q_D( RadioUiEngine );
       
   119     return d->startRadio();
       
   120 }
       
   121 
       
   122 /*!
       
   123  *
       
   124  */
       
   125 bool RadioUiEngine::isFirstTimeStart()
       
   126 {
       
   127     Q_D( RadioUiEngine );
       
   128     return d->mEngineWrapper->settings().isFirstTimeStart();
       
   129 }
       
   130 
       
   131 /*!
       
   132  * Returns the settings handler owned by the engine
       
   133  */
       
   134 RadioSettings& RadioUiEngine::settings()
       
   135 {
       
   136     Q_D( RadioUiEngine );
       
   137     return d->mEngineWrapper->settings();
       
   138 }
       
   139 
       
   140 /*!
       
   141  * Returns the station model
       
   142  */
       
   143 RadioStationModel& RadioUiEngine::model()
       
   144 {
       
   145     Q_D( RadioUiEngine );
       
   146     return *d->mStationModel;
       
   147 }
       
   148 
       
   149 /*!
       
   150  * Returns the play log model
       
   151  */
       
   152 RadioPlayLogModel& RadioUiEngine::playLogModel()
       
   153 {
       
   154     Q_D( RadioUiEngine );
       
   155     if ( !d->mPlayLogModel ) {
       
   156         d->mPlayLogModel = new RadioPlayLogModel( *this );
       
   157     }
       
   158 
       
   159     return *d->mPlayLogModel;
       
   160 }
       
   161 
       
   162 /*!
       
   163  * Returns the stations list
       
   164  */
       
   165 RadioStationFilterModel* RadioUiEngine::createNewFilterModel( QObject* parent )
       
   166 {
       
   167     return new RadioStationFilterModel( *this, parent );
       
   168 }
       
   169 
       
   170 /*!
       
   171  *
       
   172  */
       
   173 bool RadioUiEngine::isRadioOn() const
       
   174 {
       
   175     Q_D( const RadioUiEngine );
       
   176     return d->mEngineWrapper->isRadioOn();
       
   177 }
       
   178 
       
   179 /*!
       
   180  *
       
   181  */
       
   182 bool RadioUiEngine::isScanning() const
       
   183 {
       
   184     Q_D( const RadioUiEngine );
       
   185     return d->mEngineWrapper->isScanning();
       
   186 }
       
   187 
       
   188 /*!
       
   189  *
       
   190  */
       
   191 bool RadioUiEngine::isMuted() const
       
   192 {
       
   193     Q_D( const RadioUiEngine );
       
   194     return d->mEngineWrapper->isMuted();
       
   195 }
       
   196 
       
   197 /*!
       
   198  *
       
   199  */
       
   200 bool RadioUiEngine::isAntennaAttached() const
       
   201 {
       
   202     Q_D( const RadioUiEngine );
       
   203     return d->mEngineWrapper->isAntennaAttached();
       
   204 }
       
   205 
       
   206 /*!
       
   207  *
       
   208  */
       
   209 bool RadioUiEngine::isUsingLoudspeaker() const
       
   210 {
       
   211     Q_D( const RadioUiEngine );
       
   212     return d->mEngineWrapper->isUsingLoudspeaker();
       
   213 }
       
   214 
       
   215 /*!
       
   216  * Returns the selected radio region
       
   217  */
       
   218 RadioRegion::Region RadioUiEngine::region() const
       
   219 {
       
   220     Q_D( const RadioUiEngine );
       
   221     return d->mEngineWrapper->region();
       
   222 }
       
   223 
       
   224 /*!
       
   225  * Returns the currently tuned frequency
       
   226  */
       
   227 uint RadioUiEngine::currentFrequency() const
       
   228 {
       
   229     Q_D( const RadioUiEngine );
       
   230     return d->mEngineWrapper->currentFrequency();
       
   231 }
       
   232 
       
   233 /*!
       
   234  * Returns the minimum frequency
       
   235  */
       
   236 uint RadioUiEngine::minFrequency() const
       
   237 {
       
   238     Q_D( const RadioUiEngine );
       
   239     return d->mEngineWrapper->minFrequency();
       
   240 }
       
   241 
       
   242 /*!
       
   243  * Returns the maximum frequency
       
   244  */
       
   245 uint RadioUiEngine::maxFrequency() const
       
   246 {
       
   247     Q_D( const RadioUiEngine );
       
   248     return d->mEngineWrapper->maxFrequency();
       
   249 }
       
   250 
       
   251 /*!
       
   252  * Returns the frequency step size from the selected region
       
   253  */
       
   254 uint RadioUiEngine::frequencyStepSize() const
       
   255 {
       
   256     Q_D( const RadioUiEngine );
       
   257     return d->mEngineWrapper->frequencyStepSize();
       
   258 }
       
   259 
       
   260 /*!
       
   261  *
       
   262  */
       
   263 void RadioUiEngine::scanFrequencyBand()
       
   264 {
       
   265     Q_D( RadioUiEngine );
       
   266     d->mEngineWrapper->scanFrequencyBand();
       
   267 }
       
   268 
       
   269 /*!
       
   270  *
       
   271  */
       
   272 void RadioUiEngine::cancelScanFrequencyBand()
       
   273 {
       
   274     Q_D( RadioUiEngine );
       
   275     d->mEngineWrapper->cancelScanFrequencyBand();
       
   276 }
       
   277 
       
   278 /*!
       
   279  *
       
   280  */
       
   281 QList<RadioStation> RadioUiEngine::stationsInRange( uint minFrequency, uint maxFrequency )
       
   282 {
       
   283     Q_D( RadioUiEngine );
       
   284     return d->mStationModel->stationsInRange( minFrequency, maxFrequency );
       
   285 }
       
   286 
       
   287 /*!
       
   288  *
       
   289  */
       
   290 QString RadioUiEngine::genreToString( int genre )
       
   291 {
       
   292     RadioRegion::Region currentRegion = region();
       
   293 
       
   294     const GenreStruct* genreArray = currentRegion == RadioRegion::America ? AmericanGenres : EuropeanGenres;
       
   295     const int genreCount = currentRegion == RadioRegion::America ? AmericanGenresCount : EuropeanGenresCount;
       
   296 
       
   297     for( int i = 0; i < genreCount; ++i ) {
       
   298         if ( genreArray[i].mGenreCode == genre ) {
       
   299             return TRANSLATE( genreArray[i].mTranslation );
       
   300 //            return qApp->translate( GenreContext,
       
   301 //                    genreArray[i].mTranslation.source,
       
   302 //                    genreArray[i].mTranslation.comment );
       
   303         }
       
   304     }
       
   305 
       
   306     return "";
       
   307 }
       
   308 
       
   309 /*!
       
   310  *
       
   311  */
       
   312 bool RadioUiEngine::isSongRecognitionAppAvailable()
       
   313 {
       
   314     //TODO: Check if there is a better way to check if an application is available
       
   315     bool available = false;
       
   316 
       
   317     // Check the Z: drive
       
   318     QString fullPath = QString( KPathFormatter ).arg( "Z" ).arg( KApplicationDir ).arg( KSongRecognitionApp );
       
   319     available = QFile::exists( fullPath );
       
   320 
       
   321     LOG_FORMAT( "Checking file: %s. found %d", GETSTRING( fullPath ), available );
       
   322 
       
   323     if ( !available ) {
       
   324         // Check the C: drive
       
   325         fullPath = QString( KPathFormatter ).arg( "C" ).arg( KApplicationDir ).arg( KSongRecognitionApp );
       
   326         available = QFile::exists( fullPath );
       
   327         LOG_FORMAT( "Checking file: %s. found %d", GETSTRING( fullPath ), available );
       
   328         if ( !available ) {
       
   329             // Check the E: drive
       
   330             fullPath = QString( KPathFormatter ).arg( "E" ).arg( KApplicationDir ).arg( KSongRecognitionApp );
       
   331             available = QFile::exists( fullPath );
       
   332             LOG_FORMAT( "Checking file: %s. found %d", GETSTRING( fullPath ), available );
       
   333         }
       
   334     }
       
   335     return available;
       
   336 }
       
   337 
       
   338 /*!
       
   339  *
       
   340  */
       
   341 void RadioUiEngine::addRecognizedSong( const QString& artist, const QString& title )
       
   342 {
       
   343     Q_D( RadioUiEngine );
       
   344     d->mPlayLogModel->addItem( artist, title );
       
   345 }
       
   346 
       
   347 /*!
       
   348  * Public slot
       
   349  * Tunes to the given frequency
       
   350  */
       
   351 void RadioUiEngine::tuneFrequency( uint frequency, const int sender )
       
   352 {
       
   353     Q_D( RadioUiEngine );
       
   354     if ( frequency != d->mStationModel->currentStation().frequency() && d->mEngineWrapper->isFrequencyValid( frequency ) ) {
       
   355         LOG_FORMAT( "RadioUiEngine::tuneFrequency, frequency: %d", frequency );
       
   356         d->mEngineWrapper->tuneFrequency( frequency, sender );
       
   357     }
       
   358 }
       
   359 
       
   360 /*!
       
   361  * Public slot
       
   362  * Tunes to the given frequency after a delay
       
   363  */
       
   364 void RadioUiEngine::tuneWithDelay( uint frequency, const int sender )
       
   365 {
       
   366     Q_D( RadioUiEngine );
       
   367     if ( frequency != d->mStationModel->currentStation().frequency() &&  d->mEngineWrapper->isFrequencyValid( frequency ) ) {
       
   368         LOG_FORMAT( "RadioEngineWrapperPrivate::tuneWithDelay, frequency: %d", frequency );
       
   369         d->mEngineWrapper->tuneWithDelay( frequency, sender );
       
   370     }
       
   371 }
       
   372 
       
   373 /*!
       
   374  * Public slot
       
   375  * Tunes to the given preset
       
   376  */
       
   377 void RadioUiEngine::tunePreset( int presetIndex )
       
   378 {
       
   379     Q_D( RadioUiEngine );
       
   380     if ( presetIndex != d->mStationModel->currentStation().presetIndex() ) {
       
   381         RadioStation station;
       
   382         if ( d->mStationModel->findPresetIndex( presetIndex, station ) != RadioStation::NotFound &&
       
   383                 d->mEngineWrapper->isFrequencyValid( station.frequency() ) ) {
       
   384             LOG_FORMAT( "RadioEngineWrapperPrivate::tunePreset, presetIndexPosition: %d", presetIndex );
       
   385 
       
   386             d->mEngineWrapper->tuneFrequency( station.frequency(), CommandSender::Unspecified );
       
   387         }
       
   388     }
       
   389 }
       
   390 
       
   391 /*!
       
   392  * Public slot
       
   393  * volume update command slot for the engine
       
   394  */
       
   395 void RadioUiEngine::setVolume( int volume )
       
   396 {
       
   397     Q_D( RadioUiEngine );
       
   398     d->mEngineWrapper->setVolume( volume );
       
   399 }
       
   400 
       
   401 /*!
       
   402  * Public slot
       
   403  *
       
   404  */
       
   405 void RadioUiEngine::toggleMute()
       
   406 {
       
   407     Q_D( RadioUiEngine );
       
   408     d->mEngineWrapper->toggleMute();
       
   409 }
       
   410 
       
   411 /*!
       
   412  * Public slot
       
   413  *
       
   414  */
       
   415 void RadioUiEngine::toggleAudioRoute()
       
   416 {
       
   417     Q_D( RadioUiEngine );
       
   418     d->mEngineWrapper->toggleAudioRoute();
       
   419 }
       
   420 
       
   421 /*!
       
   422  * Public slot
       
   423  *
       
   424  */
       
   425 void RadioUiEngine::skipPrevious()
       
   426 {
       
   427     Q_D( RadioUiEngine );    
       
   428     d->skip( RadioUiEnginePrivate::Previous );
       
   429 }
       
   430 
       
   431 /*!
       
   432  * Public slot
       
   433  *
       
   434  */
       
   435 void RadioUiEngine::skipNext()
       
   436 {
       
   437     Q_D( RadioUiEngine );
       
   438     d->skip( RadioUiEnginePrivate::Next );
       
   439 }
       
   440 
       
   441 /*!
       
   442  * Public slot
       
   443  *
       
   444  */
       
   445 void RadioUiEngine::seekUp()
       
   446 {
       
   447     Q_D( RadioUiEngine );
       
   448     d->mEngineWrapper->startSeeking( Seeking::Up );
       
   449 }
       
   450 
       
   451 /*!
       
   452  * Public slot
       
   453  *
       
   454  */
       
   455 void RadioUiEngine::seekDown()
       
   456 {
       
   457     Q_D( RadioUiEngine );
       
   458     d->mEngineWrapper->startSeeking( Seeking::Down );
       
   459 }
       
   460 
       
   461 /*!
       
   462  * Public slot
       
   463  *
       
   464  */
       
   465 void RadioUiEngine::launchSongRecognition()
       
   466 {
       
   467     LOG_FORMAT("RadioUiEngine::launchSongRecognition() starting:  %s", GETSTRING( KSongRecognitionApp ) );
       
   468 
       
   469     QStringList arguments;
       
   470     arguments << KSongRecognitionAppParams;
       
   471 
       
   472     bool started = QProcess::startDetached( KSongRecognitionApp, arguments );
       
   473     LOG_ASSERT( started, LOG_FORMAT("RadioUiEngine::launchSongRecognition() failed to start %s", GETSTRING( KSongRecognitionApp ) ) );
       
   474 }
       
   475 
       
   476 /*!
       
   477  * Function used by the private implementation to emit a tunedToFrequency signal
       
   478  */
       
   479 void RadioUiEngine::emitTunedToFrequency( uint frequency, int commandSender )
       
   480 {
       
   481     emit tunedToFrequency( frequency, commandSender );
       
   482 }
       
   483 
       
   484 /*!
       
   485  * Function used by the private implementation to emit a seekingStarted signal
       
   486  */
       
   487 void RadioUiEngine::emitSeekingStarted( Seeking::Direction direction )
       
   488 {
       
   489     emit seekingStarted( direction );
       
   490 }
       
   491 
       
   492 /*!
       
   493  * Function used by the private implementation to emit a radioStatusChanged signal
       
   494  */
       
   495 void RadioUiEngine::emitRadioStatusChanged( bool radioIsOn )
       
   496 {
       
   497     emit radioStatusChanged( radioIsOn );
       
   498 }
       
   499 
       
   500 /*!
       
   501  * Function used by the private implementation to emit a rdsAvailabilityChanged signal
       
   502  */
       
   503 void RadioUiEngine::emitRdsAvailabilityChanged( bool available )
       
   504 {
       
   505     emit rdsAvailabilityChanged( available );
       
   506 }
       
   507 
       
   508 /*!
       
   509  * Function used by the private implementation to emit a volumeChanged signal
       
   510  */
       
   511 void RadioUiEngine::emitVolumeChanged( int volume )
       
   512 {
       
   513     emit volumeChanged( volume );
       
   514 }
       
   515 
       
   516 /*!
       
   517  * Function used by the private implementation to emit a muteChanged signal
       
   518  */
       
   519 void RadioUiEngine::emitMuteChanged( bool muted )
       
   520 {
       
   521     emit muteChanged( muted );
       
   522 }
       
   523 
       
   524 /*!
       
   525  * Function used by the private implementation to emit a audioRouteChanged signal
       
   526  */
       
   527 void RadioUiEngine::emitAudioRouteChanged( bool loudspeaker )
       
   528 {
       
   529     emit audioRouteChanged( loudspeaker );
       
   530 }
       
   531 
       
   532 /*!
       
   533  * Function used by the private implementation to emit a scanAndSaveFinished signal
       
   534  */
       
   535 void RadioUiEngine::emitScanAndSaveFinished()
       
   536 {
       
   537     emit scanAndSaveFinished();
       
   538 }
       
   539 /*!
       
   540  * Function used by the private implementation to emit a headsetStatusChanged signal
       
   541  */
       
   542 void RadioUiEngine::emitheadsetStatusChanged( bool connected )
       
   543 {
       
   544     emit headsetStatusChanged( connected );
       
   545 }
       
   546