meetingrequest/mrentry/src/cesmrfsmailboxutils.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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 FS mailbox utilities implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "cesmrfsmailboxutils.h"
       
    21 #include "esmrhelper.h"
       
    22 #include "esmrinternaluid.h"
       
    23 
       
    24 #include <calentry.h>
       
    25 #include "CFSMailClient.h"
       
    26 #include "CFSMailBox.h"
       
    27 #include <caluser.h>
       
    28 
       
    29 // Unnamed namespace for local definitions
       
    30 namespace {
       
    31 
       
    32 // Definition for email address comparison
       
    33 const TInt KEqualEmailAddress( 0 );
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // Cleanup operation for RPointerArray.
       
    37 // @param aArray Pointer to RPointerArray.
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 void MailboxPointerArrayCleanup( TAny* aArray )
       
    41     {
       
    42     RPointerArray<CFSMailBox>* entryArray =
       
    43         static_cast<RPointerArray<CFSMailBox>*>( aArray );
       
    44 
       
    45     entryArray->ResetAndDestroy();
       
    46     entryArray->Close();
       
    47     }
       
    48 
       
    49 /**
       
    50  * Listes all FS mailboxes. On return aMailboxes contains FS mailboxes
       
    51  * @param aMailClient FS mail client
       
    52  * @aMailboxes Reference to mailboxes
       
    53  */
       
    54 void ListMailBoxesL(
       
    55         CFSMailClient& aMailClient,
       
    56         RPointerArray<CFSMailBox>& aMailboxes )
       
    57     {
       
    58     TFSMailMsgId msgId;
       
    59     aMailClient.ListMailBoxes(
       
    60             msgId,
       
    61             aMailboxes );
       
    62     }
       
    63 }
       
    64 
       
    65 // ======== MEMBER FUNCTIONS ========
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CESMRFsMailboxUtils::CESMRFsMailboxUtils
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 inline CESMRFsMailboxUtils::CESMRFsMailboxUtils(
       
    72         CMRMailboxUtils& aMailboxUtils )
       
    73 :   iMRMailboxUtils( aMailboxUtils )
       
    74     {
       
    75     FUNC_LOG;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // CESMRFsMailboxUtils::~CESMRFsMailboxUtils
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CESMRFsMailboxUtils::~CESMRFsMailboxUtils()
       
    83     {
       
    84     FUNC_LOG;
       
    85     if ( iMailClient )
       
    86         {
       
    87         iMailClient->Close();
       
    88         }
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CESMRFsMailboxUtils::NewL
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 CESMRFsMailboxUtils* CESMRFsMailboxUtils::NewL(
       
    96             CMRMailboxUtils& aMailboxUtils )
       
    97     {
       
    98     FUNC_LOG;
       
    99     CESMRFsMailboxUtils* self =
       
   100             new (ELeave) CESMRFsMailboxUtils( aMailboxUtils );
       
   101     CleanupStack::PushL( self );
       
   102     self->ConstructL();
       
   103     CleanupStack::Pop( self );
       
   104     return self;
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CESMRFsMailboxUtils::ConstructL
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void CESMRFsMailboxUtils::ConstructL()
       
   112     {
       
   113     FUNC_LOG;
       
   114     // Do nothing
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CESMRFsMailboxUtils::SetPhoneOwnerL
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 TInt CESMRFsMailboxUtils::SetPhoneOwnerL(
       
   122             CCalEntry& aCalEntry,
       
   123             TMsvId aPrimaryBox )
       
   124     {
       
   125     FUNC_LOG;
       
   126     TInt err( iMRMailboxUtils.SetPhoneOwnerL( aCalEntry, aPrimaryBox ) );
       
   127 
       
   128     // Check that phone owner was set correctly
       
   129     if ( err == KErrNotFound )
       
   130         {
       
   131         // Check if phone owner can be set using FS mailboxes
       
   132         // Loop throug all mailboxes in this plug-in
       
   133         RPointerArray<CFSMailBox> mailboxes;
       
   134         CleanupStack::PushL(
       
   135                 TCleanupItem(
       
   136                         MailboxPointerArrayCleanup,
       
   137                         &mailboxes    ) );
       
   138 
       
   139         ListMailBoxesL(
       
   140                 MailClientL(),
       
   141                 mailboxes );
       
   142 
       
   143         TInt dummyIndex( -1 ); // don't care
       
   144         CCalUser* phoneOwner = PhoneOwnerL( aCalEntry, mailboxes, dummyIndex );
       
   145 
       
   146         if ( phoneOwner )
       
   147             {
       
   148             aCalEntry.SetPhoneOwnerL( phoneOwner );
       
   149             err = KErrNone;
       
   150             }
       
   151         CleanupStack::PopAndDestroy( &mailboxes );
       
   152         }
       
   153     return err;
       
   154     }
       
   155 
       
   156 // ----------------------------------------------------------------------------
       
   157 // CESMRFsMailboxUtils::SetPhoneOwnerL
       
   158 // ----------------------------------------------------------------------------
       
   159 //
       
   160 TInt CESMRFsMailboxUtils::SetPhoneOwnerL(
       
   161     CCalEntry& aCalEntry,
       
   162     CFSMailClient& aMailClient,
       
   163     CFSMailMessage& aMailMessage )
       
   164     {
       
   165     FUNC_LOG;
       
   166     TInt err( KErrNotFound );
       
   167     
       
   168     TFSMailMsgId mailboxId( aMailMessage.GetMailBoxId() );
       
   169     CFSMailBox* mailbox = aMailClient.GetMailBoxByUidL( mailboxId );
       
   170 
       
   171     if ( !mailbox )
       
   172         {
       
   173         return KErrNotFound;
       
   174         }
       
   175     
       
   176     CleanupStack::PushL( mailbox );
       
   177 
       
   178     TPtrC mailboxOwnerAddName(
       
   179             mailbox->OwnMailAddress().GetEmailAddress() );
       
   180     
       
   181     CCalUser* po = NULL;
       
   182     CCalUser* organizer = aCalEntry.OrganizerL();
       
   183 
       
   184     if ( organizer &&  
       
   185          KEqualEmailAddress == organizer->Address().CompareF(mailboxOwnerAddName) )
       
   186         {
       
   187         po = organizer;
       
   188         }
       
   189     else
       
   190         {
       
   191         RPointerArray<CCalAttendee>& attendees = aCalEntry.AttendeesL();
       
   192         TInt attendeeCount( attendees.Count() );
       
   193 
       
   194         for( TInt i(0); i < attendeeCount && !po; ++i )
       
   195             {
       
   196             CCalAttendee* attendee = attendees[i];
       
   197             TPtrC attendeeAddr( attendee->Address() );
       
   198             if ( KEqualEmailAddress == attendeeAddr.CompareF(mailboxOwnerAddName) )
       
   199                 {
       
   200                 po = attendee;
       
   201                 }
       
   202             }
       
   203         }    
       
   204     
       
   205     if ( po )
       
   206         {
       
   207         aCalEntry.SetPhoneOwnerL( po );
       
   208         err = KErrNone;
       
   209         }
       
   210     
       
   211     CleanupStack::PopAndDestroy( mailbox );
       
   212     
       
   213     return err;
       
   214     }
       
   215 
       
   216 // ----------------------------------------------------------------------------
       
   217 // CESMRFsMailboxUtils::FSEmailPluginForEntryL
       
   218 // ----------------------------------------------------------------------------
       
   219 //
       
   220 TESMRMailPlugin CESMRFsMailboxUtils::FSEmailPluginForEntryL( 
       
   221         const CCalEntry& aEntry )
       
   222     {
       
   223     FUNC_LOG;
       
   224     RPointerArray<CFSMailBox> mailboxes;
       
   225     CleanupStack::PushL(
       
   226             TCleanupItem(
       
   227                     MailboxPointerArrayCleanup,
       
   228                     &mailboxes    ) );
       
   229 
       
   230     ListMailBoxesL(
       
   231             MailClientL(),
       
   232             mailboxes );    
       
   233     
       
   234     CCalUser* phoneOwner = aEntry.PhoneOwnerL();
       
   235     TPtrC poMailAddress( phoneOwner->Address() );
       
   236     
       
   237     CFSMailBox* fsEmailMailbox = NULL;
       
   238     TInt mailboxCount( mailboxes.Count() );
       
   239     for (TInt j(0); j < mailboxCount && !fsEmailMailbox; ++j )
       
   240         {        
       
   241         TPtrC mailboxOwnerAddName(
       
   242                 mailboxes[j]->OwnMailAddress().GetEmailAddress() );
       
   243         
       
   244         if ( KEqualEmailAddress == mailboxOwnerAddName.CompareF(poMailAddress) )
       
   245             {
       
   246             // Correct mailbox is found
       
   247             fsEmailMailbox = mailboxes[j];
       
   248             }
       
   249         }    
       
   250     
       
   251     TESMRMailPlugin plugin( EESMRUnknownPlugin );
       
   252     
       
   253     if ( fsEmailMailbox )
       
   254         {
       
   255         // Mailbox was found --> We need to resolve mailboxes plug-in
       
   256         TFSMailMsgId mailboxId( fsEmailMailbox->GetId( ) );
       
   257         TUid pluginId( mailboxId.PluginId() );
       
   258 
       
   259         if ( TUid::Uid( KEasFreestylePlugin ) == pluginId )
       
   260             {
       
   261             plugin = EESMRActiveSync;
       
   262             }
       
   263         else if ( TUid::Uid( KIntellisync ) == pluginId )
       
   264             {
       
   265             plugin = EESMRIntelliSync;
       
   266             }
       
   267         else if ( TUid::Uid( KFSEmailPop3 ) == pluginId ||
       
   268                 TUid::Uid( KFSEmailImap4 ) == pluginId )
       
   269             {
       
   270             plugin = EESMRImapPop;
       
   271             }        
       
   272         }
       
   273     
       
   274     CleanupStack::PopAndDestroy( &mailboxes );    
       
   275     return plugin;
       
   276     }
       
   277 
       
   278 // ----------------------------------------------------------------------------
       
   279 // CESMRFsMailboxUtils::FSEmailMailBoxForEntryL
       
   280 // ----------------------------------------------------------------------------
       
   281 //
       
   282 TFSMailMsgId CESMRFsMailboxUtils::FSEmailMailBoxForEntryL( 
       
   283         const CCalEntry& aEntry )
       
   284     {
       
   285     FUNC_LOG;
       
   286     RPointerArray<CFSMailBox> mailboxes;
       
   287     CleanupStack::PushL(
       
   288             TCleanupItem( MailboxPointerArrayCleanup, &mailboxes ) );
       
   289 
       
   290     ListMailBoxesL( MailClientL(), mailboxes );    
       
   291     
       
   292     CCalUser* phoneOwner = aEntry.PhoneOwnerL();
       
   293     TPtrC poMailAddress( phoneOwner->Address() );
       
   294     
       
   295     CFSMailBox* fsEmailMailbox = NULL;
       
   296     TInt mailboxCount( mailboxes.Count() );
       
   297     for (TInt j(0); j < mailboxCount && !fsEmailMailbox; ++j )
       
   298         {        
       
   299         TPtrC mailboxOwnerAddName(
       
   300                 mailboxes[j]->OwnMailAddress().GetEmailAddress() );
       
   301         
       
   302         if ( KEqualEmailAddress == 
       
   303              mailboxOwnerAddName.CompareF( poMailAddress ) )
       
   304             {
       
   305             // Correct mailbox is found
       
   306             fsEmailMailbox = mailboxes[j];
       
   307             }
       
   308         }
       
   309     
       
   310     TFSMailMsgId retVal; // default ctor constructs a null id.
       
   311     
       
   312     if ( fsEmailMailbox )
       
   313         {
       
   314         retVal = fsEmailMailbox->GetId();
       
   315         }
       
   316 
       
   317     CleanupStack::PopAndDestroy( &mailboxes );
       
   318     return retVal;
       
   319     }
       
   320 
       
   321 // ----------------------------------------------------------------------------
       
   322 // CESMRFsMailboxUtils::PhoneOwnerL
       
   323 // ----------------------------------------------------------------------------
       
   324 //
       
   325 CCalUser* CESMRFsMailboxUtils::PhoneOwnerL(
       
   326     const CCalEntry& aCalEntry,
       
   327     const RPointerArray<CFSMailBox>& aMailBoxes,
       
   328     TInt& aMatchIndex )
       
   329     {
       
   330     FUNC_LOG;
       
   331     aMatchIndex = -1; // no match yet
       
   332 
       
   333     // 1. test if phone owner is organizer:
       
   334     CCalUser* organizer = aCalEntry.OrganizerL(); // ownership not transf.
       
   335     if ( organizer )
       
   336         {
       
   337         if ( IsPhoneOwnerL( *organizer, aMailBoxes, aMatchIndex ) )
       
   338             {
       
   339             return organizer;
       
   340             }
       
   341         }
       
   342 
       
   343     // 2. test if phone owner is attendee:
       
   344     RPointerArray<CCalAttendee>& attendees = aCalEntry.AttendeesL();
       
   345     TInt attCount( attendees.Count() );
       
   346     for ( TInt i( 0 );i < attCount; ++i )
       
   347         {
       
   348         CCalUser* att = static_cast<CCalUser*>(attendees[i]);
       
   349         if ( IsPhoneOwnerL( *att, aMailBoxes, aMatchIndex ) )
       
   350             {
       
   351             return att;
       
   352             }
       
   353         }
       
   354 
       
   355     // Matching attendee was not found:
       
   356     return NULL;
       
   357     }
       
   358 
       
   359 // ----------------------------------------------------------------------------
       
   360 // CESMRFsMailboxUtils::IsPhoneOwnerL
       
   361 // ----------------------------------------------------------------------------
       
   362 //
       
   363 TBool CESMRFsMailboxUtils::IsPhoneOwnerL(
       
   364     const CCalUser& aUser,
       
   365     const RPointerArray<CFSMailBox>& aMailBoxes,
       
   366     TInt& aMatchIndex )
       
   367     {
       
   368     FUNC_LOG;
       
   369     TPtrC addr = ESMRHelper::AddressWithoutMailtoPrefix( aUser.Address() );
       
   370     aMatchIndex = -1; // no match yet
       
   371     TInt boxCount( aMailBoxes.Count() );
       
   372     for ( TInt i( 0 ); i < boxCount; ++i )
       
   373         {
       
   374         TPtrC mailboxOwnerAddName(
       
   375                 aMailBoxes[i]->OwnMailAddress().GetEmailAddress() );
       
   376 
       
   377         if ( addr.CompareF( mailboxOwnerAddName ) == KEqualEmailAddress )
       
   378             {
       
   379             // first match is enough:
       
   380             aMatchIndex = i;
       
   381             return ETrue;
       
   382             }
       
   383         }
       
   384     return EFalse; // no match
       
   385     }
       
   386 
       
   387 // ----------------------------------------------------------------------------
       
   388 // CESMRFsMailboxUtils::MailClientL
       
   389 // ----------------------------------------------------------------------------
       
   390 //
       
   391 CFSMailClient& CESMRFsMailboxUtils::MailClientL()
       
   392     {
       
   393     FUNC_LOG;
       
   394     if ( !iMailClient )
       
   395         {
       
   396         iMailClient = CFSMailClient::NewL();
       
   397         }
       
   398     return *iMailClient;
       
   399     }
       
   400 
       
   401 // EOF
       
   402