1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1+
6 */
7
8 /**
9 * SECTION:as-problem
10 * @short_description: Object representing a problem with an application
11 * @include: appstream-glib.h
12 * @stability: Stable
13 *
14 * Problems have a unlocalized message and also contain a line number and kind.
15 *
16 * See also: #AsApp
17 */
18
19 #include "config.h"
20
21 #include "as-problem.h"
22
23 typedef struct
24 {
25 AsProblemKind kind;
26 gchar *message;
27 guint line_number;
28 } AsProblemPrivate;
29
G_DEFINE_TYPE_WITH_PRIVATE(AsProblem,as_problem,G_TYPE_OBJECT)30 G_DEFINE_TYPE_WITH_PRIVATE (AsProblem, as_problem, G_TYPE_OBJECT)
31
32 #define GET_PRIVATE(o) (as_problem_get_instance_private (o))
33
34 static void
35 as_problem_finalize (GObject *object)
36 {
37 AsProblem *problem = AS_PROBLEM (object);
38 AsProblemPrivate *priv = GET_PRIVATE (problem);
39
40 g_free (priv->message);
41
42 G_OBJECT_CLASS (as_problem_parent_class)->finalize (object);
43 }
44
45 static void
as_problem_init(AsProblem * problem)46 as_problem_init (AsProblem *problem)
47 {
48 AsProblemPrivate *priv = GET_PRIVATE (problem);
49 priv->kind = AS_PROBLEM_KIND_UNKNOWN;
50 priv->line_number = 0;
51 }
52
53 static void
as_problem_class_init(AsProblemClass * klass)54 as_problem_class_init (AsProblemClass *klass)
55 {
56 GObjectClass *object_class = G_OBJECT_CLASS (klass);
57 object_class->finalize = as_problem_finalize;
58 }
59
60 /**
61 * as_problem_kind_to_string:
62 * @kind: the #AsProblemKind.
63 *
64 * Converts the enumerated value to an text representation.
65 *
66 * Returns: string version of @kind
67 *
68 * Since: 0.1.4
69 **/
70 const gchar *
as_problem_kind_to_string(AsProblemKind kind)71 as_problem_kind_to_string (AsProblemKind kind)
72 {
73 if (kind == AS_PROBLEM_KIND_TAG_DUPLICATED)
74 return "tag-duplicated";
75 if (kind == AS_PROBLEM_KIND_TAG_MISSING)
76 return "tag-missing";
77 if (kind == AS_PROBLEM_KIND_TAG_INVALID)
78 return "tag-invalid";
79 if (kind == AS_PROBLEM_KIND_ATTRIBUTE_MISSING)
80 return "attribute-missing";
81 if (kind == AS_PROBLEM_KIND_ATTRIBUTE_INVALID)
82 return "attribute-invalid";
83 if (kind == AS_PROBLEM_KIND_MARKUP_INVALID)
84 return "markup-invalid";
85 if (kind == AS_PROBLEM_KIND_STYLE_INCORRECT)
86 return "style-invalid";
87 if (kind == AS_PROBLEM_KIND_TRANSLATIONS_REQUIRED)
88 return "translations-required";
89 if (kind == AS_PROBLEM_KIND_DUPLICATE_DATA)
90 return "duplicate-data";
91 if (kind == AS_PROBLEM_KIND_VALUE_MISSING)
92 return "value-missing";
93 if (kind == AS_PROBLEM_KIND_FILE_INVALID)
94 return "file-invalid";
95 if (kind == AS_PROBLEM_KIND_ASPECT_RATIO_INCORRECT)
96 return "aspect-ratio-invalid";
97 if (kind == AS_PROBLEM_KIND_RESOLUTION_INCORRECT)
98 return "resolution-invalid";
99 if (kind == AS_PROBLEM_KIND_URL_NOT_FOUND)
100 return "url-not-found";
101 return NULL;
102 }
103
104 /**
105 * as_problem_get_kind:
106 * @problem: a #AsProblem instance.
107 *
108 * Gets the problem kind.
109 *
110 * Returns: a #AsProblemKind, e.g. %AS_PROBLEM_KIND_TAG_MISSING
111 *
112 * Since: 0.1.4
113 **/
114 AsProblemKind
as_problem_get_kind(AsProblem * problem)115 as_problem_get_kind (AsProblem *problem)
116 {
117 AsProblemPrivate *priv = GET_PRIVATE (problem);
118 g_return_val_if_fail (AS_IS_PROBLEM (problem), AS_PROBLEM_KIND_UNKNOWN);
119 return priv->kind;
120 }
121
122 /**
123 * as_problem_get_line_number:
124 * @problem: a #AsProblem instance.
125 *
126 * Gets the line number of the problem if known.
127 *
128 * Returns: a line number, where 0 is unknown
129 *
130 * Since: 0.1.4
131 **/
132 guint
as_problem_get_line_number(AsProblem * problem)133 as_problem_get_line_number (AsProblem *problem)
134 {
135 AsProblemPrivate *priv = GET_PRIVATE (problem);
136 g_return_val_if_fail (AS_IS_PROBLEM (problem), 0);
137 return priv->line_number;
138 }
139
140 /**
141 * as_problem_get_message:
142 * @problem: a #AsProblem instance.
143 *
144 * Gets the specific message for the problem.
145 *
146 * Returns: the message
147 *
148 * Since: 0.1.4
149 **/
150 const gchar *
as_problem_get_message(AsProblem * problem)151 as_problem_get_message (AsProblem *problem)
152 {
153 AsProblemPrivate *priv = GET_PRIVATE (problem);
154 g_return_val_if_fail (AS_IS_PROBLEM (problem), NULL);
155 return priv->message;
156 }
157
158 /**
159 * as_problem_set_kind:
160 * @problem: a #AsProblem instance.
161 * @kind: the #AsProblemKind.
162 *
163 * Sets the problem kind.
164 *
165 * Since: 0.1.4
166 **/
167 void
as_problem_set_kind(AsProblem * problem,AsProblemKind kind)168 as_problem_set_kind (AsProblem *problem, AsProblemKind kind)
169 {
170 AsProblemPrivate *priv = GET_PRIVATE (problem);
171 g_return_if_fail (AS_IS_PROBLEM (problem));
172 priv->kind = kind;
173 }
174
175 /**
176 * as_problem_set_line_number:
177 * @problem: a #AsProblem instance.
178 * @line_number: a #guint instance.
179 *
180 * Adds an line_number to the problem.
181 *
182 * Since: 0.1.4
183 **/
184 void
as_problem_set_line_number(AsProblem * problem,guint line_number)185 as_problem_set_line_number (AsProblem *problem, guint line_number)
186 {
187 AsProblemPrivate *priv = GET_PRIVATE (problem);
188 g_return_if_fail (AS_IS_PROBLEM (problem));
189 priv->line_number = line_number;
190 }
191
192 /**
193 * as_problem_set_message:
194 * @problem: a #AsProblem instance.
195 * @message: the message text.
196 *
197 * Sets a message on the problem.
198 *
199 * Since: 0.1.4
200 **/
201 void
as_problem_set_message(AsProblem * problem,const gchar * message)202 as_problem_set_message (AsProblem *problem, const gchar *message)
203 {
204 AsProblemPrivate *priv = GET_PRIVATE (problem);
205 g_return_if_fail (AS_IS_PROBLEM (problem));
206 g_free (priv->message);
207 priv->message = g_strdup (message);
208 }
209
210 /**
211 * as_problem_new:
212 *
213 * Creates a new #AsProblem.
214 *
215 * Returns: (transfer full): a #AsProblem
216 *
217 * Since: 0.1.4
218 **/
219 AsProblem *
as_problem_new(void)220 as_problem_new (void)
221 {
222 AsProblem *problem;
223 problem = g_object_new (AS_TYPE_PROBLEM, NULL);
224 return AS_PROBLEM (problem);
225 }
226