2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file PhnCallSettingDataService.cpp
19 * @brief Call Database class
25 #include "PhnCallSettingDataService.h"
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;
33 CallSettingDataService* CallSettingDataService::__pCallSettingDataService = null;
34 Database* CallSettingDataService::__pDatabase = null;
36 CallSettingDataService::CallSettingDataService(void)
37 : __strDbName(App::GetInstance()->GetAppRootPath() + L"data/CallRejectInfoListDataBase")
45 CallSettingDataService::~CallSettingDataService(void)
47 if (__pDatabase != null)
52 __pCallSettingDataService = null;
56 CallSettingDataService*
57 CallSettingDataService::CreateInstance(void)
59 if (__pCallSettingDataService == null)
61 __pCallSettingDataService = new (std::nothrow) CallSettingDataService();
63 return __pCallSettingDataService;
66 CallSettingDataService::OpenDatabase(void)
70 // create the database if it doesn't exist
71 if (__pDatabase != null)
73 return E_OBJ_ALREADY_EXIST;
75 __pDatabase = new (std::nothrow) Database();
76 r = __pDatabase->Construct(__strDbName, true);
77 TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() database construct failed");
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");
93 CallSettingDataService::CreateCallRejectTableDatabase(void)
98 sqlQuery.Append(L"CREATE TABLE IF NOT EXISTS CallRejectInfoTable(id INTEGER PRIMARY KEY AUTOINCREMENT,phonenumber TEXT,rejectcondition INT,activated INT)");
100 r = __pDatabase->ExecuteSql(sqlQuery, true);
101 TryCatch(r == E_SUCCESS, , "CallSettingDataService::CreateCallRejectInfoTableDatabase() Create Table failed");
110 CallSettingDataService::CreateSpeedDialTableDatabase(void)
113 result r = E_SUCCESS;
115 sqlQuery.Append(L"CREATE TABLE IF NOT EXISTS SpeedDialTable(id INTEGER PRIMARY KEY AUTOINCREMENT,contactInfo TEXT,keyMappedTo INT)");
117 r = __pDatabase->ExecuteSql(sqlQuery, true);
118 TryCatch(r == E_SUCCESS, , "CallSettingDataService::CreateCallRejectInfoTableDatabase() Create Table failed");
127 CallSettingDataService::CloseDatabase(void)
139 if (__pDatabase != null)
149 CallSettingDataService::AddCallRejectInfoToDatabase(CallRejectInfo* pNewItem)
154 result r = E_FAILURE;
156 TryCatch(pNewItem != null, , "CallSettingDataService::AddCallRejectInfoToDatabase() no item to add");
158 __pDatabase->BeginTransaction();
160 statement.Append(L"INSERT INTO CallRejectInfoTable(phonenumber,rejectcondition,activated) VALUES (?,?,?)");
161 __pStmt = __pDatabase->CreateStatementN(statement);
163 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() CreateStatementN failed");
166 r = __pStmt->BindString(0, pNewItem->phoneNumber);
167 __pStmt->BindInt(1, pNewItem->rejectCondition);
168 __pStmt->BindInt(2, pNewItem->isActivated);
170 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
173 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() Add CallRejectInfo to DB failed");
175 r = __pDatabase->CommitTransaction();
176 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() CommitTransaction to DB failed");
195 CallSettingDataService::DeleteCallRejectInfoListFromDatabase(ArrayList* pIndexArray)
197 result r = E_SUCCESS;
198 IEnumerator* pEnum = null;
201 TryCatch(pIndexArray != null, , "CallSettingDataService::DeleteCallRejectInfoListFromDatabase() no item to delete");
203 pEnum = pIndexArray->GetEnumeratorN();
207 while (pEnum->MoveNext() == E_SUCCESS)
209 index = static_cast<Integer*>(pEnum->GetCurrent());
210 r = DeleteCallRejectInfoFromDatabase(index->ToInt());
211 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddCallRejectInfoToDatabase() Add CallRejectInfo to DB failed");
217 if (pIndexArray != null)
219 pIndexArray->RemoveAll(true);
231 if (pIndexArray != null)
233 pIndexArray->RemoveAll(true);
241 CallSettingDataService::DeleteCallRejectInfoFromDatabase(int aIndex)
246 result r = E_SUCCESS;
248 __pDatabase->BeginTransaction();
250 statement.Append(L"DELETE FROM CallRejectInfoTable WHERE id = ?");
251 __pStmt = __pDatabase->CreateStatementN(statement);
253 TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteCallRejectInfoFromDatabase CreateStatementN failed");
256 __pStmt->BindInt(0, aIndex);
257 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
260 TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteCallRejectInfoFromDatabase Delete CallRejectInfo from DB failed");
262 __pDatabase->CommitTransaction();
281 CallSettingDataService::UpdateCallRejectInfoDatabase(int aIndex, CallRejectInfo* pCallInfoItem)
286 result r = E_SUCCESS;
288 TryCatch(pCallInfoItem != null, , "CallSettingDataService::UpdateCallRejectInfoDatabase() no item to update");
290 __pDatabase->BeginTransaction();
292 statement.Append(L"UPDATE CallRejectInfoTable SET phonenumber=?,rejectcondition=?,activated=? WHERE id=?");
293 __pStmt = __pDatabase->CreateStatementN(statement);
295 TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateCallRejectInfoDatabase CreateStatementN failed");
298 r = __pStmt->BindString(0, pCallInfoItem->phoneNumber);
299 __pStmt->BindInt(1, pCallInfoItem->rejectCondition);
300 __pStmt->BindInt(2, pCallInfoItem->isActivated);
301 __pStmt->BindInt(3, aIndex);
303 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
306 TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateCallRejectInfoDatabase Update CallRejectInfo in DB failed");
308 __pDatabase->CommitTransaction();
327 CallSettingDataService::GetCallRejectInfoFromDatabase(int aIndex, CallRejectInfo& dbItem)
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);
337 TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCallRejectInfoFromDatabase CreateStatementN failed");
339 __pStmt->BindInt(0, aIndex);
340 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
342 TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCallRejectInfoFromDatabase Get CallRejectInfo from DB failed");
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;
355 __pDatabase->CommitTransaction();
373 CallSettingDataService::GetAllCallRejectInfoFromDatabaseN(ArrayListT<CallRejectInfo>& callRejectInfoList)
377 result r = E_SUCCESS;
378 __pDatabase->BeginTransaction();
380 sql.Append(L"SELECT id,phonenumber,rejectcondition,activated from CallRejectInfoTable");
381 __pEnum = __pDatabase->QueryN(sql);
383 TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetAllCallRejectInfoFromDatabase Get CallRejectInfo from DB failed");
385 //check if the query returned any result and then iterate through the results
388 while (__pEnum->MoveNext() == E_SUCCESS)
390 CallRejectInfo* pCallRejectInfoItem = new (std::nothrow) CallRejectInfo;
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);
402 __pDatabase->CommitTransaction();
415 CallSettingDataService::GetCallRejectCount(int& count)
418 result r = E_SUCCESS;
422 __pDatabase->BeginTransaction();
423 statement.Append(L"SELECT COUNT (*) FROM CallRejectInfoTable");
424 __pStmt = __pDatabase->CreateStatementN(statement);
426 TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCount CreateStatementN failed");
429 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
432 TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetCount Count from DB failed");
436 r = __pEnum->MoveNext();
437 __pEnum->GetIntAt(0, count);
439 __pDatabase->CommitTransaction();
458 CallSettingDataService::IsCallToBeRejected(String& phoneNumber)
460 bool isCallToBeRejected = false;
464 result r = E_SUCCESS;
465 __pDatabase->BeginTransaction();
467 //statement.Append(L"SELECT * FROM CallRejectInfoTable WHERE phonenumber LIKE ?");
468 statement.Append(L"SELECT * FROM CallRejectInfoTable WHERE activated=?");
470 __pStmt = __pDatabase->CreateStatementN(statement);
472 TryCatch(r == E_SUCCESS, , "CallSettingDataService::SearchFromDataBase CreateStatementN failed");
474 //__pStmt->BindString(0, phoneNumber);
477 __pStmt->BindInt(0,(int)true);
478 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
481 TryCatch(r == E_SUCCESS, , "CallSettingDataService::SearchFromDataBaseN search failed");
483 //check if the query returned any result and then iterate through the results
486 while (__pEnum->MoveNext() == E_SUCCESS)
488 CallRejectInfo* pCallRejectInfoItem = new (std::nothrow) CallRejectInfo;
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)
499 delete pCallRejectInfoItem;
500 isCallToBeRejected = true;
503 delete pCallRejectInfoItem;
506 __pDatabase->CommitTransaction();
518 return isCallToBeRejected;
521 return isCallToBeRejected;
525 CallSettingDataService::CheckRejectCondition(String& phoneNumber, CallRejectInfo& callRejectInfo)
527 bool isCallToBeRejected = false;
528 switch (callRejectInfo.rejectCondition)
530 case CALL_REJECT_MATCH_EXACTLY:
532 if (callRejectInfo.phoneNumber.CompareTo(phoneNumber) == 0)
534 isCallToBeRejected = true;
538 case CALL_REJECT_MATCH_START:
540 isCallToBeRejected = phoneNumber.StartsWith(callRejectInfo.phoneNumber, 0);
543 case CALL_REJECT_MATCH_END:
545 isCallToBeRejected = phoneNumber.EndsWith(callRejectInfo.phoneNumber);
548 case CALL_REJECT_MATCH_INCLUDE:
550 isCallToBeRejected = phoneNumber.Contains(callRejectInfo.phoneNumber);
557 return isCallToBeRejected;
560 CallSettingDataService::AddSpeedDialInfoToDatabase(SpeedDialInfo* pNewItem)
565 result r = E_FAILURE;
567 TryCatch(pNewItem != null, , "CallSettingDataService::AddSpeedDialInfoToDatabase() no item to add");
569 __pDatabase->BeginTransaction();
571 statement.Append(L"INSERT INTO SpeedDialTable(contactInfo,keyMappedTo) VALUES (?,?)");
572 __pStmt = __pDatabase->CreateStatementN(statement);
574 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddSpeedDialInfoToDatabase() CreateStatementN failed");
577 r = __pStmt->BindString(0, pNewItem->contactId);
578 __pStmt->BindInt(1, pNewItem->keyMapping);
580 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
583 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddSpeedDialInfoToDatabase() Add SpeedDialInfo to DB failed");
585 r = __pDatabase->CommitTransaction();
586 TryCatch(r == E_SUCCESS, , "CallSettingDataService::AddSpeedDialInfoToDatabase() CommitTransaction to DB failed");
605 CallSettingDataService::DeleteSpeedDialInfoFromDatabase(int aIndex)
610 result r = E_SUCCESS;
612 __pDatabase->BeginTransaction();
614 statement.Append(L"DELETE FROM SpeedDialTable WHERE id = ?");
615 __pStmt = __pDatabase->CreateStatementN(statement);
617 TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteSpeedDialInfoFromDatabase CreateStatementN failed");
620 __pStmt->BindInt(0, aIndex);
621 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
624 TryCatch(r == E_SUCCESS, , "CallSettingDataService::DeleteSpeedDialInfoFromDatabase Delete SpeedDialInfo from DB failed");
626 __pDatabase->CommitTransaction();
645 CallSettingDataService::UpdateSpeedDialInfoDatabase(int aIndex, SpeedDialInfo* pSpeedDialInfoItem)
650 result r = E_SUCCESS;
652 TryCatch(pSpeedDialInfoItem != null, , "CallSettingDataService::UpdateSpeedDialInfoDatabase() no item to update");
654 __pDatabase->BeginTransaction();
656 statement.Append(L"UPDATE SpeedDialTable SET contactInfo=?,keyMappedTo=? WHERE id=?");
657 __pStmt = __pDatabase->CreateStatementN(statement);
659 TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateSpeedDialInfoDatabase CreateStatementN failed");
662 r = __pStmt->BindString(0, pSpeedDialInfoItem->contactId);
663 __pStmt->BindInt(1, pSpeedDialInfoItem->keyMapping);
664 __pStmt->BindInt(2, aIndex);
666 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
669 TryCatch(r == E_SUCCESS, , "CallSettingDataService::UpdateSpeedDialInfoDatabase Update SpeedDialInfo in DB failed");
671 __pDatabase->CommitTransaction();
689 IMapT<int,SpeedDialInfo>*
690 CallSettingDataService::GetAllSpeedDialInfoMapFromDatabaseN(void)
694 result r = E_SUCCESS;
695 HashMapT<int,SpeedDialInfo>* pSpeedDialInfoList = null;
696 r = __pDatabase->BeginTransaction();
698 sql.Append(L"SELECT id,contactInfo,keyMappedTo from SpeedDialTable");
699 __pEnum = __pDatabase->QueryN(sql);
701 TryCatch(r == E_SUCCESS, , "CallSettingDataService::GetAllSpeedDialInfoFromDatabase Get SpeedDialInfo from DB failed");
703 //check if the query returned any result and then iterate through the results
706 pSpeedDialInfoList = new (std::nothrow) HashMapT<int,SpeedDialInfo>();
707 pSpeedDialInfoList->Construct(10);
708 while (__pEnum->MoveNext() == E_SUCCESS)
710 SpeedDialInfo* pSpeedDialInfoItem = new (std::nothrow) SpeedDialInfo();
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);
720 __pDatabase->CommitTransaction();
727 SyncWithCurrentAddressbookStatus(pSpeedDialInfoList);
728 return pSpeedDialInfoList;
731 return pSpeedDialInfoList;
735 CallSettingDataService::SyncWithCurrentAddressbookStatus(HashMapT<int,SpeedDialInfo>* pSpeedDialInfoList)
737 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
738 Addressbook* pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
740 if(pAddressbook == null || pSpeedDialInfoList == null)
745 IListT<SpeedDialInfo>* pSpeedDialList = pSpeedDialInfoList->GetValuesN();
746 IEnumeratorT<SpeedDialInfo> *pSpeedDialListEnum = pSpeedDialList->GetEnumeratorN();
747 while(pSpeedDialListEnum->MoveNext() == E_SUCCESS)
749 SpeedDialInfo speeDialInfo;
750 RecordId recordId = INVALID_RECORD_ID;
751 pSpeedDialListEnum->GetCurrent(speeDialInfo);
752 Integer::Parse(speeDialInfo.contactId, recordId);
754 Contact* pMappedContact = pAddressbook->GetContactN(recordId);
755 if(pMappedContact == null)
757 DeleteSpeedDialInfoFromDatabase(speeDialInfo.rowId);
758 pSpeedDialInfoList->Remove(speeDialInfo.keyMapping);
762 delete pMappedContact;
766 delete pSpeedDialListEnum;
767 delete pSpeedDialList;
772 CallSettingDataService::GetSpeedDialContactN(int aIndex)
774 result r = E_SUCCESS;
778 String* fetchedInfo = null;
780 __pDatabase->BeginTransaction();
782 statement.Append(L"SELECT contactInfo from SpeedDialTable WHERE keyMappedTo=?");
783 __pStmt = __pDatabase->CreateStatementN(statement);
785 TryCatch(r == E_SUCCESS, , "GetSpeedDialInfoN - CreateStatementN failed");
788 __pStmt->BindInt(0, aIndex);
790 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
793 TryCatch(r == E_SUCCESS, , "GetSpeedDialInfoN - ExecuteStatementN failed");
795 //check if the query returned any result and then iterate through the results
796 if (__pEnum != null && __pEnum->MoveNext() == E_SUCCESS)
799 __pEnum->GetStringAt(0, contact);
800 fetchedInfo = new (std::nothrow) String(contact);
803 __pDatabase->CommitTransaction();