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