diff ui/mainwindow.cpp @ 445:b3721ded6f5b

Sort and filter the certificate list, changed certificate item roles.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 23 Apr 2014 11:53:57 +0200
parents 4b486a14252f
children 43090637d6ae
line wrap: on
line diff
--- a/ui/mainwindow.cpp	Tue Apr 22 16:46:47 2014 +0200
+++ b/ui/mainwindow.cpp	Wed Apr 23 11:53:57 2014 +0200
@@ -294,7 +294,6 @@
     // The certificate list
     QGroupBox *certBox = new QGroupBox(tr("Managed Certificates"));
     mCertListWidget = new QListWidget;
-    mCertListWidget->setItemDelegate(new CertificateItemDelegate);
     connect(mCertListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
         this, SLOT(showDetails(QListWidgetItem*)));
     certLayout->addWidget(mCertListWidget);
@@ -395,57 +394,103 @@
 
     /* TODO: if nothing is available (neither old nor new) add some progress
      * indication */
+    QList<Certificate> newInstallCerts;
+    QList<Certificate> newRemoveCerts;
+    QList<Certificate> oldInstallCerts;
+    QList<Certificate> oldRemoveCerts;
 
-    foreach (const Certificate &cert, mListToInstall.isValid() ?
-                                      mListToInstall.getCertificates() :
-                                      mInstalledList.getCertificates()) {
-        if (!cert.isValid()) {
-            qWarning() << "Invalid certificate in list";
-            continue;
-        }
-        QListWidgetItem* item = new QListWidgetItem(cert.shortDescription());
-        SeparatorItemDelegate *separator = new SeparatorItemDelegate();
-        item->setData(CertificateItemDelegate::DetailsRole, QVariant::fromValue(cert));
-        Qt::CheckState checkedState = mPreviouslyUnselected.contains(cert.base64Line()) ?
-            Qt::Unchecked : Qt::Checked;
-
-        bool isOld = mInstalledList.getCertificates().contains(cert);
-        qDebug() << "Found old certificate.";
-        /* TODO properly work with that information. */
-
-        if (cert.isInstallCert()) {
-            // This if statements is for testing! @TODO Remove this!
-            if (isOld) {
-                item->setData(CertificateItemDelegate::StatusRole, Certificate::InstallOld);
-                item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
+    if (mListToInstall.getCertificates().isEmpty()) {
+        // No new list available, add old certificates.
+        foreach (const Certificate &cert, mInstalledList.getCertificates()) {
+            if (cert.isInstallCert()) {
+                oldInstallCerts.append(cert);
             }
             else {
-                item->setData(CertificateItemDelegate::StatusRole, Certificate::InstallNew);
-                item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
+                oldRemoveCerts.append(cert);
             }
-            if (i == 3) {
-                QListWidgetItem *sep = new QListWidgetItem("New certificates");
-                mCertListWidget->setItemDelegateForRow(i, separator);
-                mCertListWidget->addItem(sep);
-                i++;
-            }
-            item->setCheckState(checkedState);
         }
-        else {
-            // This if statements is for testing! @TODO Remove this!
-            if (i > 35) {
-                item->setData(CertificateItemDelegate::StatusRole, Certificate::RemoveNew);
-                item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
-                item->setCheckState(checkedState);
+    }
+    else {
+        // Sort and filter both lists.
+        foreach (const Certificate &cert, mListToInstall.getCertificates()) {
+            if (cert.isInstallCert()) {
+                // Certificate with status "install".
+                if (mInstalledList.getCertificates().contains(cert)) {
+                    // Was in the old list.
+                    oldInstallCerts.append(cert);
+                }
+                else {
+                    // Is a brand new certificate
+                    newInstallCerts.append(cert);
+                }
             }
             else {
-                item->setData(CertificateItemDelegate::StatusRole, Certificate::RemoveOld);
-                item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
+                // Certificate with status "remove".
+                if (mInstalledList.getCertificates().contains(cert)) {
+                    // Was in the old list.
+                    oldRemoveCerts.append(cert);
+                }
+                else {
+                    // Was in the old list with status "install" and now has the
+                    // status "remove".
+                    newRemoveCerts.append(cert);
+                }
             }
         }
-        mCertListWidget->addItem(item);
-        i++;
     }
+
+    // Add separators and certificates to list widget.
+    mCertListWidget->addItem(createSeparator(tr("New certificates to install"), i++));
+    foreach (const Certificate &cert, newInstallCerts) {
+        mCertListWidget->addItem(createListItem(cert, Certificate::InstallNew, i++));
+    }
+
+    mCertListWidget->addItem(createSeparator(tr("New certificates to remove"), i++));
+    foreach (const Certificate &cert, newRemoveCerts) {
+        mCertListWidget->addItem(createListItem(cert, Certificate::RemoveNew, i++));
+    }
+
+    mCertListWidget->addItem(createSeparator(tr("Old certificates to install"), i++));
+    foreach (const Certificate &cert, oldInstallCerts) {
+        mCertListWidget->addItem(createListItem(cert, Certificate::InstallOld, i++));
+    }
+
+    mCertListWidget->addItem(createSeparator(tr("Old certificates to remove"), i++));
+    foreach (const Certificate &cert, oldRemoveCerts) {
+        mCertListWidget->addItem(createListItem(cert, Certificate::RemoveOld, i++));
+    }
+}
+
+QListWidgetItem* MainWindow::createSeparator(const QString &text, int index)
+{
+    SeparatorItemDelegate *separatorDelegate = new SeparatorItemDelegate();
+    QListWidgetItem *separator = new QListWidgetItem(text);
+    mCertListWidget->setItemDelegateForRow(index, separatorDelegate);
+    separator->setFlags(separator->flags() ^ Qt::ItemIsUserCheckable);
+    return separator;
+}
+
+QListWidgetItem* MainWindow::createListItem(const Certificate &certificate,
+    Certificate::Status status, int index)
+{
+    CertificateItemDelegate *certDelegate = new CertificateItemDelegate();
+    QListWidgetItem* item = new QListWidgetItem(certificate.shortDescription());
+    item->setData(CertificateItemDelegate::DataRole,
+        QVariant::fromValue(certificate));
+    item->setData(CertificateItemDelegate::StatusRole, status);
+    if (!mPreviouslyUnselected.contains(certificate.base64Line()) &&
+        status == Certificate::RemoveOld) {
+        item->setFlags(item->flags() ^ Qt::ItemIsUserCheckable);
+    }
+    else {
+        Qt::CheckState checkedState =
+            mPreviouslyUnselected.contains(certificate.base64Line()) ?
+                Qt::Unchecked : Qt::Checked;
+        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
+        item->setCheckState(checkedState);
+    }
+    mCertListWidget->setItemDelegateForRow(index, certDelegate);
+    return item;
 }
 
 void MainWindow::showSettings()
@@ -476,7 +521,7 @@
 
 void MainWindow::showDetails(QListWidgetItem *item)
 {
-    Certificate cert = item->data(CertificateItemDelegate::DetailsRole).value<Certificate>();
+    Certificate cert = item->data(CertificateItemDelegate::DataRole).value<Certificate>();
     mSubjectCN->setText(cert.subjectCN());
     mSubjectOU->setText(cert.subjectOU());
     mIssuerCN->setText(cert.issuerCN());
@@ -523,10 +568,10 @@
     for (int i = 0; i < mCertListWidget->count(); i++) {
         QListWidgetItem *item = mCertListWidget->item(i);
         if (item->checkState() == Qt::Checked) {
-            choices << item->data(CertificateItemDelegate::B64LineRole).toString();
+            choices << item->data(CertificateItemDelegate::DataRole).value<Certificate>().base64Line();
             continue;
         }
-        QString certLine = item->data(CertificateItemDelegate::B64LineRole).toString();
+        QString certLine = item->data(CertificateItemDelegate::DataRole).value<Certificate>().base64Line();
         if (certLine.startsWith("I:")) {
             certLine[0] = 'R';
             choices << certLine;
@@ -580,7 +625,7 @@
         QListWidgetItem *item = mCertListWidget->item(i);
         if (item->checkState() != Qt::Checked) {
             mSettings.setValue(QString::fromLatin1("cert%1").arg(i),
-                    item->data(CertificateItemDelegate::B64LineRole).toString());
+                    item->data(CertificateItemDelegate::DataRole).value<Certificate>().base64Line());
         }
     }
     mSettings.endGroup();

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