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