DB robustness improved also for ShowEngine
authorteknolog
Sun, 04 Apr 2010 18:06:31 +0100
changeset 111 12c59f14a031
parent 110 403412eb5292
child 112 0bd6b9a3f027
DB robustness improved also for ShowEngine
application/src/PodcastQueueView.cpp
engine/inc/ShowEngine.h
engine/src/FeedEngine.cpp
engine/src/ShowEngine.cpp
--- a/application/src/PodcastQueueView.cpp	Sun Apr 04 16:23:10 2010 +0100
+++ b/application/src/PodcastQueueView.cpp	Sun Apr 04 18:06:31 2010 +0100
@@ -522,16 +522,18 @@
 			TInt index = iListContainer->Listbox()->CurrentItemIndex();
 			if (index >= 0 && index < iPodcastModel.ActiveShowList().Count())
 				{
-				if (iPodcastModel.ShowEngine().RemoveDownloadL(iPodcastModel.ActiveShowList()[index]->Uid()))
+				TRAPD(err, iPodcastModel.ShowEngine().RemoveDownloadL(iPodcastModel.ActiveShowList()[index]->Uid()));
+				
+				if (err == KErrNone)
 					{
-						iItemArray->Delete(index);
-						iItemIdArray.Remove(index);						
-						iListContainer->Listbox()->HandleItemRemovalL();
-						iListContainer->Listbox()->SetCurrentItemIndex(index - 1 > 0 ? index - 1 : 0);
-						iListContainer->Listbox()->DrawNow();
-						
-						delete iPodcastModel.ActiveShowList()[index];
-						iPodcastModel.ActiveShowList().Remove(index);
+					iItemArray->Delete(index);
+					iItemIdArray.Remove(index);						
+					iListContainer->Listbox()->HandleItemRemovalL();
+					iListContainer->Listbox()->SetCurrentItemIndex(index - 1 > 0 ? index - 1 : 0);
+					iListContainer->Listbox()->DrawNow();
+					
+					delete iPodcastModel.ActiveShowList()[index];
+					iPodcastModel.ActiveShowList().Remove(index);
 					}
 				}
 			}
--- a/engine/inc/ShowEngine.h	Sun Apr 04 16:23:10 2010 +0100
+++ b/engine/inc/ShowEngine.h	Sun Apr 04 18:06:31 2010 +0100
@@ -37,7 +37,7 @@
 	
 public:
 	IMPORT_C void AddDownloadL(CShowInfo& info);
-	IMPORT_C TBool RemoveDownloadL(TUint aUid);
+	IMPORT_C void RemoveDownloadL(TUint aUid);
 	IMPORT_C void RemoveAllDownloadsL();
 
 	IMPORT_C void SuspendDownloads();
@@ -56,11 +56,11 @@
 	IMPORT_C void GetShowsDownloadingL(RShowInfoArray &aArray);
 	IMPORT_C CShowInfo* DBGetShowByFileNameL(TFileName aFileName);
 	
-	IMPORT_C TBool AddShowL(const CShowInfo& item);
+	IMPORT_C void AddShowL(const CShowInfo& item);
 	IMPORT_C void DeletePlayedShowsL(RShowInfoArray &aShowInfoArray);
 	IMPORT_C void DeleteAllShowsByFeedL(TUint aFeedUid,TBool aDeleteFiles=ETrue);
 	IMPORT_C void DeleteShowL(TUint aShowUid, TBool aRemoveFile=ETrue);
-	IMPORT_C void DeleteOldShowsByFeed(TUint aFeedUid);
+	IMPORT_C void DeleteOldShowsByFeedL(TUint aFeedUid);
 	
 	IMPORT_C void AddObserver(MShowEngineObserver *observer);
 	IMPORT_C void RemoveObserver(MShowEngineObserver *observer);
@@ -88,7 +88,7 @@
 	CShowEngine(CPodcastModel& aPodcastModel);
 	void ConstructL();
 
-	TBool GetShowL(CShowInfo *info);
+	void GetShowL(CShowInfo *info);
 
 	void NotifyDownloadQueueUpdatedL();
 	void NotifyShowDownloadUpdatedL(TInt aBytesOfCurrentDownload, TInt aBytesTotal);
@@ -106,20 +106,20 @@
 	// DB methods
 	CShowInfo* DBGetShowByUidL(TUint aUid);
 	void DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo);
