1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2014-2016 Richard Hughes <richard@hughsie.com>
4  *
5  * SPDX-License-Identifier: LGPL-2.1+
6  */
7 
8 /**
9  * SECTION:as-enums
10  * @short_description: Helper functions for converting to and from enum strings
11  * @include: appstream-glib.h
12  * @stability: Stable
13  *
14  * These helper functions may be useful if implementing an AppStream parser.
15  */
16 
17 #include "config.h"
18 
19 #include "as-enums.h"
20 #include "as-app.h"
21 #include "as-ref-string.h"
22 
23 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
24 /**
25  * as_id_kind_to_string:
26  * @id_kind: the #AsIdKind.
27  *
28  * Converts the enumerated value to an text representation.
29  *
30  * Returns: string version of @id_kind
31  *
32  * Since: 0.1.0
33  **/
34 const gchar *
as_id_kind_to_string(AsIdKind id_kind)35 as_id_kind_to_string (AsIdKind id_kind)
36 {
37 	return as_app_kind_to_string (id_kind);
38 }
39 
40 /**
41  * as_id_kind_from_string:
42  * @id_kind: the string.
43  *
44  * Converts the text representation to an enumerated value.
45  *
46  * Returns: a #AsIdKind or %AS_ID_KIND_UNKNOWN for unknown
47  *
48  * Since: 0.1.0
49  **/
50 AsIdKind
as_id_kind_from_string(const gchar * id_kind)51 as_id_kind_from_string (const gchar *id_kind)
52 {
53 	return as_app_kind_from_string (id_kind);
54 }
55 G_GNUC_END_IGNORE_DEPRECATIONS
56 
57 /**
58  * as_url_kind_to_string:
59  * @url_kind: the @AsUrlKind.
60  *
61  * Converts the enumerated value to an text representation.
62  *
63  * Returns: string version of @url_kind
64  *
65  * Since: 0.1.0
66  **/
67 const gchar *
as_url_kind_to_string(AsUrlKind url_kind)68 as_url_kind_to_string (AsUrlKind url_kind)
69 {
70 	if (url_kind == AS_URL_KIND_HOMEPAGE)
71 		return as_ref_string_new_static ("homepage");
72 	if (url_kind == AS_URL_KIND_BUGTRACKER)
73 		return as_ref_string_new_static ("bugtracker");
74 	if (url_kind == AS_URL_KIND_FAQ)
75 		return as_ref_string_new_static ("faq");
76 	if (url_kind == AS_URL_KIND_DONATION)
77 		return as_ref_string_new_static ("donation");
78 	if (url_kind == AS_URL_KIND_HELP)
79 		return as_ref_string_new_static ("help");
80 	if (url_kind == AS_URL_KIND_MISSING)
81 		return as_ref_string_new_static ("missing");
82 	if (url_kind == AS_URL_KIND_TRANSLATE)
83 		return as_ref_string_new_static ("translate");
84 	if (url_kind == AS_URL_KIND_DETAILS)
85 		return as_ref_string_new_static ("details");
86 	if (url_kind == AS_URL_KIND_SOURCE)
87 		return as_ref_string_new_static ("source");
88 	if (url_kind == AS_URL_KIND_CONTACT)
89 		return as_ref_string_new_static ("contact");
90 	return as_ref_string_new_static ("unknown");
91 }
92 
93 /**
94  * as_url_kind_from_string:
95  * @url_kind: the string.
96  *
97  * Converts the text representation to an enumerated value.
98  *
99  * Returns: a #AsUrlKind or %AS_URL_KIND_UNKNOWN for unknown
100  *
101  * Since: 0.1.0
102  **/
103 AsUrlKind
as_url_kind_from_string(const gchar * url_kind)104 as_url_kind_from_string (const gchar *url_kind)
105 {
106 	if (g_strcmp0 (url_kind, "homepage") == 0)
107 		return AS_URL_KIND_HOMEPAGE;
108 	if (g_strcmp0 (url_kind, "bugtracker") == 0)
109 		return AS_URL_KIND_BUGTRACKER;
110 	if (g_strcmp0 (url_kind, "faq") == 0)
111 		return AS_URL_KIND_FAQ;
112 	if (g_strcmp0 (url_kind, "donation") == 0)
113 		return AS_URL_KIND_DONATION;
114 	if (g_strcmp0 (url_kind, "help") == 0)
115 		return AS_URL_KIND_HELP;
116 	if (g_strcmp0 (url_kind, "missing") == 0)
117 		return AS_URL_KIND_MISSING;
118 	if (g_strcmp0 (url_kind, "translate") == 0)
119 		return AS_URL_KIND_TRANSLATE;
120 	if (g_strcmp0 (url_kind, "details") == 0)
121 		return AS_URL_KIND_DETAILS;
122 	if (g_strcmp0 (url_kind, "source") == 0)
123 		return AS_URL_KIND_SOURCE;
124 	if (g_strcmp0 (url_kind, "contact") == 0)
125 		return AS_URL_KIND_CONTACT;
126 	return AS_URL_KIND_UNKNOWN;
127 }
128 
129 /**
130  * as_kudo_kind_to_string:
131  * @kudo_kind: the @AsKudoKind.
132  *
133  * Converts the enumerated value to an text representation.
134  *
135  * Returns: string version of @kudo_kind
136  *
137  * Since: 0.2.2
138  **/
139 const gchar *
as_kudo_kind_to_string(AsKudoKind kudo_kind)140 as_kudo_kind_to_string (AsKudoKind kudo_kind)
141 {
142 	if (kudo_kind == AS_KUDO_KIND_SEARCH_PROVIDER)
143 		return as_ref_string_new_static ("SearchProvider");
144 	if (kudo_kind == AS_KUDO_KIND_USER_DOCS)
145 		return as_ref_string_new_static ("UserDocs");
146 	if (kudo_kind == AS_KUDO_KIND_APP_MENU)
147 		return as_ref_string_new_static ("AppMenu");
148 	if (kudo_kind == AS_KUDO_KIND_MODERN_TOOLKIT)
149 		return as_ref_string_new_static ("ModernToolkit");
150 	if (kudo_kind == AS_KUDO_KIND_NOTIFICATIONS)
151 		return as_ref_string_new_static ("Notifications");
152 	if (kudo_kind == AS_KUDO_KIND_HIGH_CONTRAST)
153 		return as_ref_string_new_static ("HighContrast");
154 	if (kudo_kind == AS_KUDO_KIND_HI_DPI_ICON)
155 		return as_ref_string_new_static ("HiDpiIcon");
156 	return NULL;
157 }
158 
159 /**
160  * as_kudo_kind_from_string:
161  * @kudo_kind: the string.
162  *
163  * Converts the text representation to an enumerated value.
164  *
165  * Returns: a #AsKudoKind or %AS_KUDO_KIND_UNKNOWN for unknown
166  *
167  * Since: 0.2.2
168  **/
169 AsKudoKind
as_kudo_kind_from_string(const gchar * kudo_kind)170 as_kudo_kind_from_string (const gchar *kudo_kind)
171 {
172 	if (g_strcmp0 (kudo_kind, "SearchProvider") == 0)
173 		return AS_KUDO_KIND_SEARCH_PROVIDER;
174 	if (g_strcmp0 (kudo_kind, "UserDocs") == 0)
175 		return AS_KUDO_KIND_USER_DOCS;
176 	if (g_strcmp0 (kudo_kind, "AppMenu") == 0)
177 		return AS_KUDO_KIND_APP_MENU;
178 	if (g_strcmp0 (kudo_kind, "ModernToolkit") == 0)
179 		return AS_KUDO_KIND_MODERN_TOOLKIT;
180 	if (g_strcmp0 (kudo_kind, "Notifications") == 0)
181 		return AS_KUDO_KIND_NOTIFICATIONS;
182 	if (g_strcmp0 (kudo_kind, "HighContrast") == 0)
183 		return AS_KUDO_KIND_HIGH_CONTRAST;
184 	if (g_strcmp0 (kudo_kind, "HiDpiIcon") == 0)
185 		return AS_KUDO_KIND_HI_DPI_ICON;
186 	return AS_KUDO_KIND_UNKNOWN;
187 }
188 
189 /**
190  * as_size_kind_to_string:
191  * @size_kind: the #AsSizeKind.
192  *
193  * Converts the enumerated value to an text representation.
194  *
195  * Returns: string version of @size_kind
196  *
197  * Since: 0.5.2
198  **/
199 const gchar *
as_size_kind_to_string(AsSizeKind size_kind)200 as_size_kind_to_string (AsSizeKind size_kind)
201 {
202 	if (size_kind == AS_SIZE_KIND_INSTALLED)
203 		return as_ref_string_new_static ("installed");
204 	if (size_kind == AS_SIZE_KIND_DOWNLOAD)
205 		return as_ref_string_new_static ("download");
206 	return as_ref_string_new_static ("unknown");
207 }
208 
209 /**
210  * as_size_kind_from_string:
211  * @size_kind: the string.
212  *
213  * Converts the text representation to an enumerated value.
214  *
215  * Returns: a #AsSizeKind or %AS_SIZE_KIND_UNKNOWN for unknown
216  *
217  * Since: 0.5.2
218  **/
219 AsSizeKind
as_size_kind_from_string(const gchar * size_kind)220 as_size_kind_from_string (const gchar *size_kind)
221 {
222 	if (g_strcmp0 (size_kind, "installed") == 0)
223 		return AS_SIZE_KIND_INSTALLED;
224 	if (g_strcmp0 (size_kind, "download") == 0)
225 		return AS_SIZE_KIND_DOWNLOAD;
226 	return AS_SIZE_KIND_UNKNOWN;
227 }
228 
229 /**
230  * as_urgency_kind_to_string:
231  * @urgency_kind: the #AsUrgencyKind.
232  *
233  * Converts the enumerated value to an text representation.
234  *
235  * Returns: string version of @urgency_kind
236  *
237  * Since: 0.5.1
238  **/
239 const gchar *
as_urgency_kind_to_string(AsUrgencyKind urgency_kind)240 as_urgency_kind_to_string (AsUrgencyKind urgency_kind)
241 {
242 	if (urgency_kind == AS_URGENCY_KIND_LOW)
243 		return as_ref_string_new_static ("low");
244 	if (urgency_kind == AS_URGENCY_KIND_MEDIUM)
245 		return as_ref_string_new_static ("medium");
246 	if (urgency_kind == AS_URGENCY_KIND_HIGH)
247 		return as_ref_string_new_static ("high");
248 	if (urgency_kind == AS_URGENCY_KIND_CRITICAL)
249 		return as_ref_string_new_static ("critical");
250 	return as_ref_string_new_static ("unknown");
251 }
252 
253 /**
254  * as_urgency_kind_from_string:
255  * @urgency_kind: the string.
256  *
257  * Converts the text representation to an enumerated value.
258  *
259  * Returns: a #AsUrgencyKind or %AS_URGENCY_KIND_UNKNOWN for unknown
260  *
261  * Since: 0.5.1
262  **/
263 AsUrgencyKind
as_urgency_kind_from_string(const gchar * urgency_kind)264 as_urgency_kind_from_string (const gchar *urgency_kind)
265 {
266 	if (g_strcmp0 (urgency_kind, "low") == 0)
267 		return AS_URGENCY_KIND_LOW;
268 	if (g_strcmp0 (urgency_kind, "medium") == 0)
269 		return AS_URGENCY_KIND_MEDIUM;
270 	if (g_strcmp0 (urgency_kind, "high") == 0)
271 		return AS_URGENCY_KIND_HIGH;
272 	if (g_strcmp0 (urgency_kind, "critical") == 0)
273 		return AS_URGENCY_KIND_CRITICAL;
274 	return AS_URGENCY_KIND_UNKNOWN;
275 }
276