2929#include " LdapSearch.h"
3030#include " QSigner.h"
3131#include " Settings.h"
32- #include " Styles.h"
3332#include " TokenData.h"
3433#include " dialogs/WarningDialog.h"
3534#include " effects/Overlay.h"
3635
3736#include < QtCore/QDateTime>
38- #include < QtCore/QDebug>
3937#include < QtCore/QJsonArray>
4038#include < QtCore/QJsonObject>
4139#include < QtNetwork/QSslConfiguration>
@@ -56,22 +54,11 @@ AddRecipients::AddRecipients(ItemList* itemList, QWidget *parent)
5654 new Overlay (this );
5755
5856 ui->leftPane ->init (ria::qdigidoc4::ToAddAdresses, QT_TRANSLATE_NOOP (" ItemList" , " Add recipients" ));
59- ui->leftPane ->setFont (Styles::font (Styles::Regular, 20 ));
6057 ui->rightPane ->init (ria::qdigidoc4::AddedAdresses, QT_TRANSLATE_NOOP (" ItemList" , " Added recipients" ));
61- ui->rightPane ->setFont (Styles::font (Styles::Regular, 20 ));
6258
63- ui->fromCard ->setFont (Styles::font (Styles::Condensed, 12 ));
64- ui->fromFile ->setFont (Styles::font (Styles::Condensed, 12 ));
65- ui->fromHistory ->setFont (Styles::font (Styles::Condensed, 12 ));
66-
67- ui->cancel ->setFont (Styles::font (Styles::Condensed, 14 ));
68- ui->confirm ->setFont (Styles::font (Styles::Condensed, 14 ));
69-
70- ui->confirm ->setDisabled (rightList.isEmpty ());
7159 connect (ui->confirm , &QPushButton::clicked, this , &AddRecipients::accept);
7260 connect (ui->cancel , &QPushButton::clicked, this , &AddRecipients::reject);
7361 connect (ui->leftPane , &ItemList::search, this , [&](const QString &term) {
74- leftList.clear ();
7562 ui->leftPane ->clear ();
7663 search (term);
7764 });
@@ -81,18 +68,26 @@ AddRecipients::AddRecipients(ItemList* itemList, QWidget *parent)
8168 connect (ldap_corp, &LdapSearch::error, this , &AddRecipients::showError);
8269 connect (this , &AddRecipients::finished, this , &AddRecipients::close);
8370
84- connect (ui->leftPane , &ItemList::addAll, this , &AddRecipients::addAllRecipientToRightPane );
71+ connect (ui->leftPane , &ItemList::addAll, this , [this ] {
72+ for (Item *item: ui->leftPane ->items )
73+ addRecipientToRightPane (item);
74+ });
8575 connect (ui->rightPane , &ItemList::removed, ui->rightPane , &ItemList::removeItem );
8676
87- connect (ui->fromCard , &QPushButton::clicked, this , &AddRecipients::addRecipientFromCard);
88- connect ( qApp->signer (), &QSigner::authDataChanged, this , &AddRecipients::enableRecipientFromCard );
77+ connect (ui->fromCard , &QPushButton::clicked, this , [this ] {
78+ addRecipient (qApp->signer ()->tokenauth ().cert ());
79+ });
80+ auto enableRecipientFromCard = [this ] {
81+ ui->fromCard ->setDisabled (qApp->signer ()->tokenauth ().cert ().isNull ());
82+ };
8983 enableRecipientFromCard ();
84+ connect (qApp->signer (), &QSigner::authDataChanged, this , std::move (enableRecipientFromCard));
9085
9186 connect (ui->fromFile , &QPushButton::clicked, this , &AddRecipients::addRecipientFromFile);
9287 connect (ui->fromHistory , &QPushButton::clicked, this , &AddRecipients::addRecipientFromHistory);
9388
9489 for (Item *item: itemList->items )
95- addRecipientToRightPane ((qobject_cast<AddressItem *>( item))-> getKey () , false );
90+ addRecipientToRightPane (item, false );
9691}
9792
9893AddRecipients::~AddRecipients ()
@@ -101,25 +96,6 @@ AddRecipients::~AddRecipients()
10196 delete ui;
10297}
10398
104- void AddRecipients::addAllRecipientToRightPane ()
105- {
106- QList<SslCertificate> history;
107- for (AddressItem *value: leftList)
108- {
109- if (rightList.contains (value->getKey ()))
110- continue ;
111- addRecipientToRightPane (value);
112- history.append (value->getKey ().cert );
113- }
114- ui->confirm ->setDisabled (rightList.isEmpty ());
115- historyCertData.addAndSave (history);
116- }
117-
118- void AddRecipients::addRecipientFromCard ()
119- {
120- if (auto *item = addRecipientToLeftPane (qApp->signer ()->tokenauth ().cert ()))
121- addRecipientToRightPane (item, true );
122- }
12399
124100void AddRecipients::addRecipientFromFile ()
125101{
@@ -150,63 +126,67 @@ void AddRecipients::addRecipientFromFile()
150126 {
151127 WarningDialog::show (this , tr (" This certificate cannot be used for encryption" ));
152128 }
153- else if (auto *item = addRecipientToLeftPane (cert))
154- {
155- addRecipientToRightPane (item, true );
156- }
129+ else
130+ addRecipient (cert);
157131}
158132
159133void AddRecipients::addRecipientFromHistory ()
160134{
161135 auto *dlg = new CertificateHistory (historyCertData, this );
162- connect (dlg, &CertificateHistory::addSelectedCerts, this , &AddRecipients::addSelectedCerts);
136+ connect (dlg, &CertificateHistory::addSelectedCerts, this , [this ](const QList<HistoryCertData> &selectedCertData) {
137+ if (selectedCertData.isEmpty ())
138+ return ;
139+
140+ ui->leftPane ->clear ();
141+ for (const HistoryCertData &certData: selectedCertData) {
142+ QString term = (certData.type == QStringLiteral (" 1" ) || certData.type == QStringLiteral (" 3" )) ? certData.CN : certData.CN .split (' ,' ).value (2 );
143+ search (term, true , certData.type );
144+ }
145+ });
163146 dlg->open ();
164147}
165148
166- AddressItem * AddRecipients::addRecipientToLeftPane (const QSslCertificate& cert)
149+ void AddRecipients::addRecipient (const QSslCertificate& cert, bool select )
167150{
168- AddressItem *leftItem = leftList.value (cert);
169- if (leftItem)
170- return leftItem;
171-
172- leftItem = new AddressItem (CKey (cert), ui->leftPane );
173- leftList.insert (cert, leftItem);
174- ui->leftPane ->addWidget (leftItem);
175- bool contains = rightList.contains (cert);
176- leftItem->setDisabled (contains);
177- leftItem->showButton (contains ? AddressItem::Added : AddressItem::Add);
178-
179- connect (leftItem, &AddressItem::add, this , [this ](Item *item) {
180- addRecipientToRightPane (qobject_cast<AddressItem*>(item), true );
181- });
182-
183- if (auto *add = ui->leftPane ->findChild <QWidget*>(QStringLiteral (" add" )))
184- add->setVisible (true );
151+ AddressItem *leftItem = itemListValue (ui->leftPane , cert);
152+ if (!leftItem)
153+ {
154+ leftItem = new AddressItem (cert, AddressItem::Add, ui->leftPane );
155+ ui->leftPane ->addWidget (leftItem);
156+ bool contains = rightList.contains (cert);
157+ leftItem->setDisabled (contains);
158+ connect (leftItem, &AddressItem::add, this , [this ](Item *item) { addRecipientToRightPane (item); });
159+ if (auto *add = ui->leftPane ->findChild <QWidget*>(QStringLiteral (" add" )))
160+ add->setVisible (true );
161+ }
185162
186- return leftItem;
163+ if (select)
164+ addRecipientToRightPane (leftItem);
187165}
188166
189- bool AddRecipients::addRecipientToRightPane (const CKey &key , bool update)
167+ void AddRecipients::addRecipientToRightPane (Item *item , bool update)
190168{
191- if (rightList.contains (key))
192- return false ;
169+ auto *address = qobject_cast<AddressItem*>(item);
170+ if (!address || rightList.contains (address->getKey ()))
171+ return ;
193172
173+ const auto &key = address->getKey ();
194174 if (update)
195175 {
196176 if (auto expiryDate = key.cert .expiryDate (); expiryDate <= QDateTime::currentDateTime ())
197177 {
198178 if (Settings::CDOC2_DEFAULT && Settings::CDOC2_USE_KEYSERVER)
199179 {
200180 WarningDialog::show (this , tr (" Failed to add certificate. An expired certificate cannot be used for encryption." ));
201- return false ;
181+ return ;
202182 }
203183 auto *dlg = new WarningDialog (tr (" Are you sure that you want use certificate for encrypting, which expired on %1?<br />"
204184 " When decrypter has updated certificates then decrypting is impossible." )
205185 .arg (expiryDate.toString (QStringLiteral (" dd.MM.yyyy hh:mm:ss" ))), this );
206186 dlg->setCancelText (WarningDialog::NO);
207187 dlg->addButton (WarningDialog::YES, QMessageBox::Yes);
208188 if (dlg->exec () != QMessageBox::Yes)
209- return false ;
189+ return ;
210190 }
211191 QSslConfiguration backup = QSslConfiguration::defaultConfiguration ();
212192 QSslConfiguration::setDefaultConfiguration (CheckConnection::sslConfiguration ());
@@ -219,55 +199,47 @@ bool AddRecipients::addRecipientToRightPane(const CKey &key, bool update)
219199 dlg->setCancelText (WarningDialog::NO);
220200 dlg->addButton (WarningDialog::YES, QMessageBox::Yes);
221201 if (dlg->exec () != QMessageBox::Yes)
222- return false ;
202+ return ;
223203 }
224204 }
225205 updated = update;
226206
227207 rightList.append (key);
228208
229- auto *rightItem = new AddressItem (key, ui->rightPane );
230- connect (rightItem, &AddressItem::remove, this , &AddRecipients::removeRecipientFromRightPane);
209+ auto *rightItem = new AddressItem (key, AddressItem::Remove, ui->rightPane );
210+ connect (rightItem, &AddressItem::remove, this , [this ](Item *item) {
211+ auto *rightItem = qobject_cast<AddressItem*>(item);
212+ if (auto *leftItem = itemListValue (ui->leftPane , rightItem->getKey ().cert ))
213+ leftItem->setDisabled (false );
214+ rightList.removeAll (rightItem->getKey ());
215+ updated = true ;
216+ ui->confirm ->setDisabled (rightList.isEmpty ());
217+ });
231218 ui->rightPane ->addWidget (rightItem);
232- ui->confirm ->setDisabled (rightList.isEmpty ());
233- historyCertData.addAndSave ({key.cert });
234- return true ;
235- }
236-
237- void AddRecipients::addRecipientToRightPane (AddressItem *leftItem, bool update)
238- {
239- if (addRecipientToRightPane (leftItem->getKey (), update)) {
219+ ui->confirm ->setEnabled (true );
220+ historyCertData.addAndSave (key.cert );
221+ if (auto *leftItem = itemListValue (ui->leftPane , key))
240222 leftItem->setDisabled (true );
241- leftItem->showButton (AddressItem::Added);
242- }
243- }
244-
245- void AddRecipients::addSelectedCerts (const QList<HistoryCertData>& selectedCertData)
246- {
247- if (selectedCertData.isEmpty ())
248- return ;
249-
250- leftList.clear ();
251- ui->leftPane ->clear ();
252- for (const HistoryCertData &certData: selectedCertData) {
253- QString term = (certData.type == QStringLiteral (" 1" ) || certData.type == QStringLiteral (" 3" )) ? certData.CN : certData.CN .split (' ,' ).value (2 );
254- search (term, true , certData.type );
255- }
256223}
257224
258225QString AddRecipients::defaultUrl (QLatin1String key, const QString &defaultValue)
259226{
260227 return Application::confValue (key).toString (defaultValue);
261228}
262229
263- void AddRecipients::enableRecipientFromCard ()
230+ bool AddRecipients::isUpdated () const
264231{
265- ui-> fromCard -> setDisabled ( qApp-> signer ()-> tokenauth (). cert (). isNull () ) ;
232+ return updated ;
266233}
267234
268- bool AddRecipients::isUpdated () const
235+ AddressItem* AddRecipients::itemListValue (ItemList *list, const CKey &cert)
269236{
270- return updated;
237+ for (auto *item: list->items )
238+ {
239+ if (auto *address = qobject_cast<AddressItem*>(item); address && address->getKey () == cert)
240+ return address;
241+ }
242+ return nullptr ;
271243}
272244
273245QList<CKey> AddRecipients::keys ()
@@ -281,24 +253,9 @@ QList<CKey> AddRecipients::keys()
281253 return recipients;
282254}
283255
284- void AddRecipients::removeRecipientFromRightPane (Item *toRemove)
285- {
286- auto *rightItem = qobject_cast<AddressItem*>(toRemove);
287- if (auto it = leftList.find (rightItem->getKey ().cert ); it != leftList.end ())
288- {
289- it.value ()->setDisabled (false );
290- it.value ()->showButton (AddressItem::Add);
291- }
292- rightList.removeAll (rightItem->getKey ());
293- updated = true ;
294- ui->confirm ->setDisabled (rightList.isEmpty ());
295- }
296-
297256void AddRecipients::search (const QString &term, bool select, const QString &type)
298257{
299258 QApplication::setOverrideCursor (Qt::WaitCursor);
300- ui->confirm ->setDefault (false );
301- ui->confirm ->setAutoDefault (false );
302259
303260 QVariantMap userData {
304261 {QStringLiteral (" type" ), type},
@@ -318,25 +275,19 @@ void AddRecipients::search(const QString &term, bool select, const QString &type
318275#endif
319276 bool isDigit = false ;
320277 void (cleanTerm.toULongLong (&isDigit));
321- if (isDigit && (cleanTerm.size () == 11 || cleanTerm.size () == 8 ))
278+ if (!isDigit || (cleanTerm.size () != 11 && cleanTerm.size () != 8 ))
279+ ldap_corp->search (QStringLiteral (" (cn=*%1*)" ).arg (cleanTerm), userData);
280+ else if (cleanTerm.size () == 8 )
281+ ldap_corp->search (QStringLiteral (" (serialNumber=%1)" ).arg (cleanTerm), userData);
282+ else if (IKValidator::isValid (cleanTerm))
322283 {
323- if (cleanTerm.size () == 11 )
324- {
325- if (!IKValidator::isValid (cleanTerm))
326- {
327- QApplication::restoreOverrideCursor ();
328- WarningDialog::show (this , tr (" Personal code is not valid!" ));
329- return ;
330- }
331- userData[QStringLiteral (" personSearch" )] = true ;
332- ldap_person->search (QStringLiteral (" (serialNumber=%1%2)" ).arg (ldap_person->isSSL () ? QStringLiteral (" PNOEE-" ) : QString (), cleanTerm), userData);
333- }
334- else
335- ldap_corp->search (QStringLiteral (" (serialNumber=%1)" ).arg (cleanTerm), userData);
284+ userData[QStringLiteral (" personSearch" )] = true ;
285+ ldap_person->search (QStringLiteral (" (serialNumber=%1%2)" ).arg (ldap_person->isSSL () ? QStringLiteral (" PNOEE-" ) : QString (), cleanTerm), userData);
336286 }
337287 else
338288 {
339- ldap_corp->search (QStringLiteral (" (cn=*%1*)" ).arg (cleanTerm), userData);
289+ QApplication::restoreOverrideCursor ();
290+ WarningDialog::show (this , tr (" Personal code is not valid!" ));
340291 }
341292}
342293
@@ -348,7 +299,6 @@ void AddRecipients::showError( const QString &msg, const QString &details )
348299
349300void AddRecipients::showResult (const QList<QSslCertificate> &result, int resultCount, const QVariantMap &userData)
350301{
351- bool isEmpty = true ;
352302 for (const QSslCertificate &k: result)
353303 {
354304 SslCertificate c (k);
@@ -358,16 +308,13 @@ void AddRecipients::showResult(const QList<QSslCertificate> &result, int resultC
358308 (userData.value (QStringLiteral (" personSearch" ), false ).toBool () || !c.enhancedKeyUsage ().contains (SslCertificate::ClientAuth)) &&
359309 c.type () != SslCertificate::MobileIDType)
360310 {
361- isEmpty = false ;
362- AddressItem *item = addRecipientToLeftPane (k);
363- if (userData.value (QStringLiteral (" select" ), false ).toBool () &&
364- (userData.value (QStringLiteral (" type" )).isNull () || HistoryCertData::toType (SslCertificate (k)) == userData[QStringLiteral (" type" )]))
365- addRecipientToRightPane (item, true );
311+ addRecipient (k, userData.value (QStringLiteral (" select" ), false ).toBool () &&
312+ (userData.value (QStringLiteral (" type" )).isNull () || HistoryCertData::toType (SslCertificate (k)) == userData[QStringLiteral (" type" )]));
366313 }
367314 }
368315 if (resultCount >= 50 )
369316 showError (tr (" The name you were looking for gave too many results, please refine your search." ));
370- else if (isEmpty)
317+ else if (ui-> leftPane -> items . isEmpty () )
371318 {
372319 showError (tr (" Person or company does not own a valid certificate.<br />"
373320 " It is necessary to have a valid certificate for encryption.<br />"
0 commit comments