meetingrequest/mrgui/mrfieldbuilderpluginextension/src/resmrpluginextensionstatic.cpp
branchRCL_3
changeset 64 3533d4323edc
equal deleted inserted replaced
63:d189ee25cf9d 64:3533d4323edc
       
     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:  Static Tls data storage
       
    15  *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include "resmrpluginextensionstatic.h"
       
    20 #include "cmrmailboxutils.h"
       
    21 #include "esmrhelper.h"
       
    22 #include <ct/rcpointerarray.h>
       
    23 
       
    24 // <cmail> Removed profiling. </cmail>
       
    25 
       
    26 /**
       
    27  * Storage class for RESMRStatic.
       
    28  */
       
    29 class TESMRPluginExtensionData
       
    30     {
       
    31 public:
       
    32     TInt iInstanceCount;
       
    33     TInt iFSMailBoxCount;
       
    34     CFSMailClient* iFSMailClient;
       
    35     CFSMailBox* iDefaultFSMailBox;
       
    36     };
       
    37 
       
    38 // Unnamed namespace for local definitions, structs and functions
       
    39 namespace { // codescanner::namespace
       
    40 
       
    41 const TInt KMailBox = 0x01;
       
    42 
       
    43 // Definition for equal string
       
    44 const TInt KEqualString(0);
       
    45 
       
    46 }//namespace
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // RESMRPluginExtensionStatic::RESMRPluginExtensionStatic
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 RESMRPluginExtensionStatic::RESMRPluginExtensionStatic( )
       
    53     {
       
    54     FUNC_LOG;
       
    55     iStaticData = NULL;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // RESMRStatic::~RESMRStatic
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 RESMRPluginExtensionStatic::~RESMRPluginExtensionStatic( )
       
    63     {
       
    64     FUNC_LOG;
       
    65     Close();
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // RESMRPluginExtensionStatic::ConnectL
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void RESMRPluginExtensionStatic::ConnectL( )
       
    73     {
       
    74     FUNC_LOG;
       
    75     if (iStaticData )
       
    76         {
       
    77         return;
       
    78         }
       
    79 
       
    80     // Retrieve Tls pointer
       
    81     iStaticData = static_cast<TESMRPluginExtensionData*>( Dll::Tls());
       
    82 
       
    83     // If Tls pointer was not set, create new static stuct
       
    84     // with NULL values
       
    85     if (!iStaticData)
       
    86         {
       
    87         iStaticData = new (ELeave) TESMRPluginExtensionData();
       
    88         memset ( iStaticData, 0, sizeof( TESMRPluginExtensionData) );
       
    89         Dll::SetTls ( iStaticData );
       
    90         }
       
    91     ++iStaticData->iInstanceCount;
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // RESMRPluginExtensionStatic::Close
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void RESMRPluginExtensionStatic::Close( )
       
    99     {
       
   100     FUNC_LOG;
       
   101     if (iStaticData )
       
   102         {
       
   103 
       
   104         // If FSMailClientL or DefaultFSMailBox was used
       
   105         if (iUsedTypes & KMailBox )
       
   106             {
       
   107             // Decrease counter
       
   108             --iStaticData->iFSMailBoxCount;
       
   109 
       
   110             // If this was last instance using pointer
       
   111             if (iStaticData->iFSMailBoxCount == 0 )
       
   112                 {
       
   113                 // Owned by CFSMailClient
       
   114                 delete iStaticData->iDefaultFSMailBox;
       
   115                 iStaticData->iDefaultFSMailBox = NULL;
       
   116 
       
   117                 // Delete instance
       
   118                 if ( iStaticData->iFSMailClient )
       
   119                     {
       
   120                     iStaticData->iFSMailClient->Close();
       
   121                     iStaticData->iFSMailClient = NULL;
       
   122                     }
       
   123                 }
       
   124             }
       
   125 
       
   126         // Decrease instance counter
       
   127         --iStaticData->iInstanceCount;
       
   128 
       
   129         // If this was last instance using pointer
       
   130         if (iStaticData->iInstanceCount == 0 )
       
   131             {
       
   132             if ( iStaticData->iDefaultFSMailBox )
       
   133                 {
       
   134                 delete iStaticData->iDefaultFSMailBox;
       
   135                 iStaticData->iDefaultFSMailBox = NULL;
       
   136                 }
       
   137 
       
   138             if (iStaticData->iFSMailClient)
       
   139                 {
       
   140                 iStaticData->iFSMailClient->Close();
       
   141                 iStaticData->iFSMailClient = NULL;
       
   142                 }
       
   143 
       
   144             delete iStaticData;
       
   145             Dll::FreeTls ( );
       
   146             }
       
   147         iStaticData = NULL;
       
   148         }
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // RESMRPluginExtensionStatic::DefaultFSMailBoxL
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 CFSMailBox& RESMRPluginExtensionStatic::DefaultFSMailBoxL()
       
   156     {
       
   157     FUNC_LOG;
       
   158     if ( !iStaticData->iDefaultFSMailBox )
       
   159         {
       
   160         CFSMailClient& fsMailClient = FSMailClientL();
       
   161 
       
   162         CMRMailboxUtils* mbUtils =
       
   163                 CMRMailboxUtils::NewL();
       
   164         CleanupStack::PushL( mbUtils );
       
   165 
       
   166         CMRMailboxUtils::TMailboxInfo mbInfo;
       
   167         User::LeaveIfError(
       
   168                 mbUtils->GetDefaultMRMailBoxL( mbInfo ) );
       
   169 
       
   170         // Loop throug all mailboxes in this plug-in
       
   171         RCPointerArray<CFSMailBox> mailboxes;
       
   172         CleanupClosePushL( mailboxes );
       
   173         
       
   174         // With null uid we get all mailboxes
       
   175         TFSMailMsgId msgId;
       
   176         fsMailClient.ListMailBoxes(
       
   177                 msgId, mailboxes );
       
   178 
       
   179         TInt mailboxCount( mailboxes.Count() );
       
   180         for ( TInt j(0);
       
   181               j < mailboxCount && !iStaticData->iDefaultFSMailBox;
       
   182               ++j )
       
   183             {
       
   184             TPtrC mailboxOwnerAddName(
       
   185                     mailboxes[j]->OwnMailAddress().GetEmailAddress() );
       
   186             if ( KEqualString == mailboxOwnerAddName.Compare(mbInfo.iEmailAddress) )
       
   187                 {
       
   188                 // Correct mailbox is found
       
   189                 iStaticData->iDefaultFSMailBox = mailboxes[j];
       
   190                 mailboxes.Remove(j);
       
   191                 }
       
   192             }
       
   193 
       
   194         CleanupStack::PopAndDestroy( 2, mbUtils ); // mailboxes
       
   195         mbUtils = NULL;
       
   196 
       
   197         if ( !iStaticData->iDefaultFSMailBox )
       
   198             {
       
   199             User::Leave( KErrAbort );
       
   200             }
       
   201         }
       
   202 
       
   203     iUsedTypes |= KMailBox;
       
   204     ++iStaticData->iFSMailBoxCount;
       
   205     return *iStaticData->iDefaultFSMailBox;
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // RESMRPluginExtensionStatic::MailBoxL
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 CFSMailBox* RESMRPluginExtensionStatic::MailBoxL( const TDesC& aEmailAddress )
       
   213     {
       
   214     TPtrC address( ESMRHelper::AddressWithoutMailtoPrefix( aEmailAddress ) );
       
   215     CFSMailClient& fsMailClient = FSMailClientL();
       
   216 
       
   217     // Loop throug all mailboxes in this plug-in
       
   218     RCPointerArray<CFSMailBox> mailboxes;
       
   219     CleanupClosePushL( mailboxes );
       
   220 
       
   221     // With null uid we get all mailboxes
       
   222     TFSMailMsgId msgId;
       
   223     fsMailClient.ListMailBoxes(
       
   224             msgId, mailboxes );
       
   225 
       
   226     TInt mailboxCount( mailboxes.Count() );
       
   227     CFSMailBox* mailBox = NULL;
       
   228     
       
   229     for ( TInt i(0); i < mailboxCount && !mailBox; ++i )
       
   230         {
       
   231         TPtrC mailboxOwnerAddName(
       
   232                 mailboxes[i]->OwnMailAddress().GetEmailAddress() );
       
   233         if ( mailboxOwnerAddName.CompareF( address ) == 0 )
       
   234             {
       
   235             // Correct mailbox is found
       
   236             mailBox = mailboxes[ i ];
       
   237             mailboxes.Remove( i );
       
   238             }
       
   239         }
       
   240     CleanupStack::PopAndDestroy( &mailboxes );
       
   241 
       
   242     if ( !mailBox )
       
   243         {
       
   244         User::Leave( KErrNotFound );
       
   245         }
       
   246     
       
   247     return mailBox;
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // RESMRPluginExtensionStatic::FSMailClientL
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 CFSMailClient& RESMRPluginExtensionStatic::FSMailClientL( )
       
   255     {
       
   256     FUNC_LOG;
       
   257     if (!iStaticData->iFSMailClient)
       
   258         {
       
   259         iStaticData->iFSMailClient = CFSMailClient::NewL ( );
       
   260         }
       
   261 
       
   262     return *iStaticData->iFSMailClient;
       
   263     }
       
   264 
       
   265 // EOF
       
   266