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