engine/src/PodcastModel.cpp
changeset 109 223f270fa7ff
parent 96 a8538f50e2ba
child 110 403412eb5292
equal deleted inserted replaced
108:554d9980157f 109:223f270fa7ff
    20 #include "PodcastModel.h"
    20 #include "PodcastModel.h"
    21 #include "FeedEngine.h"
    21 #include "FeedEngine.h"
    22 #include "SettingsEngine.h"
    22 #include "SettingsEngine.h"
    23 #include "ShowEngine.h"
    23 #include "ShowEngine.h"
    24 #include "connectionengine.h"
    24 #include "connectionengine.h"
       
    25 #include "podcastutils.h"
    25 
    26 
    26 #include <cmdestination.h>
    27 #include <cmdestination.h>
    27 #include <cmmanager.h>
    28 #include <cmmanager.h>
    28 #include <bautils.h>
    29 #include <bautils.h>
    29 
    30 
    81 	UpdateIAPListL();
    82 	UpdateIAPListL();
    82 	UpdateSNAPListL();
    83 	UpdateSNAPListL();
    83 	
    84 	
    84 	iSettingsEngine = CSettingsEngine::NewL(*this);
    85 	iSettingsEngine = CSettingsEngine::NewL(*this);
    85 	iConnectionEngine = CConnectionEngine::NewL(*this);	
    86 	iConnectionEngine = CConnectionEngine::NewL(*this);	
       
    87 	
       
    88 	TRAPD(err, OpenDBL());
       
    89 	
       
    90 	if (err != KErrNone)
       
    91 		{
       
    92 		ResetDB();
       
    93 		
       
    94 		TRAP(err, OpenDBL());
       
    95 		
       
    96 		if (err != KErrNone)
       
    97 			{
       
    98 			User::Panic(_L("Podcatcher"), 1);
       
    99 			}
       
   100 		
       
   101 		}
    86 	
   102 	
    87 	iFeedEngine = CFeedEngine::NewL(*this);
   103 	iFeedEngine = CFeedEngine::NewL(*this);
    88 	iShowEngine = CShowEngine::NewL(*this);
   104 	iShowEngine = CShowEngine::NewL(*this);
    89 
   105 
    90 	DP("CPodcastModel::ConstructL END");
   106 	DP("CPodcastModel::ConstructL END");
   235 	{
   251 	{
   236 		iActiveShowList.Append(aShowArray[loop]);
   252 		iActiveShowList.Append(aShowArray[loop]);
   237 	}
   253 	}
   238 }
   254 }
   239 
   255 
   240 sqlite3* CPodcastModel::DB()
   256 void CPodcastModel::ResetDB()
   241 {
   257 	{
   242 	DP("CPodcastModel::DB BEGIN");
   258 	DP("CPodcastModel::ResetDB BEGIN");
   243 	if (iDB == NULL) {		
   259 	
   244 		TFileName dbFileName;
   260 	if (iDB != NULL)
   245 		dbFileName.Copy(iSettingsEngine->PrivatePath());
   261 		{
   246 		//iFsSession.PrivatePath(dbFileName);
   262 		sqlite3_close(iDB);
   247 		dbFileName.Append(KDBFileName);
   263 		iDB = NULL;
   248 		DP1("DB is at %S", &dbFileName);
   264 		}
   249 
   265 	
   250 		if (!BaflUtils::FileExists(iFsSession, dbFileName)) {
   266 	TFileName dbFileName;
   251 			TFileName dbTemplate;
   267 	dbFileName.Copy(iSettingsEngine->PrivatePath());
   252 			dbTemplate.Copy(iSettingsEngine->PrivatePath());
   268 	dbFileName.Append(KDBFileName);
   253 			//iFsSession.PrivatePath(dbTemplate);
   269 
   254 			dbTemplate.Append(KDBTemplateFileName);
   270 	// remove the old DB file
   255 			DP1("No DB found, copying template from %S", &dbTemplate);
   271 	if (BaflUtils::FileExists(iFsSession, dbFileName))
   256 			BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
   272 		{
   257 			iIsFirstStartup = ETrue;
   273 		BaflUtils::DeleteFile(iFsSession, dbFileName);
   258 		}
   274 		}
   259 		
   275 
       
   276 	// copy template to new DB
       
   277 	TFileName dbTemplate;
       
   278 	dbTemplate.Copy(iSettingsEngine->PrivatePath());
       
   279 	dbTemplate.Append(KDBTemplateFileName);
       
   280 
       
   281 	BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
       
   282 	iIsFirstStartup = ETrue;
       
   283 	DP("CPodcastModel::ResetDB END");
       
   284 	}
       
   285 
       
   286 
       
   287 void CPodcastModel::OpenDBL()
       
   288 	{
       
   289 	DP("CPodcastModel::OpenDBL BEGIN");
       
   290 	
       
   291 	if (iDB != NULL)
       
   292 		{
       
   293 		sqlite3_close(iDB);
       
   294 		iDB = NULL;
       
   295 		}
       
   296 	
       
   297 	TFileName dbFileName;
       
   298 	dbFileName.Copy(iSettingsEngine->PrivatePath());
       
   299 	dbFileName.Append(KDBFileName);
       
   300 		
       
   301 	if (!BaflUtils::FileExists(iFsSession, dbFileName))
       
   302 		{
       
   303 		User::Leave(KErrNotFound);
       
   304 		}
       
   305 	
       
   306 	if (iDB == NULL) {	
       
   307 		// open DB
   260 		TBuf8<KMaxFileName> filename8;
   308 		TBuf8<KMaxFileName> filename8;
   261 		filename8.Copy(dbFileName);
   309 		filename8.Copy(dbFileName);
   262 		DP("Before sqlite3_open");
       
   263 		int rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
   310 		int rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
   264 		if( rc != SQLITE_OK){
   311 		if(rc != SQLITE_OK){
   265 			DP("Error loading DB");
   312 			User::Leave(KErrCorrupt);
   266 			User::Panic(_L("Podcatcher"), 10);
   313 		}
   267 		}
   314 
   268 
   315 		// do a test query 
   269 
   316 		sqlite3_stmt *st;
   270 	}
   317 		rc = sqlite3_prepare_v2(iDB,"select count(*) from feeds" , -1, &st, (const char**) NULL);
   271 	DP("CPodcastModel::DB END");
   318 		if( rc==SQLITE_OK )
       
   319 			{
       
   320 			Cleanup_sqlite3_finalize_PushL(st);
       
   321 			rc = sqlite3_step(st);
       
   322 					
       
   323 			if (rc != SQLITE_ROW)
       
   324 				{
       
   325 				User::Leave(KErrCorrupt);
       
   326 				}
       
   327 			CleanupStack::PopAndDestroy(); // st
       
   328 			}
       
   329 		else
       
   330 			{
       
   331 			User::Leave(KErrCorrupt);
       
   332 			}
       
   333 		}
       
   334 
       
   335 	DP("CPodcastModel::OpenDBL END");	
       
   336 	}
       
   337 
       
   338 sqlite3* CPodcastModel::DB()
       
   339 {
   272 	return iDB;
   340 	return iDB;
   273 }
   341 }
   274 
   342 
   275 void CPodcastModel::SetProxyUsageIfNeededL(RHTTPSession& aSession)
   343 void CPodcastModel::SetProxyUsageIfNeededL(RHTTPSession& aSession)
   276 	{
   344 	{