summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2017-02-27 09:37:09 +0000
committerRichard Hughes <richard@hughsie.com>2017-02-27 11:36:28 +0000
commit407d3839fe54d4f7f130f77141b8b8a1c114e056 (patch)
tree3da5f3fa988ea10c5c214b5b201b5561e99d3a00
parent239dab71f09f496c80b4ceba16241b8912e03ece (diff)
downloadappstream-glib-407d3839fe54d4f7f130f77141b8b8a1c114e056.tar.gz
Add AsFormat object
This represents the file that is used to build an AsApp.
-rw-r--r--docs/api/appstream-glib-docs.sgml1
-rw-r--r--libappstream-glib/Makefile.am4
-rw-r--r--libappstream-glib/as-format.c275
-rw-r--r--libappstream-glib/as-format.h91
4 files changed, 371 insertions, 0 deletions
diff --git a/docs/api/appstream-glib-docs.sgml b/docs/api/appstream-glib-docs.sgml
index 43a0586..05d72e3 100644
--- a/docs/api/appstream-glib-docs.sgml
+++ b/docs/api/appstream-glib-docs.sgml
@@ -37,6 +37,7 @@
<xi:include href="xml/as-bundle.xml"/>
<xi:include href="xml/as-checksum.xml"/>
<xi:include href="xml/as-content-rating.xml"/>
+ <xi:include href="xml/as-format.xml"/>
<xi:include href="xml/as-icon.xml"/>
<xi:include href="xml/as-image.xml"/>
<xi:include href="xml/as-markup.xml"/>
diff --git a/libappstream-glib/Makefile.am b/libappstream-glib/Makefile.am
index ec976b6..4640a00 100644
--- a/libappstream-glib/Makefile.am
+++ b/libappstream-glib/Makefile.am
@@ -84,6 +84,7 @@ libappstream_glib_include_HEADERS = \
as-require.h \
as-review.h \
as-screenshot.h \
+ as-format.h \
as-store.h \
as-suggest.h \
as-tag.h \
@@ -106,6 +107,7 @@ libappstream_glib_la_SOURCES = \
as-content-rating.c \
as-content-rating-private.h \
as-enums.c \
+ as-format.c \
as-icon.c \
as-icon-private.h \
as-image.c \
@@ -207,6 +209,8 @@ introspection_sources = \
as-checksum.h \
as-content-rating.c \
as-content-rating.h \
+ as-format.c \
+ as-format.h \
as-enums.c \
as-enums.h \
as-icon.c \
diff --git a/libappstream-glib/as-format.c b/libappstream-glib/as-format.c
new file mode 100644
index 0000000..c8bd6b2
--- /dev/null
+++ b/libappstream-glib/as-format.c
@@ -0,0 +1,275 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 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-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);
+ 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);
+ 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;
+#ifdef HAVE_GCAB
+ if (g_str_has_suffix (filename, ".cab"))
+ return AS_FORMAT_KIND_APPSTREAM;
+#endif
+ 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)
+{
+ AsFormatPrivate *priv = GET_PRIVATE (format);
+ if (priv->kind == AS_FORMAT_KIND_UNKNOWN)
+ priv->kind = as_format_guess_kind (filename);
+ as_ref_string_assign_safe (&priv->filename, 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);
+ 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);
+
+ /* 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);
+}
diff --git a/libappstream-glib/as-format.h b/libappstream-glib/as-format.h
new file mode 100644
index 0000000..a269c52
--- /dev/null
+++ b/libappstream-glib/as-format.h
@@ -0,0 +1,91 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 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
+ */
+
+#if !defined (__APPSTREAM_GLIB_H) && !defined (AS_COMPILATION)
+#error "Only <appstream-glib.h> can be included directly."
+#endif
+
+#ifndef __AS_FORMAT_H
+#define __AS_FORMAT_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define AS_TYPE_FORMAT (as_format_get_type ())
+G_DECLARE_DERIVABLE_TYPE (AsFormat, as_format, AS, FORMAT, GObject)
+
+struct _AsFormatClass
+{
+ GObjectClass parent_class;
+ /*< private >*/
+ void (*_as_reserved1) (void);
+ void (*_as_reserved2) (void);
+ void (*_as_reserved3) (void);
+ void (*_as_reserved4) (void);
+ void (*_as_reserved5) (void);
+ void (*_as_reserved6) (void);
+ void (*_as_reserved7) (void);
+ void (*_as_reserved8) (void);
+};
+
+/**
+ * AsFormatKind:
+ * @AS_FORMAT_KIND_UNKNOWN: Not formatd from a file
+ * @AS_FORMAT_KIND_APPSTREAM: AppStream file
+ * @AS_FORMAT_KIND_DESKTOP: Desktop file
+ * @AS_FORMAT_KIND_APPDATA: AppData file
+ * @AS_FORMAT_KIND_METAINFO: MetaInfo file
+ *
+ * The format kind.
+ **/
+typedef enum {
+ AS_FORMAT_KIND_UNKNOWN, /* Since: 0.6.9 */
+ AS_FORMAT_KIND_APPSTREAM, /* Since: 0.6.9 */
+ AS_FORMAT_KIND_DESKTOP, /* Since: 0.6.9 */
+ AS_FORMAT_KIND_APPDATA, /* Since: 0.6.9 */
+ AS_FORMAT_KIND_METAINFO, /* Since: 0.6.9 */
+ /*< private >*/
+ AS_FORMAT_KIND_LAST
+} AsFormatKind;
+
+AsFormat *as_format_new (void);
+
+/* helpers */
+AsFormatKind as_format_kind_from_string (const gchar *kind);
+const gchar *as_format_kind_to_string (AsFormatKind kind);
+gboolean as_format_equal (AsFormat *format1,
+ AsFormat *format2);
+AsFormatKind as_format_guess_kind (const gchar *filename);
+
+/* getters */
+const gchar *as_format_get_filename (AsFormat *format);
+AsFormatKind as_format_get_kind (AsFormat *format);
+
+/* setters */
+void as_format_set_filename (AsFormat *format,
+ const gchar *filename);
+void as_format_set_kind (AsFormat *format,
+ AsFormatKind kind);
+
+G_END_DECLS
+
+#endif /* __AS_FORMAT_H */