| author | teknolog | 
| Thu, 25 Feb 2010 21:08:38 +0000 | |
| changeset 15 | 93d9f66bf50b | 
| parent 6 | e211a78d3037 | 
| child 21 | 420a1b4930da | 
| permissions | -rw-r--r-- | 
| 2 | 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 | if(aError != KErrCouldNotConnect) | |
| 275 | 			{
 | |
| 6 | 276 | if(aError == KErrDisconnected && iPodcastModel.SettingsEngine().DownloadSuspended()) | 
| 277 | 				{
 | |
| 2 | 278 | // no error if disconnect happened because of suspended downloading | 
| 6 | 279 | } | 
| 280 | else | |
| 281 | 				{
 | |
| 2 | 282 | iShowDownloading->SetLastError(aError); | 
| 6 | 283 | } | 
| 2 | 284 | |
| 285 | if (aError == KErrNone) | |
| 286 | 				{
 | |
| 287 | TBuf<KMimeBufLength> mimeType; | |
| 288 | GetMimeType(iShowDownloading->FileName(), mimeType); | |
| 289 | _LIT(KMimeAudio,"audio"); | |
| 290 | _LIT(KMimeVideo,"video"); | |
| 291 | if (mimeType.Left(5) == KMimeAudio) | |
| 292 | 					{
 | |
| 293 | iShowDownloading->SetShowType(EAudioPodcast); | |
| 294 | } | |
| 295 | else if (mimeType.Left(5) == KMimeVideo) | |
| 296 | 					{
 | |
| 297 | iShowDownloading->SetShowType(EVideoPodcast); | |
| 298 | } | |
| 299 | ||
| 300 | iShowDownloading->SetDownloadState(EDownloaded); | |
| 301 | DBUpdateShow(*iShowDownloading); | |
| 302 | DBRemoveDownload(iShowDownloading->Uid()); | |
| 303 | AddShowToMpxCollection(*iShowDownloading); | |
| 304 | NotifyShowFinishedL(aError); | |
| 15 
93d9f66bf50b
Cleaning description better for second line in search results
 teknolog parents: 
6diff
changeset | 305 | iDownloadErrors = 0; | 
| 2 | 306 | delete iShowDownloading; | 
| 307 | iShowDownloading = NULL; | |
| 308 | } | |
| 309 | else | |
| 310 | 				{
 | |
| 311 | // 400 and 500 series errors are serious errors on which probably another download will fail | |
| 312 | if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200) | |
| 313 | 					{
 | |
| 314 | iShowDownloading->SetDownloadState(EFailedDownload); | |
| 315 | DBUpdateShow(*iShowDownloading); | |
| 316 | DBRemoveDownload(iShowDownloading->Uid()); | |
| 317 | NotifyShowFinishedL(aError); | |
| 318 | ||
| 319 | delete iShowDownloading; | |
| 320 | iShowDownloading = NULL; | |
| 321 | } | |
| 322 | else // other kind of error, missing network etc, reque this show | |
| 323 | 					{
 | |
| 324 | iShowDownloading->SetDownloadState(EQueued); | |
| 325 | DBUpdateShow(*iShowDownloading); | |
| 326 | } | |
| 327 | ||
| 328 | iDownloadErrors++; | |
| 329 | if (iDownloadErrors > KMaxDownloadErrors) | |
| 330 | 					{
 | |
| 331 | 					DP("Too many downloading errors, suspending downloads");
 | |
| 332 | iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); | |
| 333 | NotifyShowFinishedL(aError); | |
| 334 | } | |
| 335 | } | |
| 336 | DownloadNextShowL(); | |
| 337 | } | |
| 338 | ||
| 339 | else | |
| 340 | 			{
 | |
| 341 | // Connection error | |
| 342 | if(iShowDownloading) | |
| 343 | 				{
 | |
| 344 | iShowDownloading->SetDownloadState(EQueued); | |
| 345 | DBUpdateShow(*iShowDownloading); | |
| 346 | } | |
| 347 | iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); | |
| 348 | NotifyShowFinishedL(aError); | |
| 349 | } | |
| 350 | } | |
| 351 | } | |
| 352 | ||
| 353 | EXPORT_C CShowInfo* CShowEngine::ShowDownloading() | |
| 354 | 	{
 | |
| 355 | return iShowDownloading; | |
| 356 | } | |
| 357 | ||
| 358 | EXPORT_C CShowInfo* CShowEngine::GetShowByUidL(TUint aShowUid) | |
| 359 | 	{
 | |
| 360 | return DBGetShowByUidL(aShowUid); | |
| 361 | } | |
| 362 | CShowInfo* CShowEngine::DBGetShowByUidL(TUint aUid) | |
| 363 | 	{
 | |
| 364 | 	DP("CShowEngine::DBGetShowByUid");
 | |
| 365 | CShowInfo *showInfo = NULL; | |
| 366 | _LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype from shows where uid=%u"); | |
| 367 | iSqlBuffer.Format(KSqlStatement, aUid); | |
| 368 | ||
| 369 | sqlite3_stmt *st; | |
| 370 | ||
| 371 | 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
 | |
| 372 | ||
| 373 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 374 | &st, (const void**) NULL); | |
| 375 | ||
| 376 | if (rc == SQLITE_OK) | |
| 377 | 		{
 | |
| 378 | rc = sqlite3_step(st); | |
| 379 | Cleanup_sqlite3_finalize_PushL(st); | |
| 380 | if (rc == SQLITE_ROW) | |
| 381 | 			{
 | |
| 382 | showInfo = CShowInfo::NewLC(); | |
| 383 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 384 | CleanupStack::Pop(showInfo); | |
| 385 | } | |
| 386 | CleanupStack::PopAndDestroy();//st | |
| 387 | } | |
| 388 | ||
| 389 | return showInfo; | |
| 390 | } | |
| 391 | ||
| 392 | EXPORT_C CShowInfo* CShowEngine::DBGetShowByFileNameL(TFileName aFileName) | |
| 393 | 	{
 | |
| 394 | 	DP("CShowEngine::DBGetShowByUid");
 | |
| 395 | CShowInfo *showInfo = NULL; | |
| 396 | _LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype from shows where filename=\"%S\""); | |
| 397 | iSqlBuffer.Format(KSqlStatement, &aFileName); | |
| 398 | ||
| 399 | sqlite3_stmt *st; | |
| 400 | ||
| 401 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 402 | &st, (const void**) NULL); | |
| 403 | ||
| 404 | if (rc == SQLITE_OK) | |
| 405 | 		{
 | |
| 406 | rc = sqlite3_step(st); | |
| 407 | Cleanup_sqlite3_finalize_PushL(st); | |
| 408 | if (rc == SQLITE_ROW) | |
| 409 | 			{
 | |
| 410 | showInfo = CShowInfo::NewLC(); | |
| 411 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 412 | CleanupStack::Pop(showInfo); | |
| 413 | } | |
| 414 | CleanupStack::PopAndDestroy();//st | |
| 415 | } | |
| 416 | ||
| 417 | return showInfo; | |
| 418 | } | |
| 419 | ||
| 420 | void CShowEngine::DBGetAllShowsL(RShowInfoArray& aShowArray) | |
| 421 | 	{
 | |
| 422 | 	DP("CShowEngine::DBGetAllShows");
 | |
| 423 | _LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype from shows"); | |
| 424 | iSqlBuffer.Format(KSqlStatement); | |
| 425 | ||
| 426 | sqlite3_stmt *st; | |
| 427 | ||
| 428 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 429 | &st, (const void**) NULL); | |
| 430 | ||
| 431 | if (rc == SQLITE_OK) | |
| 432 | 		{
 | |
| 433 | rc = sqlite3_step(st); | |
| 434 | Cleanup_sqlite3_finalize_PushL(st); | |
| 435 | while (rc == SQLITE_ROW) | |
| 436 | 			{
 | |
| 437 | CShowInfo* showInfo = CShowInfo::NewLC(); | |
| 438 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 439 | aShowArray.Append(showInfo); | |
| 440 | CleanupStack::Pop(showInfo); | |
| 441 | rc = sqlite3_step(st); | |
| 442 | } | |
| 443 | CleanupStack::PopAndDestroy();//st | |
| 444 | } | |
| 445 | ||
| 446 | } | |
| 447 | ||
| 448 | void CShowEngine::DBGetAllDownloadsL(RShowInfoArray& aShowArray) | |
| 449 | 	{
 | |
| 450 | 	DP("CShowEngine::DBGetAllDownloads");
 | |
| 451 | _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"); | |
| 452 | iSqlBuffer.Format(KSqlStatement); | |
| 453 | ||
| 454 | #ifndef DONT_SORT_SQL | |
| 455 | _LIT(KSqlSort, " order by dl_index"); | |
| 456 | iSqlBuffer.Append(KSqlSort); | |
| 457 | #endif | |
| 458 | sqlite3_stmt *st; | |
| 459 | ||
| 460 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 461 | &st, (const void**) NULL); | |
| 462 | ||
| 463 | if (rc == SQLITE_OK) | |
| 464 | 		{
 | |
| 465 | rc = sqlite3_step(st); | |
| 466 | Cleanup_sqlite3_finalize_PushL(st); | |
| 467 | while (rc == SQLITE_ROW) | |
| 468 | 			{
 | |
| 469 | CShowInfo* showInfo = CShowInfo::NewLC(); | |
| 470 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 471 | aShowArray.Append(showInfo); | |
| 472 | CleanupStack::Pop(showInfo); | |
| 473 | rc = sqlite3_step(st); | |
| 474 | } | |
| 475 | CleanupStack::PopAndDestroy();//st | |
| 476 | } | |
| 477 | ||
| 478 | // delete downloads that don't have a show | |
| 479 | ||
| 480 | _LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)"); | |
| 481 | iSqlBuffer.Format(KSqlStatement2); | |
| 482 | ||
| 483 | rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, (const void**) NULL); | |
| 484 | ||
| 485 | if (rc == SQLITE_OK) | |
| 486 | 		{
 | |
| 487 | rc = sqlite3_step(st); | |
| 488 | sqlite3_finalize(st); | |
| 489 | } | |
| 490 | } | |
| 491 | ||
| 492 | CShowInfo* CShowEngine::DBGetNextDownloadL() | |
| 493 | 	{
 | |
| 494 | 	DP("CShowEngine::DBGetNextDownload");
 | |
| 495 | CShowInfo *showInfo = NULL; | |
| 496 | _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"); | |
| 497 | iSqlBuffer.Format(KSqlStatement); | |
| 498 | ||
| 499 | #ifdef DONT_SORT_SQL | |
| 500 | _LIT(KSqlSort, " limit 1"); | |
| 501 | iSqlBuffer.Append(KSqlSort); | |
| 502 | #else | |
| 503 | _LIT(KSqlNoSort, " order by dl_index limit 1"); | |
| 504 | iSqlBuffer.Append(KSqlNoSort); | |
| 505 | #endif | |
| 506 | ||
| 507 | sqlite3_stmt *st; | |
| 508 | ||
| 509 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 510 | &st, (const void**) NULL); | |
| 511 | ||
| 512 | if (rc == SQLITE_OK) | |
| 513 | 		{
 | |
| 514 | rc = sqlite3_step(st); | |
| 515 | Cleanup_sqlite3_finalize_PushL(st); | |
| 516 | if (rc == SQLITE_ROW) | |
| 517 | 			{
 | |
| 518 | showInfo = CShowInfo::NewLC(); | |
| 519 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 520 | CleanupStack::Pop(showInfo); | |
| 521 | } | |
| 522 | CleanupStack::PopAndDestroy();//st | |
| 523 | } | |
| 524 | ||
| 525 | return showInfo; | |
| 526 | } | |
| 527 | ||
| 528 | void CShowEngine::DBGetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid) | |
| 529 | 	{
 | |
| 530 | 	DP1("CShowEngine::DBGetShowsByFeed BEGIN, feedUid=%u", aFeedUid);
 | |
| 531 | _LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype, lasterror from shows where feeduid=%u"); | |
| 532 | iSqlBuffer.Format(KSqlStatement, aFeedUid); | |
| 533 | ||
| 534 | #ifndef DONT_SORT_SQL | |
| 535 | _LIT(KSqlOrderByDate, " order by pubdate desc"); | |
| 536 | iSqlBuffer.Append(KSqlOrderByDate); | |
| 537 | #endif | |
| 538 | ||
| 539 | sqlite3_stmt *st; | |
| 540 | ||
| 541 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 542 | &st, (const void**) NULL); | |
| 543 | ||
| 544 | if (rc == SQLITE_OK) | |
| 545 | 		{
 | |
| 546 | rc = sqlite3_step(st); | |
| 547 | Cleanup_sqlite3_finalize_PushL(st); | |
| 548 | while (rc == SQLITE_ROW) | |
| 549 | 			{
 | |
| 550 | CShowInfo* showInfo = CShowInfo::NewLC(); | |
| 551 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 552 | aShowArray.Append(showInfo); | |
| 553 | CleanupStack::Pop(showInfo); | |
| 554 | rc = sqlite3_step(st); | |
| 555 | } | |
| 556 | CleanupStack::PopAndDestroy();//st | |
| 557 | } | |
| 558 | 	DP("CShowEngine::DBGetShowsByFeed END");
 | |
