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