emailservices/emailclientapi/src/emailinterfacefactoryimpl.cpp
changeset 54 997a02608b3a
child 62 a8c646b56683
equal deleted inserted replaced
53:bf7eb7911fc5 54:997a02608b3a
       
     1 /*
       
     2 * Copyright (c) 2010 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: This file implements class CEmailInterfaceFactoryImpl.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32property.h>                // RProperty
       
    19 #include <s32mem.h>                     // RDesRead/WriteStream
       
    20 
       
    21 #include "emailinterfacefactoryimpl.h"
       
    22 #include "emailcontent.h"
       
    23 #include "cfsmailclient.h"
       
    24 #include "emailclientapiimpldefs.h"
       
    25 #include "emailclientapiimpl.h"
       
    26 #include "emailaddress.h"
       
    27 #include "emailmessagesearch.h"
       
    28 #include "emailshutdownconst.h"
       
    29 
       
    30 _LIT( KEmailImplPanic, "Email client API" );
       
    31 const TInt KEmailUidExtraBuffer = 2 * KEmailPlatformApiUidItemSize;
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // Email client API panic wrapper
       
    35 // ---------------------------------------------------------------------------
       
    36 void Panic( TEmailImplPanic aPanic )
       
    37     {
       
    38     User::Panic( KEmailImplPanic(), aPanic );
       
    39     }
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CEmailInterfaceFactoryImpl::NewL
       
    45 // ---------------------------------------------------------------------------
       
    46 CEmailInterfaceFactoryImpl* CEmailInterfaceFactoryImpl::NewL()
       
    47     {
       
    48     CEmailInterfaceFactoryImpl* self = new (ELeave) CEmailInterfaceFactoryImpl();
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop( self );
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // CEmailInterfaceFactoryImpl::~CEmailInterfaceFactoryImpl
       
    57 // ---------------------------------------------------------------------------
       
    58 CEmailInterfaceFactoryImpl::~CEmailInterfaceFactoryImpl()
       
    59     {
       
    60     TRAP_IGNORE( AppendOrRemoveUidL( EEmailUidModeRemove ) );
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CEmailInterfaceFactoryImpl::CEmailInterfaceFactoryImpl
       
    65 // ---------------------------------------------------------------------------
       
    66 CEmailInterfaceFactoryImpl::CEmailInterfaceFactoryImpl() : 
       
    67     CEmailInterfaceFactory()
       
    68     {
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CEmailInterfaceFactoryImpl::ConstructL
       
    73 // ---------------------------------------------------------------------------
       
    74 void CEmailInterfaceFactoryImpl::ConstructL()
       
    75     {
       
    76 	// This leaves if related P&S keys are not defined by EmailServerMonitor,
       
    77 	// so EmailServerMonitor need to be started before using client API.
       
    78 	// TRAP_IGNORE should be removed after EmailServerMonitor is added to
       
    79 	// starter list.
       
    80     TRAP_IGNORE( AppendOrRemoveUidL( EEmailUidModeAppend ) );
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CEmailInterfaceFactoryImpl::InterfaceL
       
    85 // ---------------------------------------------------------------------------
       
    86 MEmailInterface* CEmailInterfaceFactoryImpl::InterfaceL( const TInt aInterfaceId )
       
    87     {
       
    88     MEmailInterface* interface = NULL;
       
    89     switch ( aInterfaceId )
       
    90         {
       
    91         case KEmailClientApiInterface:
       
    92             interface = CEmailClientApi::NewL();
       
    93             break;
       
    94         case KEmailIFUidAddress:
       
    95             interface = CEmailAddress::NewL( MEmailAddress::EUndefined, EClientOwns );
       
    96             break;
       
    97         case KEmailIFUidTextContent:
       
    98         default:
       
    99             break;
       
   100         }
       
   101     if ( !interface )
       
   102         {
       
   103         User::Leave( KErrNotSupported );
       
   104         }
       
   105     return interface;
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CEmailInterfaceFactoryImpl::AppendOrRemoveUidL
       
   110 // ---------------------------------------------------------------------------
       
   111 void CEmailInterfaceFactoryImpl::AppendOrRemoveUidL(
       
   112         const TEmailUidAppendRemoveMode aMode )
       
   113     {
       
   114     // Read buffer length
       
   115     TInt bufLength( 0 );
       
   116     User::LeaveIfError( RProperty::Get( KEmailShutdownPsCategory,
       
   117                                         EEmailPsKeyPlatformApiAppsToCloseLength,
       
   118                                         bufLength ) );
       
   119 
       
   120     // Allocate buffer for reading and then read the list of UIDs from P&S.
       
   121     // Adding some extra buffer just in case the size key and actual list
       
   122     // are out of sync. This shouldn't happen, but you never know.
       
   123     HBufC8* readBuf = HBufC8::NewLC( bufLength + KEmailUidExtraBuffer );
       
   124     TPtr8 readPtr = readBuf->Des();
       
   125     
       
   126     User::LeaveIfError( RProperty::Get( KEmailShutdownPsCategory,
       
   127                                         EEmailPsKeyPlatformApiAppsToClose,
       
   128                                         readPtr ) );
       
   129     
       
   130     // For writing get the size of the original buffer + room for our own UID
       
   131     // if needed
       
   132     TInt writeBufSize = readPtr.Length();
       
   133     if( aMode == EEmailUidModeAppend )
       
   134         {
       
   135         writeBufSize += KEmailPlatformApiUidItemSize;
       
   136         }
       
   137     
       
   138     HBufC8* writeBuf = HBufC8::NewLC( writeBufSize );
       
   139     TPtr8 writePtr = writeBuf->Des();
       
   140 
       
   141     // Read and write streams used to read/write the UIDs from/to descriptors
       
   142     RDesReadStream readStream( readPtr );
       
   143     CleanupClosePushL( readStream );
       
   144 
       
   145     RDesWriteStream writeStream( writePtr );
       
   146     CleanupClosePushL( writeStream );
       
   147 
       
   148     // Get our own process UID
       
   149     RProcess ownProcess;
       
   150     TUid ownUid = ownProcess.SecureId();
       
   151     ownProcess.Close();
       
   152 
       
   153     TInt itemsCount = readPtr.Length() / KEmailPlatformApiUidItemSize;
       
   154 
       
   155     TBool ownUidFound = EFalse;
       
   156     TInt writeLength = 0;
       
   157     for ( TInt ii = 0;ii < itemsCount; ++ii )
       
   158         {
       
   159         // Read next UID from the stream
       
   160         TUid item = TUid::Uid( readStream.ReadInt32L() );
       
   161         
       
   162         // We can skip our own UID. If we are removing, then we don't want
       
   163         // our UID to be written. If we are adding, we don't need to set
       
   164         // the new values as our UID already exists in the list.
       
   165         if( item == ownUid )
       
   166             {
       
   167             ownUidFound = ETrue;
       
   168             if( aMode == EEmailUidModeAppend )
       
   169                 {
       
   170                 // Our own UID is already in the list, so no need to update
       
   171                 // the list. Hence we can quit here.
       
   172                 break;
       
   173                 }
       
   174             }
       
   175         else
       
   176             {
       
   177             writeStream.WriteInt32L( item.iUid );
       
   178             writeLength += KEmailPlatformApiUidItemSize;
       
   179             }
       
   180         }
       
   181 
       
   182     // If we are appending our UID and it wasn't found from the list,
       
   183     // write it to the stream
       
   184     if( aMode == EEmailUidModeAppend && !ownUidFound )
       
   185         {
       
   186         writeStream.WriteInt32L( ownUid.iUid );
       
   187         writeLength += KEmailPlatformApiUidItemSize;
       
   188         }
       
   189 
       
   190     // Set correct length for the write ptr buffer as it might not be
       
   191     // updated correctly by the write stream
       
   192     writePtr.SetLength( writeLength );
       
   193 
       
   194     // Set new values to P&S only if something has changed, so either:
       
   195     // 1) We are appending our UID and it didn't exist before
       
   196     // 2) We are removing our UID and it did exist before
       
   197     if( ( aMode == EEmailUidModeAppend && !ownUidFound ) ||
       
   198         ( aMode == EEmailUidModeRemove && ownUidFound ) )
       
   199         {
       
   200         // Write first the UID list as it is more probable to fail, writing
       
   201         // plain integer value shouldn't fail in any case. This way these
       
   202         // values stay in sync also in case of error, as the list length
       
   203         // gets updated only if the list itself is updated succesfully.
       
   204         User::LeaveIfError( RProperty::Set( KEmailShutdownPsCategory,
       
   205                         EEmailPsKeyPlatformApiAppsToClose,
       
   206                         writePtr ) );
       
   207 
       
   208         User::LeaveIfError( RProperty::Set( KEmailShutdownPsCategory,
       
   209                         EEmailPsKeyPlatformApiAppsToCloseLength,
       
   210                         writeLength ) );
       
   211         }
       
   212 
       
   213     CleanupStack::PopAndDestroy( 4, readBuf );
       
   214     }
       
   215 
       
   216 // End of file