| 559 | } | |
| 560 | ||
| 561 | TUint CShowEngine::DBGetDownloadsCount() | |
| 562 | 	{
 | |
| 563 | 	DP("CShowEngine::DBGetDownloadsCount");
 | |
| 564 | ||
| 565 | _LIT(KSqlStatement, "select count(*) from downloads"); | |
| 566 | iSqlBuffer.Format(KSqlStatement); | |
| 567 | ||
| 568 | sqlite3_stmt *st; | |
| 569 | TUint count = 0; | |
| 570 | ||
| 571 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 572 | &st, (const void**) NULL); | |
| 573 | ||
| 574 | if (rc == SQLITE_OK) | |
| 575 | 		{
 | |
| 576 | rc = sqlite3_step(st); | |
| 577 | ||
| 578 | if (rc == SQLITE_ROW) | |
| 579 | 			{
 | |
| 580 | count = sqlite3_column_int(st, 0); | |
| 581 | } | |
| 582 | sqlite3_finalize(st); | |
| 583 | } | |
| 584 | return count; | |
| 585 | } | |
| 586 | ||
| 587 | void CShowEngine::DBGetDownloadedShowsL(RShowInfoArray& aShowArray) | |
| 588 | 	{
 | |
| 589 | 	DP("CShowEngine::DBGetDownloadedShows");
 | |
| 590 | _LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype, lasterror from shows where downloadstate=%u"); | |
| 591 | iSqlBuffer.Format(KSqlStatement, EDownloaded); | |
| 592 | ||
| 593 | #ifndef DONT_SORT_SQL | |
| 594 | _LIT(KSqlSort, " order by title"); | |
| 595 | iSqlBuffer.Append(KSqlSort); | |
| 596 | #endif | |
| 597 | ||
| 598 | sqlite3_stmt *st; | |
| 599 | ||
| 600 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 601 | &st, (const void**) NULL); | |
| 602 | ||
| 603 | if (rc == SQLITE_OK) | |
| 604 | 		{
 | |
| 605 | rc = sqlite3_step(st); | |
| 606 | Cleanup_sqlite3_finalize_PushL(st); | |
| 607 | while (rc == SQLITE_ROW) | |
| 608 | 			{
 | |
| 609 | CShowInfo* showInfo = CShowInfo::NewLC(); | |
| 610 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 611 | aShowArray.Append(showInfo); | |
| 612 | CleanupStack::Pop(showInfo); | |
| 613 | rc = sqlite3_step(st); | |
| 614 | } | |
| 615 | CleanupStack::PopAndDestroy();//st | |
| 616 | } | |
| 617 | } | |
| 618 | ||
| 619 | void CShowEngine::DBGetNewShowsL(RShowInfoArray& aShowArray) | |
| 620 | 	{
 | |
| 621 | 	DP("CShowEngine::DBGetNewShows");
 | |
| 622 | _LIT(KSqlStatement, "select url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype, lasterror from shows where playstate=%u"); | |
| 623 | iSqlBuffer.Format(KSqlStatement, ENeverPlayed); | |
| 624 | ||
| 625 | sqlite3_stmt *st; | |
| 626 | ||
| 627 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 628 | &st, (const void**) NULL); | |
| 629 | ||
| 630 | if (rc == SQLITE_OK) | |
| 631 | 		{
 | |
| 632 | rc = sqlite3_step(st); | |
| 633 | Cleanup_sqlite3_finalize_PushL(st); | |
| 634 | while (rc == SQLITE_ROW) | |
| 635 | 			{
 | |
| 636 | CShowInfo* showInfo = CShowInfo::NewLC(); | |
| 637 | DBFillShowInfoFromStmtL(st, showInfo); | |
| 638 | aShowArray.Append(showInfo); | |
| 639 | CleanupStack::Pop(showInfo); | |
| 640 | rc = sqlite3_step(st); | |
| 641 | } | |
| 642 | CleanupStack::PopAndDestroy();//st | |
| 643 | } | |
| 644 | } | |
| 645 | ||
| 646 | void CShowEngine::DBDeleteOldShowsByFeed(TUint aFeedUid) | |
| 647 | 	{
 | |
| 648 | 	DP("CShowEngine::DBDeleteOldShows");
 | |
| 649 | ||
| 650 | // what we do: | |
| 651 | // 1. sort shows by pubdate | |
| 652 | // 2. select the first MaxListItems shows | |
| 653 | // 3. delete the rest if downloadstate is ENotDownloaded | |
| 654 | ||
| 655 | _LIT(KSqlStatement,"delete from shows where feeduid=%u and downloadstate=0 and uid not in " \ | |
| 656 | "(select uid from shows where feeduid=%u order by pubdate desc limit %u)"); | |
| 657 | iSqlBuffer.Format(KSqlStatement, aFeedUid, aFeedUid, iPodcastModel.SettingsEngine().MaxListItems()); | |
| 658 | ||
| 659 | sqlite3_stmt *st; | |
| 660 | ||
| 661 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 662 | &st, (const void**) NULL); | |
| 663 | ||
| 664 | if (rc == SQLITE_OK) | |
| 665 | 		{
 | |
| 666 | rc = sqlite3_step(st); | |
| 667 | sqlite3_finalize(st); | |
| 668 | } | |
| 669 | ||
| 670 | _LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)"); | |
| 671 | iSqlBuffer.Format(KSqlStatement2); | |
| 672 | ||
| 673 | rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, (const void**) NULL); | |
| 674 | ||
| 675 | if (rc == SQLITE_OK) | |
| 676 | 		{
 | |
| 677 | rc = sqlite3_step(st); | |
| 678 | sqlite3_finalize(st); | |
| 679 | } | |
| 680 | } | |
| 681 | ||
| 682 | void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo) | |
| 683 | 	{
 | |
| 684 | const void *urlz = sqlite3_column_text16(st, 0); | |
| 685 | TPtrC16 url((const TUint16*) urlz); | |
| 686 | showInfo->SetUrlL(url); | |
| 687 | ||
| 688 | const void *titlez = sqlite3_column_text16(st, 1); | |
| 689 | TPtrC16 title((const TUint16*) titlez); | |
| 690 | showInfo->SetTitleL(title); | |
| 691 | ||
| 692 | const void *descz = sqlite3_column_text16(st, 2); | |
| 693 | TPtrC16 desc((const TUint16*) descz); | |
| 694 | showInfo->SetDescriptionL(desc); | |
| 695 | ||
| 696 | const void *filez = sqlite3_column_text16(st, 3); | |
| 697 | TPtrC16 file((const TUint16*) filez); | |
| 698 | showInfo->SetFileNameL(file); | |
| 699 | ||
| 700 | sqlite3_int64 pos = sqlite3_column_int64(st, 4); | |
| 701 | TTimeIntervalMicroSeconds position(pos); | |
| 702 | showInfo->SetPosition(position); | |
| 703 | ||
| 704 | TUint playtime = sqlite3_column_int(st, 5); | |
| 705 | showInfo->SetPlayTime(playtime); | |
| 706 | ||
| 707 | TUint playstate = sqlite3_column_int(st, 6); | |
| 708 | showInfo->SetPlayState((TPlayState) playstate); | |
| 709 | ||
| 710 | TUint downloadstate = sqlite3_column_int(st, 7); | |
| 711 | showInfo->SetDownloadState((TDownloadState) downloadstate); | |
| 712 | ||
| 713 | TUint feeduid = sqlite3_column_int(st, 8); | |
| 714 | showInfo->SetFeedUid(feeduid); | |
| 715 | ||
| 716 | TUint uid = sqlite3_column_int(st, 9); | |
| 717 | showInfo->SetUid(uid); | |
| 718 | ||
| 719 | TUint showsize = sqlite3_column_int(st, 10); | |
| 720 | showInfo->SetShowSize(showsize); | |
| 721 | ||
| 722 | TUint trackno = sqlite3_column_int(st, 11); | |
| 723 | showInfo->SetTrackNo((TShowType) trackno); | |
| 724 | ||
| 725 | sqlite3_int64 pubdate = sqlite3_column_int64(st, 12); | |
| 726 | TTime timepubdate(pubdate); | |
| 727 | showInfo->SetPubDate(timepubdate); | |
| 728 | ||
| 729 | TUint showtype = sqlite3_column_int(st, 13); | |
| 730 | showInfo->SetShowType((TShowType) showtype); | |
| 731 | ||
| 732 | TInt lasterror = sqlite3_column_int(st, 14); | |
| 733 | showInfo->SetLastError(lasterror); | |
| 734 | } | |
| 735 | ||
| 736 | TBool CShowEngine::DBAddShow(const CShowInfo& aItem) | |
| 737 | 	{
 | |
| 738 | 	DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url());
 | |
