summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-tag.c
blob: a431f1b115e06ac3a58b192fadd964e22c7ea7ac (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014-2016 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-tag
 * @short_description: Helper functions to convert to and from tag enums
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * These functions will convert a tag enum such as %AS_TAG_COMPONENT to
 * it's string form, and also vice-versa.
 *
 * These helper functions may be useful if implementing an AppStream parser.
 */

#include "config.h"

#include "as-tag.h"

#ifdef HAVE_GPERF
  /* we need to define this now as gperf just writes a big header file */
  const struct tag_data *as_tag_from_gperf (const char *tag, guint etag);
  #include "as-tag-private.h"
#endif

/**
 * as_tag_from_string:
 * @tag: the string.
 *
 * Converts the text representation to an enumerated value.
 *
 * Returns: a %AsTag, or %AS_TAG_UNKNOWN if not known.
 *
 * Since: 0.1.0
 **/
AsTag
as_tag_from_string (const gchar *tag)
{
	return as_tag_from_string_full (tag, AS_TAG_FLAG_NONE);
}

/**
 * as_tag_from_string_full:
 * @tag: the string.
 * @flags: the #AsTagFlags e.g. %AS_TAG_FLAG_USE_FALLBACKS
 *
 * Converts the text representation to an enumerated value also converting
 * legacy key names.
 *
 * Returns: a %AsTag, or %AS_TAG_UNKNOWN if not known.
 *
 * Since: 0.1.2
 **/
AsTag
as_tag_from_string_full (const gchar *tag, AsTagFlags flags)
{
#ifdef HAVE_GPERF
	const struct tag_data *ky;
#else
	guint i;
#endif
	AsTag etag = AS_TAG_UNKNOWN;

	/* invalid */
	if (tag == NULL)
		return AS_TAG_UNKNOWN;

#ifdef HAVE_GPERF
	/* use a perfect hash */
	ky = as_tag_from_gperf (tag, strlen (tag));
	if (ky != NULL)
		etag = ky->etag;
#else
	for (i = 0; i < AS_TAG_LAST; i++) {
		if (g_strcmp0 (tag, as_tag_to_string (i)) == 0) {
			etag = i;
			break;
		}
	}
#endif

	/* deprecated names */
	if (etag == AS_TAG_UNKNOWN && (flags & AS_TAG_FLAG_USE_FALLBACKS)) {
		if (g_strcmp0 (tag, "appcategories") == 0)
			return AS_TAG_CATEGORIES;
		if (g_strcmp0 (tag, "appcategory") == 0)
			return AS_TAG_CATEGORY;
		if (g_strcmp0 (tag, "licence") == 0)
			return AS_TAG_PROJECT_LICENSE;
		if (g_strcmp0 (tag, "applications") == 0)
			return AS_TAG_COMPONENTS;
		if (g_strcmp0 (tag, "application") == 0)
			return AS_TAG_COMPONENT;
		if (g_strcmp0 (tag, "updatecontact") == 0)
			return AS_TAG_UPDATE_CONTACT;
		/* fix spelling error */
		if (g_strcmp0 (tag, "metadata_licence") == 0)
			return AS_TAG_METADATA_LICENSE;
	}

	/* translated versions */
	if (etag == AS_TAG_UNKNOWN && (flags & AS_TAG_FLAG_USE_TRANSLATED)) {
		if (g_strcmp0 (tag, "_name") == 0)
			return AS_TAG_NAME;
		if (g_strcmp0 (tag, "_summary") == 0)
			return AS_TAG_SUMMARY;
		if (g_strcmp0 (tag, "_caption") == 0)
			return AS_TAG_CAPTION;
	}

	return etag;
}

/**
 * as_tag_to_string:
 * @tag: the %AsTag value.
 *
 * Converts the enumerated value to an text representation.
 *
 * Returns: string version of @tag
 *
 * Since: 0.1.0
 **/
const gchar *
as_tag_to_string (AsTag tag)
{
	const gchar *str[] = {
		"unknown",
		"components",
		"component",
		"id",
		"pkgname",
		"name",
		"summary",
		"description",
		"url",
		"icon",
		"categories",
		"category",
		"keywords",
		"keyword",
		"mimetypes",
		"mimetype",
		"project_group",
		"project_license",
		"screenshot",
		"screenshots",
		"update_contact",
		"image",
		"compulsory_for_desktop",
		"priority",
		"caption",
		"languages",
		"lang",
		"metadata",
		"value",
		"releases",
		"release",
		"architectures",
		"arch",
		"metadata_license",
		"provides",
		"extends",
		"developer_name",
		"kudos",
		"kudo",
		"source_pkgname",
		"vetos",
		"veto",
		"bundle",
		"permissions",
		"permission",
		"location",
		"checksum",
		"size",
		"translation",
		"content_rating",
		"content_attribute",
		"version",
		"reviews",
		"review",
		"reviewer_name",
		"reviewer_id",
		NULL };
	if (tag > AS_TAG_LAST)
		tag = AS_TAG_LAST;
	return str[tag];
}