[Release] wrt-installer_0.1.9
[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() :
32     m_dirpath(Jobs::WidgetInstall::createTempPath())
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(new WidgetLocation::DirectoryDeletor())
71 {
72     if (INSTALL_LOCATION_TYPE_PRELOAD == locationType) {
73         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
74     } else {
75         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
76     }
77 }
78
79 WidgetLocation::WidgetLocation(const std::string & widgetname,
80                                std::string sourcePath,
81                                std::string dirPath,
82                                WrtDB::PackagingType t,
83                                InstallLocationType locationType) :
84     m_pkgid(widgetname),
85     m_widgetSource(sourcePath),
86     m_type(t),
87     m_temp(new
88            WidgetLocation::DirectoryDeletor(dirPath))
89 {
90     if (INSTALL_LOCATION_TYPE_PRELOAD == locationType) {
91         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
92     } else {
93         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
94     }
95 }
96
97 // TODO cache all these paths
98 std::string WidgetLocation::getInstallationDir() const
99 {
100     return m_installedPath;
101 }
102
103 std::string WidgetLocation::getPackageInstallationDir() const
104 {
105     return m_installedPath + "/" + m_pkgid;
106 }
107
108 std::string WidgetLocation::getSourceDir() const
109 {
110     return m_installedPath + "/"
111            + m_pkgid + WrtDB::GlobalConfig::GetWidgetSrcPath();
112 }
113
114 std::string WidgetLocation::getBinaryDir() const
115 {
116     return m_installedPath + "/"
117            + m_pkgid + WrtDB::GlobalConfig::GetUserWidgetExecPath();
118 }
119
120 std::string WidgetLocation::getExecFile() const
121 {
122     return getBinaryDir() + "/" + m_appid;
123 }
124
125 std::string WidgetLocation::getBackupDir() const
126 {
127     return getPackageInstallationDir() + "/backup";
128 }
129
130 std::string WidgetLocation::getBackupSourceDir() const
131 {
132     return getBackupDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
133 }
134
135 std::string WidgetLocation::getBackupBinaryDir() const
136 {
137     return getBackupDir() + WrtDB::GlobalConfig::GetUserWidgetExecPath();
138 }
139
140 std::string WidgetLocation::getBackupExecFile() const
141 {
142     return getBackupBinaryDir() + "/" + m_appid;
143 }
144
145 std::string WidgetLocation::getUserDataRootDir() const
146 {
147     return std::string(WrtDB::GlobalConfig::GetWidgetUserDataPath()) +
148            "/" + m_pkgid;
149 }
150
151 std::string WidgetLocation::getPrivateStorageDir() const
152 {
153     return getUserDataRootDir() + "/" +
154            WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
155 }
156
157 std::string WidgetLocation::getTemporaryPackageDir() const
158 {
159     return m_temp->getTempPath();
160 }
161
162 std::string WidgetLocation::getTemporaryRootDir() const
163 {
164     if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
165         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
166     }
167     if (m_type == WrtDB::PKG_TYPE_HYBRID_WEB_APP) {
168         return getTemporaryPackageDir() + WrtDB::GlobalConfig::GetWidgetSrcPath();
169     } else {
170         return getTemporaryPackageDir();
171     }
172 }
173
174 std::string WidgetLocation::getConfigurationDir() const
175 {
176     if (m_type == WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
177         std::string path = ".";
178         std::size_t index = m_widgetSource.find_last_of("\\/");
179         if (index != std::string::npos) {
180             path = m_widgetSource.substr(0, index);
181         }
182         return path;
183     } else if (m_type == WrtDB::PKG_TYPE_DIRECTORY_WEB_APP) {
184         return getWidgetSource() + WrtDB::GlobalConfig::GetWidgetSrcPath();
185     } else {
186         return getTemporaryRootDir();
187     }
188 }
189
190 DPL::String WidgetLocation::getPkgId() const
191 {
192     return DPL::FromUTF8String(m_pkgid);
193 }
194
195 std::string WidgetLocation::getInstalledIconPath() const
196 {
197     return m_iconPath;
198 }
199
200 std::string WidgetLocation::getWidgetSource() const
201 {
202     return m_widgetSource;
203 }
204
205 void WidgetLocation::setIconTargetFilenameForLocale(const std::string & icon)
206 {
207     m_iconPath = icon;
208 }
209
210 void WidgetLocation::registerExternalLocation(const std::string & file)
211 {
212     m_externals.push_back(file);
213 }
214
215 WrtDB::ExternalLocationList WidgetLocation::listExternalLocations() const
216 {
217     return m_externals;
218 }
219
220 void WidgetLocation::registerAppid(const std::string & appid)
221 {
222     m_appid = appid;
223 }