[Release] wrt-installer_0.1.54
[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     m_pkgid(widgetname),
67     m_widgetSource(sourcePath),
68     m_type(t),
69     m_temp(
70         new WidgetLocation::DirectoryDeletor(isReadonly))
71 {
72     if (isReadonly) {
73         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
74     } else {
75         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
76     }
77     // TODO clean-up
78     if (access(m_widgetSource.c_str(), F_OK) != 0) {
79         m_widgetSource = m_installedPath + "/" + m_pkgid;
80     }
81 }
82
83 WidgetLocation::WidgetLocation(const std::string & widgetname,
84                                std::string sourcePath,
85                                std::string dirPath,
86                                WrtDB::PackagingType t,
87                                bool isReadonly) :
88     m_pkgid(widgetname),
89     m_widgetSource(sourcePath),
90     m_type(t),
91     m_temp(new WidgetLocation::DirectoryDeletor(dirPath))
92 {
93     if (isReadonly) {
94         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
95     } else {
96         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
97     }
98     // TODO clean-up
99     if (access(m_widgetSource.c_str(), F_OK) != 0) {
100         m_widgetSource = m_installedPath + "/" + m_pkgid;
101     }
102 }
103
104 // TODO cache all these paths
105 std::string WidgetLocation::getInstallationDir() const
106 {
107     return m_installedPath;
108 }
109
110 std::string WidgetLocation::getPackageInstallationDir() const
111 {
112     return m_installedPath + "/" + m_pkgid;
113 }
114
115 std::string WidgetLocation::getSourceDir() const
116 {
117     return m_installedPath + "/"
118            + m_pkgid + WrtDB::GlobalConfig::GetWidgetSrcPath();
119 }
120
121 std::string WidgetLocation::getBinaryDir() const
122 {
123     return m_installedPath + "/"
124            + m_pkgid + WrtDB::GlobalConfig::GetUserWidgetExecPath();
125 }
126
127 std::string WidgetLocation::getUserBinaryDir() const
128 {
129     return getUserDataRootDir() + "/"
130            + WrtDB::GlobalConfig::GetUserWidgetExecPath();
131 }
132
133 std::string WidgetLocation::getExecFile() const
134 {
135     return getBinaryDir() + "/" + m_appid;
136 }
137
138 std::string WidgetLocation::getBackupDir() const
139 {
140     return getPackageInstallationDir() + ".backup";
141 }
142
143 std::string WidgetLocation::getBackupSourceDir() const
144 {
145     return getBackupDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
146 }
147
148 std::string WidgetLocation::getBackupBinaryDir() const
149 {
150     return getBackupDir() + WrtDB::GlobalConfig::GetUserWidgetExecPath();
151 }
152
153 std::string WidgetLocation::getBackupExecFile() const
154 {
155     return getBackupBinaryDir() + "/" + m_appid;
156 }
157
158 std::string WidgetLocation::getBackupPrivateDir() const
159 {
160     return getBackupDir() + "/" +
161         WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
162 }
163
164 std::string WidgetLocation::getUserDataRootDir() const
165 {
166     return std::string(WrtDB::GlobalConfig::GetWidgetUserDataPath()) +
167            "/" + m_pkgid;
168 }
169
170 std::string WidgetLocation::getPrivateStorageDir() const
171 {
172     return getUserDataRootDir() + "/" +
173            WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
174 }
175
176 std::string WidgetLocation::getTemporaryPackageDir() const
177 {
178     return m_temp->getTempPath();
179 }
180
181 std::string WidgetLocation::getTemporaryRootDir() const
182 {
183     if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
184         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
185     }
186     if (m_type == WrtDB::PKG_TYPE_HYBRID_WEB_APP) {
187         return getTemporaryPackageDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
188     } else {
189         return getTemporaryPackageDir();
190     }
191 }
192
193 std::string WidgetLocation::getConfigurationDir() const
194 {
195     if (m_type == WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
196         std::string path = ".";
197         std::size_t index = m_widgetSource.find_last_of("\\/");
198         if (index != std::string::npos) {
199             path = m_widgetSource.substr(0, index);
200         }
201         return path;
202     } else if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
203         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
204     } else {
205         return getTemporaryRootDir();
206     }
207 }
208
209 DPL::String WidgetLocation::getPkgId() const
210 {
211     return DPL::FromUTF8String(m_pkgid);
212 }
213
214 std::string WidgetLocation::getInstalledIconPath() const
215 {
216     return m_iconPath;
217 }
218
219 std::string WidgetLocation::getWidgetSource() const
220 {
221     return m_widgetSource;
222 }
223
224 void WidgetLocation::setIconTargetFilenameForLocale(const std::string & icon)
225 {
226     m_iconPath = icon;
227 }
228
229 void WidgetLocation::registerExternalLocation(const std::string & file)
230 {
231     m_externals.push_back(file);
232 }
233
234 WrtDB::ExternalLocationList WidgetLocation::listExternalLocations() const
235 {
236     return m_externals;
237 }
238
239 void WidgetLocation::registerAppid(const std::string & appid)
240 {
241     m_appid = appid;
242 }
243
244 std::string WidgetLocation::getSharedRootDir() const
245 {
246     /* TODO : add wrt-commons*/
247     return getUserDataRootDir() + "/shared";
248 }
249
250 std::string WidgetLocation::getSharedResourceDir() const
251 {
252     return getSharedRootDir() + "/res";
253 }
254
255 std::string WidgetLocation::getSharedDataDir() const
256 {
257     return getSharedRootDir() + "/data";
258 }
259
260 std::string WidgetLocation::getSharedTrustedDir() const
261 {
262     return getSharedRootDir() + "/trusted";
263 }
264
265 std::string WidgetLocation::getBackupSharedDir() const
266 {
267     return getBackupDir() + "/shared";
268 }