1 /*
2  * Copyright (C) 2014 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 /**
24  * SECTION:dnf-advisoryref
25  * @short_description: Update advisory reference
26  * @include: libdnf.h
27  * @stability: Unstable
28  *
29  * This object represents a reference given to an update.
30  *
31  * See also: #DnfContext
32  */
33 
34 
35 #include <solv/repo.h>
36 #include <solv/util.h>
37 #include <glib-object.h>
38 
39 #include "dnf-advisoryref-private.hpp"
40 #include "dnf-sack-private.hpp"
41 #include "sack/advisoryref.hpp"
42 
43 /**
44  * dnf_advisoryref_new:
45  *
46  * Creates a new #DnfAdvisoryRef.
47  *
48  * Returns:(transfer full): a #DnfAdvisoryRef
49  *
50  * Since: 0.7.0
51  **/
52 DnfAdvisoryRef *
dnf_advisoryref_new(DnfSack * sack,Id a_id,int index)53 dnf_advisoryref_new(DnfSack *sack, Id a_id, int index)
54 {
55     return new libdnf::AdvisoryRef(sack, a_id, index);
56 }
57 
58 /**
59  * dnf_advisoryref_free:
60  *
61  * Destructor of #DnfAdvisoryRef.
62  *
63  * Since: 0.13.0
64  **/
65 void
dnf_advisoryref_free(DnfAdvisoryRef * advisoryref)66 dnf_advisoryref_free(DnfAdvisoryRef *advisoryref)
67 {
68     delete advisoryref;
69 }
70 
71 /**
72  * dnf_advisoryref_compare:
73  * @left: a #DnfAdvisoryRef instance.
74  * @right: another #DnfAdvisoryRef instance.
75  *
76  * Checks two #DnfAdvisoryRef objects for equality.
77  *
78  * Returns: TRUE if the #DnfAdvisoryRef objects are the same
79  *
80  * Since: 0.7.0
81  */
82 int
dnf_advisoryref_compare(DnfAdvisoryRef * left,DnfAdvisoryRef * right)83 dnf_advisoryref_compare(DnfAdvisoryRef *left, DnfAdvisoryRef *right)
84 {
85     return *left == *right;
86 }
87 
88 static const char *
advisoryref_get_str(DnfAdvisoryRef * advisoryref,Id keyname)89 advisoryref_get_str(DnfAdvisoryRef *advisoryref, Id keyname)
90 {
91     Dataiterator di;
92     const char *str = NULL;
93     int count = 0;
94     Pool *pool = dnf_sack_get_pool(advisoryref->getDnfSack());
95 
96     dataiterator_init(&di, pool, 0, advisoryref->getAdvisory(), UPDATE_REFERENCE, 0, 0);
97     while (dataiterator_step(&di)) {
98         dataiterator_setpos(&di);
99         if (count++ == advisoryref->getIndex()) {
100             str = pool_lookup_str(pool, SOLVID_POS, keyname);
101             break;
102         }
103     }
104     dataiterator_free(&di);
105 
106     return str;
107 }
108 
109 /**
110  * dnf_advisoryref_get_kind:
111  * @advisoryref: a #DnfAdvisoryRef instance.
112  *
113  * Gets the kind of advisory reference.
114  *
115  * Returns: a #DnfAdvisoryRef, e.g. %DNF_REFERENCE_KIND_BUGZILLA
116  *
117  * Since: 0.7.0
118  */
119 DnfAdvisoryRefKind
dnf_advisoryref_get_kind(DnfAdvisoryRef * advisoryref)120 dnf_advisoryref_get_kind(DnfAdvisoryRef *advisoryref)
121 {
122     const char *type;
123     type = advisoryref_get_str(advisoryref, UPDATE_REFERENCE_TYPE);
124     if (type == NULL)
125         return DNF_REFERENCE_KIND_UNKNOWN;
126     if (!g_strcmp0 (type, "bugzilla"))
127         return DNF_REFERENCE_KIND_BUGZILLA;
128     if (!g_strcmp0 (type, "cve"))
129         return DNF_REFERENCE_KIND_CVE;
130     if (!g_strcmp0 (type, "vendor"))
131         return DNF_REFERENCE_KIND_VENDOR;
132     return DNF_REFERENCE_KIND_UNKNOWN;
133 }
134 
135 /**
136  * dnf_advisoryref_get_id:
137  * @advisoryref: a #DnfAdvisoryRef instance.
138  *
139  * Gets an ID for the advisory.
140  *
141  * Returns: the advisory ID
142  *
143  * Since: 0.7.0
144  */
145 const char *
dnf_advisoryref_get_id(DnfAdvisoryRef * advisoryref)146 dnf_advisoryref_get_id(DnfAdvisoryRef *advisoryref)
147 {
148     return advisoryref_get_str(advisoryref, UPDATE_REFERENCE_ID);
149 }
150 
151 /**
152  * dnf_advisoryref_get_title:
153  * @advisoryref: a #DnfAdvisoryRef instance.
154  *
155  * Gets a title to use for the advisory.
156  *
157  * Returns: the advisory title
158  *
159  * Since: 0.7.0
160  */
161 const char *
dnf_advisoryref_get_title(DnfAdvisoryRef * advisoryref)162 dnf_advisoryref_get_title(DnfAdvisoryRef *advisoryref)
163 {
164     return advisoryref_get_str(advisoryref, UPDATE_REFERENCE_TITLE);
165 }
166 
167 /**
168  * dnf_advisoryref_get_url:
169  * @advisoryref: a #DnfAdvisoryRef instance.
170  *
171  * Gets the link for the advisory.
172  *
173  * Returns: the advisory URL
174  *
175  * Since: 0.7.0
176  */
177 const char *
dnf_advisoryref_get_url(DnfAdvisoryRef * advisoryref)178 dnf_advisoryref_get_url(DnfAdvisoryRef *advisoryref)
179 {
180     return advisoryref_get_str(advisoryref, UPDATE_REFERENCE_HREF);
181 }
182