5b29c8255f256a560893884cb3b7a90a32a6837f
[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                           WidgetHandle,
38                           WidgetDAOReadOnly,
39                           &WidgetModel::getHandle,
40                           extFun>
41 {
42 };
43
44 template <typename RetType, RetType(*extFun) (WidgetHandle)>
45 struct BindToWidgetDAOStatic :
46     DPL::Event::BindToDAO_Static<WidgetModel,
47                                  RetType,
48                                  WidgetHandle,
49                                  &WidgetModel::getHandle,
50                                  extFun>
51 {
52 };
53
54 WidgetModel::WidgetModel(const std::string &tizenId) :
55     Handle(this, WidgetDAOReadOnly::getHandle(DPL::FromASCIIString(tizenId))),
56     Type(this, &BindToWidgetDAO<WidgetType,
57          &WidgetDAOReadOnly::getWidgetType>::Get),
58     ActualSize(this),
59     PreferredSize(this,
60                   &BindToWidgetDAO<WidgetSize,
61                                    &WidgetDAOReadOnly::getPreferredSize>::Get),
62     StartURL(this),
63     //localized, so not binded
64     StartFileInfo(this),
65     //localized, so not binded
66     PrefixURL(this, L"file://"),
67     InstallPath(
68         this,
69         &BindToWidgetDAO<DPL::String, &WidgetDAOReadOnly::getFullPath>::Get),
70     PersistentStoragePath(this,
71                           DPL::FromUTF8String(
72                               WidgetConfig::GetWidgetPersistentStoragePath(
73                                   readTizenId(this)))
74                           ),
75     // use of multiple widget model
76     TemporaryStoragePath(this,
77                          DPL::FromUTF8String(
78                              WidgetConfig::GetWidgetTemporaryStoragePath(
79                                  readTizenId(this) + getTimestamp()))
80                          ),
81     GUID(
82         this,
83         &BindToWidgetDAO<WidgetGUID, &WidgetDAOReadOnly::getGUID>::Get),
84     defaultlocale(
85         this,
86         &BindToWidgetDAO<DPL::OptionalString,
87                          &WidgetDAOReadOnly::getDefaultlocale>::Get),
88     Version(this, &WidgetModel::readVersion),
89     Name(this),
90     //localized, so not binded
91     ShortName(this),
92     //localized, so not binded
93     Description(this),
94     //localized, so not binded
95     License(this),
96     //localized, so not binded
97     LicenseHref(this),
98     //localized, so not binded
99     Icon(this),
100     SplashImg(
101         this,
102         &BindToWidgetDAO<DPL::OptionalString,
103                          &WidgetDAOReadOnly::getSplashImgSrc>::Get),
104     WindowModes(
105         this,
106         &BindToWidgetDAO<WindowModeList,
107                          &WidgetDAOReadOnly::getWindowModes>::Get),
108     TizenId(this, &WidgetModel::readTizenId),
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 WidgetHandle WidgetModel::getHandle() const
138 {
139     return Handle.Get();
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->Handle.Get());
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 DPL::String WidgetModel::readTizenId(DPL::Event::Model *model)
187 {
188     WidgetModel *widgetModel = static_cast<WidgetModel *>(model);
189     DPL::OptionalString tizenId =
190         WidgetDAOReadOnly(widgetModel->Handle.Get()).getPkgname();
191
192     Assert(!!tizenId && "tizen id shouldn't be NULL");
193     DPL::String id = *tizenId;
194     return id;
195 }
196
197 namespace { //anonymous
198 const float FLOAT_WAC20_REPRESENTATION = 2.0;
199 } //namespace anonymous
200
201 WidgetStandard WidgetModel::readStandard(DPL::Event::Model *model)
202 {
203     WidgetModel *widgetModel = static_cast<WidgetModel *>(model);
204
205     DPL::OptionalString minWacVersionString =
206         WidgetDAOReadOnly(widgetModel->Handle.Get()).getMinimumWacVersion();
207
208     if (!minWacVersionString) {
209         return WidgetStandard_Unknown;
210     } else {
211         float minWacVersion;
212         std::stringstream minWacVersionStream(DPL::ToUTF8String(
213                                                   *minWacVersionString));
214         minWacVersionStream >> minWacVersion;
215         if (minWacVersionStream.fail()) {
216             return WidgetStandard_Unknown;
217         } else if (minWacVersion == FLOAT_WAC20_REPRESENTATION) {
218             return WidgetStandard_Wac20;
219         } else {
220             return WidgetStandard_Unknown;
221         }
222     }
223 }
224
225 OptionalWidgetVersion WidgetModel::readVersion(DPL::Event::Model *model)
226 {
227     WidgetModel *widgetModel = static_cast<WidgetModel *>(model);
228
229     DPL::OptionalString optionalVersionString =
230         WidgetDAOReadOnly(widgetModel->Handle.Get()).getVersion();
231
232     if (!optionalVersionString) {
233         return OptionalWidgetVersion();
234     } else {
235         return OptionalWidgetVersion(WidgetVersion(*optionalVersionString));
236     }
237 }
238
239 DPL::String WidgetModel::getTimestamp()
240 {
241     struct timeval tv;
242     char buff[128];
243
244     gettimeofday(&tv, NULL);
245     sprintf(buff, "%lf", (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f);
246     LogInfo("timestamp: " << buff);
247     DPL::String timeString = DPL::FromUTF8String(std::string(buff));
248     return timeString;
249 }