Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/xml/kgpgkeyselectiondlg.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | kgpgkeyselectiondlg.cpp | ||||
3 | ------------------- | ||||
4 | copyright : (C) 2008 by Thomas Baumgart | ||||
5 | email : ipwizard@users.sourceforge.net | ||||
6 | (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> | ||||
7 | ***************************************************************************/ | ||||
8 | | ||||
9 | /*************************************************************************** | ||||
10 | * * | ||||
11 | * This program is free software; you can redistribute it and/or modify * | ||||
12 | * it under the terms of the GNU General Public License as published by * | ||||
13 | * the Free Software Foundation; either version 2 of the License, or * | ||||
14 | * (at your option) any later version. * | ||||
15 | * * | ||||
16 | ***************************************************************************/ | ||||
17 | | ||||
18 | #include "kgpgkeyselectiondlg.h" | ||||
19 | | ||||
20 | // ---------------------------------------------------------------------------- | ||||
21 | // QT Includes | ||||
22 | #include <QPushButton> | ||||
23 | #include <QDialogButtonBox> | ||||
24 | | ||||
25 | // ---------------------------------------------------------------------------- | ||||
26 | // KDE Includes | ||||
27 | | ||||
28 | // ---------------------------------------------------------------------------- | ||||
29 | // Project Includes | ||||
30 | | ||||
31 | #include <kgpgfile.h> | ||||
32 | #include <ui_kgpgkeyselectiondlg.h> | ||||
33 | | ||||
34 | class KGpgKeySelectionDlgPrivate | ||||
35 | { | ||||
36 | Q_DISABLE_COPY(KGpgKeySelectionDlgPrivate) | ||||
37 | | ||||
38 | public: | ||||
39 | KGpgKeySelectionDlgPrivate() | ||||
40 | : ui(new Ui::KGpgKeySelectionDlg) | ||||
41 | , needCheckList(true) | ||||
42 | , listOk(false) | ||||
43 | , checkCount(0) | ||||
44 | { | ||||
45 | } | ||||
46 | | ||||
47 | ~KGpgKeySelectionDlgPrivate() | ||||
48 | { | ||||
49 | delete ui; | ||||
50 | } | ||||
51 | | ||||
52 | Ui::KGpgKeySelectionDlg* ui; | ||||
53 | bool needCheckList; | ||||
54 | bool listOk; | ||||
55 | int checkCount; | ||||
56 | }; | ||||
57 | | ||||
58 | | ||||
59 | KGpgKeySelectionDlg::KGpgKeySelectionDlg(QWidget *parent) : | ||||
60 | QDialog(parent), | ||||
61 | d_ptr(new KGpgKeySelectionDlgPrivate) | ||||
62 | { | ||||
63 | Q_D(KGpgKeySelectionDlg); | ||||
64 | d->ui->setupUi(this); | ||||
65 | connect(d->ui->m_secretKey, SIGNAL(currentIndexChanged(int)), this, SLOT(slotIdChanged())); | ||||
66 | connect(d->ui->m_listWidget, &KEditListWidget::changed, this, &KGpgKeySelectionDlg::slotIdChanged); | ||||
67 | connect(d->ui->m_listWidget, &KEditListWidget::added, this, &KGpgKeySelectionDlg::slotKeyListChanged); | ||||
68 | connect(d->ui->m_listWidget, &KEditListWidget::removed, this, &KGpgKeySelectionDlg::slotKeyListChanged); | ||||
69 | } | ||||
70 | | ||||
71 | KGpgKeySelectionDlg::~KGpgKeySelectionDlg() | ||||
72 | { | ||||
73 | Q_D(KGpgKeySelectionDlg); | ||||
74 | delete d; | ||||
75 | } | ||||
76 | | ||||
77 | void KGpgKeySelectionDlg::setSecretKeys(const QStringList& keyList, const QString& defaultKey) | ||||
78 | { | ||||
79 | static constexpr char recoveryKeyId[] = "59B0F826D2B08440"; | ||||
80 | | ||||
81 | Q_D(KGpgKeySelectionDlg); | ||||
82 | d->ui->m_secretKey->addItem(i18n("No encryption")); | ||||
83 | | ||||
84 | foreach(auto key, keyList) { | ||||
85 | QStringList fields = key.split(':', QString::SkipEmptyParts); | ||||
86 | if (fields[0] != recoveryKeyId) { | ||||
87 | // replace parenthesis in name field with brackets | ||||
88 | auto name = fields[1]; | ||||
89 | name.replace('(', "["); | ||||
90 | name.replace(')', "]"); | ||||
91 | name = QString("%1 (0x%2)").arg(name).arg(fields[0]); | ||||
92 | d->ui->m_secretKey->addItem(name); | ||||
93 | if (name.contains(defaultKey)) { | ||||
94 | d->ui->m_secretKey->setCurrentText(name); | ||||
95 | } | ||||
96 | } | ||||
97 | } | ||||
98 | } | ||||
99 | | ||||
100 | QString KGpgKeySelectionDlg::secretKey() const | ||||
101 | { | ||||
102 | Q_D(const KGpgKeySelectionDlg); | ||||
103 | const bool enabled = (d->ui->m_secretKey->currentIndex() != 0); | ||||
104 | QString key; | ||||
105 | if (enabled) { | ||||
106 | key = d->ui->m_secretKey->currentText(); | ||||
107 | } | ||||
108 | return key; | ||||
109 | } | ||||
110 | | ||||
111 | void KGpgKeySelectionDlg::setAdditionalKeys(const QStringList& list) | ||||
112 | { | ||||
113 | Q_D(KGpgKeySelectionDlg); | ||||
114 | d->ui->m_listWidget->clear(); | ||||
115 | d->ui->m_listWidget->insertStringList(list); | ||||
116 | slotKeyListChanged(); | ||||
117 | } | ||||
118 | | ||||
119 | QStringList KGpgKeySelectionDlg::additionalKeys() const | ||||
120 | { | ||||
121 | Q_D(const KGpgKeySelectionDlg); | ||||
122 | return d->ui->m_listWidget->items(); | ||||
123 | } | ||||
124 | | ||||
125 | #if 0 | ||||
126 | void KGpgKeySelectionDlg::slotShowHelp() | ||||
127 | { | ||||
128 | QString anchor = m_helpAnchor[m_criteriaTab->currentPage()]; | ||||
129 | if (anchor.isEmpty()) | ||||
130 | anchor = QString("details.search"); | ||||
131 | | ||||
132 | KHelpClient::invokeHelp(anchor); | ||||
133 | } | ||||
134 | #endif | ||||
135 | | ||||
136 | void KGpgKeySelectionDlg::slotKeyListChanged() | ||||
137 | { | ||||
138 | Q_D(KGpgKeySelectionDlg); | ||||
139 | d->needCheckList = true; | ||||
140 | slotIdChanged(); | ||||
141 | } | ||||
142 | | ||||
143 | void KGpgKeySelectionDlg::slotIdChanged() | ||||
144 | { | ||||
145 | Q_D(KGpgKeySelectionDlg); | ||||
146 | // this looks a bit awkward. Here's why: KGPGFile::keyAvailable() starts | ||||
147 | // an external task and processes UI events while it waits for the external | ||||
148 | // process to finish. Thus, the first time we get here, the external process | ||||
149 | // is started and the user may press a second key which calls this routine | ||||
150 | // again. | ||||
151 | // | ||||
152 | // The second invocation is counted, but the check is not started until the | ||||
153 | // first one finishes. Once the external process finishes, we check if we | ||||
154 | // were called in the meantime and restart the check. | ||||
155 | if (++d->checkCount == 1) { | ||||
156 | const bool enabled = (d->ui->m_secretKey->currentIndex() != 0); | ||||
157 | d->ui->m_listWidget->setEnabled(enabled); | ||||
158 | d->ui->m_keyLed->setState(enabled ? KLed::On : KLed::Off); | ||||
159 | while (enabled) { | ||||
160 | // first we check the current edit field if filled | ||||
161 | bool keysOk = true; | ||||
162 | if (!d->ui->m_listWidget->currentText().isEmpty()) { | ||||
163 | keysOk = KGPGFile::keyAvailable(d->ui->m_listWidget->currentText()); | ||||
164 | } | ||||
165 | | ||||
166 | // if it is available, then scan the current list if we need to | ||||
167 | if (keysOk) { | ||||
168 | if (d->needCheckList) { | ||||
169 | QStringList keys = d->ui->m_listWidget->items(); | ||||
170 | QStringList::const_iterator it_s; | ||||
171 | for (it_s = keys.constBegin(); keysOk && it_s != keys.constEnd(); ++it_s) { | ||||
172 | if (!KGPGFile::keyAvailable(*it_s)) | ||||
173 | keysOk = false; | ||||
174 | } | ||||
175 | d->listOk = keysOk; | ||||
176 | d->needCheckList = false; | ||||
177 | | ||||
178 | } else { | ||||
179 | keysOk = d->listOk; | ||||
180 | } | ||||
181 | } | ||||
182 | | ||||
183 | // did we receive some more requests to check? | ||||
184 | if (d->checkCount > 1) { | ||||
185 | d->checkCount = 1; | ||||
186 | continue; | ||||
187 | } | ||||
188 | | ||||
189 | if (!d->ui->m_listWidget->items().isEmpty()) { | ||||
190 | d->ui->m_keyLed->setState(static_cast<KLed::State>(keysOk ? KLed::On : KLed::Off)); | ||||
191 | } else { | ||||
192 | d->ui->m_keyLed->setState(KLed::On); | ||||
193 | } | ||||
194 | break; | ||||
195 | } | ||||
196 | | ||||
197 | --d->checkCount; | ||||
198 | d->ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!enabled || (d->ui->m_keyLed->state() == KLed::On)); | ||||
199 | } | ||||
200 | } |