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 <commdb.h>
|
|
20 |
#include "PodcastModel.h"
|
|
21 |
#include "FeedEngine.h"
|
|
22 |
#include "SettingsEngine.h"
|
|
23 |
#include "ShowEngine.h"
|
|
24 |
#include "connectionengine.h"
|
60
|
25 |
#include "podcastutils.h"
|
|
26 |
#include "Podcatcher.pan"
|
2
|
27 |
|
|
28 |
#include <cmdestination.h>
|
|
29 |
#include <cmmanager.h>
|
|
30 |
#include <bautils.h>
|
|
31 |
|
|
32 |
const TInt KDefaultGranu = 5;
|
|
33 |
_LIT(KDBFileName, "podcatcher.sqlite");
|
|
34 |
_LIT(KDBTemplateFileName, "podcatcher.sqlite.template");
|
|
35 |
|
|
36 |
EXPORT_C CPodcastModel* CPodcastModel::NewL()
|
|
37 |
{
|
|
38 |
CPodcastModel* self = new (ELeave) CPodcastModel;
|
|
39 |
CleanupStack::PushL(self);
|
|
40 |
self->ConstructL();
|
|
41 |
CleanupStack::Pop(self);
|
|
42 |
return self;
|
|
43 |
}
|
|
44 |
|
|
45 |
CPodcastModel::~CPodcastModel()
|
|
46 |
{
|
|
47 |
|
|
48 |
delete iFeedEngine;
|
|
49 |
delete iSettingsEngine;
|
|
50 |
delete iShowEngine;
|
|
51 |
|
|
52 |
delete iIapNameArray;
|
|
53 |
iIapIdArray.Close();
|
|
54 |
|
|
55 |
delete iSNAPNameArray;
|
|
56 |
iSNAPIdArray.Close();
|
|
57 |
delete iCommDB;
|
|
58 |
sqlite3_close(iDB);
|
|
59 |
iFsSession.Close();
|
|
60 |
iActiveShowList.ResetAndDestroy();
|
|
61 |
iActiveShowList.Close();
|
|
62 |
delete iConnectionEngine;
|
|
63 |
iCmManager.Close();
|
|
64 |
delete iImageHandler;
|
|
65 |
}
|
|
66 |
|
|
67 |
CPodcastModel::CPodcastModel()
|
|
68 |
{
|
|
69 |
}
|
|
70 |
|
|
71 |
void CPodcastModel::ConstructL()
|
|
72 |
{
|
|
73 |
DP("CPodcastModel::ConstructL BEGIN");
|
|
74 |
User::LeaveIfError(iFsSession.Connect());
|
|
75 |
|
|
76 |
iCommDB = CCommsDatabase::NewL (EDatabaseTypeUnspecified);
|
|
77 |
//iCommDB ->ShowHiddenRecords(); // magic
|
|
78 |
iIapNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
|
|
79 |
iSNAPNameArray = new (ELeave) CDesCArrayFlat(KDefaultGranu);
|
|
80 |
iCmManager.OpenL();
|
60
|
81 |
iImageHandler = CImageHandler::NewL(FsSession(), *this);
|
2
|
82 |
|
|
83 |
UpdateIAPListL();
|
|
84 |
UpdateSNAPListL();
|
|
85 |
|
|
86 |
iSettingsEngine = CSettingsEngine::NewL(*this);
|
|
87 |
iConnectionEngine = CConnectionEngine::NewL(*this);
|
|
88 |
|
60
|
89 |
TRAPD(err, OpenDBL());
|
|
90 |
|
|
91 |
if (err != KErrNone)
|
|
92 |
{
|
|
93 |
ResetDB();
|
|
94 |
|
|
95 |
TRAP(err, OpenDBL());
|
|
96 |
|
|
97 |
if (err != KErrNone)
|
|
98 |
{
|
|
99 |
Panic(EPodcatcherPanicDB);
|
|
100 |
}
|
|
101 |
|
|
102 |
}
|
|
103 |
|
2
|
104 |
iFeedEngine = CFeedEngine::NewL(*this);
|
|
105 |
iShowEngine = CShowEngine::NewL(*this);
|
|
106 |
|
|
107 |
DP("CPodcastModel::ConstructL END");
|
|
108 |
}
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
EXPORT_C void CPodcastModel::UpdateIAPListL()
|
|
113 |
{
|
|
114 |
iIapNameArray->Reset();
|
|
115 |
iIapIdArray.Reset();
|
|
116 |
|
|
117 |
CCommsDbTableView* table = iCommDB->OpenTableLC (TPtrC (IAP));
|
|
118 |
TInt ret = table->GotoFirstRecord ();
|
|
119 |
TPodcastIAPItem IAPItem;
|
|
120 |
TBuf <KCommsDbSvrMaxFieldLength> bufName;
|
|
121 |
while (ret == KErrNone) // There was a first record
|
|
122 |
{
|
|
123 |
table->ReadUintL(TPtrC(COMMDB_ID), IAPItem.iIapId);
|
|
124 |
table->ReadTextL (TPtrC(COMMDB_NAME), bufName);
|
|
125 |
table->ReadTextL (TPtrC(IAP_BEARER_TYPE), IAPItem.iBearerType);
|
|
126 |
table->ReadTextL (TPtrC(IAP_SERVICE_TYPE), IAPItem.iServiceType);
|
|
127 |
|
|
128 |
iIapIdArray.Append(IAPItem);
|
|
129 |
iIapNameArray->AppendL(bufName);
|
|
130 |
ret = table->GotoNextRecord();
|
|
131 |
}
|
|
132 |
CleanupStack::PopAndDestroy(); // Close table
|
|
133 |
}
|
|
134 |
|
|
135 |
EXPORT_C void CPodcastModel::UpdateSNAPListL()
|
|
136 |
{
|
|
137 |
DP("CPodcastModel::UpdateSNAPListL BEGIN");
|
|
138 |
iSNAPNameArray->Reset();
|
|
139 |
iSNAPIdArray.Reset();
|
|
140 |
|
|
141 |
RCmDestination destination;
|
|
142 |
TPodcastIAPItem IAPItem;
|
|
143 |
|
|
144 |
RArray<TUint32> destArray;
|
|
145 |
CleanupClosePushL(destArray);
|
|
146 |
iCmManager.AllDestinationsL(destArray);
|
|
147 |
|
|
148 |
TInt cnt = destArray.Count();
|
|
149 |
DP1("destArray.Count==%d", cnt);
|
|
150 |
for(TInt loop = 0;loop<cnt;loop++)
|
|
151 |
{
|
|
152 |
destination = iCmManager.DestinationL (destArray[loop]);
|
|
153 |
CleanupClosePushL(destination);
|
|
154 |
if(!destination.IsHidden())
|
|
155 |
{
|
|
156 |
IAPItem.iIapId = destArray[loop];
|
|
157 |
HBufC* name = destination.NameLC();
|
|
158 |
DP1(" destination.NameLC==%S", name);
|
|
159 |
iSNAPNameArray->AppendL(*name);
|
|
160 |
CleanupStack::PopAndDestroy(name);
|
|
161 |
iSNAPIdArray.Append(IAPItem);
|
|
162 |
}
|
|
163 |
CleanupStack::PopAndDestroy();//close destination
|
|
164 |
}
|
|
165 |
CleanupStack::PopAndDestroy();// close destArray
|
|
166 |
|
|
167 |
DP("CPodcastModel::UpdateSNAPListL END");
|
|
168 |
}
|
|
169 |
|
|
170 |
EXPORT_C CDesCArrayFlat* CPodcastModel::IAPNames()
|
|
171 |
{
|
|
172 |
return iIapNameArray;
|
|
173 |
}
|
|
174 |
|
|
175 |
EXPORT_C RArray<TPodcastIAPItem>& CPodcastModel::IAPIds()
|
|
176 |
{
|
|
177 |
return iIapIdArray;
|
|
178 |
}
|
|
179 |
|
|
180 |
EXPORT_C CDesCArrayFlat* CPodcastModel::SNAPNames()
|
|
181 |
{
|
|
182 |
return iSNAPNameArray;
|
|
183 |
}
|
|
184 |
|
|
185 |
EXPORT_C RArray<TPodcastIAPItem>& CPodcastModel::SNAPIds()
|
|
186 |
{
|
|
187 |
return iSNAPIdArray;
|
|
188 |
}
|
|
189 |
|
|
190 |
|
|
191 |
RFs& CPodcastModel::FsSession()
|
|
192 |
{
|
|
193 |
return iFsSession;
|
|
194 |
}
|
|
195 |
|
|
196 |
EXPORT_C void CPodcastModel::SetPlayingPodcast(CShowInfo* aPodcast)
|
|
197 |
{
|
|
198 |
iPlayingPodcast = aPodcast;
|
|
199 |
}
|
|
200 |
|
|
201 |
EXPORT_C CShowInfo* CPodcastModel::PlayingPodcast()
|
|
202 |
{
|
|
203 |
return iPlayingPodcast;
|
|
204 |
}
|
|
205 |
|
|
206 |
EXPORT_C CFeedEngine& CPodcastModel::FeedEngine()
|
|
207 |
{
|
|
208 |
return *iFeedEngine;
|
|
209 |
}
|
|
210 |
|
|
211 |
EXPORT_C CShowEngine& CPodcastModel::ShowEngine()
|
|
212 |
{
|
|
213 |
return *iShowEngine;
|
|
214 |
}
|
|
215 |
|
|
216 |
EXPORT_C CSettingsEngine& CPodcastModel::SettingsEngine()
|
|
217 |
{
|
|
218 |
return *iSettingsEngine;
|
|
219 |
}
|
|
220 |
|
|
221 |
EXPORT_C CConnectionEngine& CPodcastModel::ConnectionEngine()
|
|
222 |
{
|
|
223 |
return *iConnectionEngine;
|
|
224 |
}
|
|
225 |
|
|
226 |
EXPORT_C void CPodcastModel::PlayPausePodcastL(CShowInfo* aPodcast, TBool aPlayOnInit)
|
|
227 |
{
|
60
|
228 |
// TODO: interact with MPX
|
2
|
229 |
}
|
|
230 |
|
|
231 |
EXPORT_C CFeedInfo* CPodcastModel::ActiveFeedInfo()
|
|
232 |
{
|
|
233 |
return iActiveFeed;
|
|
234 |
}
|
|
235 |
|
|
236 |
EXPORT_C void CPodcastModel::SetActiveFeedInfo(CFeedInfo* aFeedInfo)
|
|
237 |
{
|
|
238 |
iActiveFeed = aFeedInfo;
|
|
239 |
}
|
|
240 |
|
|
241 |
EXPORT_C RShowInfoArray& CPodcastModel::ActiveShowList()
|
|
242 |
{
|
|
243 |
return iActiveShowList;
|
|
244 |
}
|
|
245 |
|
|
246 |
void CPodcastModel::SetActiveShowList(RShowInfoArray& aShowArray)
|
|
247 |
{
|
|
248 |
iActiveShowList.ResetAndDestroy();
|
|
249 |
TInt cnt = aShowArray.Count();
|
|
250 |
|
|
251 |
for(TInt loop = 0;loop < cnt; loop++)
|
|
252 |
{
|
|
253 |
iActiveShowList.Append(aShowArray[loop]);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
60
|
257 |
EXPORT_C void CPodcastModel::DropDB()
|
|
258 |
{
|
|
259 |
if (iDB != NULL)
|
|
260 |
{
|
|
261 |
sqlite3_close(iDB);
|
|
262 |
iDB = NULL;
|
|
263 |
}
|
|
264 |
|
|
265 |
TFileName dbFileName;
|
|
266 |
dbFileName.Copy(iSettingsEngine->PrivatePath());
|
|
267 |
dbFileName.Append(KDBFileName);
|
|
268 |
|
|
269 |
// remove the old DB file
|
|
270 |
if (BaflUtils::FileExists(iFsSession, dbFileName))
|
|
271 |
{
|
|
272 |
BaflUtils::DeleteFile(iFsSession, dbFileName);
|
|
273 |
}
|
|
274 |
}
|
|
275 |
|
|
276 |
void CPodcastModel::ResetDB()
|
|
277 |
{
|
|
278 |
DP("CPodcastModel::ResetDB BEGIN");
|
|
279 |
|
|
280 |
DropDB();
|
|
281 |
|
|
282 |
TFileName dbFileName;
|
|
283 |
dbFileName.Copy(iSettingsEngine->PrivatePath());
|
|
284 |
dbFileName.Append(KDBFileName);
|
|
285 |
|
|
286 |
// remove the old DB file
|
|
287 |
if (BaflUtils::FileExists(iFsSession, dbFileName))
|
|
288 |
{
|
|
289 |
BaflUtils::DeleteFile(iFsSession, dbFileName);
|
|
290 |
}
|
|
291 |
|
|
292 |
// copy template to new DB
|
|
293 |
TFileName dbTemplate;
|
|
294 |
dbTemplate.Copy(iSettingsEngine->PrivatePath());
|
|
295 |
dbTemplate.Append(KDBTemplateFileName);
|
|
296 |
|
|
297 |
BaflUtils::CopyFile(iFsSession, dbTemplate,dbFileName);
|
|
298 |
iIsFirstStartup = ETrue;
|
|
299 |
DP("CPodcastModel::ResetDB END");
|
|
300 |
}
|
|
301 |
|
|
302 |
|
|
303 |
void CPodcastModel::OpenDBL()
|
|
304 |
{
|
|
305 |
DP("CPodcastModel::OpenDBL BEGIN");
|
|
306 |
|
|
307 |
if (iDB != NULL)
|
|
308 |
{
|
|
309 |
sqlite3_close(iDB);
|
|
310 |
iDB = NULL;
|
|
311 |
}
|
|
312 |
|
|
313 |
TFileName dbFileName;
|
|
314 |
dbFileName.Copy(iSettingsEngine->PrivatePath());
|
|
315 |
dbFileName.Append(KDBFileName);
|
|
316 |
|
|
317 |
if (!BaflUtils::FileExists(iFsSession, dbFileName))
|
|
318 |
{
|
|
319 |
User::Leave(KErrNotFound);
|
|
320 |
}
|
|
321 |
|
|
322 |
if (iDB == NULL) {
|
|
323 |
// open DB
|
|
324 |
TBuf8<KMaxFileName> filename8;
|
|
325 |
filename8.Copy(dbFileName);
|
|
326 |
int rc = sqlite3_open((const char*) filename8.PtrZ(), &iDB);
|
|
327 |
if(rc != SQLITE_OK){
|
|
328 |
User::Leave(KErrCorrupt);
|
|
329 |
}
|
|
330 |
|
|
331 |
// do a test query
|
|
332 |
sqlite3_stmt *st;
|
|
333 |
rc = sqlite3_prepare_v2(iDB,"select count(*) from feeds" , -1, &st, (const char**) NULL);
|
|
334 |
if( rc==SQLITE_OK )
|
|
335 |
{
|
|
336 |
Cleanup_sqlite3_finalize_PushL(st);
|
|
337 |
rc = sqlite3_step(st);
|
|
338 |
|
|
339 |
if (rc != SQLITE_ROW)
|
|
340 |
{
|
|
341 |
User::Leave(KErrCorrupt);
|
|
342 |
}
|
|
343 |
CleanupStack::PopAndDestroy(); // st
|
|
344 |
}
|
|
345 |
else
|
|
346 |
{
|
|
347 |
User::Leave(KErrCorrupt);
|
|
348 |
}
|
|
349 |
}
|
|
350 |
|
|
351 |
DP("CPodcastModel::OpenDBL END");
|
|
352 |
}
|
|
353 |
|
2
|
354 |
sqlite3* CPodcastModel::DB()
|
|
355 |
{
|
|
356 |
return iDB;
|
|
357 |
}
|
|
358 |
|
|
359 |
void CPodcastModel::SetProxyUsageIfNeededL(RHTTPSession& aSession)
|
|
360 |
{
|
|
361 |
TBool useProxy = EFalse;
|
|
362 |
HBufC* serverName = NULL;
|
|
363 |
TUint32 port = 0;
|
|
364 |
|
|
365 |
TRAPD(err,GetProxyInformationForConnectionL(useProxy, serverName, port));
|
|
366 |
if (err == KErrNone && useProxy)
|
|
367 |
{
|
|
368 |
CleanupStack::PushL(serverName);
|
|
369 |
TBuf8<128> proxyAddr;
|
|
370 |
proxyAddr.Append(*serverName);
|
|
371 |
proxyAddr.Append(':');
|
|
372 |
proxyAddr.AppendNum(port);
|
|
373 |
|
|
374 |
RStringF prxAddr = aSession.StringPool().OpenFStringL(proxyAddr);
|
|
375 |
CleanupClosePushL(prxAddr);
|
|
376 |
THTTPHdrVal prxUsage(aSession.StringPool().StringF(HTTP::EUseProxy,RHTTPSession::GetTable()));
|
|
377 |
|
|
378 |
aSession.ConnectionInfo().SetPropertyL(
|
|
379 |
aSession.StringPool().StringF(HTTP::EProxyUsage,RHTTPSession::GetTable()),
|
|
380 |
aSession.StringPool().StringF(HTTP::EUseProxy,RHTTPSession::GetTable()));
|
|
381 |
|
|
382 |
aSession.ConnectionInfo().SetPropertyL(aSession.StringPool().StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()), prxAddr);
|
|
383 |
|
|
384 |
CleanupStack::PopAndDestroy(&prxAddr);
|
|
385 |
CleanupStack::PopAndDestroy(serverName);
|
|
386 |
}
|
|
387 |
}
|
|
388 |
|
|
389 |
|
|
390 |
void CPodcastModel::GetProxyInformationForConnectionL(TBool& aIsUsed, HBufC*& aProxyServerName, TUint32& aPort)
|
|
391 |
{
|
|
392 |
TInt iapId = GetIapId();
|
|
393 |
CCommsDbTableView* table = iCommDB->OpenViewMatchingUintLC(TPtrC(IAP), TPtrC(COMMDB_ID), iapId);
|
|
394 |
|
|
395 |
TUint32 iapService;
|
|
396 |
HBufC* iapServiceType;
|
|
397 |
table->ReadUintL(TPtrC(IAP_SERVICE), iapService);
|
|
398 |
iapServiceType = table->ReadLongTextLC(TPtrC(IAP_SERVICE_TYPE));
|
|
399 |
|
|
400 |
CCommsDbTableView* proxyTableView = iCommDB->OpenViewOnProxyRecordLC(iapService, *iapServiceType);
|
|
401 |
TInt err = proxyTableView->GotoFirstRecord();
|
|
402 |
if( err != KErrNone)
|
|
403 |
{
|
|
404 |
User::Leave(KErrNotFound);
|
|
405 |
}
|
|
406 |
|
|
407 |
proxyTableView->ReadBoolL(TPtrC(PROXY_USE_PROXY_SERVER), aIsUsed);
|
|
408 |
if(aIsUsed)
|
|
409 |
{
|
|
410 |
HBufC* serverName = proxyTableView->ReadLongTextLC(TPtrC(PROXY_SERVER_NAME));
|
|
411 |
proxyTableView->ReadUintL(TPtrC(PROXY_PORT_NUMBER), aPort);
|
|
412 |
aProxyServerName = serverName->AllocL();
|
|
413 |
CleanupStack::PopAndDestroy(serverName);
|
|
414 |
}
|
|
415 |
|
|
416 |
CleanupStack::PopAndDestroy(proxyTableView);
|
|
417 |
CleanupStack::PopAndDestroy(iapServiceType);
|
|
418 |
CleanupStack::PopAndDestroy(table);
|
|
419 |
}
|
|
420 |
|
|
421 |
TInt CPodcastModel::GetIapId()
|
|
422 |
{
|
|
423 |
_LIT(KSetting, "IAP\\Id");
|
|
424 |
TUint32 iapId = 0;
|
|
425 |
iConnectionEngine->Connection().GetIntSetting(KSetting, iapId);
|
|
426 |
return iapId;
|
|
427 |
}
|
|
428 |
|
8
|
429 |
EXPORT_C void CPodcastModel::GetShowsDownloadingL()
|
2
|
430 |
{
|
|
431 |
iActiveShowList.ResetAndDestroy();
|
8
|
432 |
iShowEngine->GetShowsDownloadingL(iActiveShowList);
|
2
|
433 |
}
|
|
434 |
|
8
|
435 |
EXPORT_C void CPodcastModel::GetShowsByFeedL(TUint aFeedUid)
|
2
|
436 |
{
|
|
437 |
iActiveShowList.ResetAndDestroy();
|
60
|
438 |
iShowEngine->CheckForDeletedShows(aFeedUid);
|
8
|
439 |
iShowEngine->GetShowsByFeedL(iActiveShowList, aFeedUid);
|
2
|
440 |
}
|
|
441 |
|
60
|
442 |
EXPORT_C void CPodcastModel::MarkSelectionPlayedL()
|
2
|
443 |
{
|
|
444 |
for (int i=0;i<iActiveShowList.Count();i++) {
|
|
445 |
if(iActiveShowList[i]->PlayState() != EPlayed) {
|
|
446 |
iActiveShowList[i]->SetPlayState(EPlayed);
|
60
|
447 |
iShowEngine->UpdateShowL(*iActiveShowList[i]);
|
2
|
448 |
}
|
|
449 |
}
|
|
450 |
}
|
|
451 |
|
|
452 |
TInt CPodcastModel::FindActiveShowByUid(TUint aUid)
|
|
453 |
{
|
|
454 |
for (int i=0;i<iActiveShowList.Count();i++) {
|
|
455 |
if (iActiveShowList[i]->Uid() == aUid) {
|
|
456 |
return i;
|
|
457 |
}
|
|
458 |
}
|
|
459 |
|
|
460 |
return KErrNotFound;
|
|
461 |
}
|
|
462 |
|
|
463 |
EXPORT_C TBool CPodcastModel::IsFirstStartup()
|
|
464 |
{
|
|
465 |
return iIsFirstStartup;
|
|
466 |
}
|
|
467 |
|
|
468 |
|
60
|
469 |
void CPodcastModel::ImageOperationCompleteL(TInt /*aError*/, TUint /*aHandle*/, CPodcastModel& /*aPodcastModel*/)
|
2
|
470 |
{
|
|
471 |
|
|
472 |
}
|
|
473 |
|
|
474 |
EXPORT_C CImageHandler& CPodcastModel::ImageHandler()
|
|
475 |
{
|
|
476 |
return *iImageHandler;
|
|
477 |
}
|