changeset 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 (2014-04-15)
parents 6b78a89c1e17
children 42082e402d3e
files ui/certificateitemdelegate.cpp ui/mainwindow.cpp ui/mainwindow.h
diffstat 3 files changed, 77 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/ui/certificateitemdelegate.cpp	Tue Apr 15 11:04:34 2014 +0200
+++ b/ui/certificateitemdelegate.cpp	Tue Apr 15 11:56:29 2014 +0200
@@ -1,6 +1,7 @@
 #include <QtWidgets>
 
 #include "certificate.h"
+#include "mainwindow.h"
 #include "certificateitemdelegate.h"
 
 void CertificateItemDelegate::paint(QPainter *painter,
@@ -8,7 +9,7 @@
 {
     // Save the current painter.
     painter->save();
-    int status = index.data(Qt::UserRole + 1).toInt();
+    int status = index.data(MainWindow::StatusRole).toInt();
     if (status == 0) {
         // This status is not known, so draw the default item.
         QStyledItemDelegate::paint(painter, option, index);
--- 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;
+}
--- a/ui/mainwindow.h	Tue Apr 15 11:04:34 2014 +0200
+++ b/ui/mainwindow.h	Tue Apr 15 11:56:29 2014 +0200
@@ -46,6 +46,12 @@
         TransferError
     };
 
+    enum ItemRole {
+        DetailsRole = Qt::UserRole, /* The certificate details for the window */
+        StatusRole, /* Certificate status */
+        B64LineRole /* The I:/R: <cert> line */
+    };
+
     CurrentState getState() {return mCurState;}
     void setState(CurrentState state) {mCurState = state;}
 
@@ -67,6 +73,27 @@
     void installerError(const QString& errMsg);
     void installCerts();
 
+    /** @brief saves the currently unselected certificates
+     *
+     * This creates / updates a qsettings section that
+     * [unselected] that contains the certificates that
+     * were unselected previously.
+     *
+     * Unselected are certificates that are unchecked
+     * in the certListWidget
+     *
+     * Returns false on error.
+     */
+    bool saveUnselectedCertificates();
+
+    /** @brief loads previously unselected certificates from settings
+     *
+     * The certificates are strored in the list mPreviouslyUnselected.
+     *
+     * On error mPreviouslyUnselected is empty after this call.
+     */
+    void loadUnselectedCertificates();
+
 private:
     /** @brief check the integrity of available files.
      *
@@ -100,7 +127,11 @@
     CurrentState mCurState;
     QMenuBar *mMenuBar;
 
+    /* The current list that should be installed */
     CertificateList mListToInstall;
+    /* Previously made "unselect" choices in the form of
+     * base64lines with I:/R: prefix */
+    QStringList mPreviouslyUnselected;
 
     QListWidget *mCertListWidget;
     QTextEdit *certificateDetails;

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