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,68 @@ 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, ui->leftPane );
155+ ui->leftPane ->addWidget (leftItem);
156+ bool contains = rightList.contains (cert);
157+ leftItem->setDisabled (contains);
158+ leftItem->showButton (contains ? AddressItem::Added : AddressItem::Add);
159+ connect (leftItem, &AddressItem::add, this , [this ](Item *item) { addRecipientToRightPane (item); });
160+ if (auto *add = ui->leftPane ->findChild <QWidget*>(QStringLiteral (" add" )))
161+ add->setVisible (true );
162+ }
185163
186- return leftItem;
164+ if (select)
165+ addRecipientToRightPane (leftItem);
187166}
188167
189- bool AddRecipients::addRecipientToRightPane (const CKey &key , bool update)
168+ void AddRecipients::addRecipientToRightPane (Item *item , bool update)
190169{
191- if (rightList.contains (key))
192- return false ;
170+ auto *address = qobject_cast<AddressItem*>(item);
171+ if (!address || rightList.contains (address->getKey ()))
172+ return ;
193173
174+ const auto &key = address->getKey ();
194175 if (update)
195176 {
196177 if (auto expiryDate = key.cert .expiryDate (); expiryDate <= QDateTime::currentDateTime ())
197178 {
198179 if (Settings::CDOC2_DEFAULT && Settings::CDOC2_USE_KEYSERVER)
199180 {
200181 WarningDialog::show (this , tr (" Failed to add certificate. An expired certificate cannot be used for encryption." ));
201- return false ;
182+ return ;
202183 }
203184 auto *dlg = new WarningDialog (tr (" Are you sure that you want use certificate for encrypting, which expired on %1?<br />"
204185 " When decrypter has updated certificates then decrypting is impossible." )
205186 .arg (expiryDate.toString (QStringLiteral (" dd.MM.yyyy hh:mm:ss" ))), this );
206187 dlg->setCancelText (WarningDialog::NO);
207188 dlg->addButton (WarningDialog::YES, QMessageBox::Yes);
208189 if (dlg->exec () != QMessageBox::Yes)
209- return false ;
190+ return ;
210191 }
211192 QSslConfiguration backup = QSslConfiguration::defaultConfiguration ();
212193 QSslConfiguration::setDefaultConfiguration (CheckConnection::sslConfiguration ());
@@ -219,55 +200,53 @@ bool AddRecipients::addRecipientToRightPane(const CKey &key, bool update)
219200 dlg->setCancelText (WarningDialog::NO);
220201 dlg->addButton (WarningDialog::YES, QMessageBox::Yes);
221202 if (dlg->exec () != QMessageBox::Yes)
222- return false ;
203+ return ;
223204 }
224205 }
225206 updated = update;
226207
227208 rightList.append (key);
228209
229210 auto *rightItem = new AddressItem (key, ui->rightPane );
230- connect (rightItem, &AddressItem::remove, this , &AddRecipients::removeRecipientFromRightPane);
211+ connect (rightItem, &AddressItem::remove, this , [this ](Item *item) {
212+ auto *rightItem = qobject_cast<AddressItem*>(item);
213+ if (auto *leftItem = itemListValue (ui->leftPane , rightItem->getKey ().cert ))
214+ {
215+ leftItem->setDisabled (false );
216+ leftItem->showButton (AddressItem::Add);
217+ }
218+ rightList.removeAll (rightItem->getKey ());
219+ updated = true ;
220+ ui->confirm ->setDisabled (rightList.isEmpty ());
221+ });
231222 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)) {
223+ ui->confirm ->setEnabled (true );
224+ historyCertData.addAndSave (key.cert );
225+ if (auto *leftItem = itemListValue (ui->leftPane , key))
226+ {
240227 leftItem->setDisabled (true );
241228 leftItem->showButton (AddressItem::Added);
242229 }
243230}
244231
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- }
256- }
257-
258232QString AddRecipients::defaultUrl (QLatin1String key, const QString &defaultValue)
259233{
260234 return Application::confValue (key).toString (defaultValue);
261235}
262236
263- void AddRecipients::enableRecipientFromCard ()
237+ bool AddRecipients::isUpdated () const
264238{
265- ui-> fromCard -> setDisabled ( qApp-> signer ()-> tokenauth (). cert (). isNull () ) ;
239+ return updated ;
266240}
267241
268- bool AddRecipients::isUpdated () const
242+ AddressItem* AddRecipients::itemListValue (ItemList *list, const CKey &cert)
269243{
270- return updated;
244+ for (auto *item: list->items )
245+ {
246+ if (auto *address = qobject_cast<AddressItem*>(item); address && address->getKey () == cert)
247+ return address;
248+ }
249+ return nullptr ;
271250}
272251
273252QList<CKey> AddRecipients::keys ()
@@ -281,24 +260,9 @@ QList<CKey> AddRecipients::keys()
281260 return recipients;
282261}
283262
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-
297263void AddRecipients::search (const QString &term, bool select, const QString &type)
298264{
299265 QApplication::setOverrideCursor (Qt::WaitCursor);
300- ui->confirm ->setDefault (false );
301- ui->confirm ->setAutoDefault (false );
302266
303267 QVariantMap userData {
304268 {QStringLiteral (" type" ), type},
@@ -318,25 +282,19 @@ void AddRecipients::search(const QString &term, bool select, const QString &type
318282#endif
319283 bool isDigit = false ;
320284 void (cleanTerm.toULongLong (&isDigit));
321- if (isDigit && (cleanTerm.size () == 11 || cleanTerm.size () == 8 ))
285+ if (!isDigit || (cleanTerm.size () != 11 && cleanTerm.size () != 8 ))
286+ ldap_corp->search (QStringLiteral (" (cn=*%1*)" ).arg (cleanTerm), userData);
287+ else if (cleanTerm.size () == 8 )
288+ ldap_corp->search (QStringLiteral (" (serialNumber=%1)" ).arg (cleanTerm), userData);
289+ else if (IKValidator::isValid (cleanTerm))
322290 {
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);
291+ userData[QStringLiteral (" personSearch" )] = true ;
292+ ldap_person->search (QStringLiteral (" (serialNumber=%1%2)" ).arg (ldap_person->isSSL () ? QStringLiteral (" PNOEE-" ) : QString (), cleanTerm), userData);
336293 }
337294 else
338295 {
339- ldap_corp->search (QStringLiteral (" (cn=*%1*)" ).arg (cleanTerm), userData);
296+ QApplication::restoreOverrideCursor ();
297+ WarningDialog::show (this , tr (" Personal code is not valid!" ));
340298 }
341299}
342300
@@ -348,7 +306,6 @@ void AddRecipients::showError( const QString &msg, const QString &details )
348306
349307void AddRecipients::showResult (const QList<QSslCertificate> &result, int resultCount, const QVariantMap &userData)
350308{
351- bool isEmpty = true ;
352309 for (const QSslCertificate &k: result)
353310 {
354311 SslCertificate c (k);
@@ -358,16 +315,13 @@ void AddRecipients::showResult(const QList<QSslCertificate> &result, int resultC
358315 (userData.value (QStringLiteral (" personSearch" ), false ).toBool () || !c.enhancedKeyUsage ().contains (SslCertificate::ClientAuth)) &&
359316 c.type () != SslCertificate::MobileIDType)
360317 {
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 );
318+ addRecipient (k, userData.value (QStringLiteral (" select" ), false ).toBool () &&
319+ (userData.value (QStringLiteral (" type" )).isNull () || HistoryCertData::toType (SslCertificate (k)) == userData[QStringLiteral (" type" )]));
366320 }
367321 }
368322 if (resultCount >= 50 )
369323 showError (tr (" The name you were looking for gave too many results, please refine your search." ));
370- else if (isEmpty)
324+ else if (ui-> leftPane -> items . isEmpty () )
371325 {
372326 showError (tr (" Person or company does not own a valid certificate.<br />"
373327 " It is necessary to have a valid certificate for encryption.<br />"
0 commit comments