[gnome-boxes/recommended-downloads-always-look-for-latest-version-in-osinfo-db: 1/5] os-database: Add API to find the latest version of an OS




commit 8a475f7545bb06384ca0eb3d7a08a33498b99780
Author: Felipe Borges <felipeborges gnome org>
Date:   Wed Mar 2 11:32:29 2022 +0100

    os-database: Add API to find the latest version of an OS
    
    Given the prefix of an Osinfo.Os.id, we iterate over the os database
    to find its latest version.
    
    We also skip "special" variants such as "unknown", "rawhide", "testing",
    etc...

 src/os-database.vala | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
---
diff --git a/src/os-database.vala b/src/os-database.vala
index a007b219..7aeafb69 100644
--- a/src/os-database.vala
+++ b/src/os-database.vala
@@ -119,6 +119,39 @@ else if (release_b == null)
         return os_list;
     }
 
+    private const string[] skipped_os_versions = {
+        "unknown",
+        "rawhide",
+        "Rawhide",
+        "testing",
+        "factory",
+    };
+    public async Osinfo.Os? get_latest_release_for_os_prefix (string os_id_prefix) {
+        Osinfo.Os? latest_version = null;
+
+        var os_list = db.get_os_list ().get_elements ();
+        foreach (var entity in os_list) {
+            Osinfo.Os os = entity as Osinfo.Os;
+            if (!os.id.has_prefix (os_id_prefix))
+                continue;
+
+            if (os.version in skipped_os_versions)
+                continue;
+
+            if (latest_version == null) {
+                latest_version = os;
+
+                continue;
+            }
+
+            if (double.parse (os.version) > double.parse (latest_version.version)) {
+                latest_version = os;
+            }
+        }
+
+        return latest_version;
+    }
+
     public async GLib.List<Osinfo.Media> list_downloadable_oses () throws OSDatabaseError {
         if (!yield ensure_db_loaded ())
             throw new OSDatabaseError.DB_LOADING_FAILED ("Failed to load OS database");


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