Tizen 2.4.0 rev3 SDK Public Release
[framework/web/wearable/wrt-installer.git] / src / pkg-manager / pkgmgr_signal.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  * @author      Yunchan Cho (yunchan.cho@samsung.com)
18  * @version     0.1
19  * @brief
20  */
21
22 #include <dpl/lexical_cast.h>
23 #include <dpl/wrt-dao-ro/global_config.h>
24
25 #include <pkgmgr_installer.h>
26 #include <pkg-manager/pkgmgr_signal.h>
27 #include <installer_log.h>
28
29 #define BUF_SIZE 4096
30
31 namespace {
32 // package type sent in every signal
33 const char PKGMGR_WEBAPP_TYPE[] = "wgt";
34
35 // notification about opoeration start
36 const char PKGMGR_START_KEY[] = "start";
37
38 // value for new installation
39 const char PKGMGR_START_INSTALL[] = "install";
40
41 // value for update installation
42 const char PKGMGR_START_UPDATE[] = "update";
43
44 // value for uninstallation
45 const char PKGMGR_START_UNINSTALL[] = "uninstall";
46
47 // notification about progress of installation with percentage number
48 const char PKGMGR_PROGRESS_KEY[] = "install_percent";
49
50 // notification about icon path for installation frontend
51 const char PKGMGR_ICON_PATH[] = "icon_path";
52
53 // notification about error before end with given error code
54 // (currently, same as backend exit status)
55 const char PKGMGR_ERROR[] = "error";
56
57 // notification about end of installation with status
58 const char PKGMGR_END_KEY[] = "end";
59
60 // success value of end of installation
61 const char PKGMGR_END_SUCCESS[] = "ok";
62
63 // failure value of end of installation
64 const char PKGMGR_END_FAILURE[] = "fail";
65 }
66
67 namespace PackageManager {
68 PkgmgrSignal::PkgmgrSignal() :
69     m_initialized(false),
70     m_handle(NULL),
71     m_reqType(RequestType::UNSUPPORTED),
72     m_percent(0)
73 {}
74
75 PkgmgrSignal::~PkgmgrSignal()
76 {
77     deinitialize();
78 }
79
80 bool PkgmgrSignal::initialize(int argc, char* argv[])
81 {
82     if (m_handle) {
83         _D("Release already allocated pkgmgr handle");
84         pkgmgr_installer_free(m_handle);
85         m_handle = NULL;
86     }
87
88     m_handle = pkgmgr_installer_new();
89     if (!m_handle) {
90         _E("Fail to get pkgmgr installer handle");
91         return false;
92     }
93
94     // set information from pkgmgr
95     if (!pkgmgr_installer_receive_request(
96             m_handle, argc, argv))
97     {
98         auto pkgmgrtype = pkgmgr_installer_get_request_type(m_handle);
99         switch(pkgmgrtype)
100         {
101             case PKGMGR_REQ_INSTALL:
102                 m_reqType = RequestType::INSTALL;
103                 break;
104             case PKGMGR_REQ_UNINSTALL:
105                 m_reqType = RequestType::UNINSTALL;
106                 break;
107             case PKGMGR_REQ_REINSTALL:
108                 m_reqType = RequestType::REINSTALL;
109                 break;
110             default:
111                 m_reqType = RequestType::UNSUPPORTED;
112                 break;
113         }
114
115         if (m_reqType == RequestType::UNSUPPORTED)
116         {
117             _E("Fail to get request type of pkgmgr");
118             pkgmgr_installer_free(m_handle);
119             m_handle = NULL;
120             return false;
121         }
122         const char *callerId = pkgmgr_installer_get_caller_pkgid(m_handle);
123         if(callerId)
124             m_callerId = callerId;
125
126     } else {
127         _E("Fail to get information of pkgmgr's request");
128         pkgmgr_installer_free(m_handle);
129         m_handle = NULL;
130         return false;
131     }
132
133     m_type = PKGMGR_WEBAPP_TYPE;
134     m_initialized = true;
135     return true;
136 }
137
138 bool PkgmgrSignal::deinitialize()
139 {
140     if (!m_initialized) {
141         _E("PkgmgrSingal not yet intialized");
142         return false;
143     }
144
145     if (!m_recoveryFile.empty() && (0 != unlink(m_recoveryFile.c_str()))) {
146         _E("Failed to remove %s", m_recoveryFile.c_str());
147     }
148
149     if (m_handle) {
150         pkgmgr_installer_free(m_handle);
151     }
152
153     m_handle = NULL;
154     m_initialized = false;
155     return true;
156 }
157
158 bool PkgmgrSignal::setPkgname(const std::string& name)
159 {
160     if (!m_initialized) {
161         _E("PkgmgrSingal not yet intialized");
162         return false;
163     }
164
165     if (name.empty()) {
166         _E("name is empty");
167         return false;
168     }
169
170     m_pkgname = name;
171     _D("Success to set tizen package name: %s", m_pkgname.c_str());
172     setRecoveryFile();
173
174     return true;
175 }
176
177 void PkgmgrSignal::setRecoveryFile()
178 {
179     std::string filePath = WrtDB::GlobalConfig::GetTempInstallInfoPath();
180     filePath += "/" + m_pkgname;
181
182     m_recoveryFile = filePath;
183     _D("SetRecoveryFile... %s", filePath.c_str());
184     if (access(filePath.c_str(), F_OK) != 0) {
185         FILE *file = fopen(filePath.c_str(), "w");
186         if (file != NULL) {
187             fclose(file);
188         }
189     } else {
190         _D("Recovery File : %s is already exist", filePath.c_str());
191     }
192 }
193
194 bool PkgmgrSignal::startJob(Jobs::InstallationType type)
195 {
196     switch(type)
197     {
198         case Jobs::InstallationType::NewInstallation:
199             sendSignal(PKGMGR_START_KEY, PKGMGR_START_INSTALL);
200             break;
201         case Jobs::InstallationType::UpdateInstallation:
202             sendSignal(PKGMGR_START_KEY, PKGMGR_START_UPDATE);
203             break;
204         case Jobs::InstallationType::Uninstallation:
205             sendSignal(PKGMGR_START_KEY, PKGMGR_START_UNINSTALL);
206             break;
207         default:
208             _E("Trying to send unknown installation type to pkgmgr");
209             return false;
210     }
211     return true;
212 }
213
214 bool PkgmgrSignal::endJob(Jobs::Exceptions::Type type, const char* message)
215 {
216     if(type == Jobs::Exceptions::Type::Success)
217     {
218         return sendSignal(PKGMGR_END_KEY, PKGMGR_END_SUCCESS);
219     }
220     else
221     {
222         std::string ecode = DPL::lexical_cast<std::string>(type);
223         if (message != NULL) {
224             char buf[BUF_SIZE] = {'\0'};
225
226             snprintf(buf, BUF_SIZE - 1, "%s:%s", ecode.c_str(), message);
227             sendSignal(PKGMGR_ERROR, buf);
228         }
229         return sendSignal(PKGMGR_END_KEY, PKGMGR_END_FAILURE);
230     }
231 }
232
233 bool PkgmgrSignal::sendProgress(int percent)
234 {
235     if (m_percent == percent) {
236         return true;
237     }
238
239     m_percent = percent;
240     return sendSignal(PKGMGR_PROGRESS_KEY, DPL::lexical_cast<std::string>(percent));
241 }
242
243 bool PkgmgrSignal::sendIconPath(const std::string & iconpath)
244 {
245     return sendSignal(PKGMGR_ICON_PATH, iconpath);
246 }
247
248 bool PkgmgrSignal::sendSignal(const std::string& key,
249                               const std::string& value) const
250 {
251     if (!m_initialized) {
252         _E("PkgmgrSingal not yet intialized");
253         return false;
254     }
255
256     if (key.empty() || value.empty()) {
257         _D("key or value is empty");
258         return false;
259     }
260
261     if (m_handle == NULL || m_type.empty()) {
262         _E("Some data of PkgmgrSignal is empty");
263         return false;
264     }
265
266     // send pkgmgr signal
267     if (pkgmgr_installer_send_signal(
268             m_handle, m_type.c_str(), m_pkgname.c_str(),
269             key.c_str(), value.c_str()))
270     {
271         _E("Fail to send pkgmgr signal");
272         return false;
273     }
274
275     _D("Success to send pkgmgr signal: %s - %s", key.c_str(), value.c_str());
276     return true;
277 }
278
279 std::string PkgmgrSignal::getPkgname() const
280 {
281     if (!m_initialized) {
282         _E("PkgmgrSingal not yet intialized");
283     }
284
285     return m_pkgname;
286 }
287
288 PkgmgrSignal::RequestType PkgmgrSignal::getRequestedType() const
289 {
290     if (!m_initialized) {
291         _E("PkgmgrSingal not yet intialized");
292     }
293
294     return m_reqType;
295 }
296
297 std::string PkgmgrSignal::getCallerId() const
298 {
299     if (!m_initialized) {
300         _E("PkgmgrSingal not yet intialized");
301     }
302
303     return m_callerId;
304 }
305 } // PackageManager