1 /*
2  * Copyright (C) 2014-2018 Red Hat, Inc.
3  * Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
4  *
5  * Licensed under the GNU Lesser General Public License Version 2.1
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * SECTION:dnf-advisory
24  * @short_description: Update advisory
25  * @include: libdnf.h
26  * @stability: Unstable
27  *
28  * This object represents a single update.
29  *
30  * See also: #DnfContext
31  */
32 
33 
34 #include "dnf-advisory-private.hpp"
35 #include "dnf-advisorypkg.h"
36 #include "dnf-advisoryref.h"
37 #include "sack/advisory.hpp"
38 #include "sack/advisoryref.hpp"
39 #include "sack/advisorypkg.hpp"
40 
41 
42 /**
43  * dnf_advisory_new:
44  *
45  * Creates a new #DnfAdvisory.
46  *
47  * Returns:(transfer full): a #DnfAdvisory
48  *
49  * Since: 0.7.0
50  **/
51 DnfAdvisory *
dnf_advisory_new(DnfSack * sack,Id a_id)52 dnf_advisory_new(DnfSack *sack, Id a_id)
53 {
54     return new libdnf::Advisory(sack, a_id);
55 }
56 
57 /**
58  * dnf_advisory_free:
59  *
60  * Destructor of #DnfAdvisory.
61  *
62  * Since: 0.13.0
63  **/
64 void
dnf_advisory_free(DnfAdvisory * advisory)65 dnf_advisory_free(DnfAdvisory *advisory)
66 {
67     delete advisory;
68 }
69 
70 /**
71  * dnf_advisory_compare:
72  * @left: a #DnfAdvisory instance.
73  * @right: another #DnfAdvisory instance.
74  *
75  * Compares two #DnfAdvisory objects for equality.
76  *
77  * Returns: %TRUE if they are the same
78  *
79  * Since: 0.7.0
80  */
81 int
dnf_advisory_compare(DnfAdvisory * left,DnfAdvisory * right)82 dnf_advisory_compare(DnfAdvisory *left, DnfAdvisory *right)
83 {
84     return left == right;
85 }
86 
87 /**
88  * dnf_advisory_get_title:
89  * @advisory: a #DnfAdvisory instance.
90  *
91  * Gets the title assigned to the advisory.
92  *
93  * Returns: a string value, or %NULL for unset
94  *
95  * Since: 0.7.0
96  */
97 const char *
dnf_advisory_get_title(DnfAdvisory * advisory)98 dnf_advisory_get_title(DnfAdvisory *advisory)
99 {
100     return advisory->getTitle();
101 }
102 
103 /**
104  * dnf_advisory_get_id:
105  * @advisory: a #DnfAdvisory instance.
106  *
107  * Gets the ID assigned to the advisory.
108  *
109  * Returns: a string value, or %NULL for unset
110  *
111  * Since: 0.7.0
112  */
113 const char *
dnf_advisory_get_id(DnfAdvisory * advisory)114 dnf_advisory_get_id(DnfAdvisory *advisory)
115 {
116     return advisory->getName();
117 }
118 
119 /**
120  * dnf_advisory_get_kind:
121  * @advisory: a #DnfAdvisory instance.
122  *
123  * Gets the advisory kind.
124  *
125  * Returns: a #DnfAdvisoryKind, e.g. %DNF_ADVISORY_KIND_BUGFIX
126  *
127  * Since: 0.7.0
128  */
129 DnfAdvisoryKind
dnf_advisory_get_kind(DnfAdvisory * advisory)130 dnf_advisory_get_kind(DnfAdvisory *advisory)
131 {
132     return advisory->getKind();
133 }
134 
135 /**
136  * dnf_advisory_get_description:
137  * @advisory: a #DnfAdvisory instance.
138  *
139  * Gets the advisory description.
140  *
141  * Returns: a string value, or %NULL for unset
142  *
143  * Since: 0.7.0
144  */
145 const char *
dnf_advisory_get_description(DnfAdvisory * advisory)146 dnf_advisory_get_description(DnfAdvisory *advisory)
147 {
148     return advisory->getDescription();
149 }
150 
151 /**
152  * dnf_advisory_get_rights:
153  * @advisory: a #DnfAdvisory instance.
154  *
155  * Gets the update rights for the advisory.
156  *
157  * Returns: a string value, or %NULL for unset
158  *
159  * Since: 0.7.0
160  */
161 const char *
dnf_advisory_get_rights(DnfAdvisory * advisory)162 dnf_advisory_get_rights(DnfAdvisory *advisory)
163 {
164     return advisory->getRights();
165 }
166 
167 /**
168  * dnf_advisory_get_severity:
169  * @advisory: a #DnfAdvisory instance.
170  *
171  * Gets the advisory severity.
172  *
173  * Returns: a string value, or %NULL for unset
174  *
175  * Since: 0.8.0
176  */
177 const char *
dnf_advisory_get_severity(DnfAdvisory * advisory)178 dnf_advisory_get_severity(DnfAdvisory *advisory)
179 {
180     return advisory->getSeverity();
181 }
182 
183 /**
184  * dnf_advisory_get_updated:
185  * @advisory: a #DnfAdvisory instance.
186  *
187  * Gets the timestamp when the advisory was created.
188  *
189  * Returns: %TRUE for success
190  *
191  * Since: 0.7.0
192  */
193 guint64
dnf_advisory_get_updated(DnfAdvisory * advisory)194 dnf_advisory_get_updated(DnfAdvisory *advisory)
195 {
196     return advisory->getUpdated();
197 }
198 
199 /**
200  * dnf_advisory_get_packages:
201  * @advisory: a #DnfAdvisory instance.
202  *
203  * Gets any packages referenced by the advisory.
204  *
205  * Returns: (transfer container) (element-type DnfAdvisoryPkg): a list of packages
206  *
207  * Since: 0.7.0
208  */
209 GPtrArray *
dnf_advisory_get_packages(DnfAdvisory * advisory)210 dnf_advisory_get_packages(DnfAdvisory *advisory)
211 {
212     std::vector<libdnf::AdvisoryPkg> pkgsvector;
213     advisory->getPackages(pkgsvector);
214 
215     GPtrArray *pkglist = g_ptr_array_new_with_free_func((GDestroyNotify) dnf_advisorypkg_free);
216     for (auto& advisorypkg : pkgsvector) {
217         g_ptr_array_add(pkglist, new libdnf::AdvisoryPkg(std::move(advisorypkg)));
218     }
219     return pkglist;
220 }
221 
222 /**
223  * dnf_advisory_get_references:
224  * @advisory: a #DnfAdvisory instance.
225  *
226  * Gets any references referenced by the advisory.
227  *
228  * Returns: (transfer container) (element-type DnfAdvisoryRef): a list of references
229  *
230  * Since: 0.7.0
231  */
232 GPtrArray *
dnf_advisory_get_references(DnfAdvisory * advisory)233 dnf_advisory_get_references(DnfAdvisory *advisory)
234 {
235     std::vector<libdnf::AdvisoryRef> refsvector;
236     advisory->getReferences(refsvector);
237 
238     GPtrArray *reflist = g_ptr_array_new_with_free_func((GDestroyNotify) dnf_advisoryref_free);
239     for (const auto& advisoryref : refsvector) {
240         g_ptr_array_add(reflist, new libdnf::AdvisoryRef(advisoryref));
241     }
242     return reflist;
243 }
244 
245 
246 /**
247  * dnf_advisory_match_id:
248  * @advisory: a #DnfAdvisory instance.
249  * @s: string
250  *
251  * Matches #DnfAdvisory id against string
252  *
253  * Returns: %TRUE if they are the same
254  *
255  * Since: 0.7.0
256  */
257 gboolean
dnf_advisory_match_id(DnfAdvisory * advisory,const char * s)258 dnf_advisory_match_id(DnfAdvisory *advisory, const char *s)
259 {
260     return advisory->matchName(s);
261 }
262 
263 /**
264  * dnf_advisory_match_kind:
265  * @advisory: a #DnfAdvisory instance.
266  * @s: string
267  *
268  * Matches #DnfAdvisory kind against string
269  *
270  * Returns: %TRUE if they are the same
271  *
272  * Since: 0.7.0
273  */
274 gboolean
dnf_advisory_match_kind(DnfAdvisory * advisory,const char * s)275 dnf_advisory_match_kind(DnfAdvisory *advisory, const char *s)
276 {
277     return advisory->matchKind(s);
278 }
279 
280 /**
281  * dnf_advisory_match_severity:
282  * @advisory: a #DnfAdvisory instance.
283  * @s: string
284  *
285  * Matches #DnfAdvisory severity against string
286  *
287  * Returns: %TRUE if they are the same
288  *
289  * Since: 0.8.0
290  */
291 gboolean
dnf_advisory_match_severity(DnfAdvisory * advisory,const char * s)292 dnf_advisory_match_severity(DnfAdvisory *advisory, const char *s)
293 {
294     return advisory->matchSeverity(s);
295 }
296 
297 /**
298  * dnf_advisory_match_cve:
299  * @advisory: a #DnfAdvisory instance.
300  * @s: string
301  *
302  * Matches if #DnfAdvisoryRef has a #DnfAdvisoryRef which is CVE
303  * and matches against string
304  *
305  * Returns: %TRUE if they are the same
306  *
307  * Since: 0.7.0
308  */
309 gboolean
dnf_advisory_match_cve(DnfAdvisory * advisory,const char * s)310 dnf_advisory_match_cve(DnfAdvisory *advisory, const char *s)
311 {
312     return advisory->matchCVE(s);
313 }
314 
315 /**
316  * dnf_advisory_match_bug:
317  * @advisory: a #DnfAdvisory instance.
318  * @s: string
319  *
320  * Matches if #DnfAdvisoryRef has a #DnfAdvisoryRef which is bug
321  * and matches against string
322  *
323  * Returns: %TRUE if they are the same
324  *
325  * Since: 0.7.0
326  */
327 gboolean
dnf_advisory_match_bug(DnfAdvisory * advisory,const char * s)328 dnf_advisory_match_bug(DnfAdvisory *advisory, const char *s)
329 {
330     return advisory->matchBug(s);
331 }
332