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