| 739 | ||
| 740 | _LIT(KSqlStatement, "insert into shows (url, title, description, filename, position, playtime, playstate, downloadstate, feeduid, uid, showsize, trackno, pubdate, showtype)" | |
| 741 | " values (\"%S\",\"%S\", \"%S\", \"%S\", \"%Lu\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\", \"%Lu\", \"%d\")"); | |
| 742 | iSqlBuffer.Format(KSqlStatement, &aItem.Url(), &aItem.Title(), &aItem.Description(), | |
| 743 | &aItem.FileName(), aItem.Position().Int64(), aItem.PlayTime(), | |
| 744 | aItem.PlayState(), aItem.DownloadState(), aItem.FeedUid(), | |
| 745 | aItem.Uid(), aItem.ShowSize(), aItem.TrackNo(), | |
| 746 | aItem.PubDate().Int64(), aItem.ShowType()); | |
| 747 | ||
| 748 | sqlite3_stmt *st; | |
| 749 | ||
| 750 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 751 | &st, (const void**) NULL); | |
| 752 | if (rc == SQLITE_OK) | |
| 753 | 		{
 | |
| 754 | rc = sqlite3_step(st); | |
| 755 | if (rc == SQLITE_DONE) | |
| 756 | 			{
 | |
| 757 | sqlite3_finalize(st); | |
| 758 | return ETrue; | |
| 759 | } | |
| 760 | else | |
| 761 | 			{
 | |
| 762 | sqlite3_finalize(st); | |
| 763 | } | |
| 764 | } | |
| 765 | else | |
| 766 | 		{
 | |
| 767 | 		DP1("SQLite rc=%d", rc);
 | |
| 768 | } | |
| 769 | ||
| 770 | return EFalse; | |
| 771 | } | |
| 772 | ||
| 773 | void CShowEngine::DBAddDownload(TUint aUid) | |
| 774 | 	{
 | |
| 775 | 	DP1("CShowEngine::DBAddDownload, aUid=%u", aUid);
 | |
| 776 | ||
| 777 | _LIT(KSqlStatement, "insert into downloads (uid) values (%u)"); | |
| 778 | iSqlBuffer.Format(KSqlStatement, aUid); | |
| 779 | sqlite3_stmt *st; | |
| 780 | ||
| 781 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 782 | &st, (const void**) NULL); | |
| 783 | ||
| 784 | if (rc == SQLITE_OK) | |
| 785 | 		{
 | |
| 786 | rc = sqlite3_step(st); | |
| 787 | } | |
| 788 | ||
| 789 | sqlite3_finalize(st); | |
| 790 | } | |
| 791 | ||
| 792 | TBool CShowEngine::DBUpdateShow(CShowInfo& aItem) | |
| 793 | 	{
 | |
| 794 | 	DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title());
 | |
