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