diff ui/mainwindow.cpp @ 372:6cc124e79066

Add save/load unselected and add role enumerator Explicitly naming the roles avoids confusion and makes the code more readable
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 15 Apr 2014 11:56:29 +0200
parents 75f7288ad67b
children 9e38a4bcd44e
line wrap: on
line diff
--- a/ui/mainwindow.cpp	Tue Apr 15 11:04:34 2014 +0200
+++ b/ui/mainwindow.cpp	Tue Apr 15 11:56:29 2014 +0200
@@ -56,6 +56,7 @@
     if (!trayMode) {
         show();
     }
+    loadUnselectedCertificates();
 }
 
 void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
@@ -316,7 +317,6 @@
 
 void MainWindow::loadCertificateList()
 {
-    qDebug() << "display certificates";
     mCertListWidget->clear();
     int i = 0;
     foreach (const Certificate &cert, mListToInstall.getCertificates()) {
@@ -326,18 +326,19 @@
         }
         QListWidgetItem* item = new QListWidgetItem(cert.shortDescription());
         SeparatorItemDelegate *separator = new SeparatorItemDelegate();
-        item->setData(Qt::UserRole, cert.details());
+        item->setData(DetailsRole, cert.details());
+        item->setData(B64LineRole, cert.base64Line());
+        Qt::CheckState checkedState = mPreviouslyUnselected.contains(cert.base64Line()) ?
+            Qt::Unchecked : Qt::Checked;
         if (cert.isInstallCert()) {
             // This if statements is for testing! @TODO Remove this!
             if (i <= 2) {
-                item->setData(Qt::UserRole + 1, Certificate::InstallOld);
+                item->setData(StatusRole, Certificate::InstallOld);
                 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
-                item->setCheckState(Qt::Checked);
             }
             else {
-                item->setData(Qt::UserRole + 1, Certificate::InstallNew);
+                item->setData(StatusRole, Certificate::InstallNew);
                 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
-                item->setCheckState(Qt::Checked);
             }
             if (i == 3) {
                 QListWidgetItem *sep = new QListWidgetItem("New certificates");
@@ -345,16 +346,17 @@
                 mCertListWidget->addItem(sep);
                 i++;
             }
+            item->setCheckState(checkedState);
         }
         else {
             // This if statements is for testing! @TODO Remove this!
             if (i > 35) {
-                item->setData(Qt::UserRole + 1, Certificate::RemoveNew);
+                item->setData(StatusRole, Certificate::RemoveNew);
                 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
-                item->setCheckState(Qt::Checked);
+                item->setCheckState(checkedState);
             }
             else {
-                item->setData(Qt::UserRole + 1, Certificate::RemoveOld);
+                item->setData(StatusRole, Certificate::RemoveOld);
                 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
             }
         }
@@ -391,7 +393,7 @@
 
 void MainWindow::showDetails(QListWidgetItem *item)
 {
-    QString details = item->data(Qt::UserRole).toString();
+    QString details = item->data(DetailsRole).toString();
     certificateDetails->setPlainText(details);
 }
 
@@ -411,10 +413,10 @@
     for (int i = 0; i < mCertListWidget->count(); i++) {
         QListWidgetItem *item = mCertListWidget->item(i);
         if (item->checkState() == Qt::Checked) {
-            choices << item->data(Qt::UserRole).toString();
+            choices << item->data(B64LineRole).toString();
             continue;
         }
-        QString certLine = item->data(Qt::UserRole).toString();
+        QString certLine = item->data(B64LineRole).toString();
         if (certLine.startsWith("I:")) {
             certLine[0] = 'R';
             choices << certLine;
@@ -440,4 +442,34 @@
         this, SLOT(installerError(const QString &)));
     instWrap->start();
 
+    if (!saveUnselectedCertificates()) {
+        qWarning() << "Failed to save previosly unselected certificates.";
+    }
 }
+
+void MainWindow::loadUnselectedCertificates()
+{
+    mPreviouslyUnselected.clear();
+    mSettings.beginGroup("unselected");
+    QStringList keys = mSettings.allKeys();
+    foreach (const QString &key, keys) {
+        mPreviouslyUnselected << mSettings.value(key, QString()).toString();
+    }
+    mSettings.endGroup();
+}
+
+bool MainWindow::saveUnselectedCertificates()
+{
+    mSettings.beginGroup("unselected");
+    mSettings.remove(""); /* Clears old choices */
+    for (int i = 0; i < mCertListWidget->count(); i++) {
+        QListWidgetItem *item = mCertListWidget->item(i);
+        if (item->checkState() != Qt::Checked) {
+            mSettings.setValue(QString::fromLatin1("cert%1").arg(i),
+                    item->data(B64LineRole).toString());
+        }
+    }
+    mSettings.endGroup();
+    mSettings.sync();
+    return mSettings.status() == QSettings::NoError;
+}

http://wald.intevation.org/projects/trustbridge/