changeset 448:5b4ce22186e9

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 23 Apr 2014 11:57:33 +0200
parents b1b14daf2116 (diff) a71d521612b9 (current diff)
children 023b565a02fc
files
diffstat 3 files changed, 129 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/ui/certificateitemdelegate.h	Tue Apr 22 16:14:18 2014 +0000
+++ b/ui/certificateitemdelegate.h	Wed Apr 23 11:57:33 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:
--- a/ui/mainwindow.cpp	Tue Apr 22 16:14:18 2014 +0000
+++ b/ui/mainwindow.cpp	Wed Apr 23 11:57:33 2014 +0200
@@ -270,7 +270,7 @@
     connect(status, SIGNAL(triggered()), this, SLOT(showStatus()));
     connect(help, SIGNAL(triggered()), this, SLOT(showHelp()));
     connect(about, SIGNAL(triggered()), this, SLOT(showAbout()));
-    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
+    connect(quit, SIGNAL(triggered()), this, SLOT(closeApp()));
     setMenuBar(mMenuBar);
 }
 
@@ -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);
@@ -331,7 +330,7 @@
     connect(installButton, SIGNAL(clicked()), this, SLOT(installCerts()));
     quitButton = new QPushButton(tr("Quit"));
     quitButton->setFixedHeight(20);
-    connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
+    connect(quitButton, SIGNAL(clicked()), this, SLOT(closeApp()));
     bottomLayout->insertStretch(0, 10);
     bottomLayout->addWidget(installButton);
     bottomLayout->setAlignment(installButton, Qt::AlignBottom);
@@ -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;
@@ -578,12 +623,19 @@
     mSettings.remove(""); /* Clears old choices */
     for (int i = 0; i < mCertListWidget->count(); i++) {
         QListWidgetItem *item = mCertListWidget->item(i);
-        if (item->checkState() != Qt::Checked) {
+        if (item->checkState() != Qt::Checked &&
+            (item->flags() & Qt::ItemIsUserCheckable)) {
             mSettings.setValue(QString::fromLatin1("cert%1").arg(i),
-                    item->data(CertificateItemDelegate::B64LineRole).toString());
+                    item->data(CertificateItemDelegate::DataRole).value<Certificate>().base64Line());
         }
     }
     mSettings.endGroup();
     mSettings.sync();
     return mSettings.status() == QSettings::NoError;
 }
+
+void MainWindow::closeApp()
+{
+    saveUnselectedCertificates();
+    qApp->quit();
+}
--- a/ui/mainwindow.h	Tue Apr 22 16:14:18 2014 +0000
+++ b/ui/mainwindow.h	Wed Apr 23 11:57:33 2014 +0200
@@ -100,6 +100,8 @@
      */
     void loadUnselectedCertificates();
 
+    void closeApp();
+
 private:
     /** @brief check the integrity of available files.
      *
@@ -116,6 +118,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 */

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