[retro-gtk/c-port: 22/37] Port CoreDescriptor to C



commit fafdcbc3ff197d972185fa9506694453d1aabb46
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Thu Sep 7 12:10:03 2017 +0200

    Port CoreDescriptor to C

 retro-gtk/Makefile.am                      |    5 +-
 retro-gtk/retro-core-descriptor-error.vala |    8 -
 retro-gtk/retro-core-descriptor.c          |  796 ++++++++++++++++++++++++++++
 retro-gtk/retro-core-descriptor.h          |   70 +++
 retro-gtk/retro-core-descriptor.vala       |  178 -------
 retro-gtk/retro-module-iterator.h          |    2 +
 6 files changed, 871 insertions(+), 188 deletions(-)
---
diff --git a/retro-gtk/Makefile.am b/retro-gtk/Makefile.am
index 9a3968c..88345f5 100644
--- a/retro-gtk/Makefile.am
+++ b/retro-gtk/Makefile.am
@@ -39,8 +39,7 @@ libretro_gtk_la_SOURCES = \
        core-error.vala \
        retro.vala \
        retro-cairo-display.c \
-       retro-core-descriptor.vala \
-       retro-core-descriptor-error.vala \
+       retro-core-descriptor.c \
        retro-core-view.c \
        retro-core-view-input-device.c \
        retro-game-info.c \
@@ -67,6 +66,8 @@ video/retro-video-converter.c: retro-gtk-internal.h
 
 retro-core.c: retro-gtk-internal.h
 
+# retro-core-descriptor.c: retro-gtk-internal.h
+
 retro-core-view.c: retro-gtk-internal.h
 
 retro-core-view-input-device.c: retro-gtk-internal.h
