summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-format.c
blob: c9ea54d6188d8d998e3a0a510ecbd2ff944482ee (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * SECTION:as-format
 * @short_description: Object representing where information about a AsApp came from.
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * AsApp's may be made from several information formats, and this object
 * represents the filename (and kind) of the format.
 *
 * See also: #AsApp
 */

#include "config.h"

#include "as-format.h"
#include "as-ref-string.h"

typedef struct
{
	AsFormatKind		 kind;
	AsRefString		*filename;
} AsFormatPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (AsFormat, as_format, G_TYPE_OBJECT)

#define GET_PRIVATE(o) (as_format_get_instance_private (o))

static void
as_format_finalize (GObject *object)
{
	AsFormat *format = AS_FORMAT (object);
	AsFormatPrivate *priv = GET_PRIVATE (format);

	if (priv->filename != NULL)
		as_ref_string_unref (priv->filename);

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

static void
as_format_init (AsFormat *format)
{
}

static void
as_format_class_init (AsFormatClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = as_format_finalize;
}

/**
 * as_format_kind_from_string:
 * @kind: the string.
 *
 * Converts the text representation to an enumerated value.
 *
 * Returns: (transfer full): a #AsFormatKind, or %AS_FORMAT_KIND_UNKNOWN for unknown.
 *
 * Since: 0.6.9
 **/
AsFormatKind
as_format_kind_from_string (const gchar *kind)
{
	if (g_strcmp0 (kind, "appstream") == 0)
		return AS_FORMAT_KIND_APPSTREAM;
	if (g_strcmp0 (kind, "appdata") == 0)
		return AS_FORMAT_KIND_APPDATA;
	if (g_strcmp0 (kind, "metainfo") == 0)
		return AS_FORMAT_KIND_METAINFO;
	if (g_strcmp0 (kind, "desktop") == 0)
		return AS_FORMAT_KIND_DESKTOP;
	return AS_FORMAT_KIND_UNKNOWN;
}

/**
 * as_kind_to_string:
 * @kind: the #AsFormatKind.
 *
 * Converts the enumerated value to an text representation.
 *
 * Returns: string version of @kind
 *
 * Since: 0.6.9
 **/
const gchar *
as_format_kind_to_string (AsFormatKind kind)
{
	if (kind == AS_FORMAT_KIND_APPSTREAM)
		return "appstream";
	if (kind == AS_FORMAT_KIND_APPDATA)
		return "appdata";
	if (kind == AS_FORMAT_KIND_METAINFO)
		return "metainfo";
	if (kind == AS_FORMAT_KIND_DESKTOP)
		return "desktop";
	return NULL;
}

/**
 * as_format_get_filename:
 * @format: a #AsFormat instance.
 *
 * Gets the filename required for this format.
 *
 * Returns: Runtime identifier, e.g. "org.gnome.Platform/i386/master"
 *
 * Since: 0.6.9
 **/
const gchar *
as_format_get_filename (AsFormat *format)
{
	AsFormatPrivate *priv = GET_PRIVATE (format);
	g_return_val_if_fail (AS_IS_FORMAT (format), NULL);
	return priv->filename;
}

/**
 * as_format_get_kind:
 * @format: a #AsFormat instance.
 *
 * Gets the format kind.
 *
 * Returns: the #AsFormatKind
 *
 * Since: 0.6.9
 **/
AsFormatKind
as_format_get_kind (AsFormat *format)
{
	AsFormatPrivate *priv = GET_PRIVATE (format);
	g_return_val_if_fail (AS_IS_FORMAT (format), AS_FORMAT_KIND_UNKNOWN);
	return priv->kind;
}

/**
 * as_format_guess_kind:
 * @filename: a file name
 *
 * Guesses the source kind based from the filename.
 *
 * Return value: A #AsFormatKind, e.g. %AS_FORMAT_KIND_APPSTREAM.
 *
 * Since: 0.6.9
 **/
AsFormatKind
as_format_guess_kind (const gchar *filename)
{
	if (g_str_has_suffix (filename, ".xml.gz"))
		return AS_FORMAT_KIND_APPSTREAM;
	if (g_str_has_suffix (filename, ".yml"))
		return AS_FORMAT_KIND_APPSTREAM;
	if (g_str_has_suffix (filename, ".yml.gz"))
		return AS_FORMAT_KIND_APPSTREAM;
	if (g_str_has_suffix (filename, ".desktop"))
		return AS_FORMAT_KIND_DESKTOP;
	if (g_str_has_suffix (filename, ".desktop.in"))
		return AS_FORMAT_KIND_DESKTOP;
	if (g_str_has_suffix (filename, ".appdata.xml"))
		return AS_FORMAT_KIND_APPDATA;
	if (g_str_has_suffix (filename, ".appdata.xml.in"))
		return AS_FORMAT_KIND_APPDATA;
	if (g_str_has_suffix (filename, ".metainfo.xml"))
		return AS_FORMAT_KIND_METAINFO;
	if (g_str_has_suffix (filename, ".metainfo.xml.in"))
		return AS_FORMAT_KIND_METAINFO;
	if (g_str_has_suffix (filename, ".xml"))
		return AS_FORMAT_KIND_APPSTREAM;
	return AS_FORMAT_KIND_UNKNOWN;
}

/**
 * as_format_set_filename:
 * @format: a #AsFormat instance.
 * @filename: the URL.
 *
 * Sets the filename required for this format.
 *
 * Since: 0.6.9
 **/
void
as_format_set_filename (AsFormat *format, const gchar *filename)
{
	gchar *canon_filename;

	AsFormatPrivate *priv = GET_PRIVATE (format);
	g_return_if_fail (AS_IS_FORMAT (format));
	if (priv->kind == AS_FORMAT_KIND_UNKNOWN)
		priv->kind = as_format_guess_kind (filename);
	canon_filename = g_canonicalize_filename (filename, NULL);
	as_ref_string_assign_safe (&priv->filename, canon_filename);
	g_free (canon_filename);
}

/**
 * as_format_set_kind:
 * @format: a #AsFormat instance.
 * @kind: the #AsFormatKind, e.g. %AS_FORMAT_KIND_APPDATA.
 *
 * Sets the format kind.
 *
 * Since: 0.6.9
 **/
void
as_format_set_kind (AsFormat *format, AsFormatKind kind)
{
	AsFormatPrivate *priv = GET_PRIVATE (format);
	g_return_if_fail (AS_IS_FORMAT (format));
	priv->kind = kind;
}

/**
 * as_format_equal:
 * @format1: a #AsFormat instance.
 * @format2: a #AsFormat instance.
 *
 * Checks if two formats are the same.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.6.9
 **/
gboolean
as_format_equal (AsFormat *format1, AsFormat *format2)
{
	AsFormatPrivate *priv1 = GET_PRIVATE (format1);
	AsFormatPrivate *priv2 = GET_PRIVATE (format2);

	g_return_val_if_fail (AS_IS_FORMAT (format1), FALSE);
	g_return_val_if_fail (AS_IS_FORMAT (format2), FALSE);

	/* trivial */
	if (format1 == format2)
		return TRUE;

	/* check for equality */
	if (priv1->kind != priv2->kind)
		return FALSE;
	if (g_strcmp0 (priv1->filename, priv2->filename) != 0)
		return FALSE;

	/* success */
	return TRUE;
}

/**
 * as_format_new:
 *
 * Creates a new #AsFormat.
 *
 * Returns: (transfer full): a #AsFormat
 *
 * Since: 0.6.9
 **/
AsFormat *
as_format_new (void)
{
	AsFormat *format;
	format = g_object_new (AS_TYPE_FORMAT, NULL);
	return AS_FORMAT (format);
}