engine/src/PodcastModel.cpp
changeset 60 4d230e702aa3
parent 32 26a3f2dfba08
child 65 bcd88ba95046
--- a/engine/src/PodcastModel.cpp	Thu Mar 11 20:53:00 2010 +0100
+++ b/engine/src/PodcastModel.cpp	Tue Apr 27 19:26:48 2010 +0100
@@ -19,10 +19,11 @@
 #include <commdb.h>
 #include "PodcastModel.h"
 #include "FeedEngine.h"
-#include "SoundEngine.h"
 #include "SettingsEngine.h"
 #include "ShowEngine.h"
 #include "connectionengine.h"
+#include "podcastutils.h"
+#include "Podcatcher.pan"
 
 #include <cmdestination.h>
 #include <cmmanager.h>
@@ -45,7 +46,6 @@
 {
 	
 	delete iFeedEngine;
-	delete iSoundEngine;
 	delete iSettingsEngine;
 	delete iShowEngine;
 
@@ -78,7 +78,7 @@
 	iIapNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
 	iSNAPNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
 	iCmManager.OpenL();
-	iImageHandler = CImageHandler::NewL(FsSession());
+	iImageHandler = CImageHandler::NewL(FsSession(), *this);
 	
 	UpdateIAPListL();
 	UpdateSNAPListL();
@@ -86,10 +86,24 @@
 	iSettingsEngine = CSettingsEngine::NewL(*this);
 	iConnectionEngine = CConnectionEngine::NewL(*this);	
 	
+	TRAPD(err, OpenDBL());
+	
+	if (err != KErrNone)
+		{
+		ResetDB();
+		
+		TRAP(err, OpenDBL());
+		
+		if (err != KErrNone)
+			{
+			Panic(EPodcatcherPanicDB);
+			}
+		
+		}
+	
 	iFeedEngine = CFeedEngine::NewL(*this);
 	iShowEngine = CShowEngine::NewL(*this);
 
-	iSoundEngine = CSoundEngine::NewL(*this);	
 	DP("CPodcastModel::ConstructL END");
 }
 
@@ -199,11 +213,6 @@
 	return *iShowEngine;
 }
 
-EXPORT_C CSoundEngine& CPodcastModel::SoundEngine()
-{
-	return *iSoundEngine;
-}
-
 EXPORT_C CSettingsEngine& CPodcastModel::SettingsEngine()
 {
 	return *iSettingsEngine;
@@ -216,40 +225,8 @@
 
 EXPORT_C void CPodcastModel::PlayPausePodcastL(CShowInfo* aPodcast, TBool aPlayOnInit) 
 	{
-	
-	// special treatment if this podcast is already active
-	if (iPlayingPodcast->Uid() == aPodcast->Uid() && SoundEngine().State() > ESoundEngineOpening ) {
-		if (aPodcast->PlayState() == EPlaying) {
-			SoundEngine().Pause();
-			aPodcast->SetPosition(iSoundEngine->Position());
-			aPodcast->SetPlayState(EPlayed);
-			aPodcast->SetPlayState(EPlayed);
-		} else {
-			iSoundEngine->Play();
-		}
-	} else {
-		// switching file, so save position
-		iSoundEngine->Pause();
-		if (iPlayingPodcast != NULL) {
-			iPlayingPodcast->SetPosition(iSoundEngine->Position());
-		}
-		
-		iSoundEngine->Stop(EFalse);
-
-		// we play video podcasts through the external player
-		if(aPodcast != NULL && aPodcast->ShowType() != EVideoPodcast) {
-			DP1("Starting: %S", &(aPodcast->FileName()));
-			TRAPD( error, iSoundEngine->OpenFileL(aPodcast->FileName(), aPlayOnInit) );
-			if (error != KErrNone) {
-				DP1("Error: %d", error);
-			} else {
-				iSoundEngine->SetPosition(aPodcast->Position().Int64() / 1000000);
-			}
-		}
-
-		iPlayingPodcast = aPodcast;		
+	// TODO: interact with MPX
 	}
-}
 
 EXPORT_C CFeedInfo* CPodcastModel::ActiveFeedInfo()
 {
@@ -277,33 +254,105 @@
 	}
 }
 
