[PATCH 1 of 4] Sort and filter the certificate list, changed certificate item roles

Wald Commits scm-commit at wald.intevation.org
Wed Apr 23 11:57:41 CEST 2014


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1398246837 -7200
# Node ID b3721ded6f5bf23bd13f33943f5687220cf9513c
# Parent  20f539866fa8a34c24d51244401c3ef3a3bff739
Sort and filter the certificate list, changed certificate item roles.

diff -r 20f539866fa8 -r b3721ded6f5b ui/certificateitemdelegate.h
--- a/ui/certificateitemdelegate.h	Tue Apr 22 16:46:47 2014 +0200
+++ b/ui/certificateitemdelegate.h	Wed Apr 23 11:53:57 2014 +0200
@@ -36,9 +36,8 @@
 
     /** @brief different roles for this tiem */
     enum ItemRole {
-        DetailsRole = Qt::UserRole, /* The certificate details for the window */
+        DataRole = Qt::UserRole, /* The certificate details for the window */
         StatusRole, /* Certificate status */
-        B64LineRole /* The I:/R: <cert> line */
     };
 
 private:
diff -r 20f539866fa8 -r b3721ded6f5b ui/mainwindow.cpp
--- 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();
diff -r 20f539866fa8 -r b3721ded6f5b ui/mainwindow.h
--- a/ui/mainwindow.h	Tue Apr 22 16:46:47 2014 +0200
+++ b/ui/mainwindow.h	Wed Apr 23 11:53:57 2014 +0200
@@ -116,6 +116,31 @@
     void createContent();
     void loadCertificateList();
 
+    /** @brief Create a separator item for the certificate list.
+     *
+     * The item uses a SeparatorItemDelegate for layout and styling at the given
+     * index.
+     *
+     * @param[in] text  The text for the item.
+     * @param[in] index The index of the item.
+     *
+     * @return The new separator item.
+     */
+    QListWidgetItem* createSeparator(const QString &text, int index);
+
+    /** @brief Create a certificate list item for the list.
+     *
+     * The item uses a CertificateItemDelegate for layout and styling.
+     *
+     * @param[in] text   The certificate to display.
+     * @param[in] status The certificate status.
+     * @param[in] index  The index of the item.
+     *
+     * @return The new separator item.
+     */
+    QListWidgetItem* createListItem(const Certificate &certificate,
+        Certificate::Status status, int index);
+
     /* Are we running in tray mode ?*/
     const bool mTrayMode;
     /* The message currently shown at intervals */


More information about the Trustbridge-commits mailing list