Tizen 2.0 Release
[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; using namespace DPL::DB::ORM::wrt;
74         GlobalProperties::Row row;
75         row.Set_compliance_mode(mode);
76
77         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
78         update->Values(row);
79         update->Execute();
80     }
81     Catch (DPL::DB::SqlConnection::Exception::Base){
82         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
83                    "Failed to update compliance mode");
84     }
85 }
86
87 void GlobalDAO::setComplianceFakeImei(const std::string &imei)
88 {
89     LogDebug("Setting compliance fake IMEI: " << imei);
90     Try{
91         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
92         GlobalProperties::Row row;
93         row.Set_compliance_fake_imei(DPL::FromASCIIString(imei));
94
95         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
96         update->Values(row);
97         update->Execute();
98     }
99     Catch (DPL::DB::SqlConnection::Exception::Base){
100         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
101                    "Failed to update compliance fake IMEI");
102     }
103 }
104
105 void GlobalDAO::setComplianceFakeMeid(const std::string &meid)
106 {
107     LogDebug("Setting compliance fake MEID: " << meid);
108     Try{
109         using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
110         GlobalProperties::Row row;
111         row.Set_compliance_fake_meid(DPL::FromASCIIString(meid));
112
113         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
114         update->Values(row);
115         update->Execute();
116     }
117     Catch (DPL::DB::SqlConnection::Exception::Base){
118         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
119                    "Failed to update compliance fake MEID");
120     }
121 }
122
123 void GlobalDAO::SetHomeNetworkDataUsage(GlobalDAO::NetworkAccessMode newMode)
124 {
125     LogDebug("updating home network data usage to:" << newMode);
126     Try {
127         using namespace DPL::DB::ORM;
128         using namespace DPL::DB::ORM::wrt;
129         GlobalProperties::Row row;
130         row.Set_home_network_data_usage(static_cast<int>(newMode));
131
132         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
133         update->Values(row);
134         update->Execute();
135     }
136     Catch(DPL::DB::SqlConnection::Exception::Base){
137         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
138                    "Failed to update home network data usage");
139     }
140 }
141
142 void GlobalDAO::SetRoamingDataUsage(GlobalDAO::NetworkAccessMode newMode)
143 {
144     LogDebug("updating roaming network data usage to:" << newMode);
145     Try {
146         using namespace DPL::DB::ORM;
147         using namespace DPL::DB::ORM::wrt;
148         GlobalProperties::Row row;
149         row.Set_roaming_data_usage(static_cast<int>(newMode));
150
151         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
152         update->Values(row);
153         update->Execute();
154     }
155     Catch(DPL::DB::SqlConnection::Exception::Base){
156         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
157                    "Failed to update roaming network data usage");
158     }
159 }
160
161 void GlobalDAO::AddWhiteURI(const std::string &uri, bool subDomain)
162 {
163     LogDebug("Add White URI : " << uri);
164     Try {
165         using namespace DPL::DB::ORM;
166         using namespace DPL::DB::ORM::wrt;
167         WidgetWhiteURIList::Row row;
168         row.Set_uri(DPL::FromASCIIString(uri));
169         row.Set_subdomain_access(static_cast<int>(subDomain));
170         wrt::ScopedTransaction transaction(&WrtDatabase::interface());
171
172         WRT_DB_INSERT(insert, WidgetWhiteURIList, &WrtDatabase::interface())
173
174         insert->Values(row);
175         insert->Execute();
176         transaction.Commit();
177     }
178     Catch(DPL::DB::SqlConnection::Exception::Base){
179         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
180                    "Failed to add white URI");
181     }
182 }
183
184 void GlobalDAO::RemoveWhiteURI(const std::string &uri)
185 {
186     LogDebug("Remove White URI : " << uri);
187     Try {
188         using namespace DPL::DB::ORM;
189         using namespace DPL::DB::ORM::wrt;
190
191         WRT_DB_DELETE(deleteFrom, WidgetWhiteURIList, &WrtDatabase::interface())
192         deleteFrom->Where(Equals<WidgetWhiteURIList::uri>(DPL::FromASCIIString(uri)));
193         deleteFrom->Execute();
194     } Catch(DPL::DB::SqlConnection::Exception::Base) {
195         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
196                    "Failed to removed white URI from AdultBlackList");
197     }
198 }
199
200 void GlobalDAO::SetCookieSharingMode(bool mode)
201 {
202     LogDebug("updating Cookie Sharing mode to:" << mode);
203     Try {
204         using namespace DPL::DB::ORM;
205         using namespace DPL::DB::ORM::wrt;
206         GlobalProperties::Row row;
207         row.Set_cookie_sharing_mode(mode);
208
209         WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
210         update->Values(row);
211         update->Execute();
212     }
213     Catch(DPL::DB::SqlConnection::Exception::Base){
214         ReThrowMsg(GlobalDAO::Exception::DatabaseError,
215                    "Failed to update Cookie Sharing Mode");
216     }
217 }
218
219 } // namespace WrtDB