rrenkert@582: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
rrenkert@582:  * Software engineering by Intevation GmbH
rrenkert@582:  *
rrenkert@582:  * This file is Free Software under the GNU GPL (v>=2)
rrenkert@582:  * and comes with ABSOLUTELY NO WARRANTY!
rrenkert@582:  * See LICENSE.txt for details.
rrenkert@582:  */
rrenkert@582: #include "certificateitemwidget.h"
rrenkert@582: 
rrenkert@582: #include <QHBoxLayout>
rrenkert@582: #include <QDebug>
andre@701: #include <QStyleFactory>
andre@1106: #include <QToolButton>
rrenkert@582: 
andre@687: CertificateItemWidget::CertificateItemWidget(QWidget *parent,
andre@687:                                              const Certificate &cert,
andre@687:                                              bool state,
andre@1106:                                              QToolButton *btn) :
andre@687:     QWidget(parent),
andre@1106:     mButton(btn)
rrenkert@582: {
rrenkert@582:     mCertificate = cert;
andre@1109:     mOriginalState = state;
andre@1106:     btn->setCheckable(true);
andre@1109:     btn->setStyleSheet("QToolButton:Checked{"
andre@1109:          "border: 1px solid #8f8f91;"
andre@1109:          "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
andre@1109:                                            "stop: 0 #f6f7fa, stop: 1 #dadbde);"
andre@1109:     "}"
andre@1109:     );
andre@1109:     setState(state);
rrenkert@582:     setupGUI();
rrenkert@582: }
rrenkert@582: 
rrenkert@582: void CertificateItemWidget::setupGUI()
rrenkert@582: {
andre@743:     mLabel = new QLabel;
andre@743: 
andre@786:     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
andre@786: 
emanuel@842:     const QString validity = tr("Validity: %1 until %2").arg(
andre@743:             QLocale::system().toString(mCertificate.validFrom().date(), QLocale::ShortFormat)).arg(
andre@743:             QLocale::system().toString(mCertificate.validTo().date(), QLocale::ShortFormat));
emanuel@842:     const QString fpstring = tr("Fingerprint (SHA1): <code>%1</code>").arg(mCertificate.fingerprint());
emanuel@842:     mLabel->setText(QString::fromLatin1("<big><b>%1</b></big><br/>%2<br/>%3<br/>%4").arg
andre@743:         (mCertificate.subjectCN()).arg(mCertificate.subjectO()).arg(validity).arg
andre@743:         (fpstring));
andre@743:     mLabel->setTextFormat(Qt::RichText);
andre@701: 
emanuel@842:     mLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
andre@780: 
andre@780:     mLabel->setTextInteractionFlags(
andre@780:         Qt::TextSelectableByMouse |
andre@780:         Qt::TextSelectableByKeyboard);
andre@1106:     mButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
andre@1106:     connect(mButton, SIGNAL(toggled (bool)),
andre@1106:         this, SLOT(currentStateChanged(bool)));
rrenkert@582: 
rrenkert@638:     QHBoxLayout *layout = new QHBoxLayout;
andre@1106:     layout->addWidget(mButton);
andre@1106:     mButton->setFixedSize(64, 64);
andre@1106:     mButton->setIconSize(QSize(48, 48));
rrenkert@582:     layout->addWidget(mLabel);
rrenkert@582:     this->setLayout(layout);
rrenkert@582: }
rrenkert@627: 
rrenkert@627: bool CertificateItemWidget::state()
rrenkert@627: {
andre@1106:     if (!mButton->isEnabled()) {
andre@685:         return true;
andre@685:     }
andre@701: 
andre@1109:     return !mButton->isChecked();
rrenkert@627: }
rrenkert@627: 
rrenkert@638: void CertificateItemWidget::setState(bool state)
rrenkert@638: {
andre@1109:     /* The internal state we get here is inverted for Ui reasons the logic
andre@1109:      * is if a certificate is selected for installation the button
andre@1109:      * is disabled (as this is the default) Only those that are
andre@1109:      * unselected have the enabled button. */
andre@1109:     mButton->blockSignals(true); // code did this and not the user
andre@1106:     mButton->setChecked(!state);
andre@1109:     mButton->blockSignals(false);
andre@1109:     if (mButton->isEnabled()) {
andre@1109:         mButton->setToolTip(mButton->property(!state ? "ToolTip_On" :
andre@1109:                     "ToolTip_Off").toString());
andre@1109:     }
rrenkert@638: }
rrenkert@638: 
andre@1108: Certificate CertificateItemWidget::certificate()
rrenkert@627: {
andre@1108:     return mCertificate;
rrenkert@627: }
rrenkert@627: 
andre@1106: void CertificateItemWidget::currentStateChanged(bool state)
rrenkert@627: {
andre@1109:     mButton->setToolTip(mButton->property(state ? "ToolTip_On" :
andre@1109:                 "ToolTip_Off").toString());
andre@1109:     emit stateChanged(state, mCertificate);
rrenkert@627: }