[system/karton] src: Store OS list and sort it alphanumerically

Derek Lin <[email protected]> Tue, 4 Aug 2026 17:07:04 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 9194c7e3400c95d135aab0e41aab4a60b43051a0 by Derek Lin, on behalf of Martin Sh.
Committed on 04/08/2026 at 17:07.
Pushed by kenoi into branch 'master'.

Store OS list and sort it alphanumerically

M  +19   -23   src/osinfoconfig.cpp
M  +4    -1    src/osinfoconfig.h
M  +2    -2    src/qml/InstallationDialog.qml

https://invent.kde.org/system/karton/-/commit/9194c7e3400c95d135aab0e41aab4a60b43051a0

diff --git a/src/osinfoconfig.cpp b/src/osinfoconfig.cpp
index 47083bc..a24c90d 100644
--- a/src/osinfoconfig.cpp
+++ b/src/osinfoconfig.cpp
@@ -4,6 +4,7 @@
 #include "osinfoconfig.h"
 
 #include "karton_debug.h"
+#include <QCollator>
 #include <QFileInfo>
 extern "C" // due to undefined references to libosinfo contents
 {
@@ -11,7 +12,8 @@ extern "C" // due to undefined references to libosinfo contents
 }
 
 OsinfoConfig::OsinfoConfig()
-    : m_loader([]() -> OsinfoLoader * {
+    : m_osList()
+    , m_loader([]() -> OsinfoLoader * {
         if (auto loader = osinfo_loader_new(); loader) {
             g_autoptr(GError) error = nullptr;
             osinfo_loader_process_default_path(loader, &error);
@@ -32,6 +34,22 @@ OsinfoConfig::OsinfoConfig()
         return nullptr;
     }())
 {
+    if (!m_db) {
+        qCCritical(KARTON_DEBUG) << "OS database not initialized";
+        return;
+    }
+
+    GObjectPtr<OsinfoOsList> list(osinfo_db_get_os_list(m_db.get()));
+    gint len = osinfo_list_get_length(OSINFO_LIST(list.get()));
+
+    for (gint i = 0; i < len; i++) {
+        OsinfoOs *os = OSINFO_OS(osinfo_list_get_nth(OSINFO_LIST(list.get()), i));
+        // const gchar *id = osinfo_entity_get_id(OSINFO_ENTITY(os));
+        const gchar *id = osinfo_product_get_short_id(OSINFO_PRODUCT(os));
+        m_osList.append(QString::fromUtf8(id));
+    }
+    QCollator order;
+    std::sort(m_osList.begin(), m_osList.end(), order);
 }
 
 OsinfoConfig::~OsinfoConfig() = default;
@@ -147,25 +165,3 @@ QString OsinfoConfig::getOsArchitecture(const QString &osId)
 
     return QString::fromUtf8(os_arch);
 }
-
-QStringList OsinfoConfig::getOsVariants()
-{
-    QStringList osList;
-
-    if (!m_db) {
-        qCCritical(KARTON_DEBUG) << "OS database not initialized";
-        return osList;
-    }
-
-    GObjectPtr<OsinfoOsList> list(osinfo_db_get_os_list(m_db.get()));
-    gint len = osinfo_list_get_length(OSINFO_LIST(list.get()));
-
-    for (gint i = 0; i < len; i++) {
-        OsinfoOs *os = OSINFO_OS(osinfo_list_get_nth(OSINFO_LIST(list.get()), i));
-        // const gchar *id = osinfo_entity_get_id(OSINFO_ENTITY(os));
-        const gchar *id = osinfo_product_get_short_id(OSINFO_PRODUCT(os));
-        osList.append(QString::fromUtf8(id));
-    }
-    osList.sort();
-    return osList;
-}
diff --git a/src/osinfoconfig.h b/src/osinfoconfig.h
index cfc1604..9924e59 100644
--- a/src/osinfoconfig.h
+++ b/src/osinfoconfig.h
@@ -33,6 +33,8 @@ class OsinfoConfig : public QObject
 
             Q_DISABLE_COPY_MOVE(OsinfoConfig);
 
+    Q_PROPERTY(QStringList osList MEMBER m_osList CONSTANT)
+
 public:
     explicit OsinfoConfig();
     ~OsinfoConfig();
@@ -42,12 +44,13 @@ public:
     QString getOsIdFromShortId(const QString &short_id);
     Q_INVOKABLE QString getShortIdFromId(const QString &id);
     Q_INVOKABLE QString getOsIdFromDisk(const QString &isoDiskPath);
-    Q_INVOKABLE QStringList getOsVariants();
     QString getOsArchitecture(const QString &osId);
 
 private:
     bool initOsDb();
 
+    QStringList m_osList;
+
     GObjectPtr<OsinfoLoader> m_loader;
     GObjectPtr<OsinfoDb> m_db;
 };
diff --git a/src/qml/InstallationDialog.qml b/src/qml/InstallationDialog.qml
index a2218b2..a92f1b4 100644
--- a/src/qml/InstallationDialog.qml
+++ b/src/qml/InstallationDialog.qml
@@ -31,7 +31,7 @@ Kirigami.Dialog {
             onTriggered: {
                 if (nameField.text.trim() === ""
                     || diskImageField.text.trim() === ""
-                    || !OsinfoConfig.getOsVariants().includes(osField.editText.trim())) {
+                    || !OsinfoConfig.osList.includes(osField.editText.trim())) {
                     showError = true;
                     return;
                 }
@@ -160,7 +160,7 @@ Kirigami.Dialog {
 
                             function updateSuggestions() {
                                 const filterText = osTextField.text.toLowerCase();
-                                listView.model = OsinfoConfig.getOsVariants().filter((osVariant) => osVariant.toLowerCase().includes(filterText));
+                                listView.model = OsinfoConfig.osList.filter((osVariant) => osVariant.toLowerCase().includes(filterText));
                                 listView.model.length > 0 ? open() : close();
                             }