messagingappbase/smsmtm/servermtm/src/SMSSendSession.cpp
changeset 25 84d9eb65b26f
parent 23 238255e8b033
child 27 e4592d119491
child 37 518b245aa84c
child 79 2981cb3aa489
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "SMSSendSession.h"
       
    17 
       
    18 #include <msventry.h>
       
    19 #include <smutset.h>
       
    20 #include <smuthdr.h>
       
    21 
       
    22 #include "SMSSPAN.H"
       
    23 #include "SMSSSEND.H"
       
    24 #include "SMSRecipientSend.h"
       
    25 
       
    26 const TInt KErrUnknownBioType = KErrNotSupported;
       
    27 
       
    28 CSmsSendSession* CSmsSendSession::NewL(TSmsProgress& aProgress, CMsvServerEntry& aServerEntry, RFs& aFs, CSmsHeader& aHeader, CRichText& aRichText, TMsvEntry& aEntry)
       
    29 	{
       
    30 	CSmsSendSession* self = new (ELeave) CSmsSendSession(aProgress, aFs, aServerEntry, aHeader);
       
    31 	CleanupStack::PushL(self);
       
    32 
       
    33 	self->ConstructL(aRichText, aEntry);
       
    34 
       
    35 	CleanupStack::Pop();
       
    36 
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 CSmsSendSession::CSmsSendSession(TSmsProgress& aProgress, RFs& aFs, CMsvServerEntry& aServerEntry, CSmsHeader& aHeader)
       
    41 	: CSmssActive(aFs, aServerEntry, KSmsSessionPriority), iProgress(aProgress), iSmsHeader(aHeader)
       
    42 	{
       
    43 	CActiveScheduler::Add(this);
       
    44 	}
       
    45 
       
    46 CSmsSendSession::~CSmsSendSession()
       
    47 	{
       
    48 	if (iSendTypes)
       
    49 		iSendTypes->ResetAndDestroy();
       
    50 
       
    51 	delete iSendTypes;
       
    52 	delete iSmsSend;
       
    53 	}
       
    54 
       
    55 void CSmsSendSession::ConstructL(CRichText& aRichText, TMsvEntry& aEntry)
       
    56 	{
       
    57 	iSendTypes = new (ELeave) CArrayPtrFlat<CSmsSendType>(1);
       
    58 	iSmsSend = CSmsSend::NewL(iProgress, iServerEntry, iFs, iSmsHeader, aRichText, aEntry);
       
    59 	}
       
    60 
       
    61 void CSmsSendSession::DivideMessagesL(CMsvEntrySelection& aSelection)
       
    62 	{
       
    63 	iProgress.iMsgCount = 0;
       
    64 	TInt selCount = aSelection.Count();
       
    65 
       
    66 	if (!selCount)
       
    67 		User::Leave(KErrNotFound);
       
    68 
       
    69 	CreateRecipientsL();
       
    70 	const TInt typeCount = iSendTypes->Count();
       
    71 	TInt divideError = KErrNone; //Used later as the error code to leave with if aSelection.Count() == 0
       
    72 
       
    73 	//Order important
       
    74 	while (selCount--)
       
    75 		{
       
    76 		TMsvId id = aSelection[selCount];
       
    77 		TInt err = iServerEntry.SetEntry(id);
       
    78     	SMSSLOG(FLogFormat(_L8("CSmsSendSession::DivideMessagesL() - switch to entry: %d, err: %d"), id, err ));
       
    79 
       
    80 		if (!err)
       
    81 			{
       
    82 			TMsvEntry entry = iServerEntry.Entry();
       
    83 
       
    84 			if(entry.InPreparation())
       
    85 				{
       
    86 		    	SMSSLOG(FLogFormat(_L8("CSmsSendSession::DivideMessagesL() - entry is in preparation - deleting it from selection array...")));
       
    87 				aSelection.Delete(selCount);
       
    88 				}
       
    89 			else
       
    90 				{   
       
    91 				//Restore the CSmsHeader
       
    92 				CMsvStore* store = NULL;
       
    93 				TInt error = KErrNone;
       
    94 				TRAP(error, store = iServerEntry.ReadStoreL());
       
    95 				if(error == KErrAccessDenied )
       
    96 					{
       
    97 		    		SMSSLOG(FLogFormat(_L8("CSmsSendSession::DivideMessagesL() - Error = -21 - deleting it from selection array...")));
       
    98 					entry.SetSendingState(KMsvSendStateWaiting);
       
    99 					iServerEntry.ChangeEntry(entry);
       
   100 					aSelection.Delete(selCount);
       
   101 					}
       
   102 				else
       
   103 					{
       
   104 					User::LeaveIfError(error);
       
   105 					CleanupStack::PushL(store);
       
   106 					iSmsHeader.RestoreL(*store);
       
   107 					CleanupStack::PopAndDestroy(); //store
       
   108 
       
   109 					TBool msgAdded = EFalse;
       
   110 
       
   111 					for(TInt curType = 0; curType < typeCount && !msgAdded; curType++)
       
   112 						{
       
   113 						CSmsSendType* smsType = iSendTypes->At(curType);
       
   114 		
       
   115 						if (smsType->iRecipientSend->AcceptMessage(entry, iSmsHeader))
       
   116 							{
       
   117 							smsType->iSelection->InsertL(0, id);
       
   118 							iProgress.iMsgCount++;
       
   119 							msgAdded = ETrue;
       
   120 							}
       
   121 						} //end for curType
       
   122 
       
   123 					if (!msgAdded)
       
   124 						{
       
   125 						SMSSLOG(FLogFormat(_L8("\tCannot send message %d - Invalid bioType (%d) and bioIdType (%d) combination"), id, entry.iBioType, iSmsHeader.BioMsgIdType()));
       
   126 
       
   127 						//Message is not going to be sent, so set failed
       
   128 						entry.SetFailed(ETrue);
       
   129 						entry.SetSendingState(KMsvSendStateFailed);
       
   130 						entry.iError = KErrUnknownBioType;
       
   131 						divideError = KErrUnknownBioType;
       
   132 						iServerEntry.ChangeEntry(entry); //ignore error?
       
   133 
       
   134 						aSelection.Delete(selCount);
       
   135 						}	
       
   136 					}
       
   137 				}
       
   138 			} 
       
   139 		else //SetEntry() failed
       
   140 			{
       
   141 			SMSSLOG(FLogFormat(_L8("\tCannot send message %d - iServerEntry.SetEntry(%d) returned error %d"), id, id, err));
       
   142 
       
   143 			if (err != KErrNotFound && err != KErrLocked)
       
   144 				User::Leave(err);
       
   145 
       
   146 			if (!divideError && divideError != KErrUnknownBioType)
       
   147 				divideError = err;
       
   148 
       
   149 			aSelection.Delete(selCount);
       
   150 			}
       
   151 		} //end for curMsg
       
   152 
       
   153 	if (!iProgress.iMsgCount)
       
   154 		{
       
   155 		SMSSLOG(FLogFormat(_L8("\tNO messages to send - CSmsSendSession::DivideMessagesL() error %d"), divideError));
       
   156 		User::Leave(divideError);
       
   157 		}
       
   158     SMSSLOG(FLogFormat(_L8("CSmsSendSession::DivideMessagesL() - END") ));
       
   159 	}
       
   160 
       
   161 void CSmsSendSession::CreateRecipientsL()
       
   162 	{
       
   163 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::CreateRecipientsL() - iServerEntry id: %d"), iServerEntry.Entry().Id() ));
       
   164 	//Create WAP and Text Recipients.
       
   165 	//Note: The order of adding rcptWap and rcptText to iSendTypes is important
       
   166 	//because rcptText->AcceptMessage() always returns ETrue, therefore rcptWap
       
   167 	//must be inserted before rcptText.
       
   168 
       
   169 	//Create a Wap Recipient
       
   170 	CWapRecipientSend* rcptWap = CWapRecipientSend::NewL(iProgress, iFs, iServerEntry);
       
   171 	CleanupStack::PushL(rcptWap);
       
   172 	CSmsSendType* smsType = CSmsSendType::NewL(rcptWap);
       
   173 	CleanupStack::PushL(smsType);
       
   174 	iSendTypes->AppendL(smsType);
       
   175 	CleanupStack::Pop(2); //rcptWap, smsType
       
   176 
       
   177 	//Create a Text Recipient
       
   178 	CTextRecipientSend* rcptText = CTextRecipientSend::NewL(iProgress, iFs, iServerEntry);
       
   179 	CleanupStack::PushL(rcptText);
       
   180 	smsType = CSmsSendType::NewL(rcptText);
       
   181 	CleanupStack::PushL(smsType);
       
   182 	iSendTypes->AppendL(smsType);
       
   183 	CleanupStack::Pop(2); //rcptText, smsType
       
   184 	}
       
   185 
       
   186 TMsvId CSmsSendSession::IncSms()
       
   187 	{
       
   188 	CSmsSendType* sendType = NULL;
       
   189 	TInt typeCount = iSendTypes->Count();
       
   190 
       
   191 	iId = 0;
       
   192 
       
   193 	if (typeCount)
       
   194 		{
       
   195 		while (iCurrentSendType < typeCount && !iId)
       
   196 			{
       
   197 			sendType = iSendTypes->At(iCurrentSendType);
       
   198 			CMsvEntrySelection& selection = *(sendType->iSelection);
       
   199 			sendType->iCurrentMessage++;
       
   200 
       
   201 			if (sendType->iCurrentMessage < selection.Count())
       
   202 				{
       
   203 				iId = selection[sendType->iCurrentMessage];
       
   204 				}
       
   205 			else
       
   206 				{
       
   207 				iCurrentSendType++;
       
   208 				}
       
   209 			}
       
   210 		}
       
   211 
       
   212 	return iId;
       
   213 	}
       
   214 
       
   215 void CSmsSendSession::SendSms(TRequestStatus& aStatus)
       
   216 	{
       
   217 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::SendSms() - START - iId: %d"), iId ));
       
   218 	Queue(aStatus);
       
   219 
       
   220 	if (iId)
       
   221 		{
       
   222 		iState = ESessionSending;
       
   223 		CSmsSendType* sendType = iSendTypes->At(iCurrentSendType);
       
   224 		iSmsSend->Start(iStatus, iId, sendType->iRecipientSend);
       
   225 		}
       
   226 	else
       
   227 		{
       
   228 		iState = ESessionNoMoreMessages;
       
   229 		RequestComplete(&iStatus, KErrNotFound, EFalse);
       
   230 		}
       
   231 
       
   232 	SetActive();
       
   233 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::SendSms() - END - iId: %d, iState: %d"), iId, iState ));
       
   234 	}
       
   235 
       
   236 void CSmsSendSession::DoSmssCancel()
       
   237 	{
       
   238 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::DoSmssCancel() - iId: %d, iState: %d"), iId, iState ));
       
   239 	switch (iState)
       
   240 		{
       
   241 		case ESessionSending:
       
   242 			iSmsSend->Cancel();
       
   243 			break;
       
   244 		default:
       
   245 			break;
       
   246 		}
       
   247 	}
       
   248 
       
   249 void CSmsSendSession::DoRunL()
       
   250 	{
       
   251 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::DoSmssCancel() - START - iId: %d, iState: %d"), iId, iState ));
       
   252 	switch (iState)
       
   253 		{
       
   254 		case ESessionSending:
       
   255 			break;
       
   256 		case ESessionNoMoreMessages:
       
   257 		case ESessionNoSendTypes:
       
   258 			iProgress.iError = KErrNotFound;
       
   259 			break;
       
   260 		default:
       
   261 			Panic(KSmssPanicUnexpectedState);
       
   262 		}
       
   263 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::DoSmssCancel() - END - iId: %d, iState: %d"), iId, iState ));
       
   264 	}
       
   265 
       
   266 void CSmsSendSession::DoComplete(TInt& aStatus)
       
   267 	{
       
   268 	SMSSLOG(FLogFormat(_L8("CSmsSendSession::DoSmssCancel() - aStatus: %d, iState: %d, iProgress.iError: %d"), aStatus, iState, iProgress.iError ));
       
   269 	if (aStatus != KErrNone && iProgress.iError == KErrNone)
       
   270 		{
       
   271 		iProgress.iError = aStatus;
       
   272 		}
       
   273 
       
   274 	aStatus = KErrNone;
       
   275 	}
       
   276 
       
   277 CSmsSendSession::CSmsSendType* CSmsSendSession::CSmsSendType::NewL(CSmsRecipientSend* aRecipient)
       
   278 	{
       
   279 	CSmsSendType* self = new (ELeave) CSmsSendType(aRecipient);
       
   280 	CleanupStack::PushL(self);
       
   281 
       
   282 	self->ConstructL();
       
   283 
       
   284 	CleanupStack::Pop();
       
   285 
       
   286 	return self;
       
   287 	}
       
   288 
       
   289 CSmsSendSession::CSmsSendType::CSmsSendType(CSmsRecipientSend* aRecipient)
       
   290 : iRecipientSend(aRecipient), iCurrentMessage(-1)
       
   291 	{
       
   292 	}
       
   293 
       
   294 CSmsSendSession::CSmsSendType::~CSmsSendType()
       
   295 	{
       
   296 	delete iRecipientSend;
       
   297 	delete iSelection;
       
   298 	}
       
   299 
       
   300 void CSmsSendSession::CSmsSendType::ConstructL()
       
   301 	{
       
   302 	iSelection = new (ELeave) CMsvEntrySelection();
       
   303 	}