71f8b2840d20abf22fe1cfc4fcfc2ae2ffe7a3d8
[platform/framework/web/wrt-installer.git] / src / misc / widget_location.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_location.cpp
18  * @author      Iwanek Tomasz (t.iwanek@smasung.com)
19  */
20 #include "widget_location.h"
21
22 #include <dpl/utils/wrt_utility.h>
23 #include <dpl/wrt-dao-ro/global_config.h>
24 #include <dpl/assert.h>
25 #include <dpl/log/log.h>
26 #include <dpl/sstream.h>
27 #include <dpl/localization/localization_utils.h>
28 #include <widget_install/task_commons.h>
29
30 WidgetLocation::DirectoryDeletor::DirectoryDeletor(bool isReadOnly) :
31     m_dirpath(Jobs::WidgetInstall::createTempPath(isReadOnly))
32 {}
33
34 WidgetLocation::DirectoryDeletor::DirectoryDeletor(std::string tempPath) :
35         m_dirpath(tempPath)
36 {}
37
38 WidgetLocation::DirectoryDeletor::~DirectoryDeletor()
39 {
40     LogDebug(
41         "Removing widget installation temporary directory: " << m_dirpath.c_str());
42     if (!WrtUtilRemove(m_dirpath)) {
43         LogWarning("Fail at removing directory: " << m_dirpath.c_str());
44     }
45 }
46
47 std::string WidgetLocation::DirectoryDeletor::getTempPath() const
48 {
49     return m_dirpath;
50 }
51
52 WidgetLocation::WidgetLocation()
53 {}
54
55 WidgetLocation::WidgetLocation(const std::string & widgetname) :
56     m_pkgid(widgetname)
57 {}
58
59 WidgetLocation::~WidgetLocation()
60 {}
61
62 WidgetLocation::WidgetLocation(const std::string & widgetname,
63                                std::string sourcePath,
64                                WrtDB::PackagingType t,
65                                bool isReadonly,
66                                InstallMode::ExtensionType eType) :
67     m_pkgid(widgetname),
68     m_widgetSource(sourcePath),
69     m_type(t),
70     m_temp(
71         new WidgetLocation::DirectoryDeletor(isReadonly)),
72     m_extensionType(eType)
73 {
74     if (isReadonly) {
75         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
76     } else {
77         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
78     }
79     // TODO clean-up
80     if (access(m_widgetSource.c_str(), F_OK) != 0) {
81         m_widgetSource = m_installedPath + "/" + m_pkgid;
82     }
83 }
84
85 WidgetLocation::WidgetLocation(const std::string & widgetname,
86                                std::string sourcePath,
87                                std::string dirPath,
88                                WrtDB::PackagingType t,
89                                bool isReadonly,
90                                InstallMode::ExtensionType eType) :
91     m_pkgid(widgetname),
92     m_widgetSource(sourcePath),
93     m_type(t),
94     m_temp(new WidgetLocation::DirectoryDeletor(dirPath)),
95     m_extensionType(eType)
96 {
97     if (isReadonly) {
98         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
99     } else {
100         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
101     }
102     // TODO clean-up
103     if (access(m_widgetSource.c_str(), F_OK) != 0) {
104         m_widgetSource = m_installedPath + "/" + m_pkgid;
105     }
106 }
107
108 // TODO cache all these paths
109 std::string WidgetLocation::getInstallationDir() const
110 {
111     return m_installedPath;
112 }
113
114 std::string WidgetLocation::getPackageInstallationDir() const
115 {
116     return m_installedPath + "/" + m_pkgid;
117 }
118
119 std::string WidgetLocation::getSourceDir() const
120 {
121     return m_installedPath + "/"
122            + m_pkgid + WrtDB::GlobalConfig::GetWidgetSrcPath();
123 }
124
125 std::string WidgetLocation::getBinaryDir() const
126 {
127     return m_installedPath + "/"
128            + m_pkgid + WrtDB::GlobalConfig::GetUserWidgetExecPath();
129 }
130
131 std::string WidgetLocation::getUserBinaryDir() const
132 {
133     return getUserDataRootDir() + "/"
134            + WrtDB::GlobalConfig::GetUserWidgetExecPath();
135 }
136
137 std::string WidgetLocation::getExecFile() const
138 {
139     return getBinaryDir() + "/" + m_appid;
140 }
141
142 std::string WidgetLocation::getBackupDir() const
143 {
144     return getPackageInstallationDir() + ".backup";
145 }
146
147 std::string WidgetLocation::getBackupSourceDir() const
148 {
149     return getBackupDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
150 }
151
152 std::string WidgetLocation::getBackupBinaryDir() const
153 {
154     return getBackupDir() + WrtDB::GlobalConfig::GetUserWidgetExecPath();
155 }
156
157 std::string WidgetLocation::getBackupExecFile() const
158 {
159     return getBackupBinaryDir() + "/" + m_appid;
160 }
161
162 std::string WidgetLocation::getBackupPrivateDir() const
163 {
164     return getBackupDir() + "/" +
165         WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
166 }
167
168 std::string WidgetLocation::getUserDataRootDir() const
169 {
170     return std::string(WrtDB::GlobalConfig::GetWidgetUserDataPath()) +
171            "/" + m_pkgid;
172 }
173
174 std::string WidgetLocation::getPrivateStorageDir() const
175 {
176     return getUserDataRootDir() + "/" +
177            WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
178 }
179
180 std::string WidgetLocation::getPrivateTempStorageDir() const
181 {
182     return getUserDataRootDir() + "/" +
183            WrtDB::GlobalConfig::GetWidgetPrivateTempStoragePath();
184 }
185
186
187 std::string WidgetLocation::getTemporaryPackageDir() const
188 {
189     return m_temp->getTempPath();
190 }
191
192 std::string WidgetLocation::getTemporaryRootDir() const
193 {
194     if (m_extensionType == InstallMode::ExtensionType::DIR) {
195         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
196     }
197     if (m_type == WrtDB::PKG_TYPE_HYBRID_WEB_APP) {
198         return getTemporaryPackageDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
199     } else {
200         return getTemporaryPackageDir();
201     }
202 }
203
204 std::string WidgetLocation::getConfigurationDir() const
205 {
206     if (m_type == WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
207         std::string path = ".";
208         std::size_t index = m_widgetSource.find_last_of("\\/");
209         if (index != std::string::npos) {
210             path = m_widgetSource.substr(0, index);
211         }
212         return path;
213     } else {
214         return getTemporaryRootDir();
215     }
216 }
217
218 DPL::String WidgetLocation::getPkgId() const
219 {
220     return DPL::FromUTF8String(m_pkgid);
221 }
222
223 std::string WidgetLocation::getInstalledIconPath() const
224 {
225     return m_iconPath;
226 }
227
228 std::string WidgetLocation::getWidgetSource() const
229 {
230     return m_widgetSource;
231 }
232
233 void WidgetLocation::setIconTargetFilenameForLocale(const std::string & icon)
234 {
235     m_iconPath = icon;
236 }
237
238 void WidgetLocation::registerExternalLocation(const std::string & file)
239 {
240     m_externals.push_back(file);
241 }
242
243 WrtDB::ExternalLocationList WidgetLocation::listExternalLocations() const
244 {
245     return m_externals;
246 }
247
248 void WidgetLocation::registerAppid(const std::string & appid)
249 {
250     m_appid = appid;
251 }
252
253 std::string WidgetLocation::getSharedRootDir() const
254 {
255     /* TODO : add wrt-commons*/
256     return getUserDataRootDir() + "/shared";
257 }
258
259 std::string WidgetLocation::getSharedResourceDir() const
260 {
261     return getSharedRootDir() + "/res";
262 }
263
264 std::string WidgetLocation::getSharedDataDir() const
265 {
266     return getSharedRootDir() + "/data";
267 }
268
269 std::string WidgetLocation::getSharedTrustedDir() const
270 {
271     return getSharedRootDir() + "/trusted";
272 }
273
274 std::string WidgetLocation::getBackupSharedDir() const
275 {
276     return getBackupDir() + "/shared";
277 }