+EXPORT_C void CPodcastModel::DropDB()
+	{
+	if (iDB != NULL)
+		{
+		sqlite3_close(iDB);
+		iDB = NULL;
+		}
+	
+	TFileName dbFileName;
+	dbFileName.Copy(iSettingsEngine->PrivatePath());
+	dbFileName.Append(KDBFileName);
+
+	// remove the old DB file
+	if (BaflUtils::FileExists(iFsSession, dbFileName))
+		{
+		BaflUtils::DeleteFile(iFsSession, dbFileName);
+		}
+	}
+
+void CPodcastModel::ResetDB()
+	{
+	DP("CPodcastModel::ResetDB BEGIN");
+	
+	DropDB();
+	
+	TFileName dbFileName;
+	dbFileName.Copy(iSettingsEngine->PrivatePath());
+	dbFileName.Append(KDBFileName);
+
+	// remove the old DB file
+	if (BaflUtils::FileExists(iFsSession, dbFileName))
+		{
+		BaflUtils::DeleteFile(iFsSession, dbFileName);
+		}
+
+	// copy template to new DB
+	TFileName dbTemplate;
+	dbTemplate.Copy(iSettingsEngine->PrivatePath());
+	dbTemplate.Append(KDBTemplateFileName);
+
+	BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
+	iIsFirstStartup = ETrue;
+	DP("CPodcastModel::ResetDB END");
+	}
+
+
+void CPodcastModel::OpenDBL()
+	{
+	DP("CPodcastModel::OpenDBL BEGIN");
+	
+	if (iDB != NULL)
+		{
+		sqlite3_close(iDB);
+		iDB = NULL;
+		}
+	
+	TFileName dbFileName;
+	dbFileName.Copy(iSettingsEngine->PrivatePath());
+	dbFileName.Append(KDBFileName);
+		
+	if (!BaflUtils::FileExists(iFsSession, dbFileName))
+		{
+		User::Leave(KErrNotFound);
+		}
+	
+	if (iDB == NULL) {	
+		// open DB
+		TBuf8<KMaxFileName> filename8;
+		filename8.Copy(dbFileName);
+		int rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
+		if(rc != SQLITE_OK){
+			User::Leave(KErrCorrupt);
+		}
+
+		// do a test query 
+		sqlite3_stmt *st;
+		rc = sqlite3_prepare_v2(iDB,"select count(*) from feeds" , -1, &st, (const char**) NULL);
+		if( rc==SQLITE_OK )
+			{
+			Cleanup_sqlite3_finalize_PushL(st);
+			rc = sqlite3_step(st);
+					
+			if (rc != SQLITE_ROW)
+				{
+				User::Leave(KErrCorrupt);
+				}
+			CleanupStack::PopAndDestroy(); // st
+			}
+		else
+			{
+			User::Leave(KErrCorrupt);
+			}
+		}
+
+	DP("CPodcastModel::OpenDBL END");	
+	}
+
 sqlite3* CPodcastModel::DB()
 {
-	if (iDB == NULL) {		
-		TFileName dbFileName;
-		iFsSession.PrivatePath(dbFileName);
-		dbFileName.Append(KDBFileName);
-		DP1("DB is at %S", &dbFileName);
-
-		if (!BaflUtils::FileExists(iFsSession, dbFileName)) {
-			TFileName dbTemplate;
-			iFsSession.PrivatePath(dbTemplate);
-			dbTemplate.Append(KDBTemplateFileName);
-			DP1("No DB found, copying template from %S", &dbTemplate);
-			BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
-			iIsFirstStartup = ETrue;
-		}
-		
-		TBuf8<KMaxFileName> filename8;
-		filename8.Copy(dbFileName);
-		int rc = rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
-		if( rc != SQLITE_OK){
-			DP("Error loading DB");
-			User::Panic(_L("Podcatcher"), 10);
-		}
-
-
-	}
 	return iDB;
 }
 
@@ -369,7 +418,6 @@
 	CleanupStack::PopAndDestroy(table);
 	}
 	
-	
 TInt CPodcastModel::GetIapId()
 	{
 	_LIT(KSetting, "IAP\\Id");
@@ -378,24 +426,6 @@
 	return iapId;
 	}
 
-EXPORT_C void CPodcastModel::GetAllShowsL()
-	{
-	iActiveShowList.ResetAndDestroy();
-	iShowEngine->GetAllShowsL(iActiveShowList);
-	}
-
-EXPORT_C void CPodcastModel::GetNewShowsL()
-	{
-	iActiveShowList.ResetAndDestroy();
-	iShowEngine->GetNewShowsL(iActiveShowList);	
-	}
-
-EXPORT_C void CPodcastModel::GetShowsDownloadedL()
-	{
-	iActiveShowList.ResetAndDestroy();
-	iShowEngine->GetShowsDownloadedL(iActiveShowList);
-	}
-
 EXPORT_C void CPodcastModel::GetShowsDownloadingL()
 	{
 	iActiveShowList.ResetAndDestroy();
@@ -405,15 +435,16 @@
 EXPORT_C void CPodcastModel::GetShowsByFeedL(TUint aFeedUid)
 	{
 	iActiveShowList.ResetAndDestroy();
+	iShowEngine->CheckForDeletedShows(aFeedUid);
 	iShowEngine->GetShowsByFeedL(iActiveShowList, aFeedUid);
 	}
 
-EXPORT_C void CPodcastModel::MarkSelectionPlayed()
+EXPORT_C void CPodcastModel::MarkSelectionPlayedL()
 	{
 	for (int i=0;i<iActiveShowList.Count();i++) {
 		if(iActiveShowList[i]->PlayState() != EPlayed) {
 			iActiveShowList[i]->SetPlayState(EPlayed);
-			iShowEngine->UpdateShow(*iActiveShowList[i]);
+			iShowEngine->UpdateShowL(*iActiveShowList[i]);
 		}
 	}
 	}
@@ -435,7 +466,7 @@
 	}
 
 
-void CPodcastModel::ImageOperationCompleteL(TInt /*aError*/, TUint /*aHandle*/)
+void CPodcastModel::ImageOperationCompleteL(TInt /*aError*/, TUint /*aHandle*/, CPodcastModel& /*aPodcastModel*/)
 	{
 	
 	}