summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-problem.c
blob: 92cf913951ff2a8cec922ab0f2d93814d93fbc3e (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

/**
 * SECTION:as-problem
 * @short_description: Object representing a problem with an application
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * Problems have a unlocalized message and also contain a line number and kind.
 *
 * See also: #AsApp
 */

#include "config.h"

#include "as-problem.h"

typedef struct
{
	AsProblemKind		 kind;
	gchar			*message;
	guint			 line_number;
} AsProblemPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (AsProblem, as_problem, G_TYPE_OBJECT)

#define GET_PRIVATE(o) (as_problem_get_instance_private (o))

static void
as_problem_finalize (GObject *object)
{
	AsProblem *problem = AS_PROBLEM (object);
	AsProblemPrivate *priv = GET_PRIVATE (problem);

	g_free (priv->message);

	G_OBJECT_CLASS (as_problem_parent_class)->finalize (object);
}

static void
as_problem_init (AsProblem *problem)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	priv->kind = AS_PROBLEM_KIND_UNKNOWN;
	priv->line_number = 0;
}

static void
as_problem_class_init (AsProblemClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = as_problem_finalize;
}

/**
 * as_problem_kind_to_string:
 * @kind: the #AsProblemKind.
 *
 * Converts the enumerated value to an text representation.
 *
 * Returns: string version of @kind
 *
 * Since: 0.1.4
 **/
const gchar *
as_problem_kind_to_string (AsProblemKind kind)
{
	if (kind == AS_PROBLEM_KIND_TAG_DUPLICATED)
		return "tag-duplicated";
	if (kind == AS_PROBLEM_KIND_TAG_MISSING)
		return "tag-missing";
	if (kind == AS_PROBLEM_KIND_TAG_INVALID)
		return "tag-invalid";
	if (kind == AS_PROBLEM_KIND_ATTRIBUTE_MISSING)
		return "attribute-missing";
	if (kind == AS_PROBLEM_KIND_ATTRIBUTE_INVALID)
		return "attribute-invalid";
	if (kind == AS_PROBLEM_KIND_MARKUP_INVALID)
		return "markup-invalid";
	if (kind == AS_PROBLEM_KIND_STYLE_INCORRECT)
		return "style-invalid";
	if (kind == AS_PROBLEM_KIND_TRANSLATIONS_REQUIRED)
		return "translations-required";
	if (kind == AS_PROBLEM_KIND_DUPLICATE_DATA)
		return "duplicate-data";
	if (kind == AS_PROBLEM_KIND_VALUE_MISSING)
		return "value-missing";
	if (kind == AS_PROBLEM_KIND_FILE_INVALID)
		return "file-invalid";
	if (kind == AS_PROBLEM_KIND_ASPECT_RATIO_INCORRECT)
		return "aspect-ratio-invalid";
	if (kind == AS_PROBLEM_KIND_RESOLUTION_INCORRECT)
		return "resolution-invalid";
	if (kind == AS_PROBLEM_KIND_URL_NOT_FOUND)
		return "url-not-found";
	return NULL;
}

/**
 * as_problem_get_kind:
 * @problem: a #AsProblem instance.
 *
 * Gets the problem kind.
 *
 * Returns: a #AsProblemKind, e.g. %AS_PROBLEM_KIND_TAG_MISSING
 *
 * Since: 0.1.4
 **/
AsProblemKind
as_problem_get_kind (AsProblem *problem)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	g_return_val_if_fail (AS_IS_PROBLEM (problem), AS_PROBLEM_KIND_UNKNOWN);
	return priv->kind;
}

/**
 * as_problem_get_line_number:
 * @problem: a #AsProblem instance.
 *
 * Gets the line number of the problem if known.
 *
 * Returns: a line number, where 0 is unknown
 *
 * Since: 0.1.4
 **/
guint
as_problem_get_line_number (AsProblem *problem)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	g_return_val_if_fail (AS_IS_PROBLEM (problem), 0);
	return priv->line_number;
}

/**
 * as_problem_get_message:
 * @problem: a #AsProblem instance.
 *
 * Gets the specific message for the problem.
 *
 * Returns: the message
 *
 * Since: 0.1.4
 **/
const gchar *
as_problem_get_message (AsProblem *problem)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	g_return_val_if_fail (AS_IS_PROBLEM (problem), NULL);
	return priv->message;
}

/**
 * as_problem_set_kind:
 * @problem: a #AsProblem instance.
 * @kind: the #AsProblemKind.
 *
 * Sets the problem kind.
 *
 * Since: 0.1.4
 **/
void
as_problem_set_kind (AsProblem *problem, AsProblemKind kind)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	g_return_if_fail (AS_IS_PROBLEM (problem));
	priv->kind = kind;
}

/**
 * as_problem_set_line_number:
 * @problem: a #AsProblem instance.
 * @line_number: a #guint instance.
 *
 * Adds an line_number to the problem.
 *
 * Since: 0.1.4
 **/
void
as_problem_set_line_number (AsProblem *problem, guint line_number)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	g_return_if_fail (AS_IS_PROBLEM (problem));
	priv->line_number = line_number;
}

/**
 * as_problem_set_message:
 * @problem: a #AsProblem instance.
 * @message: the message text.
 *
 * Sets a message on the problem.
 *
 * Since: 0.1.4
 **/
void
as_problem_set_message (AsProblem *problem, const gchar *message)
{
	AsProblemPrivate *priv = GET_PRIVATE (problem);
	g_return_if_fail (AS_IS_PROBLEM (problem));
	g_free (priv->message);
	priv->message = g_strdup (message);
}

/**
 * as_problem_new:
 *
 * Creates a new #AsProblem.
 *
 * Returns: (transfer full): a #AsProblem
 *
 * Since: 0.1.4
 **/
AsProblem *
as_problem_new (void)
{
	AsProblem *problem;
	problem = g_object_new (AS_TYPE_PROBLEM, NULL);
	return AS_PROBLEM (problem);
}