| 795 | ||
| 796 | _LIT(KSqlStatement, "update shows set url=\"%S\", title=\"%S\", description=\"%S\", filename=\"%S\", position=\"%Lu\"," | |
| 797 | "playtime=\"%u\", playstate=\"%u\", downloadstate=\"%u\", feeduid=\"%u\", showsize=\"%u\", trackno=\"%u\"," | |
| 798 | "pubdate=\"%Lu\", showtype=\"%d\", lasterror=\"%d\" where uid=\"%u\""); | |
| 799 | iSqlBuffer.Format(KSqlStatement, &aItem.Url(), &aItem.Title(), &aItem.Description(), | |
| 800 | &aItem.FileName(), aItem.Position().Int64(), aItem.PlayTime(), | |
| 801 | aItem.PlayState(), aItem.DownloadState(), aItem.FeedUid(), | |
| 802 | aItem.ShowSize(), aItem.TrackNo(), aItem.PubDate().Int64(), | |
| 803 | aItem.ShowType(), aItem.LastError(), aItem.Uid()); | |
| 804 | ||
| 805 | sqlite3_stmt *st; | |
| 806 | ||
| 807 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 808 | &st, (const void**) NULL); | |
| 809 | ||
| 810 | if (rc == SQLITE_OK) | |
| 811 | 		{
 | |
| 812 | rc = sqlite3_step(st); | |
| 813 | ||
| 814 | if (rc == SQLITE_DONE) | |
| 815 | 			{
 | |
| 816 | sqlite3_finalize(st); | |
| 817 | return ETrue; | |
| 818 | } | |
| 819 | else | |
| 820 | 			{
 | |
| 821 | sqlite3_finalize(st); | |
| 822 | } | |
| 823 | } | |
| 824 | else | |
| 825 | 		{
 | |
| 826 | 		DP1("SQLite rc=%d", rc);
 | |
| 827 | } | |
| 828 | ||
| 829 | return EFalse; | |
| 830 | } | |
| 831 | ||
| 832 | TBool CShowEngine::DBDeleteShow(TUint aUid) | |
| 833 | 	{
 | |
| 834 | 	DP("CShowEngine::DBDeleteShow");
 | |
| 835 | ||
| 836 | _LIT(KSqlStatement, "delete from shows where uid=%u"); | |
| 837 | iSqlBuffer.Format(KSqlStatement, aUid); | |
| 838 | ||
| 839 | sqlite3_stmt *st; | |
| 840 | ||
| 841 | 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
 | |
| 842 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 843 | &st, (const void**) NULL); | |
| 844 | ||
| 845 | if (rc == SQLITE_OK) | |
| 846 | 		{
 | |
| 847 | rc = sqlite3_step(st); | |
| 848 | ||
| 849 | if (rc == SQLITE_DONE) | |
| 850 | 			{
 | |
| 851 | sqlite3_finalize(st); | |
| 852 | return ETrue; | |
| 853 | } | |
| 854 | else | |
| 855 | 			{
 | |
| 856 | sqlite3_finalize(st); | |
| 857 | } | |
| 858 | } | |
| 859 | else | |
| 860 | 		{
 | |
| 861 | 		DP1("SQLite rc=%d", rc);
 | |
| 862 | } | |
| 863 | ||
| 864 | return EFalse; | |
| 865 | } | |
| 866 | ||
| 867 | TBool CShowEngine::DBDeleteAllShowsByFeed(TUint aFeedUid) | |
| 868 | 	{
 | |
| 869 | 	DP("CShowEngine::DBDeleteAllShowsByFeed");
 | |
| 870 | ||
| 871 | _LIT(KSqlStatement, "delete from shows where feeduid=%u"); | |
| 872 | iSqlBuffer.Format(KSqlStatement, aFeedUid); | |
| 873 | ||
| 874 | sqlite3_stmt *st; | |
| 875 | ||
| 876 | 	//DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen));
 | |
| 877 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 878 | &st, (const void**) NULL); | |
| 879 | ||
| 880 | if (rc == SQLITE_OK) | |
| 881 | 		{
 | |
| 882 | rc = sqlite3_step(st); | |
| 883 | ||
| 884 | if (rc == SQLITE_DONE) | |
| 885 | 			{
 | |
| 886 | sqlite3_finalize(st); | |
| 887 | return ETrue; | |
| 888 | } | |
| 889 | else | |
| 890 | 			{
 | |
| 891 | sqlite3_finalize(st); | |
| 892 | } | |
| 893 | } | |
| 894 | else | |
| 895 | 		{
 | |
| 896 | 		DP1("SQLite rc=%d", rc);
 | |
| 897 | } | |
| 898 | ||
| 899 | return EFalse; | |
| 900 | } | |
| 901 | ||
| 902 | void CShowEngine::DBRemoveAllDownloads() | |
| 903 | 	{
 | |
| 904 | 	DP("CShowEngine::DBRemoveAllDownloads");
 | |
| 905 | ||
| 906 | _LIT(KSqlStatement, "delete from downloads"); | |
| 907 | iSqlBuffer.Format(KSqlStatement); | |
| 908 | ||
| 909 | sqlite3_stmt *st; | |
| 910 | ||
| 911 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 912 | &st, (const void**) NULL); | |
| 913 | ||
| 914 | if (rc == SQLITE_OK) | |
| 915 | 		{
 | |
| 916 | rc = sqlite3_step(st); | |
| 917 | sqlite3_finalize(st); | |
| 918 | } | |
| 919 | ||
| 920 | _LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1"); | |
| 921 | iSqlBuffer.Format(KSqlStatement2); | |
| 922 | ||
| 923 | rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, | |
| 924 | (const void**) NULL); | |
| 925 | ||
| 926 | if (rc == SQLITE_OK) | |
| 927 | 		{
 | |
| 928 | rc = sqlite3_step(st); | |
| 929 | sqlite3_finalize(st); | |
| 930 | } | |
| 931 | ||
| 932 | } | |
| 933 | ||
| 934 | void CShowEngine::DBRemoveDownload(TUint aUid) | |
| 935 | 	{
 | |
| 936 | 	DP("CShowEngine::DBRemoveDownload");
 | |
| 937 | ||
| 938 | _LIT(KSqlStatement, "delete from downloads where uid=%u"); | |
| 939 | iSqlBuffer.Format(KSqlStatement, aUid); | |
| 940 | ||
| 941 | sqlite3_stmt *st; | |
| 942 | ||
| 943 | int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, | |
| 944 | &st, (const void**) NULL); | |
| 945 | ||
| 946 | if (rc == SQLITE_OK) | |
| 947 | 		{
 | |
| 948 | rc = sqlite3_step(st); | |
| 949 | sqlite3_finalize(st); | |
| 950 | } | |
| 951 | } | |
| 952 | ||
| 953 | EXPORT_C CShowInfo* CShowEngine::GetNextShowByTrackL(CShowInfo* aShowInfo) | |
| 954 | 	{
 | |
| 955 | CShowInfo* nextShow = NULL; | |
| 956 | RShowInfoArray array; | |
| 957 | DBGetShowsByFeedL(array, aShowInfo->FeedUid()); | |
| 958 | TUint diff = KMaxTInt; | |
| 959 | for (TInt loop = 0; loop < array.Count(); loop++) | |
| 960 | 		{
 | |
| 961 | if (aShowInfo->TrackNo() < array[loop]->TrackNo()) | |
| 962 | 			{
 | |
| 963 | if ((array[loop]->TrackNo() - aShowInfo->TrackNo()) < diff) | |
| 964 | 				{
 | |
| 965 | diff = array[loop]->TrackNo() - aShowInfo->TrackNo(); | |
| 966 | nextShow = array[loop]; | |
| 967 | } | |
| 968 | } | |
| 969 | } | |
| 970 | array.ResetAndDestroy(); | |
| 971 | return nextShow; | |
| 972 | } | |
| 973 | ||
| 974 | TBool CShowEngine::CompareShowsByUid(const CShowInfo &a, const CShowInfo &b) | |
| 975 | 	{
 | |
| 976 | return a.Uid() == b.Uid(); | |
| 977 | } | |
| 978 | ||
| 979 | TInt CShowEngine::CompareShowsByDate(const CShowInfo &a, const CShowInfo &b) | |
| 980 | 	{
 | |
| 981 | if (a.PubDate() > b.PubDate()) | |
| 982 | 		{
 | |
| 983 | 		//		DP2("Sorting %S less than %S", &a.iTitle, &b.iTitle);
 | |
| 984 | return -1; | |
| 985 | } | |
| 986 | else if (a.PubDate() == b.PubDate()) | |
| 987 | 		{
 | |
| 988 | 		//		DP2("Sorting %S equal to %S", &a.iTitle, &b.iTitle);
 | |
| 989 | return 0; | |
| 990 | } | |
| 991 | else | |
| 992 | 		{
 | |
| 993 | 		//		DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle);
 | |
| 994 | return 1; | |
| 995 | } | |
| 996 | } | |
| 997 | ||
| 998 | TInt CShowEngine::CompareShowsByTrackNo(const CShowInfo &a, const CShowInfo &b) | |
| 999 | 	{
 | |
| 1000 | if (a.TrackNo() < b.TrackNo()) | |
| 1001 | 		{
 | |
| 1002 | return -1; | |
| 1003 | } | |
| 1004 | else if (a.TrackNo() == b.TrackNo()) | |
| 1005 | 		{
 | |
| 1006 | return 0; | |
| 1007 | } | |
| 1008 | else | |
| 1009 | 		{
 | |
| 1010 | return 1; | |
| 1011 | } | |
| 1012 | } | |
| 1013 | ||
| 1014 | TInt CShowEngine::CompareShowsByTitle(const CShowInfo &a, const CShowInfo &b) | |
| 1015 | 	{
 | |
| 1016 | if (a.Title() < b.Title()) | |
| 1017 | 		{
 | |
| 1018 | 		//		DP2("Sorting %S less than %S", &a.iTitle, &b.iTitle);
 | |
| 1019 | return -1; | |
| 1020 | } | |
| 1021 | else if (a.Title() == b.Title()) | |
| 1022 | 		{
 | |
| 1023 | 		//		DP2("Sorting %S equal to %S", &a.iTitle, &b.iTitle);
 | |
| 1024 | return 0; | |
| 1025 | } | |
| 1026 | else | |
| 1027 | 		{
 | |
| 1028 | 		//		DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle);
 | |
| 1029 | return 1; | |
| 1030 | } | |
| 1031 | } | |
| 1032 | ||
| 1033 | EXPORT_C void CShowEngine::DeletePlayedShows(RShowInfoArray &aShowInfoArray) | |
| 1034 | 	{
 | |
| 1035 | for (TInt i = 0; i < aShowInfoArray.Count(); i++) | |
| 1036 | 		{
 | |
| 1037 | if (aShowInfoArray[i]->PlayState() == EPlayed | |
| 1038 | && aShowInfoArray[i]->FileName().Length() > 0) | |
| 1039 | 			{
 | |
| 1040 | if (CompareShowsByUid(*(iPodcastModel.PlayingPodcast()), *(aShowInfoArray[i])) | |
| 1041 | && iPodcastModel.SoundEngine().State() != ESoundEngineNotInitialized) | |
| 1042 | 				{
 | |
| 1043 | iPodcastModel.SoundEngine().Stop(); | |
| 1044 | } | |
| 1045 | BaflUtils::DeleteFile(iPodcastModel.FsSession(), aShowInfoArray[i]->FileName()); | |
| 1046 | aShowInfoArray[i]->SetDownloadState(ENotDownloaded); | |
| 1047 | DBUpdateShow(*aShowInfoArray[i]); | |
| 1048 | } | |
| 1049 | } | |
| 1050 | } | |
| 1051 | ||
| 1052 | EXPORT_C void CShowEngine::DeleteAllShowsByFeedL(TUint aFeedUid, TBool aDeleteFiles) | |
| 1053 | 	{
 | |
| 1054 | RShowInfoArray array; | |
| 1055 | DBGetShowsByFeedL(array, aFeedUid); | |
| 1056 | ||
| 1057 | const TInt count = array.Count(); | |
| 1058 | ||
| 1059 | for (TInt i = count - 1; i >= 0; i--) | |
| 1060 | 		{
 | |
| 1061 | if (array[i]->FileName().Length() > 0) | |
| 1062 | 			{
 | |
| 1063 | if (aDeleteFiles) | |
| 1064 | 				{
 | |
| 1065 | BaflUtils::DeleteFile(iPodcastModel.FsSession(), array[i]->FileName()); | |
| 1066 | } | |
| 1067 | } | |
| 1068 | } | |
| 1069 | array.ResetAndDestroy(); | |
| 1070 | DBDeleteAllShowsByFeed(aFeedUid); | |
| 1071 | } | |
| 1072 | ||
| 1073 | EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid) | |
| 1074 | 	{
 | |
| 1075 | DBDeleteOldShowsByFeed(aFeedUid); | |
| 1076 | } | |
| 1077 | ||
| 1078 | EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile) | |
| 1079 | 	{
 | |
| 1080 | ||
| 1081 | CShowInfo *info = DBGetShowByUidL(aShowUid); | |
| 1082 | ||
| 1083 | if (info != NULL) | |
| 1084 | 		{
 | |
| 1085 | if (info->FileName().Length() > 0 && aRemoveFile) | |
| 1086 | 			{
 | |
| 1087 | BaflUtils::DeleteFile(iPodcastModel.FsSession(), info->FileName()); | |
| 1088 | } | |
| 1089 | ||
| 1090 | info->SetDownloadState(ENotDownloaded); | |
| 1091 | DBUpdateShow(*info); | |
| 1092 | delete info; | |
| 1093 | } | |
| 1094 | } | |
| 1095 | ||
| 1096 | EXPORT_C void CShowEngine::GetShowsByFeedL(RShowInfoArray& aShowArray, TUint aFeedUid) | |
| 1097 | 	{
 | |
| 1098 | 	DP("CShowEngine::GetShowsByFeed");
 | |
| 1099 | DBGetShowsByFeedL(aShowArray, aFeedUid); | |
| 1100 | } | |
| 1101 | ||
| 1102 | EXPORT_C void CShowEngine::GetAllShowsL(RShowInfoArray &aArray) | |
| 1103 | 	{
 | |
| 1104 | 	DP("CShowEngine::GetAllShows");
 | |
| 1105 | DBGetAllShowsL(aArray); | |
| 1106 | } | |
| 1107 | ||
| 1108 | EXPORT_C void CShowEngine::GetShowsDownloadedL(RShowInfoArray &aArray) | |
| 1109 | 	{
 | |
| 1110 | 	DP("CShowEngine::GetShowsDownloaded");
 | |
| 1111 | DBGetDownloadedShowsL(aArray); | |
| 1112 | } | |
| 1113 | ||
| 1114 | EXPORT_C void CShowEngine::GetNewShowsL(RShowInfoArray &aArray) | |
| 1115 | 	{
 | |
| 1116 | 	DP("CShowEngine::GetNewShows");
 | |
| 1117 | DBGetNewShowsL(aArray); | |
| 1118 | } | |
| 1119 | ||
| 1120 | EXPORT_C void CShowEngine::GetShowsDownloadingL(RShowInfoArray &aArray) | |
| 1121 | 	{
 | |
| 1122 | 	DP("CShowEngine::GetShowsDownloading");
 | |
| 1123 | DBGetAllDownloadsL(aArray); | |
| 1124 | } | |
| 1125 | ||
| 1126 | EXPORT_C TInt CShowEngine::GetNumDownloadingShows() | |
| 1127 | 	{
 | |
| 1128 | return (const TInt) DBGetDownloadsCount(); | |
| 1129 | } | |
| 1130 | ||
| 1131 | EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo) | |
| 1132 | 	{
 | |
| 1133 | aInfo.SetDownloadState(EQueued); | |
| 1134 | DBUpdateShow(aInfo); | |
| 1135 | DBAddDownload(aInfo.Uid()); | |
| 1136 | DownloadNextShowL(); | |
| 1137 | } | |
| 1138 | ||
| 1139 | void CShowEngine::DownloadNextShowL() | |
| 1140 | 	{
 | |
| 1141 | 	DP("CShowEngine::DownloadNextShowL BEGIN");
 | |
| 1142 | // Check if we have anything in the download queue | |
| 1143 | const TInt count = DBGetDownloadsCount(); | |
| 1144 | 	DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count);
 | |
