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