[pim/incidenceeditor] src: Don't rely on secrecy enum ordering

Volker Krause <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit bcb9d751bcb7e50921ce657b29e5ba1cb3e5b083 by Volker Krause.
Committed on 05/08/2026 at 16:56.
Pushed by vkrause into branch 'master'.

Don't rely on secrecy enum ordering

Use the combobox user data field for mapping to/from the enum value
instead. Actually makes this code simpler, and avoids any assumptions
about specific enum values or their ordering.

M  +8    -14   src/incidencesecrecy.cpp

https://invent.kde.org/pim/incidenceeditor/-/commit/bcb9d751bcb7e50921ce657b29e5ba1cb3e5b083

diff --git a/src/incidencesecrecy.cpp b/src/incidencesecrecy.cpp
index 1847a142..12720b99 100644
--- a/src/incidencesecrecy.cpp
+++ b/src/incidencesecrecy.cpp
@@ -19,37 +19,31 @@ IncidenceSecrecy::IncidenceSecrecy(Ui::EventOrTodoDesktop *ui)
     : mUi(ui)
 {
     setObjectName("IncidenceSecrecy"_L1);
-    mUi->mSecrecyCombo->addItems(KCalUtils::Stringify::incidenceSecrecyList());
+    for (const auto secrecy :
+         {KCalendarCore::Incidence::SecrecyPublic, KCalendarCore::Incidence::SecrecyPrivate, KCalendarCore::Incidence::SecrecyConfidential}) {
+        mUi->mSecrecyCombo->addItem(KCalUtils::Stringify::incidenceSecrecy(secrecy), secrecy);
+    }
     connect(mUi->mSecrecyCombo, &QComboBox::currentIndexChanged, this, &IncidenceSecrecy::checkDirtyStatus);
 }
 
 void IncidenceSecrecy::load(const KCalendarCore::Incidence::Ptr &incidence)
 {
     mLoadedIncidence = incidence;
-    Q_ASSERT(mUi->mSecrecyCombo->count() == KCalUtils::Stringify::incidenceSecrecyList().count());
-    mUi->mSecrecyCombo->setCurrentIndex(incidence->secrecy());
+    mUi->mSecrecyCombo->setCurrentIndex(mUi->mSecrecyCombo->findData(mLoadedIncidence->secrecy()));
     mWasDirty = false;
 }
 
 void IncidenceSecrecy::save(const KCalendarCore::Incidence::Ptr &incidence)
 {
     Q_ASSERT(incidence);
-    switch (mUi->mSecrecyCombo->currentIndex()) {
-    case 1:
-        incidence->setSecrecy(KCalendarCore::Incidence::SecrecyPrivate);
-        break;
-    case 2:
-        incidence->setSecrecy(KCalendarCore::Incidence::SecrecyConfidential);
-        break;
-    default:
-        incidence->setSecrecy(KCalendarCore::Incidence::SecrecyPublic);
-    }
+    qDebug() << mUi->mSecrecyCombo->currentData();
+    incidence->setSecrecy(mUi->mSecrecyCombo->currentData().value<KCalendarCore::Incidence::Secrecy>());
 }
 
 bool IncidenceSecrecy::isDirty() const
 {
     if (mLoadedIncidence) {
-        if (mLoadedIncidence->secrecy() != mUi->mSecrecyCombo->currentIndex()) {
+        if (mLoadedIncidence->secrecy() != mUi->mSecrecyCombo->currentData().value<KCalendarCore::Incidence::Secrecy>()) {
             return true;
         }
     } else {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.