/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef CERTIFICATE_HXX
#define CERTIFICATE_HXX

#include <ui_certificate.h>
#include <QtCore/QDateTime>
#include <QtGui/QTreeWidget>
#include <QtGui/QTreeWidgetItem>
#include <QtNetwork/QSslCertificate>

#include <QtCore/QDebug>

class Certificate: public QWidget, protected Ui::Certificate {
  public:
    Certificate(QWidget* p): QWidget(p) {
      setupUi(this);
    }
    Certificate& certificate(const QSslCertificate& cert) {
      _cert->clear();
      if (!cert.isValid()) return *this;
      _cert->addTopLevelItem
        ((new QTreeWidgetItem
          (QStringList()<<tr("Valid Since")
           <<cert.effectiveDate().toString(Qt::SystemLocaleLongDate))));
      _cert->addTopLevelItem
        ((new QTreeWidgetItem
          (QStringList()<<tr("Valid Until")
           <<cert.expiryDate().toString(Qt::SystemLocaleLongDate))));
      if (!cert.subjectInfo(QByteArray("serialNumber")).isEmpty()) {
        _cert->addTopLevelItem
          ((new QTreeWidgetItem
            (QStringList()<<tr("SuisseID Number")
             <<cert.subjectInfo(QByteArray("serialNumber")))));
      }
      _cert->addTopLevelItem
        ((new QTreeWidgetItem
          (QStringList()<<tr("Certificate Serial Number")
           <<cert.serialNumber())));
      // _cert->addTopLevelItem
      //   ((new QTreeWidgetItem
      //     (QStringList()<<tr("Version")
      //      <<cert.version())));
      QTreeWidgetItem *it(0);
      _cert->addTopLevelItem
        ((it = new QTreeWidgetItem(QStringList()<<tr("Subject Info")<<"")));
      for (QSslCertificate::SubjectInfo
             si(QSslCertificate::StateOrProvinceName);
           si>=QSslCertificate::Organization;
           si=(QSslCertificate::SubjectInfo)((int)si-1))
        if (!cert.subjectInfo(si).isEmpty())
          it->addChild
            ((new QTreeWidgetItem
              (QStringList()<<subjectInfo(si)<<cert.subjectInfo(si))));
      QTreeWidgetItem *it2(0);
      it->addChild
        ((it2 = new QTreeWidgetItem
          (QStringList()<<tr("Alternate Subject")<<"")));
      QMultiMap<QSsl::AlternateNameEntryType, QString>
        asns(cert.alternateSubjectNames());
      for (QMultiMap<QSsl::AlternateNameEntryType, QString>::iterator
             asn(asns.begin()); asn!=asns.end(); ++asn)
        it2->addChild
          ((new QTreeWidgetItem
            (QStringList()<<alternateName(asn.key())<<asn.value())));
      _cert->addTopLevelItem
        ((it = new QTreeWidgetItem(QStringList()<<tr("Issuer Info")<<"")));
      for (QSslCertificate::SubjectInfo
             si(QSslCertificate::StateOrProvinceName);
           si>=QSslCertificate::Organization;
           si=(QSslCertificate::SubjectInfo)((int)si-1))
        if (!cert.issuerInfo(si).isEmpty())
          it->addChild
            ((new QTreeWidgetItem
              (QStringList()<<subjectInfo(si)<<cert.issuerInfo(si))));
      _cert->expandAll();
      _cert->resizeColumnToContents(0);
      _cert->resizeColumnToContents(1);
      return *this;
    }
    QString alternateName(QSsl::AlternateNameEntryType an) {
      switch (an) {
        case QSsl::EmailEntry: return tr("E-Mail");
        case QSsl::DnsEntry: return tr("URL");
      }
      return tr("error", "unknown certificate subject alternate name");
    }
    QString subjectInfo(QSslCertificate::SubjectInfo si) {
      switch (si) {
        case QSslCertificate::Organization:
          return tr("Organization");
        case QSslCertificate::CommonName:
          return tr("Common Name");
        case QSslCertificate::LocalityName:
          return tr("Locality");
        case QSslCertificate::OrganizationalUnitName:
          return tr("Organizational Unit");
        case QSslCertificate::CountryName:
          return tr("Country");
        case QSslCertificate::StateOrProvinceName:
          return tr("State or Province");
      }
      return tr("error", "unknown certificate subject info");
    }
};

#endif
