Update network account database when a default profile is changed.
[platform/framework/native/net.git] / src / FNetNetAccountManagerPartner.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 /**
19  * @file                        FNetNetAccountManagerPartner.cpp
20  * @brief               This is the implementation for the %NetAccountManager class. (only partner visibility)
21  */
22
23 #include <net_connection.h>
24 #include <FNetNetAccountManager.h>
25 #include <FBaseSysLog.h>
26 #include <FSec_AccessController.h>
27 #include <FSys_SystemInfoImpl.h>
28 #include "FNet_NetTypes.h"
29 #include "FNet_NetAccountDatabase.h"
30 #include "FNet_NetAccountInfoImpl.h"
31 #include "FNet_NetAccountManagerImpl.h"
32 #include "FNet_NetConnectionManagerImpl.h"
33 #include "FNet_NetIpcProxy.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Security;
37 using namespace Tizen::System;
38
39 namespace Tizen { namespace Net
40 {
41
42 result
43 NetAccountManager::UpdateSystemNetAccount(const NetAccountInfo& netAccountInfo)
44 {
45         result r = E_SUCCESS;
46         String profileName;
47         NetBearerType bearerType = netAccountInfo.GetBearerType();
48
49         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
50
51 #ifndef _OSP_EMUL_
52         if (bearerType == NET_BEARER_PS)
53         {
54                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
55
56                 bool isTelephonySupported = false;
57
58                 r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
59                 SysTryReturnResult(NID_NET, r == E_SUCCESS && isTelephonySupported, E_UNSUPPORTED_OPERATION, "Telephony is not supported.");
60         }
61         else if (bearerType == NET_BEARER_MMS)
62         {
63                 static const wchar_t _MMS[] = L"http://tizen.org/feature/network.telephony.mms";
64
65                 bool isMmsSupported = false;
66
67                 r = _SystemInfoImpl::GetSysInfo(_MMS, isMmsSupported);
68                 SysTryReturnResult(NID_NET, r == E_SUCCESS && isMmsSupported, E_UNSUPPORTED_OPERATION, "MMS is not supported.");
69         }
70 #endif // !_OSP_EMUL_
71
72         SysTryReturnResult(NID_NET, netAccountInfo.GetAccountId() > 0, E_INVALID_ARG,
73                         "Invalid argument is used. accountId=%d", netAccountInfo.GetAccountId());
74         SysTryReturnResult(NID_NET, !netAccountInfo.GetAccountName().IsEmpty(), E_INVALID_ARG,
75                         "Invalid argument is used. accountName is an empty string.");
76
77         r =  _NetAccountDatabase::GetProfileName(netAccountInfo.GetAccountId(), profileName);
78         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
79         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_ARG,
80                         "Invalid argument is used. accountId=%d", netAccountInfo.GetAccountId());
81
82         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
83         SysTryReturnResult(NID_NET, pProxy != null, E_SYSTEM,
84                         "A system error has been occurred. Failed to get an IPC proxy.");
85
86         r = pProxy->UpdateSystemNetAccount(profileName, netAccountInfo, bearerType);
87         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
88
89         r = _NetAccountDatabase::UpdateAccountName(netAccountInfo.GetAccountId(), netAccountInfo.GetAccountName());
90         SysTryReturnResult(NID_NET, r != E_OUT_OF_MEMORY, r, "Propagating.");
91         SysTryReturnResult(NID_NET, r == E_SUCCESS, E_SYSTEM,
92                         "A system error has been occurred. Failed to update account name.");
93
94         return r;
95 }
96
97 NetAccountId
98 NetAccountManager::GetAppNetAccountId(const String& netProfileName) const
99 {
100         result r = E_SUCCESS;
101         NetAccountId netAccountId = INVALID_HANDLE;
102
103         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
104
105 #ifndef _OSP_EMUL_
106         if (netProfileName.Equals(L"Internet", false))
107         {
108                 static const wchar_t _TELEPHONY[] = L"http://tizen.org/feature/network.telephony";
109
110                 bool isTelephonySupported = false;
111
112                 r = _SystemInfoImpl::GetSysInfo(_TELEPHONY, isTelephonySupported);
113                 SysTryReturn(NID_NET, r == E_SUCCESS && isTelephonySupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
114                                 "[%s] Telephony is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
115         }
116         else if (netProfileName.Equals(L"Mms", false))
117         {
118                 static const wchar_t _MMS[] = L"http://tizen.org/feature/network.telephony.mms";
119
120                 bool isMmsSupported = false;
121
122                 r = _SystemInfoImpl::GetSysInfo(_MMS, isMmsSupported);
123                 SysTryReturn(NID_NET, r == E_SUCCESS && isMmsSupported, INVALID_HANDLE, E_UNSUPPORTED_OPERATION,
124                                 "[%s] MMS is not supported.", GetErrorMessage(E_UNSUPPORTED_OPERATION));
125         }
126 #endif // !_OSP_EMUL_
127
128         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
129         SysTryReturn(NID_NET, pProxy != null, INVALID_HANDLE, E_SYSTEM,
130                         "[%s] A system error has been occurred. Failed to get an IPC proxy.", GetErrorMessage(E_SYSTEM));
131
132         r = pProxy->GetAppNetAccountId(netProfileName, netAccountId);
133         SysTryReturn(NID_NET, r == E_SUCCESS, INVALID_HANDLE, r, "[%s] Propagating.", GetErrorMessage(r));
134
135         if (netAccountId == _DEFAULT_PS_ACCOUNT_ID)
136         {
137                 netAccountId = _NetAccountManagerImpl::GetInternetAccountId();
138         }
139         else if (netAccountId == _DEFAULT_MMS_ACCOUNT_ID)
140         {
141                 netAccountId = _NetAccountManagerImpl::GetMmsAccountId();
142         }
143
144         ClearLastResult();
145
146         return netAccountId;
147 }
148
149 result
150 NetAccountManager::SetNetAccountId(NetAccountId netAccountId)
151 {
152         result r = E_SUCCESS;
153         NetAccountId netAccountId2 = INVALID_HANDLE;
154
155         SysAssertf(__pNetAccountManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
156
157         SysTryReturnResult(NID_NET, netAccountId > 0, E_INVALID_ARG,
158                         "Invalid argument is used. accountId=%d", netAccountId);
159
160         _NetConnectionManagerImpl* pConnectionManager = _NetConnectionManagerImpl::GetInstance();
161         SysTryReturnResult(NID_NET, pConnectionManager != null, E_SYSTEM,
162                         "A system error has been occurred. Failed to get NetConnectionManager instance.");
163
164         _NetIpcProxy* pProxy = _NetIpcProxy::GetInstance();
165         SysTryReturnResult(NID_NET, pProxy != null, E_SYSTEM,
166                         "A system error has been occurred. Failed to get an IPC proxy.");
167
168         r = pProxy->SetNetAccountId(netAccountId, netAccountId2);
169         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
170
171         r = pConnectionManager->SetManagedNetAccountId(netAccountId2);
172         SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
173
174         return r;
175 }
176
177 }} // Tizen::Net