engine/src/ShowEngine.cpp
changeset 2 29cda98b007e
child 6 e211a78d3037
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 "ShowEngine.h"
       
    20 #include "FeedEngine.h"
       
    21 #include "FeedInfo.h"
       
    22 #include <bautils.h>
       
    23 #include <s32file.h>
       
    24 #include "SettingsEngine.h"
       
    25 #include <e32hashtab.h>
       
    26 #include <httperr.h>
       
    27 #include "SoundEngine.h"
       
    28 #include "debug.h"
       
    29 #include "PodcastUtils.h"
       
    30 
       
    31 //#include <mpxmedia.h>
       
    32 //#include <mpxattribute.h>
       
    33 //#include <mpxmediageneraldefs.h>
       
    34 
       
    35 const TUint KMaxDownloadErrors = 3;
       
    36 const TInt KMimeBufLength = 100;
       
    37 
       
    38 // Cleanup stack macro for SQLite3
       
    39 // TODO Move this to some common place.
       
    40 static void Cleanup_sqlite3_finalize_wrapper(TAny* handle)
       
    41 	{
       
    42 	sqlite3_finalize(static_cast<sqlite3_stmt*>(handle));
       
    43 	}
       
    44 #define Cleanup_sqlite3_finalize_PushL(__handle) CleanupStack::PushL(TCleanupItem(&Cleanup_sqlite3_finalize_wrapper, __handle))
       
    45 
       
    46 
       
    47 CShowEngine::CShowEngine(CPodcastModel& aPodcastModel) :
       
    48 	iPodcastModel(aPodcastModel),
       
    49 	iDB(*aPodcastModel.DB())
       
    50 	{
       
    51 	}
       
    52 
       
    53 EXPORT_C CShowEngine::~CShowEngine()
       
    54 	{	
       
    55 	delete iShowClient;
       
    56 	iObservers.Close();
       
    57 	delete iShowDownloading;
       
    58 	delete iMetaDataReader;
       
    59 	iApaSession.Close();
       
    60 	}
       
    61 
       
    62 EXPORT_C CShowEngine* CShowEngine::NewL(CPodcastModel& aPodcastModel)
       
    63 	{
       
    64 	CShowEngine* self = new (ELeave) CShowEngine(aPodcastModel);
       
    65 	CleanupStack::PushL(self);
       
    66 	self->ConstructL();
       
    67 	CleanupStack::Pop(self);
       
    68 	return self;
       
    69 	}
       
    70 
       
    71 EXPORT_C void CShowEngine::GetMimeType(const TDesC& aFileName, TDes& aMimeType)
       
    72 	{
       
    73 	aMimeType.Zero();
       
    74 	RFile file;
       
    75 	if (file.Open(iPodcastModel.FsSession(), aFileName, 0) == KErrNone)
       
    76 		{
       
    77 		if (file.Read(iRecogBuffer) == KErrNone)
       
    78 			{
       
    79 			TDataRecognitionResult result;
       
    80 			if (iApaSession.RecognizeData(aFileName, iRecogBuffer, result)
       
    81 					== KErrNone)
       
    82 				{
       
    83 				aMimeType.Copy(result.iDataType.Des());
       
    84 				}
       
    85 
       
    86 			}
       
    87 		}
       
    88 	file.Close();
       
    89 	}
       
    90 
       
    91 void CShowEngine::ConstructL()
       
    92 	{	
       
    93 	iShowClient = CHttpClient::NewL(iPodcastModel, *this);
       
    94 	iShowClient->SetResumeEnabled(ETrue);
       
    95 	iMetaDataReader = new (ELeave) CMetaDataReader(*this, iPodcastModel.FsSession());
       
    96 	iMetaDataReader->ConstructL();
       
    97 	User::LeaveIfError(iApaSession.Connect());
       
    98 	}
       
    99 
       
   100 EXPORT_C void CShowEngine::SuspendDownloads()
       
   101 	{
       
   102 	iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
       
   103 	iShowClient->Stop();
       
   104 	}
       
   105 
       
   106 EXPORT_C void CShowEngine::ResumeDownloadsL()
       
   107 	{
       
   108 	DP("CShowEngine::ResumeDownloadsL BEGIN");
       
   109 	if (iPodcastModel.SettingsEngine().DownloadSuspended())
       
   110 		{
       
   111 		iPodcastModel.SettingsEngine().SetDownloadSuspended(EFalse);
       
   112 		iDownloadErrors = 0;
       
   113 		DownloadNextShowL();
       
   114 		}
       
   115 	DP("CShowEngine::ResumeDownloadsL END");
       
   116 	}
       
   117 
       
   118 EXPORT_C void CShowEngine::RemoveAllDownloads()
       
   119 	{
       
   120 	if (!iPodcastModel.SettingsEngine().DownloadSuspended())
       
   121 		{
       
   122 		SuspendDownloads();
       
   123 		}
       
   124 
       
   125 	DBRemoveAllDownloads();
       
   126 	}
       
   127 
       
   128 EXPORT_C TBool CShowEngine::RemoveDownloadL(TUint aUid)
       
   129 	{
       
   130 	DP("CShowEngine::RemoveDownload\t Trying to remove download");
       
   131 
       
   132 	TBool retVal = EFalse;
       
   133 	TBool resumeAfterRemove = EFalse;
       
   134 	// if trying to remove the present download, we first stop it
       
   135 	if (!iPodcastModel.SettingsEngine().DownloadSuspended() && iShowDownloading != NULL
       
   136 			&& iShowDownloading->Uid() == aUid)
       
   137 		{
       
   138 		DP("CShowEngine::RemoveDownload\t This is the active download, we suspend downloading");
       
   139 		SuspendDownloads();
       
   140 		resumeAfterRemove = ETrue;
       
   141 		}
       
   142 
       
   143 	CShowInfo *info = DBGetShowByUidL(aUid);
       
   144 	if (info != NULL)
       
   145 		{
       
   146 		info->SetDownloadState(ENotDownloaded);
       
   147 		DBUpdateShow(*info);
       
   148 		delete info;
       
   149 		}
       
   150 	DBRemoveDownload(aUid);
       
   151 
       
   152 	// partial downloads should be removed
       
   153 	if (iShowDownloading)
       
   154 		{
       
   155 		BaflUtils::DeleteFile(iPodcastModel.FsSession(), iShowDownloading->FileName());
       
   156 		}
       
   157 
       
   158 	NotifyShowDownloadUpdatedL(-1, -1);
       
   159 	NotifyDownloadQueueUpdatedL();
       
   160 	
       
   161 	if (resumeAfterRemove) {
       
   162 		ResumeDownloadsL();
       
   163 	}
       
   164 	
       
   165 	DownloadNextShowL();
       
   166 	retVal = ETrue;
       
   167 
       
   168 	return retVal;
       
   169 	}
       
   170 
       
   171 void CShowEngine::Connected(CHttpClient* /*aClient*/)
       
   172 	{
       
   173 
       
   174 	}
       
   175 
       
   176 void CShowEngine::Progress(CHttpClient* /*aHttpClient */, TInt aBytes,
       
   177 		TInt aTotalBytes)
       
   178 	{	
       
   179 	iShowDownloading->SetShowSize(aTotalBytes);
       
   180 	TRAP_IGNORE(NotifyShowDownloadUpdatedL(aBytes, aTotalBytes));
       
   181 	}
       
   182 
       
   183 void CShowEngine::Disconnected(CHttpClient* /*aClient */)
       
   184 	{
       
   185 	}
       
   186 
       
   187 void CShowEngine::DownloadInfo(CHttpClient* aHttpClient, TInt aTotalBytes)
       
   188 	{
       
   189 	DP1("About to download %d bytes", aTotalBytes);
       
   190 	if (aHttpClient == iShowClient && iShowDownloading != NULL
       
   191 			&& aTotalBytes != -1)
       
   192 		{
       
   193 		iShowDownloading->SetShowSize(aTotalBytes);
       
   194 		}
       
   195 	}
       
   196 
       
   197 TBool CShowEngine::GetShowL(CShowInfo *info)
       
   198 	{
       
   199 	CFeedInfo *feedInfo = iPodcastModel.FeedEngine().GetFeedInfoByUid(
       
   200 			info->FeedUid());
       
   201 	if (feedInfo == NULL)
       
   202 		{
       
   203 		DP("Feed not found for this show!");
       
   204 		return EFalse;
       
   205 		}
       
   206 
       
   207 	TFileName filePath;
       
   208 	filePath.Copy(iPodcastModel.SettingsEngine().BaseDir());
       
   209 	
       
   210 	TFileName relPath;
       
   211 	relPath.Copy(feedInfo->Title());
       
   212 	relPath.Append('\\');
       
   213 
       
   214 	TFileName fileName;
       
   215 	PodcastUtils::FileNameFromUrl(info->Url(), fileName);
       
   216 	relPath.Append(fileName);
       
   217 	PodcastUtils::EnsureProperPathName(relPath);
       
   218 
       
   219 	// complete file path is base dir + rel path
       
   220 	filePath.Append(relPath);
       
   221 	info->SetFileNameL(filePath);
       
   222 
       
   223 	return iShowClient->GetL(info->Url(), filePath);
       
   224 	}
       
   225 
       
   226 EXPORT_C TBool CShowEngine::AddShowL(const CShowInfo& aItem)
       
   227 	{
       
   228 	DP1("CShowEngine::AddShowL, title=%S", &aItem.Title());
       
   229 	CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid());
       
   230 
       
   231 	if (showInfo == NULL)
       
   232 		{
       
   233 		DBAddShow(aItem);
       
   234 		return ETrue;
       
   235 		}
       
   236 	else
       
   237 		{
       
   238 		delete showInfo;	
       
   239 		return EFalse;
       
   240 		}	
       
   241 	}
       
   242 
       
   243 EXPORT_C void CShowEngine::AddObserver(MShowEngineObserver *observer)
       
   244 	{
       
   245 	iObservers.Append(observer);
       
   246 	}
       
   247 
       
   248 EXPORT_C void CShowEngine::RemoveObserver(MShowEngineObserver *observer)
       
   249 	{
       
   250 	TInt index = iObservers.Find(observer);
       
   251 
       
   252 	if (index > KErrNotFound)
       
   253 		{
       
   254 		iObservers.Remove(index);
       
   255 		}
       
   256 	}
       
   257 
       
   258 void CShowEngine::AddShowToMpxCollection(CShowInfo &/*aShowInfo*/)
       
   259 	{
       
   260 /*	RArray<TInt> contentIDs;
       
   261 	contentIDs.AppendL( KMPXMediaIdGeneral );
       
   262 	
       
   263 	CMPXMedia* media = CMPXMedia::NewL( contentIDs.Array() );
       
   264 	CleanupStack::PushL( media );
       
   265 	contentIDs.Close();
       
   266 	CleanupStack::PopAndDestroy(media);   */
       
   267 	}
       
   268 
       
   269 void CShowEngine::CompleteL(CHttpClient* /*aHttpClient*/, TInt aError)
       
   270 	{
       
   271 	if (iShowDownloading != NULL)
       
   272 		{
       
   273 		DP1("CShowEngine::Complete\tDownload of file: %S is complete", &iShowDownloading->FileName());		
       
   274 		// decide what kind of file this is		
       
   275 		if(aError != KErrCouldNotConnect)
       
   276 			{
       
   277 			if(aError == KErrDisconnected && !iPodcastModel.SettingsEngine().DownloadSuspended()) {
       
   278 				// no error if disconnect happened because of suspended downloading
       
   279 				iShowDownloading->SetLastError(aError);
       
   280 			}
       
   281 
       
   282 			if (aError == KErrNone)
       
   283 				{
       
   284 				TBuf<KMimeBufLength> mimeType;
       
   285 				GetMimeType(iShowDownloading->FileName(), mimeType);
       
   286 				_LIT(KMimeAudio,"audio");
       
   287 				_LIT(KMimeVideo,"video");
       
   288 				if (mimeType.Left(5) == KMimeAudio)
       
   289 					{
       
   290 					iShowDownloading->SetShowType(EAudioPodcast);
       
   291 					}
       
   292 				else if (mimeType.Left(5) == KMimeVideo)
       
   293 					{
       
   294 					iShowDownloading->SetShowType(EVideoPodcast);
       
   295 					}
       
   296 
       
   297 				iShowDownloading->SetDownloadState(EDownloaded);
       
   298 				DBUpdateShow(*iShowDownloading);
       
   299 				DBRemoveDownload(iShowDownloading->Uid());
       
   300 				AddShowToMpxCollection(*iShowDownloading);				
       
   301 				NotifyShowFinishedL(aError);
       
   302 
       
   303 				delete iShowDownloading;
       
   304 				iShowDownloading = NULL;
       
   305 				}
       
   306 			else
       
   307 				{
       
   308 				// 400 and 500 series errors are serious errors on which probably another download will fail
       
   309 				if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200)
       
   310 					{
       
   311 					iShowDownloading->SetDownloadState(EFailedDownload);
       
   312 					DBUpdateShow(*iShowDownloading);
       
   313 					DBRemoveDownload(iShowDownloading->Uid());
       
   314 					NotifyShowFinishedL(aError);
       
   315 
       
   316 					delete iShowDownloading;
       
   317 					iShowDownloading = NULL;
       
   318 					}
       
   319 				else // other kind of error, missing network etc, reque this show
       
   320 					{
       
   321 					iShowDownloading->SetDownloadState(EQueued);
       
   322 					DBUpdateShow(*iShowDownloading);
       
   323 					}
       
   324 
       
   325 				iDownloadErrors++;
       
   326 				if (iDownloadErrors > KMaxDownloadErrors)
       
   327 					{
       
   328 					DP("Too many downloading errors, suspending downloads");
       
   329 					iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
       
   330 					NotifyShowFinishedL(aError);
       
   331 					}
       
   332 				}
       
   333 			DownloadNextShowL();
       
   334 			}		
       
   335 
       
   336 		else
       
   337 			{
       
   338 			// Connection error
       
   339 			if(iShowDownloading)
       
   340 				{
       
   341 				iShowDownloading->SetDownloadState(EQueued);
       
   342 				DBUpdateShow(*iShowDownloading);
       
   343 				}
       
   344 			iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
       
   345 			NotifyShowFinishedL(aError);
       
   346 			}
       
   347 		}
       
   348 	}
       
   349 
       
   350 EXPORT_C CShowInfo* CShowEngine::ShowDownloading()
       
   351 	{
       
   352 	return iShowDownloading;
       
   353 	}
       
   354 
       
   355 EXPORT_C CShowInfo* CShowEngine::GetShowByUidL(TUint aShowUid)
       
   356 	{
       
   357 	return DBGetShowByUidL(aShowUid);
       
   358 	}
       
   359 CShowInfo* CShowEngine::DBGetShowByUidL(TUint aUid)
       
   360 	{
       
   361 	DP("CShowEngine::DBGetShowByUid");
       
   362 	CShowInfo *showInfo = NULL;
       
   363 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype from shows where uid=%u");
       
   364 	iSqlBuffer.Format(KSqlStatement, aUid);
       
   365 
       
   366 	sqlite3_stmt *st;
       
   367 
       
   368 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
       
   369 
       
   370 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   371 			&st, (const void**) NULL);
       
   372 
       
   373 	if (rc == SQLITE_OK)
       
   374 		{
       
   375 		rc = sqlite3_step(st);
       
   376 		Cleanup_sqlite3_finalize_PushL(st);
       
   377 		if (rc == SQLITE_ROW)
       
   378 			{
       
   379 			showInfo = CShowInfo::NewLC();
       
   380 			DBFillShowInfoFromStmtL(st, showInfo);
       
   381 			CleanupStack::Pop(showInfo);
       
   382 			}
       
   383 		CleanupStack::PopAndDestroy();//st
       
   384 		}
       
   385 
       
   386 	return showInfo;
       
   387 	}
       
   388 
       
   389 EXPORT_C CShowInfo* CShowEngine::DBGetShowByFileNameL(TFileName aFileName)
       
   390 	{
       
   391 	DP("CShowEngine::DBGetShowByUid");
       
   392 	CShowInfo *showInfo = NULL;
       
   393 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype from shows where filename=\"%S\"");
       
   394 	iSqlBuffer.Format(KSqlStatement, &aFileName);
       
   395 
       
   396 	sqlite3_stmt *st;
       
   397 
       
   398 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   399 			&st, (const void**) NULL);
       
   400 
       
   401 	if (rc == SQLITE_OK)
       
   402 		{
       
   403 		rc = sqlite3_step(st);
       
   404 		Cleanup_sqlite3_finalize_PushL(st);
       
   405 		if (rc == SQLITE_ROW)
       
   406 			{
       
   407 			showInfo = CShowInfo::NewLC();
       
   408 			DBFillShowInfoFromStmtL(st, showInfo);
       
   409 			CleanupStack::Pop(showInfo);
       
   410 			}
       
   411 		CleanupStack::PopAndDestroy();//st
       
   412 		}
       
   413 
       
   414 	return showInfo;
       
   415 	}
       
   416 
       
   417 void CShowEngine::DBGetAllShowsL(RShowInfoArray& aShowArray)
       
   418 	{
       
   419 	DP("CShowEngine::DBGetAllShows");
       
   420 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype from shows");
       
   421 	iSqlBuffer.Format(KSqlStatement);
       
   422 
       
   423 	sqlite3_stmt *st;
       
   424 
       
   425 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   426 			&st, (const void**) NULL);
       
   427 
       
   428 	if (rc == SQLITE_OK)
       
   429 		{
       
   430 		rc = sqlite3_step(st);
       
   431 		Cleanup_sqlite3_finalize_PushL(st);
       
   432 		while (rc == SQLITE_ROW)
       
   433 			{
       
   434 			CShowInfo* showInfo = CShowInfo::NewLC();
       
   435 			DBFillShowInfoFromStmtL(st, showInfo);
       
   436 			aShowArray.Append(showInfo);
       
   437 			CleanupStack::Pop(showInfo);
       
   438 			rc = sqlite3_step(st);
       
   439 			}
       
   440 		CleanupStack::PopAndDestroy();//st
       
   441 		}
       
   442 
       
   443 	}
       
   444 
       
   445 void CShowEngine::DBGetAllDownloadsL(RShowInfoArray& aShowArray)
       
   446 	{
       
   447 	DP("CShowEngine::DBGetAllDownloads");
       
   448 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, shows.uid, showsize, trackno, pubdate, showtype, lasterror from downloads, shows where downloads.uid=shows.uid");
       
   449 	iSqlBuffer.Format(KSqlStatement);
       
   450 
       
   451 #ifndef DONT_SORT_SQL
       
   452 	_LIT(KSqlSort, " order by dl_index");
       
   453 	iSqlBuffer.Append(KSqlSort);
       
   454 #endif
       
   455 	sqlite3_stmt *st;
       
   456 
       
   457 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   458 			&st, (const void**) NULL);
       
   459 
       
   460 	if (rc == SQLITE_OK)
       
   461 		{
       
   462 		rc = sqlite3_step(st);
       
   463 		Cleanup_sqlite3_finalize_PushL(st);
       
   464 		while (rc == SQLITE_ROW)
       
   465 			{
       
   466 			CShowInfo* showInfo = CShowInfo::NewLC();
       
   467 			DBFillShowInfoFromStmtL(st, showInfo);
       
   468 			aShowArray.Append(showInfo);
       
   469 			CleanupStack::Pop(showInfo);
       
   470 			rc = sqlite3_step(st);
       
   471 			}
       
   472 		CleanupStack::PopAndDestroy();//st
       
   473 		}
       
   474 
       
   475 	// delete downloads that don't have a show
       
   476 	
       
   477 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
       
   478 	iSqlBuffer.Format(KSqlStatement2);
       
   479 
       
   480 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,	&st, (const void**) NULL);
       
   481 
       
   482 	if (rc == SQLITE_OK)
       
   483 		{
       
   484 		rc = sqlite3_step(st);
       
   485 		sqlite3_finalize(st);
       
   486 		}
       
   487 	}
       
   488 
       
   489 CShowInfo* CShowEngine::DBGetNextDownloadL()
       
   490 	{
       
   491 	DP("CShowEngine::DBGetNextDownload");
       
   492 	CShowInfo *showInfo = NULL;
       
   493 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, shows.uid, showsize, trackno, pubdate, showtype, lasterror from downloads, shows where downloads.uid=shows.uid");
       
   494 	iSqlBuffer.Format(KSqlStatement);
       
   495 
       
   496 #ifdef DONT_SORT_SQL
       
   497 	_LIT(KSqlSort, " limit 1");
       
   498 	iSqlBuffer.Append(KSqlSort);
       
   499 #else
       
   500 	_LIT(KSqlNoSort, " order by dl_index limit 1");
       
   501 	iSqlBuffer.Append(KSqlNoSort);
       
   502 #endif
       
   503 
       
   504 	sqlite3_stmt *st;
       
   505 
       
   506 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   507 			&st, (const void**) NULL);
       
   508 
       
   509 	if (rc == SQLITE_OK)
       
   510 		{
       
   511 		rc = sqlite3_step(st);
       
   512 		Cleanup_sqlite3_finalize_PushL(st);
       
   513 		if (rc == SQLITE_ROW)
       
   514 			{
       
   515 			showInfo = CShowInfo::NewLC();
       
   516 			DBFillShowInfoFromStmtL(st, showInfo);
       
   517 			CleanupStack::Pop(showInfo);
       
   518 			}
       
   519 		CleanupStack::PopAndDestroy();//st
       
   520 		}
       
   521 
       
   522 	return showInfo;
       
   523 	}
       
   524 
       
   525 void CShowEngine::DBGetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid)
       
   526 	{
       
   527 	DP1("CShowEngine::DBGetShowsByFeed BEGIN, feedUid=%u", aFeedUid);
       
   528 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype, lasterror from shows where feeduid=%u");
       
   529 	iSqlBuffer.Format(KSqlStatement, aFeedUid);
       
   530 
       
   531 #ifndef DONT_SORT_SQL	
       
   532 	_LIT(KSqlOrderByDate, " order by pubdate desc");
       
   533 	iSqlBuffer.Append(KSqlOrderByDate);
       
   534 #endif
       
   535 
       
   536 	sqlite3_stmt *st;
       
   537 
       
   538 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   539 			&st, (const void**) NULL);
       
   540 
       
   541 	if (rc == SQLITE_OK)
       
   542 		{
       
   543 		rc = sqlite3_step(st);
       
   544 		Cleanup_sqlite3_finalize_PushL(st);
       
   545 		while (rc == SQLITE_ROW)
       
   546 			{
       
   547 			CShowInfo* showInfo = CShowInfo::NewLC();
       
   548 			DBFillShowInfoFromStmtL(st, showInfo);
       
   549 			aShowArray.Append(showInfo);
       
   550 			CleanupStack::Pop(showInfo);
       
   551 			rc = sqlite3_step(st);
       
   552 			}
       
   553 		CleanupStack::PopAndDestroy();//st
       
   554 		}
       
   555 	DP("CShowEngine::DBGetShowsByFeed END");
       
   556 	}
       
   557 
       
   558 TUint CShowEngine::DBGetDownloadsCount()
       
   559 	{
       
   560 	DP("CShowEngine::DBGetDownloadsCount");
       
   561 
       
   562 	_LIT(KSqlStatement, "select count(*) from downloads");
       
   563 	iSqlBuffer.Format(KSqlStatement);
       
   564 
       
   565 	sqlite3_stmt *st;
       
   566 	TUint count = 0;
       
   567 
       
   568 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   569 			&st, (const void**) NULL);
       
   570 
       
   571 	if (rc == SQLITE_OK)
       
   572 		{
       
   573 		rc = sqlite3_step(st);
       
   574 
       
   575 		if (rc == SQLITE_ROW)
       
   576 			{
       
   577 			count = sqlite3_column_int(st, 0);
       
   578 			}
       
   579 		sqlite3_finalize(st);
       
   580 		}
       
   581 	return count;
       
   582 	}
       
   583 
       
   584 void CShowEngine::DBGetDownloadedShowsL(RShowInfoArray& aShowArray)
       
   585 	{
       
   586 	DP("CShowEngine::DBGetDownloadedShows");
       
   587 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype, lasterror from shows where downloadstate=%u");
       
   588 	iSqlBuffer.Format(KSqlStatement, EDownloaded);
       
   589 
       
   590 #ifndef DONT_SORT_SQL
       
   591 	_LIT(KSqlSort, " order by title");
       
   592 	iSqlBuffer.Append(KSqlSort);
       
   593 #endif	
       
   594 
       
   595 	sqlite3_stmt *st;
       
   596 
       
   597 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   598 			&st, (const void**) NULL);
       
   599 
       
   600 	if (rc == SQLITE_OK)
       
   601 		{
       
   602 		rc = sqlite3_step(st);
       
   603 		Cleanup_sqlite3_finalize_PushL(st);
       
   604 		while (rc == SQLITE_ROW)
       
   605 			{
       
   606 			CShowInfo* showInfo = CShowInfo::NewLC();
       
   607 			DBFillShowInfoFromStmtL(st, showInfo);
       
   608 			aShowArray.Append(showInfo);
       
   609 			CleanupStack::Pop(showInfo);
       
   610 			rc = sqlite3_step(st);
       
   611 			}
       
   612 		CleanupStack::PopAndDestroy();//st
       
   613 		}
       
   614 	}
       
   615 
       
   616 void CShowEngine::DBGetNewShowsL(RShowInfoArray& aShowArray)
       
   617 	{
       
   618 	DP("CShowEngine::DBGetNewShows");
       
   619 	_LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype, lasterror from shows where playstate=%u");
       
   620 	iSqlBuffer.Format(KSqlStatement, ENeverPlayed);
       
   621 
       
   622 	sqlite3_stmt *st;
       
   623 
       
   624 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   625 			&st, (const void**) NULL);
       
   626 
       
   627 	if (rc == SQLITE_OK)
       
   628 		{
       
   629 		rc = sqlite3_step(st);
       
   630 		Cleanup_sqlite3_finalize_PushL(st);
       
   631 		while (rc == SQLITE_ROW)
       
   632 			{
       
   633 			CShowInfo* showInfo = CShowInfo::NewLC();
       
   634 			DBFillShowInfoFromStmtL(st, showInfo);
       
   635 			aShowArray.Append(showInfo);
       
   636 			CleanupStack::Pop(showInfo);
       
   637 			rc = sqlite3_step(st);
       
   638 			}
       
   639 		CleanupStack::PopAndDestroy();//st
       
   640 		}
       
   641 	}
       
   642 
       
   643 void CShowEngine::DBDeleteOldShowsByFeed(TUint aFeedUid)
       
   644 	{
       
   645 	DP("CShowEngine::DBDeleteOldShows");
       
   646 	
       
   647 	// what we do:
       
   648 	// 1. sort shows by pubdate
       
   649 	// 2. select the first MaxListItems shows
       
   650 	// 3. delete the rest if downloadstate is ENotDownloaded
       
   651 	
       
   652 	_LIT(KSqlStatement,"delete from shows where feeduid=%u and downloadstate=0 and uid not in " \
       
   653 			"(select uid from shows where feeduid=%u order by pubdate desc limit %u)");
       
   654 	iSqlBuffer.Format(KSqlStatement, aFeedUid, aFeedUid, iPodcastModel.SettingsEngine().MaxListItems());
       
   655 
       
   656 	sqlite3_stmt *st;
       
   657 
       
   658 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   659 			&st, (const void**) NULL);
       
   660 
       
   661 	if (rc == SQLITE_OK)
       
   662 		{
       
   663 		rc = sqlite3_step(st);
       
   664 		sqlite3_finalize(st);
       
   665 		}
       
   666 	
       
   667 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
       
   668 	iSqlBuffer.Format(KSqlStatement2);
       
   669 
       
   670 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,	&st, (const void**) NULL);
       
   671 
       
   672 	if (rc == SQLITE_OK)
       
   673 		{
       
   674 		rc = sqlite3_step(st);
       
   675 		sqlite3_finalize(st);
       
   676 		}
       
   677 	}
       
   678 
       
   679 void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo)
       
   680 	{
       
   681 	const void *urlz = sqlite3_column_text16(st, 0);
       
   682 	TPtrC16 url((const TUint16*) urlz);
       
   683 	showInfo->SetUrlL(url);
       
   684 
       
   685 	const void *titlez = sqlite3_column_text16(st, 1);
       
   686 	TPtrC16 title((const TUint16*) titlez);
       
   687 	showInfo->SetTitleL(title);
       
   688 
       
   689 	const void *descz = sqlite3_column_text16(st, 2);
       
   690 	TPtrC16 desc((const TUint16*) descz);
       
   691 	showInfo->SetDescriptionL(desc);
       
   692 
       
   693 	const void *filez = sqlite3_column_text16(st, 3);
       
   694 	TPtrC16 file((const TUint16*) filez);
       
   695 	showInfo->SetFileNameL(file);
       
   696 
       
   697 	sqlite3_int64 pos = sqlite3_column_int64(st, 4);
       
   698 	TTimeIntervalMicroSeconds position(pos);
       
   699 	showInfo->SetPosition(position);
       
   700 
       
   701 	TUint playtime = sqlite3_column_int(st, 5);
       
   702 	showInfo->SetPlayTime(playtime);
       
   703 
       
   704 	TUint playstate = sqlite3_column_int(st, 6);
       
   705 	showInfo->SetPlayState((TPlayState) playstate);
       
   706 
       
   707 	TUint downloadstate = sqlite3_column_int(st, 7);
       
   708 	showInfo->SetDownloadState((TDownloadState) downloadstate);
       
   709 
       
   710 	TUint feeduid = sqlite3_column_int(st, 8);
       
   711 	showInfo->SetFeedUid(feeduid);
       
   712 
       
   713 	TUint uid = sqlite3_column_int(st, 9);
       
   714 	showInfo->SetUid(uid);
       
   715 
       
   716 	TUint showsize = sqlite3_column_int(st, 10);
       
   717 	showInfo->SetShowSize(showsize);
       
   718 
       
   719 	TUint trackno = sqlite3_column_int(st, 11);
       
   720 	showInfo->SetTrackNo((TShowType) trackno);
       
   721 
       
   722 	sqlite3_int64 pubdate = sqlite3_column_int64(st, 12);
       
   723 	TTime timepubdate(pubdate);
       
   724 	showInfo->SetPubDate(timepubdate);
       
   725 
       
   726 	TUint showtype = sqlite3_column_int(st, 13);
       
   727 	showInfo->SetShowType((TShowType) showtype);
       
   728 	
       
   729 	TInt lasterror = sqlite3_column_int(st, 14);
       
   730 	showInfo->SetLastError(lasterror);
       
   731 	}
       
   732 
       
   733 TBool CShowEngine::DBAddShow(const CShowInfo& aItem)
       
   734 	{
       
   735 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
       
   736 
       
   737 	_LIT(KSqlStatement, "insert into shows (url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype)"
       
   738 			" values (\"%S\",\"%S\", \"%S\", \"%S\", \"%Lu\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\", \"%Lu\", \"%d\")");
       
   739 	iSqlBuffer.Format(KSqlStatement, &aItem.Url(), &aItem.Title(), &aItem.Description(),
       
   740 			&aItem.FileName(), aItem.Position().Int64(), aItem.PlayTime(),
       
   741 			aItem.PlayState(), aItem.DownloadState(), aItem.FeedUid(),
       
   742 			aItem.Uid(), aItem.ShowSize(), aItem.TrackNo(),
       
   743 			aItem.PubDate().Int64(), aItem.ShowType());
       
   744 
       
   745 	sqlite3_stmt *st;
       
   746 
       
   747 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   748 			&st, (const void**) NULL);
       
   749 	if (rc == SQLITE_OK)
       
   750 		{
       
   751 		rc = sqlite3_step(st);
       
   752 		if (rc == SQLITE_DONE)
       
   753 			{
       
   754 			sqlite3_finalize(st);
       
   755 			return ETrue;
       
   756 			}
       
   757 		else
       
   758 			{
       
   759 			sqlite3_finalize(st);
       
   760 			}
       
   761 		}
       
   762 	else
       
   763 		{
       
   764 		DP1("SQLite rc=%d", rc);
       
   765 		}
       
   766 
       
   767 	return EFalse;
       
   768 	}
       
   769 
       
   770 void CShowEngine::DBAddDownload(TUint aUid)
       
   771 	{
       
   772 	DP1("CShowEngine::DBAddDownload, aUid=%u", aUid);
       
   773 
       
   774 	_LIT(KSqlStatement, "insert into downloads (uid) values (%u)");
       
   775 	iSqlBuffer.Format(KSqlStatement, aUid);
       
   776 	sqlite3_stmt *st;
       
   777 
       
   778 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   779 			&st, (const void**) NULL);
       
   780 
       
   781 	if (rc == SQLITE_OK)
       
   782 		{
       
   783 		rc = sqlite3_step(st);
       
   784 		}
       
   785 
       
   786 	sqlite3_finalize(st);
       
   787 	}
       
   788 
       
   789 TBool CShowEngine::DBUpdateShow(CShowInfo& aItem)
       
   790 	{
       
   791 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
       
   792 
       
   793 	_LIT(KSqlStatement, "update shows set url=\"%S\", title=\"%S\", description=\"%S\", filename=\"%S\", position=\"%Lu\","
       
   794 			"playtime=\"%u\", playstate=\"%u\", downloadstate=\"%u\", feeduid=\"%u\", showsize=\"%u\", trackno=\"%u\","
       
   795 			"pubdate=\"%Lu\", showtype=\"%d\", lasterror=\"%d\" where uid=\"%u\"");
       
   796 	iSqlBuffer.Format(KSqlStatement, &aItem.Url(), &aItem.Title(), &aItem.Description(),
       
   797 			&aItem.FileName(), aItem.Position().Int64(), aItem.PlayTime(),
       
   798 			aItem.PlayState(), aItem.DownloadState(), aItem.FeedUid(),
       
   799 			aItem.ShowSize(), aItem.TrackNo(), aItem.PubDate().Int64(),
       
   800 			aItem.ShowType(), aItem.LastError(), aItem.Uid());
       
   801 
       
   802 	sqlite3_stmt *st;
       
   803 
       
   804 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   805 			&st, (const void**) NULL);
       
   806 
       
   807 	if (rc == SQLITE_OK)
       
   808 		{
       
   809 		rc = sqlite3_step(st);
       
   810 
       
   811 		if (rc == SQLITE_DONE)
       
   812 			{
       
   813 			sqlite3_finalize(st);
       
   814 			return ETrue;
       
   815 			}
       
   816 		else
       
   817 			{
       
   818 			sqlite3_finalize(st);
       
   819 			}
       
   820 		}
       
   821 	else
       
   822 		{
       
   823 		DP1("SQLite rc=%d", rc);
       
   824 		}
       
   825 
       
   826 	return EFalse;
       
   827 	}
       
   828 
       
   829 TBool CShowEngine::DBDeleteShow(TUint aUid)
       
   830 	{
       
   831 	DP("CShowEngine::DBDeleteShow");
       
   832 
       
   833 	_LIT(KSqlStatement, "delete from shows where uid=%u");
       
   834 	iSqlBuffer.Format(KSqlStatement, aUid);
       
   835 
       
   836 	sqlite3_stmt *st;
       
   837 
       
   838 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
       
   839 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   840 			&st, (const void**) NULL);
       
   841 
       
   842 	if (rc == SQLITE_OK)
       
   843 		{
       
   844 		rc = sqlite3_step(st);
       
   845 
       
   846 		if (rc == SQLITE_DONE)
       
   847 			{
       
   848 			sqlite3_finalize(st);
       
   849 			return ETrue;
       
   850 			}
       
   851 		else
       
   852 			{
       
   853 			sqlite3_finalize(st);
       
   854 			}
       
   855 		}
       
   856 	else
       
   857 		{
       
   858 		DP1("SQLite rc=%d", rc);
       
   859 		}
       
   860 
       
   861 	return EFalse;
       
   862 	}
       
   863 
       
   864 TBool CShowEngine::DBDeleteAllShowsByFeed(TUint aFeedUid)
       
   865 	{
       
   866 	DP("CShowEngine::DBDeleteAllShowsByFeed");
       
   867 
       
   868 	_LIT(KSqlStatement, "delete from shows where feeduid=%u");
       
   869 	iSqlBuffer.Format(KSqlStatement, aFeedUid);
       
   870 
       
   871 	sqlite3_stmt *st;
       
   872 
       
   873 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
       
   874 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   875 			&st, (const void**) NULL);
       
   876 
       
   877 	if (rc == SQLITE_OK)
       
   878 		{
       
   879 		rc = sqlite3_step(st);
       
   880 
       
   881 		if (rc == SQLITE_DONE)
       
   882 			{
       
   883 			sqlite3_finalize(st);
       
   884 			return ETrue;
       
   885 			}
       
   886 		else
       
   887 			{
       
   888 			sqlite3_finalize(st);
       
   889 			}
       
   890 		}
       
   891 	else
       
   892 		{
       
   893 		DP1("SQLite rc=%d", rc);
       
   894 		}
       
   895 
       
   896 	return EFalse;
       
   897 	}
       
   898 
       
   899 void CShowEngine::DBRemoveAllDownloads()
       
   900 	{
       
   901 	DP("CShowEngine::DBRemoveAllDownloads");
       
   902 
       
   903 	_LIT(KSqlStatement, "delete from downloads");
       
   904 	iSqlBuffer.Format(KSqlStatement);
       
   905 
       
   906 	sqlite3_stmt *st;
       
   907 
       
   908 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   909 			&st, (const void**) NULL);
       
   910 
       
   911 	if (rc == SQLITE_OK)
       
   912 		{
       
   913 		rc = sqlite3_step(st);
       
   914 		sqlite3_finalize(st);
       
   915 		}
       
   916 
       
   917 	_LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1");
       
   918 	iSqlBuffer.Format(KSqlStatement2);
       
   919 
       
   920 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st,
       
   921 			(const void**) NULL);
       
   922 
       
   923 	if (rc == SQLITE_OK)
       
   924 		{
       
   925 		rc = sqlite3_step(st);
       
   926 		sqlite3_finalize(st);
       
   927 		}
       
   928 
       
   929 	}
       
   930 
       
   931 void CShowEngine::DBRemoveDownload(TUint aUid)
       
   932 	{
       
   933 	DP("CShowEngine::DBRemoveDownload");
       
   934 
       
   935 	_LIT(KSqlStatement, "delete from downloads where uid=%u");
       
   936 	iSqlBuffer.Format(KSqlStatement, aUid);
       
   937 
       
   938 	sqlite3_stmt *st;
       
   939 
       
   940 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
       
   941 			&st, (const void**) NULL);
       
   942 
       
   943 	if (rc == SQLITE_OK)
       
   944 		{
       
   945 		rc = sqlite3_step(st);
       
   946 		sqlite3_finalize(st);
       
   947 		}
       
   948 	}
       
   949 
       
   950 EXPORT_C CShowInfo* CShowEngine::GetNextShowByTrackL(CShowInfo* aShowInfo)
       
   951 	{
       
   952 	CShowInfo* nextShow = NULL;
       
   953 	RShowInfoArray array;
       
   954 	DBGetShowsByFeedL(array, aShowInfo->FeedUid());
       
   955 	TUint diff = KMaxTInt;
       
   956 	for (TInt loop = 0; loop < array.Count(); loop++)
       
   957 		{
       
   958 		if (aShowInfo->TrackNo() < array[loop]->TrackNo())
       
   959 			{
       
   960 			if ((array[loop]->TrackNo() - aShowInfo->TrackNo()) < diff)
       
   961 				{
       
   962 				diff = array[loop]->TrackNo() - aShowInfo->TrackNo();
       
   963 				nextShow = array[loop];
       
   964 				}
       
   965 			}
       
   966 		}
       
   967 	array.ResetAndDestroy();
       
   968 	return nextShow;
       
   969 	}
       
   970 
       
   971 TBool CShowEngine::CompareShowsByUid(const CShowInfo &a, const CShowInfo &b)
       
   972 	{
       
   973 	return a.Uid() == b.Uid();
       
   974 	}
       
   975 
       
   976 TInt CShowEngine::CompareShowsByDate(const CShowInfo &a, const CShowInfo &b)
       
   977 	{
       
   978 	if (a.PubDate() > b.PubDate())
       
   979 		{
       
   980 		//		DP2("Sorting %S less than %S", &a.iTitle, &b.iTitle);
       
   981 		return -1;
       
   982 		}
       
   983 	else if (a.PubDate() == b.PubDate())
       
   984 		{
       
   985 		//		DP2("Sorting %S equal to %S", &a.iTitle, &b.iTitle);
       
   986 		return 0;
       
   987 		}
       
   988 	else
       
   989 		{
       
   990 		//		DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle);
       
   991 		return 1;
       
   992 		}
       
   993 	}
       
   994 
       
   995 TInt CShowEngine::CompareShowsByTrackNo(const CShowInfo &a, const CShowInfo &b)
       
   996 	{
       
   997 	if (a.TrackNo() < b.TrackNo())
       
   998 		{
       
   999 		return -1;
       
  1000 		}
       
  1001 	else if (a.TrackNo() == b.TrackNo())
       
  1002 		{
       
  1003 		return 0;
       
  1004 		}
       
  1005 	else
       
  1006 		{
       
  1007 		return 1;
       
  1008 		}
       
  1009 	}
       
  1010 
       
  1011 TInt CShowEngine::CompareShowsByTitle(const CShowInfo &a, const CShowInfo &b)
       
  1012 	{
       
  1013 	if (a.Title() < b.Title())
       
  1014 		{
       
  1015 		//		DP2("Sorting %S less than %S", &a.iTitle, &b.iTitle);
       
  1016 		return -1;
       
  1017 		}
       
  1018 	else if (a.Title() == b.Title())
       
  1019 		{
       
  1020 		//		DP2("Sorting %S equal to %S", &a.iTitle, &b.iTitle);
       
  1021 		return 0;
       
  1022 		}
       
  1023 	else
       
  1024 		{
       
  1025 		//		DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle);
       
  1026 		return 1;
       
  1027 		}
       
  1028 	}
       
  1029 
       
  1030 EXPORT_C void CShowEngine::DeletePlayedShows(RShowInfoArray &aShowInfoArray)
       
  1031 	{
       
  1032 	for (TInt i = 0; i < aShowInfoArray.Count(); i++)
       
  1033 		{
       
  1034 		if (aShowInfoArray[i]->PlayState() == EPlayed
       
  1035 				&& aShowInfoArray[i]->FileName().Length() > 0)
       
  1036 			{
       
  1037 			if (CompareShowsByUid(*(iPodcastModel.PlayingPodcast()), *(aShowInfoArray[i]))
       
  1038 					&& iPodcastModel.SoundEngine().State() != ESoundEngineNotInitialized)
       
  1039 				{
       
  1040 				iPodcastModel.SoundEngine().Stop();
       
  1041 				}
       
  1042 			BaflUtils::DeleteFile(iPodcastModel.FsSession(), aShowInfoArray[i]->FileName());
       
  1043 			aShowInfoArray[i]->SetDownloadState(ENotDownloaded);
       
  1044 			DBUpdateShow(*aShowInfoArray[i]);
       
  1045 			}
       
  1046 		}
       
  1047 	}
       
  1048 
       
  1049 EXPORT_C void CShowEngine::DeleteAllShowsByFeedL(TUint aFeedUid, TBool aDeleteFiles)
       
  1050 	{
       
  1051 	RShowInfoArray array;
       
  1052 	DBGetShowsByFeedL(array, aFeedUid);
       
  1053 
       
  1054 	const TInt count = array.Count();
       
  1055 
       
  1056 	for (TInt i = count - 1; i >= 0; i--)
       
  1057 		{
       
  1058 		if (array[i]->FileName().Length() > 0)
       
  1059 			{
       
  1060 			if (aDeleteFiles)
       
  1061 				{
       
  1062 				BaflUtils::DeleteFile(iPodcastModel.FsSession(), array[i]->FileName());
       
  1063 				}
       
  1064 			}
       
  1065 		}
       
  1066 	array.ResetAndDestroy();
       
  1067 	DBDeleteAllShowsByFeed(aFeedUid);
       
  1068 	}
       
  1069 
       
  1070 EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid)
       
  1071 	{
       
  1072 		DBDeleteOldShowsByFeed(aFeedUid);
       
  1073 	}
       
  1074 
       
  1075 EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile)
       
  1076 	{
       
  1077 
       
  1078 	CShowInfo *info = DBGetShowByUidL(aShowUid);
       
  1079 
       
  1080 	if (info != NULL)
       
  1081 		{
       
  1082 		if (info->FileName().Length() > 0 && aRemoveFile)
       
  1083 			{
       
  1084 			BaflUtils::DeleteFile(iPodcastModel.FsSession(), info->FileName());
       
  1085 			}
       
  1086 		
       
  1087 		info->SetDownloadState(ENotDownloaded);
       
  1088 		DBUpdateShow(*info);
       
  1089 		delete info;
       
  1090 		}
       
  1091 	}
       
  1092 
       
  1093 EXPORT_C void CShowEngine::GetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid)
       
  1094 	{
       
  1095 	DP("CShowEngine::GetShowsByFeed");
       
  1096 	DBGetShowsByFeedL(aShowArray, aFeedUid);
       
  1097 	}
       
  1098 
       
  1099 EXPORT_C void CShowEngine::GetAllShowsL(RShowInfoArray &aArray)
       
  1100 	{
       
  1101 	DP("CShowEngine::GetAllShows");
       
  1102 	DBGetAllShowsL(aArray);
       
  1103 	}
       
  1104 
       
  1105 EXPORT_C void CShowEngine::GetShowsDownloadedL(RShowInfoArray &aArray)
       
  1106 	{
       
  1107 	DP("CShowEngine::GetShowsDownloaded");
       
  1108 	DBGetDownloadedShowsL(aArray);
       
  1109 	}
       
  1110 
       
  1111 EXPORT_C void CShowEngine::GetNewShowsL(RShowInfoArray &aArray)
       
  1112 	{
       
  1113 	DP("CShowEngine::GetNewShows");
       
  1114 	DBGetNewShowsL(aArray);
       
  1115 	}
       
  1116 
       
  1117 EXPORT_C void CShowEngine::GetShowsDownloadingL(RShowInfoArray &aArray)
       
  1118 	{
       
  1119 	DP("CShowEngine::GetShowsDownloading");
       
  1120 	DBGetAllDownloadsL(aArray);
       
  1121 	}
       
  1122 
       
  1123 EXPORT_C TInt CShowEngine::GetNumDownloadingShows()
       
  1124 	{
       
  1125 	return (const TInt) DBGetDownloadsCount();
       
  1126 	}
       
  1127 
       
  1128 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo)
       
  1129 	{
       
  1130 	aInfo.SetDownloadState(EQueued);
       
  1131 	DBUpdateShow(aInfo);
       
  1132 	DBAddDownload(aInfo.Uid());
       
  1133 	DownloadNextShowL();
       
  1134 	}
       
  1135 
       
  1136 void CShowEngine::DownloadNextShowL()
       
  1137 	{
       
  1138 	DP("CShowEngine::DownloadNextShowL BEGIN");
       
  1139 	// Check if we have anything in the download queue
       
  1140 	const TInt count = DBGetDownloadsCount();
       
  1141 	DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count);
       
  1142 
       
  1143 	// Inform the observers
       
  1144 	NotifyDownloadQueueUpdatedL();
       
  1145 
       
  1146 	if (count > 0)
       
  1147 		{
       
  1148 		if (iPodcastModel.SettingsEngine().DownloadSuspended())
       
  1149 			{
       
  1150 			DP("CShowEngine::DownloadNextShow\tDownload process is suspended, ABORTING");
       
  1151 			return;
       
  1152 			}
       
  1153 		else if (iShowClient->IsActive())
       
  1154 			{
       
  1155 			DP("CShowEngine::DownloadNextShow\tDownload process is already active.");
       
  1156 			return;
       
  1157 			}
       
  1158 		else
       
  1159 			{
       
  1160 
       
  1161 			// Start the download
       
  1162 			
       
  1163 			CShowInfo *info = DBGetNextDownloadL();
       
  1164 			
       
  1165 			while(info != NULL)
       
  1166 				{
       
  1167 				TBool getOk = EFalse;
       
  1168 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
       
  1169 				info->SetDownloadState(EDownloading);
       
  1170 				info->SetLastError(KErrNone);
       
  1171 				DBUpdateShow(*info);
       
  1172 				iShowDownloading = info;
       
  1173 				TRAPD(error,getOk = GetShowL(info));
       
  1174 				if (error != KErrNone || !getOk)
       
  1175 					{
       
  1176 					info->SetDownloadState(EFailedDownload);
       
  1177 					DBRemoveDownload(info->Uid());
       
  1178 					DBUpdateShow(*info);
       
  1179 					info = DBGetNextDownloadL();
       
  1180 					
       
  1181 					if(info == NULL)
       
  1182 						{
       
  1183 						iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
       
  1184 						iShowDownloading = NULL;
       
  1185 						}
       
  1186 					}				
       
  1187 				else
       
  1188 					{
       
  1189 					break;
       
  1190 					}
       
  1191 				}
       
  1192 			}
       
  1193 		}
       
  1194 	else
       
  1195 		{
       
  1196 		iShowDownloading = NULL;DP("CShowEngine::DownloadNextShow\tNothing to download");
       
  1197 		}
       
  1198 	DP("CShowEngine::DownloadNextShowL END");
       
  1199 	}
       
  1200 
       
  1201 void CShowEngine::NotifyDownloadQueueUpdatedL()
       
  1202 	{
       
  1203 	const TInt count = iObservers.Count();
       
  1204 	for (TInt i = 0; i < count; i++)
       
  1205 		{
       
  1206 		iObservers[i]->DownloadQueueUpdatedL(1, DBGetDownloadsCount() - 1);
       
  1207 		}
       
  1208 	}
       
  1209 
       
  1210 void CShowEngine::NotifyShowDownloadUpdatedL(TInt aBytesOfCurrentDownload, TInt aBytesTotal)
       
  1211 	{
       
  1212 	const TInt count = iObservers.Count();
       
  1213 	for (TInt i = 0; i < count; i++)
       
  1214 		{
       
  1215 			iObservers[i]->ShowDownloadUpdatedL(aBytesOfCurrentDownload, aBytesTotal);
       
  1216 		}
       
  1217 	}
       
  1218 
       
  1219 void CShowEngine::NotifyShowFinishedL(TInt aError)
       
  1220 	{
       
  1221 	const TInt count = iObservers.Count();
       
  1222 		for (TInt i = 0; i < count; i++)
       
  1223 			{
       
  1224 				iObservers[i]->ShowDownloadFinishedL(iShowDownloading?iShowDownloading->Uid():0, aError);
       
  1225 			}
       
  1226 	}
       
  1227 
       
  1228 EXPORT_C void CShowEngine::NotifyShowListUpdatedL()
       
  1229 	{
       
  1230 	for (TInt i = 0; i < iObservers.Count(); i++)
       
  1231 		{
       
  1232 		iObservers[i]->ShowListUpdatedL();
       
  1233 		}
       
  1234 	}
       
  1235 
       
  1236 void CShowEngine::ReadMetaData(CShowInfo& aShowInfo)
       
  1237 	{
       
  1238 	//DP1("Read %S", &(aShowInfo->Title()));
       
  1239 	DBUpdateShow(aShowInfo);
       
  1240 	}
       
  1241 
       
  1242 void CShowEngine::ReadMetaDataCompleteL()
       
  1243 	{
       
  1244 	NotifyShowListUpdatedL();
       
  1245 	MetaDataReader().SetIgnoreTrackNo(EFalse);
       
  1246 	}
       
  1247 
       
  1248 EXPORT_C void CShowEngine::UpdateShow(CShowInfo& aInfo)
       
  1249 	{
       
  1250 	DBUpdateShow(aInfo);
       
  1251 	}
       
  1252 
       
  1253 EXPORT_C CMetaDataReader& CShowEngine::MetaDataReader()
       
  1254 	{
       
  1255 	return *iMetaDataReader;
       
  1256 	}
       
  1257 
       
  1258 void CShowEngine::FileError(TUint /*aError*/)
       
  1259 	{
       
  1260 	//TODO: Error dialog
       
  1261 	//StopDownloads();
       
  1262 	iDownloadErrors = KMaxDownloadErrors;
       
  1263 	}