[Engine] Replace widget handle with tizen id in widgetModel
[platform/framework/web/wrt.git] / src / domain / widget_model.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    widget_model.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for widget model
21  */
22 #include <widget_model.h>
23 #include <dpl/sstream.h>
24 #include <dpl/event/model_bind_to_dao.h>
25 #include <dpl/wrt-dao-ro/global_config.h>
26 #include <dpl/wrt-dao-ro/widget_config.h>
27 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
28
29 #include <dpl/utils/folder_size.h>
30
31 using namespace WrtDB;
32
33 template <typename RetType, RetType (WidgetDAOReadOnly::*extFun)() const >
34 struct BindToWidgetDAO :
35     DPL::Event::BindToDAO<WidgetModel,
36                           RetType,
37                           DPL::String,
38                           WidgetDAOReadOnly,
39                           &WidgetModel::getTizenId,
40                           extFun>
41 {
42 };
43
44 template <typename RetType, RetType(*extFun) (DPL::String)>
45 struct BindToWidgetDAOStatic :
46     DPL::Event::BindToDAO_Static<WidgetModel,
47                                  RetType,
48                                  DPL::String,
49                                  &WidgetModel::getTizenId,
50                                  extFun>
51 {
52 };
53
54 WidgetModel::WidgetModel(const std::string &tizenId) :
55     TizenId(DPL::FromASCIIString(tizenId)),
56     Handle(this, WidgetDAOReadOnly::getHandle(DPL::FromASCIIString(tizenId))),
57     Type(this, &BindToWidgetDAO<WidgetType,
58          &WidgetDAOReadOnly::getWidgetType>::Get),
59     ActualSize(this),
60     PreferredSize(this,
61                   &BindToWidgetDAO<WidgetSize,
62                                    &WidgetDAOReadOnly::getPreferredSize>::Get),
63     StartURL(this),
64     //localized, so not binded
65     StartFileInfo(this),
66     //localized, so not binded
67     PrefixURL(this, L"file://"),
68     InstallPath(
69         this,
70         &BindToWidgetDAO<DPL::String, &WidgetDAOReadOnly::getFullPath>::Get),
71     PersistentStoragePath(this,
72                           DPL::FromUTF8String(
73                               WidgetConfig::GetWidgetPersistentStoragePath(
74                                   this->TizenId))
75                           ),
76     // use of multiple widget model
77     TemporaryStoragePath(this,
78                          DPL::FromUTF8String(
79                              WidgetConfig::GetWidgetTemporaryStoragePath(
80                                  this->TizenId + getTimestamp()))
81                          ),
82     GUID(
83         this,
84         &BindToWidgetDAO<WidgetGUID, &WidgetDAOReadOnly::getGUID>::Get),
85     defaultlocale(
86         this,
87         &BindToWidgetDAO<DPL::OptionalString,
88                          &WidgetDAOReadOnly::getDefaultlocale>::Get),
89     Version(this, &WidgetModel::readVersion),
90     Name(this),
91     //localized, so not binded
92     ShortName(this),
93     //localized, so not binded
94     Description(this),
95     //localized, so not binded
96     License(this),
97     //localized, so not binded
98     LicenseHref(this),
99     //localized, so not binded
100     Icon(this),
101     SplashImg(
102         this,
103         &BindToWidgetDAO<DPL::OptionalString,
104                          &WidgetDAOReadOnly::getSplashImgSrc>::Get),
105     WindowModes(
106         this,
107         &BindToWidgetDAO<WindowModeList,
108                          &WidgetDAOReadOnly::getWindowModes>::Get),
109     //localized, so not binded
110     Standard(this, &WidgetModel::readStandard),
111     ApplicationStorage(this, &WidgetModel::getApplicationStorage),
112 //    AccessNetwork(this, false),
113 //    WarpDefinitionEmpty(this),
114     BackSupported(
115         this,
116         //TODO this type has to be here now, as Property constructor is wrongly
117         //chosen
118         (DPL::Event::Property<bool,
119                        DPL::Event::PropertyReadOnly,
120                        DPL::Event::PropertyStorageDynamicCached>::ReadDelegateType) &
121         BindToWidgetDAO<bool, &WidgetDAOReadOnly::getBackSupported>::Get),
122     AccessList(this),
123     IsTestWidget(
124         this,
125         //TODO this type has to be here now, as Property constructor is wrongly
126         //chosen
127         (DPL::Event::Property<bool,
128                        DPL::Event::PropertyReadOnly,
129                        DPL::Event::PropertyStorageDynamicCached>::ReadDelegateType) &
130         BindToWidgetDAO<bool, &WidgetDAOReadOnly::isTestWidget>::Get),
131
132     SettingList(this),
133     AppServiceList(this)
134 {
135 }
136
137 DPL::String WidgetModel::getTizenId() const
138 {
139     return TizenId;
140 }
141
142 WidgetStorageSize WidgetModel::getApplicationStorage(DPL::Event::Model *model)
143 {
144     Assert(model);
145     WidgetModel *widgetModel = static_cast<WidgetModel *>(model);
146     WidgetDAOReadOnly widgetDao(widgetModel->TizenId);
147
148     const DPL::String installPath = widgetDao.getPath();
149     const DPL::String persistentPath =
150         widgetModel->PersistentStoragePath.Get();
151     const DPL::String temporaryPath =
152         widgetModel->TemporaryStoragePath.Get();
153
154     size_t appSize =
155         Utils::getFolderSize(DPL::ToUTF8String(installPath));
156
157     auto isPrefix =
158         [&installPath](const DPL::String& possiblePrefix)
159         {
160             return possiblePrefix.length() <= installPath.length() &&
161                 std::equal(possiblePrefix.begin(),
162                         possiblePrefix.end(),
163                         installPath.begin());
164         };
165
166     size_t persistanceSize =
167         Utils::getFolderSize(DPL::ToUTF8String(persistentPath));
168     // we have to remember that persistance path can be inside install path
169     if (isPrefix(persistentPath)) {
170         appSize -= persistanceSize;
171     }
172
173     size_t temporarySize =
174         Utils::getFolderSize(DPL::ToUTF8String(temporaryPath));
175     // we have to remember that persistance path can be inside install path
176     if (isPrefix(temporaryPath)) {
177         appSize -= temporarySize;
178     }
179
180     LogDebug("App size: " << appSize);
181     LogDebug("Persistance size: " << persistanceSize);
182     LogDebug("Temporary size: " << temporaryPath);
183     return WidgetStorageSize(appSize, persistanceSize + temporarySize);
184 }
185
186 namespace { //anonymous
187 const float FLOAT_WAC20_REPRESENTATION = 2.0;
188 } //namespace anonymous
189
190 WidgetStandard WidgetModel::readStandard(DPL::Event::Model *model)
191 {
192     WidgetModel *widgetModel = static_cast<WidgetModel *>(model);
193
194     DPL::OptionalString minWacVersionString =
195         WidgetDAOReadOnly(widgetModel->TizenId).getMinimumWacVersion();
196
197     if (!minWacVersionString) {
198         return WidgetStandard_Unknown;
199     } else {
200         float minWacVersion;
201         std::stringstream minWacVersionStream(DPL::ToUTF8String(
202                                                   *minWacVersionString));
203         minWacVersionStream >> minWacVersion;
204         if (minWacVersionStream.fail()) {
205             return WidgetStandard_Unknown;
206         } else if (minWacVersion == FLOAT_WAC20_REPRESENTATION) {
207             return WidgetStandard_Wac20;
208         } else {
209             return WidgetStandard_Unknown;
210         }
211     }
212 }
213
214 OptionalWidgetVersion WidgetModel::readVersion(DPL::Event::Model *model)
215 {
216     WidgetModel *widgetModel = static_cast<WidgetModel *>(model);
217
218     DPL::OptionalString optionalVersionString =
219         WidgetDAOReadOnly(widgetModel->TizenId).getVersion();
220
221     if (!optionalVersionString) {
222         return OptionalWidgetVersion();
223     } else {
224         return OptionalWidgetVersion(WidgetVersion(*optionalVersionString));
225     }
226 }
227
228 DPL::String WidgetModel::getTimestamp()
229 {
230     struct timeval tv;
231     char buff[128];
232
233     gettimeofday(&tv, NULL);
234     sprintf(buff, "%lf", (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f);
235     LogInfo("timestamp: " << buff);
236     DPL::String timeString = DPL::FromUTF8String(std::string(buff));
237     return timeString;
238 }