meetingrequest/mrpolicy/src/cmrpolicyresourceiterator.cpp
branchRCL_3
changeset 12 4ce476e64c59
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2 * Copyright (c) 2007-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:  ESMR policymmanager implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "cmrpolicyresourceiterator.h"
       
    21 //<cmail>
       
    22 #include "cesmrpolicy.h"
       
    23 #include <esmrpolicies.rsg>
       
    24 #include "esmrdef.h"
       
    25 //</cmail>
       
    26 #include "esmrinternaluid.h"
       
    27 #include "cesmrcalimportexporter.h"
       
    28 #include "tesmrentryfield.h"
       
    29 
       
    30 #include <coemain.h>// CCoeEnv
       
    31 #include <barsread.h>
       
    32 
       
    33 
       
    34 /// Unnamed namespace for local definitions
       
    35 namespace {
       
    36 
       
    37 // Definition for resource id array granularity
       
    38 const TInt KMRPolicyIdArrayGranularity = 4;
       
    39 
       
    40 /**
       
    41  * Reads ESMR policy resource id table from resource.
       
    42  *
       
    43  * @param aReader Reference to resource reader.
       
    44  * @param aResourceIdTable Reference to resource id table.
       
    45  */
       
    46 void ReadResourceIdArrayL(
       
    47         TResourceReader& aReader,
       
    48         RArray<TInt>& aResourceIdTable )
       
    49     {
       
    50     TInt numOfFields = aReader.ReadInt16();
       
    51     aResourceIdTable.ReserveL( numOfFields );
       
    52     
       
    53     for (TInt i = 0; i < numOfFields; ++i )
       
    54         {
       
    55         TInt resourceId= aReader.ReadInt32();
       
    56         aResourceIdTable.AppendL(resourceId);
       
    57         }
       
    58     }
       
    59 
       
    60 #ifdef _DEBUG
       
    61 
       
    62 _LIT( KPanicCategory, "MR Policy Resource Iterator" );
       
    63 
       
    64 enum TMRPolicyResourceIteratorPanic
       
    65     {
       
    66     EMRPolicyIteratorExhausted = 0
       
    67     };
       
    68 
       
    69 void Panic( TMRPolicyResourceIteratorPanic aPanic )
       
    70     {
       
    71     User::Panic( KPanicCategory, aPanic );
       
    72     }
       
    73 
       
    74 #endif
       
    75 }  // namespace
       
    76 
       
    77 // ======== MEMBER FUNCTIONS ========
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CMRPolicyResourceIterator::CMRPolicyResourceIterator
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 inline CMRPolicyResourceIterator::CMRPolicyResourceIterator()
       
    84     : iPolicyResourceIds( KMRPolicyIdArrayGranularity ),
       
    85       iArrayIndex( KErrNotFound )
       
    86     {
       
    87     FUNC_LOG;
       
    88     //do nothing
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CMRPolicyResourceIterator::~CMRPolicyResourceIterator
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C CMRPolicyResourceIterator::~CMRPolicyResourceIterator()
       
    96     {
       
    97     FUNC_LOG;
       
    98     iPolicyResourceFile.Close();
       
    99     iPolicyResourceIds.Close();
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CMRPolicyResourceIterator::NewL
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 EXPORT_C CMRPolicyResourceIterator* CMRPolicyResourceIterator::NewL(
       
   107         const TDesC& aResourceFile,
       
   108         TInt aResourceId )
       
   109     {
       
   110     FUNC_LOG;
       
   111 
       
   112     CMRPolicyResourceIterator* self = new (ELeave) CMRPolicyResourceIterator;
       
   113     CleanupStack::PushL( self );
       
   114     self->ConstructL( aResourceFile,
       
   115                       aResourceId );
       
   116     CleanupStack::Pop(self);
       
   117 
       
   118     return self;
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // CMRPolicyResourceIterator::ConstructL
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 void CMRPolicyResourceIterator::ConstructL(
       
   126         const TDesC& aResourceFile,
       
   127         TInt aResourceId)
       
   128     {
       
   129     FUNC_LOG;
       
   130     ReadPolicyFromResourceL( aResourceFile, aResourceId );
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CMRPolicyResourceIterator::HasNext
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 EXPORT_C TBool CMRPolicyResourceIterator::HasNext() const
       
   138     {
       
   139     return ( iArrayIndex < iPolicyResourceIds.Count() - 1 );
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CMRPolicyResourceIterator::NextPolicyL
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 EXPORT_C CESMRPolicy* CMRPolicyResourceIterator::NextPolicyL()
       
   147     {
       
   148     FUNC_LOG;
       
   149     
       
   150     CESMRPolicy* policy = NextPolicyLC();
       
   151     CleanupStack::Pop( policy );
       
   152     
       
   153     return policy;
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // CMRPolicyResourceIterator::NextPolicyLC
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 EXPORT_C CESMRPolicy* CMRPolicyResourceIterator::NextPolicyLC()
       
   161     {
       
   162     FUNC_LOG;
       
   163 
       
   164     __ASSERT_DEBUG( HasNext(), Panic( EMRPolicyIteratorExhausted ) );
       
   165     
       
   166     CESMRPolicy* policy = ReadPolicyLC( iPolicyResourceIds[ ++iArrayIndex ] );
       
   167     
       
   168     return policy;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CMRPolicyResourceIterator::Reset
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C void CMRPolicyResourceIterator::Reset()
       
   176     {
       
   177     FUNC_LOG;
       
   178     
       
   179     iArrayIndex = KErrNotFound;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CMRPolicyResourceIterator::ReadPolicyFromResourceL
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 void CMRPolicyResourceIterator::ReadPolicyFromResourceL(
       
   187         const TDesC& aPolicyFile,
       
   188         TInt aPolicyArrayResourceId )
       
   189     {
       
   190     FUNC_LOG;
       
   191 
       
   192     iPolicyResourceFile.Close();
       
   193     iPolicyResourceFile.OpenL( CCoeEnv::Static()->FsSession(), aPolicyFile );
       
   194     iPolicyResourceFile.ConfirmSignatureL();
       
   195 
       
   196     // R_ESMR_POLICIES
       
   197     HBufC8* resourceIdBuffer =
       
   198         iPolicyResourceFile.AllocReadLC( aPolicyArrayResourceId );
       
   199 
       
   200     // Construct resource reader
       
   201     TResourceReader reader;
       
   202     reader.SetBuffer(resourceIdBuffer);
       
   203 
       
   204     // Read policy resource ids
       
   205     iPolicyResourceIds.Reset();
       
   206     ReadResourceIdArrayL(reader, iPolicyResourceIds );
       
   207 
       
   208     CleanupStack::PopAndDestroy( resourceIdBuffer );
       
   209 
       
   210     }
       
   211 
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // CMRPolicyResourceIterator::ReadPolicyL
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 CESMRPolicy* CMRPolicyResourceIterator::ReadPolicyLC( TInt aResourceId )
       
   218     {
       
   219     FUNC_LOG;
       
   220 
       
   221     HBufC8* resourceBuffer = iPolicyResourceFile.AllocReadLC( aResourceId );
       
   222     
       
   223     TResourceReader reader;
       
   224     reader.SetBuffer(resourceBuffer);
       
   225     
       
   226     CESMRPolicy* policy = CESMRPolicy::NewL( iPolicyResourceFile, reader );
       
   227     
       
   228     CleanupStack::PopAndDestroy( resourceBuffer );
       
   229     CleanupStack::PushL( policy );
       
   230     
       
   231     return policy;
       
   232     }
       
   233 
       
   234 // EOF
       
   235