radioapp/radiopresetstorage/src/radiopresetstorage.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 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
    20 #include <presetutility.h>
       
    21 #include <preset.h>
       
    22 #else
       
    23 #   include <radiofmpresetutility.h>
       
    24 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
    25 
       
    26 #include <qstring>
       
    27 
       
    28 // User includes
       
    29 #include "radiopresetstorage.h"
       
    30 #include "radiopresetstorage_p.h"
       
    31 #include "radiostationif.h"
       
    32 
       
    33 /*!
       
    34  * Converts a symbian descriptor to Qt string
       
    35  */
       
    36 static QString convertString( const TDesC& aDesc )
       
    37 {
       
    38     return QString( (QChar*)aDesc.Ptr(), aDesc.Length() );
       
    39 }
       
    40 
       
    41 /*!
       
    42  *
       
    43  */
       
    44 RadioPresetStorage::RadioPresetStorage() :
       
    45     d_ptr( new RadioPresetStoragePrivate() )
       
    46 {
       
    47     Q_D( RadioPresetStorage );
       
    48     d->init();
       
    49 }
       
    50 
       
    51 /*!
       
    52  *
       
    53  */
       
    54 RadioPresetStorage::~RadioPresetStorage()
       
    55 {
       
    56 }
       
    57 
       
    58 /*!
       
    59  *
       
    60  */
       
    61 int RadioPresetStorage::maxNumberOfPresets() const
       
    62 {
       
    63     Q_D( const RadioPresetStorage );
       
    64 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
    65     return d->mPresetUtility->MaxNumberOfPresets();
       
    66 #else
       
    67     TInt maxPresets = 0;
       
    68     d->mPresetUtility->GetMaxNumberOfPresets( maxPresets );
       
    69     return maxPresets;
       
    70 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
    71 }
       
    72 
       
    73 /*!
       
    74  *
       
    75  */
       
    76 int RadioPresetStorage::presetCount() const
       
    77 {
       
    78     Q_D( const RadioPresetStorage );
       
    79 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
    80     return d->mPresetUtility->PresetCount();
       
    81 #else
       
    82     TInt presetCount = 0;
       
    83     d->mPresetUtility->GetNumberOfPresets( presetCount );
       
    84     return presetCount;
       
    85 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
    86 }
       
    87 
       
    88 /*!
       
    89  *
       
    90  */
       
    91 int RadioPresetStorage::firstPreset() const
       
    92 {
       
    93     Q_D( const RadioPresetStorage );
       
    94 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
    95     return d->mPresetUtility->FirstPreset();
       
    96 #else
       
    97     TInt firstIndex = -1;
       
    98     TRAPD( err, d->mPresetUtility->GetFirstPresetL( firstIndex ) );
       
    99     if ( err ) {
       
   100         firstIndex = -1;
       
   101     }
       
   102     return firstIndex;
       
   103 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   104 }
       
   105 
       
   106 /*!
       
   107  *
       
   108  */
       
   109 int RadioPresetStorage::nextPreset( int fromIndex ) const
       
   110 {
       
   111     Q_D( const RadioPresetStorage );
       
   112 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   113     return d->mPresetUtility->NextPreset( fromIndex );
       
   114 #else
       
   115     TInt nextIndex = -1;
       
   116     TRAPD( err, d->mPresetUtility->GetNextPresetL( fromIndex, nextIndex ) );
       
   117     if ( err ) {
       
   118         nextIndex = -1;
       
   119     }
       
   120     return nextIndex;
       
   121 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   122 }
       
   123 
       
   124 /*!
       
   125  *
       
   126  */
       
   127 bool RadioPresetStorage::deletePreset( int presetIndex )
       
   128 {
       
   129     Q_D( RadioPresetStorage );
       
   130 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   131     return d->mPresetUtility->DeletePreset( presetIndex ) == KErrNone;
       
   132 #else
       
   133     TRAPD( err, d->mPresetUtility->DeletePresetL( presetIndex ) );
       
   134     return err;
       
   135 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   136 }
       
   137 
       
   138 /*!
       
   139  *
       
   140  */
       
   141 bool RadioPresetStorage::savePreset( const RadioStationIf& station )
       
   142 {
       
   143     Q_D( RadioPresetStorage );
       
   144 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   145     TPreset preset;
       
   146     preset.SetFrequency( station.frequency() );
       
   147     TPresetName name( station.name().utf16() );
       
   148     preset.SetName( name );
       
   149     preset.SetRenamedByUser( station.isRenamedByUser() );
       
   150     preset.SetGenre( station.genre() );
       
   151     TRadioUrl url( station.url().utf16() );
       
   152     preset.SetUrl( url );
       
   153     preset.SetPiCode( station.piCode() );
       
   154     preset.SetFavorite( station.isFavorite() );
       
   155     preset.SetLocalStation( station.isLocalStation() );
       
   156 
       
   157     TRAPD( err, d->mPresetUtility->SavePresetL( preset, station.presetIndex() ) );
       
   158     return err == KErrNone;
       
   159 #else
       
   160     TFmPresetName name( station.name().utf16() );
       
   161     TRAPD( err, d->mPresetUtility->SetPresetL( station.presetIndex(), name, station.frequency() ) );
       
   162     return err == KErrNone;
       
   163 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   164 }
       
   165 
       
   166 /*!
       
   167  *
       
   168  */
       
   169 bool RadioPresetStorage::readPreset( int index, RadioStationIf& station )
       
   170 {
       
   171     Q_D( RadioPresetStorage );
       
   172 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   173     TPreset preset;
       
   174     TRAPD( err, d->mPresetUtility->ReadPresetL( index, preset ) );
       
   175     if ( !err ) {
       
   176 
       
   177         station.setPresetIndex( index );
       
   178         station.setFrequency( preset.Frequency() );
       
   179         station.setName( convertString( preset.Name() ) );
       
   180         station.setRenamedByUser( preset.RenamedByUser() );
       
   181         station.setGenre( preset.Genre() );
       
   182         station.setUrl( convertString( preset.Url() ) );
       
   183         station.setPiCode( preset.PiCode() );
       
   184         station.setFavorite( preset.Favorite() );
       
   185         station.setLocalStation( preset.LocalStation() );
       
   186 
       
   187         return true;
       
   188     }
       
   189     return false;
       
   190 #else
       
   191     TFmPresetName nameDesc;
       
   192     TInt frequency = 0;
       
   193     TRAPD( err, d->mPresetUtility->GetPresetL( index, nameDesc, frequency ) );
       
   194     if ( !err )
       
   195     {
       
   196         station.setPresetIndex( index );
       
   197         station.setName( convertString( nameDesc ) );
       
   198         station.setFrequency( static_cast<TUint>( frequency ) );
       
   199         station.setLocalStation( true );
       
   200     }
       
   201     return err == KErrNone;
       
   202 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   203 }
       
   204 
       
   205 /*!
       
   206  *
       
   207  */
       
   208 RadioPresetStoragePrivate::RadioPresetStoragePrivate()
       
   209 {
       
   210 }
       
   211 
       
   212 /*!
       
   213  *
       
   214  */
       
   215 RadioPresetStoragePrivate::~RadioPresetStoragePrivate()
       
   216 {
       
   217 }
       
   218 
       
   219 /*!
       
   220  *
       
   221  */
       
   222 bool RadioPresetStoragePrivate::init()
       
   223 {
       
   224 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY
       
   225     TRAPD( err, mPresetUtility.reset( CPresetUtility::NewL() ) );
       
   226     
       
   227 #else
       
   228     TRAPD( err, mPresetUtility.reset( CRadioFmPresetUtility::NewL( *this ) ) );
       
   229 #endif // COMPILE_WITH_NEW_PRESET_UTILITY
       
   230     return err == KErrNone;
       
   231 }