-	TBool DBAddShowL(const CShowInfo& aItem);
-	TBool DBUpdateShowL(CShowInfo& aItem);
+	void DBAddShowL(const CShowInfo& aItem);
+	void DBUpdateShowL(CShowInfo& aItem);
 	void DBGetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid);
 	void DBGetAllShowsL(RShowInfoArray& aShowArray);
 	void DBGetNewShowsL(RShowInfoArray& aShowArray);
 	void DBGetDownloadedShowsL(RShowInfoArray& aShowArray);
-	TBool DBDeleteAllShowsByFeed(TUint aFeedUid);
-	void DBDeleteOldShowsByFeed(TUint aFeedUid);
-	TBool DBDeleteShow(TUint aUid);
-	void DBRemoveAllDownloads();
-	void DBRemoveDownload(TUint aUid);
+	void DBDeleteAllShowsByFeedL(TUint aFeedUid);
+	void DBDeleteOldShowsByFeedL(TUint aFeedUid);
+	void DBDeleteShowL(TUint aUid);
+	void DBRemoveAllDownloadsL();
+	void DBRemoveDownloadL(TUint aUid);
 	void DBGetAllDownloadsL(RShowInfoArray& aShowArray);
-	TUint DBGetDownloadsCount();
-	void DBAddDownload(TUint aUid);
+	TUint DBGetDownloadsCountL();
+	void DBAddDownloadL(TUint aUid);
 	CShowInfo* DBGetNextDownloadL();
 	
 private:
--- a/engine/src/FeedEngine.cpp	Sun Apr 04 16:23:10 2010 +0100
+++ b/engine/src/FeedEngine.cpp	Sun Apr 04 18:06:31 2010 +0100
@@ -278,9 +278,10 @@
 		}
 	}
 	
-	TBool isShowAdded = iPodcastModel.ShowEngine().AddShowL(aItem);
+	TRAPD(err, iPodcastModel.ShowEngine().AddShowL(aItem));
 
-	if (aItem.PlayState() == ENeverPlayed && isShowAdded && iPodcastModel.SettingsEngine().DownloadAutomatically()) 
+	if (err == KErrNone && aItem.PlayState() == ENeverPlayed && 
+			iPodcastModel.SettingsEngine().DownloadAutomatically()) 
 		{
 		iPodcastModel.ShowEngine().AddDownloadL(aItem);
 		}	
@@ -576,7 +577,7 @@
 							}
 						else
 							{
-							iPodcastModel.ShowEngine().DeleteOldShowsByFeed(iActiveFeed->Uid());
+							iPodcastModel.ShowEngine().DeleteOldShowsByFeedL(iActiveFeed->Uid());
 							}
 	
 						// delete the downloaded XML file as it is no longer needed
--- a/engine/src/ShowEngine.cpp	Sun Apr 04 16:23:10 2010 +0100
+++ b/engine/src/ShowEngine.cpp	Sun Apr 04 18:06:31 2010 +0100
@@ -112,15 +112,14 @@
 		SuspendDownloads();
 		}
 
-	DBRemoveAllDownloads();
+	DBRemoveAllDownloadsL();
 	NotifyDownloadQueueUpdatedL();
 	}
 
-EXPORT_C TBool CShowEngine::RemoveDownloadL(TUint aUid)
+EXPORT_C void CShowEngine::RemoveDownloadL(TUint aUid)
 	{
-	DP("CShowEngine::RemoveDownload\t Trying to remove download");
+	DP("CShowEngine::RemoveDownloadL BEGIN");
 
-	TBool retVal = EFalse;
 	TBool resumeAfterRemove = EFalse;
 	// if trying to remove the present download, we first stop it
 	if (!iPodcastModel.SettingsEngine().DownloadSuspended() && iShowDownloading != NULL
@@ -138,7 +137,8 @@
 		DBUpdateShowL(*info);
 		delete info;
 		}
-	DBRemoveDownload(aUid);
+	
+	DBRemoveDownloadL(aUid);
 
 	// partial downloads should be removed
 	if (iShowDownloading)
@@ -154,9 +154,7 @@
 	}
 	
 	DownloadNextShowL();
-	retVal = ETrue;
-
-	return retVal;
+	DP("CShowEngine::RemoveDownloadL END");
 	}
 
 void CShowEngine::Connected(CHttpClient* /*aClient*/)
@@ -185,16 +183,11 @@
 		}
 	}
 
