serviceproviders/sapi_messaging/messagingservice/src/sendmessage.cpp
changeset 5 989d2f495d90
child 11 5c0037c72160
equal deleted inserted replaced
1:a36b1e19a461 5:989d2f495d90
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <senduiconsts.h>
       
    21 #include <mmsconst.h>
       
    22 #include <CMsvAttachment.h>
       
    23 
       
    24 #include <APGCLI.H>
       
    25 
       
    26 #include "messageheader.h"
       
    27 #include "sendmessage.h"
       
    28 
       
    29 _LIT(KBodyTextFilePath,"C:\\");
       
    30 // ---------------------------------------------------------------------------
       
    31 // Two-phased constructor.
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CSendMessage* CSendMessage::NewL( CMsvSession& aServerSession )
       
    35 	{
       
    36 	CSendMessage* self = new (ELeave) CSendMessage( aServerSession );
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Destructor.
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CSendMessage::~CSendMessage()
       
    45 	{
       
    46 	if ( IsActive() )
       
    47 		{
       
    48 		iCallNotifyForCancelFlag = EFalse;
       
    49 		Cancel();
       
    50 		}
       
    51 	
       
    52 	iMessage.Close();
       
    53 	iSendAs.Close();
       
    54 	
       
    55 	delete iMessageParam;
       
    56 	delete iNotifyCallback;
       
    57 	delete iTemplateDetail;
       
    58 	}
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // Two-phased constructor.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CSendMessage::CSendMessage( CMsvSession& aServerSession ):
       
    65 				CActive( EPriorityStandard ),
       
    66 				iServerSession( aServerSession ),
       
    67 				iCallNotifyForCancelFlag(ETrue),
       
    68 				iBodyTextFileFlag(EFalse)
       
    69 	{
       
    70 	}
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Sets message input parameters 
       
    74 // aMessageParam/aTemplateDetail/aNotifyCallback ownership is passed to this
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void CSendMessage::SetInputParamsL( CSendMessageParams* aMessageParam,
       
    78 									CMessageDetailInfo* aTemplateDetail,
       
    79 									CMsgCallbackBase* aNotifyCallback,
       
    80 									MAsyncRequestObserver* aAsyncRequestObserver )
       
    81 	{
       
    82 	iMessageParam = CSendMessageParams::NewL();
       
    83 	*iMessageParam = *aMessageParam;
       
    84 	iNotifyCallback = aNotifyCallback;
       
    85 	iTemplateDetail = aTemplateDetail;
       
    86 	iAsyncRequestObserver = aAsyncRequestObserver;
       
    87 	}
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // Sends the message
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 void CSendMessage::SendMessageL()
       
    94 	{
       
    95 	if ( iNotifyCallback ) // making call as asynchronous
       
    96 		{
       
    97 		CActiveScheduler::Add( this );
       
    98 		iMessageState = EInitialize;
       
    99 		ActivateRequest( KErrNone );
       
   100 		}
       
   101 	else				// making call as synchronous
       
   102 		{
       
   103 		InitializeL();
       
   104 		ValidateL();
       
   105 		SendL();
       
   106 		}
       
   107 	}
       
   108 	
       
   109 // ---------------------------------------------------------------------------
       
   110 // Inherited from CActive class 
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CSendMessage::DoCancel()
       
   114 	{
       
   115 	NotifyRequestResult( KErrCancel );
       
   116 	}
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // Inherited from CActive class 
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CSendMessage::RunL()
       
   123 	{
       
   124 	TInt err = iStatus.Int();
       
   125 	if ( err == KErrNone )
       
   126 		{
       
   127 		switch ( iMessageState )
       
   128 			{
       
   129 			case EInitialize: 
       
   130 				{
       
   131 				TRAP( err, InitializeL() );
       
   132 				iMessageState = EValidate;
       
   133 				ActivateRequest( err );
       
   134 				}
       
   135 				break;	
       
   136 
       
   137 			case EValidate: 
       
   138 				{
       
   139 				TRAP( err, ValidateL() );
       
   140 				iMessageState = ESend;
       
   141 				ActivateRequest( err );
       
   142 				}
       
   143 				break;	
       
   144 
       
   145 			case ESend: 
       
   146 				{
       
   147 				TRAP( err, SendL() );
       
   148 				iMessageState = EComplete;
       
   149 				ActivateRequest( err );
       
   150 				}
       
   151 				break;
       
   152 
       
   153 			case EComplete: 
       
   154 				{
       
   155 				NotifyRequestResult( err );
       
   156 				}
       
   157 				break;
       
   158 					
       
   159 			default:
       
   160 				NotifyRequestResult( KErrGeneral );	
       
   161 				break;	
       
   162 			}
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		NotifyRequestResult( err );		
       
   167 		}
       
   168 	}
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // Initialises RSendAs Server object and RSendAsMessage Object
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 void CSendMessage::InitializeL()
       
   175 	{
       
   176 	TInt err;
       
   177 	
       
   178 	err = iSendAs.Connect();
       
   179 
       
   180 	if ( err == KErrNone )
       
   181 		{
       
   182 		iMessage.CreateL( iSendAs, iMessageParam->MessageType() );
       
   183 		}
       
   184 	else
       
   185 		{
       
   186 		User::Leave(err);	
       
   187 		}
       
   188 	
       
   189 	if ( iTemplateDetail )
       
   190 		{
       
   191 		if ( iTemplateDetail->BodyText().Length() )
       
   192 			{
       
   193 			iMessageParam->AppendBodyTextL( iTemplateDetail->BodyText() );
       
   194 			}
       
   195 		
       
   196 		const CArrayPtr<CMessageAttachInfo>* attacharray = iTemplateDetail->AttachmentInfoArray();
       
   197 		
       
   198 		if ( attacharray )
       
   199 			{
       
   200 			for ( TInt pos = 0; pos < attacharray->Count(); pos++ )
       
   201 				{
       
   202 				CMessageAttachInfo* element = (*attacharray)[pos];
       
   203 				
       
   204 				TBuf8<KMaxFileName> tmpMime;
       
   205 				tmpMime.Copy( element->MimeType() );
       
   206 				
       
   207 				TRequestStatus stat;
       
   208 				RFile fileHandle = element->FileHandle();
       
   209 				iMessage.AddAttachment( fileHandle, tmpMime, stat );
       
   210 				User::WaitForRequest( stat );
       
   211 
       
   212 				}
       
   213 			}
       
   214 			
       
   215 		delete iTemplateDetail;
       
   216 		iTemplateDetail = NULL;
       
   217 		}
       
   218 	}
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // Sets Subject, Recipient, BodyText, Attachments to RSendAsMessage object
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CSendMessage::ValidateL()
       
   225 	{
       
   226 	AddSubjectL();
       
   227 	AddRecipientL();
       
   228 	AddBodyTextL();
       
   229 	AddAttachmentL();
       
   230 	}
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // Internal function 
       
   234 // ---------------------------------------------------------------------------
       
   235 //
       
   236 void CSendMessage::SendL()
       
   237 	{
       
   238 	if( iMessageParam->LaunchEditor() )
       
   239 		{
       
   240 		iMessage.LaunchEditorAndCloseL();
       
   241 		}
       
   242 	else
       
   243 		{
       
   244 		iMessage.SendMessageAndCloseL();	
       
   245 		}
       
   246 	if( iBodyTextFileFlag )	//If the bodytext attach file is created for mms, delete it
       
   247 		{
       
   248 		RFs rFs;
       
   249     	if( rFs.Connect() == KErrNone )
       
   250     		{
       
   251     		rFs.Delete( iBodyTextFileName ); //if found delete it b4 creating it
       
   252    			rFs.Close();	
       
   253     		}
       
   254 		}	
       
   255 	}
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // Activates the asynchronous request
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 void CSendMessage::ActivateRequest( TInt aReason )
       
   262 	{
       
   263 	iStatus = KRequestPending;
       
   264 	SetActive();
       
   265 	TRequestStatus* temp = &iStatus;
       
   266 	User::RequestComplete( temp, aReason );
       
   267 	}
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // Notifies callback the result for asynchronous request.
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 void CSendMessage::NotifyRequestResult( TInt aReason )
       
   274 	{
       
   275 	if ( iNotifyCallback )
       
   276 		{
       
   277 		iAsyncRequestObserver->RequestComplete( iNotifyCallback->iTransactionId );
       
   278 		
       
   279 		if( iCallNotifyForCancelFlag )
       
   280 			{
       
   281 			TRAPD( err, iNotifyCallback->NotifyResultL( aReason, NULL ));	
       
   282 			}
       
   283 		}
       
   284 	// caller will delete the object in case of cancel
       
   285 	if ( aReason != KErrCancel )
       
   286 		delete this;
       
   287 	}
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // Sets the subject for message
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 void CSendMessage::AddSubjectL()
       
   294 	{
       
   295 	TPtrC subject( iMessageParam->Subject());
       
   296 	if ( subject.Length() )
       
   297 		iMessage.SetSubjectL( iMessageParam->Subject() );
       
   298 	}
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // Sets the recipient address.
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CSendMessage::AddRecipientL()
       
   305 	{
       
   306 	const CRecipientList* recipientArray = iMessageParam->RecipientArray();
       
   307 	if ( recipientArray )
       
   308 		{
       
   309 		TInt count = recipientArray->Count();
       
   310 		for( TInt pos = 0; pos < count; pos++ )
       
   311 			{
       
   312 			iMessage.AddRecipientL( (*recipientArray)[pos], 
       
   313 				RSendAsMessage::TSendAsRecipientType( recipientArray->Type(pos) ) );
       
   314 			}
       
   315 		}
       
   316 	else if ( !iMessageParam->LaunchEditor() )
       
   317 		{
       
   318 		User::Leave( KErrArgument );
       
   319 		}
       
   320 	}
       
   321 
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // Sets body text to message.
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 void CSendMessage::AddBodyTextL()
       
   328 	{
       
   329 	TPtrC filePath( KBodyTextFilePath );
       
   330 	
       
   331 	TPtrC bodyText = iMessageParam->BodyText();
       
   332 	
       
   333 	if ( bodyText.Length() )
       
   334 		{
       
   335 		if ( iMessageParam->MessageType() == KSenduiMtmMmsUid )
       
   336 			{
       
   337     		RFs rFs;
       
   338 			RFile tmpfile;
       
   339     		User::LeaveIfError( rFs.Connect() );
       
   340    			
       
   341     		tmpfile.Temp(rFs, filePath, iBodyTextFileName, EFileShareExclusive|EFileWrite);
       
   342 			
       
   343 			iBodyTextFileFlag = ETrue;
       
   344 			
       
   345 			HBufC8* buffer=HBufC8::NewL( bodyText.Length() );
       
   346 			CleanupStack::PushL( buffer );
       
   347 			buffer->Des().Copy( bodyText );
       
   348 			tmpfile.Write( *buffer ); 
       
   349 			tmpfile.Flush();
       
   350 			tmpfile.Close();			
       
   351 			rFs.Close();
       
   352 			CleanupStack::PopAndDestroy( buffer );
       
   353 			
       
   354 			TRequestStatus stat;
       
   355 			iMessage.AddAttachment( iBodyTextFileName, KMmsTextPlain, stat );
       
   356 			User::WaitForRequest( stat );
       
   357 			}
       
   358 		else
       
   359 			{
       
   360 			iMessage.SetBodyTextL( bodyText );	
       
   361 			}
       
   362 		}
       
   363 	}
       
   364 
       
   365 // ---------------------------------------------------------------------------
       
   366 // Adds attachments to message
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 void CSendMessage::AddAttachmentL()
       
   370 	{
       
   371 	const CArrayPtr<CMsvAttachment>* attachmentArray = iMessageParam->AttachmentArray();
       
   372 	if ( attachmentArray && attachmentArray->Count() > 0 )
       
   373 		{
       
   374 		TRequestStatus attachstatus;
       
   375 		TInt error = KErrNone;
       
   376 		TInt count;
       
   377 		RApaLsSession rapaSesion;
       
   378 		RFs rFs;
       
   379 		RFile tmpfile;
       
   380 		
       
   381 		User::LeaveIfError( rapaSesion.Connect() );
       
   382 		
       
   383 		CleanupClosePushL( rapaSesion );
       
   384 		
       
   385 		User::LeaveIfError( rFs.Connect() );
       
   386 		
       
   387 		CleanupClosePushL( rFs );
       
   388 		
       
   389 		for ( count = 0; count < attachmentArray->Count(); count++ )
       
   390 			{
       
   391 			attachstatus = KRequestPending;	
       
   392 			
       
   393 			error = tmpfile.Open(rFs, ((*attachmentArray)[count])->AttachmentName(), EFileRead);
       
   394 			
       
   395 			if( error == KErrNone )
       
   396 				{
       
   397 				TBuf8<256> tmpbuf;
       
   398 				
       
   399 				tmpfile.Read(tmpbuf, 255 );
       
   400 				
       
   401 				tmpfile.Close();
       
   402 				
       
   403 				TDataRecognitionResult aDataType;
       
   404 				
       
   405 				error = rapaSesion.RecognizeData(((*attachmentArray)[count])->AttachmentName(), tmpbuf, aDataType);
       
   406 				
       
   407 				if( error == KErrNone )
       
   408 					{
       
   409 					TBuf8< KMaxDataTypeLength > tmp;
       
   410 					tmp.Copy( aDataType.iDataType.Des() );
       
   411 					
       
   412 					iMessage.AddAttachment( ((*attachmentArray)[count])->AttachmentName(),
       
   413 												tmp,
       
   414 												attachstatus );																										
       
   415 					}
       
   416 				else
       
   417 					{					
       
   418 					if ( ((*attachmentArray)[count])->MimeType().Length() )
       
   419 						{
       
   420 						iMessage.AddAttachment( ((*attachmentArray)[count])->AttachmentName(),
       
   421 												((*attachmentArray)[count])->MimeType(),
       
   422 												attachstatus );
       
   423 						}
       
   424 					else
       
   425 						{
       
   426 						iMessage.AddAttachment( ((*attachmentArray)[count])->AttachmentName(), 
       
   427 												attachstatus );
       
   428 						}
       
   429 					}	
       
   430 				
       
   431 				User::WaitForRequest( attachstatus );
       
   432 				
       
   433 				User::LeaveIfError( attachstatus.Int() );	
       
   434 				}
       
   435 			else
       
   436 				{
       
   437 				User::Leave( error );				
       
   438 				}	
       
   439 			}
       
   440 		CleanupStack::PopAndDestroy( &rFs );	
       
   441 		CleanupStack::PopAndDestroy( &rapaSesion );
       
   442 		}
       
   443 	}
       
   444 
       
   445