Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnCallSettingDataService.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file    PhnCallSettingDataService.cpp
19  * @brief       Call Database class
20  */
21
22 #include <FApp.h>
23 #include <FSocial.h>
24 #include <FIo.h>
25 #include "PhnCallSettingDataService.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Io;
30 using namespace Tizen::App;
31 using namespace Tizen::Social;
32
33 CallSettingDataService* CallSettingDataService::__pCallSettingDataService = null;
34 Database* CallSettingDataService::__pDatabase = null;
35
36 CallSettingDataService::CallSettingDataService(void)
37 : __strDbName(App::GetInstance()->GetAppRootPath() + L"data/CallRejectInfoListDataBase")
38 {
39         __pStmt = null;
40         __pEnum = null;
41
42         return;
43 }
44
45 CallSettingDataService::~CallSettingDataService(void)
46 {
47         if (__pDatabase != null)
48         {
49                 delete __pDatabase;
50                 __pDatabase = null;
51         }
52         __pCallSettingDataService = null;
53         return;
54 }
55
56 CallSettingDataService*
57 CallSettingDataService::CreateInstance(void)
58 {
59         if (__pCallSettingDataService == null)
60         {
61                 __pCallSettingDataService = new (std::nothrow) CallSettingDataService();
62         }
63         return __pCallSettingDataService;
64 }
65 result
66 CallSettingDataService::OpenDatabase(void)
67 {
68         result r = E_SUCCESS;
69
70         // create the database if it doesn't exist
71         if (__pDatabase != null)
72         {
73                 return E_OBJ_ALREADY_EXIST;
74         }
75         __pDatabase = new (std::nothrow) Database();
76         r = __pDatabase->Construct(__strDbName, true);
77         TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() database construct failed");
78
79         r = CreateCallRejectTableDatabase();
80         //      TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() call reject Create Table failed");
81         r = CreateSpeedDialTableDatabase();
82         //      TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() speed dial Create Table failed");
83
84         return r;
85
86         CATCH:
87         delete __pDatabase;
88         __pDatabase = null;
89         return r;
90 }
91
92 result
93 CallSettingDataService::CreateCallRejectTableDatabase(void)
94 {
95         String sqlQuery;
96         result r = E_SUCCESS;
97
98         sqlQuery.Append(L"CREATE TABLE IF NOT EXISTS CallRejectInfoTable(id INTEGER PRIMARY KEY AUTOINCREMENT,phonenumber TEXT,rejectcondition INT,activated INT)");
99
100         r = __pDatabase->ExecuteSql(sqlQuery, true);
101         TryCatch(r == E_SUCCESS, , "CallSettingDataService::CreateCallRejectInfoTableDatabase() Create Table failed");
102
103         return r;
104
105         CATCH:
106         return r;
107 }
108
109 result
110 CallSettingDataService::CreateSpeedDialTableDatabase(void)
111 {
112         String sqlQuery;
113         result r = E_SUCCESS;
114
115         sqlQuery.Append(L"CREATE TABLE IF NOT EXISTS SpeedDialTable(id INTEGER PRIMARY KEY AUTOINCREMENT,contactInfo TEXT,keyMappedTo INT)");
116
117         r = __pDatabase->ExecuteSql(sqlQuery, true);
118         TryCatch(r == E_SUCCESS, , "CallSettingDataService::CreateCallRejectInfoTableDatabase() Create Table failed");
119
120         return r;
121
122         CATCH:
123         return r;
124 }
125
126 result
127 CallSettingDataService::CloseDatabase(void)
128 {
129         if (__pEnum != null)
130         {
131                 delete __pEnum;
132                 __pEnum = null;
133         }
134         if (__pStmt != null)
135         {
136                 delete __pStmt;
137                 __pStmt = null;
138         }
139         if (__pDatabase != null)
140         {
141                 delete __pDatabase;
142                 __pDatabase = null;
143         }
144
145         return E_SUCCESS;
146 }
147
148 result
149 CallSettingDataService::AddCallRejectInfoToDatabase(CallRejectInfo* pNewItem)
150 {
151         String statement;
152         __pStmt = null;
153         __pEnum = null;
154         result r = E_FAILURE;
155
156         TryCatch(pNewItem != null, , "CallSettingDataService::AddCallRejectInfoToDatabase() no item to add");
157
158         __pDatabase->BeginTransaction();
159
160         statement.Append(L"INSERT INTO CallRejectInfoTable(phonenumber,rejectcondition,activated) VALUES (?,?,?)");
161         __pStmt = __pDatabase->CreateStatementN(statement);
162         r = GetLastResult();
163         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() CreateStatementN failed");
164         if (__pStmt != null)
165         {
166                 r = __pStmt->BindString(0, pNewItem->phoneNumber);
167                 __pStmt->BindInt(1, pNewItem->rejectCondition);
168                 __pStmt->BindInt(2, pNewItem->isActivated);
169
170                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
171         }
172         r = GetLastResult();
173         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() Add CallRejectInfo to DB failed");
174
175         r = __pDatabase->CommitTransaction();
176         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() CommitTransaction to DB failed");
177
178         if (__pStmt != null)
179         {
180                 delete __pStmt;
181                 __pStmt = null;
182         }
183         if (__pEnum != null)
184         {
185                 delete __pEnum;
186                 __pEnum = null;
187         }
188         return r;
189
190         CATCH:
191         return r;
192 }
193
194 result
195 CallSettingDataService::DeleteCallRejectInfoListFromDatabase(ArrayList* pIndexArray)
196 {
197         result r = E_SUCCESS;
198         IEnumerator* pEnum = null;
199         Integer* index;
200
201         TryCatch(pIndexArray != null, , "CallSettingDataService::DeleteCallRejectInfoListFromDatabase() no item to delete");
202
203         pEnum = pIndexArray->GetEnumeratorN();
204         index = null;
205         if (pEnum != null)
206         {
207                 while (pEnum->MoveNext() == E_SUCCESS)
208                 {
209                         index = static_cast<Integer*>(pEnum->GetCurrent());
210                         r = DeleteCallRejectInfoFromDatabase(index->ToInt());
211                         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() Add CallRejectInfo to DB failed");
212                 }
213                 delete pEnum;
214                 pEnum = null;
215         }
216
217         if (pIndexArray != null)
218         {
219                 pIndexArray->RemoveAll(true);
220                 delete pIndexArray;
221                 pIndexArray = null;
222         }
223         return r;
224
225         CATCH:
226         if (pEnum != null)
227         {
228                 delete pEnum;
229                 pEnum = null;
230         }
231         if (pIndexArray != null)
232         {
233                 pIndexArray->RemoveAll(true);
234                 delete pIndexArray;
235                 pIndexArray = null;
236         }
237         return r;
238 }
239
240 result
241 CallSettingDataService::DeleteCallRejectInfoFromDatabase(int aIndex)
242 {
243         String statement;
244         __pStmt = null;
245         __pEnum = null;
246         result r = E_SUCCESS;
247
248         __pDatabase->BeginTransaction();
249
250         statement.Append(L"DELETE FROM CallRejectInfoTable WHERE id = ?");
251         __pStmt = __pDatabase->CreateStatementN(statement);
252         r = GetLastResult();
253         TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteCallRejectInfoFromDatabase CreateStatementN failed");
254         if (__pStmt != null)
255         {
256                 __pStmt->BindInt(0, aIndex);
257                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
258         }
259         r = GetLastResult();
260         TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteCallRejectInfoFromDatabase Delete CallRejectInfo from DB failed");
261
262         __pDatabase->CommitTransaction();
263
264         if (__pStmt != null)
265         {
266                 delete __pStmt;
267                 __pStmt = null;
268         }
269         if (__pEnum != null)
270         {
271                 delete __pEnum;
272                 __pEnum = null;
273         }
274         return r;
275
276         CATCH:
277         return r;
278 }
279
280 result
281 CallSettingDataService::UpdateCallRejectInfoDatabase(int aIndex, CallRejectInfo* pCallInfoItem)
282 {
283         String statement;
284         __pStmt = null;
285         __pEnum = null;
286         result r = E_SUCCESS;
287
288         TryCatch(pCallInfoItem != null, , "CallSettingDataService::UpdateCallRejectInfoDatabase() no item to update");
289
290         __pDatabase->BeginTransaction();
291
292         statement.Append(L"UPDATE CallRejectInfoTable SET phonenumber=?,rejectcondition=?,activated=? WHERE id=?");
293         __pStmt = __pDatabase->CreateStatementN(statement);
294         r = GetLastResult();
295         TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateCallRejectInfoDatabase CreateStatementN failed");
296         if (__pStmt != null)
297         {
298                 r = __pStmt->BindString(0, pCallInfoItem->phoneNumber);
299                 __pStmt->BindInt(1, pCallInfoItem->rejectCondition);
300                 __pStmt->BindInt(2, pCallInfoItem->isActivated);
301                 __pStmt->BindInt(3, aIndex);
302
303                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
304         }
305         r = GetLastResult();
306         TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateCallRejectInfoDatabase Update CallRejectInfo in DB failed");
307
308         __pDatabase->CommitTransaction();
309
310         if (__pStmt != null)
311         {
312                 delete __pStmt;
313                 __pStmt = null;
314         }
315         if (__pEnum != null)
316         {
317                 delete __pEnum;
318                 __pEnum = null;
319         }
320         return r;
321
322         CATCH:
323         return r;
324 }
325
326 result
327 CallSettingDataService::GetCallRejectInfoFromDatabase(int aIndex, CallRejectInfo& dbItem)
328 {
329         String statement;
330         __pStmt = null;
331         __pEnum = null;
332         result r = E_SUCCESS;
333         __pDatabase->BeginTransaction();
334         statement.Append(L"SELECT id,phonenumber,rejectcondition,activated FROM CallRejectInfoTable WHERE id=?");
335         __pStmt = __pDatabase->CreateStatementN(statement);
336         r = GetLastResult();
337         TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCallRejectInfoFromDatabase CreateStatementN failed");
338
339         __pStmt->BindInt(0, aIndex);
340         __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
341         r = GetLastResult();
342         TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCallRejectInfoFromDatabase Get CallRejectInfo from DB failed");
343
344         if (__pEnum != null)
345         {
346                 int activateflag;
347                 int rejectCondition;
348                 __pEnum->GetIntAt(0, dbItem.rowId);
349                 __pEnum->GetStringAt(1, dbItem.phoneNumber);
350                 __pEnum->GetIntAt(2, rejectCondition);
351                 __pEnum->GetIntAt(3, activateflag);
352                 dbItem.rejectCondition = (CallRejectMatchCondition)rejectCondition;
353                 dbItem.isActivated = (bool)activateflag;
354         }
355         __pDatabase->CommitTransaction();
356
357
358         delete __pStmt;
359         __pStmt = NULL;
360
361         if (__pEnum != null)
362         {
363                 delete __pEnum;
364                 __pEnum = NULL;
365         }
366         return r;
367
368         CATCH:
369         return r;
370 }
371
372 result
373 CallSettingDataService::GetAllCallRejectInfoFromDatabaseN(ArrayListT<CallRejectInfo>& callRejectInfoList)
374 {
375         String sql;
376         __pEnum = null;
377         result r = E_SUCCESS;
378         __pDatabase->BeginTransaction();
379
380         sql.Append(L"SELECT id,phonenumber,rejectcondition,activated from CallRejectInfoTable");
381         __pEnum = __pDatabase->QueryN(sql);
382         r = GetLastResult();
383         TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetAllCallRejectInfoFromDatabase Get CallRejectInfo from DB failed");
384
385         //check if the query returned any result and then iterate through the results
386         if (__pEnum != null)
387         {
388                 while (__pEnum->MoveNext() == E_SUCCESS)
389                 {
390                         CallRejectInfo* pCallRejectInfoItem = new (std::nothrow) CallRejectInfo;
391                         int activateflag;
392                         int rejectCondition;
393                         __pEnum->GetIntAt(0, pCallRejectInfoItem->rowId);
394                         __pEnum->GetStringAt(1, pCallRejectInfoItem->phoneNumber);
395                         __pEnum->GetIntAt(2, rejectCondition);
396                         __pEnum->GetIntAt(3, activateflag);
397                         pCallRejectInfoItem->rejectCondition = (CallRejectMatchCondition)rejectCondition;
398                         pCallRejectInfoItem->isActivated = (bool)activateflag;
399                         callRejectInfoList.Add(*pCallRejectInfoItem);
400                 }
401         }
402         __pDatabase->CommitTransaction();
403         if (__pEnum != null)
404         {
405                 delete __pEnum;
406                 __pEnum = NULL;
407         }
408         return r;
409
410         CATCH:
411         return r;
412 }
413
414 result
415 CallSettingDataService::GetCallRejectCount(int& count)
416 {
417         String statement;
418         result r = E_SUCCESS;
419         __pStmt = null;
420         __pEnum = null;
421
422         __pDatabase->BeginTransaction();
423         statement.Append(L"SELECT COUNT (*) FROM CallRejectInfoTable");
424         __pStmt = __pDatabase->CreateStatementN(statement);
425         r = GetLastResult();
426         TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCount CreateStatementN failed");
427         if (__pStmt != null)
428         {
429                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
430         }
431         r = GetLastResult();
432         TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCount Count from DB failed");
433
434         if (__pEnum != null)
435         {
436                 r = __pEnum->MoveNext();
437                 __pEnum->GetIntAt(0, count);
438         }
439         __pDatabase->CommitTransaction();
440
441         if (__pStmt != null)
442         {
443                 delete __pStmt;
444                 __pStmt = NULL;
445         }
446         if (__pEnum != null)
447         {
448                 delete __pEnum;
449                 __pEnum = NULL;
450         }
451         return r;
452
453         CATCH:
454         return r;
455 }
456
457 bool
458 CallSettingDataService::IsCallToBeRejected(String& phoneNumber)
459 {
460         bool isCallToBeRejected  = false;
461         String statement;
462         __pStmt = null;
463         __pEnum = null;
464         result r = E_SUCCESS;
465         __pDatabase->BeginTransaction();
466
467         //statement.Append(L"SELECT * FROM CallRejectInfoTable WHERE phonenumber LIKE ?");
468         statement.Append(L"SELECT * FROM CallRejectInfoTable WHERE activated=?");
469
470         __pStmt = __pDatabase->CreateStatementN(statement);
471         r = GetLastResult();
472         TryCatch(r == E_SUCCESS, , "CallSettingDataService::SearchFromDataBase CreateStatementN failed");
473
474         //__pStmt->BindString(0, phoneNumber);
475         if (__pStmt != null)
476         {
477                 __pStmt->BindInt(0,(int)true);
478                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
479         }
480         r = GetLastResult();
481         TryCatch(r == E_SUCCESS, , "CallSettingDataService::SearchFromDataBaseN search failed");
482
483         //check if the query returned any result and then iterate through the results
484         if (__pEnum != null)
485         {
486                 while (__pEnum->MoveNext() == E_SUCCESS)
487                 {
488                         CallRejectInfo* pCallRejectInfoItem = new (std::nothrow) CallRejectInfo;
489                         int activateflag;
490                         int rejectCondition;
491                         __pEnum->GetIntAt(0, pCallRejectInfoItem->rowId);
492                         __pEnum->GetStringAt(1, pCallRejectInfoItem->phoneNumber);
493                         __pEnum->GetIntAt(2, rejectCondition);
494                         __pEnum->GetIntAt(3, activateflag);
495                         pCallRejectInfoItem->rejectCondition = (CallRejectMatchCondition)rejectCondition;
496                         pCallRejectInfoItem->isActivated = (bool)activateflag;
497                         if (CheckRejectCondition(phoneNumber, *pCallRejectInfoItem) == true)
498                         {
499                                 delete pCallRejectInfoItem;
500                                 isCallToBeRejected = true;
501                                 break;
502                         }
503                         delete pCallRejectInfoItem;
504                 }
505         }
506         __pDatabase->CommitTransaction();
507
508         if (__pStmt != null)
509         {
510                 delete __pStmt;
511                 __pStmt = NULL;
512         }
513         if (__pEnum != null)
514         {
515                 delete __pEnum;
516                 __pEnum = NULL;
517         }
518         return isCallToBeRejected;
519
520         CATCH:
521         return isCallToBeRejected;
522 }
523
524 bool
525 CallSettingDataService::CheckRejectCondition(String& phoneNumber, CallRejectInfo& callRejectInfo)
526 {
527         bool isCallToBeRejected  = false;
528         switch (callRejectInfo.rejectCondition)
529         {
530         case CALL_REJECT_MATCH_EXACTLY:
531         {
532                 if (callRejectInfo.phoneNumber.CompareTo(phoneNumber) == 0)
533                 {
534                         isCallToBeRejected = true;
535                 }
536         }
537         break;
538         case CALL_REJECT_MATCH_START:
539         {
540                 isCallToBeRejected = phoneNumber.StartsWith(callRejectInfo.phoneNumber, 0);
541         }
542         break;
543         case CALL_REJECT_MATCH_END:
544         {
545                 isCallToBeRejected = phoneNumber.EndsWith(callRejectInfo.phoneNumber);
546         }
547         break;
548         case CALL_REJECT_MATCH_INCLUDE:
549         {
550                 isCallToBeRejected = phoneNumber.Contains(callRejectInfo.phoneNumber);
551         }
552         break;
553         default:
554                 break;
555         }
556
557         return isCallToBeRejected;
558 }
559 result
560 CallSettingDataService::AddSpeedDialInfoToDatabase(SpeedDialInfo* pNewItem)
561 {
562         String statement;
563         __pStmt = null;
564         __pEnum = null;
565         result r = E_FAILURE;
566
567         TryCatch(pNewItem != null, , "CallSettingDataService::AddSpeedDialInfoToDatabase() no item to add");
568
569         __pDatabase->BeginTransaction();
570
571         statement.Append(L"INSERT INTO SpeedDialTable(contactInfo,keyMappedTo) VALUES (?,?)");
572         __pStmt = __pDatabase->CreateStatementN(statement);
573         r = GetLastResult();
574         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddSpeedDialInfoToDatabase() CreateStatementN failed");
575         if (__pStmt != null)
576         {
577                 r = __pStmt->BindString(0, pNewItem->contactId);
578                 __pStmt->BindInt(1, pNewItem->keyMapping);
579
580                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
581         }
582         r = GetLastResult();
583         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddSpeedDialInfoToDatabase() Add SpeedDialInfo to DB failed");
584
585         r = __pDatabase->CommitTransaction();
586         TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddSpeedDialInfoToDatabase() CommitTransaction to DB failed");
587
588         if (__pStmt != null)
589         {
590                 delete __pStmt;
591                 __pStmt = null;
592         }
593         if (__pEnum != null)
594         {
595                 delete __pEnum;
596                 __pEnum = null;
597         }
598         return r;
599
600         CATCH:
601         return r;
602 }
603
604 result
605 CallSettingDataService::DeleteSpeedDialInfoFromDatabase(int aIndex)
606 {
607         String statement;
608         __pStmt = null;
609         __pEnum = null;
610         result r = E_SUCCESS;
611
612         __pDatabase->BeginTransaction();
613
614         statement.Append(L"DELETE FROM SpeedDialTable WHERE id = ?");
615         __pStmt = __pDatabase->CreateStatementN(statement);
616         r = GetLastResult();
617         TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteSpeedDialInfoFromDatabase CreateStatementN failed");
618         if (__pStmt != null)
619         {
620                 __pStmt->BindInt(0, aIndex);
621                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
622         }
623         r = GetLastResult();
624         TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteSpeedDialInfoFromDatabase Delete SpeedDialInfo from DB failed");
625
626         __pDatabase->CommitTransaction();
627
628         if (__pStmt != null)
629         {
630                 delete __pStmt;
631                 __pStmt = null;
632         }
633         if (__pEnum != null)
634         {
635                 delete __pEnum;
636                 __pEnum = null;
637         }
638         return r;
639
640         CATCH:
641         return r;
642 }
643
644 result
645 CallSettingDataService::UpdateSpeedDialInfoDatabase(int aIndex, SpeedDialInfo* pSpeedDialInfoItem)
646 {
647         String statement;
648         __pStmt = null;
649         __pEnum = null;
650         result r = E_SUCCESS;
651
652         TryCatch(pSpeedDialInfoItem != null, , "CallSettingDataService::UpdateSpeedDialInfoDatabase() no item to update");
653
654         __pDatabase->BeginTransaction();
655
656         statement.Append(L"UPDATE SpeedDialTable SET contactInfo=?,keyMappedTo=? WHERE id=?");
657         __pStmt = __pDatabase->CreateStatementN(statement);
658         r = GetLastResult();
659         TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateSpeedDialInfoDatabase CreateStatementN failed");
660         if (__pStmt != null)
661         {
662                 r = __pStmt->BindString(0, pSpeedDialInfoItem->contactId);
663                 __pStmt->BindInt(1, pSpeedDialInfoItem->keyMapping);
664                 __pStmt->BindInt(2, aIndex);
665
666                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
667         }
668         r = GetLastResult();
669         TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateSpeedDialInfoDatabase Update SpeedDialInfo in DB failed");
670
671         __pDatabase->CommitTransaction();
672
673         if (__pStmt != null)
674         {
675                 delete __pStmt;
676                 __pStmt = null;
677         }
678         if (__pEnum != null)
679         {
680                 delete __pEnum;
681                 __pEnum = null;
682         }
683         return r;
684
685         CATCH:
686         return r;
687 }
688
689 IMapT<int,SpeedDialInfo>*
690 CallSettingDataService::GetAllSpeedDialInfoMapFromDatabaseN(void)
691 {
692         String sql;
693         __pEnum = null;
694         result r = E_SUCCESS;
695         HashMapT<int,SpeedDialInfo>* pSpeedDialInfoList = null;
696         r = __pDatabase->BeginTransaction();
697
698         sql.Append(L"SELECT id,contactInfo,keyMappedTo from SpeedDialTable");
699         __pEnum = __pDatabase->QueryN(sql);
700         r = GetLastResult();
701         TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetAllSpeedDialInfoFromDatabase Get SpeedDialInfo from DB failed");
702
703         //check if the query returned any result and then iterate through the results
704         if (__pEnum != null)
705         {
706                 pSpeedDialInfoList =  new (std::nothrow) HashMapT<int,SpeedDialInfo>();
707                 pSpeedDialInfoList->Construct(10);
708                 while (__pEnum->MoveNext() == E_SUCCESS)
709                 {
710                         SpeedDialInfo* pSpeedDialInfoItem = new (std::nothrow) SpeedDialInfo();
711                         int keyMappedTo;;
712                         __pEnum->GetIntAt(0, pSpeedDialInfoItem->rowId);
713                         __pEnum->GetStringAt(1, pSpeedDialInfoItem->contactId);
714                         __pEnum->GetIntAt(2, keyMappedTo);
715                         pSpeedDialInfoItem->keyMapping = keyMappedTo;
716                         pSpeedDialInfoList->Add(keyMappedTo ,*pSpeedDialInfoItem);
717                 }
718         }
719
720         __pDatabase->CommitTransaction();
721
722         if (__pEnum != null)
723         {
724                 delete __pEnum;
725                 __pEnum = NULL;
726         }
727         SyncWithCurrentAddressbookStatus(pSpeedDialInfoList);
728         return pSpeedDialInfoList;
729
730         CATCH:
731         return pSpeedDialInfoList;
732 }
733
734 void
735 CallSettingDataService::SyncWithCurrentAddressbookStatus(HashMapT<int,SpeedDialInfo>* pSpeedDialInfoList)
736 {
737         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
738         Addressbook* pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
739
740         if(pAddressbook == null || pSpeedDialInfoList == null)
741         {
742                 return;
743         }
744
745          IListT<SpeedDialInfo>* pSpeedDialList = pSpeedDialInfoList->GetValuesN();
746          IEnumeratorT<SpeedDialInfo> *pSpeedDialListEnum = pSpeedDialList->GetEnumeratorN();
747         while(pSpeedDialListEnum->MoveNext() == E_SUCCESS)
748         {
749                 SpeedDialInfo speeDialInfo;
750                 RecordId recordId = INVALID_RECORD_ID;
751                 pSpeedDialListEnum->GetCurrent(speeDialInfo);
752                 Integer::Parse(speeDialInfo.contactId, recordId);
753
754                 Contact* pMappedContact = pAddressbook->GetContactN(recordId);
755                 if(pMappedContact == null)
756                 {
757                         DeleteSpeedDialInfoFromDatabase(speeDialInfo.rowId);
758                         pSpeedDialInfoList->Remove(speeDialInfo.keyMapping);
759                 }
760                 else
761                 {
762                         delete pMappedContact;
763                 }
764         }
765         delete pAddressbook;
766         delete pSpeedDialListEnum;
767         delete pSpeedDialList;
768
769 }
770
771 String*
772 CallSettingDataService::GetSpeedDialContactN(int aIndex)
773 {
774         result r = E_SUCCESS;
775         String statement;
776         __pStmt = null;
777         __pEnum = null;
778         String* fetchedInfo = null;
779
780         __pDatabase->BeginTransaction();
781
782         statement.Append(L"SELECT contactInfo from SpeedDialTable WHERE keyMappedTo=?");
783         __pStmt = __pDatabase->CreateStatementN(statement);
784         r = GetLastResult();
785         TryCatch(r == E_SUCCESS, , "GetSpeedDialInfoN - CreateStatementN failed");
786         if (__pStmt != null)
787         {
788                 __pStmt->BindInt(0, aIndex);
789
790                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
791         }
792         r = GetLastResult();
793         TryCatch(r == E_SUCCESS, , "GetSpeedDialInfoN - ExecuteStatementN failed");
794
795         //check if the query returned any result and then iterate through the results
796         if (__pEnum != null && __pEnum->MoveNext() == E_SUCCESS)
797         {
798                 String contact;
799                 __pEnum->GetStringAt(0, contact);
800                 fetchedInfo = new (std::nothrow) String(contact);
801         }
802
803         __pDatabase->CommitTransaction();
804
805         if (__pStmt != null)
806         {
807                 delete __pStmt;
808                 __pStmt = null;
809         }
810         if (__pEnum != null)
811         {
812                 delete __pEnum;
813                 __pEnum = null;
814         }
815         return fetchedInfo;
816
817         CATCH:
818         return fetchedInfo;
819 }