ipsservices/ipssosplugin/src/ipsplgsmtpoperation.cpp
changeset 0 8466d47a6819
child 4 e7aa27f58ae1
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007 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 CIpsPlgSmtpOperation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "ipsplgheaders.h"
       
    21 
       
    22 
       
    23 const TInt KIpsSmtpOperationCharLessThan = '<';
       
    24 const TInt KIpsSmtpOperationCharMoreThan = '>';
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 EXPORT_C CIpsPlgSmtpOperation* CIpsPlgSmtpOperation::NewL( 
       
    30     CMsvSession& aMsvSession, 
       
    31     TInt aPriority, TRequestStatus& 
       
    32     aObserverRequestStatus,
       
    33     TBool /*aUsePublishSubscribe*/ )
       
    34     {
       
    35     FUNC_LOG;
       
    36     CIpsPlgSmtpOperation* self = CIpsPlgSmtpOperation::NewLC(
       
    37         aMsvSession, aPriority, aObserverRequestStatus, ETrue );
       
    38     CleanupStack::Pop( self );
       
    39     return self;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C CIpsPlgSmtpOperation* CIpsPlgSmtpOperation::NewLC( 
       
    46     CMsvSession& aMsvSession, 
       
    47     TInt aPriority, 
       
    48     TRequestStatus& aObserverRequestStatus,
       
    49     TBool /*aUsePublishSubscribe*/ )
       
    50     {
       
    51     FUNC_LOG;
       
    52     CIpsPlgSmtpOperation* self = new( ELeave ) CIpsPlgSmtpOperation(
       
    53         aMsvSession, aPriority, aObserverRequestStatus );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     return self;
       
    57     }
       
    58    
       
    59 // ---------------------------------------------------------------------------
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CIpsPlgSmtpOperation::CIpsPlgSmtpOperation( 
       
    63     CMsvSession& aMsvSession, 
       
    64     TInt aPriority, 
       
    65     TRequestStatus& aObserverRequestStatus ) :
       
    66     CMsvOperation( aMsvSession, aPriority, aObserverRequestStatus ),
       
    67     iSmtpMtm( NULL ),
       
    68     iOperation( NULL ),
       
    69     iSelection( NULL ),  
       
    70     iMtmRegistry( NULL )
       
    71     {
       
    72     FUNC_LOG;
       
    73     }
       
    74   
       
    75 // ---------------------------------------------------------------------------
       
    76 // ---------------------------------------------------------------------------
       
    77 //    
       
    78 CIpsPlgSmtpOperation::~CIpsPlgSmtpOperation() 
       
    79     {
       
    80     FUNC_LOG;
       
    81     Cancel();
       
    82     delete iSmtpMtm;
       
    83     delete iMtmRegistry;
       
    84     delete iSelection;
       
    85     delete iOperation;
       
    86     } 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CIpsPlgSmtpOperation::ConstructL()
       
    92     {
       
    93     FUNC_LOG;
       
    94     iSelection = new (ELeave) CMsvEntrySelection();
       
    95     CActiveScheduler::Add( this );
       
    96     }
       
    97     
       
    98 // ---------------------------------------------------------------------------
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CIpsPlgSmtpOperation::RunL()
       
   102     {
       
   103     FUNC_LOG;
       
   104     
       
   105     
       
   106     if ( iState == EMovingOutbox )
       
   107         {
       
   108         delete iOperation;
       
   109         iOperation = NULL;
       
   110         iSelection->InsertL(0, iSmtpService );
       
   111         CallSendL( );
       
   112         }
       
   113     else if ( iState == ESending )
       
   114         {
       
   115         TImSmtpProgress prog;
       
   116         TPckg<TImSmtpProgress> param(prog);
       
   117         param.Copy( iOperation->FinalProgress() ); 
       
   118         
       
   119         TRequestStatus* status = &iObserverRequestStatus;
       
   120         User::RequestComplete( status, prog.Error() );
       
   121         }    
       
   122     }
       
   123     
       
   124 // ---------------------------------------------------------------------------
       
   125 // ---------------------------------------------------------------------------
       
   126 //    
       
   127 void CIpsPlgSmtpOperation::DoCancel()
       
   128     {
       
   129     FUNC_LOG;
       
   130     
       
   131     if ( iOperation )
       
   132         {
       
   133         iOperation->Cancel();
       
   134         }
       
   135     TRequestStatus* status = &iObserverRequestStatus;
       
   136     User::RequestComplete(status, KErrCancel);
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // ---------------------------------------------------------------------------
       
   141 //   
       
   142 const TDesC8& CIpsPlgSmtpOperation::ProgressL()
       
   143     {
       
   144     FUNC_LOG;
       
   145     
       
   146     if( iOperation && iState == ESending  )
       
   147         {
       
   148         return iOperation->ProgressL();
       
   149         }
       
   150     return KNullDesC8;
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // ---------------------------------------------------------------------------
       
   155 //       
       
   156 EXPORT_C void CIpsPlgSmtpOperation::StartSendL( TMsvId aMessageId )
       
   157     {
       
   158     FUNC_LOG;
       
   159     
       
   160     TMsvId service;
       
   161     TMsvEntry entry;
       
   162     User::LeaveIfError( 
       
   163         iMsvSession.GetEntry( aMessageId, service, entry ) );
       
   164     if ( entry.Id() == KMsvNullIndexEntryIdValue )
       
   165         {
       
   166         User::Leave( KErrNotFound );
       
   167         }
       
   168     
       
   169     iSmtpService = entry.iServiceId;
       
   170     ConstructSmtpMtmL();
       
   171     iSmtpMtm->SwitchCurrentEntryL( entry.Id() );
       
   172     
       
   173     if ( entry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue )
       
   174         {
       
   175         iSelection->Reset();
       
   176         // smtp service id have to be first in array
       
   177         // here must be iSmtpService
       
   178         iSelection->AppendL( iSmtpService );
       
   179         // then message id
       
   180         iSelection->AppendL( entry.Id() );
       
   181         CallSendL( );
       
   182         }
       
   183     else
       
   184         {
       
   185         // 1. perform validity checks for recipient addresses
       
   186         // 2. move message to outbox
       
   187         // Perform validity checks for recipient addresses
       
   188         // before moving
       
   189         CMsvEntry* msgCEntry = CMsvEntry::NewL(
       
   190             iMsvSession, aMessageId, TMsvSelectionOrdering() );
       
   191         CleanupStack::PushL( msgCEntry );
       
   192         ValidateRecipientsL( *msgCEntry );
       
   193         CleanupStack::PopAndDestroy( msgCEntry );
       
   194         msgCEntry = NULL;
       
   195         
       
   196         iSelection->Reset();
       
   197         // get message entry
       
   198         msgCEntry = CMsvEntry::NewL(
       
   199             iMsvSession, entry.Parent(), TMsvSelectionOrdering() );
       
   200         CleanupStack::PushL( msgCEntry );
       
   201 
       
   202         // do the move
       
   203         iSelection->AppendL( entry.Id());
       
   204         iStatus = KRequestPending;
       
   205         delete iOperation;
       
   206         iOperation = NULL;
       
   207         iOperation = msgCEntry->MoveL( 
       
   208             *iSelection, KMsvGlobalOutBoxIndexEntryIdValue, iStatus );
       
   209         SetActive();
       
   210         CleanupStack::PopAndDestroy( msgCEntry );
       
   211         iState = EMovingOutbox; 
       
   212         }
       
   213     }
       
   214     
       
   215 // ---------------------------------------------------------------------------
       
   216 // ---------------------------------------------------------------------------
       
   217 //    
       
   218 EXPORT_C TInt CIpsPlgSmtpOperation::EmptyOutboxFromPendingMessagesL( 
       
   219     TMsvId aMailboxId  )
       
   220     {
       
   221     FUNC_LOG;
       
   222     TMsvId service;
       
   223     TMsvEntry entry;
       
   224     User::LeaveIfError( 
       
   225         iMsvSession.GetEntry( aMailboxId, service, entry ) );
       
   226     
       
   227     iSmtpService = entry.iRelatedId;
       
   228     
       
   229     TInt retValue = KErrNone;
       
   230     
       
   231     iSelection->Reset();
       
   232     ConstructSmtpMtmL();
       
   233     iSmtpMtm->SwitchCurrentEntryL( KMsvGlobalOutBoxIndexEntryIdValue );
       
   234     
       
   235     // in sending first entry in selection array have to be service id
       
   236     iSelection->AppendL(iSmtpService);
       
   237     //index to first message entry of selection array
       
   238     
       
   239     CMsvEntrySelection* childs = GetOutboxChildrensL( );
       
   240     CleanupStack::PushL( childs );
       
   241     for ( TInt i = 0; i < childs->Count(); i++ )
       
   242         {
       
   243         iMsvSession.GetEntry( childs->At(i), service, entry );
       
   244         TUint sendState = entry.SendingState();
       
   245         
       
   246         if ( iSmtpService == entry.iServiceId &&
       
   247             ( sendState != KMsvSendStateUnknown
       
   248               || sendState != KMsvSendStateUponRequest
       
   249               || sendState != KMsvSendStateSent ) )
       
   250             {
       
   251             // chage suspended flag to waiting
       
   252             // is it safe to change flag from suspended to waiting?
       
   253             if ( sendState == KMsvSendStateSuspended )
       
   254                 {
       
   255                 CMsvEntry* cEntry = CMsvEntry::NewL(
       
   256                     iMsvSession, entry.Id(), TMsvSelectionOrdering() );
       
   257                 CleanupStack::PushL( cEntry );
       
   258                 CIpsPlgOperationWait* wait = CIpsPlgOperationWait::NewLC( );
       
   259                 entry.SetSendingState( KMsvSendStateWaiting );
       
   260                 cEntry->ChangeL( entry, wait->iStatus );
       
   261                 wait->Start();
       
   262                 CleanupStack::PopAndDestroy(2, cEntry ); // wait, cEntry
       
   263                 }
       
   264             // add to send list
       
   265             iSelection->AppendL( entry.Id() );
       
   266             ++retValue;
       
   267             }
       
   268         }
       
   269     CleanupStack::PopAndDestroy( childs );
       
   270     if (retValue > 0)
       
   271         {
       
   272         CallSendL();
       
   273         }
       
   274     return retValue;
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 EXPORT_C CMsvEntrySelection* CIpsPlgSmtpOperation::GetOutboxChildrensL( )
       
   281     {
       
   282     FUNC_LOG;
       
   283     ConstructSmtpMtmL();
       
   284     iSmtpMtm->SwitchCurrentEntryL( KMsvGlobalOutBoxIndexEntryIdValue );
       
   285     return iSmtpMtm->Entry().ChildrenL();
       
   286     }
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 void CIpsPlgSmtpOperation::CallSendL( )
       
   292     {
       
   293     FUNC_LOG;
       
   294     TBuf8<1> params;
       
   295     
       
   296     iOperation = iSmtpMtm->InvokeAsyncFunctionL(
       
   297         KSMTPMTMSendOnNextConnection, *iSelection, params, iStatus);
       
   298     SetActive();
       
   299     iState = ESending;
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 void CIpsPlgSmtpOperation::ConstructSmtpMtmL()
       
   306     {
       
   307     FUNC_LOG;
       
   308     if ( !iMtmRegistry )
       
   309         {
       
   310         iMtmRegistry = CClientMtmRegistry::NewL( iMsvSession );
       
   311         }
       
   312     if ( !iSmtpMtm )
       
   313         {
       
   314         iSmtpMtm = static_cast<CSmtpClientMtm*>(
       
   315             iMtmRegistry->NewMtmL( KSenduiMtmSmtpUid ) );
       
   316         }
       
   317     }
       
   318     
       
   319 // ---------------------------------------------------------------------------
       
   320 // ---------------------------------------------------------------------------
       
   321 // 
       
   322 void CIpsPlgSmtpOperation::ValidateRecipientsL( CMsvEntry& aEntry )
       
   323     {
       
   324     FUNC_LOG;
       
   325     
       
   326     // get store object
       
   327     CMsvStore* store = aEntry.ReadStoreL();
       
   328     CleanupStack::PushL( store );   // << store
       
   329     
       
   330     // create symbian email header object
       
   331     CImHeader* header = CImHeader::NewLC(); // << header
       
   332     header->RestoreL( *store );
       
   333 
       
   334     // checking separate recipient fields
       
   335     ValidateAddressArrayL( header->ToRecipients() );
       
   336     ValidateAddressArrayL( header->CcRecipients() );
       
   337     ValidateAddressArrayL( header->BccRecipients() );
       
   338     
       
   339     // Cleanup
       
   340     CleanupStack::PopAndDestroy( header );
       
   341     CleanupStack::PopAndDestroy( store );
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // ---------------------------------------------------------------------------
       
   346 // 
       
   347 void CIpsPlgSmtpOperation::ValidateAddressArrayL( const CDesCArray& aRecipients )
       
   348     {
       
   349     FUNC_LOG;
       
   350     
       
   351     TInt count( aRecipients.Count() );
       
   352     for ( TInt i(0); i < count; i++ )
       
   353         {
       
   354         TPtrC fullName( aRecipients[i] );
       
   355         
       
   356         // Need to locate angle brackets from end of the data, if there
       
   357         // is display name with special characters
       
   358         TInt start( fullName.LocateReverse( KIpsSmtpOperationCharLessThan ) ); 
       
   359         TInt end( fullName.LocateReverse( KIpsSmtpOperationCharMoreThan ) );
       
   360         if ( KErrNotFound != start && KErrNotFound != end )
       
   361             {
       
   362             start += 1;
       
   363             fullName.Set( aRecipients[i].Mid( start, ( end - start ) ) );
       
   364             }
       
   365         // make basic sanity checks for email address
       
   366         if( !IpsSetUtils::IsValidEmailAddressL( fullName ) )
       
   367             {
       
   368             User::Leave( KErrBadName );
       
   369             }
       
   370         }
       
   371     }
       
   372 
       
   373 // End of File
       
   374