-TBool CShowEngine::GetShowL(CShowInfo *info)
+void CShowEngine::GetShowL(CShowInfo *info)
 	{
 	CFeedInfo *feedInfo = iPodcastModel.FeedEngine().GetFeedInfoByUid(
 			info->FeedUid());
-	if (feedInfo == NULL)
-		{
-		DP("Feed not found for this show!");
-		return EFalse;
-		}
-
+	
 	TFileName filePath;
 	filePath.Copy(iPodcastModel.SettingsEngine().BaseDir());
 	
@@ -211,10 +204,10 @@
 	filePath.Append(relPath);
 	info->SetFileNameL(filePath);
 
-	return iShowClient->GetL(info->Url(), filePath);
+	User::LeaveIfError(iShowClient->GetL(info->Url(), filePath));
 	}
 
-EXPORT_C TBool CShowEngine::AddShowL(const CShowInfo& aItem)
+EXPORT_C void CShowEngine::AddShowL(const CShowInfo& aItem)
 	{
 	DP1("CShowEngine::AddShowL, title=%S", &aItem.Title());
 	CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid());
@@ -222,12 +215,11 @@
 	if (showInfo == NULL)
 		{
 		DBAddShowL(aItem);
-		return ETrue;
 		}
 	else
 		{
 		delete showInfo;	
-		return EFalse;
+		User::Leave(KErrAlreadyExists);
 		}	
 	}
 
@@ -290,7 +282,7 @@
 
 				iShowDownloading->SetDownloadState(EDownloaded);
 				DBUpdateShowL(*iShowDownloading);
-				DBRemoveDownload(iShowDownloading->Uid());
+				DBRemoveDownloadL(iShowDownloading->Uid());
 				AddShowToMpxCollection(*iShowDownloading);				
 				NotifyShowFinishedL(aError);
 				iDownloadErrors = 0;
@@ -304,7 +296,7 @@
 					{
 					iShowDownloading->SetDownloadState(EFailedDownload);
 					DBUpdateShowL(*iShowDownloading);
-					DBRemoveDownload(iShowDownloading->Uid());
+					DBRemoveDownloadL(iShowDownloading->Uid());
 					NotifyShowFinishedL(aError);
 
 					delete iShowDownloading;
@@ -465,6 +457,10 @@
 			}
 		CleanupStack::PopAndDestroy();//st
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 
 	// delete downloads that don't have a show
 	
@@ -475,8 +471,13 @@
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
-		sqlite3_finalize(st);
+		CleanupStack::PopAndDestroy(); // st
+		}
+	else
+		{
+		User::Leave(KErrCorrupt);
 		}
 	}
 
@@ -510,8 +511,16 @@
 			DBFillShowInfoFromStmtL(st, showInfo);
 			CleanupStack::Pop(showInfo);
 			}
+		else
+			{
+			User::Leave(KErrUnknown);
+			}
 		CleanupStack::PopAndDestroy();//st
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 
 	return showInfo;
 	}
@@ -546,10 +555,14 @@
 			}
 		CleanupStack::PopAndDestroy();//st
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 	DP("CShowEngine::DBGetShowsByFeed END");
 	}
 
-TUint CShowEngine::DBGetDownloadsCount()
+TUint CShowEngine::DBGetDownloadsCountL()
 	{
 	DP("CShowEngine::DBGetDownloadsCount");
 
@@ -564,13 +577,23 @@
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
 
 		if (rc == SQLITE_ROW)
 			{
 			count = sqlite3_column_int(st, 0);
 			}
-		sqlite3_finalize(st);
+		else
+			{
+			User::Leave(KErrUnknown);
+			}
+		
+		CleanupStack::PopAndDestroy(); //st
+		}
+	else
+		{
+		User::Leave(KErrCorrupt);
 		}
 	return count;
 	}
@@ -605,6 +628,10 @@
 			}
 		CleanupStack::PopAndDestroy();//st
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 	}
 
 void CShowEngine::DBGetNewShowsL(RShowInfoArray& aShowArray)
@@ -632,9 +659,13 @@
 			}
 		CleanupStack::PopAndDestroy();//st
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 	}
 
-void CShowEngine::DBDeleteOldShowsByFeed(TUint aFeedUid)
+void CShowEngine::DBDeleteOldShowsByFeedL(TUint aFeedUid)
 	{
 	DP("CShowEngine::DBDeleteOldShows");
 	
@@ -657,6 +688,10 @@
 		rc = sqlite3_step(st);
 		sqlite3_finalize(st);
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 	
 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
 	iSqlBuffer.Format(KSqlStatement2);
@@ -668,6 +703,10 @@
 		rc = sqlite3_step(st);
 		sqlite3_finalize(st);
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 	}
 
 void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo)
