engine/src/PodcastModel.cpp
changeset 2 29cda98b007e
child 8 aab3aa4acdd6
equal deleted inserted replaced
1:5f8e5adbbed9 2:29cda98b007e
       
     1 /*
       
     2 * Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB
       
     3 *
       
     4 * All rights reserved.
       
     5 * This component and the accompanying materials are made available
       
     6 * under the terms of the License "Eclipse Public License v1.0"
       
     7 * which accompanies this distribution, and is available
       
     8 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 *
       
    10 * Initial Contributors:
       
    11 * EmbedDev AB - initial contribution.
       
    12 *
       
    13 * Contributors:
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 #include <commdb.h>
       
    20 #include "PodcastModel.h"
       
    21 #include "FeedEngine.h"
       
    22 #include "SoundEngine.h"
       
    23 #include "SettingsEngine.h"
       
    24 #include "ShowEngine.h"
       
    25 #include "connectionengine.h"
       
    26 
       
    27 #include <cmdestination.h>
       
    28 #include <cmmanager.h>
       
    29 #include <bautils.h>
       
    30 
       
    31 const TInt KDefaultGranu = 5;
       
    32 _LIT(KDBFileName, "podcatcher.sqlite");
       
    33 _LIT(KDBTemplateFileName, "podcatcher.sqlite.template");
       
    34 
       
    35 EXPORT_C CPodcastModel* CPodcastModel::NewL()
       
    36 {
       
    37 	CPodcastModel* self = new (ELeave) CPodcastModel;
       
    38 	CleanupStack::PushL(self);
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 }
       
    43 
       
    44 CPodcastModel::~CPodcastModel()
       
    45 {
       
    46 	
       
    47 	delete iFeedEngine;
       
    48 	delete iSoundEngine;
       
    49 	delete iSettingsEngine;
       
    50 	delete iShowEngine;
       
    51 
       
    52 	delete iIapNameArray;
       
    53 	iIapIdArray.Close();
       
    54 	
       
    55 	delete iSNAPNameArray;
       
    56 	iSNAPIdArray.Close();
       
    57 	delete iCommDB;	
       
    58 	sqlite3_close(iDB);
       
    59 	iFsSession.Close();
       
    60 	iActiveShowList.ResetAndDestroy();
       
    61 	iActiveShowList.Close();
       
    62 	delete iConnectionEngine;
       
    63 	iCmManager.Close();
       
    64 	delete iImageHandler;
       
    65 }
       
    66 
       
    67 CPodcastModel::CPodcastModel()
       
    68 {	
       
    69 }
       
    70 
       
    71 void CPodcastModel::ConstructL()
       
    72 {
       
    73 	DP("CPodcastModel::ConstructL BEGIN");
       
    74 	User::LeaveIfError(iFsSession.Connect());
       
    75 	
       
    76 	iCommDB = CCommsDatabase::NewL (EDatabaseTypeUnspecified);
       
    77 	//iCommDB ->ShowHiddenRecords(); // magic
       
    78 	iIapNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
       
    79 	iSNAPNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
       
    80 	iCmManager.OpenL();
       
    81 	iImageHandler = CImageHandler::NewL(FsSession());
       
    82 	
       
    83 	UpdateIAPListL();
       
    84 	UpdateSNAPListL();
       
    85 	
       
    86 	iSettingsEngine = CSettingsEngine::NewL(*this);
       
    87 	iConnectionEngine = CConnectionEngine::NewL(*this);	
       
    88 	
       
    89 	iFeedEngine = CFeedEngine::NewL(*this);
       
    90 	iShowEngine = CShowEngine::NewL(*this);
       
    91 
       
    92 	iSoundEngine = CSoundEngine::NewL(*this);	
       
    93 	DP("CPodcastModel::ConstructL END");
       
    94 }
       
    95 
       
    96 
       
    97 
       
    98 EXPORT_C void CPodcastModel::UpdateIAPListL()
       
    99 	{
       
   100 	iIapNameArray->Reset();
       
   101 	iIapIdArray.Reset();	   
       
   102 
       
   103 	CCommsDbTableView* table = iCommDB->OpenTableLC (TPtrC (IAP)); 
       
   104 	TInt ret = table->GotoFirstRecord ();
       
   105 	TPodcastIAPItem IAPItem;
       
   106 	TBuf <KCommsDbSvrMaxFieldLength> bufName;
       
   107 	while (ret == KErrNone) // There was a first record
       
   108 		{
       
   109 		table->ReadUintL(TPtrC(COMMDB_ID), IAPItem.iIapId);
       
   110 		table->ReadTextL (TPtrC(COMMDB_NAME), bufName);
       
   111 		table->ReadTextL (TPtrC(IAP_BEARER_TYPE), IAPItem.iBearerType);
       
   112 		table->ReadTextL (TPtrC(IAP_SERVICE_TYPE), IAPItem.iServiceType);
       
   113 
       
   114 		iIapIdArray.Append(IAPItem);
       
   115 		iIapNameArray->AppendL(bufName); 		 
       
   116 		ret = table->GotoNextRecord();
       
   117 		}
       
   118 	CleanupStack::PopAndDestroy(); // Close table
       
   119 	}
       
   120 
       
   121 EXPORT_C void CPodcastModel::UpdateSNAPListL()
       
   122 {
       
   123 	DP("CPodcastModel::UpdateSNAPListL BEGIN");
       
   124 	iSNAPNameArray->Reset();
       
   125 	iSNAPIdArray.Reset();
       
   126 	
       
   127 	RCmDestination destination;
       
   128 	TPodcastIAPItem IAPItem;
       
   129 	
       
   130 	RArray<TUint32> destArray;
       
   131 	CleanupClosePushL(destArray);
       
   132 	iCmManager.AllDestinationsL(destArray);
       
   133 	
       
   134 	TInt cnt = destArray.Count();
       
   135 	DP1("destArray.Count==%d", cnt);
       
   136 	for(TInt loop = 0;loop<cnt;loop++)
       
   137 		{
       
   138 		destination = iCmManager.DestinationL (destArray[loop]);
       
   139 		CleanupClosePushL(destination);
       
   140 		if(!destination.IsHidden())
       
   141 			{
       
   142 			IAPItem.iIapId = destArray[loop];
       
   143 			HBufC* name = destination.NameLC();
       
   144 			DP1(" destination.NameLC==%S", name);
       
   145 			iSNAPNameArray->AppendL(*name);
       
   146 			CleanupStack::PopAndDestroy(name);
       
   147 			iSNAPIdArray.Append(IAPItem);
       
   148 			}
       
   149 		CleanupStack::PopAndDestroy();//close destination
       
   150 		}
       
   151 	CleanupStack::PopAndDestroy();// close destArray
       
   152 
       
   153 	DP("CPodcastModel::UpdateSNAPListL END");
       
   154 }
       
   155 
       
   156 EXPORT_C CDesCArrayFlat* CPodcastModel::IAPNames()
       
   157 {
       
   158 	return iIapNameArray;
       
   159 }
       
   160 
       
   161 EXPORT_C RArray<TPodcastIAPItem>& CPodcastModel::IAPIds()
       
   162 {
       
   163 	return iIapIdArray;
       
   164 }
       
   165 
       
   166 EXPORT_C CDesCArrayFlat* CPodcastModel::SNAPNames()
       
   167 {
       
   168 	return iSNAPNameArray;
       
   169 }
       
   170 
       
   171 EXPORT_C RArray<TPodcastIAPItem>& CPodcastModel::SNAPIds()
       
   172 {
       
   173 	return iSNAPIdArray;
       
   174 }
       
   175 
       
   176 
       
   177 RFs& CPodcastModel::FsSession()
       
   178 	{
       
   179 	return iFsSession;
       
   180 	}
       
   181 
       
   182 EXPORT_C void CPodcastModel::SetPlayingPodcast(CShowInfo* aPodcast)
       
   183 {
       
   184 	iPlayingPodcast = aPodcast;
       
   185 }
       
   186 
       
   187 EXPORT_C CShowInfo* CPodcastModel::PlayingPodcast()
       
   188 {
       
   189 	return iPlayingPodcast;
       
   190 }
       
   191 
       
   192 EXPORT_C CFeedEngine& CPodcastModel::FeedEngine()
       
   193 {
       
   194 	return *iFeedEngine;
       
   195 }
       
   196 	
       
   197 EXPORT_C CShowEngine& CPodcastModel::ShowEngine()
       
   198 {
       
   199 	return *iShowEngine;
       
   200 }
       
   201 
       
   202 EXPORT_C CSoundEngine& CPodcastModel::SoundEngine()
       
   203 {
       
   204 	return *iSoundEngine;
       
   205 }
       
   206 
       
   207 EXPORT_C CSettingsEngine& CPodcastModel::SettingsEngine()
       
   208 {
       
   209 	return *iSettingsEngine;
       
   210 }
       
   211 
       
   212 EXPORT_C CConnectionEngine& CPodcastModel::ConnectionEngine()
       
   213 {
       
   214 	return *iConnectionEngine;
       
   215 }
       
   216 
       
   217 EXPORT_C void CPodcastModel::PlayPausePodcastL(CShowInfo* aPodcast, TBool aPlayOnInit) 
       
   218 	{
       
   219 	
       
   220 	// special treatment if this podcast is already active
       
   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 
       
   254 EXPORT_C CFeedInfo* CPodcastModel::ActiveFeedInfo()
       
   255 {
       
   256 	return iActiveFeed;
       
   257 }
       
   258 
       
   259 EXPORT_C void CPodcastModel::SetActiveFeedInfo(CFeedInfo* aFeedInfo)
       
   260 {
       
   261 	iActiveFeed = aFeedInfo;
       
   262 }
       
   263 
       
   264 EXPORT_C RShowInfoArray& CPodcastModel::ActiveShowList()
       
   265 {
       
   266 	return iActiveShowList;
       
   267 }
       
   268 
       
   269 void CPodcastModel::SetActiveShowList(RShowInfoArray& aShowArray)
       
   270 {
       
   271 	iActiveShowList.ResetAndDestroy();
       
   272 	TInt cnt = aShowArray.Count();
       
   273 
       
   274 	for(TInt loop = 0;loop < cnt; loop++)
       
   275 	{
       
   276 		iActiveShowList.Append(aShowArray[loop]);
       
   277 	}
       
   278 }
       
   279 
       
   280 sqlite3* CPodcastModel::DB()
       
   281 {
       
   282 	if (iDB == NULL) {		
       
   283 		TFileName dbFileName;
       
   284 		iFsSession.PrivatePath(dbFileName);
       
   285 		dbFileName.Append(KDBFileName);
       
   286 		DP1("DB is at %S", &dbFileName);
       
   287 
       
   288 		if (!BaflUtils::FileExists(iFsSession, dbFileName)) {
       
   289 			TFileName dbTemplate;
       
   290 			iFsSession.PrivatePath(dbTemplate);
       
   291 			dbTemplate.Append(KDBTemplateFileName);
       
   292 			DP1("No DB found, copying template from %S", &dbTemplate);
       
   293 			BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
       
   294 			iIsFirstStartup = ETrue;
       
   295 		}
       
   296 		
       
   297 		TBuf8<KMaxFileName> filename8;
       
   298 		filename8.Copy(dbFileName);
       
   299 		int rc = rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
       
   300 		if( rc != SQLITE_OK){
       
   301 			DP("Error loading DB");
       
   302 			User::Panic(_L("Podcatcher"), 10);
       
   303 		}
       
   304 
       
   305 
       
   306 	}
       
   307 	return iDB;
       
   308 }
       
   309 
       
   310 void CPodcastModel::SetProxyUsageIfNeededL(RHTTPSession& aSession)
       
   311 	{
       
   312 	TBool useProxy = EFalse;
       
   313 	HBufC* serverName = NULL;
       
   314 	TUint32 port = 0;
       
   315 	
       
   316 	TRAPD(err,GetProxyInformationForConnectionL(useProxy, serverName, port));
       
   317 	if (err == KErrNone && useProxy)
       
   318 		{
       
   319 		CleanupStack::PushL(serverName);
       
   320 		TBuf8<128> proxyAddr;
       
   321 		proxyAddr.Append(*serverName);
       
   322 		proxyAddr.Append(':');
       
   323 		proxyAddr.AppendNum(port);
       
   324 				
       
   325 		RStringF prxAddr = aSession.StringPool().OpenFStringL(proxyAddr);
       
   326 		CleanupClosePushL(prxAddr);
       
   327 		THTTPHdrVal prxUsage(aSession.StringPool().StringF(HTTP::EUseProxy,RHTTPSession::GetTable()));
       
   328 
       
   329 		aSession.ConnectionInfo().SetPropertyL(
       
   330 						aSession.StringPool().StringF(HTTP::EProxyUsage,RHTTPSession::GetTable()), 
       
   331 						aSession.StringPool().StringF(HTTP::EUseProxy,RHTTPSession::GetTable()));
       
   332 
       
   333 		aSession.ConnectionInfo().SetPropertyL(aSession.StringPool().StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()), prxAddr); 
       
   334 		
       
   335 		CleanupStack::PopAndDestroy(&prxAddr);
       
   336 		CleanupStack::PopAndDestroy(serverName);
       
   337 		}
       
   338 	}
       
   339 
       
   340 
       
   341 void CPodcastModel::GetProxyInformationForConnectionL(TBool& aIsUsed, HBufC*& aProxyServerName, TUint32& aPort)
       
   342 	{
       
   343 	TInt iapId = GetIapId();
       
   344 	CCommsDbTableView* table = iCommDB->OpenViewMatchingUintLC(TPtrC(IAP), TPtrC(COMMDB_ID), iapId);
       
   345 	
       
   346 	TUint32 iapService;
       
   347 	HBufC* iapServiceType;
       
   348 	table->ReadUintL(TPtrC(IAP_SERVICE), iapService);
       
   349 	iapServiceType = table->ReadLongTextLC(TPtrC(IAP_SERVICE_TYPE));
       
   350 	
       
   351 	CCommsDbTableView* proxyTableView = iCommDB->OpenViewOnProxyRecordLC(iapService, *iapServiceType);
       
   352 	TInt err = proxyTableView->GotoFirstRecord();
       
   353 	if( err != KErrNone)
       
   354 		{
       
   355 		User::Leave(KErrNotFound);	
       
   356 		}
       
   357 
       
   358 	proxyTableView->ReadBoolL(TPtrC(PROXY_USE_PROXY_SERVER), aIsUsed);
       
   359 	if(aIsUsed)
       
   360 		{
       
   361 		HBufC* serverName = proxyTableView->ReadLongTextLC(TPtrC(PROXY_SERVER_NAME));
       
   362 		proxyTableView->ReadUintL(TPtrC(PROXY_PORT_NUMBER), aPort);
       
   363 		aProxyServerName = serverName->AllocL();
       
   364 		CleanupStack::PopAndDestroy(serverName);
       
   365 		}
       
   366 		
       
   367 	CleanupStack::PopAndDestroy(proxyTableView);
       
   368 	CleanupStack::PopAndDestroy(iapServiceType);
       
   369 	CleanupStack::PopAndDestroy(table);
       
   370 	}
       
   371 	
       
   372 	
       
   373 TInt CPodcastModel::GetIapId()
       
   374 	{
       
   375 	_LIT(KSetting, "IAP\\Id");
       
   376 	TUint32 iapId = 0;
       
   377 	iConnectionEngine->Connection().GetIntSetting(KSetting, iapId);
       
   378 	return iapId;
       
   379 	}
       
   380 
       
   381 EXPORT_C void CPodcastModel::GetAllShows()
       
   382 	{
       
   383 	iActiveShowList.ResetAndDestroy();
       
   384 	TRAP_IGNORE(iShowEngine->GetAllShowsL(iActiveShowList));
       
   385 	}
       
   386 
       
   387 EXPORT_C void CPodcastModel::GetNewShows()
       
   388 	{
       
   389 	iActiveShowList.ResetAndDestroy();
       
   390 	TRAP_IGNORE(iShowEngine->GetNewShowsL(iActiveShowList));	
       
   391 	}
       
   392 
       
   393 EXPORT_C void CPodcastModel::GetShowsDownloaded()
       
   394 	{
       
   395 	iActiveShowList.ResetAndDestroy();
       
   396 	TRAP_IGNORE(iShowEngine->GetShowsDownloadedL(iActiveShowList));
       
   397 	}
       
   398 
       
   399 EXPORT_C void CPodcastModel::GetShowsDownloading()
       
   400 	{
       
   401 	iActiveShowList.ResetAndDestroy();
       
   402 	TRAP_IGNORE(iShowEngine->GetShowsDownloadingL(iActiveShowList));
       
   403 	}
       
   404 
       
   405 EXPORT_C void CPodcastModel::GetShowsByFeed(TUint aFeedUid)
       
   406 	{
       
   407 	iActiveShowList.ResetAndDestroy();
       
   408 	TRAP_IGNORE(iShowEngine->GetShowsByFeedL(iActiveShowList, aFeedUid));
       
   409 	}
       
   410 
       
   411 EXPORT_C void CPodcastModel::MarkSelectionPlayed()
       
   412 	{
       
   413 	for (int i=0;i<iActiveShowList.Count();i++) {
       
   414 		if(iActiveShowList[i]->PlayState() != EPlayed) {
       
   415 			iActiveShowList[i]->SetPlayState(EPlayed);
       
   416 			iShowEngine->UpdateShow(*iActiveShowList[i]);
       
   417 		}
       
   418 	}
       
   419 	}
       
   420 
       
   421 TInt CPodcastModel::FindActiveShowByUid(TUint aUid)
       
   422 	{
       
   423 	for (int i=0;i<iActiveShowList.Count();i++) {
       
   424 		if (iActiveShowList[i]->Uid() == aUid) {
       
   425 			return i;
       
   426 		}
       
   427 	}
       
   428 	
       
   429 	return KErrNotFound;
       
   430 	}
       
   431 
       
   432 EXPORT_C TBool CPodcastModel::IsFirstStartup()
       
   433 	{
       
   434 	return iIsFirstStartup;
       
   435 	}
       
   436 
       
   437 
       
   438 void CPodcastModel::ImageOperationCompleteL(TInt /*aError*/)
       
   439 	{
       
   440 	
       
   441 	}
       
   442 
       
   443 EXPORT_C CImageHandler& CPodcastModel::ImageHandler()
       
   444 	{
       
   445 	return *iImageHandler;
       
   446 	}