File indexing completed on 2021-12-21 14:36:55

0001 /*
0002 
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 */
0006 
0007 #include "selectsecretkey.h"
0008 
0009 #include "kgpgsettings.h"
0010 #include "core/images.h"
0011 #include "core/KGpgRootNode.h"
0012 #include "model/kgpgitemmodel.h"
0013 #include "model/selectkeyproxymodel.h"
0014 
0015 #include <KConfigGroup>
0016 #include <KLocalizedString>
0017 
0018 #include <QCheckBox>
0019 #include <QComboBox>
0020 #include <QDialogButtonBox>
0021 #include <QLabel>
0022 #include <QPushButton>
0023 #include <QTableView>
0024 #include <QVBoxLayout>
0025 
0026 using namespace KgpgCore;
0027 
0028 KgpgSelectSecretKey::KgpgSelectSecretKey(QWidget *parent, KGpgItemModel *model, const int countkey, const bool allowLocal, const bool allowTerminal)
0029     : QDialog(parent),
0030     m_localsign(nullptr),
0031     m_terminalsign(nullptr),
0032     m_signtrust(nullptr),
0033     m_proxy(new SelectSecretKeyProxyModel(this))
0034 {
0035     setWindowTitle(i18n("Private Key List"));
0036     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0037     QWidget *mainWidget = new QWidget(this);
0038     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0039     setLayout(mainLayout);
0040     mainLayout->addWidget(mainWidget);
0041     m_okButton = buttonBox->button(QDialogButtonBox::Ok);
0042     m_okButton->setDefault(true);
0043     m_okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0044     connect(buttonBox, &QDialogButtonBox::accepted, this, &KgpgSelectSecretKey::accept);
0045     connect(buttonBox, &QDialogButtonBox::rejected, this, &KgpgSelectSecretKey::reject);
0046     m_okButton->setDefault(true);
0047     QWidget *page = new QWidget(this);
0048 
0049     QLabel *label = new QLabel(i18n("Choose secret key for signing:"), page);
0050 
0051     m_proxy->setKeyModel(model);
0052 
0053     m_keyslist = new QTableView(page);
0054     m_keyslist->setModel(m_proxy);
0055     m_keyslist->setSortingEnabled(true);
0056     m_keyslist->setSelectionBehavior(QAbstractItemView::SelectRows);
0057     m_keyslist->resizeColumnsToContents();
0058 
0059     QVBoxLayout *vbox = new QVBoxLayout(page);
0060     vbox->addWidget(label);
0061     vbox->addWidget(m_keyslist);
0062 
0063     if (countkey > 0) {
0064         QLabel *signchecklabel = new QLabel(i18np("How carefully have you checked that the key really "
0065                         "belongs to the person with whom you wish to communicate:",
0066                         "How carefully have you checked that the %1 keys really "
0067                         "belong to the people with whom you wish to communicate:", countkey), page);
0068         signchecklabel->setWordWrap(true);
0069 
0070         m_signtrust = new QComboBox(page);
0071         m_signtrust->addItem(i18n("I Will Not Answer"));
0072         m_signtrust->addItem(i18n("I Have Not Checked at All"));
0073         m_signtrust->addItem(i18n("I Have Done Casual Checking"));
0074         m_signtrust->addItem(i18n("I Have Done Very Careful Checking"));
0075 
0076         vbox->addWidget(signchecklabel);
0077         vbox->addWidget(m_signtrust);
0078         if (allowLocal){
0079             m_localsign = new QCheckBox(i18n("Local signature (cannot be exported)"), page);
0080             vbox->addWidget(m_localsign);
0081         }
0082         if (allowTerminal && (countkey == 1)) {
0083             m_terminalsign = new QCheckBox(i18n("Do not sign all user id's (open terminal)"), page);
0084             vbox->addWidget(m_terminalsign);
0085         }
0086     }
0087 
0088     KGpgNode *nd = model->getRootNode()->findKey(KGpgSettings::defaultKey());
0089     if (nd != nullptr) {
0090         QModelIndex sidx = model->nodeIndex(nd);
0091         QModelIndex pidx = m_proxy->mapFromSource(sidx);
0092         m_keyslist->selectionModel()->setCurrentIndex(pidx, QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
0093     }
0094 
0095     setMinimumSize(550, 200);
0096     slotSelectionChanged();
0097     mainLayout->addWidget(page);
0098     mainLayout->addWidget(buttonBox);
0099 
0100     connect(m_keyslist->selectionModel(), &QItemSelectionModel::selectionChanged, this, &KgpgSelectSecretKey::slotSelectionChanged);
0101     connect(m_keyslist, &QTableView::doubleClicked, this, &KgpgSelectSecretKey::slotOk);
0102 }
0103 
0104 KgpgSelectSecretKey::~KgpgSelectSecretKey()
0105 {
0106 }
0107 
0108 QString KgpgSelectSecretKey::getKeyID() const
0109 {
0110     if (!m_keyslist->selectionModel()->hasSelection())
0111         return QString();
0112     return m_proxy->nodeForIndex(m_keyslist->selectionModel()->selectedIndexes().at(0))->getId();
0113 }
0114 
0115 QString KgpgSelectSecretKey::getKeyMail() const
0116 {
0117     if (!m_keyslist->selectionModel()->hasSelection())
0118         return QString();
0119     return m_proxy->nodeForIndex(m_keyslist->selectionModel()->selectedIndexes().at(0))->getEmail();
0120 }
0121 
0122 int KgpgSelectSecretKey::getSignTrust() const
0123 {
0124     if (m_signtrust)
0125         return m_signtrust->currentIndex();
0126     return -1;
0127 }
0128 
0129 bool KgpgSelectSecretKey::isLocalSign() const
0130 {
0131     return m_localsign && m_localsign->isChecked();
0132 }
0133 
0134 bool KgpgSelectSecretKey::isTerminalSign() const
0135 {
0136     return m_terminalsign && m_terminalsign->isChecked();
0137 }
0138 
0139 void KgpgSelectSecretKey::slotSelectionChanged()
0140 {
0141     m_okButton->setEnabled(m_keyslist->selectionModel()->hasSelection());
0142 }
0143 
0144 void KgpgSelectSecretKey::slotOk()
0145 {
0146     if (m_keyslist->selectionModel()->hasSelection())
0147         accept();
0148 }