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> |
33 //#include <mpxmediageneraldefs.h> |
32 //#include <mpxmediageneraldefs.h> |
34 |
33 |
35 const TUint KMaxDownloadErrors = 3; |
34 const TUint KMaxDownloadErrors = 3; |
36 const TInt KMimeBufLength = 100; |
35 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 |
36 |
47 CShowEngine::CShowEngine(CPodcastModel& aPodcastModel) : |
37 CShowEngine::CShowEngine(CPodcastModel& aPodcastModel) : |
48 iPodcastModel(aPodcastModel), |
38 iPodcastModel(aPodcastModel), |
49 iDB(*aPodcastModel.DB()) |
39 iDB(*aPodcastModel.DB()) |
50 { |
40 { |
113 DownloadNextShowL(); |
103 DownloadNextShowL(); |
114 } |
104 } |
115 DP("CShowEngine::ResumeDownloadsL END"); |
105 DP("CShowEngine::ResumeDownloadsL END"); |
116 } |
106 } |
117 |
107 |
118 EXPORT_C void CShowEngine::RemoveAllDownloads() |
108 EXPORT_C void CShowEngine::RemoveAllDownloadsL() |
119 { |
109 { |
120 if (!iPodcastModel.SettingsEngine().DownloadSuspended()) |
110 if (!iPodcastModel.SettingsEngine().DownloadSuspended()) |
121 { |
111 { |
122 SuspendDownloads(); |
112 SuspendDownloads(); |
123 } |
113 } |
124 |
114 |
125 DBRemoveAllDownloads(); |
115 DBRemoveAllDownloadsL(); |
126 NotifyDownloadQueueUpdatedL(); |
116 NotifyDownloadQueueUpdatedL(); |
127 } |
117 } |
128 |
118 |
129 EXPORT_C TBool CShowEngine::RemoveDownloadL(TUint aUid) |
119 EXPORT_C void CShowEngine::RemoveDownloadL(TUint aUid) |
130 { |
120 { |
131 DP("CShowEngine::RemoveDownload\t Trying to remove download"); |
121 DP("CShowEngine::RemoveDownloadL BEGIN"); |
132 |
122 |
133 TBool retVal = EFalse; |
|
134 TBool resumeAfterRemove = EFalse; |
123 TBool resumeAfterRemove = EFalse; |
135 // if trying to remove the present download, we first stop it |
124 // if trying to remove the present download, we first stop it |
136 if (!iPodcastModel.SettingsEngine().DownloadSuspended() && iShowDownloading != NULL |
125 if (!iPodcastModel.SettingsEngine().DownloadSuspended() && iShowDownloading != NULL |
137 && iShowDownloading->Uid() == aUid) |
126 && iShowDownloading->Uid() == aUid) |
138 { |
127 { |
139 DP("CShowEngine::RemoveDownload\t This is the active download, we suspend downloading"); |
128 DP("CShowEngine::RemoveDownload\t This is the active download, we suspend downloading"); |
140 SuspendDownloads(); |
129 SuspendDownloads(); |
|
130 |
|
131 // partial downloads should be removed |
|
132 BaflUtils::DeleteFile(iPodcastModel.FsSession(), iShowDownloading->FileName()); |
|
133 |
141 resumeAfterRemove = ETrue; |
134 resumeAfterRemove = ETrue; |
142 } |
135 } |
143 |
136 |
144 CShowInfo *info = DBGetShowByUidL(aUid); |
137 CShowInfo *info = DBGetShowByUidL(aUid); |
145 if (info != NULL) |
138 if (info != NULL) |
146 { |
139 { |
147 info->SetDownloadState(ENotDownloaded); |
140 info->SetDownloadState(ENotDownloaded); |
148 DBUpdateShow(*info); |
141 DBUpdateShowL(*info); |
149 delete info; |
142 delete info; |
150 } |
143 } |
151 DBRemoveDownload(aUid); |
144 |
152 |
145 DBRemoveDownloadL(aUid); |
153 // partial downloads should be removed |
146 |
154 if (iShowDownloading) |
147 if (resumeAfterRemove) |
155 { |
148 { |
156 BaflUtils::DeleteFile(iPodcastModel.FsSession(), iShowDownloading->FileName()); |
|
157 } |
|
158 |
|
159 NotifyShowDownloadUpdatedL(-1, -1); |
|
160 NotifyDownloadQueueUpdatedL(); |
|
161 |
|
162 if (resumeAfterRemove) { |
|
163 ResumeDownloadsL(); |
149 ResumeDownloadsL(); |
164 } |
150 } |
|
151 else |
|
152 { |
|
153 NotifyShowDownloadUpdatedL(-1, -1); |
|
154 NotifyDownloadQueueUpdatedL(); |
|
155 } |
165 |
156 |
166 DownloadNextShowL(); |
157 DownloadNextShowL(); |
167 retVal = ETrue; |
158 DP("CShowEngine::RemoveDownloadL END"); |
168 |
|
169 return retVal; |
|
170 } |
159 } |
171 |
160 |
172 void CShowEngine::Connected(CHttpClient* /*aClient*/) |
161 void CShowEngine::Connected(CHttpClient* /*aClient*/) |
173 { |
162 { |
174 |
163 |
219 |
203 |
220 // complete file path is base dir + rel path |
204 // complete file path is base dir + rel path |
221 filePath.Append(relPath); |
205 filePath.Append(relPath); |
222 info->SetFileNameL(filePath); |
206 info->SetFileNameL(filePath); |
223 |
207 |
224 return iShowClient->GetL(info->Url(), filePath); |
208 User::LeaveIfError(iShowClient->GetL(info->Url(), filePath)); |
225 } |
209 } |
226 |
210 |
227 EXPORT_C TBool CShowEngine::AddShowL(const CShowInfo& aItem) |
211 EXPORT_C void CShowEngine::AddShowL(const CShowInfo& aItem) |
228 { |
212 { |
229 DP1("CShowEngine::AddShowL, title=%S", &aItem.Title()); |
213 DP1("CShowEngine::AddShowL, title=%S", &aItem.Title()); |
230 CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid()); |
214 CShowInfo *showInfo = DBGetShowByUidL(aItem.Uid()); |
231 |
215 |
232 if (showInfo == NULL) |
216 if (showInfo == NULL) |
233 { |
217 { |
234 DBAddShow(aItem); |
218 DBAddShowL(aItem); |
235 return ETrue; |
|
236 } |
219 } |
237 else |
220 else |
238 { |
221 { |
239 delete showInfo; |
222 delete showInfo; |
240 return EFalse; |
223 User::Leave(KErrAlreadyExists); |
241 } |
224 } |
242 } |
225 } |
243 |
226 |
244 EXPORT_C void CShowEngine::AddObserver(MShowEngineObserver *observer) |
227 EXPORT_C void CShowEngine::AddObserver(MShowEngineObserver *observer) |
245 { |
228 { |
311 { |
294 { |
312 // 400 and 500 series errors are serious errors on which probably another download will fail |
295 // 400 and 500 series errors are serious errors on which probably another download will fail |
313 if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200) |
296 if(aError >= HTTPStatus::EBadRequest && aError <= HTTPStatus::EBadRequest+200) |
314 { |
297 { |
315 iShowDownloading->SetDownloadState(EFailedDownload); |
298 iShowDownloading->SetDownloadState(EFailedDownload); |
316 DBUpdateShow(*iShowDownloading); |
299 DBUpdateShowL(*iShowDownloading); |
317 DBRemoveDownload(iShowDownloading->Uid()); |
300 DBRemoveDownloadL(iShowDownloading->Uid()); |
318 NotifyShowFinishedL(aError); |
301 NotifyShowFinishedL(aError); |
319 |
302 |
320 delete iShowDownloading; |
303 delete iShowDownloading; |
321 iShowDownloading = NULL; |
304 iShowDownloading = NULL; |
|
305 } |
|
306 else if (aError == KErrDiskFull) |
|
307 { |
|
308 // stop downloading immediately if disk is full |
|
309 iShowDownloading->SetDownloadState(EQueued); |
|
310 DBUpdateShowL(*iShowDownloading); |
|
311 iDownloadErrors = KMaxDownloadErrors; |
322 } |
312 } |
323 else // other kind of error, missing network etc, reque this show |
313 else // other kind of error, missing network etc, reque this show |
324 { |
314 { |
325 iShowDownloading->SetDownloadState(EQueued); |
315 iShowDownloading->SetDownloadState(EQueued); |
326 DBUpdateShow(*iShowDownloading); |
316 DBUpdateShowL(*iShowDownloading); |
327 } |
317 } |
328 |
318 |
|
319 NotifyDownloadQueueUpdatedL(); |
329 iDownloadErrors++; |
320 iDownloadErrors++; |
330 if (iDownloadErrors > KMaxDownloadErrors) |
321 if (iDownloadErrors >= KMaxDownloadErrors) |
331 { |
322 { |
332 DP("Too many downloading errors, suspending downloads"); |
323 DP("Too many downloading errors, suspending downloads"); |
333 iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); |
324 iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); |
334 NotifyShowFinishedL(aError); |
325 NotifyShowFinishedL(aError); |
335 } |
326 } |
473 CleanupStack::Pop(showInfo); |
464 CleanupStack::Pop(showInfo); |
474 rc = sqlite3_step(st); |
465 rc = sqlite3_step(st); |
475 } |
466 } |
476 CleanupStack::PopAndDestroy();//st |
467 CleanupStack::PopAndDestroy();//st |
477 } |
468 } |
|
469 else |
|
470 { |
|
471 User::Leave(KErrCorrupt); |
|
472 } |
478 |
473 |
479 // delete downloads that don't have a show |
474 // delete downloads that don't have a show |
480 |
475 |
481 _LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)"); |
476 _LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)"); |
482 iSqlBuffer.Format(KSqlStatement2); |
477 iSqlBuffer.Format(KSqlStatement2); |
483 |
478 |
484 rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, (const void**) NULL); |
479 rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, (const void**) NULL); |
485 |
480 |
486 if (rc == SQLITE_OK) |
481 if (rc == SQLITE_OK) |
487 { |
482 { |
488 rc = sqlite3_step(st); |
483 Cleanup_sqlite3_finalize_PushL(st); |
489 sqlite3_finalize(st); |
484 rc = sqlite3_step(st); |
|
485 CleanupStack::PopAndDestroy(); // st |
|
486 } |
|
487 else |
|
488 { |
|
489 User::Leave(KErrCorrupt); |
490 } |
490 } |
491 } |
491 } |
492 |
492 |
493 CShowInfo* CShowEngine::DBGetNextDownloadL() |
493 CShowInfo* CShowEngine::DBGetNextDownloadL() |
494 { |
494 { |
554 CleanupStack::Pop(showInfo); |
562 CleanupStack::Pop(showInfo); |
555 rc = sqlite3_step(st); |
563 rc = sqlite3_step(st); |
556 } |
564 } |
557 CleanupStack::PopAndDestroy();//st |
565 CleanupStack::PopAndDestroy();//st |
558 } |
566 } |
|
567 else |
|
568 { |
|
569 User::Leave(KErrCorrupt); |
|
570 } |
559 DP("CShowEngine::DBGetShowsByFeed END"); |
571 DP("CShowEngine::DBGetShowsByFeed END"); |
560 } |
572 } |
561 |
573 |
562 TUint CShowEngine::DBGetDownloadsCount() |
574 TUint CShowEngine::DBGetDownloadsCountL() |
563 { |
575 { |
564 DP("CShowEngine::DBGetDownloadsCount"); |
576 DP("CShowEngine::DBGetDownloadsCount"); |
565 |
577 |
566 _LIT(KSqlStatement, "select count(*) from downloads"); |
578 _LIT(KSqlStatement, "select count(*) from downloads"); |
567 iSqlBuffer.Format(KSqlStatement); |
579 iSqlBuffer.Format(KSqlStatement); |
572 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
584 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
573 &st, (const void**) NULL); |
585 &st, (const void**) NULL); |
574 |
586 |
575 if (rc == SQLITE_OK) |
587 if (rc == SQLITE_OK) |
576 { |
588 { |
|
589 Cleanup_sqlite3_finalize_PushL(st); |
577 rc = sqlite3_step(st); |
590 rc = sqlite3_step(st); |
578 |
591 |
579 if (rc == SQLITE_ROW) |
592 if (rc == SQLITE_ROW) |
580 { |
593 { |
581 count = sqlite3_column_int(st, 0); |
594 count = sqlite3_column_int(st, 0); |
582 } |
595 } |
583 sqlite3_finalize(st); |
596 else |
|
597 { |
|
598 User::Leave(KErrUnknown); |
|
599 } |
|
600 |
|
601 CleanupStack::PopAndDestroy(); //st |
|
602 } |
|
603 else |
|
604 { |
|
605 User::Leave(KErrCorrupt); |
584 } |
606 } |
585 return count; |
607 return count; |
586 } |
608 } |
587 |
609 |
588 void CShowEngine::DBGetDownloadedShowsL(RShowInfoArray& aShowArray) |
610 void CShowEngine::DBGetDownloadedShowsL(RShowInfoArray& aShowArray) |
665 if (rc == SQLITE_OK) |
695 if (rc == SQLITE_OK) |
666 { |
696 { |
667 rc = sqlite3_step(st); |
697 rc = sqlite3_step(st); |
668 sqlite3_finalize(st); |
698 sqlite3_finalize(st); |
669 } |
699 } |
|
700 else |
|
701 { |
|
702 User::Leave(KErrCorrupt); |
|
703 } |
670 |
704 |
671 _LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)"); |
705 _LIT(KSqlStatement2, "delete from downloads where uid not in (select downloads.uid from shows, downloads where shows.uid=downloads.uid)"); |
672 iSqlBuffer.Format(KSqlStatement2); |
706 iSqlBuffer.Format(KSqlStatement2); |
673 |
707 |
674 rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, (const void**) NULL); |
708 rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, (const void**) NULL); |
675 |
709 |
676 if (rc == SQLITE_OK) |
710 if (rc == SQLITE_OK) |
677 { |
711 { |
678 rc = sqlite3_step(st); |
712 rc = sqlite3_step(st); |
679 sqlite3_finalize(st); |
713 sqlite3_finalize(st); |
|
714 } |
|
715 else |
|
716 { |
|
717 User::Leave(KErrCorrupt); |
680 } |
718 } |
681 } |
719 } |
682 |
720 |
683 void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo) |
721 void CShowEngine::DBFillShowInfoFromStmtL(sqlite3_stmt *st, CShowInfo* showInfo) |
684 { |
722 { |
732 |
770 |
733 TInt lasterror = sqlite3_column_int(st, 14); |
771 TInt lasterror = sqlite3_column_int(st, 14); |
734 showInfo->SetLastError(lasterror); |
772 showInfo->SetLastError(lasterror); |
735 } |
773 } |
736 |
774 |
737 TBool CShowEngine::DBAddShow(const CShowInfo& aItem) |
775 void CShowEngine::DBAddShowL(const CShowInfo& aItem) |
738 { |
776 { |
739 DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url()); |
777 DP2("CShowEngine::DBAddShow, title=%S, URL=%S", &aItem.Title(), &aItem.Url()); |
740 |
778 |
741 HBufC* titleBuf = HBufC::NewLC(KMaxLineLength); |
779 HBufC* titleBuf = HBufC::NewLC(KMaxLineLength); |
742 TPtr titlePtr(titleBuf->Des()); |
780 TPtr titlePtr(titleBuf->Des()); |
762 |
800 |
763 sqlite3_stmt *st; |
801 sqlite3_stmt *st; |
764 |
802 |
765 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
803 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
766 &st, (const void**) NULL); |
804 &st, (const void**) NULL); |
767 if (rc == SQLITE_OK) |
805 |
768 { |
806 if (rc == SQLITE_OK) |
769 rc = sqlite3_step(st); |
807 { |
770 if (rc == SQLITE_DONE) |
808 Cleanup_sqlite3_finalize_PushL(st); |
771 { |
809 rc = sqlite3_step(st); |
772 sqlite3_finalize(st); |
810 if (rc != SQLITE_DONE) |
773 return ETrue; |
811 { |
774 } |
812 User::Leave(KErrAlreadyExists); |
775 else |
813 } |
776 { |
814 CleanupStack::PopAndDestroy(); // st |
777 sqlite3_finalize(st); |
815 } |
778 } |
816 else |
779 } |
817 { |
780 else |
818 User::Leave(KErrCorrupt); |
781 { |
819 } |
782 DP1("SQLite rc=%d", rc); |
820 } |
783 } |
821 |
784 |
822 void CShowEngine::DBAddDownloadL(TUint aUid) |
785 return EFalse; |
|
786 } |
|
787 |
|
788 void CShowEngine::DBAddDownload(TUint aUid) |
|
789 { |
823 { |
790 DP1("CShowEngine::DBAddDownload, aUid=%u", aUid); |
824 DP1("CShowEngine::DBAddDownload, aUid=%u", aUid); |
791 |
825 |
792 _LIT(KSqlStatement, "insert into downloads (uid) values (%u)"); |
826 _LIT(KSqlStatement, "insert into downloads (uid) values (%u)"); |
793 iSqlBuffer.Format(KSqlStatement, aUid); |
827 iSqlBuffer.Format(KSqlStatement, aUid); |
796 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
830 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
797 &st, (const void**) NULL); |
831 &st, (const void**) NULL); |
798 |
832 |
799 if (rc == SQLITE_OK) |
833 if (rc == SQLITE_OK) |
800 { |
834 { |
801 rc = sqlite3_step(st); |
835 Cleanup_sqlite3_finalize_PushL(st); |
802 } |
836 rc = sqlite3_step(st); |
803 |
837 if (rc != SQLITE_DONE) |
804 sqlite3_finalize(st); |
838 { |
805 } |
839 User::Leave(KErrUnknown); |
806 |
840 } |
807 TBool CShowEngine::DBUpdateShow(CShowInfo& aItem) |
841 CleanupStack::PopAndDestroy(); // st |
|
842 } |
|
843 else |
|
844 { |
|
845 User::Leave(KErrCorrupt); |
|
846 } |
|
847 } |
|
848 |
|
849 void CShowEngine::DBUpdateShowL(CShowInfo& aItem) |
808 { |
850 { |
809 DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title()); |
851 DP1("CShowEngine::DBUpdateShow, title='%S'", &aItem.Title()); |
810 |
852 |
811 HBufC* titleBuf = HBufC::NewLC(KMaxLineLength); |
853 HBufC* titleBuf = HBufC::NewLC(KMaxLineLength); |
812 TPtr titlePtr(titleBuf->Des()); |
854 TPtr titlePtr(titleBuf->Des()); |
835 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
877 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
836 &st, (const void**) NULL); |
878 &st, (const void**) NULL); |
837 |
879 |
838 if (rc == SQLITE_OK) |
880 if (rc == SQLITE_OK) |
839 { |
881 { |
840 rc = sqlite3_step(st); |
882 Cleanup_sqlite3_finalize_PushL(st); |
841 |
883 rc = sqlite3_step(st); |
842 if (rc == SQLITE_DONE) |
884 |
843 { |
885 if (rc != SQLITE_DONE) |
844 sqlite3_finalize(st); |
886 { |
845 return ETrue; |
887 User::Leave(KErrUnknown); |
846 } |
888 } |
847 else |
889 CleanupStack::PopAndDestroy(); // st |
848 { |
890 } |
849 sqlite3_finalize(st); |
891 else |
850 } |
892 { |
851 } |
893 User::Leave(KErrCorrupt); |
852 else |
894 } |
853 { |
895 } |
854 DP1("SQLite rc=%d", rc); |
896 |
855 } |
897 void CShowEngine::DBDeleteShowL(TUint aUid) |
856 |
|
857 return EFalse; |
|
858 } |
|
859 |
|
860 TBool CShowEngine::DBDeleteShow(TUint aUid) |
|
861 { |
898 { |
862 DP("CShowEngine::DBDeleteShow"); |
899 DP("CShowEngine::DBDeleteShow"); |
863 |
900 |
864 _LIT(KSqlStatement, "delete from shows where uid=%u"); |
901 _LIT(KSqlStatement, "delete from shows where uid=%u"); |
865 iSqlBuffer.Format(KSqlStatement, aUid); |
902 iSqlBuffer.Format(KSqlStatement, aUid); |
866 |
903 |
867 sqlite3_stmt *st; |
904 sqlite3_stmt *st; |
868 |
905 |
869 //DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen)); |
906 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
870 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
907 &st, (const void**) NULL); |
871 &st, (const void**) NULL); |
908 |
872 |
909 if (rc == SQLITE_OK) |
873 if (rc == SQLITE_OK) |
910 { |
874 { |
911 Cleanup_sqlite3_finalize_PushL(st); |
875 rc = sqlite3_step(st); |
912 rc = sqlite3_step(st); |
876 |
913 |
877 if (rc == SQLITE_DONE) |
914 if (rc != SQLITE_DONE) |
878 { |
915 { |
879 sqlite3_finalize(st); |
916 User::Leave(KErrUnknown); |
880 return ETrue; |
917 } |
881 } |
918 CleanupStack::PopAndDestroy(); // st |
882 else |
919 } |
883 { |
920 else |
884 sqlite3_finalize(st); |
921 { |
885 } |
922 User::Leave(KErrCorrupt); |
886 } |
923 } |
887 else |
924 } |
888 { |
925 |
889 DP1("SQLite rc=%d", rc); |
926 void CShowEngine::DBDeleteAllShowsByFeedL(TUint aFeedUid) |
890 } |
|
891 |
|
892 return EFalse; |
|
893 } |
|
894 |
|
895 TBool CShowEngine::DBDeleteAllShowsByFeed(TUint aFeedUid) |
|
896 { |
927 { |
897 DP("CShowEngine::DBDeleteAllShowsByFeed"); |
928 DP("CShowEngine::DBDeleteAllShowsByFeed"); |
898 |
929 |
899 _LIT(KSqlStatement, "delete from shows where feeduid=%u"); |
930 _LIT(KSqlStatement, "delete from shows where feeduid=%u"); |
900 iSqlBuffer.Format(KSqlStatement, aFeedUid); |
931 iSqlBuffer.Format(KSqlStatement, aFeedUid); |
901 |
932 |
902 sqlite3_stmt *st; |
933 sqlite3_stmt *st; |
903 |
934 |
904 //DP1("SQL: %S", &iSqlBuffer.Left(KSqlDPLen)); |
935 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
905 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
936 &st, (const void**) NULL); |
906 &st, (const void**) NULL); |
937 |
907 |
938 if (rc == SQLITE_OK) |
908 if (rc == SQLITE_OK) |
939 { |
909 { |
940 Cleanup_sqlite3_finalize_PushL(st); |
910 rc = sqlite3_step(st); |
941 rc = sqlite3_step(st); |
911 |
942 |
912 if (rc == SQLITE_DONE) |
943 if (rc != SQLITE_DONE) |
913 { |
944 { |
914 sqlite3_finalize(st); |
945 User::Leave(KErrUnknown); |
915 return ETrue; |
946 } |
916 } |
947 CleanupStack::PopAndDestroy(); // st |
917 else |
948 } |
918 { |
949 else |
919 sqlite3_finalize(st); |
950 { |
920 } |
951 User::Leave(KErrCorrupt); |
921 } |
952 } |
922 else |
953 } |
923 { |
954 |
924 DP1("SQLite rc=%d", rc); |
955 void CShowEngine::DBRemoveAllDownloadsL() |
925 } |
|
926 |
|
927 return EFalse; |
|
928 } |
|
929 |
|
930 void CShowEngine::DBRemoveAllDownloads() |
|
931 { |
956 { |
932 DP("CShowEngine::DBRemoveAllDownloads"); |
957 DP("CShowEngine::DBRemoveAllDownloads"); |
933 |
958 |
934 _LIT(KSqlStatement, "delete from downloads"); |
959 _LIT(KSqlStatement, "delete from downloads"); |
935 iSqlBuffer.Format(KSqlStatement); |
960 iSqlBuffer.Format(KSqlStatement); |
942 if (rc == SQLITE_OK) |
967 if (rc == SQLITE_OK) |
943 { |
968 { |
944 rc = sqlite3_step(st); |
969 rc = sqlite3_step(st); |
945 sqlite3_finalize(st); |
970 sqlite3_finalize(st); |
946 } |
971 } |
|
972 else |
|
973 { |
|
974 User::Leave(KErrCorrupt); |
|
975 } |
947 |
976 |
948 _LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1"); |
977 _LIT(KSqlStatement2, "update shows set downloadstate=0 where downloadstate=1"); |
949 iSqlBuffer.Format(KSqlStatement2); |
978 iSqlBuffer.Format(KSqlStatement2); |
950 |
979 |
951 rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, |
980 rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, &st, |
952 (const void**) NULL); |
981 (const void**) NULL); |
953 |
982 |
954 if (rc == SQLITE_OK) |
983 if (rc == SQLITE_OK) |
955 { |
984 { |
956 rc = sqlite3_step(st); |
985 Cleanup_sqlite3_finalize_PushL(st); |
957 sqlite3_finalize(st); |
986 rc = sqlite3_step(st); |
958 } |
987 if (rc != SQLITE_DONE) |
959 |
988 { |
960 } |
989 User::Leave(KErrUnknown); |
961 |
990 } |
962 void CShowEngine::DBRemoveDownload(TUint aUid) |
991 CleanupStack::PopAndDestroy(); // st |
|
992 } |
|
993 else |
|
994 { |
|
995 User::Leave(KErrCorrupt); |
|
996 } |
|
997 |
|
998 } |
|
999 |
|
1000 void CShowEngine::DBRemoveDownloadL(TUint aUid) |
963 { |
1001 { |
964 DP("CShowEngine::DBRemoveDownload"); |
1002 DP("CShowEngine::DBRemoveDownload"); |
965 |
1003 |
966 _LIT(KSqlStatement, "delete from downloads where uid=%u"); |
1004 _LIT(KSqlStatement, "delete from downloads where uid=%u"); |
967 iSqlBuffer.Format(KSqlStatement, aUid); |
1005 iSqlBuffer.Format(KSqlStatement, aUid); |
971 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
1009 int rc = sqlite3_prepare16_v2(&iDB, (const void*) iSqlBuffer.PtrZ(), -1, |
972 &st, (const void**) NULL); |
1010 &st, (const void**) NULL); |
973 |
1011 |
974 if (rc == SQLITE_OK) |
1012 if (rc == SQLITE_OK) |
975 { |
1013 { |
976 rc = sqlite3_step(st); |
1014 Cleanup_sqlite3_finalize_PushL(st); |
977 sqlite3_finalize(st); |
1015 rc = sqlite3_step(st); |
|
1016 if (rc != SQLITE_DONE) |
|
1017 { |
|
1018 User::Leave(KErrUnknown); |
|
1019 } |
|
1020 CleanupStack::PopAndDestroy(); // st |
|
1021 } |
|
1022 else |
|
1023 { |
|
1024 User::Leave(KErrCorrupt); |
978 } |
1025 } |
979 } |
1026 } |
980 |
1027 |
981 EXPORT_C CShowInfo* CShowEngine::GetNextShowByTrackL(CShowInfo* aShowInfo) |
1028 EXPORT_C CShowInfo* CShowEngine::GetNextShowByTrackL(CShowInfo* aShowInfo) |
982 { |
1029 { |
1056 // DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle); |
1103 // DP2("Sorting %S greater than %S", &a.iTitle, &b.iTitle); |
1057 return 1; |
1104 return 1; |
1058 } |
1105 } |
1059 } |
1106 } |
1060 |
1107 |
1061 EXPORT_C void CShowEngine::DeletePlayedShows(RShowInfoArray &aShowInfoArray) |
1108 EXPORT_C void CShowEngine::DeletePlayedShowsL(RShowInfoArray &aShowInfoArray) |
1062 { |
1109 { |
1063 for (TInt i = 0; i < aShowInfoArray.Count(); i++) |
1110 for (TInt i = 0; i < aShowInfoArray.Count(); i++) |
1064 { |
1111 { |
1065 if (aShowInfoArray[i]->PlayState() == EPlayed |
1112 if (aShowInfoArray[i]->PlayState() == EPlayed |
1066 && aShowInfoArray[i]->FileName().Length() > 0) |
1113 && aShowInfoArray[i]->FileName().Length() > 0) |
1067 { |
1114 { |
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()); |
1115 BaflUtils::DeleteFile(iPodcastModel.FsSession(), aShowInfoArray[i]->FileName()); |
1074 aShowInfoArray[i]->SetDownloadState(ENotDownloaded); |
1116 aShowInfoArray[i]->SetDownloadState(ENotDownloaded); |
1075 DBUpdateShow(*aShowInfoArray[i]); |
1117 DBUpdateShowL(*aShowInfoArray[i]); |
1076 } |
1118 } |
1077 } |
1119 } |
1078 } |
1120 } |
1079 |
1121 |
1080 EXPORT_C void CShowEngine::DeleteAllShowsByFeedL(TUint aFeedUid, TBool aDeleteFiles) |
1122 EXPORT_C void CShowEngine::DeleteAllShowsByFeedL(TUint aFeedUid, TBool aDeleteFiles) |
1084 |
1126 |
1085 const TInt count = array.Count(); |
1127 const TInt count = array.Count(); |
1086 |
1128 |
1087 for (TInt i = count - 1; i >= 0; i--) |
1129 for (TInt i = count - 1; i >= 0; i--) |
1088 { |
1130 { |
|
1131 if (iShowDownloading && iShowDownloading->Uid() == array[i]->Uid()) |
|
1132 { |
|
1133 // trying to delete the active download |
|
1134 RemoveDownloadL(iShowDownloading->Uid()); |
|
1135 } |
|
1136 |
|
1137 // delete downloaded file |
1089 if (array[i]->FileName().Length() > 0) |
1138 if (array[i]->FileName().Length() > 0) |
1090 { |
1139 { |
1091 if (aDeleteFiles) |
1140 if (aDeleteFiles) |
1092 { |
1141 { |
1093 BaflUtils::DeleteFile(iPodcastModel.FsSession(), array[i]->FileName()); |
1142 BaflUtils::DeleteFile(iPodcastModel.FsSession(), array[i]->FileName()); |
1094 } |
1143 } |
1095 } |
1144 } |
1096 } |
1145 } |
1097 array.ResetAndDestroy(); |
1146 array.ResetAndDestroy(); |
1098 DBDeleteAllShowsByFeed(aFeedUid); |
1147 |
1099 } |
1148 // delete all shows from DB |
1100 |
1149 DBDeleteAllShowsByFeedL(aFeedUid); |
1101 EXPORT_C void CShowEngine::DeleteOldShowsByFeed(TUint aFeedUid) |
1150 |
1102 { |
1151 // this will clear out deleted shows from the download queue |
1103 DBDeleteOldShowsByFeed(aFeedUid); |
1152 DBGetAllDownloadsL(array); |
|
1153 array.ResetAndDestroy(); |
|
1154 |
|
1155 NotifyDownloadQueueUpdatedL(); |
|
1156 } |
|
1157 |
|
1158 EXPORT_C void CShowEngine::DeleteOldShowsByFeedL(TUint aFeedUid) |
|
1159 { |
|
1160 DBDeleteOldShowsByFeedL(aFeedUid); |
1104 } |
1161 } |
1105 |
1162 |
1106 EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile) |
1163 EXPORT_C void CShowEngine::DeleteShowL(TUint aShowUid, TBool aRemoveFile) |
1107 { |
1164 { |
1108 |
1165 |
1151 DBGetAllDownloadsL(aArray); |
1208 DBGetAllDownloadsL(aArray); |
1152 } |
1209 } |
1153 |
1210 |
1154 EXPORT_C TInt CShowEngine::GetNumDownloadingShows() |
1211 EXPORT_C TInt CShowEngine::GetNumDownloadingShows() |
1155 { |
1212 { |
1156 return (const TInt) DBGetDownloadsCount(); |
1213 TUint count = 0; |
|
1214 TRAP_IGNORE(count = DBGetDownloadsCountL()); |
|
1215 |
|
1216 return (const TInt) count; |
1157 } |
1217 } |
1158 |
1218 |
1159 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo) |
1219 EXPORT_C void CShowEngine::AddDownloadL(CShowInfo& aInfo) |
1160 { |
1220 { |
1161 aInfo.SetDownloadState(EQueued); |
1221 aInfo.SetDownloadState(EQueued); |
1162 DBUpdateShow(aInfo); |
1222 DBUpdateShowL(aInfo); |
1163 DBAddDownload(aInfo.Uid()); |
1223 DBAddDownloadL(aInfo.Uid()); |
1164 DownloadNextShowL(); |
1224 DownloadNextShowL(); |
1165 } |
1225 } |
1166 |
1226 |
1167 void CShowEngine::DownloadNextShowL() |
1227 void CShowEngine::DownloadNextShowL() |
1168 { |
1228 { |
1169 DP("CShowEngine::DownloadNextShowL BEGIN"); |
1229 DP("CShowEngine::DownloadNextShowL BEGIN"); |
1170 // Check if we have anything in the download queue |
1230 // Check if we have anything in the download queue |
1171 const TInt count = DBGetDownloadsCount(); |
1231 const TInt count = DBGetDownloadsCountL(); |
1172 DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count); |
1232 DP("CShowEngine::DownloadNextShow\tTrying to start new download");DP1("CShowEngine::DownloadNextShow\tShows in download queue %d", count); |
1173 |
1233 |
1174 if (count > 0) |
1234 if (count > 0) |
1175 { |
1235 { |
1176 if (iPodcastModel.SettingsEngine().DownloadSuspended()) |
1236 if (iPodcastModel.SettingsEngine().DownloadSuspended()) |
1187 NotifyDownloadQueueUpdatedL(); |
1247 NotifyDownloadQueueUpdatedL(); |
1188 return; |
1248 return; |
1189 } |
1249 } |
1190 else |
1250 else |
1191 { |
1251 { |
1192 |
1252 if (iShowDownloading) { |
|
1253 delete iShowDownloading; |
|
1254 } |
|
1255 |
1193 // Start the download |
1256 // Start the download |
|
1257 iShowDownloading = DBGetNextDownloadL(); |
1194 |
1258 |
1195 CShowInfo *info = DBGetNextDownloadL(); |
1259 while(iShowDownloading != NULL) |
1196 |
|
1197 while(info != NULL) |
|
1198 { |
1260 { |
1199 TBool getOk = EFalse; |
1261 DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(iShowDownloading->Title())); |
1200 DP1("CShowEngine::DownloadNextShow\tDownloading: %S", &(info->Title())); |
1262 iShowDownloading->SetDownloadState(EDownloading); |
1201 info->SetDownloadState(EDownloading); |
1263 iShowDownloading->SetLastError(KErrNone); |
1202 info->SetLastError(KErrNone); |
1264 DBUpdateShowL(*iShowDownloading); |
1203 DBUpdateShow(*info); |
|
1204 iShowDownloading = info; |
|
1205 // Inform the observers |
1265 // Inform the observers |
1206 // important to do this after we change download state |
1266 // important to do this after we change download state |
1207 NotifyDownloadQueueUpdatedL(); |
1267 NotifyDownloadQueueUpdatedL(); |
1208 |
1268 |
1209 TRAPD(error,getOk = GetShowL(info)); |
1269 TRAPD(error,GetShowL(iShowDownloading)); |
1210 if (error != KErrNone || !getOk) |
1270 if (error == KErrNone) |
1211 { |
|
1212 info->SetDownloadState(EFailedDownload); |
|
1213 DBRemoveDownload(info->Uid()); |
|
1214 DBUpdateShow(*info); |
|
1215 info = DBGetNextDownloadL(); |
|
1216 |
|
1217 if(info == NULL) |
|
1218 { |
|
1219 iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); |
|
1220 iShowDownloading = NULL; |
|
1221 } |
|
1222 } |
|
1223 else |
|
1224 { |
1271 { |
1225 break; |
1272 break; |
1226 } |
1273 } |
|
1274 else |
|
1275 { |
|
1276 iShowDownloading->SetDownloadState(EFailedDownload); |
|
1277 DBRemoveDownloadL(iShowDownloading->Uid()); |
|
1278 DBUpdateShowL(*iShowDownloading); |
|
1279 CleanupStack::PopAndDestroy(iShowDownloading); |
|
1280 |
|
1281 iShowDownloading = DBGetNextDownloadL(); |
|
1282 if(iShowDownloading == NULL) |
|
1283 { |
|
1284 iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue); |
|
1285 } |
|
1286 } |
1227 } |
1287 } |
1228 } |
1288 } |
1229 } |
1289 } |
1230 else |
1290 else |
1231 { |
1291 { |
1269 { |
1329 { |
1270 iObservers[i]->ShowListUpdatedL(); |
1330 iObservers[i]->ShowListUpdatedL(); |
1271 } |
1331 } |
1272 } |
1332 } |
1273 |
1333 |
1274 void CShowEngine::ReadMetaData(CShowInfo& aShowInfo) |
1334 void CShowEngine::ReadMetaDataL(CShowInfo& aShowInfo) |
1275 { |
1335 { |
1276 //DP1("Read %S", &(aShowInfo->Title())); |
1336 //DP1("Read %S", &(aShowInfo->Title())); |
1277 DBUpdateShow(aShowInfo); |
1337 DBUpdateShowL(aShowInfo); |
1278 } |
1338 } |
1279 |
1339 |
1280 void CShowEngine::ReadMetaDataCompleteL() |
1340 void CShowEngine::ReadMetaDataCompleteL() |
1281 { |
1341 { |
1282 NotifyShowListUpdatedL(); |
1342 NotifyShowListUpdatedL(); |
1283 MetaDataReader().SetIgnoreTrackNo(EFalse); |
1343 MetaDataReader().SetIgnoreTrackNo(EFalse); |
1284 } |
1344 } |
1285 |
1345 |
1286 EXPORT_C void CShowEngine::UpdateShow(CShowInfo& aInfo) |
1346 EXPORT_C void CShowEngine::UpdateShowL(CShowInfo& aInfo) |
1287 { |
1347 { |
1288 DBUpdateShow(aInfo); |
1348 DBUpdateShowL(aInfo); |
1289 } |
1349 } |
1290 |
1350 |
1291 EXPORT_C CMetaDataReader& CShowEngine::MetaDataReader() |
1351 EXPORT_C CMetaDataReader& CShowEngine::MetaDataReader() |
1292 { |
1352 { |
1293 return *iMetaDataReader; |
1353 return *iMetaDataReader; |
1294 } |
1354 } |
1295 |
1355 |
1296 void CShowEngine::FileError(TUint /*aError*/) |
1356 void CShowEngine::FileError(TUint /*aError*/) |
1297 { |
1357 { |
1298 //TODO: Error dialog |
|
1299 //StopDownloads(); |
|
1300 iDownloadErrors = KMaxDownloadErrors; |
1358 iDownloadErrors = KMaxDownloadErrors; |
1301 } |
1359 } |
|
1360 |
|
1361 EXPORT_C void CShowEngine::CheckForDeletedShows(TUint aFeedUid) |
|
1362 { |
|
1363 RShowInfoArray shows; |
|
1364 |
|
1365 TRAPD(err, DBGetShowsByFeedL(shows, aFeedUid)); |
|
1366 |
|
1367 if (err != KErrNone) |
|
1368 { |
|
1369 // probably a catastrophic error, but it doesn't |
|
1370 // matter for this method |
|
1371 return; |
|
1372 } |
|
1373 |
|
1374 for (int i=0;i<shows.Count();i++) |
|
1375 { |
|
1376 if (shows[i]->DownloadState() == EDownloaded && shows[i]->FileName() != KNullDesC) |
|
1377 { |
|
1378 if(!BaflUtils::FileExists(iPodcastModel.FsSession(),shows[i]->FileName())) |
|
1379 { |
|
1380 // file doesn't exist anymore, assume it was deleted from outside |
|
1381 DP1("Show %S does not exist on disk, flagging as non downloaded", &shows[i]->FileName()); |
|
1382 shows[i]->SetDownloadState(ENotDownloaded); |
|
1383 shows[i]->SetPlayState(EPlayed); |
|
1384 TRAP_IGNORE(DBUpdateShowL(*shows[i])); |
|
1385 } |
|
1386 } |
|
1387 } |
|
1388 } |
|
1389 |