Tizen 2.0 Release
[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
36 bool GlobalDAOReadOnly::GetDeveloperMode()
37 {
38     LogDebug("Getting Developer mode");
39     Try {
40         using namespace DPL::DB::ORM;
41         using namespace DPL::DB::ORM::wrt;
42         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
43         return select->GetSingleValue<GlobalProperties::developer_mode>();
44     }
45     Catch(DPL::DB::SqlConnection::Exception::Base){
46         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
47                    "Failed to get developer mode");
48     }
49 }
50
51 bool GlobalDAOReadOnly::GetSecureByDefault()
52 {
53     using namespace DPL::DB::ORM;
54     using namespace DPL::DB::ORM::wrt;
55     WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
56     return select->GetSingleValue<GlobalProperties::secure_by_default>();
57 }
58
59 bool GlobalDAOReadOnly::getComplianceMode()
60 {
61     LogDebug("Getting compliance mode");
62     Try{
63         using namespace DPL::DB::ORM; 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; using namespace DPL::DB::ORM::wrt;
78         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
79         DPL::Optional<DPL::String> result =
80             select->GetSingleValue<GlobalProperties::compliance_fake_imei>();
81         if (!result) {
82             return std::string();
83         }
84         return DPL::ToUTF8String(*result);
85     }
86     Catch (DPL::DB::SqlConnection::Exception::Base){
87         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
88                    "Failed to get compliance fake IMEI");
89     }
90 }
91
92 std::string GlobalDAOReadOnly::getComplianceFakeMeid()
93 {
94     LogDebug("Getting compliance fake MEID");
95     Try{
96         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
97         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
98         DPL::Optional<DPL::String> result =
99             select->GetSingleValue<GlobalProperties::compliance_fake_meid>();
100         if (!result) {
101             return std::string();
102         }
103         return DPL::ToUTF8String(*result);
104     }
105     Catch (DPL::DB::SqlConnection::Exception::Base){
106         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
107                    "Failed to get compliance fake MEID");
108     }
109 }
110
111 bool GlobalDAOReadOnly::IsValidSubTag(const DPL::String& tag, int type)
112 {
113     using namespace DPL::DB::ORM;
114     using namespace DPL::DB::ORM::wrt;
115     WRT_DB_SELECT(select, iana_records, &WrtDatabase::interface())
116     select->Where(Equals<iana_records::SUBTAG>(tag));
117     auto row = select->GetRowList();
118     if (row.size() == 0 || row.front().Get_TYPE() != type) {
119         return false;
120     } else {
121         return true;
122     }
123 }
124
125 GlobalDAOReadOnly::NetworkAccessMode
126         GlobalDAOReadOnly::GetHomeNetworkDataUsage()
127 {
128     LogDebug("Getting home network data usage");
129     Try {
130         using namespace DPL::DB::ORM;
131         using namespace DPL::DB::ORM::wrt;
132         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
133         return static_cast<GlobalDAOReadOnly::NetworkAccessMode>(
134             select->GetSingleValue<
135             GlobalProperties::home_network_data_usage>());
136     }
137     Catch(DPL::DB::SqlConnection::Exception::Base){
138         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
139                    "Failed to get home network data usage");
140     }
141 }
142
143 GlobalDAOReadOnly::NetworkAccessMode GlobalDAOReadOnly::GetRoamingDataUsage()
144 {
145     LogDebug("Getting roaming network data usage");
146     Try {
147         using namespace DPL::DB::ORM;
148         using namespace DPL::DB::ORM::wrt;
149         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
150         return static_cast<GlobalDAOReadOnly::NetworkAccessMode>(
151             select->GetSingleValue<GlobalProperties::roaming_data_usage>());
152     }
153     Catch(DPL::DB::SqlConnection::Exception::Base){
154         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
155                    "Failed to get roaming network data usage");
156     }
157 }
158
159 //user agent strings are stored in db...
160 //and it is configurable for test in development.
161 DPL::String GlobalDAOReadOnly::GetUserAgentValue(const DPL::String &key)
162 {
163     LogDebug("Get User Agent Value : " << key);
164     Try {
165         using namespace DPL::DB::ORM;
166         using namespace DPL::DB::ORM::wrt;
167         WRT_DB_SELECT(select, UserAgents, &WrtDatabase::interface())
168         select->Where(Equals<UserAgents::key_name>(key));
169         DPL::Optional<DPL::String> agent =
170             select->GetSingleValue<UserAgents::key_value>();
171         if (agent.IsNull()) {
172             return DPL::FromUTF8String("");
173         } else {
174             return *agent;
175         }
176     }
177     Catch(DPL::DB::SqlConnection::Exception::Base){
178         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
179                    "Failed to get user agent string");
180     }
181 }
182
183 DeviceCapabilitySet GlobalDAOReadOnly::GetDeviceCapability(
184         const DPL::String &apifeature)
185 {
186     // This could be done with one simply sql query but support for join is
187     // needed in orm.
188     Try{
189         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
190
191         int featureUUID;
192         FeatureDeviceCapProxy::Select::RowList rows;
193         DeviceCapabilitySet result;
194
195         {
196             WRT_DB_SELECT(select, FeaturesList, &WrtDatabase::interface())
197             select->Where(Equals<FeaturesList::FeatureName>(apifeature));
198             featureUUID = select->GetSingleValue<FeaturesList::FeatureUUID>();
199         }
200
201         {
202             WRT_DB_SELECT(select, FeatureDeviceCapProxy, &WrtDatabase::interface())
203             select->Where(Equals<FeatureDeviceCapProxy::FeatureUUID>(
204                 featureUUID));
205             rows = select->GetRowList();
206         }
207
208         FOREACH(it, rows){
209             WRT_DB_SELECT(select, DeviceCapabilities, &WrtDatabase::interface())
210             select->Where(Equals<DeviceCapabilities::DeviceCapID>(
211                 it->Get_DeviceCapID()));
212             result.insert(select->
213                           GetSingleValue<DeviceCapabilities::DeviceCapName>());
214         }
215
216         return result;
217     }Catch(DPL::DB::SqlConnection::Exception::Base){
218         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
219                    "Failed to update roaming network data usage");
220     }
221 }
222
223 WidgetAccessInfoList GlobalDAOReadOnly::GetWhiteURIList()
224 {
225     LogDebug("Getting WhiteURIList.");
226     Try{
227         using namespace DPL::DB::ORM;
228         using namespace DPL::DB::ORM::wrt;
229         WRT_DB_SELECT(select, WidgetWhiteURIList, &WrtDatabase::interface())
230
231         WidgetAccessInfoList resultList;
232         typedef std::list<WidgetWhiteURIList::Row> RowList;
233         RowList list = select->GetRowList();
234
235         for (RowList::iterator i = list.begin(); i != list.end(); ++i) {
236             WidgetAccessInfo whiteURI;
237             whiteURI.strIRI = i->Get_uri();
238             whiteURI.bSubDomains = i->Get_subdomain_access();
239             resultList.push_back(whiteURI);
240             LogInfo("[uri] : " << whiteURI.strIRI <<
241                     ", [subdomain] : " << whiteURI.bSubDomains);
242         }
243         return resultList;
244     } Catch(DPL::DB::SqlConnection::Exception::Base) {
245         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
246                    "Failed to get whiteURI list");
247     }
248 }
249
250 bool GlobalDAOReadOnly::GetCookieSharingMode()
251 {
252     LogDebug("Getting Cookie Sharing mode");
253     Try {
254         using namespace DPL::DB::ORM;
255         using namespace DPL::DB::ORM::wrt;
256         WRT_DB_SELECT(select, GlobalProperties, &WrtDatabase::interface())
257         return select->GetSingleValue<GlobalProperties::cookie_sharing_mode>();
258     }
259     Catch(DPL::DB::SqlConnection::Exception::Base){
260         ReThrowMsg(GlobalDAOReadOnly::Exception::DatabaseError,
261                    "Failed to get Cookie Sharing mode");
262     }
263 }
264
265 } // namespace WrtDB