File indexing completed on 2022-09-27 16:29:24

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2006 Petri Damsten <damu@iki.fi>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "password.h"
0008 
0009 #ifdef HAVE_LIBGPGME
0010 
0011 #include <QDialogButtonBox>
0012 #include <QHBoxLayout>
0013 #include <QLocale>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 
0017 #include <KConfigGroup>
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 
0021 #include "basketscene.h"
0022 #include "kgpgme.h"
0023 
0024 PasswordDlg::PasswordDlg(QWidget *parent)
0025     : QDialog(parent)
0026     , w(0)
0027 {
0028     // QDialog options
0029     setWindowTitle(i18n("Password Protection"));
0030     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0031     QWidget *mainWidget = new QWidget(this);
0032     QVBoxLayout *mainLayout = new QVBoxLayout;
0033     setLayout(mainLayout);
0034     mainLayout->addWidget(mainWidget);
0035     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0036     okButton->setDefault(true);
0037     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0038     connect(buttonBox, &QDialogButtonBox::accepted, this, &PasswordDlg::accept);
0039     connect(buttonBox, &QDialogButtonBox::rejected, this, &PasswordDlg::reject);
0040     mainLayout->addWidget(buttonBox);
0041     okButton->setDefault(true);
0042     setModal(true);
0043 
0044     QHBoxLayout *toplayout = new QHBoxLayout(mainWidget);
0045     w = new Password;
0046     toplayout->addWidget(w, 1);
0047 }
0048 
0049 PasswordDlg::~PasswordDlg()
0050 {
0051     delete w;
0052 }
0053 
0054 void PasswordDlg::accept()
0055 {
0056     int n = type();
0057     if (n == BasketScene::PrivateKeyEncryption && key().isEmpty())
0058         KMessageBox::error(w, i18n("No private key selected."));
0059     else
0060         QDialog::accept();
0061 }
0062 
0063 QString PasswordDlg::key() const
0064 {
0065     QString s = w->keyCombo->currentText();
0066     if (s.length() < 16)
0067         return QString();
0068     int n = s.lastIndexOf(' ');
0069     if (n < 0)
0070         return QString();
0071     return s.mid(n + 1);
0072 }
0073 
0074 int PasswordDlg::type() const
0075 {
0076     if (w->noPasswordRadioButton->isChecked())
0077         return BasketScene::NoEncryption;
0078     else if (w->passwordRadioButton->isChecked())
0079         return BasketScene::PasswordEncryption;
0080     else if (w->publicPrivateRadioButton->isChecked())
0081         return BasketScene::PrivateKeyEncryption;
0082     return -1;
0083 }
0084 
0085 void PasswordDlg::setKey(const QString &key)
0086 {
0087     for (int i = 0; i < w->keyCombo->count(); ++i) {
0088         if (w->keyCombo->itemText(i).contains(key)) {
0089             w->keyCombo->setCurrentIndex(i);
0090             return;
0091         }
0092     }
0093 }
0094 
0095 void PasswordDlg::setType(int type)
0096 {
0097     if (type == BasketScene::NoEncryption)
0098         w->noPasswordRadioButton->setChecked(true);
0099     else if (type == BasketScene::PasswordEncryption)
0100         w->passwordRadioButton->setChecked(true);
0101     else if (type == BasketScene::PrivateKeyEncryption)
0102         w->publicPrivateRadioButton->setChecked(true);
0103 }
0104 
0105 Password::Password(QWidget *parent)
0106     : QWidget(parent)
0107 {
0108     // Setup from the UI file
0109     setupUi(this);
0110 
0111     KGpgMe gpg;
0112 
0113     KGpgKeyList list = gpg.keys(true);
0114     for (KGpgKeyList::iterator it = list.begin(); it != list.end(); ++it) {
0115         QString name = gpg.checkForUtf8((*it).name);
0116 
0117         keyCombo->addItem(QString("%1 <%2> %3").arg(name).arg((*it).email).arg((*it).id));
0118     }
0119     publicPrivateRadioButton->setEnabled(keyCombo->count() > 0);
0120     keyCombo->setEnabled(keyCombo->count() > 0);
0121 }
0122 
0123 Password::~Password()
0124 {
0125 }
0126 
0127 #endif