engine/src/ShowEngine.cpp
changeset 111 12c59f14a031
parent 109 223f270fa7ff
child 112 0bd6b9a3f027
equal deleted inserted replaced
110:403412eb5292 111:12c59f14a031
   110 	if (!iPodcastModel.SettingsEngine().DownloadSuspended())
   110 	if (!iPodcastModel.SettingsEngine().DownloadSuspended())
   111 		{
   111 		{
   112 		SuspendDownloads();
   112 		SuspendDownloads();
   113 		}
   113 		}
   114 
   114 
   115 	DBRemoveAllDownloads();
   115 	DBRemoveAllDownloadsL();
   116 	NotifyDownloadQueueUpdatedL();
   116 	NotifyDownloadQueueUpdatedL();
   117 	}
   117 	}
   118 
   118 
   119 EXPORT_C TBool CShowEngine::RemoveDownloadL(TUint aUid)
   119 EXPORT_C void CShowEngine::RemoveDownloadL(TUint aUid)
   120 	{
   120 	{
   121 	DP("CShowEngine::RemoveDownload\t Trying to remove download");
   121 	DP("CShowEngine::RemoveDownloadL BEGIN");
   122 
   122 
   123 	TBool retVal = EFalse;
       
   124 	TBool resumeAfterRemove = EFalse;
   123 	TBool resumeAfterRemove = EFalse;
   125 	// if trying to remove the present download, we first stop it
   124 	// if trying to remove the present download, we first stop it
   126 	if (!iPodcastModel.SettingsEngine().DownloadSuspended() && iShowDownloading != NULL
   125 	if (!iPodcastModel.SettingsEngine().DownloadSuspended() && iShowDownloading != NULL
   127 			&& iShowDownloading->Uid() == aUid)
   126 			&& iShowDownloading->Uid() == aUid)
   128 		{
   127 		{
   136 		{
   135 		{
   137 		info->SetDownloadState(ENotDownloaded);
   136 		info->SetDownloadState(ENotDownloaded);
   138 		DBUpdateShowL(*info);
   137 		DBUpdateShowL(*info);
   139 		delete info;
   138 		delete info;
   140 		}
   139 		}
   141 	DBRemoveDownload(aUid);
   140 	
       
   141 	DBRemoveDownloadL(aUid);
   142 
   142 
   143 	// partial downloads should be removed
   143 	// partial downloads should be removed
   144 	if (iShowDownloading)
   144 	if (iShowDownloading)
   145 		{
   145 		{
   146 		BaflUtils::DeleteFile(iPodcastModel.FsSession(), iShowDownloading->FileName());
   146 		BaflUtils::DeleteFile(iPodcastModel.FsSession(), iShowDownloading->FileName());
   152 	if (resumeAfterRemove) {
   152 	if (resumeAfterRemove) {
   153 		ResumeDownloadsL();
   153 		ResumeDownloadsL();
   154 	}
   154 	}
   155 	
   155 	
   156 	DownloadNextShowL();
   156 	DownloadNextShowL();
   157 	retVal = ETrue;
   157 	DP("CShowEngine::RemoveDownloadL END");
   158 
       
   159 	return retVal;
       
   160 	}
   158 	}
   161 
   159 
   162 void CShowEngine::Connected(CHttpClient* /*aClient*/)
   160 void CShowEngine::Connected(CHttpClient* /*aClient*/)
   163 	{
   161 	{
   164 
   162 
   183 		{
   181 		{
   184 		iShowDownloading->SetShowSize(aTotalBytes);
   182 		iShowDownloading->SetShowSize(aTotalBytes);
   185 		}
   183 		}
   186 	}
   184 	}
   187 
   185 
   188 TBool CShowEngine::GetShowL(CShowInfo *info)
   186 void CShowEngine::GetShowL(CShowInfo *info)
   189 	{
   187 	{
   190 	CFeedInfo *feedInfo = iPodcastModel.FeedEngine().GetFeedInfoByUid(
   188 	CFeedInfo *feedInfo = iPodcastModel.FeedEngine().GetFeedInfoByUid(
   191 			info->FeedUid());
   189 			info->FeedUid());
   192 	if (feedInfo == NULL)
   190 	
   193 		{
       
   194 		DP("Feed not found for this show!");
       
   195 		return EFalse;
       
   196 		}
       
   197 
       
   198 	TFileName filePath;
   191 	TFileName filePath;
   199 	filePath.Copy(iPodcastModel.SettingsEngine().BaseDir());
   192 	filePath.Copy(iPodcastModel.SettingsEngine().BaseDir());
   200 	
   193 	
   201 	TFileName relPath;
   194 	TFileName relPath;
   202 	relPath.Copy(feedInfo->Title());
   195 	relPath.Copy(feedInfo->Title());
   209 
   202 
   210 	// complete file path is base dir + rel path
   203 	// complete file path is base dir + rel path
   211 	filePath.Append(relPath);
   204 	filePath.Append(relPath);
   212 	info->SetFileNameL(filePath);
   205 	info->SetFileNameL(filePath);
   213 
   206 
   214 	return iShowClient->GetL(info->Url(), filePath);
   207 	User::LeaveIfError(iShowClient->GetL(info->Url(), filePath));
   215 	}
   208 	}
   216 
   209 
   217 EXPORT_C TBool CShowEngine::AddShowL(const CShowInfo& aItem)
   210 EXPORT_C void CShowEngine::AddShowL(const CShowInfo& aItem)
   218 	{
   211 	{
   219 	DP1("CShowEngine::AddShowL, title=%S", &aItem.Title());
   212 	DP1("CShowEngine::AddShowL, title=%S", &aItem.Title());
   220 	CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid());
   213 	CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid());
   221 
   214 
   222 	if (showInfo == NULL)
   215 	if (showInfo == NULL)
   223 		{
   216 		{
   224 		DBAddShowL(aItem);
   217 		DBAddShowL(aItem);
   225 		return ETrue;
       
   226 		}
   218 		}
   227 	else
   219 	else
   228 		{
   220 		{
   229 		delete showInfo;	
   221 		delete showInfo;	
   230 		return EFalse;
   222 		User::Leave(KErrAlreadyExists);
   231 		}	
   223 		}	
   232 	}
   224 	}
   233 
   225 
   234 EXPORT_C void CShowEngine::AddObserver(MShowEngineObserver *observer)
   226 EXPORT_C void CShowEngine::AddObserver(MShowEngineObserver *observer)
   235 	{
   227 	{
   288 					iShowDownloading->SetShowType(EVideoPodcast);
   280 					iShowDownloading->SetShowType(EVideoPodcast);
   289 					}
   281 					}
   290 
   282 
   291 				iShowDownloading->SetDownloadState(EDownloaded);
   283 				iShowDownloading->SetDownloadState(EDownloaded);
   292 				DBUpdateShowL(*iShowDownloading);
   284 				DBUpdateShowL(*iShowDownloading);
   293 				DBRemoveDownload(iShowDownloading->Uid());
   285 				DBRemoveDownloadL(iShowDownloading->Uid());
   294 				AddShowToMpxCollection(*iShowDownloading);				
   286 				AddShowToMpxCollection(*iShowDownloading);				
   295 				NotifyShowFinishedL(aError);
   287 				NotifyShowFinishedL(aError);
   296 				iDownloadErrors = 0;
   288 				iDownloadErrors = 0;
   297 				delete iShowDownloading;
   289 				delete iShowDownloading;
   298 				iShowDownloading = NULL;
   290 				iShowDownloading = NULL;
   302 				// 400 and 500 series errors are serious errors on which probably another download will fail
   294 				// 400 and 500 series errors are serious errors on which probably another download will fail
   303 				if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200)
   295 				if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200)
   304 					{
   296 					{
   305 					iShowDownloading->SetDownloadState(EFailedDownload);
   297 					iShowDownloading->SetDownloadState(EFailedDownload);
   306 					DBUpdateShowL(*iShowDownloading);
   298 					DBUpdateShowL(*iShowDownloading);
   307 					DBRemoveDownload(iShowDownloading->Uid());
   299 					DBRemoveDownloadL(iShowDownloading->Uid());
   308 					NotifyShowFinishedL(aError);
   300 					NotifyShowFinishedL(aError);
   309 
   301 
   310 					delete iShowDownloading;
   302 					delete iShowDownloading;
   311 					iShowDownloading = NULL;
   303 					iShowDownloading = NULL;
   312 					}
   304 					}
   463 			CleanupStack::Pop(showInfo);
   455 			CleanupStack::Pop(showInfo);
   464 			rc = sqlite3_step(st);
   456 			rc = sqlite3_step(st);
   465 			}
   457 			}
   466 		CleanupStack::PopAndDestroy();//st
   458 		CleanupStack::PopAndDestroy();//st
   467 		}
   459 		}
       
   460 	else
       
   461 		{
       
   462 		User::Leave(KErrCorrupt);
       
   463 		}
   468 
   464 
   469 	// delete downloads that don't have a show
   465 	// delete downloads that don't have a show
   470 	
   466 	
   471 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
   467 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
   472 	iSqlBuffer.Format(KSqlStatement2);
   468 	iSqlBuffer.Format(KSqlStatement2);
   473 
   469 
   474 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,	&st, (const void**) NULL);
   470 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,	&st, (const void**) NULL);
   475 
   471 
   476 	if (rc == SQLITE_OK)
   472 	if (rc == SQLITE_OK)
   477 		{
   473 		{
   478 		rc = sqlite3_step(st);
   474 		Cleanup_sqlite3_finalize_PushL(st);
   479 		sqlite3_finalize(st);
   475 		rc = sqlite3_step(st);
       
   476 		CleanupStack::PopAndDestroy(); // st
       
   477 		}
       
   478 	else
       
   479 		{
       
   480 		User::Leave(KErrCorrupt);
   480 		}
   481 		}
   481 	}
   482 	}
   482 
   483 
   483 CShowInfo* CShowEngine::DBGetNextDownloadL()
   484 CShowInfo* CShowEngine::DBGetNextDownloadL()
   484 	{
   485 	{
   508 			{
   509 			{
   509 			showInfo = CShowInfo::NewLC();
   510 			showInfo = CShowInfo::NewLC();
   510 			DBFillShowInfoFromStmtL(st, showInfo);
   511 			DBFillShowInfoFromStmtL(st, showInfo);
   511 			CleanupStack::Pop(showInfo);
   512 			CleanupStack::Pop(showInfo);
   512 			}
   513 			}
       
   514 		else
       
   515 			{
       
   516 			User::Leave(KErrUnknown);
       
   517 			}
   513 		CleanupStack::PopAndDestroy();//st
   518 		CleanupStack::PopAndDestroy();//st
       
   519 		}
       
   520 	else
       
   521 		{
       
   522 		User::Leave(KErrCorrupt);
   514 		}
   523 		}
   515 
   524 
   516 	return showInfo;
   525 	return showInfo;
   517 	}
   526 	}
   518 
   527 
   544 			CleanupStack::Pop(showInfo);
   553 			CleanupStack::Pop(showInfo);
   545 			rc = sqlite3_step(st);
   554 			rc = sqlite3_step(st);
   546 			}
   555 			}
   547 		CleanupStack::PopAndDestroy();//st
   556 		CleanupStack::PopAndDestroy();//st
   548 		}
   557 		}
       
   558 	else
       
   559 		{
       
   560 		User::Leave(KErrCorrupt);
       
   561 		}
   549 	DP("CShowEngine::DBGetShowsByFeed END");
   562 	DP("CShowEngine::DBGetShowsByFeed END");
   550 	}
   563 	}
   551 
   564 
   552 TUint CShowEngine::DBGetDownloadsCount()
   565 TUint CShowEngine::DBGetDownloadsCountL()
   553 	{
   566 	{
   554 	DP("CShowEngine::DBGetDownloadsCount");
   567 	DP("CShowEngine::DBGetDownloadsCount");
   555 
   568 
   556 	_LIT(KSqlStatement, "select count(*) from downloads");
   569 	_LIT(KSqlStatement, "select count(*) from downloads");
   557 	iSqlBuffer.Format(KSqlStatement);
   570 	iSqlBuffer.Format(KSqlStatement);
   562 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   575 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   563 			&st, (const void**) NULL);
   576 			&st, (const void**) NULL);
   564 
   577 
   565 	if (rc == SQLITE_OK)
   578 	if (rc == SQLITE_OK)
   566 		{
   579 		{
       
   580 		Cleanup_sqlite3_finalize_PushL(st);
   567 		rc = sqlite3_step(st);
   581 		rc = sqlite3_step(st);
   568 
   582 
   569 		if (rc == SQLITE_ROW)
   583 		if (rc == SQLITE_ROW)
   570 			{
   584 			{
   571 			count = sqlite3_column_int(st, 0);
   585 			count = sqlite3_column_int(st, 0);
   572 			}
   586 			}
   573 		sqlite3_finalize(st);
   587 		else
       
   588 			{
       
   589 			User::Leave(KErrUnknown);
       
   590 			}
       
   591 		
       
   592 		CleanupStack::PopAndDestroy(); //st
       
   593 		}
       
   594 	else
       
   595 		{
       
   596 		User::Leave(KErrCorrupt);
   574 		}
   597 		}
   575 	return count;
   598 	return count;
   576 	}
   599 	}
   577 
   600 
   578 void CShowEngine::DBGetDownloadedShowsL(RShowInfoArray& aShowArray)
   601 void CShowEngine::DBGetDownloadedShowsL(RShowInfoArray& aShowArray)
   603 			CleanupStack::Pop(showInfo);
   626 			CleanupStack::Pop(showInfo);
   604 			rc = sqlite3_step(st);
   627 			rc = sqlite3_step(st);
   605 			}
   628 			}
   606 		CleanupStack::PopAndDestroy();//st
   629 		CleanupStack::PopAndDestroy();//st
   607 		}
   630 		}
       
   631 	else
       
   632 		{
       
   633 		User::Leave(KErrCorrupt);
       
   634 		}
   608 	}
   635 	}
   609 
   636 
   610 void CShowEngine::DBGetNewShowsL(RShowInfoArray& aShowArray)
   637 void CShowEngine::DBGetNewShowsL(RShowInfoArray& aShowArray)
   611 	{
   638 	{
   612 	DP("CShowEngine::DBGetNewShows");
   639 	DP("CShowEngine::DBGetNewShows");
   630 			CleanupStack::Pop(showInfo);
   657 			CleanupStack::Pop(showInfo);
   631 			rc = sqlite3_step(st);
   658 			rc = sqlite3_step(st);
   632 			}
   659 			}
   633 		CleanupStack::PopAndDestroy();//st
   660 		CleanupStack::PopAndDestroy();//st
   634 		}
   661 		}
   635 	}
   662 	else
   636 
   663 		{
   637 void CShowEngine::DBDeleteOldShowsByFeed(TUint aFeedUid)
   664 		User::Leave(KErrCorrupt);
       
   665 		}
       
   666 	}
       
   667 
       
   668 void CShowEngine::DBDeleteOldShowsByFeedL(TUint aFeedUid)
   638 	{
   669 	{
   639 	DP("CShowEngine::DBDeleteOldShows");
   670 	DP("CShowEngine::DBDeleteOldShows");
   640 	
   671 	
   641 	// what we do:
   672 	// what we do:
   642 	// 1. sort shows by pubdate
   673 	// 1. sort shows by pubdate
   655 	if (rc == SQLITE_OK)
   686 	if (rc == SQLITE_OK)
   656 		{
   687 		{
   657 		rc = sqlite3_step(st);
   688 		rc = sqlite3_step(st);
   658 		sqlite3_finalize(st);
   689 		sqlite3_finalize(st);
   659 		}
   690 		}
       
   691 	else
       
   692 		{
       
   693 		User::Leave(KErrCorrupt);
       
   694 		}
   660 	
   695 	
   661 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
   696 	_LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)");
   662 	iSqlBuffer.Format(KSqlStatement2);
   697 	iSqlBuffer.Format(KSqlStatement2);
   663 
   698 
   664 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,	&st, (const void**) NULL);
   699 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,	&st, (const void**) NULL);
   665 
   700 
   666 	if (rc == SQLITE_OK)
   701 	if (rc == SQLITE_OK)
   667 		{
   702 		{
   668 		rc = sqlite3_step(st);
   703 		rc = sqlite3_step(st);
   669 		sqlite3_finalize(st);
   704 		sqlite3_finalize(st);
       
   705 		}
       
   706 	else
       
   707 		{
       
   708 		User::Leave(KErrCorrupt);
   670 		}
   709 		}
   671 	}
   710 	}
   672 
   711 
   673 void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo)
   712 void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo)
   674 	{
   713 	{
   722 	
   761 	
   723 	TInt lasterror = sqlite3_column_int(st, 14);
   762 	TInt lasterror = sqlite3_column_int(st, 14);
   724 	showInfo->SetLastError(lasterror);
   763 	showInfo->SetLastError(lasterror);
   725 	}
   764 	}
   726 
   765 
   727 TBool CShowEngine::DBAddShowL(const CShowInfo& aItem)
   766 void CShowEngine::DBAddShowL(const CShowInfo& aItem)
   728 	{
   767 	{
   729 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
   768 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
   730 
   769 
   731 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   770 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   732 	TPtr titlePtr(titleBuf->Des());
   771 	TPtr titlePtr(titleBuf->Des());
   752 
   791 
   753 	sqlite3_stmt *st;
   792 	sqlite3_stmt *st;
   754 
   793 
   755 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   794 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   756 			&st, (const void**) NULL);
   795 			&st, (const void**) NULL);
   757 	if (rc == SQLITE_OK)
   796 	
   758 		{
   797 	if (rc == SQLITE_OK)
   759 		rc = sqlite3_step(st);
   798 		{
   760 		if (rc == SQLITE_DONE)
   799 		Cleanup_sqlite3_finalize_PushL(st);
   761 			{
   800 		rc = sqlite3_step(st);
   762 			sqlite3_finalize(st);
   801 		if (rc != SQLITE_DONE)
   763 			return ETrue;
   802 			{
   764 			}
   803 			User::Leave(KErrAlreadyExists);
   765 		else
   804 			}
   766 			{
   805 		CleanupStack::PopAndDestroy(); // st
   767 			sqlite3_finalize(st);
   806 		}
   768 			}
   807 	else
   769 		}
   808 		{
   770 	else
   809 		User::Leave(KErrCorrupt);
   771 		{
   810 		}
   772 		DP1("SQLite rc=%d", rc);
   811 	}
   773 		}
   812 
   774 
   813 void CShowEngine::DBAddDownloadL(TUint aUid)
   775 	return EFalse;
       
   776 	}
       
   777 
       
   778 void CShowEngine::DBAddDownload(TUint aUid)
       
   779 	{
   814 	{
   780 	DP1("CShowEngine::DBAddDownload, aUid=%u", aUid);
   815 	DP1("CShowEngine::DBAddDownload, aUid=%u", aUid);
   781 
   816 
   782 	_LIT(KSqlStatement, "insert into downloads (uid) values (%u)");
   817 	_LIT(KSqlStatement, "insert into downloads (uid) values (%u)");
   783 	iSqlBuffer.Format(KSqlStatement, aUid);
   818 	iSqlBuffer.Format(KSqlStatement, aUid);
   786 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   821 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   787 			&st, (const void**) NULL);
   822 			&st, (const void**) NULL);
   788 
   823 
   789 	if (rc == SQLITE_OK)
   824 	if (rc == SQLITE_OK)
   790 		{
   825 		{
   791 		rc = sqlite3_step(st);
   826 		Cleanup_sqlite3_finalize_PushL(st);
   792 		}
   827 		rc = sqlite3_step(st);
   793 
   828 		if (rc =! SQLITE_DONE)
   794 	sqlite3_finalize(st);
   829 			{
   795 	}
   830 			User::Leave(KErrUnknown);
   796 
   831 			}
   797 TBool CShowEngine::DBUpdateShowL(CShowInfo& aItem)
   832 		CleanupStack::PopAndDestroy(); // st
       
   833 		}
       
   834 	else
       
   835 		{
       
   836 		User::Leave(KErrCorrupt);
       
   837 		}
       
   838 	}
       
   839 
       
   840 void CShowEngine::DBUpdateShowL(CShowInfo& aItem)
   798 	{
   841 	{
   799 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
   842 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
   800 
   843 
   801 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   844 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   802 	TPtr titlePtr(titleBuf->Des());
   845 	TPtr titlePtr(titleBuf->Des());
   825 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   868 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   826 			&st, (const void**) NULL);
   869 			&st, (const void**) NULL);
   827 
   870 
   828 	if (rc == SQLITE_OK)
   871 	if (rc == SQLITE_OK)
   829 		{
   872 		{
   830 		rc = sqlite3_step(st);
   873 		Cleanup_sqlite3_finalize_PushL(st);
   831 
   874 		rc = sqlite3_step(st);		
   832 		if (rc == SQLITE_DONE)
   875 
   833 			{
   876 		if (rc =! SQLITE_DONE)
   834 			sqlite3_finalize(st);
   877 			{
   835 			return ETrue;
   878 			User::Leave(KErrUnknown);
   836 			}
   879 			}
   837 		else
   880 		CleanupStack::PopAndDestroy(); // st
   838 			{
   881 		}
   839 			sqlite3_finalize(st);
   882 	else
   840 			}
   883 		{
   841 		}
   884 		User::Leave(KErrCorrupt);
   842 	else
   885 		}
   843 		{
   886 	}
   844 		DP1("SQLite rc=%d", rc);
   887 
   845 		}
   888 void CShowEngine::DBDeleteShowL(TUint aUid)
   846 
       
   847 	return EFalse;
       
   848 	}
       
   849 
       
   850 TBool CShowEngine::DBDeleteShow(TUint aUid)
       
   851 	{
   889 	{
   852 	DP("CShowEngine::DBDeleteShow");
   890 	DP("CShowEngine::DBDeleteShow");
   853 
   891 
   854 	_LIT(KSqlStatement, "delete from shows where uid=%u");
   892 	_LIT(KSqlStatement, "delete from shows where uid=%u");
   855 	iSqlBuffer.Format(KSqlStatement, aUid);
   893 	iSqlBuffer.Format(KSqlStatement, aUid);
   856 
   894 
   857 	sqlite3_stmt *st;
   895 	sqlite3_stmt *st;
   858 
   896 
   859 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
   897 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   860 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   898 			&st, (const void**) NULL);
   861 			&st, (const void**) NULL);
   899 
   862 
   900 	if (rc == SQLITE_OK)
   863 	if (rc == SQLITE_OK)
   901 		{
   864 		{
   902 		Cleanup_sqlite3_finalize_PushL(st);
   865 		rc = sqlite3_step(st);
   903 		rc = sqlite3_step(st);
   866 
   904 
   867 		if (rc == SQLITE_DONE)
   905 		if (rc =! SQLITE_DONE)
   868 			{
   906 			{
   869 			sqlite3_finalize(st);
   907 			User::Leave(KErrUnknown);
   870 			return ETrue;
   908 			}
   871 			}
   909 		CleanupStack::PopAndDestroy(); // st
   872 		else
   910 		}
   873 			{
   911 	else
   874 			sqlite3_finalize(st);
   912 		{
   875 			}
   913 		User::Leave(KErrCorrupt);
   876 		}
   914 		}
   877 	else
   915 	}
   878 		{
   916 
   879 		DP1("SQLite rc=%d", rc);
   917 void CShowEngine::DBDeleteAllShowsByFeedL(TUint aFeedUid)
   880 		}
       
   881 
       
   882 	return EFalse;
       
   883 	}
       
   884 
       
   885 TBool CShowEngine::DBDeleteAllShowsByFeed(TUint aFeedUid)
       
   886 	{
   918 	{
   887 	DP("CShowEngine::DBDeleteAllShowsByFeed");
   919 	DP("CShowEngine::DBDeleteAllShowsByFeed");
   888 
   920 
   889 	_LIT(KSqlStatement, "delete from shows where feeduid=%u");
   921 	_LIT(KSqlStatement, "delete from shows where feeduid=%u");
   890 	iSqlBuffer.Format(KSqlStatement, aFeedUid);
   922 	iSqlBuffer.Format(KSqlStatement, aFeedUid);
   891 
   923 
   892 	sqlite3_stmt *st;
   924 	sqlite3_stmt *st;
   893 
   925 
   894 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
   926 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   895 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   927 			&st, (const void**) NULL);
   896 			&st, (const void**) NULL);
   928 
   897 
   929 	if (rc == SQLITE_OK)
   898 	if (rc == SQLITE_OK)
   930 		{
   899 		{
   931 		Cleanup_sqlite3_finalize_PushL(st);
   900 		rc = sqlite3_step(st);
   932 		rc = sqlite3_step(st);
   901 
   933 
   902 		if (rc == SQLITE_DONE)
   934 		if (rc =! SQLITE_DONE)
   903 			{
   935 			{
   904 			sqlite3_finalize(st);
   936 			User::Leave(KErrUnknown);
   905 			return ETrue;
   937 			}
   906 			}
   938 		CleanupStack::PopAndDestroy(); // st
   907 		else
   939 		}
   908 			{
   940 	else
   909 			sqlite3_finalize(st);
   941 		{
   910 			}
   942 		User::Leave(KErrCorrupt);
   911 		}
   943 		}
   912 	else
   944 	}
   913 		{
   945 
   914 		DP1("SQLite rc=%d", rc);
   946 void CShowEngine::DBRemoveAllDownloadsL()
   915 		}
       
   916 
       
   917 	return EFalse;
       
   918 	}
       
   919 
       
   920 void CShowEngine::DBRemoveAllDownloads()
       
   921 	{
   947 	{
   922 	DP("CShowEngine::DBRemoveAllDownloads");
   948 	DP("CShowEngine::DBRemoveAllDownloads");
   923 
   949 
   924 	_LIT(KSqlStatement, "delete from downloads");
   950 	_LIT(KSqlStatement, "delete from downloads");
   925 	iSqlBuffer.Format(KSqlStatement);
   951 	iSqlBuffer.Format(KSqlStatement);
   932 	if (rc == SQLITE_OK)
   958 	if (rc == SQLITE_OK)
   933 		{
   959 		{
   934 		rc = sqlite3_step(st);
   960 		rc = sqlite3_step(st);
   935 		sqlite3_finalize(st);
   961 		sqlite3_finalize(st);
   936 		}
   962 		}
       
   963 	else
       
   964 		{
       
   965 		User::Leave(KErrCorrupt);
       
   966 		}
   937 
   967 
   938 	_LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1");
   968 	_LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1");
   939 	iSqlBuffer.Format(KSqlStatement2);
   969 	iSqlBuffer.Format(KSqlStatement2);
   940 
   970 
   941 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st,
   971 	rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st,
   942 			(const void**) NULL);
   972 			(const void**) NULL);
   943 
   973 
   944 	if (rc == SQLITE_OK)
   974 	if (rc == SQLITE_OK)
   945 		{
   975 		{
   946 		rc = sqlite3_step(st);
   976 		Cleanup_sqlite3_finalize_PushL(st);
   947 		sqlite3_finalize(st);
   977 		rc = sqlite3_step(st);
   948 		}
   978 		if (rc =! SQLITE_DONE)
   949 
   979 			{
   950 	}
   980 			User::Leave(KErrUnknown);
   951 
   981 			}
   952 void CShowEngine::DBRemoveDownload(TUint aUid)
   982 		CleanupStack::PopAndDestroy(); // st
       
   983 		}
       
   984 	else
       
   985 		{
       
   986 		User::Leave(KErrCorrupt);
       
   987 		}
       
   988 
       
   989 	}
       
   990 
       
   991 void CShowEngine::DBRemoveDownloadL(TUint aUid)
   953 	{
   992 	{
   954 	DP("CShowEngine::DBRemoveDownload");
   993 	DP("CShowEngine::DBRemoveDownload");
   955 
   994 
   956 	_LIT(KSqlStatement, "delete from downloads where uid=%u");
   995 	_LIT(KSqlStatement, "delete from downloads where uid=%u");
   957 	iSqlBuffer.Format(KSqlStatement, aUid);
   996 	iSqlBuffer.Format(KSqlStatement, aUid);
   961 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
  1000 	int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1,
   962 			&st, (const void**) NULL);
  1001 			&st, (const void**) NULL);
   963 
  1002 
   964 	if (rc == SQLITE_OK)
  1003 	if (rc == SQLITE_OK)
   965 		{
  1004 		{
   966 		rc = sqlite3_step(st);
  1005 		Cleanup_sqlite3_finalize_PushL(st);
   967 		sqlite3_finalize(st);
  1006 		rc = sqlite3_step(st);
       
  1007 		if (rc =! SQLITE_DONE)
       
  1008 			{
       
  1009 			User::Leave(KErrUnknown);
       
  1010 			}
       
  1011 		CleanupStack::PopAndDestroy(); // st
       
  1012 		}
       
  1013 	else
       
  1014 		{
       
  1015 		User::Leave(KErrCorrupt);
   968 		}
  1016 		}
   969 	}
  1017 	}
   970 
  1018 
   971 EXPORT_C CShowInfo* CShowEngine::GetNextShowByTrackL(CShowInfo* aShowInfo)
  1019 EXPORT_C CShowInfo* CShowEngine::GetNextShowByTrackL(CShowInfo* aShowInfo)
   972 	{
  1020 	{
  1087 			}
  1135 			}
  1088 		}
  1136 		}
  1089 	array.ResetAndDestroy();
  1137 	array.ResetAndDestroy();
  1090 	
  1138 	
  1091 	// delete all shows from DB
  1139 	// delete all shows from DB
  1092 	DBDeleteAllShowsByFeed(aFeedUid);
  1140 	DBDeleteAllShowsByFeedL(aFeedUid);
  1093 
  1141 
  1094 	// this will clear out deleted shows from the download queue
  1142 	// this will clear out deleted shows from the download queue
  1095 	DBGetAllDownloadsL(array);
  1143 	DBGetAllDownloadsL(array);
  1096 	array.ResetAndDestroy();
  1144 	array.ResetAndDestroy();
  1097 
  1145 
  1098 	NotifyDownloadQueueUpdatedL();
  1146 	NotifyDownloadQueueUpdatedL();
  1099 	}
  1147 	}
  1100 
  1148 
  1101 EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid)
  1149 EXPORT_C void CShowEngine::DeleteOldShowsByFeedL(TUint aFeedUid)
  1102 	{
  1150 	{
  1103 		DBDeleteOldShowsByFeed(aFeedUid);
  1151 	DBDeleteOldShowsByFeedL(aFeedUid);
  1104 	}
  1152 	}
  1105 
  1153 
  1106 EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile)
  1154 EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile)
  1107 	{
  1155 	{
  1108 
  1156 
  1151 	DBGetAllDownloadsL(aArray);
  1199 	DBGetAllDownloadsL(aArray);
  1152 	}
  1200 	}
  1153 
  1201 
  1154 EXPORT_C TInt CShowEngine::GetNumDownloadingShows()
  1202 EXPORT_C TInt CShowEngine::GetNumDownloadingShows()
  1155 	{
  1203 	{
  1156 	return (const TInt) DBGetDownloadsCount();
  1204 	TUint count;
       
  1205 	TRAP_IGNORE(count = DBGetDownloadsCountL());
       
  1206 		
       
  1207 	return (const TInt) count;
  1157 	}
  1208 	}
  1158 
  1209 
  1159 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo)
  1210 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo)
  1160 	{
  1211 	{
  1161 	aInfo.SetDownloadState(EQueued);
  1212 	aInfo.SetDownloadState(EQueued);
  1162 	DBUpdateShowL(aInfo);
  1213 	DBUpdateShowL(aInfo);
  1163 	DBAddDownload(aInfo.Uid());
  1214 	DBAddDownloadL(aInfo.Uid());
  1164 	DownloadNextShowL();
  1215 	DownloadNextShowL();
  1165 	}
  1216 	}
  1166 
  1217 
  1167 void CShowEngine::DownloadNextShowL()
  1218 void CShowEngine::DownloadNextShowL()
  1168 	{
  1219 	{
  1169 	DP("CShowEngine::DownloadNextShowL BEGIN");
  1220 	DP("CShowEngine::DownloadNextShowL BEGIN");
  1170 	// Check if we have anything in the download queue
  1221 	// Check if we have anything in the download queue
  1171 	const TInt count = DBGetDownloadsCount();
  1222 	const TInt count = DBGetDownloadsCountL();
  1172 	DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count);
  1223 	DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count);
  1173 
  1224 
  1174 	if (count > 0)
  1225 	if (count > 0)
  1175 		{
  1226 		{
  1176 		if (iPodcastModel.SettingsEngine().DownloadSuspended())
  1227 		if (iPodcastModel.SettingsEngine().DownloadSuspended())
  1194 			
  1245 			
  1195 			CShowInfo *info = DBGetNextDownloadL();
  1246 			CShowInfo *info = DBGetNextDownloadL();
  1196 			
  1247 			
  1197 			while(info != NULL)
  1248 			while(info != NULL)
  1198 				{
  1249 				{
  1199 				TBool getOk = EFalse;
       
  1200 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
  1250 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
  1201 				info->SetDownloadState(EDownloading);
  1251 				info->SetDownloadState(EDownloading);
  1202 				info->SetLastError(KErrNone);
  1252 				info->SetLastError(KErrNone);
  1203 				DBUpdateShowL(*info);
  1253 				DBUpdateShowL(*info);
  1204 				iShowDownloading = info;
  1254 				iShowDownloading = info;
  1205 				// Inform the observers
  1255 				// Inform the observers
  1206 				// important to do this after we change download state
  1256 				// important to do this after we change download state
  1207 				NotifyDownloadQueueUpdatedL();
  1257 				NotifyDownloadQueueUpdatedL();
  1208 
  1258 
  1209 				TRAPD(error,getOk = GetShowL(info));
  1259 				TRAPD(error,GetShowL(info));
  1210 				if (error != KErrNone || !getOk)
  1260 				if (error != KErrNone)
  1211 					{
  1261 					{
  1212 					info->SetDownloadState(EFailedDownload);
  1262 					info->SetDownloadState(EFailedDownload);
  1213 					DBRemoveDownload(info->Uid());
  1263 					DBRemoveDownloadL(info->Uid());
  1214 					DBUpdateShowL(*info);
  1264 					DBUpdateShowL(*info);
  1215 					info = DBGetNextDownloadL();
  1265 					info = DBGetNextDownloadL();
  1216 					
  1266 					
  1217 					if(info == NULL)
  1267 					if(info == NULL)
  1218 						{
  1268 						{
  1239 void CShowEngine::NotifyDownloadQueueUpdatedL()
  1289 void CShowEngine::NotifyDownloadQueueUpdatedL()
  1240 	{
  1290 	{
  1241 	const TInt count = iObservers.Count();
  1291 	const TInt count = iObservers.Count();
  1242 	for (TInt i = 0; i < count; i++)
  1292 	for (TInt i = 0; i < count; i++)
  1243 		{
  1293 		{
  1244 		iObservers[i]->DownloadQueueUpdatedL(1, DBGetDownloadsCount() - 1);
  1294 		iObservers[i]->DownloadQueueUpdatedL(1, DBGetDownloadsCountL() - 1);
  1245 		}
  1295 		}
  1246 	}
  1296 	}
  1247 
  1297 
  1248 void CShowEngine::NotifyShowDownloadUpdatedL(TInt aBytesOfCurrentDownload, TInt aBytesTotal)
  1298 void CShowEngine::NotifyShowDownloadUpdatedL(TInt aBytesOfCurrentDownload, TInt aBytesTotal)
  1249 	{
  1299 	{