merge with master
[platform/framework/web/wrt-commons.git] / modules / widget_dao / dao / global_dao.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.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-rw/global_dao.h>
24 #include <dpl/log/log.h>
25 #include <dpl/string.h>
26 #include <dpl/db/orm.h>
27 #include <orm_generator_wrt.h>
28 #include <dpl/wrt-dao-ro/webruntime_database.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
30
31 namespace WrtDB {
32 void GlobalDAO::SetDeveloperMode(bool mode)
33 {
34     LogDebug("updating Developer mode to:" << mode);
35     Try {
36         using namespace DPL::DB::ORM;
37         using namespace DPL::DB::ORM::wrt;
38         GlobalProperties::Row row;
39         row.Set_developer_mode(mode);
40
41         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
42         update->Values(row);
43         update->Execute();
44     }
45     Catch(DPL::DB::SqlConnection::Exception::Base){
46         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
47                    "Failed to update Developer Mode");
48     }
49 }
50
51 void GlobalDAO::SetSecureByDefault(bool secure)
52 {
53     //If secure == true -> widget does not need to be signed
54     Try {
55         using namespace DPL::DB::ORM;
56         using namespace DPL::DB::ORM::wrt;
57         GlobalProperties::Row row;
58         row.Set_secure_by_default(secure);
59         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
60         update->Values(row);
61         update->Execute();
62     }
63     Catch(DPL::DB::SqlConnection::Exception::Base) {
64         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
65                    "Failed to update secureByDefault");
66     }
67 }
68
69 void GlobalDAO::setComplianceMode(bool mode)
70 {
71     LogDebug("Updating compliance mode to:" << mode);
72     Try {
73         using namespace DPL::DB::ORM;
74         using namespace DPL::DB::ORM::wrt;
75         GlobalProperties::Row row;
76         row.Set_compliance_mode(mode);
77
78         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
79         update->Values(row);
80         update->Execute();
81     }
82     Catch(DPL::DB::SqlConnection::Exception::Base){
83         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
84                    "Failed to update compliance mode");
85     }
86 }
87
88 void GlobalDAO::setComplianceFakeImei(const std::string &imei)
89 {
90     LogDebug("Setting compliance fake IMEI: " << imei);
91     Try {
92         using namespace DPL::DB::ORM;
93         using namespace DPL::DB::ORM::wrt;
94         GlobalProperties::Row row;
95         row.Set_compliance_fake_imei(DPL::FromASCIIString(imei));
96
97         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
98         update->Values(row);
99         update->Execute();
100     }
101     Catch(DPL::DB::SqlConnection::Exception::Base){
102         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
103                    "Failed to update compliance fake IMEI");
104     }
105 }
106
107 void GlobalDAO::setComplianceFakeMeid(const std::string &meid)
108 {
109     LogDebug("Setting compliance fake MEID: " << meid);
110     Try {
111         using namespace DPL::DB::ORM;
112         using namespace DPL::DB::ORM::wrt;
113         GlobalProperties::Row row;
114         row.Set_compliance_fake_meid(DPL::FromASCIIString(meid));
115
116         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
117         update->Values(row);
118         update->Execute();
119     }
120     Catch(DPL::DB::SqlConnection::Exception::Base){
121         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
122                    "Failed to update compliance fake MEID");
123     }
124 }
125
126 void GlobalDAO::SetHomeNetworkDataUsage(GlobalDAO::NetworkAccessMode newMode)
127 {
128     LogDebug("updating home network data usage to:" << newMode);
129     Try {
130         using namespace DPL::DB::ORM;
131         using namespace DPL::DB::ORM::wrt;
132         GlobalProperties::Row row;
133         row.Set_home_network_data_usage(static_cast<int>(newMode));
134
135         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
136         update->Values(row);
137         update->Execute();
138     }
139     Catch(DPL::DB::SqlConnection::Exception::Base){
140         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
141                    "Failed to update home network data usage");
142     }
143 }
144
145 void GlobalDAO::SetRoamingDataUsage(GlobalDAO::NetworkAccessMode newMode)
146 {
147     LogDebug("updating roaming network data usage to:" << newMode);
148     Try {
149         using namespace DPL::DB::ORM;
150         using namespace DPL::DB::ORM::wrt;
151         GlobalProperties::Row row;
152         row.Set_roaming_data_usage(static_cast<int>(newMode));
153
154         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
155         update->Values(row);
156         update->Execute();
157     }
158     Catch(DPL::DB::SqlConnection::Exception::Base){
159         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
160                    "Failed to update roaming network data usage");
161     }
162 }
163
164 void GlobalDAO::AddWhiteURI(const std::string &uri, bool subDomain)
165 {
166     LogDebug("Add White URI : " << uri);
167     Try {
168         using namespace DPL::DB::ORM;
169         using namespace DPL::DB::ORM::wrt;
170         WidgetWhiteURIList::Row row;
171         row.Set_uri(DPL::FromASCIIString(uri));
172         row.Set_subdomain_access(static_cast<int>(subDomain));
173         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
174
175         WRT_DB_INSERT(insert, WidgetWhiteURIList, &WrtDatabase::interface())
176
177         insert->Values(row);
178         insert->Execute();
179         transaction.Commit();
180     }
181     Catch(DPL::DB::SqlConnection::Exception::Base){
182         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
183                    "Failed to add white URI");
184     }
185 }
186
187 void GlobalDAO::RemoveWhiteURI(const std::string &uri)
188 {
189     LogDebug("Remove White URI : " << uri);
190     Try {
191         using namespace DPL::DB::ORM;
192         using namespace DPL::DB::ORM::wrt;
193
194         WRT_DB_DELETE(deleteFrom, WidgetWhiteURIList, &WrtDatabase::interface())
195         deleteFrom->Where(Equals<WidgetWhiteURIList::uri>(DPL::FromASCIIString(
196                                                               uri)));
197         deleteFrom->Execute();
198     } Catch(DPL::DB::SqlConnection::Exception::Base) {
199         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
200                    "Failed to removed white URI from AdultBlackList");
201     }
202 }
203
204 void GlobalDAO::SetCookieSharingMode(bool mode)
205 {
206     LogDebug("updating Cookie Sharing mode to:" << mode);
207     Try {
208         using namespace DPL::DB::ORM;
209         using namespace DPL::DB::ORM::wrt;
210         GlobalProperties::Row row;
211         row.Set_cookie_sharing_mode(mode);
212
213         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
214         update->Values(row);
215         update->Execute();
216     }
217     Catch(DPL::DB::SqlConnection::Exception::Base){
218         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
219                    "Failed to update Cookie Sharing Mode");
220     }
221 }
222 } // namespace WrtDB