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