meetingrequestengines/mrmailboxutils/src/cmrmailboxutilsimpl.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2005 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: Provides methods for Meeting request mailbox utils ECom implementation.  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 
       
    21 #include "cmrmailboxutilsimpl.h"
       
    22 #include "cmrutilsmsvhelper.h"
       
    23 #include "cmrutilssettingsmgr.h"
       
    24 #include "cmrmailboxinfo.h"
       
    25 #include <cmrmbutilsmbinfohandler.h>
       
    26 #include <bldvariant.hrh>
       
    27 #include <featmgr.h>
       
    28 #include <calentry.h>
       
    29 #include <calsession.h>
       
    30 #include <calcommon.h>
       
    31 #include <caluser.h>
       
    32 #include <utf.h>
       
    33 #include <mmrmbutilssettings.h>
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 namespace
       
    38 {
       
    39 
       
    40 const TInt KObserverGranularity = 1; // in most cases 0 or 1 observer
       
    41 _LIT( KMailtoMatchPattern, "mailto:*" ); // this is never localized
       
    42 const TInt KMailtoLength = 7; // "mailto:" length
       
    43 
       
    44 }
       
    45 
       
    46 // ========================= MEMBER FUNCTIONS ================================
       
    47 
       
    48 // ----------------------------------------------------------------------------
       
    49 // CMRMailboxUtilsImpl::NewL
       
    50 // Symbian two-phased constructor.
       
    51 // (other items were commented in a header).
       
    52 // ----------------------------------------------------------------------------
       
    53 //
       
    54 CMRMailboxUtilsImpl* CMRMailboxUtilsImpl::NewL( CMsvSession* aMsvSession )
       
    55 	{	
       
    56 	CMRMailboxUtilsImpl* self = new( ELeave ) CMRMailboxUtilsImpl();
       
    57 	CleanupStack::PushL( self );
       
    58 	self->ConstructL( aMsvSession );
       
    59 	CleanupStack::Pop();
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 // ----------------------------------------------------------------------------
       
    64 // CMRMailboxUtilsImpl::CMRMailboxUtilsImpl
       
    65 // Default constructor
       
    66 // ----------------------------------------------------------------------------
       
    67 //
       
    68 CMRMailboxUtilsImpl::CMRMailboxUtilsImpl()
       
    69     : iObservers( KObserverGranularity )
       
    70     {    
       
    71     }
       
    72     
       
    73 // ----------------------------------------------------------------------------
       
    74 // CMRMailboxUtilsImpl::~CMRMailboxUtilsImpl
       
    75 // Destructor.
       
    76 // ----------------------------------------------------------------------------
       
    77 //        
       
    78 CMRMailboxUtilsImpl::~CMRMailboxUtilsImpl()
       
    79     {
       
    80     FeatureManager::UnInitializeLib();
       
    81     
       
    82     iObservers.Close();    
       
    83     delete iMsvHelper;
       
    84     delete iSettingsMgr;
       
    85     delete iCmailHandler;
       
    86     }
       
    87     
       
    88 // ----------------------------------------------------------------------------
       
    89 // CMRMailboxUtilsImpl::ConstructL
       
    90 //
       
    91 // 2nd phase constructor.
       
    92 // ----------------------------------------------------------------------------
       
    93 //        
       
    94 void CMRMailboxUtilsImpl::ConstructL( CMsvSession* aMsvSession )    
       
    95     {
       
    96     FeatureManager::InitializeLibL();
       
    97     
       
    98     iSettingsMgr = CMRUtilsSettingsMgr::NewL();
       
    99     iMsvHelper = CMRUtilsMsvHelper::NewL( aMsvSession, *this );
       
   100     
       
   101 	if ( FeatureManager::FeatureSupported( KFeatureIdFfEmailFramework ) )
       
   102 		{
       
   103 		// Create this only when CMail is present
       
   104 		_LIT8( KCMail, "CMAIL" );
       
   105 		TPtrC8 cmail( KCMail );
       
   106 		iCmailHandler = 
       
   107                 CMRMBUtilsMBInfoHandler::NewL( 
       
   108                         cmail,
       
   109                         *this );
       
   110 		}
       
   111     }
       
   112     
       
   113     
       
   114 // ----------------------------------------------------------------------------
       
   115 // CMRMailboxUtilsImpl::GetDefaultMRMailBoxL
       
   116 // ----------------------------------------------------------------------------
       
   117 //    
       
   118 TInt CMRMailboxUtilsImpl::GetDefaultMRMailBoxL( TMailboxInfo& aBoxInfo )
       
   119     {
       
   120     aBoxInfo.iEntryId = KMsvNullIndexEntryId;
       
   121     TMsvId candidateId( KMsvNullIndexEntryId );
       
   122     TInt retVal = iSettingsMgr->GetDefaultMRMailBoxL( candidateId );
       
   123     
       
   124     if ( iCmailHandler )
       
   125         {
       
   126         TInt cmailId;
       
   127         MMRMBUtilsSettings* cmailSettings = 
       
   128                 iCmailHandler->SettingsMgr();
       
   129                     
       
   130         if ( cmailSettings )
       
   131             {
       
   132             cmailSettings->GetDefaultMailboxSettingsL( cmailId );            
       
   133             if ( cmailId )
       
   134                 {
       
   135                 candidateId = cmailId;
       
   136                 }
       
   137             }
       
   138         }    
       
   139     
       
   140     if ( retVal == KErrNone )
       
   141         {
       
   142         // try to read mailbox info
       
   143         const CMRMailBoxInfo* info = iMsvHelper->MRMailBoxInfoL( candidateId );
       
   144         
       
   145         if ( !info && iCmailHandler )
       
   146         	{
       
   147         	// Tru getting mailbox information using extension
       
   148         	info = iCmailHandler->MRMailBoxInfoL( candidateId );
       
   149         	}
       
   150         
       
   151         if ( !info )
       
   152             { 
       
   153             // default doesn't exist anymore, try to set another one
       
   154             RArray<CMRMailboxUtils::TMailboxInfo> mailBoxes;
       
   155             CleanupClosePushL( mailBoxes );
       
   156             ListMailBoxesL( mailBoxes );
       
   157             
       
   158             if ( mailBoxes.Count() > 0 )
       
   159                 {
       
   160                 candidateId = mailBoxes[0].iEntryId; 
       
   161                 if ( SetDefaultMRMailBoxL( candidateId ) == KErrNone )
       
   162                     { 
       
   163                     // setting succeeded, read data of the new default mailbox                    
       
   164                     info = iMsvHelper->MRMailBoxInfoL( candidateId );
       
   165                     if ( !info && iCmailHandler )
       
   166                     	{
       
   167                     	// Try getting mailbox information using extension
       
   168                     	info = iCmailHandler->MRMailBoxInfoL( candidateId );
       
   169                     	}                    
       
   170                     }
       
   171                 }
       
   172             
       
   173             // mailboxes
       
   174             CleanupStack::PopAndDestroy(); // codescanner::cleanup
       
   175             }
       
   176         
       
   177         if ( info )
       
   178             {
       
   179             // After all the steps we have found a valid MR mailbox,
       
   180             // give it to the caller then           
       
   181             aBoxInfo.iName.Set( info->Name() );
       
   182             aBoxInfo.iEmailAddress.Set( info->Address() );
       
   183             aBoxInfo.iMtmUid = info->MtmUid();
       
   184             aBoxInfo.iEntryId = info->EntryId();
       
   185             }
       
   186         else
       
   187             {            
       
   188             // We only return KErrNone or KErrNotFound based on the first
       
   189             // GetDefaultMRMailBoxL() check, later errors cause a leave
       
   190             User::Leave( KErrGeneral );
       
   191             }        
       
   192         }        
       
   193             
       
   194     return retVal;
       
   195     }
       
   196 
       
   197 
       
   198 // ----------------------------------------------------------------------------
       
   199 // CMRMailboxUtilsImpl::SetDefaultMRMailBoxL
       
   200 // ----------------------------------------------------------------------------
       
   201 // 
       
   202 TInt CMRMailboxUtilsImpl::SetDefaultMRMailBoxL( TMsvId aDefaultBox )
       
   203     {
       
   204     TInt retVal = iSettingsMgr->SetDefaultMRMailBoxL( aDefaultBox );
       
   205     
       
   206     if ( iCmailHandler )
       
   207         {        
       
   208         const CMRMailBoxInfo* info = 
       
   209                 iCmailHandler->MRMailBoxInfoL( aDefaultBox);
       
   210         
       
   211         if ( info )
       
   212             {
       
   213             // This is CMAIL box
       
   214             MMRMBUtilsSettings* cmailSettings = 
       
   215                     iCmailHandler->SettingsMgr();
       
   216             
       
   217             if ( cmailSettings )
       
   218                 {
       
   219                 cmailSettings->SetDefaultMailboxSettingL( aDefaultBox );
       
   220                 }
       
   221             }
       
   222         }
       
   223     
       
   224     NotifyObserversL( MMRMailboxUtilsObserver::EMRDefaultSet );
       
   225     return retVal;
       
   226     }
       
   227 
       
   228 // ----------------------------------------------------------------------------
       
   229 // CMRMailboxUtilsImpl::ListMailBoxesL
       
   230 // ----------------------------------------------------------------------------
       
   231 // 
       
   232 void CMRMailboxUtilsImpl::ListMailBoxesL( RArray<TMailboxInfo>& aMailBoxes )
       
   233     {
       
   234     iMsvHelper->ListMRMailBoxesL( aMailBoxes );
       
   235     if ( iCmailHandler )
       
   236     	{
       
   237     	iCmailHandler->ListMRMailBoxesL( aMailBoxes );
       
   238     	}
       
   239     }
       
   240 
       
   241 // ----------------------------------------------------------------------------
       
   242 // CMRMailboxUtilsImpl::GetSourceMtmUidL
       
   243 // ----------------------------------------------------------------------------
       
   244 //  
       
   245 TInt CMRMailboxUtilsImpl::GetSourceMtmUidL(
       
   246     const CCalEntry& aCalEntry,
       
   247     TUid& aMtmUid )
       
   248     {
       
   249     TInt retVal( KErrNotFound );    
       
   250     
       
   251     RArray<CMRMailboxUtilsImpl::TMailboxInfo> mailboxes;
       
   252     CleanupClosePushL( mailboxes );
       
   253     ListMailBoxesL( mailboxes );
       
   254     
       
   255     TInt matchIndex( KErrNotFound );
       
   256     // Ownership of phone owner is not transferred:
       
   257     CCalUser* owner = PhoneOwnerL( aCalEntry, mailboxes, matchIndex );    
       
   258     if ( owner && KErrNotFound != matchIndex )
       
   259         {
       
   260         // Array contains itself out-of-bounds check
       
   261         aMtmUid = mailboxes[matchIndex].iMtmUid; // codescanner::accessArrayElementWithoutCheck2
       
   262         retVal = KErrNone;
       
   263         }
       
   264         
       
   265     CleanupStack::PopAndDestroy(); // mailboxes
       
   266     return retVal;  
       
   267     }
       
   268 
       
   269 // ----------------------------------------------------------------------------
       
   270 // CMRMailboxUtilsImpl::ThisAttendeeL
       
   271 // ----------------------------------------------------------------------------
       
   272 // 
       
   273 CCalAttendee* CMRMailboxUtilsImpl::ThisAttendeeL( const CCalEntry& aCalEntry )
       
   274     {    
       
   275     CCalAttendee* thisAtt = NULL;    
       
   276     CCalUser* phoneOwner = aCalEntry.PhoneOwnerL(); // ownership not transf.       
       
   277 
       
   278     if ( phoneOwner )
       
   279         {
       
   280         RPointerArray<CCalAttendee>& attendees = aCalEntry.AttendeesL();    
       
   281         TInt attCount( attendees.Count() );
       
   282         for ( TInt i( 0 );i < attCount; ++i )
       
   283             {            
       
   284             CCalAttendee* att = attendees[i];
       
   285             if ( att && phoneOwner == att )
       
   286                 {
       
   287                 // first match is enough:
       
   288                 thisAtt = att;
       
   289                 break;
       
   290                 }
       
   291             }            
       
   292         }
       
   293     return thisAtt; // ownership not transferred, may be also NULL
       
   294     }
       
   295 
       
   296 // ----------------------------------------------------------------------------
       
   297 // CMRMailboxUtilsImpl::IsOrganizerL
       
   298 // ----------------------------------------------------------------------------
       
   299 // 
       
   300 TBool CMRMailboxUtilsImpl::IsOrganizerL( const CCalEntry& aCalEntry )
       
   301     {
       
   302     TBool retVal( EFalse );
       
   303     CCalUser* organizer = aCalEntry.OrganizerL(); // ownership not transf.
       
   304     CCalUser* phoneOwner = aCalEntry.PhoneOwnerL(); // ownership not transf.
       
   305 
       
   306     if ( organizer && phoneOwner && organizer == phoneOwner )
       
   307         {
       
   308         retVal = ETrue;
       
   309         }
       
   310     return retVal;
       
   311     }
       
   312 
       
   313 // ----------------------------------------------------------------------------
       
   314 // CMRMailboxUtilsImpl::SetPhoneOwnerL
       
   315 // ----------------------------------------------------------------------------
       
   316 //    
       
   317 TInt CMRMailboxUtilsImpl::SetPhoneOwnerL(
       
   318     CCalEntry& aCalEntry,
       
   319     TMsvId /*aPrimaryBox*/ )
       
   320     {
       
   321     TInt retVal( KErrNone );
       
   322     CCalUser* phoneOwner = aCalEntry.PhoneOwnerL(); // ownership not transf.
       
   323     
       
   324     // TODO: we should optimize the lookup, by:
       
   325     // prioritising the mailbox where entry received + calendar mr mailbox
       
   326     // prioritising mailbox which causes last match (cache last match)    
       
   327     
       
   328     if ( !phoneOwner )
       
   329         {
       
   330         RArray<CMRMailboxUtilsImpl::TMailboxInfo> mailboxes;
       
   331         CleanupClosePushL( mailboxes );
       
   332         // TODO: handle aPrimaryBox!
       
   333         ListMailBoxesL( mailboxes );        
       
   334                 
       
   335         // Match email addresses to organizer + attendee list addresses:
       
   336         TInt dummyIndex( -1 ); // don't care
       
   337         phoneOwner = PhoneOwnerL( aCalEntry, mailboxes, dummyIndex );
       
   338         
       
   339         if ( phoneOwner )
       
   340             {
       
   341             aCalEntry.SetPhoneOwnerL( phoneOwner );
       
   342             }
       
   343         else
       
   344             {
       
   345             retVal = KErrNotFound;
       
   346             }
       
   347 
       
   348         CleanupStack::PopAndDestroy(); // mailboxes
       
   349         }
       
   350     return retVal;        
       
   351     }
       
   352             
       
   353 // ---------------------------------------------------------
       
   354 // CMRMailboxUtilsImpl::AddressWithoutMailtoPrefix
       
   355 // ---------------------------------------------------------
       
   356 //
       
   357 TPtrC CMRMailboxUtilsImpl::AddressWithoutMailtoPrefix( const TDesC& aAddress )
       
   358 	{
       
   359 	TPtrC addrWithoutPrefix;
       
   360     if ( aAddress.MatchF( KMailtoMatchPattern ) != KErrNotFound )
       
   361     	{
       
   362         addrWithoutPrefix.Set( aAddress.Mid( KMailtoLength ) );
       
   363         }
       
   364     else
       
   365     	{
       
   366         addrWithoutPrefix.Set( aAddress );
       
   367         }
       
   368     return addrWithoutPrefix;
       
   369 	}
       
   370 	
       
   371 // ----------------------------------------------------------------------------
       
   372 // CMRMailboxUtilsImpl::PhoneOwnerL
       
   373 // ----------------------------------------------------------------------------
       
   374 //   
       
   375 CCalUser* CMRMailboxUtilsImpl::PhoneOwnerL(
       
   376     const CCalEntry& aCalEntry,
       
   377     const RArray<CMRMailboxUtilsImpl::TMailboxInfo>& aMailBoxes,
       
   378     TInt& aMatchIndex )
       
   379     {
       
   380     aMatchIndex = -1; // no match yet
       
   381 
       
   382     // 1. test if phone owner is organizer:
       
   383 
       
   384     CCalUser* organizer = aCalEntry.OrganizerL(); // ownership not transf.
       
   385     if ( organizer )
       
   386         {
       
   387         if ( IsPhoneOwnerL( *organizer, aMailBoxes, aMatchIndex ) )
       
   388             {
       
   389             return organizer;
       
   390             }
       
   391         }
       
   392 
       
   393     // 2. test if phone owner is attendee:
       
   394 
       
   395     RPointerArray<CCalAttendee>& attendees = aCalEntry.AttendeesL();
       
   396     TInt attCount( attendees.Count() );
       
   397     for ( TInt i( 0 );i < attCount; ++i )
       
   398         {            
       
   399         CCalUser* att = attendees[i];
       
   400         if ( IsPhoneOwnerL( *att, aMailBoxes, aMatchIndex ) )
       
   401             {
       
   402             return att;
       
   403             }        
       
   404         }
       
   405         
       
   406     // Matching attendee was not found:
       
   407     return NULL;
       
   408     }
       
   409 
       
   410 // ----------------------------------------------------------------------------
       
   411 // CMRMailboxUtilsImpl::IsPhoneOwnerL
       
   412 // ----------------------------------------------------------------------------
       
   413 //   
       
   414 TBool CMRMailboxUtilsImpl::IsPhoneOwnerL(
       
   415     const CCalUser& aUser,
       
   416     const RArray<CMRMailboxUtilsImpl::TMailboxInfo>& aMailBoxes,
       
   417     TInt& aMatchIndex )
       
   418     {
       
   419     TPtrC addr = AddressWithoutMailtoPrefix( aUser.Address() );
       
   420     aMatchIndex = -1; // no match yet
       
   421     TInt boxCount( aMailBoxes.Count() );    
       
   422     for ( TInt i( 0 ); i < boxCount; ++i )
       
   423         {
       
   424         TPtrC boxAddr = aMailBoxes[i].iEmailAddress; 
       
   425         if ( addr.CompareF( boxAddr ) == 0 )
       
   426             {
       
   427             // first match is enough:
       
   428             aMatchIndex = i;
       
   429             return ETrue;
       
   430             }
       
   431         }
       
   432     return EFalse; // no match
       
   433     }
       
   434 
       
   435 // ----------------------------------------------------------------------------
       
   436 // CMRMailboxUtilsImpl::AddObserverL
       
   437 // ----------------------------------------------------------------------------
       
   438 //  
       
   439 void CMRMailboxUtilsImpl::AddObserverL( MMRMailboxUtilsObserver& aObserver )
       
   440     {
       
   441     iObservers.InsertInAddressOrderL( &aObserver );
       
   442     }
       
   443     
       
   444 // ----------------------------------------------------------------------------
       
   445 // CMRMailboxUtilsImpl::RemoveObserver
       
   446 // ----------------------------------------------------------------------------
       
   447 //  
       
   448 void CMRMailboxUtilsImpl::RemoveObserver( MMRMailboxUtilsObserver& aObserver )
       
   449     {
       
   450     TInt index = iObservers.FindInAddressOrder( &aObserver );
       
   451     if ( index != KErrNotFound )
       
   452         {        
       
   453         iObservers.Remove( index );
       
   454         }
       
   455     }
       
   456     
       
   457 // ----------------------------------------------------------------------------
       
   458 // CMRMailboxUtilsImpl::MsvSessionL
       
   459 // ----------------------------------------------------------------------------
       
   460 //  
       
   461 CMsvSession& CMRMailboxUtilsImpl::MsvSessionL()
       
   462     {
       
   463     return iMsvHelper->MsvSessionL();
       
   464     }
       
   465     
       
   466 // ----------------------------------------------------------------------------
       
   467 // CMRMailboxUtilsImpl::HandleSessionEventL
       
   468 // ----------------------------------------------------------------------------
       
   469 //
       
   470 void CMRMailboxUtilsImpl::HandleSessionEventL(
       
   471     TMsvSessionEvent aEvent,
       
   472     TAny* aArg1,
       
   473     TAny* /*aArg2*/,
       
   474     TAny* /*aArg3*/ )
       
   475     {
       
   476     CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>( aArg1 );
       
   477     switch ( aEvent )
       
   478         {        
       
   479         case EMsvEntriesCreated:
       
   480             {
       
   481             NotifyObserversL( MMRMailboxUtilsObserver::EMRMailboxCreated );
       
   482             break;
       
   483             }
       
   484         case EMsvEntriesChanged:
       
   485             {
       
   486             TMsvId defaultId;
       
   487             TInt err = iSettingsMgr->GetDefaultMRMailBoxL( defaultId );
       
   488             if ( err == KErrNone &&
       
   489                  entries->Find( defaultId ) != KErrNotFound )
       
   490                 { // default changed
       
   491                 NotifyObserversL( MMRMailboxUtilsObserver::EMRDefaultChanged );
       
   492                 }
       
   493             else
       
   494                 {                    
       
   495                 NotifyObserversL( MMRMailboxUtilsObserver::EMRMailboxChanged );
       
   496                 }
       
   497             break;
       
   498             }        
       
   499         case EMsvEntriesDeleted:
       
   500             {
       
   501             TMsvId defaultId;
       
   502             TInt err = iSettingsMgr->GetDefaultMRMailBoxL( defaultId );
       
   503             if ( err == KErrNone &&
       
   504                  entries->Find( defaultId ) != KErrNotFound )
       
   505                 { // default deleted
       
   506                 NotifyObserversL( MMRMailboxUtilsObserver::EMRDefaultDeleted );
       
   507                 }
       
   508             else
       
   509                 {
       
   510                 NotifyObserversL( MMRMailboxUtilsObserver::EMRMailboxDeleted );
       
   511                 }
       
   512             break;
       
   513             }
       
   514         default:
       
   515             { // not interested in other even types
       
   516             break;
       
   517             }
       
   518         }    
       
   519     }
       
   520 
       
   521 // ----------------------------------------------------------------------------
       
   522 // CMRMailboxUtilsImpl::HandleMRMailboxEventL
       
   523 // ----------------------------------------------------------------------------
       
   524 //
       
   525 void CMRMailboxUtilsImpl::HandleMRMailboxEventL( 
       
   526         MMRMailboxUtilsObserver::TEventType aType )
       
   527     {
       
   528     NotifyObserversL( aType );
       
   529     }
       
   530 
       
   531 // ----------------------------------------------------------------------------
       
   532 // CMRMailboxUtilsImpl::NotifyObserversL
       
   533 // ----------------------------------------------------------------------------
       
   534 //
       
   535 void CMRMailboxUtilsImpl::NotifyObserversL(
       
   536     MMRMailboxUtilsObserver::TEventType aEvent )
       
   537     {
       
   538     TInt count( iObservers.Count() );   
       
   539     for ( TInt i( 0 ); i < count; ++i )
       
   540         {
       
   541         TRAP_IGNORE( iObservers[i]->HandleMRMailboxEventL( aEvent ) );
       
   542         }
       
   543     }    
       
   544     
       
   545 // End of file