[Release] wrt-installer_0.1.25
[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
29 #include <widget_install/task_commons.h>
30
31 WidgetLocation::DirectoryDeletor::DirectoryDeletor(bool isPreload) :
32     m_dirpath(Jobs::WidgetInstall::createTempPath(isPreload))
33 {}
34
35 WidgetLocation::DirectoryDeletor::DirectoryDeletor(std::string tempPath) :
36         m_dirpath(tempPath)
37 {}
38
39 WidgetLocation::DirectoryDeletor::~DirectoryDeletor()
40 {
41     LogDebug(
42         "Removing widget installation temporary directory: " << m_dirpath.c_str());
43     if (!WrtUtilRemove(m_dirpath)) {
44         LogError("Fail at removing directory: " << m_dirpath.c_str());
45     }
46 }
47
48 std::string WidgetLocation::DirectoryDeletor::getTempPath() const
49 {
50     return m_dirpath;
51 }
52
53 WidgetLocation::WidgetLocation()
54 {}
55
56 WidgetLocation::WidgetLocation(const std::string & widgetname) :
57     m_pkgid(widgetname)
58 {}
59
60 WidgetLocation::~WidgetLocation()
61 {}
62
63 WidgetLocation::WidgetLocation(const std::string & widgetname,
64                                std::string sourcePath,
65                                WrtDB::PackagingType t,
66                                InstallLocationType locationType) :
67     m_pkgid(widgetname),
68     m_widgetSource(sourcePath),
69     m_type(t),
70     m_temp(
71         new WidgetLocation::DirectoryDeletor(
72             INSTALL_LOCATION_TYPE_PRELOAD == locationType))
73 {
74     if (INSTALL_LOCATION_TYPE_PRELOAD == locationType) {
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                                InstallLocationType locationType) :
90     m_pkgid(widgetname),
91     m_widgetSource(sourcePath),
92     m_type(t),
93     m_temp(new
94            WidgetLocation::DirectoryDeletor(dirPath))
95 {
96     if (INSTALL_LOCATION_TYPE_PRELOAD == locationType) {
97         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
98     } else {
99         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
100     }
101     // TODO clean-up
102     if (access(m_widgetSource.c_str(), F_OK) != 0) {
103         m_widgetSource = m_installedPath + "/" + m_pkgid;
104     }
105 }
106
107 // TODO cache all these paths
108 std::string WidgetLocation::getInstallationDir() const
109 {
110     return m_installedPath;
111 }
112
113 std::string WidgetLocation::getPackageInstallationDir() const
114 {
115     return m_installedPath + "/" + m_pkgid;
116 }
117
118 std::string WidgetLocation::getSourceDir() const
119 {
120     return m_installedPath + "/"
121            + m_pkgid + WrtDB::GlobalConfig::GetWidgetSrcPath();
122 }
123
124 std::string WidgetLocation::getBinaryDir() const
125 {
126     return m_installedPath + "/"
127            + m_pkgid + WrtDB::GlobalConfig::GetUserWidgetExecPath();
128 }
129
130 std::string WidgetLocation::getExecFile() const
131 {
132     return getBinaryDir() + "/" + m_appid;
133 }
134
135 std::string WidgetLocation::getBackupDir() const
136 {
137     return getPackageInstallationDir() + "/backup";
138 }
139
140 std::string WidgetLocation::getBackupSourceDir() const
141 {
142     return getBackupDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
143 }
144
145 std::string WidgetLocation::getBackupBinaryDir() const
146 {
147     return getBackupDir() + WrtDB::GlobalConfig::GetUserWidgetExecPath();
148 }
149
150 std::string WidgetLocation::getBackupExecFile() const
151 {
152     return getBackupBinaryDir() + "/" + m_appid;
153 }
154
155 std::string WidgetLocation::getUserDataRootDir() const
156 {
157     return std::string(WrtDB::GlobalConfig::GetWidgetUserDataPath()) +
158            "/" + m_pkgid;
159 }
160
161 std::string WidgetLocation::getPrivateStorageDir() const
162 {
163     return getUserDataRootDir() + "/" +
164            WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
165 }
166
167 std::string WidgetLocation::getTemporaryPackageDir() const
168 {
169     return m_temp->getTempPath();
170 }
171
172 std::string WidgetLocation::getTemporaryRootDir() const
173 {
174     if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
175         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
176     }
177     if (m_type == WrtDB::PKG_TYPE_HYBRID_WEB_APP) {
178         return getTemporaryPackageDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
179     } else {
180         return getTemporaryPackageDir();
181     }
182 }
183
184 std::string WidgetLocation::getConfigurationDir() const
185 {
186     if (m_type == WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
187         std::string path = ".";
188         std::size_t index = m_widgetSource.find_last_of("\\/");
189         if (index != std::string::npos) {
190             path = m_widgetSource.substr(0, index);
191         }
192         return path;
193     } else if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
194         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
195     } else {
196         return getTemporaryRootDir();
197     }
198 }
199
200 DPL::String WidgetLocation::getPkgId() const
201 {
202     return DPL::FromUTF8String(m_pkgid);
203 }
204
205 std::string WidgetLocation::getInstalledIconPath() const
206 {
207     return m_iconPath;
208 }
209
210 std::string WidgetLocation::getWidgetSource() const
211 {
212     return m_widgetSource;
213 }
214
215 void WidgetLocation::setIconTargetFilenameForLocale(const std::string & icon)
216 {
217     m_iconPath = icon;
218 }
219
220 void WidgetLocation::registerExternalLocation(const std::string & file)
221 {
222     m_externals.push_back(file);
223 }
224
225 WrtDB::ExternalLocationList WidgetLocation::listExternalLocations() const
226 {
227     return m_externals;
228 }
229
230 void WidgetLocation::registerAppid(const std::string & appid)
231 {
232     m_appid = appid;
233 }