arrangement of directory creation.
[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                                bool preload):
72                                     m_pkgname(widgetname),
73                                     m_widgetSource(sourcePath),
74                                     m_type(t),
75                                     m_temp(new WidgetLocation::DirectoryDeletor())
76 {
77     if (preload) {
78         m_installedPath += WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
79     } else {
80         m_installedPath += WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
81     }
82 }
83
84 WidgetLocation::WidgetLocation(const std::string & widgetname,
85                                std::string sourcePath,
86                                std::string dirPath,
87                                WrtDB::PackagingType t,
88                                bool preload):
89                                     m_pkgname(widgetname),
90                                     m_widgetSource(sourcePath),
91                                     m_type(t),
92                                     m_temp(new
93                                             WidgetLocation::DirectoryDeletor(dirPath))
94 {
95     if (preload) {
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::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();
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
195     {
196         return getTemporaryRootDir();
197     }
198 }
199
200 DPL::String WidgetLocation::getPkgname() const
201 {
202     return DPL::FromUTF8String(m_pkgname);
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 }