Return an error when an initialization has failed.
[platform/framework/native/net.git] / src / FNet_NetAccountDatabase.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18 * @file                 FNet_NetAccountDatabase.cpp
19 * @brief                This is the implementation file for the %_NetAccountDatabase class.
20 *
21 * This file contains the implementation of the %_NetAccountDatabase class.
22 */
23
24 #include <unique_ptr.h>
25 #include <FBaseString.h>
26 #include <FBaseColIList.h>
27 #include <FBaseColIListT.h>
28 #include <FIoDatabase.h>
29 #include <FIoDbStatement.h>
30 #include <FIoDbEnumerator.h>
31 #include <FIoDirectory.h>
32 #include <FAppApp.h>
33 #include <FBaseSysLog.h>
34 #include <FApp_AppInfo.h>
35 #include "FNet_NetTypes.h"
36 #include "FNet_NetUtility.h"
37 #include "FNet_NetAccountDatabase.h"
38
39 using namespace std;
40 using namespace Tizen::App;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Io;
44
45 namespace Tizen { namespace Net {
46
47 result
48 _NetAccountDatabase::InitializeRepository(void)
49 {
50         static const wchar_t _NET_ACCOUNT_DATABASE_CREATE_TABLE_STATEMENT[] =
51                         L"CREATE TABLE IF NOT EXISTS NetAccountTable (accountId INTEGER UNIQUE, accountName TEXT(256) UNIQUE, profileName TEXT(256) UNIQUE, isReadOnly INTEGER)";
52
53         result r = E_SUCCESS;
54         Database accountDb;
55
56         if (!Database::Exists(_NetAccountDatabase::GetDbPath()))
57         {
58                 SysLog(NID_NET, "NetAccount database is NOT found, so create it now.");
59         }
60
61         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), true);
62         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
63
64         r = accountDb.ExecuteSql(String(_NET_ACCOUNT_DATABASE_CREATE_TABLE_STATEMENT), true);
65         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
66
67         return r;
68 }
69
70 result
71 _NetAccountDatabase::AddAccount(const String& accountName, const String& profileName, _NetAccountOwner owner, NetAccountId& accountId)
72 {
73         static const wchar_t _NET_ACCOUNT_DATABASE_GET_MAX_ACCOUNT_ID_STATEMENT[] =
74                         L"SELECT MAX(accountId) FROM NetAccountTable";
75         static const wchar_t _NET_ACCOUNT_DATABASE_ADD_ACCOUNT_STATEMENT[] =
76                         L"INSERT INTO NetAccountTable (accountId, accountName, profileName, isReadOnly) VALUES(?, ?, ?, ?)";
77
78         result r = E_SUCCESS;
79         Database accountDb;
80         unique_ptr<DbStatement> pStmt;
81         unique_ptr<DbEnumerator> pEnum;
82         bool isReadOnly = true;
83         NetAccountId netAccountId = INVALID_HANDLE;
84
85         SysSecureLog(NID_NET, "AddAccount() has been called with accountName:%ls, profileName:%ls, owner:%d",
86                         accountName.GetPointer(), profileName.GetPointer(), owner);
87
88         accountId = INVALID_HANDLE;
89
90         SysTryReturnResult(NID_NET, !accountName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. accountName is an empty string.");
91         SysTryReturnResult(NID_NET, !profileName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. profileName is an empty string.");
92
93         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
94         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
95
96         if (owner == _NET_ACCOUNT_OWNER_THIS)
97         {
98                 isReadOnly = false;
99         }
100         else
101         {
102                 isReadOnly = true;
103         }
104
105         if (owner == _NET_ACCOUNT_OWNER_SYSTEM_INTERNET)
106         {
107                 netAccountId = _DEFAULT_PS_ACCOUNT_ID;
108         }
109         else if (owner == _NET_ACCOUNT_OWNER_SYSTEM_MMS)
110         {
111                 netAccountId = _DEFAULT_MMS_ACCOUNT_ID;
112         }
113         else
114         {
115                 pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_MAX_ACCOUNT_ID_STATEMENT)));
116                 r = GetLastResult();
117                 SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
118
119                 pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
120                 if (pEnum != null)
121                 {
122                         while (pEnum->MoveNext() == E_SUCCESS)
123                         {
124                                 pEnum->GetIntAt(0, netAccountId);
125                                 break;
126                         }
127                 }
128
129                 SysTryReturnResult(NID_NET, netAccountId >= 0, E_SYSTEM,
130                                 "[%s] A system error has been occurred. Failed to allocate a new accountId.", GetErrorMessage(E_SYSTEM));
131
132                 if (netAccountId >= _CUSTOM_ACCOUNT_ID_START)
133                 {
134                         netAccountId++;
135                 }
136                 else
137                 {
138                         netAccountId = _CUSTOM_ACCOUNT_ID_START;
139                 }
140         }
141
142         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_ADD_ACCOUNT_STATEMENT)));
143         r = GetLastResult();
144         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
145
146         r = pStmt->BindInt(0, netAccountId);
147         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
148
149         r = pStmt->BindString(1, accountName);
150         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
151
152         r = pStmt->BindString(2, profileName);
153         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
154
155         r = pStmt->BindInt(3, (int)isReadOnly);
156         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
157
158         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
159         r = GetLastResult();
160         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
161
162         accountId = netAccountId;
163
164         return r;
165 }
166
167 result
168 _NetAccountDatabase::RemoveAccountByAccountId(NetAccountId accountId)
169 {
170         static const wchar_t _NET_ACCOUNT_DATABASE_REMOVE_ACCOUNT_BY_ACCOUNT_ID_STATEMENT[] =
171                         L"DELETE FROM NetAccountTable WHERE accountId=?";
172
173         SysSecureLog(NID_NET, "RemoveAccountByAccountId() has been called with accountId:%d", accountId);
174
175         result r = E_SUCCESS;
176         Database accountDb;
177         unique_ptr<DbStatement> pStmt;
178         unique_ptr<DbEnumerator> pEnum;
179
180         SysTryReturnResult(NID_NET, accountId > 0, E_INVALID_ARG, "Invalid argument is used. accountId=%d", accountId);
181
182         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
183         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
184
185         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_REMOVE_ACCOUNT_BY_ACCOUNT_ID_STATEMENT)));
186         r = GetLastResult();
187         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
188
189         r = pStmt->BindInt(0, accountId);
190         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
191
192         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
193         r = GetLastResult();
194
195         return r;
196 }
197
198 result
199 _NetAccountDatabase::RemoveAccountByProfileName(const String& profileName)
200 {
201         static const wchar_t _NET_ACCOUNT_DATABASE_REMOVE_ACCOUNT_BY_PROFILE_NAME_STATEMENT[] =
202                         L"DELETE FROM NetAccountTable WHERE profileName=?";
203
204         SysLog(NID_NET, "RemoveAccountByProfileName() has been called with profileName:%ls", profileName.GetPointer());
205
206         result r = E_SUCCESS;
207         Database accountDb;
208         unique_ptr<DbStatement> pStmt;
209         unique_ptr<DbEnumerator> pEnum;
210
211         SysTryReturnResult(NID_NET, !profileName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. profileName is an empty string.");
212
213         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
214         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
215
216         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_REMOVE_ACCOUNT_BY_PROFILE_NAME_STATEMENT)));
217         r = GetLastResult();
218         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
219
220         r = pStmt->BindString(0, profileName);
221         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
222
223         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
224         r = GetLastResult();
225
226         return r;
227 }
228
229 result
230 _NetAccountDatabase::UpdateAccountName(NetAccountId accountId, const String& accountName)
231 {
232         static const wchar_t _NET_ACCOUNT_DATABASE_UPDATE_ACCOUNT_NAME_STATEMENT[] =
233                         L"UPDATE NetAccountTable SET accountName=? WHERE accountId=?";
234
235         SysSecureLog(NID_NET, "UpdateAccountName() has been called with accountId:%d, accountName:%ls", accountId, accountName.GetPointer());
236
237         result r = E_SUCCESS;
238         Database accountDb;
239         unique_ptr<DbStatement> pStmt;
240         unique_ptr<DbEnumerator> pEnum;
241
242         SysTryReturnResult(NID_NET, accountId > 0, E_INVALID_ARG, "AccountId[%d] is invalid.", accountId);
243         SysTryReturnResult(NID_NET, !accountName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. accountName is an empty string.");
244
245         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
246         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
247
248         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_UPDATE_ACCOUNT_NAME_STATEMENT)));
249         r = GetLastResult();
250         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
251
252         r = pStmt->BindString(0, accountName);
253         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
254
255         r = pStmt->BindInt(1, accountId);
256         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
257
258         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
259         r = GetLastResult();
260
261         return r;
262 }
263
264 IListT<NetAccountId>*
265 _NetAccountDatabase::GetAccountIdsN(void)
266 {
267         static const wchar_t _NET_ACCOUNT_DATABASE_GET_ACCOUNT_IDS_STATEMENT[] =
268                         L"SELECT accountId FROM NetAccountTable";
269
270         result r = E_SUCCESS;
271         Database accountDb;
272         unique_ptr<DbStatement> pStmt;
273         unique_ptr<DbEnumerator> pEnum;
274         unique_ptr< ArrayListT<NetAccountId> > pList;
275
276         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
277         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
278
279         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_ACCOUNT_IDS_STATEMENT)));
280         r = GetLastResult();
281         SysTryReturn(NID_NET, pStmt != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
282
283         pList.reset(new (std::nothrow) ArrayListT<NetAccountId>());
284         r = GetLastResult();
285         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
286
287         r = pList->Construct();
288         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
289
290         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
291         r = GetLastResult();
292         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
293
294         if (pEnum != null)
295         {
296                 while (pEnum->MoveNext() == E_SUCCESS)
297                 {
298                         NetAccountId accountId = INVALID_HANDLE;
299
300                         r = pEnum->GetIntAt(0, accountId);
301                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
302
303                         r = pList->Add(accountId);
304                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
305                 }
306         }
307
308         ClearLastResult(); // To suppress E_OUT_OF_RANGE
309
310         return pList.release();
311 }
312
313 IList*
314 _NetAccountDatabase::GetAccountNamesN(void)
315 {
316         static const wchar_t _NET_ACCOUNT_DATABASE_GET_ACCOUNT_NAMES_STATEMENT[] =
317                         L"SELECT accountName FROM NetAccountTable";
318
319         result r = E_SUCCESS;
320         Database accountDb;
321         unique_ptr<DbStatement> pStmt;
322         unique_ptr<DbEnumerator> pEnum;
323         unique_ptr<ArrayList, _CollectionDeleter> pList;
324
325         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
326         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
327
328         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_ACCOUNT_NAMES_STATEMENT)));
329         r = GetLastResult();
330         SysTryReturn(NID_NET, pStmt != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
331
332         pList.reset(new (std::nothrow) ArrayList());
333         r = GetLastResult();
334         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
335
336         r = pList->Construct();
337         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
338
339         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
340         r = GetLastResult();
341         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
342
343         if (pEnum != null)
344         {
345                 while (pEnum->MoveNext() == E_SUCCESS)
346                 {
347                         unique_ptr<String> pAccountName(new (std::nothrow) String());
348                         SysTryReturn(NID_NET, pAccountName != null, null, E_OUT_OF_MEMORY,
349                                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
350
351                         r = pEnum->GetStringAt(0, *pAccountName);
352                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
353
354                         r = pList->Add(*pAccountName);
355                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
356
357                         pAccountName.release();
358                 }
359         }
360
361         ClearLastResult(); // To suppress E_OUT_OF_RANGE
362
363         return pList.release();
364 }
365
366 IList*
367 _NetAccountDatabase::GetProfileNamesN(void)
368 {
369         static const wchar_t _NET_ACCOUNT_DATABASE_GET_PROFILE_NAMES_STATEMENT[] =
370                         L"SELECT profileName FROM NetAccountTable";
371
372         result r = E_SUCCESS;
373         Database accountDb;
374         unique_ptr<DbStatement> pStmt;
375         unique_ptr<DbEnumerator> pEnum;
376         unique_ptr<ArrayList, _CollectionDeleter> pList;
377
378         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
379         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
380
381         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_PROFILE_NAMES_STATEMENT)));
382         r = GetLastResult();
383         SysTryReturn(NID_NET, pStmt != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
384
385         pList.reset(new (std::nothrow) ArrayList());
386         r = GetLastResult();
387         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
388
389         r = pList->Construct();
390         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
391
392         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
393         r = GetLastResult();
394         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
395
396         if (pEnum != null)
397         {
398                 while (pEnum->MoveNext() == E_SUCCESS)
399                 {
400                         unique_ptr<String> pProfileName(new (std::nothrow) String());
401                         SysTryReturn(NID_NET, pProfileName != null, null, E_OUT_OF_MEMORY,
402                                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
403
404                         r = pEnum->GetStringAt(0, *pProfileName);
405                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
406
407                         r = pList->Add(*pProfileName);
408                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
409
410                         pProfileName.release();
411                 }
412         }
413
414         ClearLastResult(); // To suppress E_OUT_OF_RANGE
415
416         return pList.release();
417 }
418
419 IList*
420 _NetAccountDatabase::GetAppProfileNamesN(void)
421 {
422         static const wchar_t _NET_ACCOUNT_DATABASE_GET_PROFILE_NAMES_STATEMENT[] =
423                         L"SELECT profileName FROM NetAccountTable WHERE accountId>=?";
424
425         result r = E_SUCCESS;
426         Database accountDb;
427         unique_ptr<DbStatement> pStmt;
428         unique_ptr<DbEnumerator> pEnum;
429         unique_ptr<ArrayList, _CollectionDeleter> pList;
430
431         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
432         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
433
434         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_PROFILE_NAMES_STATEMENT)));
435         r = GetLastResult();
436         SysTryReturn(NID_NET, pStmt != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
437
438         r = pStmt->BindInt(0, _CUSTOM_ACCOUNT_ID_START);
439         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
440
441         pList.reset(new (std::nothrow) ArrayList());
442         r = GetLastResult();
443         SysTryReturn(NID_NET, pList != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
444
445         r = pList->Construct();
446         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
447
448         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
449         r = GetLastResult();
450         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
451
452         if (pEnum != null)
453         {
454                 while (pEnum->MoveNext() == E_SUCCESS)
455                 {
456                         unique_ptr<String> pProfileName(new (std::nothrow) String());
457                         SysTryReturn(NID_NET, pProfileName != null, null, E_OUT_OF_MEMORY,
458                                         "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
459
460                         r = pEnum->GetStringAt(0, *pProfileName);
461                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
462
463                         r = pList->Add(*pProfileName);
464                         SysTryReturn(NID_NET, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
465
466                         pProfileName.release();
467                 }
468         }
469
470         ClearLastResult(); // To suppress E_OUT_OF_RANGE
471
472         return pList.release();
473 }
474
475 result
476 _NetAccountDatabase::GetAccountName(NetAccountId accountId, String& accountName)
477 {
478         static const wchar_t _NET_ACCOUNT_DATABASE_GET_ACCOUNT_NAME_STATEMENT[] =
479                         L"SELECT accountName FROM NetAccountTable WHERE accountId=?";
480
481         SysSecureLog(NID_NET, "GetAccountName() has been called with accountId:%d", accountId);
482
483         result r = E_SUCCESS;
484         Database accountDb;
485         unique_ptr<DbStatement> pStmt;
486         unique_ptr<DbEnumerator> pEnum;
487
488         SysTryReturnResult(NID_NET, accountId > 0, E_INVALID_ARG, "Invalid argument is used. accountId=%d", accountId);
489
490         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
491         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
492
493         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_ACCOUNT_NAME_STATEMENT)));
494         r = GetLastResult();
495         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
496
497         r = pStmt->BindInt(0, accountId);
498         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
499
500         r = E_OBJ_NOT_FOUND;
501
502         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
503         if (pEnum != null)
504         {
505                 while (pEnum->MoveNext() == E_SUCCESS)
506                 {
507                         pEnum->GetStringAt(0, accountName);
508                         r = E_SUCCESS;
509                         break;
510                 }
511         }
512
513         return r;
514 }
515
516 result
517 _NetAccountDatabase::GetProfileName(NetAccountId accountId, String& profileName)
518 {
519         static const wchar_t _NET_ACCOUNT_DATABASE_GET_PROFILE_NAME_STATEMENT[] =
520                         L"SELECT profileName FROM NetAccountTable WHERE accountId=?";
521
522         SysSecureLog(NID_NET, "GetProfileName() has been called with accountId:%d", accountId);
523
524         result r = E_SUCCESS;
525         Database accountDb;
526         unique_ptr<DbStatement> pStmt;
527         unique_ptr<DbEnumerator> pEnum;
528
529         SysTryReturnResult(NID_NET, accountId > 0, E_INVALID_ARG, "Invalid argument is used. accountId=%d", accountId);
530
531         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
532         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
533
534         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_PROFILE_NAME_STATEMENT)));
535         r = GetLastResult();
536         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
537
538         r = pStmt->BindInt(0, accountId);
539         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
540
541         r = E_OBJ_NOT_FOUND;
542
543         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
544         if (pEnum != null)
545         {
546                 while (pEnum->MoveNext() == E_SUCCESS)
547                 {
548                         pEnum->GetStringAt(0, profileName);
549                         r = E_SUCCESS;
550                         break;
551                 }
552         }
553
554         return r;
555 }
556
557 result
558 _NetAccountDatabase::GetAccountIdByAccountName(const String& accountName, NetAccountId& accountId)
559 {
560         static const wchar_t _NET_ACCOUNT_DATABASE_GET_ACCOUNT_ID_BY_ACCOUNT_NAME_STATEMENT[] =
561                         L"SELECT accountId FROM NetAccountTable WHERE accountName=?";
562
563         SysSecureLog(NID_NET, "GetAccountIdByAccountName() has been called with accountName:%ls", accountName.GetPointer());
564
565         result r = E_SUCCESS;
566         Database accountDb;
567         unique_ptr<DbStatement> pStmt;
568         unique_ptr<DbEnumerator> pEnum;
569
570         SysTryReturnResult(NID_NET, !accountName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. accountName is an empty string.");
571
572         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
573         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
574
575         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_ACCOUNT_ID_BY_ACCOUNT_NAME_STATEMENT)));
576         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
577
578         r = pStmt->BindString(0, accountName);
579         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
580
581         r = E_OBJ_NOT_FOUND;
582
583         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
584         if (pEnum != null)
585         {
586                 while (pEnum->MoveNext() == E_SUCCESS)
587                 {
588                         pEnum->GetIntAt(0, accountId);
589                         r = E_SUCCESS;
590                         break;
591                 }
592         }
593
594         return r;
595 }
596
597 result
598 _NetAccountDatabase::GetAccountIdByProfileName(const String& profileName, NetAccountId& accountId)
599 {
600         static const wchar_t _NET_ACCOUNT_DATABASE_GET_ACCOUNT_ID_BY_PROFILE_NAME_STATEMENT[] =
601                         L"SELECT accountId FROM NetAccountTable WHERE profileName=?";
602
603         SysLog(NID_NET, "GetAccountIdByProfileName() has been called with profileName:%ls", profileName.GetPointer());
604
605         result r = E_SUCCESS;
606         Database accountDb;
607         unique_ptr<DbStatement> pStmt;
608         unique_ptr<DbEnumerator> pEnum;
609
610         SysTryReturnResult(NID_NET, !profileName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. profileName is an empty string.");
611
612         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
613         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
614
615         pStmt.reset(accountDb.CreateStatementN(String(_NET_ACCOUNT_DATABASE_GET_ACCOUNT_ID_BY_PROFILE_NAME_STATEMENT)));
616         r = GetLastResult();
617         SysTryReturnResult(NID_NET, pStmt != null, r, "Propagating.");
618
619         r = pStmt->BindString(0, profileName);
620         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
621
622         r = E_OBJ_NOT_FOUND;
623
624         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
625         if (pEnum != null)
626         {
627                 while (pEnum->MoveNext() == E_SUCCESS)
628                 {
629                         pEnum->GetIntAt(0, accountId);
630                         r = E_SUCCESS;
631                         break;
632                 }
633         }
634
635         return r;
636 }
637
638 bool
639 _NetAccountDatabase::IsReadOnly(NetAccountId accountId)
640 {
641         static const wchar_t NET_ACCOUNT_DATABASE_GET_IS_READ_ONLY_STATEMENT[] =
642                         L"SELECT isReadOnly FROM NetAccountTable WHERE accountId=?";
643
644         SysSecureLog(NID_NET, "IsReadOnly() has been called with accountId:%d", accountId);
645
646         result r = E_SUCCESS;
647         Database accountDb;
648         unique_ptr<DbStatement> pStmt;
649         unique_ptr<DbEnumerator> pEnum;
650         int isReadOnly = 1;
651
652         SysTryReturn(NID_NET, accountId > 0, true, E_INVALID_ARG,
653                         "[%s] Invalid argument is used. accountId=%d", GetErrorMessage(E_INVALID_ARG), accountId);
654
655         r = accountDb.Construct(_NetAccountDatabase::GetDbPath(), false);
656         SysTryReturn(NID_NET, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
657
658         pStmt.reset(accountDb.CreateStatementN(String(NET_ACCOUNT_DATABASE_GET_IS_READ_ONLY_STATEMENT)));
659         r = GetLastResult();
660         SysTryReturn(NID_NET, pStmt != null, true, r, "[%s] Propagating.", GetErrorMessage(r));
661
662         r = pStmt->BindInt(0, accountId);
663         SysTryReturn(NID_NET, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
664
665         r = E_OBJ_NOT_FOUND;
666         pEnum.reset(accountDb.ExecuteStatementN(*pStmt));
667         if (pEnum != null)
668         {
669                 while (pEnum->MoveNext() == E_SUCCESS)
670                 {
671                         pEnum->GetIntAt(0, isReadOnly);
672                         r = E_SUCCESS;
673                         break;
674                 }
675         }
676         SysTryReturn(NID_NET, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));
677
678         ClearLastResult();
679
680         return (bool)isReadOnly;
681 }
682
683 String
684 _NetAccountDatabase::GetDbPath(void)
685 {
686         static const wchar_t _OLD_DATA_PATH[] = L"/Home/";
687         static const wchar_t _NET_ACCOUNT_DATABASE_FILE_NAME[] = L"netAccount.db";
688
689         String dbPath;
690         _ApiVersion apiVersion = _AppInfo::GetApiVersion();
691
692         if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
693         {
694                 dbPath = String(_OLD_DATA_PATH);
695         }
696         else
697         {
698                 dbPath = Tizen::App::App::GetInstance()->GetAppDataPath();
699         }
700
701         dbPath += String(_NET_ACCOUNT_DATABASE_FILE_NAME);
702
703         SysLog(NID_NET, "GetDbPath() has been succeeded with the path:%ls", dbPath.GetPointer());
704
705         return dbPath;
706 }
707
708 }} // Tizen::Net