[Release] wrt-installer_0.1.28
[framework/web/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/log/log.h>
23 #include <pkgmgr_installer.h>
24 #include <pkg-manager/pkgmgr_signal.h>
25
26 namespace PackageManager {
27 PkgmgrSignal::PkgmgrSignal() :
28     m_initialized(false),
29     m_handle(NULL),
30     m_noPopup(false),
31     m_reqType(PKGMGR_REQ_INVALID)
32 {}
33
34 PkgmgrSignal::~PkgmgrSignal()
35 {}
36
37 bool PkgmgrSignal::initialize(int argc, char* argv[])
38 {
39     if (m_handle) {
40         LogInfo("Release already allocated pkgmgr handle");
41         pkgmgr_installer_free(m_handle);
42         m_handle = NULL;
43     }
44
45     m_handle = pkgmgr_installer_new();
46     if (!m_handle) {
47         LogError("Fail to get pkgmgr installer handle");
48         return false;
49     }
50
51     // set information from pkgmgr
52     if (!pkgmgr_installer_receive_request(
53             m_handle, argc, argv))
54     {
55         m_noPopup = pkgmgr_installer_is_quiet(m_handle);
56         m_reqType = pkgmgr_installer_get_request_type(m_handle);
57         if (m_reqType != PKGMGR_REQ_INSTALL &&
58             m_reqType != PKGMGR_REQ_UNINSTALL &&
59             m_reqType != PKGMGR_REQ_REINSTALL)
60         {
61             LogError("Fail to get request type of pkgmgr");
62             pkgmgr_installer_free(m_handle);
63             m_handle = NULL;
64             return false;
65         }
66     } else {
67         LogError("Fail to get information of pkgmgr's request");
68         pkgmgr_installer_free(m_handle);
69         m_handle = NULL;
70         return false;
71     }
72
73     m_type = PKGMGR_WEBAPP_TYPE;
74     m_initialized = true;
75     return true;
76 }
77
78 bool PkgmgrSignal::deinitialize()
79 {
80     if (!m_initialized) {
81         LogError("PkgmgrSingal not yet intialized");
82         return false;
83     }
84
85     pkgmgr_installer_free(m_handle);
86     m_handle = NULL;
87     m_initialized = false;
88     return true;
89 }
90
91 bool PkgmgrSignal::setPkgname(const std::string& name)
92 {
93     if (!m_initialized) {
94         LogError("PkgmgrSingal not yet intialized");
95         return false;
96     }
97
98     if (name.empty()) {
99         LogError("name is empty");
100         return false;
101     }
102
103     m_pkgname = name;
104     LogInfo("Success to set tizen package name: " << m_pkgname);
105
106     return true;
107 }
108
109 bool PkgmgrSignal::sendSignal(const std::string& key,
110                               const std::string& value) const
111 {
112     if (!m_initialized) {
113         LogError("PkgmgrSingal not yet intialized");
114         return false;
115     }
116
117     if (key.empty() || value.empty()) {
118         LogDebug("key or value is empty");
119         return false;
120     }
121
122     if (m_handle == NULL || m_type.empty() || m_pkgname.empty()) {
123         LogError("Some data of PkgmgrSignal is empty");
124         return false;
125     }
126
127     // send pkgmgr signal
128     if (pkgmgr_installer_send_signal(
129             m_handle, m_type.c_str(), m_pkgname.c_str(),
130             key.c_str(), value.c_str()))
131     {
132         LogError("Fail to send pkgmgr signal");
133         return false;
134     }
135
136     LogInfo("Success to send pkgmgr signal: " << key <<
137             " - " << value);
138     return true;
139 }
140
141 std::string PkgmgrSignal::getPkgname() const
142 {
143     if (!m_initialized) {
144         LogError("PkgmgrSingal not yet intialized");
145     }
146
147     return m_pkgname;
148 }
149
150 int PkgmgrSignal::getRequestedType() const
151 {
152     if (!m_initialized) {
153         LogError("PkgmgrSingal not yet intialized");
154     }
155
156     return m_reqType;
157 }
158
159 bool PkgmgrSignal::isNoPopupRequired() const
160 {
161     if (!m_initialized) {
162         LogError("PkgmgrSingal not yet intialized");
163     }
164
165     return m_noPopup;
166 }
167 } // PackageManager