[Libosinfo] [PATCHv2] Add osinfo_product_foreach_related()

Christophe Fergeau cfergeau at redhat.com
Tue Apr 2 13:21:42 UTC 2013


This method iterates over all products that are related to a
given product. This will be useful to implement
osinfo_platform_get_all_devices().
---
 osinfo/Makefile.am              |  1 +
 osinfo/osinfo_product.c         | 60 +++++++++++++++++++++++++++++++++++++++++
 osinfo/osinfo_product_private.h | 51 +++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 osinfo/osinfo_product_private.h

diff --git a/osinfo/Makefile.am b/osinfo/Makefile.am
index 5e9a761..496ee34 100644
--- a/osinfo/Makefile.am
+++ b/osinfo/Makefile.am
@@ -75,6 +75,7 @@ OSINFO_HEADER_FILES =			\
   osinfo_install_script.h		\
   osinfo_install_scriptlist.h		\
   osinfo_product.h			\
+  osinfo_product_private.h		\
   osinfo_productfilter.h		\
   osinfo_productlist.h			\
   osinfo_platform.h			\
diff --git a/osinfo/osinfo_product.c b/osinfo/osinfo_product.c
index d4d9ec9..561da63 100644
--- a/osinfo/osinfo_product.c
+++ b/osinfo/osinfo_product.c
@@ -30,6 +30,8 @@
 #include <string.h>
 #include <glib/gi18n-lib.h>
 
+#include "osinfo/osinfo_product_private.h"
+
 G_DEFINE_ABSTRACT_TYPE (OsinfoProduct, osinfo_product, OSINFO_TYPE_ENTITY);
 
 #define OSINFO_PRODUCT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), OSINFO_TYPE_PRODUCT, OsinfoProductPrivate))
@@ -367,6 +369,64 @@ const gchar *osinfo_product_get_logo(OsinfoProduct *prod)
     return osinfo_entity_get_param_value(OSINFO_ENTITY(prod), OSINFO_PRODUCT_PROP_LOGO);
 }
 
+static OsinfoList *osinfo_list_append(OsinfoList *appendee,
+                                      OsinfoList *appended)
+{
+    OsinfoList *result;
+    result = osinfo_list_new_union(appendee, appended);
+    g_object_unref(G_OBJECT(appendee));
+
+    return result;
+}
+
+void osinfo_product_foreach_related(OsinfoProduct *product,
+                                    OsinfoProductForeachFlag flags,
+                                    OsinfoProductForeach foreach_func,
+                                    gpointer user_data)
+{
+    OsinfoList *related_list;
+    OsinfoProductList *tmp_related;
+    guint i;
+
+    foreach_func(product, user_data);
+
+    related_list = OSINFO_LIST(osinfo_productlist_new());
+    if (flags & OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM) {
+        tmp_related = osinfo_product_get_related
+                      (product, OSINFO_PRODUCT_RELATIONSHIP_DERIVES_FROM);
+        related_list = osinfo_list_append(related_list,
+                                          OSINFO_LIST(tmp_related));
+        g_object_unref(G_OBJECT(tmp_related));
+    }
+
+    if (flags & OSINFO_PRODUCT_FOREACH_FLAG_UPGRADES) {
+        tmp_related = osinfo_product_get_related
+                      (product, OSINFO_PRODUCT_RELATIONSHIP_UPGRADES);
+        related_list = osinfo_list_append(related_list,
+                                          OSINFO_LIST(tmp_related));
+        g_object_unref(G_OBJECT(tmp_related));
+    }
+
+    if (flags & OSINFO_PRODUCT_FOREACH_FLAG_CLONES) {
+        tmp_related = osinfo_product_get_related
+                      (product, OSINFO_PRODUCT_RELATIONSHIP_CLONES);
+        related_list = osinfo_list_append(related_list,
+                                          OSINFO_LIST(tmp_related));
+        g_object_unref(G_OBJECT(tmp_related));
+    }
+
+    for (i = 0; i < osinfo_list_get_length(related_list); i++) {
+        OsinfoEntity *related;
+
+        related = osinfo_list_get_nth(related_list, i);
+        osinfo_product_foreach_related(OSINFO_PRODUCT(related),
+                                       flags,
+                                       foreach_func,
+                                       user_data);
+    }
+    g_object_unref (related_list);
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/osinfo/osinfo_product_private.h b/osinfo/osinfo_product_private.h
new file mode 100644
index 0000000..b3c279d
--- /dev/null
+++ b/osinfo/osinfo_product_private.h
@@ -0,0 +1,51 @@
+/*
+ * libosinfo: a software product
+ *
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *   Christophe Fergeau <cfergeau at redhat.com>
+ */
+
+#include "osinfo_product.h"
+
+#ifndef __OSINFO_PRODUCT_PRIVATE_H__
+#define __OSINFO_PRODUCT_PRIVATE_H__
+
+
+typedef void (*OsinfoProductForeach)(OsinfoProduct *product, gpointer user_data);
+
+typedef enum { /*< skip >*/
+    OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM = 1 << 0,
+    OSINFO_PRODUCT_FOREACH_FLAG_UPGRADES = 1 << 1,
+    OSINFO_PRODUCT_FOREACH_FLAG_CLONES = 1 << 2,
+} OsinfoProductForeachFlag;
+
+void osinfo_product_foreach_related(OsinfoProduct *product,
+                                    OsinfoProductForeachFlag flags,
+                                    OsinfoProductForeach foreach_func,
+                                    gpointer user_data);
+
+#endif /* __OSINFO_PRODUCT_PRIVATE_H__ */
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
-- 
1.8.1.4




More information about the Libosinfo mailing list