File indexing completed on 2024-04-14 05:43:37

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 QString KgpgSelectSecretKey::getKeyID() const
0105 {
0106     if (!m_keyslist->selectionModel()->hasSelection())
0107         return QString();
0108     return m_proxy->nodeForIndex(m_keyslist->selectionModel()->selectedIndexes().at(0))->getId();
0109 }
0110 
0111 QString KgpgSelectSecretKey::getKeyMail() const
0112 {
0113     if (!m_keyslist->selectionModel()->hasSelection())
0114         return QString();
0115     return m_proxy->nodeForIndex(m_keyslist->selectionModel()->selectedIndexes().at(0))->getEmail();
0116 }
0117 
0118 int KgpgSelectSecretKey::getSignTrust() const
0119 {
0120     if (m_signtrust)
0121         return m_signtrust->currentIndex();
0122     return -1;
0123 }
0124 
0125 bool KgpgSelectSecretKey::isLocalSign() const
0126 {
0127     return m_localsign && m_localsign->isChecked();
0128 }
0129 
0130 bool KgpgSelectSecretKey::isTerminalSign() const
0131 {
0132     return m_terminalsign && m_terminalsign->isChecked();
0133 }
0134 
0135 void KgpgSelectSecretKey::slotSelectionChanged()
0136 {
0137     m_okButton->setEnabled(m_keyslist->selectionModel()->hasSelection());
0138 }
0139 
0140 void KgpgSelectSecretKey::slotOk()
0141 {
0142     if (m_keyslist->selectionModel()->hasSelection())
0143         accept();
0144 }