Source code formating unification
[framework/web/wrt-commons.git] / modules / widget_dao / dao / global_dao_read_only.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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  * @file    global_dao_read_only.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains the definition of global dao class.
21  */
22 #include <stddef.h>
23 #include <dpl/wrt-dao-ro/global_dao_read_only.h>
24
25 #include <dpl/foreach.h>
26 #include <dpl/log/log.h>
27 #include <dpl/string.h>
28 #include <dpl/db/orm.h>
29 #include <orm_generator_wrt.h>
30 #include <dpl/wrt-dao-ro/webruntime_database.h>
31 #include <dpl/wrt-dao-ro/WrtDatabase.h>
32 #include <dpl/wrt-dao-ro/common_dao_types.h>
33
34 namespace WrtDB {
35 bool GlobalDAOReadOnly::GetDeveloperMode()
36 {
37     LogDebug("Getting Developer mode");
38     Try {
39         using namespace DPL::DB::ORM;
40         using namespace DPL::DB::ORM::wrt;
41         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
42         return select->GetSingleValue<GlobalProperties::developer_mode>();
43     }
44     Catch(DPL::DB::SqlConnection::Exception::Base){
45         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
46                    "Failed to get developer mode");
47     }
48 }
49
50 bool GlobalDAOReadOnly::GetSecureByDefault()
51 {
52     using namespace DPL::DB::ORM;
53     using namespace DPL::DB::ORM::wrt;
54     WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
55     return select->GetSingleValue<GlobalProperties::secure_by_default>();
56 }
57
58 bool GlobalDAOReadOnly::getComplianceMode()
59 {
60     LogDebug("Getting compliance mode");
61     Try {
62         using namespace DPL::DB::ORM;
63         using namespace DPL::DB::ORM::wrt;
64         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
65         return select->GetSingleValue<GlobalProperties::compliance_mode>();
66     }
67     Catch(DPL::DB::SqlConnection::Exception::Base){
68         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
69                    "Failed to get compliance mode");
70     }
71 }
72
73 std::string GlobalDAOReadOnly::getComplianceFakeImei()
74 {
75     LogDebug("Getting compliance fake IMEI");
76     Try {
77         using namespace DPL::DB::ORM;
78         using namespace DPL::DB::ORM::wrt;
79         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
80         DPL::Optional<DPL::String> result =
81             select->GetSingleValue<GlobalProperties::compliance_fake_imei>();
82         if (!result) {
83             return std::string();
84         }
85         return DPL::ToUTF8String(*result);
86     }
87     Catch(DPL::DB::SqlConnection::Exception::Base){
88         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
89                    "Failed to get compliance fake IMEI");
90     }
91 }
92
93 std::string GlobalDAOReadOnly::getComplianceFakeMeid()
94 {
95     LogDebug("Getting compliance fake MEID");
96     Try {
97         using namespace DPL::DB::ORM;
98         using namespace DPL::DB::ORM::wrt;
99         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
100         DPL::Optional<DPL::String> result =
101             select->GetSingleValue<GlobalProperties::compliance_fake_meid>();
102         if (!result) {
103             return std::string();
104         }
105         return DPL::ToUTF8String(*result);
106     }
107     Catch(DPL::DB::SqlConnection::Exception::Base){
108         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
109                    "Failed to get compliance fake MEID");
110     }
111 }
112
113 bool GlobalDAOReadOnly::IsValidSubTag(const DPL::String& tag, int type)
114 {
115     using namespace DPL::DB::ORM;
116     using namespace DPL::DB::ORM::wrt;
117     WRT_DB_SELECT(select, iana_records, &WrtDatabase::interface())
118     select->Where(Equals<iana_records::SUBTAG>(tag));
119     auto row = select->GetRowList();
120     if (row.size() == 0 || row.front().Get_TYPE() != type) {
121         return false;
122     } else {
123         return true;
124     }
125 }
126
127 GlobalDAOReadOnly::NetworkAccessMode
128 GlobalDAOReadOnly::GetHomeNetworkDataUsage()
129 {
130     LogDebug("Getting home network data usage");
131     Try {
132         using namespace DPL::DB::ORM;
133         using namespace DPL::DB::ORM::wrt;
134         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
135         return static_cast<GlobalDAOReadOnly::NetworkAccessMode>(
136                    select->GetSingleValue<
137                        GlobalProperties::home_network_data_usage>());
138     }
139     Catch(DPL::DB::SqlConnection::Exception::Base){
140         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
141                    "Failed to get home network data usage");
142     }
143 }
144
145 GlobalDAOReadOnly::NetworkAccessMode GlobalDAOReadOnly::GetRoamingDataUsage()
146 {
147     LogDebug("Getting roaming network data usage");
148     Try {
149         using namespace DPL::DB::ORM;
150         using namespace DPL::DB::ORM::wrt;
151         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
152         return static_cast<GlobalDAOReadOnly::NetworkAccessMode>(
153                    select->GetSingleValue<GlobalProperties::roaming_data_usage>());
154     }
155     Catch(DPL::DB::SqlConnection::Exception::Base){
156         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
157                    "Failed to get roaming network data usage");
158     }
159 }
160
161 //user agent strings are stored in db...
162 //and it is configurable for test in development.
163 DPL::String GlobalDAOReadOnly::GetUserAgentValue(const DPL::String &key)
164 {
165     LogDebug("Get User Agent Value : " << key);
166     Try {
167         using namespace DPL::DB::ORM;
168         using namespace DPL::DB::ORM::wrt;
169         WRT_DB_SELECT(select, UserAgents, &WrtDatabase::interface())
170         select->Where(Equals<UserAgents::key_name>(key));
171         DPL::Optional<DPL::String> agent =
172             select->GetSingleValue<UserAgents::key_value>();
173         if (agent.IsNull()) {
174             return DPL::FromUTF8String("");
175         } else {
176             return *agent;
177         }
178     }
179     Catch(DPL::DB::SqlConnection::Exception::Base){
180         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
181                    "Failed to get user agent string");
182     }
183 }
184
185 DeviceCapabilitySet GlobalDAOReadOnly::GetDeviceCapability(
186     const DPL::String &apifeature)
187 {
188     // This could be done with one simply sql query but support for join is
189     // needed in orm.
190     Try {
191         using namespace DPL::DB::ORM;
192         using namespace DPL::DB::ORM::wrt;
193
194         int featureUUID;
195         FeatureDeviceCapProxy::Select::RowList rows;
196         DeviceCapabilitySet result;
197
198         {
199             WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
200             select->Where(Equals<FeaturesList::FeatureName>(apifeature));
201             featureUUID = select->GetSingleValue<FeaturesList::FeatureUUID>();
202         }
203
204         {
205             WRT_DB_SELECT(select,
206                           FeatureDeviceCapProxy,
207                           &WrtDatabase::interface())
208             select->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
209                               featureUUID));
210             rows = select->GetRowList();
211         }
212
213         FOREACH(it, rows){
214             WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
215             select->Where(Equals<DeviceCapabilities::DeviceCapID>(
216                               it->Get_DeviceCapID()));
217             result.insert(select->
218                               GetSingleValue<DeviceCapabilities::DeviceCapName>());
219         }
220
221         return result;
222     } Catch(DPL::DB::SqlConnection::Exception::Base){
223         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
224                    "Failed to update roaming network data usage");
225     }
226 }
227
228 WidgetAccessInfoList GlobalDAOReadOnly::GetWhiteURIList()
229 {
230     LogDebug("Getting WhiteURIList.");
231     Try {
232         using namespace DPL::DB::ORM;
233         using namespace DPL::DB::ORM::wrt;
234         WRT_DB_SELECT(select, WidgetWhiteURIList, &WrtDatabase::interface())
235
236         WidgetAccessInfoList resultList;
237         typedef std::list<WidgetWhiteURIList::Row> RowList;
238         RowList list = select->GetRowList();
239
240         for (RowList::iterator i = list.begin(); i != list.end(); ++i) {
241             WidgetAccessInfo whiteURI;
242             whiteURI.strIRI = i->Get_uri();
243             whiteURI.bSubDomains = i->Get_subdomain_access();
244             resultList.push_back(whiteURI);
245             LogInfo("[uri] : " << whiteURI.strIRI <<
246                     ", [subdomain] : " << whiteURI.bSubDomains);
247         }
248         return resultList;
249     } Catch(DPL::DB::SqlConnection::Exception::Base) {
250         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
251                    "Failed to get whiteURI list");
252     }
253 }
254
255 bool GlobalDAOReadOnly::GetCookieSharingMode()
256 {
257     LogDebug("Getting Cookie Sharing mode");
258     Try {
259         using namespace DPL::DB::ORM;
260         using namespace DPL::DB::ORM::wrt;
261         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
262         return select->GetSingleValue<GlobalProperties::cookie_sharing_mode>();
263     }
264     Catch(DPL::DB::SqlConnection::Exception::Base){
265         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
266                    "Failed to get Cookie Sharing mode");
267     }
268 }
269 } // namespace WrtDB