messagingappbase/smsmtm/servermtm/src/smssactive.cpp
changeset 23 238255e8b033
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     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 #ifdef _DEBUG
       
    17 #undef _MSG_NO_LOGGING
       
    18 #endif
       
    19 
       
    20 #include "smssactive.h"
       
    21 
       
    22 #include <gsmubuf.h>
       
    23 #include <msvstd.h>
       
    24 #include <smutset.h>
       
    25 #include <msventry.h>
       
    26 #include <smuthdr.h>
       
    27 
       
    28 #include "SMSSPAN.H"
       
    29 
       
    30 
       
    31 #ifndef _MSG_NO_LOGGING
       
    32 	#include <flogger.h>
       
    33 	_LIT(KSmssLogFile, "Smss.txt");
       
    34 	_LIT(KSmssLogDir, "Sms");
       
    35 	const TInt KSmssLogMaxLength = KLogBufferSize - 22;
       
    36 	/**
       
    37 	Names for Multimode ETel sub-sessions
       
    38 	*/
       
    39 	_LIT(KETelMeSmsStore,"S13");
       
    40 	_LIT(KETelIccSmsStore,"S14");
       
    41 	_LIT(KETelCombinedSmsStore,"S15");
       
    42 #endif
       
    43 
       
    44 
       
    45 
       
    46 CSmssActive::CSmssActive(RFs& aFs, CMsvServerEntry& aServerEntry, TInt aPriority)
       
    47 : CActive(aPriority), iFs(aFs), iServerEntry(aServerEntry)
       
    48 	{
       
    49 	}
       
    50 
       
    51 CSmssActive::~CSmssActive()
       
    52 	{
       
    53 #ifndef _MSG_NO_LOGGING
       
    54 	delete iLastLogFile;
       
    55 
       
    56 	if (iFlogger.LogValid())
       
    57 		iFlogger.CloseLog();
       
    58 
       
    59 	iFlogger.Close();
       
    60 #endif
       
    61 	}
       
    62 
       
    63 void CSmssActive::RequestComplete(TRequestStatus* aStatus, TInt aError, TBool aSetActive)
       
    64 	{
       
    65 	User::RequestComplete(aStatus, aError);
       
    66 
       
    67 	if (aSetActive)
       
    68 		{
       
    69 		SetActive();
       
    70 		}
       
    71 	}
       
    72 
       
    73 void CSmssActive::Queue(TRequestStatus& aStatus)
       
    74 //
       
    75 // call this last when an asynch operation has been requested
       
    76 //
       
    77 	{
       
    78 	__ASSERT_DEBUG(iReport==NULL, Panic(KSmssPanicAlreadyActive));
       
    79 
       
    80 	aStatus=KRequestPending;
       
    81 	iReport=&aStatus;
       
    82 	}
       
    83 
       
    84 void CSmssActive::DoCancel()
       
    85 //
       
    86 // must be called at end of derived classes DoCancel()
       
    87 //
       
    88 	{
       
    89 	DoSmssCancel();
       
    90 	TInt result=KErrCancel;
       
    91 	Complete(result);	// can be done safely as asynch reporting
       
    92 	}
       
    93 
       
    94 TInt CSmssActive::RunError(TInt aError)
       
    95 	{
       
    96 	Complete(aError);
       
    97 	return KErrNone;
       
    98 	}
       
    99 
       
   100 void CSmssActive::RunL()
       
   101 	{
       
   102 	User::LeaveIfError(iStatus.Int()); //This will be traped by RunError().
       
   103 
       
   104 	DoRunL();
       
   105 
       
   106 	if (!IsActive())
       
   107 		Complete(iStatus.Int());
       
   108 	}
       
   109 
       
   110 TInt CSmssActive::Complete(TInt aStatus)
       
   111 	{
       
   112 	if (iReport)
       
   113 		{
       
   114 		const TBool isCancelling = (aStatus == KErrCancel);
       
   115 
       
   116 		DoComplete(aStatus);
       
   117 
       
   118 		if (!IsActive() || isCancelling)
       
   119 			{
       
   120 			User::RequestComplete(iReport, aStatus);
       
   121 			iReport = NULL;
       
   122 			}
       
   123 		}
       
   124 
       
   125 	return aStatus;
       
   126 	}
       
   127 
       
   128 TBool CSmssActive::CanSendMessage(const TMsvEntry& aEntry) const
       
   129 	{
       
   130 	TBool retVal = ETrue;
       
   131 	switch (aEntry.SendingState())
       
   132 		{
       
   133 		case KMsvSendStateSuspended:
       
   134 		case KMsvSendStateSent:
       
   135 		case KMsvSendStateNotApplicable:
       
   136 			retVal = EFalse;
       
   137 			break;
       
   138 		default:
       
   139 			break;
       
   140 		}
       
   141 
       
   142 	return retVal;
       
   143 	}
       
   144 
       
   145 TBool CSmssActive::CanSendMessageToRecipient(const TMsvEntry& aEntry, const CSmsNumber& aRcpt) const
       
   146 	{
       
   147 	return (aRcpt.Status() != CMsvRecipient::ESentSuccessfully) && CanSendMessage(aEntry);
       
   148 	}
       
   149 
       
   150 #ifndef _MSG_NO_LOGGING
       
   151 TInt CSmssActive::FLogMessage(const TMsvEntry& aEntry, const CSmsMessage& aSmsMessage, TBioMsgIdType aBearer, const TDesC& aFile)
       
   152 	{
       
   153 	TRAPD(err, DoFLogMessageL(aEntry, aSmsMessage, aBearer, aFile));
       
   154 	return err;
       
   155 	}
       
   156 
       
   157 void CSmssActive::DoFLogMessageL(const TMsvEntry& aEntry, const CSmsMessage& aSmsMessage, TBioMsgIdType aBearer, const TDesC& aFile)
       
   158 	{
       
   159 	if (!IsLogging(aFile))
       
   160 		return;
       
   161 	
       
   162 	TBuf8<32> temp;
       
   163 	TBuf16<32> temp1;
       
   164 	TPtrC temp2;
       
   165 	
       
   166 	switch (aSmsMessage.Type())
       
   167 		{
       
   168 		case CSmsPDU::ESmsSubmit:
       
   169 			temp = _L8("[Submit%d]");
       
   170 			break;
       
   171 		case CSmsPDU::ESmsDeliver:
       
   172 			temp = _L8("[Deliver%d]");
       
   173 			break;
       
   174 		default:
       
   175 			temp = _L8("[Message%d]");
       
   176 			break;
       
   177 		}
       
   178 
       
   179 	FLogFormat(aFile, temp, aEntry.Id());
       
   180 
       
   181 	TInt length = aSmsMessage.Buffer().Length();
       
   182 	HBufC* hBuf = HBufC::NewLC(32 + length);
       
   183 	TPtr buf(hBuf->Des());
       
   184 	aSmsMessage.Buffer().Extract(buf, 0, length);
       
   185 	buf.Insert(0, _L("Message= "));
       
   186 	FLog(aFile, buf, EFalse);
       
   187 	CleanupStack::PopAndDestroy(hBuf);
       
   188 	hBuf = NULL;
       
   189 
       
   190 	temp2.Set(aSmsMessage.ToFromAddress());
       
   191 	FLogFormat(aFile, _L("Recipients= %S"), &temp2);
       
   192 
       
   193 	temp2.Set(aSmsMessage.ServiceCenterAddress());
       
   194 	FLogFormat(aFile, _L("SC= %S"), &temp2);
       
   195 
       
   196 	FLogFormat(aFile, _L("BioUid= %d"), aEntry.iBioType);
       
   197 
       
   198 	const CSmsPDU& pdu = aSmsMessage.SmsPDU();
       
   199 
       
   200 	if (pdu.DataCodingSchemePresent())
       
   201 		{
       
   202 		temp.Zero();
       
   203 		temp.Append(_L8("Encoding= "));
       
   204 
       
   205 		switch (pdu.Alphabet())
       
   206 			{
       
   207 			case TSmsDataCodingScheme::ESmsAlphabet7Bit:
       
   208 				temp.Append(_L8("7"));
       
   209 				break;
       
   210 			case TSmsDataCodingScheme::ESmsAlphabet8Bit:
       
   211 				temp.Append(_L8("8"));
       
   212 				break;
       
   213 			case TSmsDataCodingScheme::ESmsAlphabetUCS2:
       
   214 				temp.Append(_L8("16"));
       
   215 				break;
       
   216 			default:
       
   217 				temp.Append(_L8("Unsupported"));
       
   218 				break;
       
   219 			}
       
   220 
       
   221 		FLogFormat(aFile, temp);
       
   222 		}
       
   223 
       
   224 	if (aSmsMessage.Type() == CSmsPDU::ESmsSubmit)
       
   225 		{
       
   226 		temp.Zero();
       
   227 		temp.Append(_L8("DeliveryReport= "));
       
   228 
       
   229 		const CSmsSubmit& submit = (CSmsSubmit&) pdu;
       
   230 		if (submit.StatusReportRequest())
       
   231 			{
       
   232 			temp.Append(_L8("YES"));
       
   233 			}
       
   234 		else
       
   235 			{
       
   236 			temp.Append(_L8("NO"));
       
   237 			}
       
   238 
       
   239 		FLogFormat(aFile, temp);
       
   240 		}
       
   241 	
       
   242 	temp.Zero();
       
   243 	temp.Append(_L("Bearer= "));
       
   244 	
       
   245 	switch (aBearer)
       
   246 		{
       
   247 		case EBioMsgIdNbs:
       
   248 			temp.Append(_L("NBS"));
       
   249 			break;
       
   250 		case EBioMsgIdWap:
       
   251 			temp.Append(_L("WAP"));
       
   252 			break;
       
   253 		case EBioMsgIdWapSecure:
       
   254 			temp.Append(_L("WAPSE"));
       
   255 			break;
       
   256 		default:
       
   257 			temp.Append(_L("UnSup"));
       
   258 			break;
       
   259 		}
       
   260 
       
   261 	FLogFormat(aFile, temp);
       
   262 	
       
   263 	temp1.Zero();
       
   264 	temp1.Append(_L("Storage= "));
       
   265 
       
   266 	switch (aSmsMessage.Storage())
       
   267 		{
       
   268 		case CSmsMessage::ESmsSIMStorage:
       
   269 			temp1.Append(KETelIccSmsStore);
       
   270 			break;
       
   271 		case CSmsMessage::ESmsPhoneStorage:
       
   272 			temp1.Append(KETelMeSmsStore);
       
   273 			break;
       
   274 		case CSmsMessage::ESmsCombinedStorage:
       
   275 			temp1.Append(KETelCombinedSmsStore);
       
   276 			break;
       
   277 		case CSmsMessage::ESmsNoStorage:
       
   278 		default:
       
   279 			temp1.Append(_L("NONE"));
       
   280 		};
       
   281 
       
   282 	FLogFormat(aFile, temp1);
       
   283 	FLogFormat(aFile, KNullDesC);
       
   284 	}
       
   285 
       
   286 void CSmssActive::FLog(const TDesC& aFile, const TDesC& aInputLine, TBool aIgnoreLineBreaks)
       
   287 	{
       
   288 	if (!IsLogging(aFile))
       
   289 		return;
       
   290 
       
   291 	TPtrC str(aInputLine);
       
   292 	TInt length = str.Length();
       
   293 
       
   294 	if (aIgnoreLineBreaks)
       
   295 		{
       
   296 		//Ignore trailing spaces
       
   297 		while (length--)
       
   298 			{
       
   299 			const TChar ch(str[length]);
       
   300 			if (!ch.IsSpace())
       
   301 				{
       
   302 				break;
       
   303 				}
       
   304 			}
       
   305 
       
   306 		length++;
       
   307 		str.Set(aInputLine.Left(length));
       
   308 
       
   309 		//Break the message up and print only 255 chars at a time
       
   310 		TInt start = 0;
       
   311 
       
   312 		while (start < length)
       
   313 			{
       
   314 			TPtrC buf(str.Mid(start, Min(KSmssLogMaxLength, length - start)));
       
   315 			iFlogger.Write(buf);
       
   316 			start += KSmssLogMaxLength;
       
   317 			}
       
   318 		}
       
   319 	else
       
   320 		{
       
   321 		//Log each line in aInputLine individually
       
   322 		while (length > 0)
       
   323 			{
       
   324 			TInt find = str.Locate('\n');
       
   325 
       
   326 			if (find == KErrNotFound)
       
   327 				{
       
   328 				FLog(aFile, str, ETrue);
       
   329 				break;
       
   330 				}
       
   331 			else
       
   332 				{
       
   333 				if (find == 0)
       
   334 					FLog(aFile, KNullDesC, ETrue);
       
   335 				else
       
   336 					FLog(aFile, str.Left(find), ETrue);
       
   337 
       
   338 				if (find < length - 1)
       
   339 					str.Set(str.Mid(find + 1));
       
   340 				else
       
   341 					break;
       
   342 				}
       
   343 
       
   344 			length = str.Length();
       
   345 			}
       
   346 		}
       
   347 	}
       
   348 
       
   349 void CSmssActive::FLog(const TDesC& aInputLine, TBool aIgnoreLineBreaks)
       
   350 	{
       
   351 	FLog(KSmssLogFile, aInputLine, aIgnoreLineBreaks);
       
   352 	}
       
   353 
       
   354 void CSmssActive::FLogFormat(const TDesC& aFile, TRefByValue<const TDesC> aFormat, ...)
       
   355 	{
       
   356 	VA_LIST list;
       
   357 	VA_START(list, aFormat);
       
   358 	FLogFormat(aFile, aFormat, list);
       
   359 	}
       
   360 
       
   361 void CSmssActive::FLogFormat(const TDesC& aFile, TRefByValue<const TDesC> aFormat, VA_LIST& aList)
       
   362 	{
       
   363 	if (!IsLogging(aFile))
       
   364 		return;
       
   365 
       
   366 	iFlogger.WriteFormat(aFormat, aList);
       
   367 	}
       
   368 
       
   369 
       
   370 void CSmssActive::FLogFormat(TRefByValue<const TDesC> aFormat, ...)
       
   371 	{
       
   372 	VA_LIST list;
       
   373 	VA_START(list, aFormat);
       
   374 	FLogFormat(KSmssLogFile, aFormat, list);
       
   375 	}
       
   376 
       
   377 void CSmssActive::FLogFormat(const TDesC& aFile, TRefByValue<const TDesC8> aFormat, ...)
       
   378 	{
       
   379 	VA_LIST list;
       
   380 	VA_START(list, aFormat);
       
   381 	FLogFormat(aFile, aFormat, list);
       
   382 	}
       
   383 
       
   384 void CSmssActive::FLogFormat(const TDesC& aFile, TRefByValue<const TDesC8> aFormat, VA_LIST& aList)
       
   385 	{
       
   386 	if (!IsLogging(aFile))
       
   387 		return;
       
   388 	
       
   389 	iFlogger.WriteFormat(aFormat, aList);
       
   390 	}
       
   391 
       
   392 
       
   393 void CSmssActive::FLogFormat(TRefByValue<const TDesC8> aFormat, ...)
       
   394 	{
       
   395 	VA_LIST list;
       
   396 	VA_START(list, aFormat);
       
   397 	FLogFormat(KSmssLogFile, aFormat, list);
       
   398 	}
       
   399 
       
   400 TBool CSmssActive::IsLogging(const TDesC& aFile)
       
   401 	{
       
   402 	return (ConstructFlogger(aFile) == KErrNone);
       
   403 	}
       
   404 
       
   405 TInt CSmssActive::ConstructFlogger(const TDesC& aFile)
       
   406 	{
       
   407 	TRAPD(err, DoConstructFloggerL(aFile));
       
   408 	return err;
       
   409 	}
       
   410 
       
   411 void CSmssActive::DoConstructFloggerL(const TDesC& aFile)
       
   412 	{
       
   413 	if (iFlogger.Handle() == NULL)
       
   414 		User::LeaveIfError(iFlogger.Connect());
       
   415 
       
   416 	if (iLastLogFile == NULL || *iLastLogFile != aFile)
       
   417 		{
       
   418 		if (iFlogger.LogValid())
       
   419 			iFlogger.CloseLog();
       
   420 
       
   421 		delete iLastLogFile;
       
   422 		iLastLogFile = NULL;
       
   423 		iLastLogFile = aFile.AllocL();
       
   424 		iFlogger.CreateLog(KSmssLogDir, aFile, EFileLoggingModeAppend);
       
   425 		}
       
   426 
       
   427 	if (!iFlogger.LogValid())
       
   428 		User::Leave(KErrNotFound);
       
   429 	}
       
   430 #endif
       
   431 
       
   432 
       
   433 void CSmssActive::ChangeEntryL(const TMsvEntry& aNewEntry)
       
   434  	{
       
   435  	__ASSERT_DEBUG(iServerEntry.Entry().Id() == aNewEntry.Id(), Panic(ESmssEntryNotSet));
       
   436  
       
   437  	if (!(aNewEntry == iServerEntry.Entry()))
       
   438  		User::LeaveIfError(iServerEntry.ChangeEntry(aNewEntry));
       
   439  	}
       
   440  
       
   441 void CSmssActive::StoreHeaderL(const CSmsHeader& aNewHeader)
       
   442 	{
       
   443  	CMsvStore* msvStore = iServerEntry.EditStoreL();
       
   444  	CleanupStack::PushL(msvStore);
       
   445  	aNewHeader.StoreL(*msvStore);
       
   446  	msvStore->CommitL();
       
   447  	CleanupStack::PopAndDestroy(msvStore);
       
   448  	}