contacts_plat/phonebook_2_contact_related_icon_customization_api/tsrc/TestPlugin/src/CPbk2IconCustomApiTestPlugin.cpp
branchRCL_3
changeset 6 e8e3147d53eb
equal deleted inserted replaced
5:81f8547efd4f 6:e8e3147d53eb
       
     1 /*
       
     2 * Copyright (c) 2008-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 // INCLUDE FILES
       
    18 #include <coemain.h>
       
    19 #include <gulicon.h>
       
    20 #include <e32math.h>
       
    21 #include <fbs.h>
       
    22 #include <cntdef.h>
       
    23 #include <e32property.h>
       
    24 
       
    25 #include <CCustomIconDecisionData.h>
       
    26 #include <CCustomIconIdMap.h>
       
    27 #include <MCustomIconChangeObserver.h>
       
    28 
       
    29 #include "CPbk2IconCustomApiTestPlugin.h"
       
    30 
       
    31 #if defined(__WINS__) || defined(WINS)
       
    32 _LIT( KIconFile, "Z:\\Data\\Test\\CustIconApiTestIcons.mbm" );
       
    33 #else
       
    34 _LIT( KIconFile, "C:\\Data\\Test\\CustIconApiTestIcons.mbm" );
       
    35 #endif
       
    36 
       
    37 // Publish/Subscribe category UID of used for configuring test environment.
       
    38 // See SetPsProperty() implementation.
       
    39 const TUint KStiffPsCategoryUid = 0x1000007A;
       
    40 const TUint KIconApiTestPSPropertyCategoryUID = KStiffPsCategoryUid;
       
    41 
       
    42 
       
    43 // Publish/Subscribe keys under KIconApiTestPSPropertyCategoryUID category.
       
    44 // Make sure these are same as in Pbk2IconCustomApiTestModule.cfg
       
    45 const TInt KPsKeyIconUpdaterEnabled = 1;
       
    46 const TInt KPsKeyIconUpdaterIntervalMs = 2;
       
    47 const TInt KPsKeyNotificationEventType = 3;
       
    48 const TInt KPsKeyNotificationEmptyUidArray = 4;
       
    49 
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // GetPsTestProperty
       
    53 // Read property value from Publish/Subscribe subsystem.
       
    54 // The values are set by test module dll, contacts_dom\
       
    55 //      phonebook_2_contact_related_icon_customization_api\Internal\Test
       
    56 // and are meant to configure this plug-in at runtime.
       
    57 // ----------------------------------------------------------------------------
       
    58 //
       
    59 LOCAL_C TInt GetPsTestProperty( TInt aKey, TInt& aResult )
       
    60     {
       
    61     TInt value;
       
    62     TInt err = RProperty::Get(
       
    63         TUid::Uid( KIconApiTestPSPropertyCategoryUID ),
       
    64         aKey,
       
    65         value );
       
    66     if( !err )
       
    67         {
       
    68         aResult = value;
       
    69         }
       
    70     return err;
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // ValidateHintL
       
    75 // ----------------------------------------------------------------------------
       
    76 //
       
    77 LOCAL_C void ValidateHintL( const CCustomIconDecisionData& aHint )
       
    78     {
       
    79     // At least one of the members should have been filled by the caller
       
    80     if( aHint.ContactLinkRef() == NULL &&
       
    81         aHint.PhoneNumber() == KNullDesC &&
       
    82         aHint.Time() == NULL)
       
    83         {
       
    84         User::Leave( KErrArgument );
       
    85         }
       
    86 
       
    87     }
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // AppendIconToMapL
       
    91 // ----------------------------------------------------------------------------
       
    92 //
       
    93 LOCAL_C void AppendIconToMapL( const TDesC& aMbmFileName, TInt aMbmIndex,
       
    94     TInt aId, CCustomIconIdMap* aIconMap )
       
    95     {
       
    96     CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
    97     CleanupStack::PushL(bitmap);
       
    98     User::LeaveIfError( bitmap->Load( aMbmFileName, aMbmIndex ) );
       
    99     CGulIcon* icon = CGulIcon::NewL( bitmap ); // takes ownership of bitmap
       
   100     CleanupStack::Pop( bitmap );
       
   101     CleanupStack::PushL( icon );
       
   102     aIconMap->AppendL( icon, aId );            // takes ownership of icon
       
   103     CleanupStack::Pop( ); // icon
       
   104     }
       
   105 
       
   106 // ----------------------------------------------------------------------------
       
   107 // ESMRHelper::LocateResourceFile
       
   108 // ----------------------------------------------------------------------------
       
   109 //
       
   110 LOCAL_C TInt LocateResourceFile(
       
   111         const TDesC& aResource,
       
   112         const TDesC& aPath,
       
   113         TFileName &aResourceFile)
       
   114     {
       
   115     RFs* fsSession;
       
   116 
       
   117     fsSession=&CCoeEnv::Static()->FsSession();//codescanner::eikonenvstatic
       
   118 
       
   119     TFindFile resourceFile( *fsSession );
       
   120     TInt err = resourceFile.FindByDir(
       
   121             aResource,
       
   122             aPath );
       
   123 
       
   124     if ( KErrNone == err )
       
   125         {
       
   126         aResourceFile.Copy( resourceFile.File() );
       
   127         }
       
   128 
       
   129     return err;
       
   130     }
       
   131 
       
   132 // ----------------------------------------------------------------------------
       
   133 // CPbk2IconCustomApiTestPlugin::NewL
       
   134 // ----------------------------------------------------------------------------
       
   135 //
       
   136 CPbk2IconCustomApiTestPlugin* CPbk2IconCustomApiTestPlugin::NewL()
       
   137     {
       
   138     CPbk2IconCustomApiTestPlugin* self =
       
   139         new(ELeave)CPbk2IconCustomApiTestPlugin();
       
   140     CleanupStack::PushL( self );
       
   141     self->ConstructL();
       
   142     CleanupStack::Pop( self );
       
   143     return self;
       
   144     }
       
   145 
       
   146 // ----------------------------------------------------------------------------
       
   147 // CPbk2IconCustomApiTestPlugin::CPbk2IconCustomApiTestPlugin
       
   148 // ----------------------------------------------------------------------------
       
   149 //
       
   150 CPbk2IconCustomApiTestPlugin::CPbk2IconCustomApiTestPlugin()
       
   151     {
       
   152     }
       
   153 
       
   154 // ----------------------------------------------------------------------------
       
   155 // CPbk2IconCustomApiTestPlugin::~CPbk2IconCustomApiTestPlugin
       
   156 // ----------------------------------------------------------------------------
       
   157 //
       
   158 CPbk2IconCustomApiTestPlugin::~CPbk2IconCustomApiTestPlugin()
       
   159     {
       
   160     iObservers.Close();
       
   161     delete iTimer;
       
   162     }
       
   163 
       
   164 // ----------------------------------------------------------------------------
       
   165 // CPbk2IconCustomApiTestPlugin::ConstructL
       
   166 // ----------------------------------------------------------------------------
       
   167 //
       
   168 void CPbk2IconCustomApiTestPlugin::ConstructL()
       
   169     {
       
   170     TTime now;
       
   171     now.HomeTime();
       
   172     iSeed = now.Int64();
       
   173 
       
   174     // If the test case has defined icon update property, initialize
       
   175     // periodical timer for generating icon update events
       
   176     TInt psResult( EFalse );
       
   177     if( GetPsTestProperty( KPsKeyIconUpdaterEnabled, psResult ) == KErrNone &&
       
   178         psResult )
       
   179         {
       
   180         if( GetPsTestProperty(
       
   181             KPsKeyIconUpdaterIntervalMs, psResult ) == KErrNone )
       
   182             {
       
   183             iTimer = CPeriodic::NewL( CActive:: EPriorityStandard );
       
   184             TInt tickIntevalMicroseconds = 1000 * psResult;
       
   185             iTimer->Start( tickIntevalMicroseconds,
       
   186                 tickIntevalMicroseconds,
       
   187                 TCallBack( Tick, this) );
       
   188             }
       
   189         }
       
   190     }
       
   191 
       
   192 // ----------------------------------------------------------------------------
       
   193 // CPbk2IconCustomApiTestPlugin::AddObserverL
       
   194 // ----------------------------------------------------------------------------
       
   195 //
       
   196 void CPbk2IconCustomApiTestPlugin::AddObserverL(
       
   197     MCustomIconChangeObserver* aObserver )
       
   198     {
       
   199     iObservers.AppendL( aObserver );
       
   200     // Test that observer interface returns NULL extension for NULL UID.
       
   201     if( aObserver->IconChangeNotificationExtension( KNullUid ) )
       
   202         {
       
   203         User::Leave( KErrGeneral );
       
   204         }
       
   205     }
       
   206 
       
   207 // ----------------------------------------------------------------------------
       
   208 // CPbk2IconCustomApiTestPlugin::RemoveObserver
       
   209 // ----------------------------------------------------------------------------
       
   210 //
       
   211 void CPbk2IconCustomApiTestPlugin::RemoveObserver(
       
   212     MCustomIconChangeObserver* aObserver )
       
   213     {
       
   214     TInt index = iObservers.Find( aObserver );
       
   215     if( index > 0 )
       
   216         {
       
   217         iObservers.Remove( index );
       
   218         }
       
   219     }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // CPbk2IconCustomApiTestPlugin::IconsL
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 CCustomIconIdMap* CPbk2IconCustomApiTestPlugin::IconsL(
       
   226     TUid /*aContext*/,
       
   227     TIconSizeHint /*aSizeHint*/ )
       
   228     {
       
   229 
       
   230     CCustomIconIdMap* iconMap = new( ELeave )CCustomIconIdMap( 2 );
       
   231     CleanupStack::PushL( iconMap );
       
   232     
       
   233     AppendIconToMapL( KIconFile, 0, 653, iconMap );
       
   234     AppendIconToMapL( KIconFile, 1, 145, iconMap );
       
   235 
       
   236     CleanupStack::Pop( iconMap );
       
   237     return iconMap;
       
   238     }
       
   239 
       
   240 // ----------------------------------------------------------------------------
       
   241 // CPbk2IconCustomApiTestPlugin::IconChoiceL
       
   242 // ----------------------------------------------------------------------------
       
   243 //
       
   244 TBool CPbk2IconCustomApiTestPlugin::IconChoiceL(
       
   245     const CCustomIconDecisionData& aHint, TInt& aIdResult )
       
   246     {
       
   247     ValidateHintL( aHint );
       
   248     TInt random = Math::Rand( iSeed ) % 2;
       
   249     if( random == 0)
       
   250         {
       
   251         aIdResult = 653;
       
   252         }
       
   253     else
       
   254         {
       
   255         aIdResult = 145;
       
   256         }
       
   257     return ETrue;
       
   258     }
       
   259 
       
   260 // ----------------------------------------------------------------------------
       
   261 // CPbk2IconCustomApiTestPlugin::IconL
       
   262 // ----------------------------------------------------------------------------
       
   263 //
       
   264 CGulIcon* CPbk2IconCustomApiTestPlugin::IconL(
       
   265     const CCustomIconDecisionData& aHint, TIconSizeHint /*aSizeHint*/ )
       
   266     {
       
   267     ValidateHintL( aHint );
       
   268     CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   269     CleanupStack::PushL( bitmap );
       
   270     TInt context = aHint.Context().iUid;
       
   271     const TTime* time = aHint.Time();
       
   272     // Icon depends on context param & time, here simply whether they
       
   273     // has bee defined..
       
   274     if( context || time )
       
   275         {
       
   276         User::LeaveIfError( bitmap->Load( KIconFile, 0 ) );
       
   277         }
       
   278     else
       
   279         {
       
   280         User::LeaveIfError( bitmap->Load( KIconFile, 1 ) );
       
   281         }
       
   282     CGulIcon* icon = CGulIcon::NewL( bitmap ); // takes ownership of bitmap
       
   283     CleanupStack::Pop( bitmap );
       
   284     return icon;
       
   285     }
       
   286 
       
   287 // ----------------------------------------------------------------------------
       
   288 // CPbk2IconCustomApiTestPlugin::IconL
       
   289 // ----------------------------------------------------------------------------
       
   290 //
       
   291 CGulIcon* CPbk2IconCustomApiTestPlugin::IconL( TUid /*aContext*/,
       
   292     TCustomIconId aIconId,
       
   293     TIconSizeHint /*aSizeHint*/ )
       
   294     {
       
   295     TInt mbmIndex = KErrNotFound;
       
   296     if( aIconId == 653 )
       
   297         {
       
   298         mbmIndex = 0;
       
   299         }
       
   300     else if( aIconId == 145 )
       
   301         {
       
   302         mbmIndex = 1;
       
   303         }
       
   304     CGulIcon* icon = NULL;
       
   305     if( mbmIndex != KErrNotFound )
       
   306         {
       
   307         CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   308         CleanupStack::PushL( bitmap );
       
   309         
       
   310         User::LeaveIfError( bitmap->Load( KIconFile, 0 ) );
       
   311         icon = CGulIcon::NewL( bitmap ); // takes ownership of bitmap
       
   312         CleanupStack::Pop( bitmap );
       
   313         }
       
   314     return icon;
       
   315     }
       
   316 
       
   317 // ----------------------------------------------------------------------------
       
   318 // CPbk2IconCustomApiTestPlugin::Tick
       
   319 // ----------------------------------------------------------------------------
       
   320 //
       
   321 TInt CPbk2IconCustomApiTestPlugin::Tick( TAny* aObject )
       
   322     {
       
   323     // cast, and call non-static function
       
   324     TRAP_IGNORE(((CPbk2IconCustomApiTestPlugin*)aObject)->NotifyObserversL();)
       
   325     return ETrue; // continue ticking
       
   326     }
       
   327 
       
   328 // ----------------------------------------------------------------------------
       
   329 // CPbk2IconCustomApiTestPlugin::NotifyObserversL
       
   330 // ----------------------------------------------------------------------------
       
   331 //
       
   332 void CPbk2IconCustomApiTestPlugin::NotifyObserversL()
       
   333     {
       
   334     TInt i;
       
   335     TInt count = iObservers.Count();
       
   336     MCustomIconChangeObserver* observer;
       
   337     for( i=0; i<count; i++ )
       
   338         {
       
   339         observer = iObservers[i];
       
   340         if( observer )
       
   341             {
       
   342             // Setup notification parameters from the values defined by the
       
   343             // Publish/Subscribe system (test case being the definer)
       
   344             // If values not found, use default values
       
   345             TInt eventType( MCustomIconChangeObserver::EIconsMajorChange );
       
   346             TBool emptyArray( ETrue );
       
   347             GetPsTestProperty( KPsKeyNotificationEventType, eventType );
       
   348             GetPsTestProperty( KPsKeyNotificationEmptyUidArray, emptyArray );
       
   349 
       
   350             RArray<TCustomIconId> array;
       
   351             CleanupClosePushL( array );
       
   352             if( !emptyArray )
       
   353                 {
       
   354                 array.AppendL( TCustomIconId( 653 ) );
       
   355                 array.AppendL( TCustomIconId( 145 ) );
       
   356                 }
       
   357             observer->CustomIconEvent( *this,
       
   358                 (MCustomIconChangeObserver::TEventType)eventType,
       
   359                 array.Array() );
       
   360 
       
   361             CleanupStack::PopAndDestroy( &array );
       
   362             }
       
   363         }
       
   364     }
       
   365 
       
   366 // End of file
       
   367 
       
   368