engine/src/ShowEngine.cpp
changeset 66 41d00e97e2f7
parent 52 b778853e60a7
child 76 223f270fa7ff
equal deleted inserted replaced
65:523e04129df6 66:41d00e97e2f7
    22 #include <bautils.h>
    22 #include <bautils.h>
    23 #include <s32file.h>
    23 #include <s32file.h>
    24 #include "SettingsEngine.h"
    24 #include "SettingsEngine.h"
    25 #include <e32hashtab.h>
    25 #include <e32hashtab.h>
    26 #include <httperr.h>
    26 #include <httperr.h>
    27 #include "SoundEngine.h"
       
    28 #include "debug.h"
    27 #include "debug.h"
    29 #include "PodcastUtils.h"
    28 #include "PodcastUtils.h"
    30 
    29 
    31 //#include <mpxmedia.h>
    30 //#include <mpxmedia.h>
    32 //#include <mpxattribute.h>
    31 //#include <mpxattribute.h>
   113 		DownloadNextShowL();
   112 		DownloadNextShowL();
   114 		}
   113 		}
   115 	DP("CShowEngine::ResumeDownloadsL END");
   114 	DP("CShowEngine::ResumeDownloadsL END");
   116 	}
   115 	}
   117 
   116 
   118 EXPORT_C void CShowEngine::RemoveAllDownloads()
   117 EXPORT_C void CShowEngine::RemoveAllDownloadsL()
   119 	{
   118 	{
   120 	if (!iPodcastModel.SettingsEngine().DownloadSuspended())
   119 	if (!iPodcastModel.SettingsEngine().DownloadSuspended())
   121 		{
   120 		{
   122 		SuspendDownloads();
   121 		SuspendDownloads();
   123 		}
   122 		}
   143 
   142 
   144 	CShowInfo *info = DBGetShowByUidL(aUid);
   143 	CShowInfo *info = DBGetShowByUidL(aUid);
   145 	if (info != NULL)
   144 	if (info != NULL)
   146 		{
   145 		{
   147 		info->SetDownloadState(ENotDownloaded);
   146 		info->SetDownloadState(ENotDownloaded);
   148 		DBUpdateShow(*info);
   147 		DBUpdateShowL(*info);
   149 		delete info;
   148 		delete info;
   150 		}
   149 		}
   151 	DBRemoveDownload(aUid);
   150 	DBRemoveDownload(aUid);
   152 
   151 
   153 	// partial downloads should be removed
   152 	// partial downloads should be removed
   229 	DP1("CShowEngine::AddShowL, title=%S", &aItem.Title());
   228 	DP1("CShowEngine::AddShowL, title=%S", &aItem.Title());
   230 	CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid());
   229 	CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid());
   231 
   230 
   232 	if (showInfo == NULL)
   231 	if (showInfo == NULL)
   233 		{
   232 		{
   234 		DBAddShow(aItem);
   233 		DBAddShowL(aItem);
   235 		return ETrue;
   234 		return ETrue;
   236 		}
   235 		}
   237 	else
   236 	else
   238 		{
   237 		{
   239 		delete showInfo;	
   238 		delete showInfo;	
   297 					{
   296 					{
   298 					iShowDownloading->SetShowType(EVideoPodcast);
   297 					iShowDownloading->SetShowType(EVideoPodcast);
   299 					}
   298 					}
   300 
   299 
   301 				iShowDownloading->SetDownloadState(EDownloaded);
   300 				iShowDownloading->SetDownloadState(EDownloaded);
   302 				DBUpdateShow(*iShowDownloading);
   301 				DBUpdateShowL(*iShowDownloading);
   303 				DBRemoveDownload(iShowDownloading->Uid());
   302 				DBRemoveDownload(iShowDownloading->Uid());
   304 				AddShowToMpxCollection(*iShowDownloading);				
   303 				AddShowToMpxCollection(*iShowDownloading);				
   305 				NotifyShowFinishedL(aError);
   304 				NotifyShowFinishedL(aError);
   306 				iDownloadErrors = 0;
   305 				iDownloadErrors = 0;
   307 				delete iShowDownloading;
   306 				delete iShowDownloading;
   311 				{
   310 				{
   312 				// 400 and 500 series errors are serious errors on which probably another download will fail
   311 				// 400 and 500 series errors are serious errors on which probably another download will fail
   313 				if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200)
   312 				if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200)
   314 					{
   313 					{
   315 					iShowDownloading->SetDownloadState(EFailedDownload);
   314 					iShowDownloading->SetDownloadState(EFailedDownload);
   316 					DBUpdateShow(*iShowDownloading);
   315 					DBUpdateShowL(*iShowDownloading);
   317 					DBRemoveDownload(iShowDownloading->Uid());
   316 					DBRemoveDownload(iShowDownloading->Uid());
   318 					NotifyShowFinishedL(aError);
   317 					NotifyShowFinishedL(aError);
   319 
   318 
   320 					delete iShowDownloading;
   319 					delete iShowDownloading;
   321 					iShowDownloading = NULL;
   320 					iShowDownloading = NULL;
   322 					}
   321 					}
   323 				else // other kind of error, missing network etc, reque this show
   322 				else // other kind of error, missing network etc, reque this show
   324 					{
   323 					{
   325 					iShowDownloading->SetDownloadState(EQueued);
   324 					iShowDownloading->SetDownloadState(EQueued);
   326 					DBUpdateShow(*iShowDownloading);
   325 					DBUpdateShowL(*iShowDownloading);
   327 					}
   326 					}
   328 
   327 
   329 				iDownloadErrors++;
   328 				iDownloadErrors++;
   330 				if (iDownloadErrors > KMaxDownloadErrors)
   329 				if (iDownloadErrors > KMaxDownloadErrors)
   331 					{
   330 					{
   341 			{
   340 			{
   342 			// Connection error
   341 			// Connection error
   343 			if(iShowDownloading)
   342 			if(iShowDownloading)
   344 				{
   343 				{
   345 				iShowDownloading->SetDownloadState(EQueued);
   344 				iShowDownloading->SetDownloadState(EQueued);
   346 				DBUpdateShow(*iShowDownloading);
   345 				DBUpdateShowL(*iShowDownloading);
   347 				}
   346 				}
   348 			iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
   347 			iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
   349 			NotifyShowFinishedL(aError);
   348 			NotifyShowFinishedL(aError);
   350 			}
   349 			}
   351 		}
   350 		}
   732 	
   731 	
   733 	TInt lasterror = sqlite3_column_int(st, 14);
   732 	TInt lasterror = sqlite3_column_int(st, 14);
   734 	showInfo->SetLastError(lasterror);
   733 	showInfo->SetLastError(lasterror);
   735 	}
   734 	}
   736 
   735 
   737 TBool CShowEngine::DBAddShow(const CShowInfo& aItem)
   736 TBool CShowEngine::DBAddShowL(const CShowInfo& aItem)
   738 	{
   737 	{
   739 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
   738 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
   740 
   739 
   741 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   740 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   742 	TPtr titlePtr(titleBuf->Des());
   741 	TPtr titlePtr(titleBuf->Des());
   802 		}
   801 		}
   803 
   802 
   804 	sqlite3_finalize(st);
   803 	sqlite3_finalize(st);
   805 	}
   804 	}
   806 
   805 
   807 TBool CShowEngine::DBUpdateShow(CShowInfo& aItem)
   806 TBool CShowEngine::DBUpdateShowL(CShowInfo& aItem)
   808 	{
   807 	{
   809 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
   808 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
   810 
   809 
   811 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   810 	HBufC* titleBuf = HBufC::NewLC(KMaxLineLength);
   812 	TPtr titlePtr(titleBuf->Des());
   811 	TPtr titlePtr(titleBuf->Des());
  1056 		//		DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle);
  1055 		//		DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle);
  1057 		return 1;
  1056 		return 1;
  1058 		}
  1057 		}
  1059 	}
  1058 	}
  1060 
  1059 
  1061 EXPORT_C void CShowEngine::DeletePlayedShows(RShowInfoArray &aShowInfoArray)
  1060 EXPORT_C void CShowEngine::DeletePlayedShowsL(RShowInfoArray &aShowInfoArray)
  1062 	{
  1061 	{
  1063 	for (TInt i = 0; i < aShowInfoArray.Count(); i++)
  1062 	for (TInt i = 0; i < aShowInfoArray.Count(); i++)
  1064 		{
  1063 		{
  1065 		if (aShowInfoArray[i]->PlayState() == EPlayed
  1064 		if (aShowInfoArray[i]->PlayState() == EPlayed
  1066 				&& aShowInfoArray[i]->FileName().Length() > 0)
  1065 				&& aShowInfoArray[i]->FileName().Length() > 0)
  1067 			{
  1066 			{
  1068 			if (CompareShowsByUid(*(iPodcastModel.PlayingPodcast()), *(aShowInfoArray[i]))
       
  1069 					&& iPodcastModel.SoundEngine().State() != ESoundEngineNotInitialized)
       
  1070 				{
       
  1071 				iPodcastModel.SoundEngine().Stop();
       
  1072 				}
       
  1073 			BaflUtils::DeleteFile(iPodcastModel.FsSession(), aShowInfoArray[i]->FileName());
  1067 			BaflUtils::DeleteFile(iPodcastModel.FsSession(), aShowInfoArray[i]->FileName());
  1074 			aShowInfoArray[i]->SetDownloadState(ENotDownloaded);
  1068 			aShowInfoArray[i]->SetDownloadState(ENotDownloaded);
  1075 			DBUpdateShow(*aShowInfoArray[i]);
  1069 			DBUpdateShowL(*aShowInfoArray[i]);
  1076 			}
  1070 			}
  1077 		}
  1071 		}
  1078 	}
  1072 	}
  1079 
  1073 
  1080 EXPORT_C void CShowEngine::DeleteAllShowsByFeedL(TUint aFeedUid, TBool aDeleteFiles)
  1074 EXPORT_C void CShowEngine::DeleteAllShowsByFeedL(TUint aFeedUid, TBool aDeleteFiles)
  1084 
  1078 
  1085 	const TInt count = array.Count();
  1079 	const TInt count = array.Count();
  1086 
  1080 
  1087 	for (TInt i = count - 1; i >= 0; i--)
  1081 	for (TInt i = count - 1; i >= 0; i--)
  1088 		{
  1082 		{
       
  1083 		if (iShowDownloading && iShowDownloading->Uid() == array[i]->Uid())
       
  1084 			{
       
  1085 			// trying to delete the active download
       
  1086 			RemoveDownloadL(iShowDownloading->Uid());
       
  1087 			}
       
  1088 		
       
  1089 		// delete downloaded file
  1089 		if (array[i]->FileName().Length() > 0)
  1090 		if (array[i]->FileName().Length() > 0)
  1090 			{
  1091 			{
  1091 			if (aDeleteFiles)
  1092 			if (aDeleteFiles)
  1092 				{
  1093 				{
  1093 				BaflUtils::DeleteFile(iPodcastModel.FsSession(), array[i]->FileName());
  1094 				BaflUtils::DeleteFile(iPodcastModel.FsSession(), array[i]->FileName());
  1094 				}
  1095 				}
  1095 			}
  1096 			}
  1096 		}
  1097 		}
  1097 	array.ResetAndDestroy();
  1098 	array.ResetAndDestroy();
       
  1099 	
       
  1100 	// delete all shows from DB
  1098 	DBDeleteAllShowsByFeed(aFeedUid);
  1101 	DBDeleteAllShowsByFeed(aFeedUid);
       
  1102 
       
  1103 	// this will clear out deleted shows from the download queue
       
  1104 	DBGetAllDownloadsL(array);
       
  1105 	array.ResetAndDestroy();
       
  1106 
       
  1107 	NotifyDownloadQueueUpdatedL();
  1099 	}
  1108 	}
  1100 
  1109 
  1101 EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid)
  1110 EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid)
  1102 	{
  1111 	{
  1103 		DBDeleteOldShowsByFeed(aFeedUid);
  1112 		DBDeleteOldShowsByFeed(aFeedUid);
  1114 			{
  1123 			{
  1115 			BaflUtils::DeleteFile(iPodcastModel.FsSession(), info->FileName());
  1124 			BaflUtils::DeleteFile(iPodcastModel.FsSession(), info->FileName());
  1116 			}
  1125 			}
  1117 		
  1126 		
  1118 		info->SetDownloadState(ENotDownloaded);
  1127 		info->SetDownloadState(ENotDownloaded);
  1119 		DBUpdateShow(*info);
  1128 		DBUpdateShowL(*info);
  1120 		delete info;
  1129 		delete info;
  1121 		}
  1130 		}
  1122 	}
  1131 	}
  1123 
  1132 
  1124 EXPORT_C void CShowEngine::GetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid)
  1133 EXPORT_C void CShowEngine::GetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid)
  1157 	}
  1166 	}
  1158 
  1167 
  1159 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo)
  1168 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo)
  1160 	{
  1169 	{
  1161 	aInfo.SetDownloadState(EQueued);
  1170 	aInfo.SetDownloadState(EQueued);
  1162 	DBUpdateShow(aInfo);
  1171 	DBUpdateShowL(aInfo);
  1163 	DBAddDownload(aInfo.Uid());
  1172 	DBAddDownload(aInfo.Uid());
  1164 	DownloadNextShowL();
  1173 	DownloadNextShowL();
  1165 	}
  1174 	}
  1166 
  1175 
  1167 void CShowEngine::DownloadNextShowL()
  1176 void CShowEngine::DownloadNextShowL()
  1198 				{
  1207 				{
  1199 				TBool getOk = EFalse;
  1208 				TBool getOk = EFalse;
  1200 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
  1209 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
  1201 				info->SetDownloadState(EDownloading);
  1210 				info->SetDownloadState(EDownloading);
  1202 				info->SetLastError(KErrNone);
  1211 				info->SetLastError(KErrNone);
  1203 				DBUpdateShow(*info);
  1212 				DBUpdateShowL(*info);
  1204 				iShowDownloading = info;
  1213 				iShowDownloading = info;
  1205 				// Inform the observers
  1214 				// Inform the observers
  1206 				// important to do this after we change download state
  1215 				// important to do this after we change download state
  1207 				NotifyDownloadQueueUpdatedL();
  1216 				NotifyDownloadQueueUpdatedL();
  1208 
  1217 
  1209 				TRAPD(error,getOk = GetShowL(info));
  1218 				TRAPD(error,getOk = GetShowL(info));
  1210 				if (error != KErrNone || !getOk)
  1219 				if (error != KErrNone || !getOk)
  1211 					{
  1220 					{
  1212 					info->SetDownloadState(EFailedDownload);
  1221 					info->SetDownloadState(EFailedDownload);
  1213 					DBRemoveDownload(info->Uid());
  1222 					DBRemoveDownload(info->Uid());
  1214 					DBUpdateShow(*info);
  1223 					DBUpdateShowL(*info);
  1215 					info = DBGetNextDownloadL();
  1224 					info = DBGetNextDownloadL();
  1216 					
  1225 					
  1217 					if(info == NULL)
  1226 					if(info == NULL)
  1218 						{
  1227 						{
  1219 						iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
  1228 						iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
  1269 		{
  1278 		{
  1270 		iObservers[i]->ShowListUpdatedL();
  1279 		iObservers[i]->ShowListUpdatedL();
  1271 		}
  1280 		}
  1272 	}
  1281 	}
  1273 
  1282 
  1274 void CShowEngine::ReadMetaData(CShowInfo& aShowInfo)
  1283 void CShowEngine::ReadMetaDataL(CShowInfo& aShowInfo)
  1275 	{
  1284 	{
  1276 	//DP1("Read %S", &(aShowInfo->Title()));
  1285 	//DP1("Read %S", &(aShowInfo->Title()));
  1277 	DBUpdateShow(aShowInfo);
  1286 	DBUpdateShowL(aShowInfo);
  1278 	}
  1287 	}
  1279 
  1288 
  1280 void CShowEngine::ReadMetaDataCompleteL()
  1289 void CShowEngine::ReadMetaDataCompleteL()
  1281 	{
  1290 	{
  1282 	NotifyShowListUpdatedL();
  1291 	NotifyShowListUpdatedL();
  1283 	MetaDataReader().SetIgnoreTrackNo(EFalse);
  1292 	MetaDataReader().SetIgnoreTrackNo(EFalse);
  1284 	}
  1293 	}
  1285 
  1294 
  1286 EXPORT_C void CShowEngine::UpdateShow(CShowInfo& aInfo)
  1295 EXPORT_C void CShowEngine::UpdateShowL(CShowInfo& aInfo)
  1287 	{
  1296 	{
  1288 	DBUpdateShow(aInfo);
  1297 	DBUpdateShowL(aInfo);
  1289 	}
  1298 	}
  1290 
  1299 
  1291 EXPORT_C CMetaDataReader& CShowEngine::MetaDataReader()
  1300 EXPORT_C CMetaDataReader& CShowEngine::MetaDataReader()
  1292 	{
  1301 	{
  1293 	return *iMetaDataReader;
  1302 	return *iMetaDataReader;