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