@@ -724,7 +763,7 @@
 	showInfo->SetLastError(lasterror);
 	}
 
-TBool CShowEngine::DBAddShowL(const CShowInfo& aItem)
+void CShowEngine::DBAddShowL(const CShowInfo& aItem)
 	{
 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
 
@@ -754,28 +793,24 @@
 
 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
 			&st, (const void**) NULL);
+	
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
-		if (rc == SQLITE_DONE)
+		if (rc != SQLITE_DONE)
 			{
-			sqlite3_finalize(st);
-			return ETrue;
+			User::Leave(KErrAlreadyExists);
 			}
-		else
-			{
-			sqlite3_finalize(st);
-			}
+		CleanupStack::PopAndDestroy(); // st
 		}
 	else
 		{
-		DP1("SQLite rc=%d", rc);
+		User::Leave(KErrCorrupt);
 		}
-
-	return EFalse;
 	}
 
-void CShowEngine::DBAddDownload(TUint aUid)
+void CShowEngine::DBAddDownloadL(TUint aUid)
 	{
 	DP1("CShowEngine::DBAddDownload, aUid=%u", aUid);
 
@@ -788,13 +823,21 @@
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
+		if (rc =! SQLITE_DONE)
+			{
+			User::Leave(KErrUnknown);
+			}
+		CleanupStack::PopAndDestroy(); // st
 		}
-
-	sqlite3_finalize(st);
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 	}
 
-TBool CShowEngine::DBUpdateShowL(CShowInfo& aItem)
+void CShowEngine::DBUpdateShowL(CShowInfo& aItem)
 	{
 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
 
@@ -827,27 +870,22 @@
 
 	if (rc == SQLITE_OK)
 		{
-		rc = sqlite3_step(st);
+		Cleanup_sqlite3_finalize_PushL(st);
+		rc = sqlite3_step(st);		
 
-		if (rc == SQLITE_DONE)
+		if (rc =! SQLITE_DONE)
 			{
-			sqlite3_finalize(st);
-			return ETrue;
+			User::Leave(KErrUnknown);
 			}
-		else
-			{
-			sqlite3_finalize(st);
-			}
+		CleanupStack::PopAndDestroy(); // st
 		}
 	else
 		{
-		DP1("SQLite rc=%d", rc);
+		User::Leave(KErrCorrupt);
 		}
-
-	return EFalse;
 	}
 
-TBool CShowEngine::DBDeleteShow(TUint aUid)
+void CShowEngine::DBDeleteShowL(TUint aUid)
 	{
 	DP("CShowEngine::DBDeleteShow");
 
@@ -856,33 +894,27 @@
 
 	sqlite3_stmt *st;
 
-	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
 			&st, (const void**) NULL);
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
 
-		if (rc == SQLITE_DONE)
+		if (rc =! SQLITE_DONE)
 			{
-			sqlite3_finalize(st);
-			return ETrue;
+			User::Leave(KErrUnknown);
 			}
-		else
-			{
-			sqlite3_finalize(st);
-			}
+		CleanupStack::PopAndDestroy(); // st
 		}
 	else
 		{
-		DP1("SQLite rc=%d", rc);
+		User::Leave(KErrCorrupt);
 		}
-
-	return EFalse;
 	}
 
-TBool CShowEngine::DBDeleteAllShowsByFeed(TUint aFeedUid)
+void CShowEngine::DBDeleteAllShowsByFeedL(TUint aFeedUid)
 	{
 	DP("CShowEngine::DBDeleteAllShowsByFeed");
 
@@ -891,33 +923,27 @@
 
 	sqlite3_stmt *st;
 
-	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
 			&st, (const void**) NULL);
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
 
-		if (rc == SQLITE_DONE)
+		if (rc =! SQLITE_DONE)
 			{
-			sqlite3_finalize(st);
-			return ETrue;
+			User::Leave(KErrUnknown);
 			}
-		else
-			{
-			sqlite3_finalize(st);
-			}
+		CleanupStack::PopAndDestroy(); // st
 		}
 	else
 		{
-		DP1("SQLite rc=%d", rc);
+		User::Leave(KErrCorrupt);
 		}
-
-	return EFalse;
 	}
 
