50ffc766069acb85acae9dc0a9589f957fa8217e
[apps/osp/Call.git] / src / CallSettingDataService.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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    CallCallSettingDataService.cpp
19  * @brief       Call Database class
20  */
21
22 #include <FApp.h>
23 #include <FSocial.h>
24 #include <FIo.h>
25 #include "CallSettingDataService.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 static const wchar_t* REJECT_DATABASE_PATH = L"/opt/usr/apps/zktdpemtmw/";
34
35 CallSettingDataService* CallSettingDataService::__pCallSettingDataService = null;
36 Database* CallSettingDataService::__pDatabase = null;
37
38 CallSettingDataService::CallSettingDataService(void)
39 : __strDbName(String(REJECT_DATABASE_PATH) + L"data/CallRejectInfoListDataBase")
40 {
41         __pStmt = null;
42         __pEnum = null;
43
44         return;
45 }
46
47 CallSettingDataService::~CallSettingDataService(void)
48 {
49         if (__pDatabase != null)
50         {
51                 delete __pDatabase;
52                 __pDatabase = null;
53         }
54         __pCallSettingDataService = null;
55         return;
56 }
57
58 CallSettingDataService*
59 CallSettingDataService::CreateInstance(void)
60 {
61         if (__pCallSettingDataService == null)
62         {
63                 __pCallSettingDataService = new (std::nothrow) CallSettingDataService();
64         }
65         return __pCallSettingDataService;
66 }
67 result
68 CallSettingDataService::OpenDatabase(void)
69 {
70         result r = E_SUCCESS;
71
72         // create the database if it doesn't exist
73         if (__pDatabase != null)
74         {
75                 return E_OBJ_ALREADY_EXIST;
76         }
77         __pDatabase = new (std::nothrow) Database();
78         r = __pDatabase->Construct(__strDbName, true);
79         TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() database construct failed");
80
81         r = CreateCallRejectTableDatabase();
82         //      TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() call reject Create Table failed");
83         r = CreateSpeedDialTableDatabase();
84         //      TryCatch(r == E_SUCCESS, , "CallSettingDataService::OpenDatabase() speed dial Create Table failed");
85
86         return r;
87
88         CATCH:
89         delete __pDatabase;
90         __pDatabase = null;
91         return r;
92 }
93
94 result
95 CallSettingDataService::CreateCallRejectTableDatabase(void)
96 {
97         String sqlQuery;
98         result r = E_SUCCESS;
99
100         sqlQuery.Append(L"CREATE TABLE IF NOT EXISTS CallRejectInfoTable(id INTEGER PRIMARY KEY AUTOINCREMENT,phonenumber TEXT,rejectcondition INT,activated INT)");
101
102         r = __pDatabase->ExecuteSql(sqlQuery, true);
103         TryCatch(r == E_SUCCESS, , "CallSettingDataService::CreateCallRejectInfoTableDatabase() Create Table failed");
104
105         return r;
106
107         CATCH:
108         return r;
109 }
110
111 result
112 CallSettingDataService::CreateSpeedDialTableDatabase(void)
113 {
114         String sqlQuery;
115         result r = E_SUCCESS;
116
117         sqlQuery.Append(L"CREATE TABLE IF NOT EXISTS SpeedDialTable(id INTEGER PRIMARY KEY AUTOINCREMENT,contactInfo TEXT,keyMappedTo INT)");
118
119         r = __pDatabase->ExecuteSql(sqlQuery, true);
120         TryCatch(r == E_SUCCESS, , "CallSettingDataService::CreateCallRejectInfoTableDatabase() Create Table failed");
121
122         return r;
123
124         CATCH:
125         return r;
126 }
127
128 result
129 CallSettingDataService::CloseDatabase(void)
130 {
131         if (__pEnum != null)
132         {
133                 delete __pEnum;
134                 __pEnum = null;
135         }
136         if (__pStmt != null)
137         {
138                 delete __pStmt;
139                 __pStmt = null;
140         }
141         if (__pDatabase != null)
142         {
143                 delete __pDatabase;
144                 __pDatabase = null;
145         }
146
147         return E_SUCCESS;
148 }
149
150
151 bool
152 CallSettingDataService::IsCallToBeRejected(String& phoneNumber)
153 {
154         AppLogDebug("Enter");
155         bool isCallToBeRejected  = false;
156         String statement;
157         __pStmt = null;
158         __pEnum = null;
159         result r = E_SUCCESS;
160         __pDatabase->BeginTransaction();
161
162         //statement.Append(L"SELECT * FROM CallRejectInfoTable WHERE phonenumber LIKE ?");
163         statement.Append(L"SELECT * FROM CallRejectInfoTable WHERE activated=?");
164
165         __pStmt = __pDatabase->CreateStatementN(statement);
166         r = GetLastResult();
167         TryCatch(r == E_SUCCESS, , "CallSettingDataService::SearchFromDataBase CreateStatementN failed");
168
169         //__pStmt->BindString(0, phoneNumber);
170         if (__pStmt != null)
171         {
172                 __pStmt->BindInt(0,(int)true);
173                 __pEnum = __pDatabase->ExecuteStatementN(*__pStmt);
174         }
175         r = GetLastResult();
176         TryCatch(r == E_SUCCESS, , "CallSettingDataService::SearchFromDataBaseN search failed");
177
178         //check if the query returned any result and then iterate through the results
179         if (__pEnum != null)
180         {
181                 while (__pEnum->MoveNext() == E_SUCCESS)
182                 {
183                         CallRejectInfo* pCallRejectInfoItem = new (std::nothrow) CallRejectInfo;
184                         int activateflag;
185                         int rejectCondition;
186                         __pEnum->GetIntAt(0, pCallRejectInfoItem->rowId);
187                         __pEnum->GetStringAt(1, pCallRejectInfoItem->phoneNumber);
188                         __pEnum->GetIntAt(2, rejectCondition);
189                         __pEnum->GetIntAt(3, activateflag);
190                         pCallRejectInfoItem->rejectCondition = (CallRejectMatchCondition)rejectCondition;
191                         pCallRejectInfoItem->isActivated = (bool)activateflag;
192                         if (CheckRejectCondition(phoneNumber, *pCallRejectInfoItem) == true)
193                         {
194                                 delete pCallRejectInfoItem;
195                                 isCallToBeRejected = true;
196                                 break;
197                         }
198                         delete pCallRejectInfoItem;
199                 }
200         }
201         __pDatabase->CommitTransaction();
202
203         if (__pStmt != null)
204         {
205                 delete __pStmt;
206                 __pStmt = NULL;
207         }
208         if (__pEnum != null)
209         {
210                 delete __pEnum;
211                 __pEnum = NULL;
212         }
213         AppLogDebug("%d",isCallToBeRejected);
214         return isCallToBeRejected;
215
216         CATCH:
217         return isCallToBeRejected;
218 }
219
220 bool
221 CallSettingDataService::CheckRejectCondition(String& phoneNumber, CallRejectInfo& callRejectInfo)
222 {
223         bool isCallToBeRejected  = false;
224         switch (callRejectInfo.rejectCondition)
225         {
226         case CALL_REJECT_MATCH_EXACTLY:
227         {
228                 if (callRejectInfo.phoneNumber.CompareTo(phoneNumber) == 0)
229                 {
230                         isCallToBeRejected = true;
231                 }
232         }
233         break;
234         case CALL_REJECT_MATCH_START:
235         {
236                 isCallToBeRejected = phoneNumber.StartsWith(callRejectInfo.phoneNumber, 0);
237         }
238         break;
239         case CALL_REJECT_MATCH_END:
240         {
241                 isCallToBeRejected = phoneNumber.EndsWith(callRejectInfo.phoneNumber);
242         }
243         break;
244         case CALL_REJECT_MATCH_INCLUDE:
245         {
246                 isCallToBeRejected = phoneNumber.Contains(callRejectInfo.phoneNumber);
247         }
248         break;
249         default:
250                 break;
251         }
252
253         return isCallToBeRejected;
254 }
255