| 1145 | ||
| 1146 | // Inform the observers | |
| 1147 | NotifyDownloadQueueUpdatedL(); | |
| 1148 | ||
| 1149 | if (count > 0) | |
| 1150 | 		{
 | |
| 1151 | if (iPodcastModel.SettingsEngine().DownloadSuspended()) | |
| 1152 | 			{
 | |
| 1153 | 			DP("CShowEngine::DownloadNextShow\tDownload process is suspended, ABORTING");
 | |
| 1154 | return; | |
| 1155 | } | |
| 1156 | else if (iShowClient->IsActive()) | |
| 1157 | 			{
 | |
| 1158 | 			DP("CShowEngine::DownloadNextShow\tDownload process is already active.");
 | |
| 1159 | return; | |
| 1160 | } | |
| 1161 | else | |
| 1162 | 			{
 | |
| 1163 | ||
| 1164 | // Start the download | |
| 1165 | ||
| 1166 | CShowInfo *info = DBGetNextDownloadL(); | |
| 1167 | ||
| 1168 | while(info != NULL) | |
| 1169 | 				{
 | |
| 1170 | TBool getOk = EFalse; | |
| 1171 | 				DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title()));
 | |
| 1172 | info->SetDownloadState(EDownloading); | |
| 1173 | info->SetLastError(KErrNone); | |
| 1174 | DBUpdateShow(*info); | |
| 1175 | iShowDownloading = info; | |
| 1176 | TRAPD(error,getOk = GetShowL(info)); | |
| 1177 | if (error != KErrNone || !getOk) | |
| 1178 | 					{
 | |
| 1179 | info->SetDownloadState(EFailedDownload); | |
| 1180 | DBRemoveDownload(info->Uid()); | |
| 1181 | DBUpdateShow(*info); | |
| 1182 | info = DBGetNextDownloadL(); | |
| 1183 | ||
| 1184 | if(info == NULL) | |
| 1185 | 						{
 | |
| 1186 | iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); | |
| 1187 | iShowDownloading = NULL; | |
| 1188 | } | |
| 1189 | } | |
| 1190 | else | |
| 1191 | 					{
 | |
| 1192 | break; | |
| 1193 | } | |
| 1194 | } | |
| 1195 | } | |
| 1196 | } | |
| 1197 | else | |
| 1198 | 		{
 | |
| 1199 | 		iShowDownloading = NULL;DP("CShowEngine::DownloadNextShow\tNothing to download");
 | |
| 1200 | } | |
| 1201 | 	DP("CShowEngine::DownloadNextShowL END");
 | |
