-
Notifications
You must be signed in to change notification settings - Fork 112
/
as-relation-check-result.c
318 lines (284 loc) · 9.42 KB
/
as-relation-check-result.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2022-2024 Matthias Klumpp <matthias@tenstral.net>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the license, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:as-releation-check-result
* @short_description: Result from a satisfication check on an #AsRelation
* @include: appstream.h
*
* This class contains resulting information from a check for whether an
* #AsRelation is satisfied on a specific system configuration.
*
* See also: #AsRelation
*/
#include "config.h"
#include "as-relation-check-result-private.h"
#include "as-relation-private.h"
typedef struct {
AsRelationStatus status;
AsRelation *relation;
gchar *message;
AsRelationError error_code;
} AsRelationCheckResultPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AsRelationCheckResult, as_relation_check_result, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (as_relation_check_result_get_instance_private (o))
static void
as_relation_check_result_init (AsRelationCheckResult *relcr)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
priv->message = NULL;
}
static void
as_relation_check_result_finalize (GObject *object)
{
AsRelationCheckResult *relcr = AS_RELATION_CHECK_RESULT (object);
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
g_free (priv->message);
if (priv->relation != NULL)
g_object_unref (priv->relation);
G_OBJECT_CLASS (as_relation_check_result_parent_class)->finalize (object);
}
static void
as_relation_check_result_class_init (AsRelationCheckResultClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = as_relation_check_result_finalize;
}
/**
* as_relation_check_result_new:
*
* Creates a new #AsRelationCheckResult.
*
* Returns: (transfer full): a #AsRelationCheckResult
*
* Since: 1.0.0
**/
AsRelationCheckResult *
as_relation_check_result_new (void)
{
AsRelationCheckResult *relcr;
relcr = g_object_new (AS_TYPE_RELATION_CHECK_RESULT, NULL);
return AS_RELATION_CHECK_RESULT (relcr);
}
/**
* as_relation_check_result_get_status:
* @relcr: an #AsRelationCheckResult instance.
*
* Returns the status of this relation check result.
* If the status is %AS_RELATION_STATUS_ERROR, an error message will
* have been set as message.
*
* Returns: an #AsRelationStatus
*/
AsRelationStatus
as_relation_check_result_get_status (AsRelationCheckResult *relcr)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
return priv->status;
}
/**
* as_relation_check_result_set_status:
* @relcr: an #AsRelationCheckResult instance.
* @status: the new #AsRelationStatus
*
* Set the outcome of this relation check result.
*/
void
as_relation_check_result_set_status (AsRelationCheckResult *relcr, AsRelationStatus status)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
priv->status = status;
}
/**
* as_relation_check_result_get_relation:
* @relcr: an #AsRelationCheckResult instance.
*
* Get the relation that this check result was generated for.
*
* Returns: (nullable) (transfer none): an #AsRelation or %NULL
*/
AsRelation *
as_relation_check_result_get_relation (AsRelationCheckResult *relcr)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
return priv->relation;
}
/**
* as_relation_check_result_set_relation:
* @relcr: an #AsRelationCheckResult instance.
* @relation: the #AsRelation
*
* Set an #AsRelation to associate with this check result.
*/
void
as_relation_check_result_set_relation (AsRelationCheckResult *relcr, AsRelation *relation)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
if (priv->relation != NULL)
g_object_unref (priv->relation);
priv->relation = g_object_ref (relation);
}
/**
* as_relation_check_result_get_message:
* @relcr: an #AsRelationCheckResult instance.
*
* Get a human-readable message about the state of this relation.
* May be %NULL in case the relation is satisfied and there is no further information about it.
*
* Returns: (nullable): a human-readable message about this relation's state.
*/
const gchar *
as_relation_check_result_get_message (AsRelationCheckResult *relcr)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
return priv->message;
}
/**
* as_relation_check_result_set_message:
* @relcr: an #AsRelationCheckResult instance.
* @format: the new message
*
* Set a human-readable information message about the satisfaction state
* of the dependency under the checked system configuration.
*/
void
as_relation_check_result_set_message (AsRelationCheckResult *relcr, const gchar *format, ...)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
va_list args;
g_free (priv->message);
va_start (args, format);
priv->message = g_strdup_vprintf (format, args);
va_end (args);
}
/**
* as_relation_check_result_get_error_code:
* @relcr: an #AsRelationCheckResult instance.
*
* Retrieve the error code, in case this result represents an error.
*
* Returns: an #AsRelationError
*/
AsRelationError
as_relation_check_result_get_error_code (AsRelationCheckResult *relcr)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
return priv->error_code;
}
/**
* as_relation_check_result_set_error_code:
* @relcr: an #AsRelationCheckResult instance.
* @ecode: the #AsRelationError
*
* Set the error code in case this result represents an error.
*/
void
as_relation_check_result_set_error_code (AsRelationCheckResult *relcr, AsRelationError ecode)
{
AsRelationCheckResultPrivate *priv = GET_PRIVATE (relcr);
priv->error_code = ecode;
}
/**
* as_relation_check_results_get_compatibility_score:
* @rc_results: (element-type AsRelationCheckResult): an array of #AsRelationCheckResult
*
* Calculate a compatibility sore between 0 and 100 based on the given set of
* AsRelationCheckResults.
*
* A compatibility of 100 means all requirements are satisfied and the component will
* run perfectly on the confoguration it was tested agains, while 0 means it will not run at all.
*/
gint
as_relation_check_results_get_compatibility_score (GPtrArray *rc_results)
{
/* we assume 100% compatibility by default */
gint score = 100;
gboolean have_control_supports = FALSE;
gboolean found_supported_control = FALSE;
for (guint i = 0; i < rc_results->len; i++) {
AsRelationCheckResult *rcr = AS_RELATION_CHECK_RESULT (
g_ptr_array_index (rc_results, i));
AsRelationKind rel_kind;
AsRelationItemKind rel_item_kind;
AsRelationStatus status;
AsRelation *rel = as_relation_check_result_get_relation (rcr);
if (rel == NULL) {
g_warning ("Missing associated relation for relation-check result entity.");
continue;
}
rel_kind = as_relation_get_kind (rel);
rel_item_kind = as_relation_get_item_kind (rel);
status = as_relation_check_result_get_status (rcr);
/* Anything that is required and not fulfilled will give an instant 0% compatibility,
* if we don't know the status of a required element, we give a strong penality. */
if (rel_kind == AS_RELATION_KIND_REQUIRES) {
if (status == AS_RELATION_STATUS_UNKNOWN)
score -= 30;
else if (status != AS_RELATION_STATUS_SATISFIED)
return 0;
/* if we are here, the requirement is satisfied, and if it is an input control,
* we recognize an input control is available */
if (rel_item_kind == AS_RELATION_ITEM_KIND_CONTROL) {
have_control_supports = TRUE;
found_supported_control = TRUE;
}
continue;
}
/* Missing recommended items get a penalty */
if (rel_kind == AS_RELATION_KIND_RECOMMENDS) {
/* for compatibility, we treat recommends a bit like supports with regards to controls */
if (rel_item_kind == AS_RELATION_ITEM_KIND_CONTROL)
have_control_supports = TRUE;
if (status != AS_RELATION_STATUS_SATISFIED) {
score -= 10;
if (rel_item_kind == AS_RELATION_ITEM_KIND_DISPLAY_LENGTH) {
/* reduce score again if the recommendation was for display */
score -= 20;
}
} else if (rel_item_kind == AS_RELATION_ITEM_KIND_CONTROL) {
found_supported_control = TRUE;
score += 5;
}
}
/* Increase score in case a supported item is present */
if (rel_kind == AS_RELATION_KIND_SUPPORTS) {
/* controls are special - if we have *none* of the supported/required/recommended
* controls, that is a pretty big issue, but as long as one is supported we are good */
if (rel_item_kind == AS_RELATION_ITEM_KIND_CONTROL) {
have_control_supports = TRUE;
if (status == AS_RELATION_STATUS_SATISFIED) {
score += 4;
found_supported_control = TRUE;
}
} else {
if (status == AS_RELATION_STATUS_SATISFIED)
score += 2;
}
}
}
/* If we have controls defined, but the configuration has no control that was listed as
* supported, controlling this software will be difficult. We add a huge penalty for that. */
if (have_control_supports && !found_supported_control)
score -= 60;
if (score < 0)
return 0;
if (score > 100)
return 100;
return score;
}