messagingfw/biomsgfw/BioWatchers/Src/SmsWatcher.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2003-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 // SmsWatcher.CPP
       
    15 //
       
    16 
       
    17 #include <e32std.h>
       
    18 #include <biouids.h>
       
    19 #include <c32comm.h>
       
    20 #include <smut.h>
       
    21 
       
    22 #include "SmsWatcher.h"
       
    23 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS 
       
    24 #include "cwatcher.h"
       
    25 #endif
       
    26 
       
    27 const TInt KAllBioUids = 0;
       
    28 
       
    29 EXPORT_C CSmsBaseWatcher::CSmsBaseWatcher(RFs& aFs, CWatcherLog& aLog, TInt aPriority)
       
    30 : CActive(aPriority), iFs(aFs), iWatcherLog(aLog)
       
    31 	{
       
    32 	CActiveScheduler::Add(this);
       
    33 	}
       
    34 
       
    35 //
       
    36 // Construction/destruction
       
    37 
       
    38 EXPORT_C void CSmsBaseWatcher::ConstructL()
       
    39 	{
       
    40 	// The session to the message server is created in the ConstructL because at this 
       
    41 	// moment the message server still runs because of the open session in CWatcherLauncherArray
       
    42 	// If we would open the session in StartL, the message server will be opened and closed  
       
    43 	// several times, which has a negative effect on performance.
       
    44 	iSession = CMsvSession::OpenSyncL(*this);
       
    45 	
       
    46 	User::LeaveIfError(iTimer.CreateLocal());
       
    47 	ConstructDbL();
       
    48 	CompleteMyself(KErrNone);
       
    49 	}
       
    50 
       
    51 EXPORT_C void CSmsBaseWatcher::StartL()
       
    52 	{
       
    53 	// put the real construction here
       
    54 	iSocketWatchers = new (ELeave) CArrayPtrFlat<CBaseSmsActiveSocketWatcher>(10);
       
    55 
       
    56 	// Hook into the Bif Change Observer
       
    57 	iBifObserver = CBifChangeObserver::NewL(*this, iFs);
       
    58 	iBifObserver->Start();
       
    59 
       
    60 	}
       
    61 
       
    62 //
       
    63 // Construction/destruction
       
    64 EXPORT_C CSmsBaseWatcher::~CSmsBaseWatcher()
       
    65 	{
       
    66 	Cancel();
       
    67 	Reset();
       
    68 	ResetDb();
       
    69 	iTimer.Close();
       
    70 	
       
    71 	delete iSession;
       
    72 	}
       
    73 
       
    74 EXPORT_C void CSmsBaseWatcher::Reset()
       
    75 	{
       
    76 	__ASSERT_DEBUG(!IsActive(), PanicWatcher(ESocketWatcherAlreadyActive));
       
    77 
       
    78 	if (iSocketWatchers)
       
    79 		iSocketWatchers->ResetAndDestroy();
       
    80 
       
    81 	delete iSocketWatchers;
       
    82 	iSocketWatchers = NULL;
       
    83 
       
    84 	delete iBifObserver;
       
    85 	iBifObserver = NULL;
       
    86 
       
    87 	//Do not close iTimer. Leave that for the destructor.
       
    88 	}
       
    89 
       
    90 //
       
    91 EXPORT_C void CSmsBaseWatcher::HandleBifChangeL(TBifChangeEvent aEvent, TUid aBioID)
       
    92 	{
       
    93 	BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: Watcher HandleBifChangeL(Event: %d, BioUid: %d)"), aEvent, aBioID.iUid));
       
    94 	TInt err = KErrNone;
       
    95 
       
    96 	switch(aEvent)
       
    97 		{
       
    98 		case EBifAdded:
       
    99 			TRAP(err, AddBifL(aBioID));
       
   100 			break;
       
   101 		case EBifDeleted:
       
   102 			TRAP(err, DeleteBifL(aBioID));
       
   103 			break;
       
   104 		case EBifChanged:
       
   105 			TRAP(err, ReloadBifL(aBioID));
       
   106 			break;
       
   107 		default:
       
   108 			break;
       
   109 		} 
       
   110 
       
   111 	if (err)
       
   112 		{
       
   113 		BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: Watcher HandleBifChangeL(Event: %d, BioUid: %d) error: %d"), aEvent, aBioID.iUid, err));
       
   114 		}
       
   115 	}
       
   116 
       
   117 EXPORT_C void CSmsBaseWatcher::RunL()
       
   118 	{
       
   119 	BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: Attempting restart [iStatus=%d]"), iStatus.Int()));
       
   120 
       
   121 	//Restart the watcher
       
   122 	Reset();
       
   123 	StartL();
       
   124 	}
       
   125 
       
   126 EXPORT_C void CSmsBaseWatcher::DoCancel()
       
   127 	{
       
   128 	iTimer.Cancel();
       
   129 	}
       
   130 
       
   131 EXPORT_C void CSmsBaseWatcher::HandleWatcherComplete(CBaseSmsActiveSocketWatcher& aWatcher, TInt aError)
       
   132 	{
       
   133 	//Only try to re-start the watcher if it wasn't cancelled.
       
   134 	if (!iSocketWatchers || aError == KErrCancel)
       
   135 		return;
       
   136 
       
   137 	TInt count = iSocketWatchers->Count();
       
   138 	TInt err = KErrNone;
       
   139 	
       
   140 	while (count--)
       
   141 		{
       
   142 		CBaseSmsActiveSocketWatcher* watcher = iSocketWatchers->At(count);
       
   143 		
       
   144 		if (watcher == &aWatcher)
       
   145 			{
       
   146 			//Found the same watcher
       
   147 			TRAP(err, watcher->SetupL());
       
   148 				
       
   149 			if (!err)
       
   150 				{
       
   151 				TRAP(err, watcher->StartL());
       
   152 				}
       
   153 				
       
   154 			if (err)
       
   155 				{
       
   156 				iSocketWatchers->Delete(count);
       
   157 				delete watcher;
       
   158 				}
       
   159 			
       
   160 			break;
       
   161 			}
       
   162 		}
       
   163 	
       
   164 	if (err)
       
   165 		{
       
   166 		//This will call RunL, which will restart the watcher.
       
   167 		BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: Unable to restart watcher [err=%d, IsActive=%d]"), err, IsActive()));
       
   168 		Cancel();
       
   169 		iTimer.After(iStatus, KWatcherDelay);
       
   170 		SetActive();
       
   171 		}
       
   172 	}
       
   173 
       
   174 EXPORT_C void CSmsBaseWatcher::DeleteSocketWatchersWithUidL(TUid aUid)
       
   175 	{
       
   176 	TInt count = iSocketWatchers->Count();
       
   177 
       
   178 	while (count--)
       
   179 		{
       
   180 		CBaseSmsActiveSocketWatcher* socketWatcher = iSocketWatchers->At(count);
       
   181 		
       
   182 		if (socketWatcher->BioMsgUID() == aUid)
       
   183 			{
       
   184 			socketWatcher->Cancel();
       
   185 			delete socketWatcher;
       
   186 			iSocketWatchers->Delete(count);
       
   187 			}
       
   188 		}
       
   189 	}
       
   190 
       
   191 EXPORT_C void CSmsBaseWatcher::CreateSocketWatchersFromBioDbL(TBioMsgIdType aType)
       
   192 	{
       
   193 	CreateSocketWatchersFromBioDbL(aType, *iSocketWatchers);
       
   194 	}
       
   195 
       
   196 void CSmsBaseWatcher::CreateSocketWatchersFromBioDbL(TBioMsgIdType aType, CArrayPtrFlat<CBaseSmsActiveSocketWatcher>& rSocketWatchers)
       
   197 	{
       
   198 	TUid uid;
       
   199 	uid.iUid = KAllBioUids;
       
   200 	CreateSocketWatchersFromBioDbL(aType, uid, rSocketWatchers);
       
   201 	}
       
   202 
       
   203 EXPORT_C void CSmsBaseWatcher::CreateSocketWatchersFromBioDbL(TBioMsgIdType aType, TUid aBioID)
       
   204 	{
       
   205 	CreateSocketWatchersFromBioDbL(aType, aBioID, *iSocketWatchers);
       
   206 	}
       
   207 
       
   208 void CSmsBaseWatcher::CreateSocketWatchersFromBioDbL(TBioMsgIdType aType, TUid aUid, CArrayPtrFlat<CBaseSmsActiveSocketWatcher>& rSocketWatchers)
       
   209 	{
       
   210 	ConstructDbL();
       
   211 
       
   212 	TInt pos;
       
   213 	TUid bioMsgID(KNullUid);
       
   214 	CBaseSmsActiveSocketWatcher* socketWatcher = NULL;
       
   215 
       
   216 	const CArrayFix<TBioMsgId>* bioIDs = iBioDb->BioEntryByTypeLC(CBIODatabase::EStart, aType, pos);
       
   217 
       
   218 	if (bioIDs)
       
   219 		iBioDb->GetBioMsgID(pos, bioMsgID);
       
   220 
       
   221 	TInt bioDBCount = iBioDb->BIOCount();
       
   222 	while( bioIDs != 0 || pos < bioDBCount )
       
   223 		{
       
   224 		if (aUid.iUid == KAllBioUids || bioMsgID == aUid)
       
   225 			{
       
   226 			TInt index = 0;
       
   227 			if (bioIDs)
       
   228 				{
       
   229 				index = bioIDs->Count();
       
   230 				}
       
   231 
       
   232 			while (index--)
       
   233 				{
       
   234 				// Create the Socket Watcher and Add it to the list
       
   235 				TBioMsgId msgId = bioIDs->At(index);
       
   236 
       
   237 				if (msgId.iType == aType && SupportBioMsgId(msgId))
       
   238 					{
       
   239 					socketWatcher = CreateSocketWatcherLC(bioMsgID, msgId);
       
   240 					TInt error = SetupAndAppendL(*socketWatcher, rSocketWatchers);
       
   241 
       
   242 					if (error)
       
   243 						CleanupStack::PopAndDestroy();
       
   244 					else
       
   245 						CleanupStack::Pop();
       
   246 					}
       
   247 				}
       
   248 			}
       
   249 		
       
   250 		CleanupStack::PopAndDestroy();	// bioIDs
       
   251 		bioIDs = NULL;
       
   252 		
       
   253 		bioIDs = iBioDb->BioEntryByTypeLC(CBIODatabase::ENext, aType, pos);
       
   254 		if (bioIDs)
       
   255 			iBioDb->GetBioMsgID(pos, bioMsgID);
       
   256 		}
       
   257 	}
       
   258 
       
   259 EXPORT_C void CSmsBaseWatcher::StartSocketWatchersL()
       
   260 	{
       
   261 	StartSocketWatchersL(*iSocketWatchers);
       
   262 	}
       
   263 
       
   264 void CSmsBaseWatcher::StartSocketWatchersL(const CArrayPtrFlat<CBaseSmsActiveSocketWatcher>& aSocketWatchers)
       
   265 	{
       
   266 	TInt count = aSocketWatchers.Count();
       
   267 
       
   268 	//Order Important
       
   269 	for (TInt i = 0; i < count; i++)
       
   270 		{
       
   271 		CBaseSmsActiveSocketWatcher* socketWatcher = aSocketWatchers[i];
       
   272 		socketWatcher->SetObserver(this);
       
   273 		socketWatcher->StartL();
       
   274 		}
       
   275 	}
       
   276 
       
   277 EXPORT_C void CSmsBaseWatcher::DeleteBifL(TUid aBioID)
       
   278 	{
       
   279 	DeleteSocketWatchersWithUidL(aBioID);
       
   280 	}
       
   281 
       
   282 EXPORT_C void CSmsBaseWatcher::ReloadBifL(TUid aBioID)
       
   283 	{
       
   284 	DeleteBifL(aBioID);
       
   285 	AddBifL(aBioID);
       
   286 	}
       
   287 
       
   288 EXPORT_C void CSmsBaseWatcher::AddBifWithTypeL(TBioMsgIdType aType, TUid aBioID)
       
   289 	{
       
   290 	CArrayPtrFlat<CBaseSmsActiveSocketWatcher>* socketWatchers = new (ELeave) CArrayPtrFlat<CBaseSmsActiveSocketWatcher>(1);
       
   291 	CleanupStack::PushL(socketWatchers);
       
   292 
       
   293 	CreateSocketWatchersFromBioDbL(aType, aBioID, *socketWatchers);
       
   294 	StartSocketWatchersL(*socketWatchers);
       
   295 
       
   296 	TInt count = socketWatchers->Count();
       
   297 	while (count--)
       
   298 		{
       
   299 		iSocketWatchers->AppendL(socketWatchers->At(count));
       
   300 		}
       
   301 
       
   302 	CleanupStack::PopAndDestroy(); //socketWatchers
       
   303 	}
       
   304 
       
   305 EXPORT_C TInt CSmsBaseWatcher::SetupAndAppendL(CBaseSmsActiveSocketWatcher& aSocketWatcher, CArrayPtrFlat<CBaseSmsActiveSocketWatcher>& rSocketWatchers)
       
   306 	{
       
   307 	TRAPD(error, aSocketWatcher.SetupL());
       
   308 
       
   309 	if (!error)
       
   310 		{
       
   311 		rSocketWatchers.AppendL(&aSocketWatcher);
       
   312 		}
       
   313 	else 
       
   314 		{
       
   315 		BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: Watcher setup error - %d"), error));
       
   316 
       
   317 		if (error != KErrAlreadyExists && error != KErrNotFound)
       
   318 			User::Leave(error);
       
   319 		else
       
   320 			BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: Ignoring watcher error - Config problem?")));
       
   321 		}
       
   322 
       
   323 	return error;
       
   324 	}
       
   325 
       
   326 void CSmsBaseWatcher::ConstructDbL()
       
   327 	{
       
   328 	if (iBioDb == NULL)
       
   329 		iBioDb = CBIODatabase::NewL(iFs);
       
   330 	}
       
   331 
       
   332 EXPORT_C void CSmsBaseWatcher::ResetDb()
       
   333 	{
       
   334 	delete iBioDb;
       
   335 	iBioDb = NULL;
       
   336 	}
       
   337 
       
   338 EXPORT_C void CSmsBaseWatcher::CompleteMyself(TInt aError)
       
   339 	{
       
   340 	TRequestStatus* status = &iStatus;
       
   341 	User::RequestComplete(status, aError);
       
   342 	SetActive();
       
   343 	}
       
   344 
       
   345 EXPORT_C TInt CSmsBaseWatcher::RunError(TInt aError)
       
   346 	{
       
   347 	BIOWATCHERLOG(iWatcherLog.Printf(_L8("Bio: RunL left with %d. Restarting in %d sec"), aError, KWatcherDelay / 1000000));
       
   348 
       
   349 	iTimer.After(iStatus, KWatcherDelay);
       
   350 	SetActive();
       
   351 
       
   352 	return KErrNone;
       
   353 	}
       
   354 
       
   355 EXPORT_C void CSmsBaseWatcher::GetBioServiceId(CMsvSession& aSession, TMsvId& aBioServiceId, TMsvId& aSmsServiceId)
       
   356 	{
       
   357 	TInt err = KErrNone;
       
   358 	TRAP(err, TSmsUtilities::ServiceIdL(aSession, aBioServiceId, KUidBIOMessageTypeMtm));
       
   359 	if (err == KErrNotFound)
       
   360 		{
       
   361 		aBioServiceId = KMsvLocalServiceIndexEntryId;
       
   362 		}
       
   363 	TRAP(err, TSmsUtilities::ServiceIdL(aSession, aSmsServiceId));
       
   364 	if (err == KErrNotFound)
       
   365 		{
       
   366 		aSmsServiceId = KMsvLocalServiceIndexEntryId;
       
   367 		}
       
   368 	}
       
   369 
       
   370