[Release] wrt-installer_0.0.89
[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_pkgname(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_pkgname(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_pkgname(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_pkgname;
111 }
112
113 std::string WidgetLocation::getSourceDir() const
114 {
115     return m_installedPath + "/"
116             + m_pkgname + WrtDB::GlobalConfig::GetWidgetSrcPath();
117 }
118
119 std::string WidgetLocation::getBinaryDir() const
120 {
121     return m_installedPath + "/"
122             + m_pkgname + WrtDB::GlobalConfig::GetUserWidgetExecPath();
123 }
124
125 std::string WidgetLocation::getExecFile() const
126 {
127     return getBinaryDir() + "/" + m_pkgname;
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_pkgname;
148 }
149
150 std::string WidgetLocation::getUserDataRootDir() const
151 {
152     return std::string(WrtDB::GlobalConfig::GetWidgetUserDataPath()) +
153         "/" + m_pkgname;
154 }
155
156 std::string WidgetLocation::getPrivateStorageDir() const
157 {
158     return getUserDataRootDir() + "/" +
159         WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
160 }
161
162 std::string WidgetLocation::getShareDir() const
163 {
164     return std::string(WrtDB::GlobalConfig::GetUserInstalledWidgetPath()) + "/"
165             + m_pkgname + "/" + WrtDB::GlobalConfig::GetWidgetSharePath();
166 }
167
168 std::string WidgetLocation::getTemporaryPackageDir() const
169 {
170     return m_temp->getTempPath();
171 }
172
173 std::string WidgetLocation::getTemporaryRootDir() const
174 {
175     if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
176         return getWidgetSource();
177     }
178     if(m_type == WrtDB::PKG_TYPE_HYBRID_WEB_APP)
179     {
180         return getTemporaryPackageDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
181     }
182     else
183     {
184         return getTemporaryPackageDir();
185     }
186 }
187
188 std::string WidgetLocation::getConfigurationDir() const
189 {
190     if(m_type == WrtDB::PKG_TYPE_HOSTED_WEB_APP)
191     {
192         std::string path = ".";
193         int index = m_widgetSource.find_last_of("\\/");
194         if (index != std::string::npos)
195         {
196             path = m_widgetSource.substr(0, index);
197         }
198         return path;
199     }
200     else if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
201     {
202         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
203     } else
204     {
205         return getTemporaryRootDir();
206     }
207 }
208
209 DPL::String WidgetLocation::getPkgname() const
210 {
211     return DPL::FromUTF8String(m_pkgname);
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 }