engine/src/PodcastModel.cpp
changeset 60 4d230e702aa3
parent 32 26a3f2dfba08
child 65 bcd88ba95046
equal deleted inserted replaced
59:9569ea080d5a 60:4d230e702aa3
    17 */
    17 */
    18 
    18 
    19 #include <commdb.h>
    19 #include <commdb.h>
    20 #include "PodcastModel.h"
    20 #include "PodcastModel.h"
    21 #include "FeedEngine.h"
    21 #include "FeedEngine.h"
    22 #include "SoundEngine.h"
       
    23 #include "SettingsEngine.h"
    22 #include "SettingsEngine.h"
    24 #include "ShowEngine.h"
    23 #include "ShowEngine.h"
    25 #include "connectionengine.h"
    24 #include "connectionengine.h"
       
    25 #include "podcastutils.h"
       
    26 #include "Podcatcher.pan"
    26 
    27 
    27 #include <cmdestination.h>
    28 #include <cmdestination.h>
    28 #include <cmmanager.h>
    29 #include <cmmanager.h>
    29 #include <bautils.h>
    30 #include <bautils.h>
    30 
    31 
    43 
    44 
    44 CPodcastModel::~CPodcastModel()
    45 CPodcastModel::~CPodcastModel()
    45 {
    46 {
    46 	
    47 	
    47 	delete iFeedEngine;
    48 	delete iFeedEngine;
    48 	delete iSoundEngine;
       
    49 	delete iSettingsEngine;
    49 	delete iSettingsEngine;
    50 	delete iShowEngine;
    50 	delete iShowEngine;
    51 
    51 
    52 	delete iIapNameArray;
    52 	delete iIapNameArray;
    53 	iIapIdArray.Close();
    53 	iIapIdArray.Close();
    76 	iCommDB = CCommsDatabase::NewL (EDatabaseTypeUnspecified);
    76 	iCommDB = CCommsDatabase::NewL (EDatabaseTypeUnspecified);
    77 	//iCommDB ->ShowHiddenRecords(); // magic
    77 	//iCommDB ->ShowHiddenRecords(); // magic
    78 	iIapNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
    78 	iIapNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
    79 	iSNAPNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
    79 	iSNAPNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
    80 	iCmManager.OpenL();
    80 	iCmManager.OpenL();
    81 	iImageHandler = CImageHandler::NewL(FsSession());
    81 	iImageHandler = CImageHandler::NewL(FsSession(), *this);
    82 	
    82 	
    83 	UpdateIAPListL();
    83 	UpdateIAPListL();
    84 	UpdateSNAPListL();
    84 	UpdateSNAPListL();
    85 	
    85 	
    86 	iSettingsEngine = CSettingsEngine::NewL(*this);
    86 	iSettingsEngine = CSettingsEngine::NewL(*this);
    87 	iConnectionEngine = CConnectionEngine::NewL(*this);	
    87 	iConnectionEngine = CConnectionEngine::NewL(*this);	
    88 	
    88 	
       
    89 	TRAPD(err, OpenDBL());
       
    90 	
       
    91 	if (err != KErrNone)
       
    92 		{
       
    93 		ResetDB();
       
    94 		
       
    95 		TRAP(err, OpenDBL());
       
    96 		
       
    97 		if (err != KErrNone)
       
    98 			{
       
    99 			Panic(EPodcatcherPanicDB);
       
   100 			}
       
   101 		
       
   102 		}
       
   103 	
    89 	iFeedEngine = CFeedEngine::NewL(*this);
   104 	iFeedEngine = CFeedEngine::NewL(*this);
    90 	iShowEngine = CShowEngine::NewL(*this);
   105 	iShowEngine = CShowEngine::NewL(*this);
    91 
   106 
    92 	iSoundEngine = CSoundEngine::NewL(*this);	
       
    93 	DP("CPodcastModel::ConstructL END");
   107 	DP("CPodcastModel::ConstructL END");
    94 }
   108 }
    95 
   109 
    96 
   110 
    97 
   111 
   197 EXPORT_C CShowEngine& CPodcastModel::ShowEngine()
   211 EXPORT_C CShowEngine& CPodcastModel::ShowEngine()
   198 {
   212 {
   199 	return *iShowEngine;
   213 	return *iShowEngine;
   200 }
   214 }
   201 
   215 
   202 EXPORT_C CSoundEngine& CPodcastModel::SoundEngine()
       
   203 {
       
   204 	return *iSoundEngine;
       
   205 }
       
   206 
       
   207 EXPORT_C CSettingsEngine& CPodcastModel::SettingsEngine()
   216 EXPORT_C CSettingsEngine& CPodcastModel::SettingsEngine()
   208 {
   217 {
   209 	return *iSettingsEngine;
   218 	return *iSettingsEngine;
   210 }
   219 }
   211 
   220 
   214 	return *iConnectionEngine;
   223 	return *iConnectionEngine;
   215 }
   224 }
   216 
   225 
   217 EXPORT_C void CPodcastModel::PlayPausePodcastL(CShowInfo* aPodcast, TBool aPlayOnInit) 
   226 EXPORT_C void CPodcastModel::PlayPausePodcastL(CShowInfo* aPodcast, TBool aPlayOnInit) 
   218 	{
   227 	{
   219 	
   228 	// TODO: interact with MPX
   220 	// special treatment if this podcast is already active
   229 	}
   221 	if (iPlayingPodcast->Uid() == aPodcast->Uid() && SoundEngine().State() > ESoundEngineOpening ) {
       
   222 		if (aPodcast->PlayState() == EPlaying) {
       
   223 			SoundEngine().Pause();
       
   224 			aPodcast->SetPosition(iSoundEngine->Position());
       
   225 			aPodcast->SetPlayState(EPlayed);
       
   226 			aPodcast->SetPlayState(EPlayed);
       
   227 		} else {
       
   228 			iSoundEngine->Play();
       
   229 		}
       
   230 	} else {
       
   231 		// switching file, so save position
       
   232 		iSoundEngine->Pause();
       
   233 		if (iPlayingPodcast != NULL) {
       
   234 			iPlayingPodcast->SetPosition(iSoundEngine->Position());
       
   235 		}
       
   236 		
       
   237 		iSoundEngine->Stop(EFalse);
       
   238 
       
   239 		// we play video podcasts through the external player
       
   240 		if(aPodcast != NULL && aPodcast->ShowType() != EVideoPodcast) {
       
   241 			DP1("Starting: %S", &(aPodcast->FileName()));
       
   242 			TRAPD( error, iSoundEngine->OpenFileL(aPodcast->FileName(), aPlayOnInit) );
       
   243 			if (error != KErrNone) {
       
   244 				DP1("Error: %d", error);
       
   245 			} else {
       
   246 				iSoundEngine->SetPosition(aPodcast->Position().Int64() / 1000000);
       
   247 			}
       
   248 		}
       
   249 
       
   250 		iPlayingPodcast = aPodcast;		
       
   251 	}
       
   252 }
       
   253 
   230 
   254 EXPORT_C CFeedInfo* CPodcastModel::ActiveFeedInfo()
   231 EXPORT_C CFeedInfo* CPodcastModel::ActiveFeedInfo()
   255 {
   232 {
   256 	return iActiveFeed;
   233 	return iActiveFeed;
   257 }
   234 }
   275 	{
   252 	{
   276 		iActiveShowList.Append(aShowArray[loop]);
   253 		iActiveShowList.Append(aShowArray[loop]);
   277 	}
   254 	}
   278 }
   255 }
   279 
   256 
   280 sqlite3* CPodcastModel::DB()
   257 EXPORT_C void CPodcastModel::DropDB()
   281 {
   258 	{
   282 	if (iDB == NULL) {		
   259 	if (iDB != NULL)
   283 		TFileName dbFileName;
   260 		{
   284 		iFsSession.PrivatePath(dbFileName);
   261 		sqlite3_close(iDB);
   285 		dbFileName.Append(KDBFileName);
   262 		iDB = NULL;
   286 		DP1("DB is at %S", &dbFileName);
   263 		}
   287 
   264 	
   288 		if (!BaflUtils::FileExists(iFsSession, dbFileName)) {
   265 	TFileName dbFileName;
   289 			TFileName dbTemplate;
   266 	dbFileName.Copy(iSettingsEngine->PrivatePath());
   290 			iFsSession.PrivatePath(dbTemplate);
   267 	dbFileName.Append(KDBFileName);
   291 			dbTemplate.Append(KDBTemplateFileName);
   268 
   292 			DP1("No DB found, copying template from %S", &dbTemplate);
   269 	// remove the old DB file
   293 			BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
   270 	if (BaflUtils::FileExists(iFsSession, dbFileName))
   294 			iIsFirstStartup = ETrue;
   271 		{
   295 		}
   272 		BaflUtils::DeleteFile(iFsSession, dbFileName);
   296 		
   273 		}
       
   274 	}
       
   275 
       
   276 void CPodcastModel::ResetDB()
       
   277 	{
       
   278 	DP("CPodcastModel::ResetDB BEGIN");
       
   279 	
       
   280 	DropDB();
       
   281 	
       
   282 	TFileName dbFileName;
       
   283 	dbFileName.Copy(iSettingsEngine->PrivatePath());
       
   284 	dbFileName.Append(KDBFileName);
       
   285 
       
   286 	// remove the old DB file
       
   287 	if (BaflUtils::FileExists(iFsSession, dbFileName))
       
   288 		{
       
   289 		BaflUtils::DeleteFile(iFsSession, dbFileName);
       
   290 		}
       
   291 
       
   292 	// copy template to new DB
       
   293 	TFileName dbTemplate;
       
   294 	dbTemplate.Copy(iSettingsEngine->PrivatePath());
       
   295 	dbTemplate.Append(KDBTemplateFileName);
       
   296 
       
   297 	BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
       
   298 	iIsFirstStartup = ETrue;
       
   299 	DP("CPodcastModel::ResetDB END");
       
   300 	}
       
   301 
       
   302 
       
   303 void CPodcastModel::OpenDBL()
       
   304 	{
       
   305 	DP("CPodcastModel::OpenDBL BEGIN");
       
   306 	
       
   307 	if (iDB != NULL)
       
   308 		{
       
   309 		sqlite3_close(iDB);
       
   310 		iDB = NULL;
       
   311 		}
       
   312 	
       
   313 	TFileName dbFileName;
       
   314 	dbFileName.Copy(iSettingsEngine->PrivatePath());
       
   315 	dbFileName.Append(KDBFileName);
       
   316 		
       
   317 	if (!BaflUtils::FileExists(iFsSession, dbFileName))
       
   318 		{
       
   319 		User::Leave(KErrNotFound);
       
   320 		}
       
   321 	
       
   322 	if (iDB == NULL) {	
       
   323 		// open DB
   297 		TBuf8<KMaxFileName> filename8;
   324 		TBuf8<KMaxFileName> filename8;
   298 		filename8.Copy(dbFileName);
   325 		filename8.Copy(dbFileName);
   299 		int rc = rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
   326 		int rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
   300 		if( rc != SQLITE_OK){
   327 		if(rc != SQLITE_OK){
   301 			DP("Error loading DB");
   328 			User::Leave(KErrCorrupt);
   302 			User::Panic(_L("Podcatcher"), 10);
   329 		}
   303 		}
   330 
   304 
   331 		// do a test query 
   305 
   332 		sqlite3_stmt *st;
   306 	}
   333 		rc = sqlite3_prepare_v2(iDB,"select count(*) from feeds" , -1, &st, (const char**) NULL);
       
   334 		if( rc==SQLITE_OK )
       
   335 			{
       
   336 			Cleanup_sqlite3_finalize_PushL(st);
       
   337 			rc = sqlite3_step(st);
       
   338 					
       
   339 			if (rc != SQLITE_ROW)
       
   340 				{
       
   341 				User::Leave(KErrCorrupt);
       
   342 				}
       
   343 			CleanupStack::PopAndDestroy(); // st
       
   344 			}
       
   345 		else
       
   346 			{
       
   347 			User::Leave(KErrCorrupt);
       
   348 			}
       
   349 		}
       
   350 
       
   351 	DP("CPodcastModel::OpenDBL END");	
       
   352 	}
       
   353 
       
   354 sqlite3* CPodcastModel::DB()
       
   355 {
   307 	return iDB;
   356 	return iDB;
   308 }
   357 }
   309 
   358 
   310 void CPodcastModel::SetProxyUsageIfNeededL(RHTTPSession& aSession)
   359 void CPodcastModel::SetProxyUsageIfNeededL(RHTTPSession& aSession)
   311 	{
   360 	{
   367 	CleanupStack::PopAndDestroy(proxyTableView);
   416 	CleanupStack::PopAndDestroy(proxyTableView);
   368 	CleanupStack::PopAndDestroy(iapServiceType);
   417 	CleanupStack::PopAndDestroy(iapServiceType);
   369 	CleanupStack::PopAndDestroy(table);
   418 	CleanupStack::PopAndDestroy(table);
   370 	}
   419 	}
   371 	
   420 	
   372 	
       
   373 TInt CPodcastModel::GetIapId()
   421 TInt CPodcastModel::GetIapId()
   374 	{
   422 	{
   375 	_LIT(KSetting, "IAP\\Id");
   423 	_LIT(KSetting, "IAP\\Id");
   376 	TUint32 iapId = 0;
   424 	TUint32 iapId = 0;
   377 	iConnectionEngine->Connection().GetIntSetting(KSetting, iapId);
   425 	iConnectionEngine->Connection().GetIntSetting(KSetting, iapId);
   378 	return iapId;
   426 	return iapId;
   379 	}
   427 	}
   380 
   428 
   381 EXPORT_C void CPodcastModel::GetAllShowsL()
       
   382 	{
       
   383 	iActiveShowList.ResetAndDestroy();
       
   384 	iShowEngine->GetAllShowsL(iActiveShowList);
       
   385 	}
       
   386 
       
   387 EXPORT_C void CPodcastModel::GetNewShowsL()
       
   388 	{
       
   389 	iActiveShowList.ResetAndDestroy();
       
   390 	iShowEngine->GetNewShowsL(iActiveShowList);	
       
   391 	}
       
   392 
       
   393 EXPORT_C void CPodcastModel::GetShowsDownloadedL()
       
   394 	{
       
   395 	iActiveShowList.ResetAndDestroy();
       
   396 	iShowEngine->GetShowsDownloadedL(iActiveShowList);
       
   397 	}
       
   398 
       
   399 EXPORT_C void CPodcastModel::GetShowsDownloadingL()
   429 EXPORT_C void CPodcastModel::GetShowsDownloadingL()
   400 	{
   430 	{
   401 	iActiveShowList.ResetAndDestroy();
   431 	iActiveShowList.ResetAndDestroy();
   402 	iShowEngine->GetShowsDownloadingL(iActiveShowList);
   432 	iShowEngine->GetShowsDownloadingL(iActiveShowList);
   403 	}
   433 	}
   404 
   434 
   405 EXPORT_C void CPodcastModel::GetShowsByFeedL(TUint aFeedUid)
   435 EXPORT_C void CPodcastModel::GetShowsByFeedL(TUint aFeedUid)
   406 	{
   436 	{
   407 	iActiveShowList.ResetAndDestroy();
   437 	iActiveShowList.ResetAndDestroy();
       
   438 	iShowEngine->CheckForDeletedShows(aFeedUid);
   408 	iShowEngine->GetShowsByFeedL(iActiveShowList, aFeedUid);
   439 	iShowEngine->GetShowsByFeedL(iActiveShowList, aFeedUid);
   409 	}
   440 	}
   410 
   441 
   411 EXPORT_C void CPodcastModel::MarkSelectionPlayed()
   442 EXPORT_C void CPodcastModel::MarkSelectionPlayedL()
   412 	{
   443 	{
   413 	for (int i=0;i<iActiveShowList.Count();i++) {
   444 	for (int i=0;i<iActiveShowList.Count();i++) {
   414 		if(iActiveShowList[i]->PlayState() != EPlayed) {
   445 		if(iActiveShowList[i]->PlayState() != EPlayed) {
   415 			iActiveShowList[i]->SetPlayState(EPlayed);
   446 			iActiveShowList[i]->SetPlayState(EPlayed);
   416 			iShowEngine->UpdateShow(*iActiveShowList[i]);
   447 			iShowEngine->UpdateShowL(*iActiveShowList[i]);
   417 		}
   448 		}
   418 	}
   449 	}
   419 	}
   450 	}
   420 
   451 
   421 TInt CPodcastModel::FindActiveShowByUid(TUint aUid)
   452 TInt CPodcastModel::FindActiveShowByUid(TUint aUid)
   433 	{
   464 	{
   434 	return iIsFirstStartup;
   465 	return iIsFirstStartup;
   435 	}
   466 	}
   436 
   467 
   437 
   468 
   438 void CPodcastModel::ImageOperationCompleteL(TInt /*aError*/, TUint /*aHandle*/)
   469 void CPodcastModel::ImageOperationCompleteL(TInt /*aError*/, TUint /*aHandle*/, CPodcastModel& /*aPodcastModel*/)
   439 	{
   470 	{
   440 	
   471 	
   441 	}
   472 	}
   442 
   473 
   443 EXPORT_C CImageHandler& CPodcastModel::ImageHandler()
   474 EXPORT_C CImageHandler& CPodcastModel::ImageHandler()