htiui/HtiServicePlugins/HtiMessagesServicePlugin/src/MessageMgmntHandler.cpp
changeset 38 aca9d8bb5fbc
parent 31 e7a04a6385be
child 39 a5449825eaf3
equal deleted inserted replaced
31:e7a04a6385be 38:aca9d8bb5fbc
     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:  Functional implementation of HtiMessagesServicePlugin service
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HtiMessagesServicePlugin.h"
       
    21 #include "MessageMgmntHandler.h"
       
    22 
       
    23 #include <HtiDispatcherInterface.h>
       
    24 #include <HtiLogging.h>
       
    25 #include <mtclreg.h>
       
    26 #include <smscmds.h>
       
    27 #include <smtcmtm.h>
       
    28 #include <smuthdr.h>
       
    29 #include <smsclnt.h>
       
    30 #include <utf.h>
       
    31 #include <mmsclient.h>
       
    32 #include <mmsconst.h>
       
    33 #include <miutset.h>
       
    34 #include <irmsgtypeuid.h>
       
    35 #include <btmsgtypeuid.h>
       
    36 #include <biouids.h>
       
    37 #include <apgcli.h>
       
    38 #include <apmstd.h>
       
    39 #include <bautils.h>
       
    40 #include <obexclientmtm.h>
       
    41 #include <cmsvmimeheaders.h>
       
    42 #include <mmsvattachmentmanager.h>
       
    43 
       
    44 // CONSTANTS
       
    45 _LIT8( KErrorMissingCommand,        "Command was not given - message was empty" );
       
    46 _LIT8( KErrorUnrecognizedCommand,   "Unrecognized command" );
       
    47 _LIT8( KErrorInvalidParameters,     "Invalid command parameters");
       
    48 _LIT8( KErrorTooLongSmsBody,        "Too long SMS body" );
       
    49 _LIT8( KErrorInvalidId,             "Invalid SMS id parameter" );
       
    50 _LIT8( KErrorInvalidFolder,         "Invalid folder parameter" );
       
    51 _LIT8( KErrorItemNotFound,          "Item not found" );
       
    52 _LIT8( KErrorFailedDelete,          "Failed to delete item" );
       
    53 _LIT8( KErrorNotSupported,          "Not supported" );
       
    54 _LIT8( KErrorSmsSettingNotDefined,  "SMS settings not defined" );
       
    55 _LIT8( KErrorMmsSettingNotDefined,  "MMS settings not defined" );
       
    56 _LIT8( KErrorMailboxNotDefined,     "Mailbox not defined" );
       
    57 _LIT8( KErrorMsgTypeNotFound,       "Message type module not found" );
       
    58 _LIT8( KErrorMsgStoreOpenFailed,    "Could not open message store" );
       
    59 _LIT8( KErrorRfsConnectFailed,      "Could not connect to file server session" );
       
    60 _LIT8( KErrorAttachmentNotFound,    "Attachment not found" );
       
    61 _LIT8( KErrorInvalidFolderForSmartMsg, "Only inbox allowed for smart messages" );
       
    62 
       
    63 const static TInt KAddSmsCmdMinLength        = 7;
       
    64 const static TInt KAddMmsOrEmailCmdMinLength = 8;
       
    65 const static TInt KAddObexMsgCmdMinLength    = 6;
       
    66 const static TInt KAddSmartMsgCmdMinLength   = 11;
       
    67 const static TInt KAddAudioCmdMinLength      = 10;
       
    68 
       
    69 // ----------------------------------------------------------------------------
       
    70 CMessageMgmntHandler* CMessageMgmntHandler::NewL()
       
    71     {
       
    72     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::NewL" );
       
    73     CMessageMgmntHandler* self = new (ELeave) CMessageMgmntHandler();
       
    74     CleanupStack::PushL ( self );
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop();
       
    77     HTI_LOG_FUNC_OUT( "CMessageMgmntHandler::NewL: Done" );
       
    78     return self;
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 CMessageMgmntHandler::CMessageMgmntHandler()
       
    83     {
       
    84     }
       
    85 
       
    86 // ----------------------------------------------------------------------------
       
    87 CMessageMgmntHandler::~CMessageMgmntHandler()
       
    88     {
       
    89     delete iMtmReg;
       
    90     delete iSession;
       
    91     }
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 void CMessageMgmntHandler::ConstructL()
       
    95     {
       
    96     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::ConstructL" );
       
    97     iSession = CMsvSession::OpenSyncL( *this );
       
    98     iMtmReg = CClientMtmRegistry::NewL( *iSession );
       
    99     HTI_LOG_FUNC_OUT( "CMessageMgmntHandler::ConstructL: Done" );
       
   100     }
       
   101 
       
   102 // ----------------------------------------------------------------------------
       
   103 void CMessageMgmntHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
       
   104     {
       
   105     iDispatcher = aDispatcher;
       
   106     }
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 void CMessageMgmntHandler::ProcessMessageL( const TDesC8& aMessage,
       
   110                                             THtiMessagePriority /*aPriority*/ )
       
   111     {
       
   112     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::ProcessMessageL" );
       
   113     HTI_LOG_FORMAT( "Msg length: %d", aMessage.Length() );
       
   114 
       
   115     if ( aMessage.Length() == 0 )
       
   116         {
       
   117         SendErrorMessageL( KErrArgument, KErrorMissingCommand );
       
   118         return;
       
   119         }
       
   120 
       
   121     switch ( aMessage[0] )
       
   122         {
       
   123         case CHtiMessagesServicePlugin::EAddSms:
       
   124             HTI_LOG_TEXT( "Add SMS" );
       
   125             HandleCreateSmsL( aMessage.Right( aMessage.Length() - 1 ) );
       
   126             break;
       
   127 
       
   128         case CHtiMessagesServicePlugin::EAddMms:
       
   129         case CHtiMessagesServicePlugin::EAddAudioMsg: // special MMS sub type
       
   130             HTI_LOG_TEXT( "Add MMS" );
       
   131             HandleCreateMmsL( aMessage );
       
   132             break;
       
   133 
       
   134         case CHtiMessagesServicePlugin::EAddEmail:
       
   135             HTI_LOG_TEXT( "Add Email" );
       
   136             HandleCreateEmailL( aMessage );
       
   137             break;
       
   138 
       
   139         case CHtiMessagesServicePlugin::EAddIrMsg:
       
   140             HTI_LOG_TEXT( "Add IR msg" );
       
   141             HandleCreateObexMsgL( aMessage.Right( aMessage.Length() - 1 ),
       
   142                                   TUid::Uid( KUidMsgTypeIrTInt32 ),
       
   143                                   KUidMsgTypeIrUID );
       
   144             break;
       
   145 
       
   146         case CHtiMessagesServicePlugin::EAddBtMsg:
       
   147             HTI_LOG_TEXT( "Add BT msg" );
       
   148             HandleCreateObexMsgL( aMessage.Right( aMessage.Length() - 1 ),
       
   149                                   TUid::Uid( KUidMsgTypeBtTInt32 ),
       
   150                                   KUidMsgTypeBt );
       
   151             break;
       
   152 
       
   153         case CHtiMessagesServicePlugin::EAddSmartMsg:
       
   154             HTI_LOG_TEXT( "Add smart msg" );
       
   155             HandleCreateSmartMsgL( aMessage.Right( aMessage.Length() - 1 ) );
       
   156             break;
       
   157 
       
   158         case CHtiMessagesServicePlugin::EDeleteMessage:
       
   159             HTI_LOG_TEXT( "Delete message" );
       
   160             HandleDeleteMessageL( aMessage.Right( aMessage.Length() - 1 ) );
       
   161             break;
       
   162 
       
   163         case CHtiMessagesServicePlugin::EDeleteFolderContent:
       
   164             HTI_LOG_TEXT( "Delete messages" );
       
   165             HandleDeleteMessagesL( aMessage.Right( aMessage.Length() - 1 ) );
       
   166             break;
       
   167 
       
   168         default:
       
   169             HTI_LOG_TEXT( "Unknown command" );
       
   170             SendErrorMessageL( KErrUnknown, KErrorUnrecognizedCommand );
       
   171             break;
       
   172         }
       
   173 
       
   174     HTI_LOG_FUNC_OUT( "CMessageMgmntHandler::ProcessMessageL: Done" );
       
   175     }
       
   176 
       
   177 // ----------------------------------------------------------------------------
       
   178 void CMessageMgmntHandler::HandleCreateSmsL( const TDesC8& aData )
       
   179     {
       
   180     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleSmsImportFuncL" );
       
   181 
       
   182     if ( ValidateAddSmsCommand( aData ) )
       
   183         {
       
   184         TInt position( 0 );
       
   185         HBufC16* fromTo = ExtractDesLC( aData, position, 1 );
       
   186         HBufC16* description = ExtractDesLC( aData, position, 1 );
       
   187         HBufC16* body = ExtractDesLC( aData, position, 2 );
       
   188         TBool isNew = (TBool)aData[position];
       
   189         TBool isUnread = (TBool)aData[position+1];
       
   190         TFolder folder = (TFolder)aData[position+2];
       
   191 
       
   192         CSmsClientMtm* smsMtm = NULL;
       
   193         TRAPD( err, smsMtm = ( CSmsClientMtm* )iMtmReg->NewMtmL(
       
   194                 KUidMsgTypeSMS ) );
       
   195         if ( err || !smsMtm )
       
   196             {
       
   197             HTI_LOG_TEXT( "SMS message type module not found" );
       
   198             SendErrorMessageL( KErrNotFound, KErrorMsgTypeNotFound );
       
   199             CleanupStack::PopAndDestroy( body );
       
   200             CleanupStack::PopAndDestroy( description );
       
   201             CleanupStack::PopAndDestroy( fromTo );
       
   202             return;
       
   203             }
       
   204         CleanupStack::PushL( smsMtm );
       
   205 
       
   206         CMsvEntry* entry = CMsvEntry::NewL( *iSession,
       
   207                                             KMsvGlobalInBoxIndexEntryId,
       
   208                                             TMsvSelectionOrdering() );
       
   209         CleanupStack::PushL( entry );
       
   210 
       
   211         // get the default service
       
   212         TMsvId defaultServiceId = 0;
       
   213         TRAP( err, defaultServiceId = smsMtm->DefaultServiceL() );
       
   214         if ( err )
       
   215             {
       
   216             HTI_LOG_FORMAT( "Could not get default service, err: %d", err );
       
   217             SendErrorMessageL( err, KErrorSmsSettingNotDefined );
       
   218             CleanupStack::PopAndDestroy( entry );
       
   219             CleanupStack::PopAndDestroy( smsMtm );
       
   220             CleanupStack::PopAndDestroy( body );
       
   221             CleanupStack::PopAndDestroy( description );
       
   222             CleanupStack::PopAndDestroy( fromTo );
       
   223             return;
       
   224             }
       
   225 
       
   226         // map the folder parameter to folder id
       
   227         TMsvId folderId = KMsvGlobalInBoxIndexEntryId;
       
   228         TRAP( err, folderId = MapFolderToIdL( folder ) );
       
   229         if ( err )
       
   230             {
       
   231             HTI_LOG_FORMAT( "Invalid folder: %d", folder );
       
   232             SendErrorMessageL( err, KErrorInvalidFolder );
       
   233             CleanupStack::PopAndDestroy( entry );
       
   234             CleanupStack::PopAndDestroy( smsMtm );
       
   235             CleanupStack::PopAndDestroy( body );
       
   236             CleanupStack::PopAndDestroy( description );
       
   237             CleanupStack::PopAndDestroy( fromTo );
       
   238             return;
       
   239             }
       
   240         entry->SetEntryL( folderId );
       
   241 
       
   242         // mtm takes ownership of entry context
       
   243         smsMtm->SetCurrentEntryL( entry );
       
   244         CleanupStack::Pop( entry );
       
   245 
       
   246         // create a new message
       
   247         smsMtm->CreateMessageL( defaultServiceId );
       
   248 
       
   249         if ( folder == EInbox )
       
   250             {
       
   251             CSmsHeader* smsHeader = &( smsMtm->SmsHeader() );
       
   252             delete smsHeader;
       
   253             smsHeader = NULL;
       
   254             smsHeader = CSmsHeader::NewL( CSmsPDU::ESmsDeliver, smsMtm->Body() );
       
   255             smsHeader->SetFromAddressL( fromTo->Des() );
       
   256             }
       
   257         else
       
   258             {
       
   259             smsMtm->AddAddresseeL( fromTo->Des() );
       
   260 
       
   261             // set delivery settings
       
   262             CSmsSettings* sendOptions = CSmsSettings::NewL();
       
   263             CleanupStack::PushL( sendOptions );
       
   264             sendOptions->CopyL( smsMtm->ServiceSettings() );
       
   265             sendOptions->SetDelivery( ESmsDeliveryImmediately );
       
   266 
       
   267             CSmsHeader* smsHeader = &( smsMtm->SmsHeader() );
       
   268             smsHeader->SetSmsSettingsL( *sendOptions );
       
   269             CleanupStack::PopAndDestroy( sendOptions );
       
   270             }
       
   271 
       
   272 
       
   273 
       
   274         // set body
       
   275         smsMtm->Body().Reset();
       
   276         smsMtm->Body().InsertL( 0, *body );
       
   277 
       
   278         // save the message
       
   279         smsMtm->SaveMessageL();
       
   280 
       
   281         // get the entry of the message
       
   282         TMsvEntry tentry = smsMtm->Entry().Entry();
       
   283 
       
   284         // set the details field
       
   285         tentry.iDetails.Set( fromTo->Des() );
       
   286 
       
   287         // set the description field if it is given.
       
   288         // (with no description the beginning of the message body
       
   289         //  is used as a description)
       
   290         if ( description->Length() > 0 )
       
   291             {
       
   292             tentry.iDescription.Set( description->Des() );
       
   293             }
       
   294 
       
   295         // final fine tuning
       
   296         tentry.SetAttachment( EFalse );
       
   297         tentry.iDate.UniversalTime();
       
   298         tentry.SetVisible( ETrue );
       
   299         tentry.SetInPreparation( EFalse );
       
   300         tentry.SetUnread( isUnread );
       
   301         tentry.SetNew( isNew );
       
   302         tentry.SetComplete( ETrue );
       
   303         tentry.SetSendingState( KMsvSendStateWaiting );
       
   304         tentry.iServiceId = defaultServiceId;
       
   305         tentry.iRelatedId = 0;
       
   306         if ( folder == EInbox )
       
   307             {
       
   308             tentry.SetReadOnly( ETrue );
       
   309             }
       
   310 
       
   311         smsMtm->Entry().ChangeL( tentry );
       
   312 
       
   313         // send the message, if it is in outbox
       
   314         if ( folder == EOutbox )
       
   315             {
       
   316             CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
   317             CleanupStack::PushL( selection );
       
   318             selection->AppendL( tentry.Id() );
       
   319 
       
   320             TBuf8<1> dummyParameter;
       
   321             CMsvOperationWait* waiter = CMsvOperationWait::NewLC();
       
   322             CMsvOperation* op = smsMtm->InvokeAsyncFunctionL(
       
   323                     ESmsMtmCommandScheduleCopy, *selection,
       
   324                     dummyParameter, waiter->iStatus );
       
   325             CleanupStack::PushL( op );
       
   326             waiter->Start();
       
   327             CActiveScheduler::Start();
       
   328             CleanupStack::PopAndDestroy( op );
       
   329             CleanupStack::PopAndDestroy( waiter );
       
   330             CleanupStack::PopAndDestroy( selection );
       
   331             }
       
   332 
       
   333         CleanupStack::PopAndDestroy( smsMtm );
       
   334         CleanupStack::PopAndDestroy( body );
       
   335         CleanupStack::PopAndDestroy( description );
       
   336         CleanupStack::PopAndDestroy( fromTo );
       
   337 
       
   338         TInt32 id = tentry.Id();
       
   339         TBuf8<8> idStr;
       
   340         idStr.Copy( ( TUint8* )( &id ), sizeof( id ) );
       
   341         SendOkMsgL( idStr );
       
   342         }
       
   343 
       
   344     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleSmsImportFuncL: Done");
       
   345     }
       
   346 
       
   347 // ----------------------------------------------------------------------------
       
   348 void CMessageMgmntHandler::HandleCreateMmsL( const TDesC8& aData )
       
   349     {
       
   350     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleCreateMmsL" );
       
   351 
       
   352     if ( !ValidateAddMmsOrAddEmailCommand( aData ) )
       
   353         {
       
   354         // Error message has been sent from validation method.
       
   355         return;
       
   356         }
       
   357 
       
   358     // parse the parameters
       
   359     TInt position( 0 );
       
   360     TInt cmdCode = aData[position];
       
   361     position++;
       
   362     HBufC16* fromTo = ExtractDesLC( aData, position, 1 );
       
   363     HBufC16* description = ExtractDesLC( aData, position, 1 );
       
   364     HBufC8* body = NULL;
       
   365     if ( cmdCode == CHtiMessagesServicePlugin::EAddMms )
       
   366         {
       
   367         body = ExtractDes8LC( aData, position, 2 );
       
   368         }
       
   369     else // Audio msg does not have body text
       
   370         {
       
   371         body = HBufC8::NewLC( 0 );
       
   372         }
       
   373     HBufC16* attPath = ExtractDesLC( aData, position, 1 );
       
   374     TBool isNew = (TBool)aData[position];
       
   375     TBool isUnread = (TBool)aData[position+1];
       
   376     TFolder folder = (TFolder)aData[position+2];
       
   377     TInt extraAttNum;
       
   378     position += 3;
       
   379     TInt len = aData.Length();
       
   380     if( aData.Length() > position )
       
   381         {
       
   382         extraAttNum = (TInt)aData[position];
       
   383         }
       
   384     else
       
   385         {
       
   386         extraAttNum = 0;
       
   387         }
       
   388     position++;
       
   389 
       
   390     HTI_LOG_TEXT( "Creating MMS Client MTM" );
       
   391     CMmsClientMtm* mmsMtm = NULL;
       
   392     TRAPD( err , mmsMtm = ( CMmsClientMtm* )iMtmReg->NewMtmL(
       
   393             KUidMsgTypeMultimedia ) );
       
   394     if ( err || !mmsMtm )
       
   395         {
       
   396         HTI_LOG_TEXT( "MMS message type module not found" );
       
   397         SendErrorMessageL( KErrNotFound, KErrorMsgTypeNotFound );
       
   398         CleanupStack::PopAndDestroy( attPath );
       
   399         CleanupStack::PopAndDestroy( body );
       
   400         CleanupStack::PopAndDestroy( description );
       
   401         CleanupStack::PopAndDestroy( fromTo );
       
   402         return;
       
   403         }
       
   404     CleanupStack::PushL( mmsMtm );
       
   405 
       
   406     HTI_LOG_TEXT( "Creating MMS Client MTM" );
       
   407     CMsvEntry* entry = CMsvEntry::NewL( *iSession,
       
   408                                         KMsvGlobalInBoxIndexEntryId,
       
   409                                         TMsvSelectionOrdering() );
       
   410     CleanupStack::PushL( entry );
       
   411 
       
   412     // get the default service
       
   413     TMsvId defaultServiceId = 0;
       
   414     TRAP( err, defaultServiceId = mmsMtm->DefaultServiceL() );
       
   415     if ( err )
       
   416         {
       
   417         HTI_LOG_FORMAT( "Could not get default service, err: %d", err );
       
   418         SendErrorMessageL( err, KErrorMmsSettingNotDefined );
       
   419         CleanupStack::PopAndDestroy( entry );
       
   420         CleanupStack::PopAndDestroy( mmsMtm );
       
   421         CleanupStack::PopAndDestroy( attPath );
       
   422         CleanupStack::PopAndDestroy( body );
       
   423         CleanupStack::PopAndDestroy( description );
       
   424         CleanupStack::PopAndDestroy( fromTo );
       
   425         return;
       
   426         }
       
   427 
       
   428     // map the folder parameter to folder id
       
   429     TMsvId folderId = KMsvGlobalInBoxIndexEntryId;
       
   430     TRAP( err, folderId = MapFolderToIdL( folder ) );
       
   431     if ( err )
       
   432         {
       
   433         HTI_LOG_FORMAT( "Invalid folder: %d", folder );
       
   434         SendErrorMessageL( err, KErrorInvalidFolder );
       
   435         CleanupStack::PopAndDestroy( entry );
       
   436         CleanupStack::PopAndDestroy( mmsMtm );
       
   437         CleanupStack::PopAndDestroy( attPath );
       
   438         CleanupStack::PopAndDestroy( body );
       
   439         CleanupStack::PopAndDestroy( description );
       
   440         CleanupStack::PopAndDestroy( fromTo );
       
   441         return;
       
   442         }
       
   443     entry->SetEntryL( folderId );
       
   444 
       
   445     // mtm takes ownership of entry context
       
   446     mmsMtm->SetCurrentEntryL( entry );
       
   447     CleanupStack::Pop( entry );
       
   448 
       
   449     HTI_LOG_TEXT( "Creating MMS..." );
       
   450     mmsMtm->CreateMessageL( defaultServiceId );
       
   451     mmsMtm->SetMessageClass( EMmsClassPersonal );
       
   452     mmsMtm->SetExpiryInterval( 86400 );
       
   453     mmsMtm->SetDeliveryTimeInterval( 0 );
       
   454     mmsMtm->SetMessagePriority( EMmsPriorityNormal );
       
   455     mmsMtm->SetSenderVisibility( EMmsMaximumSenderVisibility );
       
   456     mmsMtm->SetDeliveryReport( EMmsDeliveryReportNo );
       
   457     mmsMtm->SetReadReply( EMmsReadReplyYes );
       
   458 
       
   459     if ( description->Length() > 0 )
       
   460         {
       
   461         mmsMtm->SetSubjectL( description->Des() );
       
   462         }
       
   463 
       
   464     if ( folder == EInbox )
       
   465         {
       
   466         mmsMtm->SetSenderL( fromTo->Des() );
       
   467         }
       
   468     else
       
   469         {
       
   470         mmsMtm->AddAddresseeL( fromTo->Des() );
       
   471         }
       
   472 
       
   473     // get an access to the message store
       
   474     HTI_LOG_TEXT( "Getting message store..." );
       
   475     CMsvStore* store = NULL;
       
   476     TRAP( err, store = entry->EditStoreL() );
       
   477     if ( err )
       
   478         {
       
   479         HTI_LOG_FORMAT( "Could not get access to message store, err: %d", err );
       
   480         SendErrorMessageL( err, KErrorMsgStoreOpenFailed );
       
   481         CleanupStack::PopAndDestroy( mmsMtm );
       
   482         CleanupStack::PopAndDestroy( attPath );
       
   483         CleanupStack::PopAndDestroy( body );
       
   484         CleanupStack::PopAndDestroy( description );
       
   485         CleanupStack::PopAndDestroy( fromTo );
       
   486         return;
       
   487         }
       
   488     CleanupStack::PushL( store );
       
   489 
       
   490     MMsvAttachmentManager& attachMan = store->AttachmentManagerL();
       
   491     // set body attachment only for normal MMS - audio message doesn't have body
       
   492     if ( cmdCode == CHtiMessagesServicePlugin::EAddMms )
       
   493         {
       
   494         // Set the message body as attachment
       
   495         // Use UTF-8 as charset because MMS created with MMS editor seems to
       
   496         // save text attachments also as UTF-8.
       
   497         HTI_LOG_TEXT( "Setting body..." );
       
   498         CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewL();
       
   499         CleanupStack::PushL( mimeHeaders );
       
   500         mimeHeaders->SetContentTypeL( _L8( "text" ) );
       
   501         mimeHeaders->SetContentSubTypeL( _L8( "plain" ) );
       
   502         mimeHeaders->SetMimeCharset( KMmsUtf8 );
       
   503         mimeHeaders->SetSuggestedFilenameL( _L( "body.txt" ) );
       
   504 
       
   505         // ownership of bodyAttachment will be transferred
       
   506         CMsvAttachment* bodyAttachment = CMsvAttachment::NewL(
       
   507                 CMsvAttachment::EMsvFile );
       
   508         CleanupStack::PushL( bodyAttachment );
       
   509         bodyAttachment->SetAttachmentNameL( _L( "body.txt" ) );
       
   510         bodyAttachment->SetMimeTypeL( _L8( "text/plain" ) );
       
   511         mimeHeaders->StoreL( *bodyAttachment );
       
   512 
       
   513         RFile textFile;
       
   514         CleanupClosePushL( textFile );
       
   515         CWaiter* waiter = CWaiter::NewLC();
       
   516         attachMan.CreateAttachmentL( _L( "body.txt" ), textFile,
       
   517                 bodyAttachment, waiter->iStatus );
       
   518         waiter->StartAndWait();
       
   519         CleanupStack::PopAndDestroy( waiter );
       
   520 
       
   521         // write the UTF-8 body data to attachment file
       
   522         textFile.Write( *body );
       
   523         CleanupStack::PopAndDestroy(); // textFile
       
   524         CleanupStack::Pop( bodyAttachment ); // ownership transfered
       
   525         CleanupStack::PopAndDestroy( mimeHeaders );
       
   526         }
       
   527 
       
   528     // get the entry of the message
       
   529     TMsvEntry tentry = mmsMtm->Entry().Entry();
       
   530 
       
   531     // set the details field
       
   532     tentry.iDetails.Set( *fromTo );
       
   533 
       
   534     // set the description field
       
   535     if ( description->Length() > 0 )
       
   536         {
       
   537         tentry.iDescription.Set( description->Left( KMmsMaxDescription ) );
       
   538         }
       
   539     else
       
   540         {
       
   541         TBuf<KMmsMaxDescription> descr;
       
   542         CnvUtfConverter::ConvertToUnicodeFromUtf8( descr, *body );
       
   543         tentry.iDescription.Set( descr );
       
   544         }
       
   545 
       
   546     // if this is audio message, set the bio type uid
       
   547     if ( cmdCode == CHtiMessagesServicePlugin::EAddAudioMsg )
       
   548         {
       
   549         tentry.iBioType = KUidMsgSubTypeMmsAudioMsg.iUid;
       
   550         }
       
   551 
       
   552     // handle attachment
       
   553     TBool attachmentsExist = EFalse;
       
   554 	if ( attPath->Length() > 0 )
       
   555 		{
       
   556 		HTI_LOG_TEXT( "Handling attachment..." );
       
   557 		// check that attachment exists
       
   558 		RFs fsSession;
       
   559 		if ( fsSession.Connect() != KErrNone )
       
   560 			{
       
   561 			HTI_LOG_FORMAT( "Error in connecting to file server session: %d", err );
       
   562 			SendErrorMessageL( KErrCouldNotConnect, KErrorRfsConnectFailed );
       
   563 			CleanupStack::PopAndDestroy( store );
       
   564 			CleanupStack::PopAndDestroy( mmsMtm );
       
   565 			CleanupStack::PopAndDestroy( attPath );
       
   566 			CleanupStack::PopAndDestroy( body );
       
   567 			CleanupStack::PopAndDestroy( description );
       
   568 			CleanupStack::PopAndDestroy( fromTo );
       
   569 			return;
       
   570 			}
       
   571 
       
   572 		TBool fileExists = BaflUtils::FileExists( fsSession, attPath->Des() );
       
   573 		fsSession.Close();
       
   574 		if ( !fileExists )
       
   575 			{
       
   576 			HTI_LOG_TEXT( "Attachment file not found" );
       
   577 			SendErrorMessageL( KErrPathNotFound, KErrorAttachmentNotFound );
       
   578 			store->RevertL();
       
   579 			CleanupStack::PopAndDestroy( store );
       
   580 			CleanupStack::PopAndDestroy( mmsMtm );
       
   581 			CleanupStack::PopAndDestroy( attPath );
       
   582 			CleanupStack::PopAndDestroy( body );
       
   583 			CleanupStack::PopAndDestroy( description );
       
   584 			CleanupStack::PopAndDestroy( fromTo );
       
   585 			return;
       
   586 			}
       
   587 		else
       
   588 			{
       
   589 			// save the attachment
       
   590 			TParse parser;
       
   591 			parser.Set( *attPath, NULL, NULL);
       
   592 			TFileName shortFileName = parser.NameAndExt();
       
   593 
       
   594 			// get the mime type
       
   595 			RApaLsSession ls;
       
   596 			User::LeaveIfError( ls.Connect() );
       
   597 			CleanupClosePushL( ls );
       
   598 			TUid appUid;
       
   599 			TDataType dataType;
       
   600 			ls.AppForDocument( *attPath, appUid, dataType );
       
   601 			CleanupStack::PopAndDestroy(); // ls
       
   602 			TPtrC8 mimeType = dataType.Des8();
       
   603 
       
   604 			// attachment settings
       
   605 			// ownership of attachment will be transferred
       
   606 			CMsvAttachment* attachment = CMsvAttachment::NewL(
       
   607 					CMsvAttachment::EMsvFile );
       
   608 			attachment->SetAttachmentNameL( shortFileName );
       
   609 			attachment->SetMimeTypeL( mimeType );
       
   610 
       
   611 			// save
       
   612 			CWaiter* waiter = CWaiter::NewLC();
       
   613 			attachMan.AddAttachmentL( *attPath, attachment, waiter->iStatus );
       
   614 			waiter->StartAndWait();
       
   615 			CleanupStack::PopAndDestroy( waiter );
       
   616 			attachmentsExist = ETrue;
       
   617 			}
       
   618 		}
       
   619 
       
   620     do
       
   621 		{
       
   622 		HBufC16* attPath2;
       
   623        if(extraAttNum-- > 0)
       
   624             {
       
   625             attPath2 = ExtractDesLC( aData, position, 1 );
       
   626             }
       
   627         else
       
   628             {
       
   629             break;
       
   630             }
       
   631 
       
   632 		if ( attPath2->Length() > 0 )
       
   633 			{
       
   634 			HTI_LOG_TEXT( "Handling attachment..." );
       
   635 			// check that attachment exists
       
   636 			RFs fsSession;
       
   637 			if ( fsSession.Connect() != KErrNone )
       
   638 				{
       
   639 				HTI_LOG_FORMAT( "Error in connecting to file server session: %d", err );
       
   640 				SendErrorMessageL( KErrCouldNotConnect, KErrorRfsConnectFailed );
       
   641 				CleanupStack::PopAndDestroy( store );
       
   642 				CleanupStack::PopAndDestroy( mmsMtm );
       
   643 				CleanupStack::PopAndDestroy( attPath );
       
   644 				CleanupStack::PopAndDestroy( body );
       
   645 				CleanupStack::PopAndDestroy( description );
       
   646 				CleanupStack::PopAndDestroy( fromTo );
       
   647 				CleanupStack::PopAndDestroy( attPath2 );
       
   648 				return;
       
   649 				}
       
   650 	
       
   651 			TBool fileExists = BaflUtils::FileExists( fsSession, attPath2->Des() );
       
   652 			fsSession.Close();
       
   653 			if ( !fileExists )
       
   654 				{
       
   655 				HTI_LOG_TEXT( "Attachment file not found" );
       
   656 				SendErrorMessageL( KErrPathNotFound, KErrorAttachmentNotFound );
       
   657 				store->RevertL();
       
   658 				CleanupStack::PopAndDestroy( store );
       
   659 				CleanupStack::PopAndDestroy( mmsMtm );
       
   660 				CleanupStack::PopAndDestroy( attPath );
       
   661 				CleanupStack::PopAndDestroy( body );
       
   662 				CleanupStack::PopAndDestroy( description );
       
   663 				CleanupStack::PopAndDestroy( fromTo );
       
   664 				CleanupStack::PopAndDestroy( attPath2 );
       
   665 				return;
       
   666 				}
       
   667 			else
       
   668 				{
       
   669 				// save the attachment
       
   670 				TParse parser;
       
   671 				parser.Set( *attPath2, NULL, NULL);
       
   672 				TFileName shortFileName = parser.NameAndExt();
       
   673 	
       
   674 				// get the mime type
       
   675 				RApaLsSession ls;
       
   676 				User::LeaveIfError( ls.Connect() );
       
   677 				CleanupClosePushL( ls );
       
   678 				TUid appUid;
       
   679 				TDataType dataType;
       
   680 				ls.AppForDocument( *attPath2, appUid, dataType );
       
   681 				CleanupStack::PopAndDestroy(); // ls
       
   682 				TPtrC8 mimeType = dataType.Des8();
       
   683 	
       
   684 				// attachment settings
       
   685 				// ownership of attachment will be transferred
       
   686 				CMsvAttachment* attachment = CMsvAttachment::NewL(
       
   687 						CMsvAttachment::EMsvFile );
       
   688 				attachment->SetAttachmentNameL( shortFileName );
       
   689 				attachment->SetMimeTypeL( mimeType );
       
   690 	
       
   691 				// save
       
   692 				CWaiter* waiter = CWaiter::NewLC();
       
   693 				attachMan.AddAttachmentL( *attPath2, attachment, waiter->iStatus );
       
   694 				waiter->StartAndWait();
       
   695 				CleanupStack::PopAndDestroy( waiter );
       
   696 				attachmentsExist = ETrue;
       
   697 				}
       
   698 
       
   699 			CleanupStack::PopAndDestroy( attPath2 );
       
   700 			}
       
   701 		} while(ETrue);
       
   702     // save the changes made to the message store
       
   703     store->CommitL();
       
   704     CleanupStack::PopAndDestroy( store );
       
   705 
       
   706     // save the message
       
   707     mmsMtm->SaveMessageL();
       
   708 
       
   709     // final fine tuning
       
   710     tentry.SetAttachment( attachmentsExist );
       
   711     tentry.iDate.UniversalTime();
       
   712     tentry.SetVisible( ETrue );
       
   713     tentry.SetInPreparation( EFalse );
       
   714     if ( folder == EDrafts )
       
   715         {
       
   716         tentry.SetReadOnly( EFalse );
       
   717         }
       
   718     else
       
   719         {
       
   720         tentry.SetReadOnly( ETrue );
       
   721         }
       
   722     tentry.SetUnread( isUnread );
       
   723     tentry.SetNew( isNew );
       
   724     tentry.SetComplete( ETrue );
       
   725     tentry.SetSendingState( KMsvSendStateWaiting );
       
   726     tentry.iServiceId = defaultServiceId;
       
   727     tentry.iRelatedId = 0;
       
   728     tentry.iMtmData1 = KMmsMessageMRetrieveConf | KMmsMessageMobileTerminated;
       
   729 
       
   730     mmsMtm->Entry().ChangeL( tentry );
       
   731 
       
   732     HTI_LOG_TEXT( "MMS created and ready" );
       
   733 
       
   734     // send the message, if it is in outbox
       
   735     if ( folder == EOutbox )
       
   736         {
       
   737         HTI_LOG_TEXT( "MMS is in Outbox, sending it..." );
       
   738 
       
   739         CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
   740         CleanupStack::PushL( selection );
       
   741         selection->AppendL( tentry.Id() );
       
   742 
       
   743         CMsvOperationWait* waiter = CMsvOperationWait::NewLC();
       
   744         CMsvOperation* op = mmsMtm->SendL( *selection,
       
   745                                            waiter->iStatus,
       
   746                                            tentry.iDate );
       
   747         CleanupStack::PushL( op );
       
   748         waiter->Start();
       
   749         CActiveScheduler::Start();
       
   750         CleanupStack::PopAndDestroy( op );
       
   751         CleanupStack::PopAndDestroy( waiter );
       
   752         CleanupStack::PopAndDestroy( selection );
       
   753         }
       
   754 
       
   755     HTI_LOG_TEXT( "Cleaning up" );
       
   756     CleanupStack::PopAndDestroy( mmsMtm );
       
   757     CleanupStack::PopAndDestroy( attPath );
       
   758     CleanupStack::PopAndDestroy( body );
       
   759     CleanupStack::PopAndDestroy( description );
       
   760     CleanupStack::PopAndDestroy( fromTo );
       
   761 
       
   762     // send the message id back
       
   763     TInt32 id = tentry.Id();
       
   764     TBuf8<8> idStr;
       
   765     idStr.Copy( ( TUint8* )( &id ), sizeof( id ) );
       
   766     SendOkMsgL( idStr );
       
   767 
       
   768     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleCreateMmsL: Done");
       
   769     }
       
   770 
       
   771 
       
   772 // ----------------------------------------------------------------------------
       
   773 void CMessageMgmntHandler::HandleCreateEmailL( const TDesC8& aData )
       
   774     {
       
   775     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleCreateEmailL" );
       
   776 
       
   777     if ( ValidateAddMmsOrAddEmailCommand( aData ) )
       
   778         {
       
   779         // parse the parameters
       
   780         TInt position( 1 ); // position 0 is command code
       
   781         HBufC16* fromTo = ExtractDesLC( aData, position, 1 );
       
   782         HBufC16* description = ExtractDesLC( aData, position, 1 );
       
   783         HBufC16* body = ExtractDesLC( aData, position, 2 );
       
   784         HBufC16* attPath = ExtractDesLC( aData, position, 1 );
       
   785         TBool isNew = (TBool)aData[position];
       
   786         TBool isUnread = (TBool)aData[position+1];
       
   787         TFolder folder = (TFolder)aData[position+2];
       
   788 	    TInt extraAttNum;
       
   789 	    position += 3;
       
   790 	    TInt len = aData.Length();
       
   791 	    if( aData.Length() > position )
       
   792 	        {
       
   793 	        extraAttNum = (TInt)aData[position];
       
   794 	        }
       
   795 	    else
       
   796 	        {
       
   797 	        extraAttNum = 0;
       
   798 	        }
       
   799 	    position++;
       
   800 
       
   801         HTI_LOG_TEXT( "Creating SMTP Client MTM" );
       
   802         CSmtpClientMtm* smtpMtm = NULL;
       
   803         TRAPD( err, smtpMtm = ( CSmtpClientMtm* )iMtmReg->NewMtmL(
       
   804                 KUidMsgTypeSMTP ) );
       
   805         if ( err || !smtpMtm )
       
   806             {
       
   807             HTI_LOG_TEXT( "SMTP message type module not found" );
       
   808             SendErrorMessageL( KErrNotFound, KErrorMsgTypeNotFound );
       
   809             CleanupStack::PopAndDestroy( attPath );
       
   810             CleanupStack::PopAndDestroy( body );
       
   811             CleanupStack::PopAndDestroy( description );
       
   812             CleanupStack::PopAndDestroy( fromTo );
       
   813             return;
       
   814             }
       
   815         CleanupStack::PushL( smtpMtm );
       
   816 
       
   817         HTI_LOG_TEXT( "Creating a new CMsvEntry" );
       
   818         CMsvEntry* entry = CMsvEntry::NewL( *iSession,
       
   819                                             KMsvGlobalInBoxIndexEntryId,
       
   820                                             TMsvSelectionOrdering() );
       
   821         CleanupStack::PushL( entry );
       
   822 
       
   823         // get the default service
       
   824         HTI_LOG_TEXT( "Getting the default service" );
       
   825         TMsvId defaultServiceId = 0;
       
   826         TRAP( err, defaultServiceId = smtpMtm->DefaultServiceL() );
       
   827         if ( err )
       
   828             {
       
   829             HTI_LOG_FORMAT( "Could not get default service, err: %d", err );
       
   830             SendErrorMessageL( err, KErrorMailboxNotDefined );
       
   831             CleanupStack::PopAndDestroy( entry );
       
   832             CleanupStack::PopAndDestroy( smtpMtm );
       
   833             CleanupStack::PopAndDestroy( attPath );
       
   834             CleanupStack::PopAndDestroy( body );
       
   835             CleanupStack::PopAndDestroy( description );
       
   836             CleanupStack::PopAndDestroy( fromTo );
       
   837             return;
       
   838             }
       
   839 
       
   840         // map the folder parameter to folder id
       
   841         HTI_LOG_TEXT( "Mapping the folder parameter to folder id" );
       
   842         TMsvId folderId = KMsvGlobalInBoxIndexEntryId;
       
   843         TRAP( err, folderId = MapFolderToIdL( folder ) );
       
   844         if ( err )
       
   845             {
       
   846             HTI_LOG_FORMAT( "Invalid folder: %d", folder );
       
   847             SendErrorMessageL( err, KErrorInvalidFolder );
       
   848             CleanupStack::PopAndDestroy( entry );
       
   849             CleanupStack::PopAndDestroy( smtpMtm );
       
   850             CleanupStack::PopAndDestroy( attPath );
       
   851             CleanupStack::PopAndDestroy( body );
       
   852             CleanupStack::PopAndDestroy( description );
       
   853             CleanupStack::PopAndDestroy( fromTo );
       
   854             return;
       
   855             }
       
   856         entry->SetEntryL( folderId );
       
   857 
       
   858         // mtm takes ownership of entry context
       
   859         smtpMtm->SetCurrentEntryL( entry );
       
   860         CleanupStack::Pop( entry );
       
   861 
       
   862         // create a message and set subject and body
       
   863         smtpMtm->CreateMessageL( defaultServiceId );
       
   864         smtpMtm->SetSubjectL( description->Des() );
       
   865         smtpMtm->Body().Reset();
       
   866         smtpMtm->Body().InsertL( 0, body->Des() );
       
   867 
       
   868         // get the entry of the message
       
   869         TMsvEntry tentry = smtpMtm->Entry().Entry();
       
   870 
       
   871         // add addressee
       
   872         smtpMtm->AddAddresseeL( fromTo->Des() );
       
   873         tentry.iDetails.Set( fromTo->Des() );
       
   874 
       
   875         // If creating to Inbox use other than KUidMsgTypeSMTP so that the
       
   876         // mail displays "from" field and not "to" field.
       
   877         if ( folder == EInbox )
       
   878             {
       
   879             tentry.iMtm = KUidMsgTypeIMAP4;
       
   880             }
       
   881 
       
   882         // set the description field same as the message subject
       
   883         tentry.iDescription.Set( description->Des() );
       
   884 
       
   885         // save the changes done above
       
   886         smtpMtm->Entry().ChangeL( tentry );
       
   887 
       
   888         // get an access to the message store
       
   889         CMsvStore* store = entry->EditStoreL();
       
   890         CleanupStack::PushL( store );
       
   891         CImHeader* header = CImHeader::NewLC();
       
   892         header->RestoreL( *store );
       
   893         TUint charset = header->Charset();
       
   894         CleanupStack::PopAndDestroy( header );
       
   895         CleanupStack::PopAndDestroy( store );
       
   896 
       
   897         // handle attachment
       
   898         TBool attachmentsExist = EFalse;
       
   899         if ( attPath->Length() > 0 )
       
   900             {
       
   901             // check that attachment exists
       
   902             RFs fsSession;
       
   903             if ( fsSession.Connect() != KErrNone )
       
   904                 {
       
   905                 HTI_LOG_FORMAT( "Error in connecting to file server session: %d", err );
       
   906                 SendErrorMessageL( KErrCouldNotConnect, KErrorRfsConnectFailed );
       
   907                 CleanupStack::PopAndDestroy( smtpMtm );
       
   908                 CleanupStack::PopAndDestroy( attPath );
       
   909                 CleanupStack::PopAndDestroy( body );
       
   910                 CleanupStack::PopAndDestroy( description );
       
   911                 CleanupStack::PopAndDestroy( fromTo );
       
   912                 return;
       
   913                 }
       
   914             CleanupClosePushL( fsSession );
       
   915 
       
   916             TBool fileExists = BaflUtils::FileExists( fsSession, attPath->Des() );
       
   917             if ( !fileExists )
       
   918                 {
       
   919                 HTI_LOG_TEXT( "Attachment file not found" );
       
   920                 SendErrorMessageL( KErrPathNotFound, KErrorAttachmentNotFound );
       
   921                 CleanupStack::PopAndDestroy(); // fsSession
       
   922                 CleanupStack::PopAndDestroy( smtpMtm );
       
   923                 CleanupStack::PopAndDestroy( attPath );
       
   924                 CleanupStack::PopAndDestroy( body );
       
   925                 CleanupStack::PopAndDestroy( description );
       
   926                 CleanupStack::PopAndDestroy( fromTo );
       
   927                 return;
       
   928                 }
       
   929             else
       
   930                 {
       
   931                 // get the mime type
       
   932                 HTI_LOG_TEXT( "Getting the attachment's mime type" );
       
   933                 RApaLsSession ls;
       
   934                 User::LeaveIfError( ls.Connect() );
       
   935                 TUid appUid;
       
   936                 TDataType dataType;
       
   937                 ls.AppForDocument( *attPath, appUid, dataType );
       
   938                 TPtrC8 mimeType = dataType.Des8();
       
   939 
       
   940                 HTI_LOG_TEXT( "Adding the attachment" );
       
   941                 CWaiter* waiter = CWaiter::NewLC();
       
   942                 smtpMtm->AddAttachmentL( attPath->Des(), mimeType, charset,
       
   943                         waiter->iStatus );
       
   944                 waiter->StartAndWait();
       
   945                 CleanupStack::PopAndDestroy( waiter );
       
   946                 HTI_LOG_TEXT( "Attachment added succesfully" );
       
   947                 ls.Close();
       
   948                 attachmentsExist = ETrue;
       
   949                 }
       
   950 
       
   951             CleanupStack::PopAndDestroy(); // fsSession
       
   952             }
       
   953 
       
   954 		do
       
   955 			{
       
   956 			HBufC16* attPath2;
       
   957 			if(extraAttNum-- > 0)
       
   958 	            {
       
   959 	            attPath2 = ExtractDesLC( aData, position, 1 );
       
   960 	            }
       
   961 	        else
       
   962 	            {
       
   963 	            break;
       
   964 	            }
       
   965             // check that attachment exists
       
   966             RFs fsSession;
       
   967             if ( fsSession.Connect() != KErrNone )
       
   968                 {
       
   969                 HTI_LOG_FORMAT( "Error in connecting to file server session: %d", err );
       
   970                 SendErrorMessageL( KErrCouldNotConnect, KErrorRfsConnectFailed );
       
   971                 CleanupStack::PopAndDestroy( smtpMtm );
       
   972                 CleanupStack::PopAndDestroy( attPath );
       
   973                 CleanupStack::PopAndDestroy( body );
       
   974                 CleanupStack::PopAndDestroy( description );
       
   975                 CleanupStack::PopAndDestroy( fromTo );
       
   976                 CleanupStack::PopAndDestroy( attPath2 );
       
   977                 return;
       
   978                 }
       
   979             CleanupClosePushL( fsSession );
       
   980 
       
   981             TBool fileExists = BaflUtils::FileExists( fsSession, attPath2->Des() );
       
   982             if ( !fileExists )
       
   983                 {
       
   984                 HTI_LOG_TEXT( "Attachment file not found" );
       
   985                 SendErrorMessageL( KErrPathNotFound, KErrorAttachmentNotFound );
       
   986                 CleanupStack::PopAndDestroy(); // fsSession
       
   987                 CleanupStack::PopAndDestroy( smtpMtm );
       
   988                 CleanupStack::PopAndDestroy( attPath );
       
   989                 CleanupStack::PopAndDestroy( body );
       
   990                 CleanupStack::PopAndDestroy( description );
       
   991                 CleanupStack::PopAndDestroy( fromTo );
       
   992                 CleanupStack::PopAndDestroy( attPath2 );
       
   993                 return;
       
   994                 }
       
   995             else
       
   996                 {
       
   997                 // get the mime type
       
   998                 HTI_LOG_TEXT( "Getting the attachment's mime type" );
       
   999                 RApaLsSession ls;
       
  1000                 User::LeaveIfError( ls.Connect() );
       
  1001                 TUid appUid;
       
  1002                 TDataType dataType;
       
  1003                 ls.AppForDocument( *attPath2, appUid, dataType );
       
  1004                 TPtrC8 mimeType = dataType.Des8();
       
  1005 
       
  1006                 HTI_LOG_TEXT( "Adding the attachment" );
       
  1007                 CWaiter* waiter = CWaiter::NewLC();
       
  1008                 smtpMtm->AddAttachmentL( attPath2->Des(), mimeType, charset,
       
  1009                         waiter->iStatus );
       
  1010                 waiter->StartAndWait();
       
  1011                 CleanupStack::PopAndDestroy( waiter );
       
  1012                 HTI_LOG_TEXT( "Attachment added succesfully" );
       
  1013                 ls.Close();
       
  1014                 attachmentsExist = ETrue;
       
  1015                 }
       
  1016 
       
  1017             CleanupStack::PopAndDestroy(); // fsSession
       
  1018 			CleanupStack::PopAndDestroy( attPath2 );
       
  1019 		} while(ETrue);
       
  1020 
       
  1021         // save the message
       
  1022         smtpMtm->SaveMessageL();
       
  1023 
       
  1024         // final fine tuning
       
  1025         TMsvEmailEntry temailEntry = static_cast<TMsvEmailEntry>( tentry );
       
  1026         temailEntry.SetMessageFolderType( EFolderTypeUnknown );
       
  1027         temailEntry.SetDisconnectedOperation( ENoDisconnectedOperations );
       
  1028         temailEntry.SetEncrypted( EFalse );
       
  1029         temailEntry.SetSigned( EFalse );
       
  1030         temailEntry.SetVCard( EFalse );
       
  1031         temailEntry.SetVCalendar( EFalse );
       
  1032         temailEntry.SetReceipt( EFalse );
       
  1033         temailEntry.SetMHTMLEmail( EFalse );
       
  1034         temailEntry.SetBodyTextComplete( ETrue );
       
  1035         temailEntry.SetAttachment( attachmentsExist );
       
  1036         temailEntry.iDate.UniversalTime();
       
  1037         temailEntry.SetVisible( ETrue );
       
  1038         temailEntry.SetInPreparation( EFalse );
       
  1039         temailEntry.SetSendingState( KMsvSendStateWaiting );
       
  1040         temailEntry.SetUnread( isUnread );
       
  1041         temailEntry.SetNew( isNew );
       
  1042         temailEntry.SetComplete( ETrue );
       
  1043         temailEntry.iServiceId = defaultServiceId;
       
  1044         temailEntry.iRelatedId = 0;
       
  1045 
       
  1046         smtpMtm->Entry().ChangeL( temailEntry );
       
  1047 
       
  1048         // get an access to the message store
       
  1049         store = entry->EditStoreL();
       
  1050         CleanupStack::PushL( store );
       
  1051 
       
  1052         // set email header info
       
  1053         header = CImHeader::NewLC();
       
  1054         header->RestoreL( *store );
       
  1055         header->SetSubjectL( description->Des() );
       
  1056         header->SetFromL( fromTo->Des() );
       
  1057         header->SetReceiptAddressL( fromTo->Des() );
       
  1058         header->StoreL( *store );
       
  1059         store->CommitL();
       
  1060         CleanupStack::PopAndDestroy( header );
       
  1061         CleanupStack::PopAndDestroy( store );
       
  1062 
       
  1063         // send the message, if it is in outbox
       
  1064         if ( folder == EOutbox )
       
  1065             {
       
  1066             HTI_LOG_TEXT( "E-Mail was created in outbox, marking it to be sent on next connection" );
       
  1067 
       
  1068             CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection;
       
  1069             CleanupStack::PushL( selection );
       
  1070             selection->AppendL( temailEntry.Id() );
       
  1071 
       
  1072             TBuf8<1> dummyParameter;
       
  1073             CMsvOperationActiveSchedulerWait* waiter =
       
  1074                     CMsvOperationActiveSchedulerWait::NewLC();
       
  1075             CMsvOperation* op = smtpMtm->InvokeAsyncFunctionL(
       
  1076                     KSMTPMTMSendOnNextConnection, *selection,
       
  1077                     dummyParameter, waiter->iStatus );
       
  1078             CleanupStack::PushL( op );
       
  1079             waiter->Start();
       
  1080             CleanupStack::PopAndDestroy( op );
       
  1081             CleanupStack::PopAndDestroy( waiter );
       
  1082             CleanupStack::PopAndDestroy( selection );
       
  1083             }
       
  1084 
       
  1085         HTI_LOG_TEXT( "Cleaning up" );
       
  1086         CleanupStack::PopAndDestroy( smtpMtm );
       
  1087         CleanupStack::PopAndDestroy( attPath );
       
  1088         CleanupStack::PopAndDestroy( body );
       
  1089         CleanupStack::PopAndDestroy( description );
       
  1090         CleanupStack::PopAndDestroy( fromTo );
       
  1091 
       
  1092         // send the message id back
       
  1093         TInt32 id = tentry.Id();
       
  1094         TBuf8<8> idStr;
       
  1095         idStr.Copy( ( TUint8* )( &id ), sizeof( id ) );
       
  1096         SendOkMsgL( idStr );
       
  1097         }
       
  1098 
       
  1099     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleCreateEmailL: Done");
       
  1100     }
       
  1101 
       
  1102 // ----------------------------------------------------------------------------
       
  1103 void CMessageMgmntHandler::HandleCreateObexMsgL( const TDesC8& aData,
       
  1104                                                  TUid aMtmUid,
       
  1105                                                  TUid aMsgTypeUid )
       
  1106     {
       
  1107     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleCreateObexMsgL" );
       
  1108 
       
  1109     if ( ValidateAddObexMsgCommand( aData ) )
       
  1110         {
       
  1111         // parse the parameters
       
  1112         TInt position( 0 );
       
  1113         HBufC16* fromTo = ExtractDesLC( aData, position, 1 );
       
  1114         HBufC16* description = ExtractDesLC( aData, position, 1 );
       
  1115         HBufC16* attPath = ExtractDesLC( aData, position, 1 );
       
  1116         TBool isNew = (TBool)aData[position];
       
  1117         TBool isUnread = (TBool)aData[position+1];
       
  1118         TFolder folder = (TFolder)aData[position+2];
       
  1119 
       
  1120         // Adding Obex messages to the outbox is not allowed
       
  1121         if ( folder == EOutbox )
       
  1122             {
       
  1123             HTI_LOG_TEXT( "Outbox not supported with Obex messages" );
       
  1124             SendErrorMessageL( KErrNotSupported, KErrorNotSupported );
       
  1125             CleanupStack::PopAndDestroy( attPath );
       
  1126             CleanupStack::PopAndDestroy( description );
       
  1127             CleanupStack::PopAndDestroy( fromTo );
       
  1128             return;
       
  1129             }
       
  1130 
       
  1131         CObexClientMtm* obexMtm = NULL;
       
  1132         TRAPD( err, obexMtm = ( CObexClientMtm* )iMtmReg->NewMtmL( aMtmUid ) );
       
  1133         if ( err || !obexMtm )
       
  1134             {
       
  1135             HTI_LOG_TEXT( "Obex message type module not found" );
       
  1136             SendErrorMessageL( KErrNotFound, KErrorMsgTypeNotFound );
       
  1137             CleanupStack::PopAndDestroy( attPath );
       
  1138             CleanupStack::PopAndDestroy( description );
       
  1139             CleanupStack::PopAndDestroy( fromTo );
       
  1140             return;
       
  1141             }
       
  1142         CleanupStack::PushL( obexMtm );
       
  1143 
       
  1144         CMsvEntry* entry = CMsvEntry::NewL( *iSession,
       
  1145                                             KMsvGlobalInBoxIndexEntryId,
       
  1146                                             TMsvSelectionOrdering() );
       
  1147         CleanupStack::PushL( entry );
       
  1148 
       
  1149         TMsvId defaultServiceId = 0;
       
  1150 
       
  1151         // map the folder parameter to folder id
       
  1152         TMsvId folderId = KMsvGlobalInBoxIndexEntryId;
       
  1153         TRAP( err, folderId = MapFolderToIdL( folder ) );
       
  1154         if ( err )
       
  1155             {
       
  1156             HTI_LOG_FORMAT( "Invalid folder: %d", folder );
       
  1157             SendErrorMessageL( err, KErrorInvalidFolder );
       
  1158             CleanupStack::PopAndDestroy( entry );
       
  1159             CleanupStack::PopAndDestroy( obexMtm );
       
  1160             CleanupStack::PopAndDestroy( attPath );
       
  1161             CleanupStack::PopAndDestroy( description );
       
  1162             CleanupStack::PopAndDestroy( fromTo );
       
  1163             return;
       
  1164             }
       
  1165         entry->SetEntryL( folderId );
       
  1166 
       
  1167         // mtm takes ownership of entry context
       
  1168         obexMtm->SetCurrentEntryL( entry );
       
  1169         CleanupStack::Pop( entry );
       
  1170 
       
  1171         // create a new message
       
  1172         obexMtm->CreateMessageL( defaultServiceId );
       
  1173 
       
  1174         // get the entry of the message
       
  1175         TMsvEntry tentry = obexMtm->Entry().Entry();
       
  1176 
       
  1177         // set subject
       
  1178         obexMtm->SetSubjectL( description->Des() );
       
  1179         tentry.iDescription.Set( description->Des() );
       
  1180 
       
  1181         // set body, must be empty for obex messages
       
  1182         obexMtm->Body().Reset();
       
  1183 
       
  1184         // set the details field and
       
  1185         tentry.iDetails.Set( fromTo->Des() );
       
  1186 
       
  1187         // set mtm
       
  1188         tentry.iMtm = aMtmUid;
       
  1189         tentry.iType = KUidMsvMessageEntry;
       
  1190         tentry.iServiceId = KMsvUnknownServiceIndexEntryId;
       
  1191 
       
  1192         // save the changes done above
       
  1193         obexMtm->Entry().ChangeL( tentry );
       
  1194 
       
  1195         // save the message
       
  1196         obexMtm->SaveMessageL();
       
  1197 
       
  1198         // final fine tuning
       
  1199         tentry.iDate.HomeTime();
       
  1200         tentry.SetVisible( ETrue );
       
  1201         tentry.SetInPreparation( EFalse );
       
  1202         tentry.SetUnread( isUnread );
       
  1203         tentry.SetNew( isNew );
       
  1204         tentry.SetComplete( ETrue );
       
  1205         obexMtm->Entry().ChangeL( tentry );
       
  1206 
       
  1207         // handle attachment
       
  1208         if ( attPath->Length() > 0 )
       
  1209             {
       
  1210             // check that attachment exists
       
  1211             RFs fsSession;
       
  1212             if ( fsSession.Connect() != KErrNone )
       
  1213                 {
       
  1214                 HTI_LOG_FORMAT( "Error in connecting to file server session: %d", err );
       
  1215                 SendErrorMessageL( KErrCouldNotConnect, KErrorRfsConnectFailed );
       
  1216                 CleanupStack::PopAndDestroy( obexMtm );
       
  1217                 CleanupStack::PopAndDestroy( attPath );
       
  1218                 CleanupStack::PopAndDestroy( description );
       
  1219                 CleanupStack::PopAndDestroy( fromTo );
       
  1220                 return;
       
  1221                 }
       
  1222 
       
  1223             TBool fileExists = BaflUtils::FileExists( fsSession, attPath->Des() );
       
  1224             fsSession.Close();
       
  1225             if ( !fileExists )
       
  1226                 {
       
  1227                 HTI_LOG_TEXT( "Attachment file not found" );
       
  1228                 SendErrorMessageL( KErrPathNotFound, KErrorAttachmentNotFound );
       
  1229                 CleanupStack::PopAndDestroy( obexMtm );
       
  1230                 CleanupStack::PopAndDestroy( attPath );
       
  1231                 CleanupStack::PopAndDestroy( description );
       
  1232                 CleanupStack::PopAndDestroy( fromTo );
       
  1233                 return;
       
  1234                 }
       
  1235             else
       
  1236                 {
       
  1237                 // create a new entry for the attachment
       
  1238                 TMsvEntry attachTEntry;
       
  1239                 attachTEntry.iType = KUidMsvAttachmentEntry;
       
  1240                 attachTEntry.iServiceId = KMsvUnknownServiceIndexEntryId;
       
  1241                 attachTEntry.iMtm = aMsgTypeUid; //save as bt message
       
  1242 
       
  1243                 entry->CreateL( attachTEntry );
       
  1244 
       
  1245                 CMsvEntry* attachEntry = iSession->GetEntryL( attachTEntry.Id() );
       
  1246                 obexMtm->SetCurrentEntryL( attachEntry );
       
  1247 
       
  1248                 // get source file
       
  1249                 TFileName sourceFileName = attPath->Des();
       
  1250 
       
  1251                 // get the mime type
       
  1252                 RApaLsSession ls;
       
  1253                 User::LeaveIfError( ls.Connect() );
       
  1254                 CleanupClosePushL<RApaLsSession>(ls);
       
  1255                 TUid appUid;
       
  1256                 TDataType mimeType;
       
  1257                 ls.AppForDocument( sourceFileName, appUid, mimeType );
       
  1258                 CleanupStack::PopAndDestroy(); //ls
       
  1259 
       
  1260                 CWaiter* waiter = CWaiter::NewLC();
       
  1261 
       
  1262                 // add an attachment to the current message entry
       
  1263                 obexMtm->AddAttachmentL( sourceFileName, mimeType.Des8(), 0,
       
  1264                         waiter->iStatus );
       
  1265                 waiter->StartAndWait();
       
  1266                 CleanupStack::PopAndDestroy( waiter );
       
  1267                 }
       
  1268             }
       
  1269 
       
  1270         CleanupStack::PopAndDestroy( obexMtm );
       
  1271         CleanupStack::PopAndDestroy( attPath );
       
  1272         CleanupStack::PopAndDestroy( description );
       
  1273         CleanupStack::PopAndDestroy( fromTo );
       
  1274 
       
  1275         // send the message id back
       
  1276         TInt32 id = tentry.Id();
       
  1277         TBuf8<8> idStr;
       
  1278         idStr.Copy( ( TUint8* )( &id ), sizeof( id ) );
       
  1279         SendOkMsgL( idStr );
       
  1280         }
       
  1281 
       
  1282     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleCreateObexMsgL: Done");
       
  1283     }
       
  1284 
       
  1285 
       
  1286 // ----------------------------------------------------------------------------
       
  1287 void CMessageMgmntHandler::HandleCreateSmartMsgL( const TDesC8& aData )
       
  1288     {
       
  1289     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleCreateSmartMsgL" );
       
  1290 
       
  1291     if ( ValidateAddSmartMsgCommand( aData ) )
       
  1292         {
       
  1293         TInt position( 0 );
       
  1294         HBufC16* fromTo = ExtractDesLC( aData, position, 1 );
       
  1295         HBufC16* description = ExtractDesLC( aData, position, 1 );
       
  1296         HBufC16* body = ExtractDesLC( aData, position, 2 );
       
  1297         TBool isNew = (TBool)aData[position];
       
  1298         TBool isUnread = (TBool)aData[position+1];
       
  1299         TFolder folder = (TFolder)aData[position+2];
       
  1300         TInt bioUidValue =   aData[position+3] +
       
  1301                            ( aData[position+4] << 8 ) +
       
  1302                            ( aData[position+5] << 16 ) +
       
  1303                            ( aData[position+6] << 24 );
       
  1304 
       
  1305 
       
  1306         // Smart messages can be created only to inbox.
       
  1307         // For sending smart messages, create a normal SMS with smart message
       
  1308         // content as a body and send it.
       
  1309         if ( folder != EInbox )
       
  1310             {
       
  1311             HTI_LOG_TEXT( "Invalid folder specified for smart message" );
       
  1312             SendErrorMessageL( KErrArgument, KErrorInvalidFolderForSmartMsg );
       
  1313             CleanupStack::PopAndDestroy( body );
       
  1314             CleanupStack::PopAndDestroy( description );
       
  1315             CleanupStack::PopAndDestroy( fromTo );
       
  1316             return;
       
  1317 
       
  1318             }
       
  1319 
       
  1320         CSmsClientMtm* smsMtm = NULL;
       
  1321         TRAPD( err, smsMtm = ( CSmsClientMtm* )iMtmReg->NewMtmL( KUidMsgTypeSMS ) );
       
  1322         if ( err || !smsMtm )
       
  1323             {
       
  1324             HTI_LOG_TEXT( "SMS message type module not found" );
       
  1325             SendErrorMessageL( KErrNotFound, KErrorMsgTypeNotFound );
       
  1326             CleanupStack::PopAndDestroy( body );
       
  1327             CleanupStack::PopAndDestroy( description );
       
  1328             CleanupStack::PopAndDestroy( fromTo );
       
  1329             return;
       
  1330             }
       
  1331         CleanupStack::PushL( smsMtm );
       
  1332 
       
  1333         CMsvEntry* entry = CMsvEntry::NewL( *iSession,
       
  1334                 KMsvGlobalInBoxIndexEntryId, TMsvSelectionOrdering() );
       
  1335         CleanupStack::PushL( entry );
       
  1336 
       
  1337         // get the default service
       
  1338         TMsvId defaultServiceId = 0;
       
  1339         TRAP( err, defaultServiceId = smsMtm->DefaultServiceL() );
       
  1340         if ( err )
       
  1341             {
       
  1342             HTI_LOG_FORMAT( "Could not get default service, err: %d", err );
       
  1343             SendErrorMessageL( err, KErrorSmsSettingNotDefined );
       
  1344             CleanupStack::PopAndDestroy( entry );
       
  1345             CleanupStack::PopAndDestroy( smsMtm );
       
  1346             CleanupStack::PopAndDestroy( body );
       
  1347             CleanupStack::PopAndDestroy( description );
       
  1348             CleanupStack::PopAndDestroy( fromTo );
       
  1349             return;
       
  1350             }
       
  1351 
       
  1352         // no need for folder mapping, since only inbox allowed for smart messages
       
  1353         TMsvId folderId = KMsvGlobalInBoxIndexEntryId;
       
  1354         entry->SetEntryL( folderId );
       
  1355 
       
  1356         // mtm takes ownership of entry context
       
  1357         smsMtm->SetCurrentEntryL( entry );
       
  1358         CleanupStack::Pop( entry );
       
  1359 
       
  1360         // create a new message
       
  1361         smsMtm->CreateMessageL( defaultServiceId );
       
  1362 
       
  1363         // update the message header
       
  1364         CSmsHeader* smsHeader = &( smsMtm->SmsHeader() );
       
  1365         delete smsHeader;
       
  1366         smsHeader = NULL;
       
  1367         smsHeader = CSmsHeader::NewL( CSmsPDU::ESmsSubmit, smsMtm->Body() );
       
  1368         smsHeader->SetFromAddressL( fromTo->Des() );
       
  1369 
       
  1370         // set body, the actual BIO message content
       
  1371         smsMtm->Body().Reset();
       
  1372         smsMtm->Body().InsertL( 0, body->Des() );
       
  1373 
       
  1374         // get the entry of the message
       
  1375         TMsvEntry tentry = smsMtm->Entry().Entry();
       
  1376 
       
  1377         // set BIO message type specific data
       
  1378         tentry.iBioType = bioUidValue;
       
  1379         smsMtm->BioTypeChangedL( TUid::Uid( bioUidValue ) );
       
  1380 
       
  1381         // set details field
       
  1382         tentry.iDetails.Set( fromTo->Des() );
       
  1383 
       
  1384         // set the description field
       
  1385         tentry.iDescription.Set( description->Des() );
       
  1386 
       
  1387         // set correct MTM type
       
  1388         tentry.iMtm= KUidBIOMessageTypeMtm;
       
  1389 
       
  1390         // final fine tuning
       
  1391         tentry.SetAttachment( EFalse );
       
  1392         tentry.iDate.UniversalTime();
       
  1393         tentry.SetVisible( ETrue );
       
  1394         tentry.SetInPreparation( EFalse );
       
  1395         tentry.SetUnread( isUnread );
       
  1396         tentry.SetNew( isNew );
       
  1397         tentry.SetComplete( ETrue );
       
  1398         tentry.SetSendingState( KMsvSendStateWaiting );
       
  1399         tentry.iServiceId = defaultServiceId;
       
  1400         tentry.iRelatedId = 0;
       
  1401 
       
  1402         // save the changes done above
       
  1403         smsMtm->Entry().ChangeL( tentry );
       
  1404 
       
  1405         // save the message
       
  1406         smsMtm->SaveMessageL();
       
  1407 
       
  1408         CleanupStack::PopAndDestroy( smsMtm );
       
  1409         CleanupStack::PopAndDestroy( body );
       
  1410         CleanupStack::PopAndDestroy( description );
       
  1411         CleanupStack::PopAndDestroy( fromTo );
       
  1412 
       
  1413         TInt32 id = tentry.Id();
       
  1414         TBuf8<8> idStr;
       
  1415         idStr.Copy( ( TUint8* )( &id ), sizeof( id ) );
       
  1416         SendOkMsgL( idStr );
       
  1417         }
       
  1418 
       
  1419     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleCreateSmartMsgL: Done");
       
  1420     }
       
  1421 
       
  1422 
       
  1423 // ----------------------------------------------------------------------------
       
  1424 void CMessageMgmntHandler::HandleDeleteMessageL( const TDesC8& aData )
       
  1425     {
       
  1426     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleDeleteMessageL" );
       
  1427 
       
  1428     if ( aData.Length() != 4 )
       
  1429         {
       
  1430         HTI_LOG_TEXT( "CMessageMgmntHandler: Error: wrong length of data" );
       
  1431         SendErrorMessageL( KErrArgument, KErrorInvalidId );
       
  1432         return;
       
  1433         }
       
  1434 
       
  1435     TMsvId entryId =   aData[0] +
       
  1436                      ( aData[1] << 8 ) +
       
  1437                      ( aData[2] << 16 ) +
       
  1438                      ( aData[3] << 24 );
       
  1439     HTI_LOG_FORMAT( "CMessageMgmntHandler: Deleting one message, id: %d", entryId );
       
  1440     TMsvEntry entry;
       
  1441     TMsvId service;
       
  1442     User::LeaveIfError( iSession->GetEntry( entryId, service, entry ) );
       
  1443 
       
  1444     CMsvEntry* parentCEntry = iSession->GetEntryL( entry.Parent() );
       
  1445     CleanupStack::PushL( parentCEntry );
       
  1446     TRAPD( err, parentCEntry->DeleteL( entry.Id() ) );
       
  1447     CleanupStack::PopAndDestroy( parentCEntry );
       
  1448 
       
  1449     if ( err == KErrNone )
       
  1450         {
       
  1451         SendOkMsgL( KNullDesC8 );
       
  1452         }
       
  1453     else if ( err == KErrNotFound )
       
  1454         {
       
  1455         SendErrorMessageL( err, KErrorItemNotFound );
       
  1456         }
       
  1457     else
       
  1458         {
       
  1459         SendErrorMessageL( err, KErrorFailedDelete );
       
  1460         }
       
  1461 
       
  1462     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleDeleteMessageL: Done");
       
  1463     }
       
  1464 
       
  1465 // ----------------------------------------------------------------------------
       
  1466 void CMessageMgmntHandler::HandleDeleteMessagesL( const TDesC8& aData )
       
  1467     {
       
  1468     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleDeleteMessagesFuncL" );
       
  1469 
       
  1470     if ( aData.Length() != 2 )
       
  1471         {
       
  1472         HTI_LOG_TEXT( "CMessageMgmntHandler: Error: wrong length of data" );
       
  1473         SendErrorMessageL( KErrArgument, KErrorInvalidFolder );
       
  1474         return;
       
  1475         }
       
  1476 
       
  1477     if ( aData[0] == EAllFolders )
       
  1478         {
       
  1479         HandleDeleteFromAllFoldersL( (TMessageType)aData[1] );
       
  1480         }
       
  1481     else if ( aData[1] == EAllMessageTypes )
       
  1482         {
       
  1483         HandleDeleteAllMessageTypesL( (TFolder)aData[0] );
       
  1484         }
       
  1485     else
       
  1486         {
       
  1487         HandleDeleteFromFolderByTypeL( (TFolder)aData[0],
       
  1488                                        (TMessageType)aData[1] );
       
  1489         }
       
  1490 
       
  1491     SendOkMsgL( KNullDesC8 );
       
  1492     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleDeleteMessagesFuncL: Done");
       
  1493     }
       
  1494 
       
  1495 // ----------------------------------------------------------------------------
       
  1496 void CMessageMgmntHandler::HandleDeleteFromAllFoldersL( TMessageType aType )
       
  1497     {
       
  1498     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleDeleteFromAllFoldersL" );
       
  1499 
       
  1500     if ( aType == EAllMessageTypes )
       
  1501         {
       
  1502         for ( TInt i = 1; i < ENumberOfFolders; i++ )
       
  1503             {
       
  1504             HandleDeleteAllMessageTypesL( (TFolder)i );
       
  1505             }
       
  1506         }
       
  1507     else
       
  1508         {
       
  1509         for ( TInt i = 1; i < ENumberOfFolders; i++ )
       
  1510             {
       
  1511             HandleDeleteFromFolderByTypeL( (TFolder)i, aType );
       
  1512             }
       
  1513         }
       
  1514 
       
  1515     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleDeleteFromAllFoldersL: Done");
       
  1516     }
       
  1517 
       
  1518 // ----------------------------------------------------------------------------
       
  1519 void CMessageMgmntHandler::HandleDeleteAllMessageTypesL( TFolder aFolder )
       
  1520     {
       
  1521     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleDeleteAllMessageTypesL" );
       
  1522 
       
  1523     if ( aFolder == EAllFolders )
       
  1524         {
       
  1525         for ( TInt i = 1; i < ENumberOfMessageTypes; i++ )
       
  1526             {
       
  1527             HandleDeleteFromAllFoldersL( (TMessageType)i );
       
  1528             }
       
  1529         }
       
  1530     else
       
  1531         {
       
  1532         for ( TInt i = 1; i < ENumberOfMessageTypes; i++ )
       
  1533             {
       
  1534             HandleDeleteFromFolderByTypeL( aFolder, (TMessageType)i );
       
  1535             }
       
  1536         }
       
  1537 
       
  1538     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleDeleteAllMessageTypesL: Done");
       
  1539     }
       
  1540 
       
  1541 // ----------------------------------------------------------------------------
       
  1542 void CMessageMgmntHandler::HandleDeleteFromFolderByTypeL( TFolder aFolder,
       
  1543                                                           TMessageType aType )
       
  1544     {
       
  1545     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::HandleDeleteFromFolderByTypeL" );
       
  1546 
       
  1547     TMsvId folderId = MapFolderToIdL( aFolder );
       
  1548     TUid msgTypeUid = MapMessageTypeToUidL( aType );
       
  1549 
       
  1550     HTI_LOG_TEXT( "Deleting messages..." );
       
  1551     HTI_LOG_FORMAT( "Folder: %d", aFolder );
       
  1552     HTI_LOG_FORMAT( "Message type: %d", aType );
       
  1553 
       
  1554     CMsvEntry* folder = CMsvEntry::NewL( *iSession,
       
  1555                                          folderId,
       
  1556                                          TMsvSelectionOrdering() );
       
  1557     CleanupStack::PushL( folder );
       
  1558     CMsvEntrySelection* sel = folder->ChildrenWithMtmL( msgTypeUid );
       
  1559 
       
  1560     CleanupStack::PushL( sel );
       
  1561     HTI_LOG_FORMAT( "Found %d matching items", sel->Count() );
       
  1562 
       
  1563     for ( TInt i = 0; i < sel->Count(); i++ )
       
  1564         {
       
  1565         TMsvId entryId = sel->At( i );
       
  1566         TMsvEntry entry;
       
  1567         TMsvId service;
       
  1568         User::LeaveIfError( iSession->GetEntry( entryId, service, entry ) );
       
  1569         if ( ( aType == EAudioMessage && entry.iBioType != KUidMsgSubTypeMmsAudioMsg.iUid ) ||
       
  1570                 ( aType == EMMS && entry.iBioType == KUidMsgSubTypeMmsAudioMsg.iUid ) )
       
  1571             {
       
  1572             // do not delete audio messages when MMS deletion
       
  1573             // requested and vice versa
       
  1574             continue;
       
  1575             }
       
  1576         CMsvEntry* parentCEntry = iSession->GetEntryL( entry.Parent() );
       
  1577         CleanupStack::PushL( parentCEntry );
       
  1578         parentCEntry->DeleteL( entry.Id() );
       
  1579         CleanupStack::PopAndDestroy( parentCEntry );
       
  1580         }
       
  1581 
       
  1582     CleanupStack::PopAndDestroy( sel );
       
  1583     CleanupStack::PopAndDestroy( folder );
       
  1584 
       
  1585     if ( aType == EEmail )
       
  1586         {
       
  1587         HandleDeleteFromFolderByTypeL( aFolder, EEmailPOP3 );
       
  1588         HandleDeleteFromFolderByTypeL( aFolder, EEmailIMAP4 );
       
  1589         }
       
  1590 
       
  1591     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::HandleDeleteFromFolderByTypeL: Done");
       
  1592     }
       
  1593 
       
  1594 // ----------------------------------------------------------------------------
       
  1595 void CMessageMgmntHandler::SendOkMsgL( const TDesC8& aData )
       
  1596     {
       
  1597     HTI_LOG_FUNC_IN("CMessageMgmntHandler::SendOkMsgL: Starting");
       
  1598 
       
  1599     User::LeaveIfNull( iDispatcher );
       
  1600 
       
  1601     HBufC8* temp = HBufC8::NewL( aData.Length() + 1 );
       
  1602     TPtr8 response = temp->Des();
       
  1603     response.Append( (TChar) CHtiMessagesServicePlugin::EResultOk );
       
  1604     response.Append( aData );
       
  1605     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
  1606         temp, KHtiMessagesServiceUid ) );
       
  1607 
       
  1608     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::SendOkMsgL: Done");
       
  1609     }
       
  1610 
       
  1611 // ----------------------------------------------------------------------------
       
  1612 void CMessageMgmntHandler::SendErrorMessageL( TInt aError,
       
  1613                                               const TDesC8& aDescription )
       
  1614     {
       
  1615     HTI_LOG_FUNC_IN("CMessageMgmntHandler::SendErrorMessageL: Starting");
       
  1616     User::LeaveIfNull( iDispatcher );
       
  1617     User::LeaveIfError( iDispatcher->DispatchOutgoingErrorMessage(
       
  1618         aError, aDescription, KHtiMessagesServiceUid ) );
       
  1619     HTI_LOG_FUNC_OUT("CMessageMgmntHandler::SendErrorMessageL: Done");
       
  1620     }
       
  1621 
       
  1622 // ----------------------------------------------------------------------------
       
  1623 TBool CMessageMgmntHandler::ValidateAddSmsCommand( const TDesC8& aData )
       
  1624     {
       
  1625     if ( aData.Length() < KAddSmsCmdMinLength )
       
  1626         {
       
  1627         HTI_LOG_TEXT( "ValidateAddSmsCommand: Error: missing data" );
       
  1628         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1629         return EFalse;
       
  1630         }
       
  1631 
       
  1632     TInt offset = 0;
       
  1633     TInt fromLength = aData[offset];
       
  1634 
       
  1635     offset = 1 + fromLength;
       
  1636     if ( offset > aData.Length() - 1 )
       
  1637         {
       
  1638         HTI_LOG_TEXT( "ValidateAddSmsCommand: Error: wrong length of data" );
       
  1639         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1640         return EFalse;
       
  1641         }
       
  1642     TInt descrLength = aData[offset];
       
  1643 
       
  1644     offset = offset + 1 + descrLength;
       
  1645     if ( offset > aData.Length() - 2 ) // body length in two bytes
       
  1646         {
       
  1647         HTI_LOG_TEXT( "ValidateAddSmsCommand: Error: wrong length of data" );
       
  1648         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1649         return EFalse;
       
  1650         }
       
  1651     TInt bodyLength = aData[offset] + ( aData[offset+1] << 8 );
       
  1652 
       
  1653     TInt wholeLength = 1 + fromLength +
       
  1654                        1 + descrLength +
       
  1655                        2 + bodyLength +
       
  1656                        1 + // is new
       
  1657                        1 + // is unread
       
  1658                        1;  // folder
       
  1659 
       
  1660     if ( wholeLength != aData.Length() )
       
  1661         {
       
  1662         HTI_LOG_TEXT( "ValidateAddSmsCommand: Error: wrong length of data" );
       
  1663         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1664         return EFalse;
       
  1665         }
       
  1666 
       
  1667     if ( bodyLength > 160 )
       
  1668         {
       
  1669         HTI_LOG_TEXT( "ValidateAddSmsCommand: Error: too long SMS body" );
       
  1670         SendErrorMessageL( KErrOverflow, KErrorTooLongSmsBody );
       
  1671         return EFalse;
       
  1672         }
       
  1673 
       
  1674     return ETrue;
       
  1675     }
       
  1676 
       
  1677 
       
  1678 // ----------------------------------------------------------------------------
       
  1679 TBool CMessageMgmntHandler::ValidateAddMmsOrAddEmailCommand( const TDesC8& aData )
       
  1680     {
       
  1681     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::ValidateAddMmsOrAddEmailCommand" );
       
  1682     if ( aData.Length() < KAddMmsOrEmailCmdMinLength + 1 ) // +1 = cmd code
       
  1683         {
       
  1684         HTI_LOG_TEXT( "Error: missing data" );
       
  1685         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1686         return EFalse;
       
  1687         }
       
  1688 
       
  1689     if ( aData[0] == CHtiMessagesServicePlugin::EAddAudioMsg &&
       
  1690             aData.Length() < KAddAudioCmdMinLength + 1 ) // +1 = cmd code
       
  1691         {
       
  1692         HTI_LOG_TEXT( "ValidateAddMmsOrAddEmailCommand: Error: missing data" );
       
  1693         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1694         return EFalse;
       
  1695         }
       
  1696 
       
  1697     TInt offset = 0;
       
  1698     TInt cmdCode = aData[offset];
       
  1699     offset++;
       
  1700     TInt fromToLength = aData[offset];
       
  1701     fromToLength++; // the length byte
       
  1702 
       
  1703     offset = offset + fromToLength;
       
  1704     if ( offset > aData.Length() - 1 )
       
  1705         {
       
  1706         HTI_LOG_TEXT( "Error: wrong length of data" );
       
  1707         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1708         return EFalse;
       
  1709         }
       
  1710     TInt descrLength = aData[offset];
       
  1711     descrLength++; // the length byte
       
  1712 
       
  1713     offset = offset + descrLength;
       
  1714     TInt bodyLength = 0;
       
  1715     if ( cmdCode != CHtiMessagesServicePlugin::EAddAudioMsg )
       
  1716         {
       
  1717         if ( offset > aData.Length() - 2 ) // body length in two bytes
       
  1718             {
       
  1719             HTI_LOG_TEXT( "Error: wrong length of data" );
       
  1720             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1721             return EFalse;
       
  1722             }
       
  1723         bodyLength = aData[offset] + ( aData[offset+1] << 8 );
       
  1724         bodyLength += 2; // the body length bytes
       
  1725         }
       
  1726 
       
  1727     offset = offset + bodyLength;
       
  1728     if ( offset > aData.Length() - 1 )
       
  1729         {
       
  1730         HTI_LOG_TEXT( ": wrong length of data" );
       
  1731         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1732         return EFalse;
       
  1733         }
       
  1734     TInt attPathLength = aData[offset];
       
  1735     if ( attPathLength == 0 && cmdCode == CHtiMessagesServicePlugin::EAddAudioMsg )
       
  1736         {
       
  1737         // attachment (the audio) is mandatory for audio message
       
  1738         HTI_LOG_TEXT( "Error: missing attachment" );
       
  1739         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1740         return EFalse;
       
  1741         }
       
  1742     attPathLength++; // the length byte
       
  1743 
       
  1744     TInt wholeLength = 1 + // command code
       
  1745                        fromToLength + descrLength +  bodyLength + attPathLength +
       
  1746                        1 + // is new
       
  1747                        1 + // is unread
       
  1748                        1;  // folder
       
  1749 
       
  1750     TInt extraAttPathLength = 0;
       
  1751     TInt extraAttNum = 0;
       
  1752     TInt extraNumLen = 0;
       
  1753     if( wholeLength < aData.Length() )
       
  1754     	{
       
  1755     	offset = wholeLength;
       
  1756         extraAttNum = aData[offset];
       
  1757         offset ++;
       
  1758 
       
  1759         extraNumLen = 1;
       
  1760 
       
  1761 	    while( offset < aData.Length() && extraAttNum > 0)
       
  1762 	    	{
       
  1763 	        extraAttPathLength += aData[offset];
       
  1764 	        extraAttPathLength ++;
       
  1765 	        extraAttNum --;
       
  1766 	        offset += 1 + aData[offset];
       
  1767 	    	}
       
  1768 	    }
       
  1769 
       
  1770 	wholeLength += extraNumLen + extraAttPathLength;
       
  1771 	
       
  1772     if ( wholeLength != aData.Length() )
       
  1773         {
       
  1774         HTI_LOG_TEXT( "Error: wrong length of data (wholeLength)" );
       
  1775         HTI_LOG_FORMAT( "Expected: %d", wholeLength );
       
  1776         HTI_LOG_FORMAT( "Was:      %d", aData.Length() );
       
  1777         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1778         return EFalse;
       
  1779         }
       
  1780 
       
  1781     return ETrue;
       
  1782     }
       
  1783 
       
  1784 
       
  1785 // ----------------------------------------------------------------------------
       
  1786 TBool CMessageMgmntHandler::ValidateAddObexMsgCommand( const TDesC8& aData )
       
  1787     {
       
  1788     if ( aData.Length() < KAddObexMsgCmdMinLength )
       
  1789         {
       
  1790         HTI_LOG_TEXT( "ValidateAddObexMsgCommand: Error: missing data" );
       
  1791         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1792         return EFalse;
       
  1793         }
       
  1794 
       
  1795     TInt offset = 0;
       
  1796     TInt fromToLength = aData[offset];
       
  1797 
       
  1798     offset = 1 + fromToLength;
       
  1799     if ( offset > aData.Length() - 1 )
       
  1800         {
       
  1801         HTI_LOG_TEXT( "ValidateAddObexMsgCommand: Error: wrong length of data" );
       
  1802         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1803         return EFalse;
       
  1804         }
       
  1805     TInt descrLength = aData[offset];
       
  1806 
       
  1807     offset = offset + 1 + descrLength;
       
  1808     if ( offset > aData.Length() - 1 )
       
  1809         {
       
  1810         HTI_LOG_TEXT( "ValidateAddObexMsgCommand: Error: wrong length of data" );
       
  1811         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1812         return EFalse;
       
  1813         }
       
  1814     TInt attPathLength = aData[offset];
       
  1815 
       
  1816     TInt wholeLength = 1 + fromToLength +
       
  1817                        1 + descrLength +
       
  1818                        1 + attPathLength +
       
  1819                        1 + // is new
       
  1820                        1 + // is unread
       
  1821                        1;  // folder
       
  1822 
       
  1823     if ( wholeLength != aData.Length() )
       
  1824         {
       
  1825         HTI_LOG_TEXT( "ValidateAddObexMsgCommand: Error: wrong length of data" );
       
  1826         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1827         return EFalse;
       
  1828         }
       
  1829 
       
  1830     return ETrue;
       
  1831     }
       
  1832 
       
  1833 // ----------------------------------------------------------------------------
       
  1834 TBool CMessageMgmntHandler::ValidateAddSmartMsgCommand( const TDesC8& aData )
       
  1835     {
       
  1836     if ( aData.Length() < KAddSmartMsgCmdMinLength )
       
  1837         {
       
  1838         HTI_LOG_TEXT( "ValidateAddSmartMsgCommand: Error: missing data" );
       
  1839         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1840         return EFalse;
       
  1841         }
       
  1842 
       
  1843     TInt offset = 0;
       
  1844     TInt fromToLength = aData[offset];
       
  1845 
       
  1846     offset = 1 + fromToLength;
       
  1847     if ( offset > aData.Length() - 1 )
       
  1848         {
       
  1849         HTI_LOG_TEXT( "ValidateAddSmartMsgCommand: Error: wrong length of data" );
       
  1850         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1851         return EFalse;
       
  1852         }
       
  1853     TInt descrLength = aData[offset];
       
  1854 
       
  1855     offset = offset + 1 + descrLength;
       
  1856     if ( offset > aData.Length() - 2 ) // body length in two bytes
       
  1857         {
       
  1858         HTI_LOG_TEXT( "ValidateAddSmartMsgCommand: Error: wrong length of data" );
       
  1859         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1860         return EFalse;
       
  1861         }
       
  1862     TInt bodyLength = aData[offset] + ( aData[offset+1] << 8 );
       
  1863 
       
  1864     TInt wholeLength = 1 + fromToLength +
       
  1865                        1 + descrLength +
       
  1866                        2 + bodyLength +
       
  1867                        1 + // is new
       
  1868                        1 + // is unread
       
  1869                        1 + // folder
       
  1870                        4;  // biomessage uid
       
  1871 
       
  1872     if ( wholeLength != aData.Length() )
       
  1873         {
       
  1874         HTI_LOG_TEXT( "ValidateAddSmartMsgCommand: Error: wrong length of data" );
       
  1875         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
  1876         return EFalse;
       
  1877         }
       
  1878 
       
  1879     return ETrue;
       
  1880     }
       
  1881 
       
  1882 
       
  1883 // ----------------------------------------------------------------------------
       
  1884 // Extracts UTF-8 data, converts it to Unicode and returns as 16-bit descriptor.
       
  1885 // Within aData, read descriptor from aPosition:
       
  1886 //  - first bytes tell the size of data for UTF8 formatted data
       
  1887 //  - next bytes are the data as indicated by the size
       
  1888 //  - position is finally set to the end of UTF8 data area
       
  1889 // ----------------------------------------------------------------------------
       
  1890 HBufC16* CMessageMgmntHandler::ExtractDesLC( const TDesC8& aUtf8Data,
       
  1891                                              TInt& aPosition,
       
  1892                                              TInt aSizeBytes )
       
  1893     {
       
  1894     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::ExtractDesLC" );
       
  1895     TInt length = 0;
       
  1896     for ( TInt i = 0; i < aSizeBytes; i++ )
       
  1897         {
       
  1898         length += ( aUtf8Data[aPosition+i] << ( i * 8 ) );
       
  1899         }
       
  1900 
       
  1901     if ( length < 0 ||
       
  1902          length > aUtf8Data.Mid( aPosition ).Length() )
       
  1903         {
       
  1904         User::Leave( KErrBadDescriptor );
       
  1905         }
       
  1906 
       
  1907     HBufC16* result = NULL;
       
  1908 
       
  1909     if ( length > 0 )
       
  1910         {
       
  1911         result = CnvUtfConverter::ConvertToUnicodeFromUtf8L(
       
  1912             aUtf8Data.Mid( aPosition + aSizeBytes, length ) );
       
  1913         HTI_LOG_TEXT( "ExtractDesLC: Conversion to Unicode done" );
       
  1914         CleanupStack::PushL( result );
       
  1915         }
       
  1916 
       
  1917     else
       
  1918         {
       
  1919         result = HBufC16::NewLC( 0 );
       
  1920         }
       
  1921 
       
  1922     aPosition += ( aSizeBytes + length );
       
  1923 
       
  1924     HTI_LOG_FUNC_OUT( "CMessageMgmntHandler::ExtractDesLC" );
       
  1925     return result;
       
  1926     }
       
  1927 
       
  1928 
       
  1929 // ----------------------------------------------------------------------------
       
  1930 // Extracts UTF-8 data to 8-bit descriptor without doing any conversions.
       
  1931 // ----------------------------------------------------------------------------
       
  1932 HBufC8* CMessageMgmntHandler::ExtractDes8LC( const TDesC8& aUtf8Data,
       
  1933                                              TInt& aPosition,
       
  1934                                              TInt aSizeBytes )
       
  1935     {
       
  1936     HTI_LOG_FUNC_IN( "CMessageMgmntHandler::ExtractDes8LC" );
       
  1937     TInt length = 0;
       
  1938     for ( TInt i = 0; i < aSizeBytes; i++ )
       
  1939         {
       
  1940         length += ( aUtf8Data[aPosition+i] << ( i * 8 ) );
       
  1941         }
       
  1942 
       
  1943     if ( length < 0 ||
       
  1944          length > aUtf8Data.Mid( aPosition ).Length() )
       
  1945         {
       
  1946         User::Leave( KErrBadDescriptor );
       
  1947         }
       
  1948 
       
  1949     HBufC8* result = HBufC8::NewLC( length );
       
  1950 
       
  1951     if ( length > 0 )
       
  1952         {
       
  1953         result->Des().Copy( aUtf8Data.Mid( aPosition + aSizeBytes, length ) );
       
  1954         }
       
  1955 
       
  1956     aPosition += ( aSizeBytes + length );
       
  1957 
       
  1958     HTI_LOG_FUNC_OUT( "CMessageMgmntHandler::ExtractDes8LC" );
       
  1959     return result;
       
  1960     }
       
  1961 
       
  1962 
       
  1963 // ----------------------------------------------------------------------------
       
  1964 TMsvId CMessageMgmntHandler::MapFolderToIdL( TFolder aFolder )
       
  1965     {
       
  1966     TMsvId id = 0;
       
  1967 
       
  1968     switch ( aFolder )
       
  1969         {
       
  1970         case EInbox:  { id = KMsvGlobalInBoxIndexEntryId;   break; }
       
  1971         case EDrafts: { id = KMsvDraftEntryId;              break; }
       
  1972         case ESent:   { id = KMsvSentEntryId;               break; }
       
  1973         case EOutbox: { id = KMsvGlobalOutBoxIndexEntryId;  break; }
       
  1974         default:      { User::Leave( KErrArgument );        break; }
       
  1975         }
       
  1976 
       
  1977     return id;
       
  1978     }
       
  1979 
       
  1980 // ----------------------------------------------------------------------------
       
  1981 TUid CMessageMgmntHandler::MapMessageTypeToUidL( TMessageType aType )
       
  1982     {
       
  1983     TUid uid = { 0 };
       
  1984 
       
  1985     switch ( aType )
       
  1986         {
       
  1987         case ESMS:          { uid = KUidMsgTypeSMS;            break; }
       
  1988         case EAudioMessage: // fall through - audio msg is MMS sub type
       
  1989         case EMMS:          { uid = KUidMsgTypeMultimedia;     break; }
       
  1990         case ESmartMessage: { uid = KUidBIOMessageTypeMtm;     break; }
       
  1991         case EEmail:        { uid = KUidMsgTypeSMTP;           break; }
       
  1992         case EEmailPOP3:    { uid = KUidMsgTypePOP3;           break; }
       
  1993         case EEmailIMAP4:   { uid = KUidMsgTypeIMAP4;          break; }
       
  1994         case EIrMessage:    { uid = KUidMsgTypeIrUID;          break; }
       
  1995         case EBtMessage:    { uid = KUidMsgTypeBt;             break; }
       
  1996         default:            { User::Leave( KErrArgument );     break; }
       
  1997         }
       
  1998 
       
  1999     return uid;
       
  2000     }
       
  2001 
       
  2002 // ----------------------------------------------------------------------------
       
  2003 void CMessageMgmntHandler::HandleSessionEventL( TMsvSessionEvent /*aEvent*/,
       
  2004                                                 TAny* /*aArg1*/,
       
  2005                                                 TAny* /*aArg2*/,
       
  2006                                                 TAny* /*aArg3*/ )
       
  2007     {
       
  2008     }
       
  2009 
       
  2010 
       
  2011 
       
  2012 // ----------------------------------------------------------------------------
       
  2013 CWaiter* CWaiter::NewL( TInt aPriority )
       
  2014     {
       
  2015     CWaiter* self = new(ELeave) CWaiter( aPriority );
       
  2016     return self;
       
  2017     }
       
  2018 
       
  2019 // ----------------------------------------------------------------------------
       
  2020 CWaiter* CWaiter::NewLC( TInt aPriority )
       
  2021     {
       
  2022     CWaiter* self = new(ELeave) CWaiter( aPriority );
       
  2023     CleanupStack::PushL( self );
       
  2024     return self;
       
  2025     }
       
  2026 
       
  2027 // ----------------------------------------------------------------------------
       
  2028 CWaiter::CWaiter( TInt aPriority ) : CActive( aPriority )
       
  2029     {
       
  2030     CActiveScheduler::Add( this );
       
  2031     }
       
  2032 
       
  2033 // ----------------------------------------------------------------------------
       
  2034 CWaiter::~CWaiter()
       
  2035     {
       
  2036     Cancel();
       
  2037     }
       
  2038 
       
  2039 // ----------------------------------------------------------------------------
       
  2040 void CWaiter::StartAndWait()
       
  2041     {
       
  2042     iStatus = KRequestPending;
       
  2043     SetActive();
       
  2044     iWait.Start();
       
  2045     }
       
  2046 
       
  2047 // ----------------------------------------------------------------------------
       
  2048 TInt CWaiter::Result() const
       
  2049     {
       
  2050     return iResult;
       
  2051     }
       
  2052 
       
  2053 // ----------------------------------------------------------------------------
       
  2054 void CWaiter::RunL()
       
  2055     {
       
  2056     iResult = iStatus.Int();
       
  2057     iWait.AsyncStop();
       
  2058     }
       
  2059 
       
  2060 // ----------------------------------------------------------------------------
       
  2061 void CWaiter::DoCancel()
       
  2062     {
       
  2063     iResult = KErrCancel;
       
  2064     if ( iStatus == KRequestPending )
       
  2065         {
       
  2066         TRequestStatus* status = &iStatus;
       
  2067         User::RequestComplete( status, KErrCancel );
       
  2068         }
       
  2069 
       
  2070     iWait.AsyncStop();
       
  2071     }
       
  2072 
       
  2073 
       
  2074 // End of file