| 1202 | } | |
| 1203 | ||
| 1204 | void CShowEngine::NotifyDownloadQueueUpdatedL() | |
| 1205 | 	{
 | |
| 1206 | const TInt count = iObservers.Count(); | |
| 1207 | for (TInt i = 0; i < count; i++) | |
| 1208 | 		{
 | |
| 1209 | iObservers[i]->DownloadQueueUpdatedL(1, DBGetDownloadsCount() - 1); | |
| 1210 | } | |
| 1211 | } | |
| 1212 | ||
| 1213 | void CShowEngine::NotifyShowDownloadUpdatedL(TInt aBytesOfCurrentDownload, TInt aBytesTotal) | |
| 1214 | 	{
 | |
| 1215 | const TInt count = iObservers.Count(); | |
| 1216 | for (TInt i = 0; i < count; i++) | |
| 1217 | 		{
 | |
| 1218 | iObservers[i]->ShowDownloadUpdatedL(aBytesOfCurrentDownload, aBytesTotal); | |
| 1219 | } | |
| 1220 | } | |
| 1221 | ||
| 1222 | void CShowEngine::NotifyShowFinishedL(TInt aError) | |
| 1223 | 	{
 | |
| 1224 | const TInt count = iObservers.Count(); | |
| 1225 | for (TInt i = 0; i < count; i++) | |
| 1226 | 			{
 | |
| 1227 | iObservers[i]->ShowDownloadFinishedL(iShowDownloading?iShowDownloading->Uid():0, aError); | |
| 1228 | } | |
| 1229 | } | |
| 1230 | ||
| 1231 | EXPORT_C void CShowEngine::NotifyShowListUpdatedL() | |
| 1232 | 	{
 | |
| 1233 | for (TInt i = 0; i < iObservers.Count(); i++) | |
| 1234 | 		{
 | |
| 1235 | iObservers[i]->ShowListUpdatedL(); | |
| 1236 | } | |
| 1237 | } | |
| 1238 | ||
| 1239 | void CShowEngine::ReadMetaData(CShowInfo& aShowInfo) | |
| 1240 | 	{
 | |
| 1241 | 	//DP1("Read %S", &(aShowInfo->Title()));
 | |
| 1242 | DBUpdateShow(aShowInfo); | |
| 1243 | } | |
| 1244 | ||
| 1245 | void CShowEngine::ReadMetaDataCompleteL() | |
| 1246 | 	{
 | |
| 1247 | NotifyShowListUpdatedL(); | |
| 1248 | MetaDataReader().SetIgnoreTrackNo(EFalse); | |
| 1249 | } | |
| 1250 | ||
| 1251 | EXPORT_C void CShowEngine::UpdateShow(CShowInfo& aInfo) | |
| 1252 | 	{
 | |
| 1253 | DBUpdateShow(aInfo); | |
| 1254 | } | |
| 1255 | ||
| 1256 | EXPORT_C CMetaDataReader& CShowEngine::MetaDataReader() | |
| 1257 | 	{
 | |
| 1258 | return *iMetaDataReader; | |
| 1259 | } | |
| 1260 | ||
| 1261 | void CShowEngine::FileError(TUint /*aError*/) | |
| 1262 | 	{
 | |
| 1263 | //TODO: Error dialog | |
| 1264 | //StopDownloads(); | |
| 1265 | iDownloadErrors = KMaxDownloadErrors; | |
| 1266 | } |