htiui/HtiServicePlugins/HtiMessagesServicePlugin/src/HtiMailboxHandler.cpp
changeset 0 d6fe6244b863
child 11 454d022d514b
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     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:  Implementation of HTI mailbox settings handling.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <cmconnectionmethod.h>
       
    21 #include <cmconnectionmethoddef.h>
       
    22 #include <cmdestination.h>
       
    23 #include <cmmanager.h>
       
    24 #include <cemailaccounts.h>
       
    25 #include <etelmm.h>
       
    26 #include <iapprefs.h>
       
    27 #include <imapset.h>
       
    28 #include <mtmuibas.h>
       
    29 #include <senduiconsts.h>
       
    30 #include <pop3set.h>
       
    31 #include <smtpset.h>
       
    32 #include <txtrich.h>
       
    33 
       
    34 #include <HtiDispatcherInterface.h>
       
    35 #include <HTILogging.h>
       
    36 #include "HtiMessagesServicePlugin.h"
       
    37 #include "HtiMailboxHandler.h"
       
    38 
       
    39 // EXTERNAL DATA STRUCTURES
       
    40 
       
    41 // EXTERNAL FUNCTION PROTOTYPES
       
    42 
       
    43 // CONSTANTS
       
    44 const TInt KMinCreateMsgLength = 36;
       
    45 const TInt KMinDeleteMsgLength = 3;
       
    46 
       
    47 const TInt KImeiLength = 15;
       
    48 
       
    49 const TInt KDefaultImapPortSSL = 993;
       
    50 const TInt KDefaultPopPortSSL  = 995;
       
    51 const TInt KDefaultSmtpPortSSL = 465;
       
    52 
       
    53 // MACROS
       
    54 
       
    55 // LOCAL CONSTANTS AND MACROS
       
    56 _LIT8( KErrorInvalidParameters, "Invalid command parameters" );
       
    57 _LIT8( KErrorCreateFailed, "Mailbox creation failed" );
       
    58 _LIT8( KErrorDeleteFailed, "Mailbox deletion failed" );
       
    59 
       
    60 #ifdef __WINS__
       
    61 _LIT( KWinsImei, "123456789012345" );
       
    62 #else
       
    63 _LIT( KTsyName, "phonetsy" );
       
    64 #endif
       
    65 
       
    66 // MODULE DATA STRUCTURES
       
    67 
       
    68 // LOCAL FUNCTION PROTOTYPES
       
    69 
       
    70 // FORWARD DECLARATIONS
       
    71 
       
    72 // ============================ MEMBER FUNCTIONS ===============================
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CHtiMailboxHandler::NewL
       
    76 // Two-phased constructor.
       
    77 // -----------------------------------------------------------------------------
       
    78 CHtiMailboxHandler* CHtiMailboxHandler::NewL()
       
    79     {
       
    80     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::NewL" );
       
    81     CHtiMailboxHandler* self = new (ELeave) CHtiMailboxHandler();
       
    82     CleanupStack::PushL ( self );
       
    83     self->ConstructL();
       
    84     CleanupStack::Pop();
       
    85     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::NewL" );
       
    86     return self;
       
    87     }
       
    88 
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // CHtiMailboxHandler::CHtiMailboxHandler
       
    92 // C++ default constructor can NOT contain any code, that
       
    93 // might leave.
       
    94 // ----------------------------------------------------------------------------
       
    95 CHtiMailboxHandler::CHtiMailboxHandler()
       
    96     {
       
    97     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::CHtiMailboxHandler" );
       
    98 
       
    99     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::CHtiMailboxHandler" );
       
   100     }
       
   101 
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CHtiMailboxHandler::~CHtiMailboxHandler
       
   105 // Destructor.
       
   106 // -----------------------------------------------------------------------------
       
   107 CHtiMailboxHandler::~CHtiMailboxHandler()
       
   108     {
       
   109     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::~CHtiMailboxHandler" );
       
   110     delete iMailboxName;
       
   111     delete iIncomingServer;
       
   112     delete iIncomingApName;
       
   113     delete iIncomingUserName;
       
   114     delete iIncomingPassword;
       
   115     delete iOutgoingServer;
       
   116     delete iOutgoingApName;
       
   117     delete iOutgoingUserName;
       
   118     delete iOutgoingPassword;
       
   119     delete iOwnMailAddress;
       
   120     delete iOwnName;
       
   121     delete iSignatureText;
       
   122     delete iImapFolderPath;
       
   123     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::~CHtiMailboxHandler" );
       
   124     }
       
   125 
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CHtiMailboxHandler::ConstructL
       
   129 // Symbian 2nd phase constructor can leave.
       
   130 // -----------------------------------------------------------------------------
       
   131 void CHtiMailboxHandler::ConstructL()
       
   132     {
       
   133     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::ConstructL" );
       
   134 
       
   135     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::ConstructL" );
       
   136     }
       
   137 
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CHtiMailboxHandler::SetDispatcher
       
   141 // Sets the dispatcher pointer.
       
   142 // -----------------------------------------------------------------------------
       
   143 
       
   144 void CHtiMailboxHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
       
   145     {
       
   146     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::SetDispatcher" );
       
   147     iDispatcher = aDispatcher;
       
   148     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::SetDispatcher" );
       
   149     }
       
   150 
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CHtiMailboxHandler::ProcessMessageL
       
   154 // Parses the received message and calls handler functions.
       
   155 // -----------------------------------------------------------------------------
       
   156 void CHtiMailboxHandler::ProcessMessageL( const TDesC8& aMessage,
       
   157     THtiMessagePriority /*aPriority*/ )
       
   158     {
       
   159     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::ProcessMessageL" );
       
   160 
       
   161     // Zero length message and command code validity already checked
       
   162     // in HtiMessagesServicePlugin.
       
   163 
       
   164     if ( aMessage[0] == CHtiMessagesServicePlugin::ECreateMailBox )
       
   165         {
       
   166         if ( aMessage.Length() < KMinCreateMsgLength )
       
   167             {
       
   168             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   169             }
       
   170         else
       
   171             {
       
   172             iCmManager.OpenL();
       
   173             TRAPD( err, HandleCreateMailboxL(
       
   174                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   175             if ( err != KErrNone )
       
   176                 {
       
   177                 if ( err == KErrArgument )
       
   178                     {
       
   179                     SendErrorMessageL( err, KErrorInvalidParameters );
       
   180                     }
       
   181                 else
       
   182                     {
       
   183                     HTI_LOG_FORMAT( "Create error %d", err );
       
   184                     SendErrorMessageL( err, KErrorCreateFailed );
       
   185                     }
       
   186                 }
       
   187             else
       
   188                 {
       
   189                 SendOkMsgL( KNullDesC8 );
       
   190                 }
       
   191             ResetVariables();
       
   192             }
       
   193         }
       
   194 
       
   195     else
       
   196         {
       
   197         if ( aMessage.Length() < KMinDeleteMsgLength )
       
   198             {
       
   199             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   200             }
       
   201         else
       
   202             {
       
   203             TRAPD( err, HandleDeleteMailboxL(
       
   204                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   205             if ( err != KErrNone )
       
   206                 {
       
   207                 HTI_LOG_FORMAT( "Delete error %d", err );
       
   208                 SendErrorMessageL( err, KErrorDeleteFailed );
       
   209                 }
       
   210             else
       
   211                 {
       
   212                 SendOkMsgL( KNullDesC8 );
       
   213                 }
       
   214             }
       
   215         }
       
   216 
       
   217     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::ProcessMessageL" );
       
   218     }
       
   219 
       
   220 // ----------------------------------------------------------------------------
       
   221 // CHtiMailboxHandler::HandleCreateMailboxL
       
   222 // Creates new mailbox.
       
   223 // ----------------------------------------------------------------------------
       
   224 void CHtiMailboxHandler::HandleCreateMailboxL( const TDesC8& aData )
       
   225     {
       
   226     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::HandleCreateMailboxL" );
       
   227 
       
   228     ParseCreateMsgL( aData );
       
   229 
       
   230     TSmtpAccount smtpAcc;
       
   231     TInt result = FindSmtpAccount( *iMailboxName, smtpAcc );
       
   232     if ( result != KErrNotFound )
       
   233         {
       
   234         User::Leave( KErrAlreadyExists );
       
   235         }
       
   236 
       
   237     CImImap4Settings* imap4Set = new (ELeave) CImImap4Settings;
       
   238     CleanupStack::PushL( imap4Set );
       
   239     CImPop3Settings* pop3Set = new (ELeave) CImPop3Settings;
       
   240     CleanupStack::PushL( pop3Set );
       
   241     CImSmtpSettings* smtpSet = new (ELeave) CImSmtpSettings;
       
   242     CleanupStack::PushL( smtpSet );
       
   243 
       
   244     CImIAPPreferences* incomingIapSet = CImIAPPreferences::NewLC();
       
   245     CImIAPPreferences* outgoingIapSet = CImIAPPreferences::NewLC();
       
   246 
       
   247     CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   248     accounts->PopulateDefaultSmtpSettingsL( *smtpSet, *outgoingIapSet );
       
   249     if ( iMailboxType == 0 )  // POP3
       
   250         {
       
   251         accounts->PopulateDefaultPopSettingsL( *pop3Set, *incomingIapSet );
       
   252         }
       
   253     else // IMAP4
       
   254         {
       
   255         accounts->PopulateDefaultImapSettingsL( *imap4Set, *incomingIapSet );
       
   256         }
       
   257 
       
   258     // Set access point settings
       
   259     TImIAPChoice imIAPChoice;
       
   260     imIAPChoice.iIAP = 0;
       
   261     imIAPChoice.iDialogPref = ECommDbDialogPrefPrompt;
       
   262 
       
   263     if ( iIncomingApName->Length() > 0 )
       
   264         {
       
   265         User::LeaveIfError(
       
   266             imIAPChoice.iIAP = AccessPointUIDL( *iIncomingApName ) );
       
   267         imIAPChoice.iDialogPref = ECommDbDialogPrefDoNotPrompt;
       
   268         HTI_LOG_TEXT( "Incoming IAP user defined" );
       
   269         }
       
   270     incomingIapSet->AddIAPL( imIAPChoice );
       
   271 
       
   272     imIAPChoice.iIAP = 0;
       
   273     imIAPChoice.iDialogPref = ECommDbDialogPrefPrompt;
       
   274 
       
   275     if ( iOutgoingApName->Length() > 0 )
       
   276         {
       
   277         User::LeaveIfError(
       
   278             imIAPChoice.iIAP = AccessPointUIDL( *iOutgoingApName ) );
       
   279         imIAPChoice.iDialogPref = ECommDbDialogPrefDoNotPrompt;
       
   280         HTI_LOG_TEXT( "Outgoing IAP user defined" );
       
   281         }
       
   282     outgoingIapSet->AddIAPL( imIAPChoice );
       
   283 
       
   284     // Set SMTP settings
       
   285     HTI_LOG_TEXT( "Setting SMTP settings" );
       
   286     smtpSet->SetServerAddressL( *iOutgoingServer );
       
   287     smtpSet->SetLoginNameL( *iOutgoingUserName );
       
   288     smtpSet->SetPasswordL( *iOutgoingPassword );
       
   289     smtpSet->SetEmailAddressL( *iOwnMailAddress );
       
   290     smtpSet->SetReplyToAddressL( *iOwnMailAddress );
       
   291     smtpSet->SetEmailAliasL( *iOwnName );
       
   292     if ( iSignatureText->Length() > 0 )
       
   293         {
       
   294         smtpSet->SetAddSignatureToEmail( ETrue );
       
   295         }
       
   296     else
       
   297         {
       
   298         smtpSet->SetAddSignatureToEmail( EFalse );
       
   299         }
       
   300     smtpSet->SetSendCopyToSelf( (TImSMTPSendCopyToSelf) iSendCopyToOwnAddress );
       
   301     smtpSet->SetSendMessageOption( (TImSMTPSendMessageOption) iSendOption );
       
   302     if ( iOutgoingSecurity == 0 )
       
   303         {
       
   304         smtpSet->SetSecureSockets( EFalse );
       
   305         }
       
   306     else
       
   307         {
       
   308         smtpSet->SetSecureSockets( ETrue );
       
   309         if ( iOutgoingSecurity == 1 )
       
   310             {
       
   311             smtpSet->SetSSLWrapper( EFalse );
       
   312             }
       
   313         else // 2 = SSL
       
   314             {
       
   315             smtpSet->SetSSLWrapper( ETrue );
       
   316             }
       
   317         }
       
   318     if ( iOutgoingPort > 0 )
       
   319         {
       
   320         smtpSet->SetPort( (TUint) iOutgoingPort );
       
   321         }
       
   322     else
       
   323         {
       
   324         if ( iOutgoingSecurity == 2 ) // SSL
       
   325             {
       
   326             smtpSet->SetPort( KDefaultSmtpPortSSL );
       
   327             }
       
   328         else
       
   329             {
       
   330             smtpSet->SetPort( KSMTPDefaultPortNumber );
       
   331             }
       
   332         }
       
   333     smtpSet->SetBodyEncoding( EMsgOutboxMIME );
       
   334     smtpSet->SetAddVCardToEmail( EFalse );
       
   335     smtpSet->SetRequestReceipts( EFalse );
       
   336     smtpSet->SetSMTPAuth( ETrue );
       
   337 
       
   338     TSmtpAccount smtpAccount;
       
   339     TPopAccount popAccount;
       
   340     TImapAccount imapAccount;
       
   341 
       
   342     // Set POP3 settings
       
   343     if ( iMailboxType == 0 )  // POP3
       
   344         {
       
   345         HTI_LOG_TEXT( "Setting POP3 settings" );
       
   346         pop3Set->SetServerAddressL( *iIncomingServer );
       
   347         pop3Set->SetLoginNameL( *iIncomingUserName );
       
   348         pop3Set->SetPasswordL( *iIncomingPassword );
       
   349         pop3Set->SetApop( iAPopSecureLogin );
       
   350         if ( iIncomingSecurity == 0 )
       
   351             {
       
   352             pop3Set->SetSecureSockets( EFalse );
       
   353             }
       
   354         else
       
   355             {
       
   356             pop3Set->SetSecureSockets( ETrue );
       
   357             if ( iIncomingSecurity == 1 )
       
   358                 {
       
   359                 pop3Set->SetSSLWrapper( EFalse );
       
   360                 }
       
   361             else
       
   362                 {
       
   363                 pop3Set->SetSSLWrapper( ETrue );
       
   364                 }
       
   365             }
       
   366         if ( iIncomingPort > 0 )
       
   367             {
       
   368             pop3Set->SetPort( (TUint) iIncomingPort );
       
   369             }
       
   370         else
       
   371             {
       
   372             if ( iIncomingSecurity == 2 ) // SSL
       
   373                 {
       
   374                 pop3Set->SetPort( KDefaultPopPortSSL );
       
   375                 }
       
   376             else
       
   377                 {
       
   378                 pop3Set->SetPort( KPOP3DefaultPortNumber );
       
   379                 }
       
   380             }
       
   381         if ( iRetrievedParts == 0 ) // Only headers
       
   382             {
       
   383             pop3Set->SetGetMailOptions( EGetPop3EmailHeaders );
       
   384             pop3Set->SetPopulationLimitL( -2 ); // -2 = headers only (EmailUtils.h)
       
   385             }
       
   386         else
       
   387             {
       
   388             pop3Set->SetGetMailOptions( EGetPop3EmailMessages );
       
   389             if ( iRetrievedParts == 1 ) // Less than (kb)
       
   390                 {
       
   391                 pop3Set->SetMaxEmailSize( iRetrieveSizeLimitKb );
       
   392                 pop3Set->SetPopulationLimitL( iRetrieveSizeLimitKb );
       
   393                 }
       
   394             else
       
   395                 {
       
   396                 pop3Set->SetPopulationLimitL( -1 ); // -1 = body and attachment (EmailUtils.h)
       
   397                 }
       
   398             }
       
   399 
       
   400         if ( iEmailsToRetrieve > 0 )
       
   401             {
       
   402             pop3Set->SetInboxSynchronisationLimit( iEmailsToRetrieve );
       
   403             }
       
   404         else
       
   405             {
       
   406             // If limit is not set (zero), retrieves all
       
   407             pop3Set->SetInboxSynchronisationLimit( KErrNotFound );
       
   408             }
       
   409 
       
   410         pop3Set->SetAcknowledgeReceipts( EFalse );
       
   411         pop3Set->SetDisconnectedUserMode( ETrue );
       
   412         pop3Set->SetAutoSendOnConnect( ETrue );
       
   413         pop3Set->SetDeleteEmailsWhenDisconnecting( EFalse );
       
   414 
       
   415         popAccount = accounts->CreatePopAccountL(
       
   416             *iMailboxName, *pop3Set, *incomingIapSet, EFalse );
       
   417         HTI_LOG_FORMAT( "Created POP3 account %d", popAccount.iPopAccountId );
       
   418         smtpAccount = accounts->CreateSmtpAccountL(
       
   419             popAccount, *smtpSet, *outgoingIapSet, EFalse );
       
   420         HTI_LOG_FORMAT( "Created SMTP account %d", smtpAccount.iSmtpAccountId );
       
   421         }
       
   422 
       
   423     // Set IMAP4 settings
       
   424     else
       
   425         {
       
   426         HTI_LOG_TEXT( "Setting IMAP4 settings" );
       
   427         imap4Set->SetServerAddressL( *iIncomingServer );
       
   428         imap4Set->SetLoginNameL( *iIncomingUserName );
       
   429         imap4Set->SetPasswordL( *iIncomingPassword );
       
   430         if ( iIncomingSecurity == 0 )
       
   431             {
       
   432             imap4Set->SetSecureSockets( EFalse );
       
   433             }
       
   434         else
       
   435             {
       
   436             imap4Set->SetSecureSockets( ETrue );
       
   437             if ( iIncomingSecurity == 1 )
       
   438                 {
       
   439                 imap4Set->SetSSLWrapper( EFalse );
       
   440                 }
       
   441             else
       
   442                 {
       
   443                 imap4Set->SetSSLWrapper( ETrue );
       
   444                 }
       
   445             }
       
   446         if ( iIncomingPort > 0 )
       
   447             {
       
   448             imap4Set->SetPort( (TUint) iIncomingPort );
       
   449             }
       
   450         else
       
   451             {
       
   452             if ( iIncomingSecurity == 2 ) // SSL
       
   453                 {
       
   454                 imap4Set->SetPort( KDefaultImapPortSSL );
       
   455                 }
       
   456             else
       
   457                 {
       
   458                 imap4Set->SetPort( KIMAPDefaultPortNumber );
       
   459                 }
       
   460             }
       
   461         if ( iEmailsToRetrieve > 0 )
       
   462             {
       
   463             imap4Set->SetInboxSynchronisationLimit( iEmailsToRetrieve );
       
   464             imap4Set->SetMailboxSynchronisationLimit( iEmailsToRetrieve );
       
   465             }
       
   466         else
       
   467             {
       
   468             // If limit is not set (zero), retrieves all
       
   469             imap4Set->SetInboxSynchronisationLimit( KErrNotFound );
       
   470             imap4Set->SetMailboxSynchronisationLimit( KErrNotFound );
       
   471             }
       
   472 
       
   473         imap4Set->SetFolderPathL( *iImapFolderPath );
       
   474 
       
   475         imap4Set->SetGetMailOptions( EGetImap4EmailHeaders );
       
   476         imap4Set->SetPartialMailOptionsL( ENoSizeLimits );
       
   477         imap4Set->SetBodyTextSizeLimitL( KMaxTInt );
       
   478         imap4Set->SetAttachmentSizeLimitL( KMaxTInt );
       
   479         imap4Set->SetAcknowledgeReceipts( EFalse );
       
   480         imap4Set->SetDisconnectedUserMode( ETrue );
       
   481         imap4Set->SetSynchronise( EUseLocal );
       
   482         imap4Set->SetSubscribe( EUpdateNeither );
       
   483         imap4Set->SetAutoSendOnConnect( ETrue );
       
   484         imap4Set->SetDeleteEmailsWhenDisconnecting( EFalse );
       
   485         imap4Set->SetImapIdle( ETrue );
       
   486         imap4Set->SetUpdatingSeenFlags( ETrue );
       
   487 
       
   488         imapAccount = accounts->CreateImapAccountL(
       
   489             *iMailboxName, *imap4Set, *incomingIapSet, EFalse );
       
   490         HTI_LOG_FORMAT( "Created IMAP4 account %d", imapAccount.iImapAccountId );
       
   491         smtpAccount = accounts->CreateSmtpAccountL(
       
   492             imapAccount, *smtpSet, *outgoingIapSet, EFalse );
       
   493         HTI_LOG_FORMAT( "Created SMTP account %d", smtpAccount.iSmtpAccountId );
       
   494         }
       
   495 
       
   496     // Set as default if requested
       
   497      if ( iSetAsDefault )
       
   498         {
       
   499         accounts->SetDefaultSmtpAccountL( smtpAccount );
       
   500         }
       
   501 
       
   502     // accounts, outgoingIapSet, incomingIapSet, smtpSet, pop3Set, imap4set
       
   503     CleanupStack::PopAndDestroy( 6 );
       
   504 
       
   505     // Get IDs of created incoming mail account
       
   506     TMsvId mailboxId = 0;
       
   507     TUint32 accountId = 0;
       
   508     TUid protocol = TUid::Null();
       
   509     if ( iMailboxType == 0 ) // POP3
       
   510         {
       
   511         mailboxId = popAccount.iPopService;
       
   512         accountId = popAccount.iPopAccountId;
       
   513         protocol.iUid = KSenduiMtmPop3UidValue;
       
   514         }
       
   515 
       
   516     else // IMAP4
       
   517         {
       
   518         mailboxId = imapAccount.iImapService;
       
   519         accountId = imapAccount.iImapAccountId;
       
   520         protocol.iUid = KSenduiMtmImap4UidValue;
       
   521         iRetrievedParts = 0;
       
   522         }
       
   523 
       
   524     // Get the created mailbox entries
       
   525     HTI_LOG_TEXT( "Opening MSV session" );
       
   526     CMsvSession* session = CMsvSession::OpenSyncL( *this );
       
   527     CleanupStack::PushL( session );
       
   528     HTI_LOG_TEXT( "Getting mailbox MSV entries" );
       
   529     CMsvEntry* incomingEntry = session->GetEntryL( mailboxId );
       
   530     CleanupStack::PushL( incomingEntry );
       
   531     CMsvEntry* outgoingEntry = session->GetEntryL( smtpAccount.iSmtpService );
       
   532     CleanupStack::PushL( outgoingEntry );
       
   533 
       
   534     // Store account IDs and device IMEI to entries
       
   535     HTI_LOG_TEXT( "Storing account IDs and IMEI" );
       
   536     TBuf<KImeiLength> phoneImei;
       
   537     GetDeviceImeiL( phoneImei );
       
   538     HTI_LOG_FORMAT( "Got IMEI: %S", &phoneImei );
       
   539     TMsvEntry inTEntry = incomingEntry->Entry();
       
   540     inTEntry.iMtmData2 = accountId;
       
   541     inTEntry.iDescription.Set( phoneImei );
       
   542     incomingEntry->ChangeL( inTEntry );
       
   543     TMsvEntry outTEntry = outgoingEntry->Entry();
       
   544     outTEntry.iMtmData2 = smtpAccount.iSmtpAccountId;
       
   545     outTEntry.iDescription.Set( phoneImei );
       
   546     outgoingEntry->ChangeL( outTEntry );
       
   547 
       
   548     // Create the signature text if given
       
   549     if ( iSignatureText->Length() > 0 )
       
   550         {
       
   551         // Get signature store
       
   552         HTI_LOG_TEXT( "Getting signature store" );
       
   553         CMsvStore* store = outgoingEntry->EditStoreL();
       
   554         CleanupStack::PushL( store );
       
   555 
       
   556         // Create signarure rich text
       
   557         HTI_LOG_TEXT( "Creating signature CRichText" );
       
   558         CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
       
   559         CleanupStack::PushL( paraFormatLayer );
       
   560         CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL();
       
   561         CleanupStack::PushL( charFormatLayer );
       
   562         CRichText* signature = CRichText::NewL( paraFormatLayer,
       
   563                                                 charFormatLayer );
       
   564         CleanupStack::PushL( signature );
       
   565         signature->InsertL( 0, *iSignatureText );
       
   566 
       
   567         // Store the signature
       
   568         store->StoreBodyTextL( *signature );
       
   569         store->CommitL();
       
   570 
       
   571         // signature, charFormatLayer, paraFormatLayer, store
       
   572         CleanupStack::PopAndDestroy( 4 );
       
   573         }
       
   574 
       
   575     CleanupStack::PopAndDestroy( 3 ); // outgoingEntry, incomingEntry, session
       
   576     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::HandleCreateMailboxL" );
       
   577     }
       
   578 
       
   579 
       
   580 // ----------------------------------------------------------------------------
       
   581 // CHtiMailboxHandler::HandleDeleteMailboxL
       
   582 // Deletes a mailbox.
       
   583 // ----------------------------------------------------------------------------
       
   584 void CHtiMailboxHandler::HandleDeleteMailboxL( const TDesC8& aData )
       
   585     {
       
   586     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::HandleDeleteMailboxL" );
       
   587     // Deprecated
       
   588     aData.Size();
       
   589     User::Leave( KErrNotSupported );
       
   590     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::HandleDeleteMailboxL" );
       
   591     }
       
   592 
       
   593 
       
   594 // ----------------------------------------------------------------------------
       
   595 // CHtiMailboxHandler::ParseCreateMsgL
       
   596 // Parses the parameters from the create account message.
       
   597 // ----------------------------------------------------------------------------
       
   598 void CHtiMailboxHandler::ParseCreateMsgL( const TDesC8& aData )
       
   599     {
       
   600     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::ParseCreateMsgL" );
       
   601     /*  ACCOUNT SETTINGS
       
   602      *   Field                              Size      Values    Mandatory
       
   603      *   ---------------------------------------------------------------------
       
   604      *   Mailbox type                       1 byte    0,1       YES
       
   605      *   Mailbox name length                1 byte    1-30      YES
       
   606      *   Mailbox name                       1-30                YES
       
   607      *   Incoming mail server length        1 byte    3-50      YES
       
   608      *   Incoming mail server               3-50                YES
       
   609      *   Incoming access point name length  1 byte    0-255     YES
       
   610      *   Incoming access point name         0-255               NO
       
   611      *   Incoming user name length          1 byte    0-50      YES
       
   612      *   Incoming user name                 0-50                NO
       
   613      *   Incoming password length           1 byte    0-50      YES
       
   614      *   Incoming password                  0-50                NO
       
   615      *   Incoming security                  1 byte    0,1,2     YES
       
   616      *   Incoming port                      2 bytes   0-999     YES
       
   617      *   A POP secure login                 1 byte    0,1       YES
       
   618      *   Outgoing mail server length        1 byte    1-50      YES
       
   619      *   Outgoing mail server               1-50                YES
       
   620      *   Outgoing access point name length  1 byte    0-255     YES
       
   621      *   Outgoing access point name         0-255               NO
       
   622      *   Outgoing user name length          1 byte    0-50      YES
       
   623      *   Outgoing user name                 0-50                NO
       
   624      *   Outgoing password length           1 byte    0-50      YES
       
   625      *   Outgoing password                  0-50                NO
       
   626      *   Outgoing security                  1 byte    0,1,2     YES
       
   627      *   Outgoing port                      2 bytes   0-999     YES
       
   628      *   Own mail address length            1 byte    1-100     YES
       
   629      *   Own mail address                   1-100               YES
       
   630      *   Own name length                    1 byte    0-100     YES
       
   631      *   Own name                           0-100               NO
       
   632      *   Send message option                1 byte    0,1,2     YES
       
   633      *   Copy to own mail address           1 byte    0,1,2,3   YES
       
   634      *   Signature length                   2 bytes   0-500     YES
       
   635      *   Signature text                     0-500               NO
       
   636      *   New mail indicators                1 byte    0,1       YES
       
   637      *   Retrieved parts                    1 byte    0,1,2     YES
       
   638      *   Retrieve size limit                2 bytes   1-999     YES
       
   639      *   Emails to retrieve                 2 bytes   0-999     YES
       
   640      *   IMAP4 folder path length           1 byte    0-100     YES
       
   641      *   IMAP4 folder path                  0-100               NO
       
   642      *   Automatic update                   1 byte    0,1,2     YES
       
   643      *   Set as default for outgoing mail   1 byte    0,1       YES
       
   644      */
       
   645 
       
   646     TInt dataLength = aData.Length();
       
   647     HTI_LOG_FORMAT( "Data length = %d", dataLength );
       
   648     TInt offset = 0;
       
   649     iMailboxType = aData[offset];
       
   650     HTI_LOG_FORMAT( "Mailbox type = %d", iMailboxType );
       
   651     if ( iMailboxType != 0 && iMailboxType != 1 )
       
   652         {
       
   653         User::Leave( KErrArgument );
       
   654         }
       
   655     offset++;
       
   656 
       
   657     iMailboxName = ParseStringL( aData, offset );
       
   658     offset += iMailboxName->Length() + 1;
       
   659 
       
   660     iIncomingServer = ParseStringL( aData, offset );
       
   661     offset += iIncomingServer->Length() + 1;
       
   662 
       
   663 
       
   664     iIncomingApName = ParseStringL( aData, offset );
       
   665     offset += iIncomingApName->Length() + 1;
       
   666 
       
   667     iIncomingUserName = ParseString8L( aData, offset );
       
   668     offset += iIncomingUserName->Length() + 1;
       
   669 
       
   670     iIncomingPassword = ParseString8L( aData, offset );
       
   671     offset += iIncomingPassword->Length() + 1;
       
   672 
       
   673     // Check that it's safe to read next 4 bytes
       
   674     if ( offset + 3 >= dataLength )
       
   675         {
       
   676         User::Leave( KErrArgument );
       
   677         }
       
   678 
       
   679     iIncomingSecurity = aData[offset];
       
   680     offset++;
       
   681     HTI_LOG_FORMAT( "Incoming security = %d", iIncomingSecurity );
       
   682 
       
   683     iIncomingPort = aData[offset] + ( aData[offset+1] << 8 );
       
   684     offset += 2;
       
   685     HTI_LOG_FORMAT( "Incoming port = %d", iIncomingPort );
       
   686 
       
   687     iAPopSecureLogin = (TBool) aData[offset];
       
   688     offset++;
       
   689     HTI_LOG_FORMAT( "A POP secure login = %d", iAPopSecureLogin );
       
   690 
       
   691     iOutgoingServer = ParseStringL( aData, offset );
       
   692     offset += iOutgoingServer->Length() + 1;
       
   693 
       
   694     iOutgoingApName = ParseStringL( aData, offset );
       
   695     offset += iOutgoingApName->Length() + 1;
       
   696 
       
   697     iOutgoingUserName = ParseString8L( aData, offset );
       
   698     offset += iOutgoingUserName->Length() + 1;
       
   699 
       
   700     iOutgoingPassword = ParseString8L( aData, offset );
       
   701     offset += iOutgoingPassword->Length() + 1;
       
   702 
       
   703     // Check that it's safe to read next 3 bytes
       
   704     if ( offset + 2 >= dataLength )
       
   705         {
       
   706         User::Leave( KErrArgument );
       
   707         }
       
   708 
       
   709     iOutgoingSecurity = aData[offset];
       
   710     offset++;
       
   711     HTI_LOG_FORMAT( "Outgoing security = %d", iOutgoingSecurity );
       
   712 
       
   713     iOutgoingPort = aData[offset] + ( aData[offset+1] << 8 );
       
   714     offset += 2;
       
   715     HTI_LOG_FORMAT( "Outgoing port = %d", iOutgoingPort );
       
   716 
       
   717     iOwnMailAddress = ParseStringL( aData, offset );
       
   718     offset += iOwnMailAddress->Length() + 1;
       
   719 
       
   720     iOwnName = ParseStringL( aData, offset );
       
   721     offset += iOwnName->Length() + 1;
       
   722 
       
   723     // Check that it's safe to read next 2 bytes
       
   724     if ( offset + 1 >= dataLength )
       
   725         {
       
   726         User::Leave( KErrArgument );
       
   727         }
       
   728 
       
   729     iSendOption = aData[offset];
       
   730     offset++;
       
   731     HTI_LOG_FORMAT( "Send option = %d", iSendOption );
       
   732 
       
   733     iSendCopyToOwnAddress = aData[offset];
       
   734     offset++;
       
   735     HTI_LOG_FORMAT( "Send copy = %d", iSendCopyToOwnAddress );
       
   736 
       
   737     iSignatureText = ParseStringL( aData, offset, 2 );
       
   738     offset += iSignatureText->Length() + 2;
       
   739 
       
   740     // Check that it's safe to read next 6 bytes
       
   741     if ( offset + 5 >= dataLength )
       
   742         {
       
   743         User::Leave( KErrArgument );
       
   744         }
       
   745 
       
   746     iNewMailIndicators = (TBool) aData[offset];
       
   747     offset++;
       
   748     HTI_LOG_FORMAT( "Indicators = %d", iNewMailIndicators );
       
   749 
       
   750     iRetrievedParts = aData[offset];
       
   751     offset++;
       
   752     HTI_LOG_FORMAT( "Retrieved parts = %d", iRetrievedParts );
       
   753 
       
   754     iRetrieveSizeLimitKb = aData[offset] + ( aData[offset+1] << 8 );
       
   755     offset += 2;
       
   756     HTI_LOG_FORMAT( "Size limit = %d", iRetrieveSizeLimitKb );
       
   757 
       
   758     iEmailsToRetrieve = aData[offset] + ( aData[offset+1] << 8 );
       
   759     offset += 2;
       
   760     HTI_LOG_FORMAT( "Emails to retrieve = %d", iEmailsToRetrieve );
       
   761 
       
   762     iImapFolderPath = ParseString8L( aData, offset );
       
   763     offset += iImapFolderPath->Length() + 1;
       
   764 
       
   765     // Check that it's safe to read next 2 bytes
       
   766     if ( offset + 1 >= dataLength )
       
   767         {
       
   768         User::Leave( KErrArgument );
       
   769         }
       
   770 
       
   771     iAutomaticUpdate = aData[offset];
       
   772     offset++;
       
   773     HTI_LOG_FORMAT( "Automatic update = %d", iAutomaticUpdate );
       
   774 
       
   775     iSetAsDefault = (TBool) aData[offset];
       
   776     offset++;
       
   777     HTI_LOG_FORMAT( "Set as default = %d", iSetAsDefault );
       
   778 
       
   779     HTI_LOG_FORMAT( "Final offset= %d", offset );
       
   780     // Check that there's no extra bytes
       
   781     if ( offset != dataLength )
       
   782         {
       
   783         User::Leave( KErrArgument );
       
   784         }
       
   785 
       
   786     // Validate fields - limits based on S60 3.0 mailbox settings UI
       
   787     if ( iMailboxName->Length()      < 1   ||
       
   788          iMailboxName->Length()      > 30  ||
       
   789          iIncomingServer->Length()   < 3   ||    // a.b
       
   790          iIncomingServer->Length()   > 50  ||
       
   791          iOutgoingServer->Length()   < 3   ||    // a.b
       
   792          iOutgoingServer->Length()   > 50  ||
       
   793          iOwnMailAddress->Length()   < 5   ||    // a@b.c
       
   794          iOwnMailAddress->Length()   > 100 ||
       
   795          iIncomingUserName->Length() > 50 ||
       
   796          iIncomingPassword->Length() > 50 ||
       
   797          iOutgoingUserName->Length() > 50 ||
       
   798          iOutgoingPassword->Length() > 50 ||
       
   799          iOwnName->Length()          > 100 ||
       
   800          iSignatureText->Length()    > 500 ||
       
   801          iImapFolderPath->Length()   > 100 ||
       
   802          iIncomingPort               > 999 ||
       
   803          iOutgoingPort               > 999 ||
       
   804          iRetrieveSizeLimitKb        > 999 ||
       
   805          iEmailsToRetrieve           > 999 ||
       
   806          iIncomingSecurity           > 2   ||
       
   807          iOutgoingSecurity           > 2   ||
       
   808          iSendOption                 > 2   ||
       
   809          iSendCopyToOwnAddress       > 3   ||
       
   810          iRetrievedParts             > 2   ||
       
   811          iAutomaticUpdate            > 2      )
       
   812         {
       
   813         User::Leave( KErrArgument );
       
   814         }
       
   815 
       
   816     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::ParseCreateMsgL" );
       
   817     }
       
   818 
       
   819 
       
   820 // ----------------------------------------------------------------------------
       
   821 // CHtiMailboxHandler::ParseStringL
       
   822 // Helper for parsing one string from the message.
       
   823 // ----------------------------------------------------------------------------
       
   824 HBufC* CHtiMailboxHandler::ParseStringL( const TDesC8& aData,
       
   825                                          TInt aStrLenOffset,
       
   826                                          TInt aStrLenBytes )
       
   827     {
       
   828     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::ParseStringL" );
       
   829     if ( aStrLenOffset >= aData.Length() ||
       
   830          aStrLenBytes > 2 || aStrLenBytes < 1 )
       
   831         {
       
   832         User::Leave( KErrArgument );
       
   833         }
       
   834     TInt strLen = 0;
       
   835     if ( aStrLenBytes == 1 )
       
   836         {
       
   837         strLen = aData[aStrLenOffset];
       
   838         }
       
   839     else
       
   840         {
       
   841         strLen = aData[aStrLenOffset] + ( aData[aStrLenOffset +1 ] << 8 );
       
   842         }
       
   843 
       
   844     if ( aData.Length() <= aStrLenOffset + strLen )
       
   845         {
       
   846         User::Leave( KErrArgument );
       
   847         }
       
   848     HTI_LOG_FORMAT( "String length = %d", strLen );
       
   849     HBufC* result = HBufC::NewL( strLen );
       
   850     result->Des().Copy( aData.Mid( aStrLenOffset + aStrLenBytes, strLen ) );
       
   851     HTI_LOG_FORMAT( "Parsed: %S", result );
       
   852     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::ParseStringL" );
       
   853     return result;
       
   854     }
       
   855 
       
   856 
       
   857 // ----------------------------------------------------------------------------
       
   858 // CHtiMailboxHandler::ParseString8L
       
   859 // Helper for parsing one string from the message.
       
   860 // ----------------------------------------------------------------------------
       
   861 HBufC8* CHtiMailboxHandler::ParseString8L( const TDesC8& aData,
       
   862                                            TInt aStrLenOffset,
       
   863                                            TInt aStrLenBytes )
       
   864     {
       
   865     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::ParseString8L" );
       
   866     if ( aStrLenOffset >= aData.Length() ||
       
   867          aStrLenBytes > 2 || aStrLenBytes < 1  )
       
   868         {
       
   869         User::Leave( KErrArgument );
       
   870         }
       
   871     TInt strLen = 0;
       
   872     if ( aStrLenBytes == 1 )
       
   873         {
       
   874         strLen = aData[aStrLenOffset];
       
   875         }
       
   876     else
       
   877         {
       
   878         strLen = aData[aStrLenOffset] + ( aData[aStrLenOffset + 1] << 8 );
       
   879         }
       
   880     if ( aData.Length() <= aStrLenOffset + strLen )
       
   881         {
       
   882         User::Leave( KErrArgument );
       
   883         }
       
   884     HTI_LOG_FORMAT( "String length = %d", strLen );
       
   885     HBufC8* result = aData.Mid( aStrLenOffset + aStrLenBytes, strLen ).AllocL();
       
   886     HTI_LOG_FORMAT( "Parsed: %S", result );
       
   887     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::ParseString8L" );
       
   888     return result;
       
   889     }
       
   890 
       
   891 
       
   892 // ----------------------------------------------------------------------------
       
   893 // CHtiMailboxHandler::AccessPointUIDL
       
   894 // Gets the UID of Access Point named by aApName.
       
   895 // Returns KErrNotFound if AP not found.
       
   896 // ----------------------------------------------------------------------------
       
   897 TInt CHtiMailboxHandler::AccessPointUIDL( const TDesC& aApName )
       
   898     {
       
   899     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::AccessPointUIDL" );
       
   900     TInt iapId = KErrNotFound;
       
   901 
       
   902     // Search from uncategorised first
       
   903     RArray<TUint32> array = RArray<TUint32>();
       
   904     iCmManager.ConnectionMethodL( array );
       
   905     CleanupClosePushL( array );
       
   906     TInt i = 0;
       
   907     while ( i < array.Count() && iapId == KErrNotFound )
       
   908         {
       
   909         RCmConnectionMethod cm = iCmManager.ConnectionMethodL( array[i] );
       
   910         CleanupClosePushL( cm );
       
   911         HBufC* name = cm.GetStringAttributeL( CMManager::ECmName );
       
   912         HTI_LOG_FORMAT( "Found name: %S", name );
       
   913         CleanupStack::PushL( name );
       
   914         if ( aApName.Compare( *name ) == 0 )
       
   915             {
       
   916             iapId = cm.GetIntAttributeL( CMManager::ECmId );
       
   917             HTI_LOG_FORMAT( "Match: UID = %d", iapId );
       
   918             }
       
   919         CleanupStack::PopAndDestroy(); // name
       
   920         CleanupStack::PopAndDestroy(); // cm
       
   921         i++;
       
   922         }
       
   923     CleanupStack::PopAndDestroy(); // array
       
   924 
       
   925     // If not found from uncategorised, search from all destinations
       
   926     if ( iapId == KErrNotFound )
       
   927         {
       
   928         HTI_LOG_TEXT( "Not found from uncategorised" );
       
   929         RArray<TUint32> destIdArray = RArray<TUint32>();
       
   930         iCmManager.AllDestinationsL( destIdArray );
       
   931         CleanupClosePushL( destIdArray );
       
   932         i = 0;
       
   933         while ( i < destIdArray.Count() && iapId == KErrNotFound )
       
   934             {
       
   935             RCmDestination dest = iCmManager.DestinationL( destIdArray[i] );
       
   936             CleanupClosePushL( dest );
       
   937             TInt j = 0;
       
   938             while ( j < dest.ConnectionMethodCount() && iapId == KErrNotFound )
       
   939                 {
       
   940                 HBufC* name = dest.ConnectionMethodL( j ).GetStringAttributeL(
       
   941                         CMManager::ECmName );
       
   942                 CleanupStack::PushL( name );
       
   943                 HTI_LOG_FORMAT( "Found name: %S", name );
       
   944                 if ( aApName.Compare( *name ) == 0 )
       
   945                     {
       
   946                     iapId = dest.ConnectionMethodL( j ).GetIntAttributeL(
       
   947                             CMManager::ECmId );
       
   948                     HTI_LOG_FORMAT( "Match: UID = %d", iapId );
       
   949                     }
       
   950                 CleanupStack::PopAndDestroy(); // name
       
   951                 j++;
       
   952                 }
       
   953             CleanupStack::PopAndDestroy(); // dest
       
   954             i++;
       
   955             }
       
   956         CleanupStack::PopAndDestroy(); // destIdArray
       
   957         }
       
   958 
       
   959     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::AccessPointUIDL" );
       
   960     return iapId;
       
   961     }
       
   962 
       
   963 
       
   964 // ----------------------------------------------------------------------------
       
   965 // CHtiMailboxHandler::FindSmtpAccount
       
   966 // Searches for an SMTP account by name.
       
   967 // Returns KErrNone if found, otherwise KErrNotFound or other error.
       
   968 // ----------------------------------------------------------------------------
       
   969 TInt CHtiMailboxHandler::FindSmtpAccount( const TDesC& aAccountName,
       
   970                                           TSmtpAccount& aAccount )
       
   971     {
       
   972     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::FindSmtpAccount" );
       
   973 
       
   974     TInt err = KErrNone;
       
   975     TInt popCount = 0;
       
   976 
       
   977     RArray<TSmtpAccount> smtpAccounts;
       
   978     CleanupClosePushL( smtpAccounts );
       
   979     popCount++; // smtpAccounts
       
   980 
       
   981     CEmailAccounts* accounts = NULL;
       
   982     TRAP( err, accounts = CEmailAccounts::NewL() );
       
   983 
       
   984     if ( err == KErrNone )
       
   985         {
       
   986         CleanupStack::PushL( accounts );
       
   987         popCount++; // accounts
       
   988         TRAP( err, accounts->GetSmtpAccountsL( smtpAccounts ) );
       
   989         }
       
   990 
       
   991     if ( err == KErrNone )
       
   992         {
       
   993         TInt count = smtpAccounts.Count();
       
   994         HTI_LOG_FORMAT( "Found %d SMTP accounts", count );
       
   995         err = KErrNotFound;
       
   996         for ( TInt i = 0; i < count; i++ )
       
   997             {
       
   998             aAccount = smtpAccounts[i];
       
   999             HTI_LOG_FORMAT( "Account name: %S", &aAccount.iSmtpAccountName );
       
  1000             if ( aAccount.iSmtpAccountName.Compare( aAccountName ) == 0 )
       
  1001                 {
       
  1002                 HTI_LOG_FORMAT( "Match - MSV ID %d", aAccount.iSmtpService );
       
  1003                 err = KErrNone;
       
  1004                 break;
       
  1005                 }
       
  1006             }
       
  1007         }
       
  1008 
       
  1009     CleanupStack::PopAndDestroy( popCount );
       
  1010     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::FindSmtpAccount" );
       
  1011     return err;
       
  1012     }
       
  1013 
       
  1014 
       
  1015 // ----------------------------------------------------------------------------
       
  1016 // CHtiMailboxHandler::GetDeviceImeiL()
       
  1017 // Gets the device IMEI code and stores it to aResult.
       
  1018 // ----------------------------------------------------------------------------
       
  1019 //
       
  1020 void CHtiMailboxHandler::GetDeviceImeiL( TDes& aResult )
       
  1021     {
       
  1022     if ( aResult.MaxLength() < KImeiLength )
       
  1023         {
       
  1024         User::Leave( KErrBadDescriptor );
       
  1025         }
       
  1026 #ifdef __WINS__
       
  1027     aResult.Copy( KWinsImei );
       
  1028 #else
       
  1029     RTelServer telServer;
       
  1030     User::LeaveIfError( telServer.Connect() );
       
  1031     CleanupClosePushL( telServer );
       
  1032     User::LeaveIfError( telServer.LoadPhoneModule( KTsyName ) );
       
  1033 
       
  1034     TInt phoneCount = 0;
       
  1035     User::LeaveIfError( telServer.EnumeratePhones( phoneCount ) );
       
  1036     if ( phoneCount < 1 )
       
  1037         {
       
  1038         User::Leave( KErrNotFound );
       
  1039         }
       
  1040 
       
  1041     RTelServer::TPhoneInfo phoneInfo;
       
  1042     User::LeaveIfError( telServer.GetPhoneInfo( 0, phoneInfo ) );
       
  1043     RMobilePhone mobilePhone;
       
  1044     User::LeaveIfError( mobilePhone.Open( telServer, phoneInfo.iName ) );
       
  1045     CleanupClosePushL( mobilePhone );
       
  1046 
       
  1047     RMobilePhone::TMobilePhoneIdentityV1 identity;
       
  1048     TRequestStatus status;
       
  1049     mobilePhone.GetPhoneId( status, identity );
       
  1050     User::WaitForRequest( status );
       
  1051     User::LeaveIfError( status.Int() );
       
  1052 
       
  1053     aResult.Copy( identity.iSerialNumber );
       
  1054     CleanupStack::PopAndDestroy( 2 ); // mobilePhone, telServer
       
  1055 #endif
       
  1056     }
       
  1057 
       
  1058 
       
  1059 // ----------------------------------------------------------------------------
       
  1060 // CHtiMailboxHandler::ResetVariables
       
  1061 // Clears the instance variables that store the create message parameters.
       
  1062 // ----------------------------------------------------------------------------
       
  1063 void CHtiMailboxHandler::ResetVariables()
       
  1064     {
       
  1065     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::ResetVariables" );
       
  1066     delete iMailboxName;
       
  1067     iMailboxName = NULL;
       
  1068     delete iIncomingServer;
       
  1069     iIncomingServer = NULL;
       
  1070     delete iIncomingApName;
       
  1071     iIncomingApName = NULL;
       
  1072     delete iIncomingUserName;
       
  1073     iIncomingUserName = NULL;
       
  1074     delete iIncomingPassword;
       
  1075     iIncomingPassword = NULL;
       
  1076     delete iOutgoingServer;
       
  1077     iOutgoingServer = NULL;
       
  1078     delete iOutgoingApName;
       
  1079     iOutgoingApName = NULL;
       
  1080     delete iOutgoingUserName;
       
  1081     iOutgoingUserName = NULL;
       
  1082     delete iOutgoingPassword;
       
  1083     iOutgoingPassword = NULL;
       
  1084     delete iOwnMailAddress;
       
  1085     iOwnMailAddress = NULL;
       
  1086     delete iOwnName;
       
  1087     iOwnName = NULL;
       
  1088     delete iSignatureText;
       
  1089     iSignatureText = NULL;
       
  1090     delete iImapFolderPath;
       
  1091     iImapFolderPath = NULL;
       
  1092 
       
  1093     iMailboxType          = KErrUnknown;
       
  1094     iIncomingSecurity     = KErrUnknown;
       
  1095     iIncomingPort         = KErrUnknown;
       
  1096     iOutgoingSecurity     = KErrUnknown;
       
  1097     iOutgoingPort         = KErrUnknown;
       
  1098     iSendOption           = KErrUnknown;
       
  1099     iSendCopyToOwnAddress = KErrUnknown;
       
  1100     iRetrievedParts       = KErrUnknown;
       
  1101     iRetrieveSizeLimitKb  = KErrUnknown;
       
  1102     iEmailsToRetrieve     = KErrUnknown;
       
  1103     iAutomaticUpdate      = KErrUnknown;
       
  1104 
       
  1105     iAPopSecureLogin   = EFalse;
       
  1106     iNewMailIndicators = EFalse;
       
  1107     iSetAsDefault      = EFalse;
       
  1108 
       
  1109     iCmManager.Close();
       
  1110 
       
  1111     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::ResetVariables" );
       
  1112     }
       
  1113 
       
  1114 
       
  1115 // ----------------------------------------------------------------------------
       
  1116 // CHtiMailboxHandler::SendOkMsgL
       
  1117 // Helper function for sending response messages.
       
  1118 // ----------------------------------------------------------------------------
       
  1119 void CHtiMailboxHandler::SendOkMsgL( const TDesC8& aData )
       
  1120     {
       
  1121     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::SendOkMsgL" );
       
  1122     User::LeaveIfNull( iDispatcher );
       
  1123     HBufC8* temp = HBufC8::NewL( aData.Length() + 1 );
       
  1124     TPtr8 response = temp->Des();
       
  1125     response.Append( ( TChar ) CHtiMessagesServicePlugin::EResultOk );
       
  1126     response.Append( aData );
       
  1127     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
  1128         temp, KHtiMessagesServiceUid ) );
       
  1129     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::SendOkMsgL" );
       
  1130     }
       
  1131 
       
  1132 
       
  1133 // ----------------------------------------------------------------------------
       
  1134 // CHtiMailboxHandler::SendErrorMessageL
       
  1135 // Helper function for sending error response messages.
       
  1136 // ----------------------------------------------------------------------------
       
  1137 void CHtiMailboxHandler::SendErrorMessageL( TInt aError, const TDesC8& aDescription )
       
  1138     {
       
  1139     HTI_LOG_FUNC_IN( "CHtiMailboxHandler::SendErrorMessageL" );
       
  1140     User::LeaveIfNull( iDispatcher );
       
  1141     User::LeaveIfError( iDispatcher->DispatchOutgoingErrorMessage(
       
  1142         aError, aDescription, KHtiMessagesServiceUid ) );
       
  1143     HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::SendErrorMessageL" );
       
  1144     }
       
  1145 
       
  1146 
       
  1147 // ----------------------------------------------------------------------------
       
  1148 // CHtiMailboxHandler::HandleSessionEventL
       
  1149 // From MMsvSessionObserver.
       
  1150 // ----------------------------------------------------------------------------
       
  1151 void CHtiMailboxHandler::HandleSessionEventL( TMsvSessionEvent /*aEvent*/,
       
  1152                             TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/ )
       
  1153     {
       
  1154     // HTI_LOG_FUNC_IN( "CHtiMailboxHandler::HandleSessionEventL" );
       
  1155 
       
  1156     // HTI_LOG_FUNC_OUT( "CHtiMailboxHandler::HandleSessionEventL" );
       
  1157     }
       
  1158 
       
  1159 // End of file