-void CShowEngine::DBRemoveAllDownloads()
+void CShowEngine::DBRemoveAllDownloadsL()
 	{
 	DP("CShowEngine::DBRemoveAllDownloads");
 
@@ -934,6 +960,10 @@
 		rc = sqlite3_step(st);
 		sqlite3_finalize(st);
 		}
+	else
+		{
+		User::Leave(KErrCorrupt);
+		}
 
 	_LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1");
 	iSqlBuffer.Format(KSqlStatement2);
@@ -943,13 +973,22 @@
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
-		sqlite3_finalize(st);
+		if (rc =! SQLITE_DONE)
+			{
+			User::Leave(KErrUnknown);
+			}
+		CleanupStack::PopAndDestroy(); // st
+		}
+	else
+		{
+		User::Leave(KErrCorrupt);
 		}
 
 	}
 
-void CShowEngine::DBRemoveDownload(TUint aUid)
+void CShowEngine::DBRemoveDownloadL(TUint aUid)
 	{
 	DP("CShowEngine::DBRemoveDownload");
 
@@ -963,8 +1002,17 @@
 
 	if (rc == SQLITE_OK)
 		{
+		Cleanup_sqlite3_finalize_PushL(st);
 		rc = sqlite3_step(st);
-		sqlite3_finalize(st);
+		if (rc =! SQLITE_DONE)
+			{
+			User::Leave(KErrUnknown);
+			}
+		CleanupStack::PopAndDestroy(); // st
+		}
+	else
+		{
+		User::Leave(KErrCorrupt);
 		}
 	}
 
@@ -1089,7 +1137,7 @@
 	array.ResetAndDestroy();
 	
 	// delete all shows from DB
-	DBDeleteAllShowsByFeed(aFeedUid);
+	DBDeleteAllShowsByFeedL(aFeedUid);
 
 	// this will clear out deleted shows from the download queue
 	DBGetAllDownloadsL(array);
@@ -1098,9 +1146,9 @@
 	NotifyDownloadQueueUpdatedL();
 	}
 
-EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid)
+EXPORT_C void CShowEngine::DeleteOldShowsByFeedL(TUint aFeedUid)
 	{
-		DBDeleteOldShowsByFeed(aFeedUid);
+	DBDeleteOldShowsByFeedL(aFeedUid);
 	}
 
 EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile)
@@ -1153,14 +1201,17 @@
 
 EXPORT_C TInt CShowEngine::GetNumDownloadingShows()
 	{
-	return (const TInt) DBGetDownloadsCount();
+	TUint count;
+	TRAP_IGNORE(count = DBGetDownloadsCountL());
+		
+	return (const TInt) count;
 	}
 
 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo)
 	{
 	aInfo.SetDownloadState(EQueued);
 	DBUpdateShowL(aInfo);
-	DBAddDownload(aInfo.Uid());
+	DBAddDownloadL(aInfo.Uid());
 	DownloadNextShowL();
 	}
 
@@ -1168,7 +1219,7 @@
 	{
 	DP("CShowEngine::DownloadNextShowL BEGIN");
 	// Check if we have anything in the download queue
-	const TInt count = DBGetDownloadsCount();
+	const TInt count = DBGetDownloadsCountL();
 	DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count);
 
 	if (count > 0)
@@ -1196,7 +1247,6 @@
 			
 			while(info != NULL)
 				{
-				TBool getOk = EFalse;
 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
 				info->SetDownloadState(EDownloading);
 				info->SetLastError(KErrNone);
@@ -1206,11 +1256,11 @@
 				// important to do this after we change download state
 				NotifyDownloadQueueUpdatedL();
 
-				TRAPD(error,getOk = GetShowL(info));
-				if (error != KErrNone || !getOk)
+				TRAPD(error,GetShowL(info));
+				if (error != KErrNone)
 					{
 					info->SetDownloadState(EFailedDownload);
-					DBRemoveDownload(info->Uid());
+					DBRemoveDownloadL(info->Uid());
 					DBUpdateShowL(*info);
 					info = DBGetNextDownloadL();
 					
@@ -1241,7 +1291,7 @@
 	const TInt count = iObservers.Count();
 	for (TInt i = 0; i < count; i++)
 		{
-		iObservers[i]->DownloadQueueUpdatedL(1, DBGetDownloadsCount() - 1);
+		iObservers[i]->DownloadQueueUpdatedL(1, DBGetDownloadsCountL() - 1);
 		}
 	}