diff --git a/retro-gtk/retro-core-descriptor.c b/retro-gtk/retro-core-descriptor.c
new file mode 100644
index 0000000..0d2b26b
--- /dev/null
+++ b/retro-gtk/retro-core-descriptor.c
@@ -0,0 +1,796 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#include "retro-core-descriptor.h"
+
+struct _RetroCoreDescriptor
+{
+  GObject parent_instance;
+  gchar *filename;
+  GKeyFile *key_file;
+};
+
+G_DEFINE_TYPE (RetroCoreDescriptor, retro_core_descriptor, G_TYPE_OBJECT)
+
+#define RETRO_CORE_DESCRIPTOR_ERROR (retro_core_descriptor_error_quark ())
+
+enum {
+  RETRO_CORE_DESCRIPTOR_ERROR_REQUIRED_GROUP_NOT_FOUND,
+  RETRO_CORE_DESCRIPTOR_ERROR_REQUIRED_KEY_NOT_FOUND,
+  RETRO_CORE_DESCRIPTOR_ERROR_FIRMWARE_NOT_FOUND
+};
+
+#define RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP "Libretro"
+#define RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX "Platform:"
+#define RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX "Firmware:"
+
+#define RETRO_CORE_DESCRIPTOR_TYPE_KEY "Type"
+#define RETRO_CORE_DESCRIPTOR_NAME_KEY "Name"
+#define RETRO_CORE_DESCRIPTOR_ICON_KEY "Icon"
+#define RETRO_CORE_DESCRIPTOR_MODULE_KEY "Module"
+#define RETRO_CORE_DESCRIPTOR_LIBRETRO_VERSION_KEY "LibretroVersion"
+
+#define RETRO_CORE_DESCRIPTOR_PLATFORM_MIME_TYPE_KEY "MimeType"
+#define RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY "Firmwares"
+
+#define RETRO_CORE_DESCRIPTOR_FIRMWARE_PATH_KEY "Path"
+#define RETRO_CORE_DESCRIPTOR_FIRMWARE_MD5_KEY "MD5"
+#define RETRO_CORE_DESCRIPTOR_FIRMWARE_SHA512_KEY "SHA-512"
+#define RETRO_CORE_DESCRIPTOR_FIRMWARE_MANDATORY_KEY "Mandatory"
+
+#define RETRO_CORE_DESCRIPTOR_TYPE_GAME "Game"
+#define RETRO_CORE_DESCRIPTOR_TYPE_EMULATOR "Emulator"
+
+/* Private */
+
+static GQuark
+retro_core_descriptor_error_quark (void)
+{
+  return g_quark_from_static_string ("retro-core-descriptor-error-quark");
+}
+
+static void
+retro_core_descriptor_finalize (GObject *object)
+{
+  RetroCoreDescriptor *self = RETRO_CORE_DESCRIPTOR (object);
+
+  g_free (self->filename);
+  g_key_file_unref (self->key_file);
+
+  G_OBJECT_CLASS (retro_core_descriptor_parent_class)->finalize (object);
+}
+
+static void
+retro_core_descriptor_class_init (RetroCoreDescriptorClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = retro_core_descriptor_finalize;
+}
+
+static void
+retro_core_descriptor_init (RetroCoreDescriptor *self)
+{
+}
+
+static gboolean
+has_group_prefixed (RetroCoreDescriptor *self,
+                    const gchar         *group_prefix,
+                    const gchar         *group_suffix)
+{
+  gchar *group;
+  gboolean result;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+  g_return_val_if_fail (group_prefix != NULL, FALSE);
+  g_return_val_if_fail (group_suffix != NULL, FALSE);
+
+  group = g_strconcat (group_prefix, group_suffix, NULL);
+  result = g_key_file_has_group (self->key_file, group);
+  g_free (group);
+
+  return result;
+}
+
+static gboolean
+has_key_prefixed (RetroCoreDescriptor  *self,
+                  const gchar          *group_prefix,
+                  const gchar          *group_suffix,
+                  const gchar          *key,
+                  GError              **error)
+{
+  gchar *group;
+  gboolean result;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+  g_return_val_if_fail (group_prefix != NULL, FALSE);
+  g_return_val_if_fail (group_suffix != NULL, FALSE);
+  g_return_val_if_fail (key != NULL, FALSE);
+
+  group = g_strconcat (group_prefix, group_suffix, NULL);
+  result = g_key_file_has_key (self->key_file,
+                               group,
+                               key,
+                               &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_free (group);
+    g_propagate_error (error, tmp_error);
+
+    return FALSE;
+  }
+
+  g_free (group);
+
+  return result;
+}
+
+static gchar *
+get_string_prefixed (RetroCoreDescriptor  *self,
+                     const gchar          *group_prefix,
+                     const gchar          *group_suffix,
+                     const gchar          *key,
+                     GError              **error)
+{
+  gchar *group;
+  gchar *result;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+  g_return_val_if_fail (group_prefix != NULL, NULL);
+  g_return_val_if_fail (group_suffix != NULL, NULL);
+  g_return_val_if_fail (key != NULL, NULL);
+
+  group = g_strconcat (group_prefix, group_suffix, NULL);
+
+  result = g_key_file_get_string (self->key_file,
+                                  group,
+                                  key,
+                                  &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_free (group);
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  g_free (group);
+
+  return result;
+}
+
+static gchar **
+get_string_list_prefixed (RetroCoreDescriptor  *self,
+                          const gchar          *group_prefix,
+                          const gchar          *group_suffix,
+                          const gchar          *key,
+                          gsize                *length,
+                          GError              **error)
+{
+  gchar *group;
+  gchar **result;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+  g_return_val_if_fail (group_prefix != NULL, NULL);
+  g_return_val_if_fail (group_suffix != NULL, NULL);
+  g_return_val_if_fail (key != NULL, NULL);
+  g_return_val_if_fail (length != NULL, NULL);
+
+  group = g_strconcat (group_prefix, group_suffix, NULL);
+  result = g_key_file_get_string_list (self->key_file,
+                                       group,
+                                       key,
+                                       length,
+                                       &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+  g_free (group);
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  g_free (group);
+
+  return result;
+}
+
+static void
+retro_core_descriptor_check_has_required_key (RetroCoreDescriptor  *self,
+                                              const gchar          *group,
+                                              const gchar          *key,
+                                              GError              **error)
+{
+  gboolean has_key;
+  GError *tmp_error = NULL;
+
+  g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
+  g_return_if_fail (group != NULL);
+  g_return_if_fail (key != NULL);
+
+  has_key = g_key_file_has_key (self->key_file,
+                                RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                RETRO_CORE_DESCRIPTOR_TYPE_KEY,
+                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  if (!has_key)
+    g_set_error (error,
+                 RETRO_CORE_DESCRIPTOR_ERROR,
+                 RETRO_CORE_DESCRIPTOR_ERROR_REQUIRED_KEY_NOT_FOUND,
+                 "%s isn't a valid Libretro core descriptor: "
+                 "required key %s not found in group [%s].",
+                 self->filename,
+                 key,
+                 group);
+}
+
+// FIXME static
+void
+retro_core_descriptor_check_libretro_group (RetroCoreDescriptor  *self,
+                                            GError              **error)
+{
+  GError *tmp_error = NULL;
+
+  g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                                RETRO_CORE_DESCRIPTOR_TYPE_KEY,
+                                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                                RETRO_CORE_DESCRIPTOR_NAME_KEY,
+                                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                                RETRO_CORE_DESCRIPTOR_MODULE_KEY,
+                                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                                RETRO_CORE_DESCRIPTOR_LIBRETRO_VERSION_KEY,
+                                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+}
+
+// FIXME static
+void
+retro_core_descriptor_check_platform_group (RetroCoreDescriptor  *self,
+                                            const gchar          *group,
+                                            GError              **error)
+{
+  gboolean has_key;
+  gchar **firmwares;
+  gchar **firmware_p;
+  gchar *firmware_group;
+  GError *tmp_error = NULL;
+
+  g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
+  g_return_if_fail (group != NULL);
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                group,
+                                                RETRO_CORE_DESCRIPTOR_PLATFORM_MIME_TYPE_KEY,
+                                                &tmp_error);
+
+  has_key = g_key_file_has_key (self->key_file,
+                                group,
+                                RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY,
+                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  if (!has_key)
+    return;
+
+  firmwares = g_key_file_get_string_list (self->key_file,
+                                          group,
+                                          RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY,
+                                          NULL,
+                                          &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  for (firmware_p = firmwares; firmware_p != NULL; firmware_p++) {
+    firmware_group = g_strconcat (RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX, *firmware_p, NULL);
+    if (!g_key_file_has_group (self->key_file, firmware_group)) {
+      g_set_error (error,
+                   RETRO_CORE_DESCRIPTOR_ERROR,
+                   RETRO_CORE_DESCRIPTOR_ERROR_FIRMWARE_NOT_FOUND,
+                   "%s isn't a valid Libretro core descriptor:"
+                   "[%s] mentioned in [%s] not found.",
+                   self->filename,
+                   firmware_group,
+                   group);
+
+      g_free (firmware_group);
+      g_strfreev (firmwares);
+
+      return;
+    }
+
+    g_free (firmware_group);
+  }
+
+  g_strfreev (firmwares);
+}
+
+static void
+retro_core_descriptor_check_firmware_group (RetroCoreDescriptor  *self,
+                                            const gchar          *group,
+                                            GError              **error)
+{
+  GError *tmp_error = NULL;
+
+  g_return_if_fail (RETRO_IS_CORE_DESCRIPTOR (self));
+  g_return_if_fail (group != NULL);
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                group,
+                                                RETRO_CORE_DESCRIPTOR_FIRMWARE_PATH_KEY,
+                                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+
+  retro_core_descriptor_check_has_required_key (self,
+                                                group,
+                                                RETRO_CORE_DESCRIPTOR_FIRMWARE_MANDATORY_KEY,
+                                                &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return;
+  }
+}
+
+/* Public */
+
+gboolean
+retro_core_descriptor_has_icon (RetroCoreDescriptor  *self,
+                                GError              **error)
+{
+  gboolean has_icon;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+
+  has_icon = g_key_file_has_key (self->key_file, RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP, 
RETRO_CORE_DESCRIPTOR_ICON_KEY, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return FALSE;
+  }
+
+  return has_icon;
+}
+
+gchar *
+retro_core_descriptor_get_uri (RetroCoreDescriptor *self)
+{
+  gchar *result;
+  GFile *file;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+
+  file = g_file_new_for_path (self->filename);
+  result = g_file_get_uri (file);
+  g_object_unref (file);
+
+  return result;
+}
+
+gchar *
+retro_core_descriptor_get_id (RetroCoreDescriptor *self)
+{
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+
+  return g_path_get_basename (self->filename);
+}
+
+gboolean
+retro_core_descriptor_get_is_game (RetroCoreDescriptor  *self,
+                                   GError              **error)
+{
+  gchar *type;
+  gboolean is_game;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+
+  type = g_key_file_get_string (self->key_file,
+                                  RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                  RETRO_CORE_DESCRIPTOR_TYPE_KEY,
+                                  &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return FALSE;
+  }
+
+  is_game = g_strcmp0 (type, RETRO_CORE_DESCRIPTOR_TYPE_GAME) == 0;
+  g_free (type);
+
+  return is_game;
+}
+
+gboolean
+retro_core_descriptor_get_is_emulator (RetroCoreDescriptor  *self,
+                                       GError              **error)
+{
+  gchar *type;
+  gboolean is_game;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+
+  type = g_key_file_get_string (self->key_file,
+                                  RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                  RETRO_CORE_DESCRIPTOR_TYPE_KEY,
+                                  &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return FALSE;
+  }
+
+  is_game = g_strcmp0 (type, RETRO_CORE_DESCRIPTOR_TYPE_EMULATOR) == 0;
+  g_free (type);
+
+  return is_game;
+}
+
+GIcon *
+retro_core_descriptor_get_icon (RetroCoreDescriptor  *self,
+                                GError              **error)
+{
+  gchar *icon_name;
+  GIcon *icon;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+
+  icon_name = g_key_file_get_string (self->key_file,
+                                     RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                     RETRO_CORE_DESCRIPTOR_ICON_KEY,
+                                     &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  icon = G_ICON (g_themed_icon_new (icon_name));
+  g_free (icon_name);
+
+  return icon;
+}
+
+char *
+retro_core_descriptor_get_module (RetroCoreDescriptor  *self,
+                                  GError              **error)
+{
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+
+  return g_key_file_get_string (self->key_file,
+                                RETRO_CORE_DESCRIPTOR_LIBRETRO_GROUP,
+                                RETRO_CORE_DESCRIPTOR_MODULE_KEY,
+                                error);
+}
+
+GFile *
+retro_core_descriptor_get_module_file (RetroCoreDescriptor  *self,
+                                       GError              **error)
+{
+  GFile *file;
+  GFile *dir;
+  gchar *module;
+  GFile *module_file;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), NULL);
+
+  file = g_file_new_for_path (self->filename);
+  dir = g_file_get_parent (file);
+  g_object_unref (file);
+  if (dir == NULL)
+    return NULL;
+
+  module = retro_core_descriptor_get_module (self, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+    g_object_unref (dir);
+
+    return NULL;
+  }
+
+  module_file = g_file_get_child (dir, module);
+  if (!g_file_query_exists (module_file, NULL)) {
+    g_object_unref (dir);
+    g_free (module);
+    g_object_unref (module_file);
+
+    return NULL;
+  }
+
+  g_object_unref (dir);
+  g_free (module);
+
+  return module_file;
+}
+
+gboolean
+retro_core_descriptor_has_platform (RetroCoreDescriptor *self,
+                                    const gchar         *platform)
+{
+  return has_group_prefixed (self,
+                           RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX,
+                           platform);
+}
+
+gboolean
+retro_core_descriptor_has_firmwares (RetroCoreDescriptor  *self,
+                                     const gchar          *firmware,
+                                     GError              **error)
+{
+  return has_key_prefixed (self,
+                           RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX,
+                           firmware,
+                           RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY,
+                           error);
+}
+
+gboolean
+retro_core_descriptor_has_firmware_md5 (RetroCoreDescriptor  *self,
+                                        const gchar          *firmware,
+                                        GError              **error)
+{
+  return has_key_prefixed (self,
+                           RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX,
+                           firmware,
+                           RETRO_CORE_DESCRIPTOR_FIRMWARE_MD5_KEY,
+                           error);
+}
+
+gboolean
+retro_core_descriptor_has_firmware_sha512 (RetroCoreDescriptor  *self,
+                                           const gchar          *firmware,
+                                           GError              **error)
+{
+  return has_key_prefixed (self,
+                           RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX,
+                           firmware,
+                           RETRO_CORE_DESCRIPTOR_FIRMWARE_SHA512_KEY,
+                           error);
+}
+
+gchar **
+retro_core_descriptor_get_mime_type (RetroCoreDescriptor  *self,
+                                     const gchar          *platform,
+                                     gsize                *length,
+                                     GError              **error)
+{
+  return get_string_list_prefixed (self,
+                                   RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX,
+                                   platform,
+                                   RETRO_CORE_DESCRIPTOR_PLATFORM_MIME_TYPE_KEY,
+                                   length,
+                                   error);
+}
+
+gchar **
+retro_core_descriptor_get_firmwares (RetroCoreDescriptor  *self,
+                                     const gchar          *platform,
+                                     gsize                *length,
+                                     GError              **error)
+{
+  return get_string_list_prefixed (self,
+                                   RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX,
+                                   platform,
+                                   RETRO_CORE_DESCRIPTOR_PLATFORM_FIRMWARES_KEY,
+                                   length,
+                                   error);
+}
+
+gchar *
+retro_core_descriptor_get_firmware_path (RetroCoreDescriptor  *self,
+                                         const gchar          *firmware,
+                                         GError              **error)
+{
+  return get_string_prefixed (self,
+                                       RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX,
+                                       firmware,
+                                       RETRO_CORE_DESCRIPTOR_FIRMWARE_PATH_KEY,
+                                       error);
+}
+
+gchar *
+retro_core_descriptor_get_firmware_md5 (RetroCoreDescriptor  *self,
+                                        const gchar          *firmware,
+                                        GError              **error)
+{
+  return get_string_prefixed (self,
+                                       RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX,
+                                       firmware,
+                                       RETRO_CORE_DESCRIPTOR_FIRMWARE_MD5_KEY,
+                                       error);
+}
+
+gchar *
+retro_core_descriptor_get_firmware_sha512 (RetroCoreDescriptor  *self,
+                                           const gchar          *firmware,
+                                           GError              **error)
+{
+  return get_string_prefixed (self,
+                                       RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX,
+                                       firmware,
+                                       RETRO_CORE_DESCRIPTOR_FIRMWARE_SHA512_KEY,
+                                       error);
+}
+
+gboolean
+retro_core_descriptor_get_is_firmware_mandatory (RetroCoreDescriptor  *self,
+                                                 const gchar          *firmware,
+                                                 GError              **error)
+{
+  gchar *group;
+  gboolean result;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+  g_return_val_if_fail (firmware != NULL, FALSE);
+
+  group = g_strconcat (RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX, firmware, NULL);
+  result = g_key_file_get_boolean (self->key_file,
+                                   group,
+                                   RETRO_CORE_DESCRIPTOR_FIRMWARE_MANDATORY_KEY,
+                                   &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_free (group);
+    g_propagate_error (error, tmp_error);
+
+    return FALSE;
+  }
+
+  g_free (group);
+
+  return result;
+}
+
+gboolean
+retro_core_descriptor_get_platform_supports_mime_types (RetroCoreDescriptor  *self,
+                                                        const gchar          *platform,
+                                                        gchar               **mime_types,
+                                                        gsize                 mime_types_length,
+                                                        GError              **error)
+{
+  gchar **supported_mime_types;
+  gsize supported_mime_types_length;
+  gsize i;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (RETRO_IS_CORE_DESCRIPTOR (self), FALSE);
+  g_return_val_if_fail (platform != NULL, FALSE);
+  g_return_val_if_fail (mime_types != NULL, FALSE);
+
+  supported_mime_types =
+    retro_core_descriptor_get_mime_type (self,
+                                         platform,
+                                         &supported_mime_types_length,
+                                         &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_propagate_error (error, tmp_error);
+
+    return FALSE;
+  }
+
+  for (i = 0; i < mime_types_length; i++)
+    if (!g_strv_contains ((const char * const *) supported_mime_types,
+                          mime_types[i])) {
+      g_strfreev (supported_mime_types);
+
+      return FALSE;
+    }
+
+  g_strfreev (supported_mime_types);
+
+  return TRUE;
+}
+
+RetroCoreDescriptor *
+retro_core_descriptor_new (const gchar  *filename,
+                           GError      **error)
+{
+  RetroCoreDescriptor *self;
+  gchar ** groups;
+  gsize groups_length;
+  gsize i;
+  GError *tmp_error = NULL;
+
+  g_return_val_if_fail (filename != NULL, NULL);
+
+  self =  g_object_new (RETRO_TYPE_CORE_DESCRIPTOR, NULL);
+  self->filename = g_strdup (filename);
+  self->key_file = g_key_file_new ();
+  g_key_file_load_from_file (self->key_file,
+                             filename,
+                             G_KEY_FILE_NONE,
+                             &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_object_unref (self);
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  retro_core_descriptor_check_libretro_group (self, &tmp_error);
+  if (G_UNLIKELY (tmp_error != NULL)) {
+    g_object_unref (self);
+    g_propagate_error (error, tmp_error);
+
+    return NULL;
+  }
+
+  groups = g_key_file_get_groups (self->key_file, &groups_length);
+  for (i = 0; i < groups_length; i++) {
+    if (g_str_has_prefix (groups[i],
+                          RETRO_CORE_DESCRIPTOR_PLATFORM_GROUP_PREFIX)) {
+      retro_core_descriptor_check_platform_group (self, groups[i], &tmp_error);
+      if (G_UNLIKELY (tmp_error != NULL)) {
+        g_strfreev (groups);
+        g_object_unref (self);
+        g_propagate_error (error, tmp_error);
+
+        return NULL;
+      }
+    }
+    else if (g_str_has_prefix (groups[i],
+                               RETRO_CORE_DESCRIPTOR_FIRMWARE_GROUP_PREFIX)) {
+      retro_core_descriptor_check_firmware_group (self, groups[i], &tmp_error);
+      if (G_UNLIKELY (tmp_error != NULL)) {
+        g_strfreev (groups);
+        g_object_unref (self);
+        g_propagate_error (error, tmp_error);
+
+        return NULL;
+      }
+    }
+  }
+
+  g_strfreev (groups);
+
+  return self;
+}
diff --git a/retro-gtk/retro-core-descriptor.h b/retro-gtk/retro-core-descriptor.h
new file mode 100644
index 0000000..e7d72dc
--- /dev/null
+++ b/retro-gtk/retro-core-descriptor.h
@@ -0,0 +1,70 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#ifndef RETRO_CORE_DESCRIPTOR_H
+#define RETRO_CORE_DESCRIPTOR_H
+
+#include <gio/gio.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define RETRO_TYPE_CORE_DESCRIPTOR (retro_core_descriptor_get_type())
+
+G_DECLARE_FINAL_TYPE (RetroCoreDescriptor, retro_core_descriptor, RETRO, CORE_DESCRIPTOR, GObject)
+
+RetroCoreDescriptor *retro_core_descriptor_new (const gchar  *filename,
+                                                GError      **error);
+gboolean retro_core_descriptor_has_icon (RetroCoreDescriptor  *self,
+                                         GError              **error);
+gchar *retro_core_descriptor_get_uri (RetroCoreDescriptor *self);
+gchar *retro_core_descriptor_get_id (RetroCoreDescriptor *self);
+gboolean retro_core_descriptor_get_is_game (RetroCoreDescriptor  *self,
+                                            GError              **error);
+gboolean retro_core_descriptor_get_is_emulator (RetroCoreDescriptor  *self,
+                                                GError              **error);
+GIcon *retro_core_descriptor_get_icon (RetroCoreDescriptor  *self,
+                                       GError              **error);
+char *retro_core_descriptor_get_module (RetroCoreDescriptor  *self,
+                                        GError              **error);
+GFile *retro_core_descriptor_get_module_file (RetroCoreDescriptor  *self,
+                                              GError              **error);
+gboolean retro_core_descriptor_has_platform (RetroCoreDescriptor *self,
+                                             const gchar         *platform);
+gboolean retro_core_descriptor_has_firmwares (RetroCoreDescriptor  *self,
+                                              const gchar          *firmware,
+                                              GError              **error);
+gboolean retro_core_descriptor_has_firmware_md5 (RetroCoreDescriptor  *self,
+                                                 const gchar          *firmware,
+                                                 GError              **error);
+gboolean retro_core_descriptor_has_firmware_sha512 (RetroCoreDescriptor  *self,
+                                                    const gchar          *firmware,
+                                                    GError              **error);
+gchar **retro_core_descriptor_get_mime_type (RetroCoreDescriptor  *self,
+                                             const gchar          *platform,
+                                             gsize                *length,
+                                             GError              **error);
+gchar **retro_core_descriptor_get_firmwares (RetroCoreDescriptor  *self,
+                                             const gchar          *platform,
+                                             gsize                *length,
+                                             GError              **error);
+gchar *retro_core_descriptor_get_firmware_path (RetroCoreDescriptor  *self,
+                                                const gchar          *firmware,
+                                                GError              **error);
+gchar * retro_core_descriptor_get_firmware_md5 (RetroCoreDescriptor  *self,
+                                                const gchar          *firmware,
+                                                GError              **error);
+gchar * retro_core_descriptor_get_firmware_sha512 (RetroCoreDescriptor  *self,
+                                                   const gchar          *firmware,
+                                                   GError              **error);
+gboolean retro_core_descriptor_get_is_firmware_mandatory (RetroCoreDescriptor  *self,
+                                                          const gchar          *firmware,
+                                                          GError              **error);
+gboolean retro_core_descriptor_get_platform_supports_mime_types (RetroCoreDescriptor  *self,
+                                                                 const gchar          *platform,
+                                                                 gchar               **mime_types,
+                                                                 gsize                 mime_types_length,
+                                                                 GError              **error);
+
+G_END_DECLS
+
+#endif /* RETRO_CORE_DESCRIPTOR_H */
diff --git a/retro-gtk/retro-module-iterator.h b/retro-gtk/retro-module-iterator.h
index e3912a0..11b4ccc 100644
--- a/retro-gtk/retro-module-iterator.h
+++ b/retro-gtk/retro-module-iterator.h
@@ -5,6 +5,8 @@
 
 #include <glib-object.h>
 
+#include "retro-core-descriptor.h"
+
 G_BEGIN_DECLS
 
 // FIXME Remove as soon as possible.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]