LeechCraft  0.6.70-6645-gcd10d7e
Modular cross-platform feature rich live environment.
sslerror2treeitem.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "sslerror2treeitem.h"
31 #include <QStringList>
32 #include <QDateTime>
33 #include <QTreeWidgetItem>
34 #include <QSslError>
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
40  QTreeWidgetItem* SslError2TreeItem (const QSslError& error)
41  {
42  const auto item = new QTreeWidgetItem { { "Error:", error.errorString () } };
43 
44  const auto& cer = error.certificate ();
45  if (cer.isNull ())
46  {
47  new QTreeWidgetItem
48  {
49  item,
50  { QObject::tr ("Certificate"), QObject::tr ("(No certificate available for this error)") }
51  };
52  return item;
53  }
54 
55  new QTreeWidgetItem
56  {
57  item,
58  {
59  QObject::tr ("Valid:"),
60  #if QT_VERSION < 0x050000
61  cer.isValid () ? QObject::tr ("yes") : QObject::tr ("no")
62  #else
63  !cer.isBlacklisted () ? QObject::tr ("yes") : QObject::tr ("no")
64  #endif
65  }
66  };
67  new QTreeWidgetItem { item, { QObject::tr ("Effective date:"), cer.effectiveDate ().toString () } };
68  new QTreeWidgetItem { item, { QObject::tr ("Expiry date:"), cer.expiryDate ().toString () } };
69  new QTreeWidgetItem { item, { QObject::tr ("Version:"), cer.version () } };
70  new QTreeWidgetItem { item, { QObject::tr ("Serial number:"), cer.serialNumber () } };
71  new QTreeWidgetItem { item, { QObject::tr ("MD5 digest:"), cer.digest ().toHex () } };
72  new QTreeWidgetItem { item, { QObject::tr ("SHA1 digest:"), cer.digest (QCryptographicHash::Sha1).toHex () } };
73 
74  QString tmpString;
75  #if QT_VERSION >= 0x050000
76  auto cvt = [] (const QStringList& list) { return list.join ("; "); };
77  #else
78  auto cvt = [] (const QString& str) { return str; };
79  #endif
80 
81  const auto issuer = new QTreeWidgetItem { item, { QObject::tr ("Issuer info") } };
82  auto mkIssuerItem = [&cvt, &cer, issuer] (const QString& name,
83  QSslCertificate::SubjectInfo field)
84  {
85  const auto& value = cvt (cer.issuerInfo (field));
86  if (!value.isEmpty ())
87  new QTreeWidgetItem { issuer, { name, value } };
88  };
89 
90  mkIssuerItem (QObject::tr ("Organization:"), QSslCertificate::Organization);
91  mkIssuerItem (QObject::tr ("Common name:"), QSslCertificate::CommonName);
92  mkIssuerItem (QObject::tr ("Locality:"), QSslCertificate::LocalityName);
93  mkIssuerItem (QObject::tr ("Organizational unit name:"), QSslCertificate::OrganizationalUnitName);
94  mkIssuerItem (QObject::tr ("Country name:"), QSslCertificate::CountryName);
95  mkIssuerItem (QObject::tr ("State or province name:"), QSslCertificate::StateOrProvinceName);
96 
97  const auto subject = new QTreeWidgetItem { item, { QObject::tr ("Subject info") } };
98  auto mkSubjectItem = [&cvt, &cer, subject] (const QString& name,
99  QSslCertificate::SubjectInfo field)
100  {
101  const auto& value = cvt (cer.subjectInfo (field));
102  if (!value.isEmpty ())
103  new QTreeWidgetItem { subject, { name, value } };
104  };
105 
106  mkSubjectItem (QObject::tr ("Organization:"), QSslCertificate::Organization);
107  mkSubjectItem (QObject::tr ("Common name:"), QSslCertificate::CommonName);
108  mkSubjectItem (QObject::tr ("Locality:"), QSslCertificate::LocalityName);
109  mkSubjectItem (QObject::tr ("Organizational unit name:"), QSslCertificate::OrganizationalUnitName);
110  mkSubjectItem (QObject::tr ("Country name:"), QSslCertificate::CountryName);
111  mkSubjectItem (QObject::tr ("State or province name:"), QSslCertificate::StateOrProvinceName);
112 
113  return item;
114  }
115 }
116 }
QTreeWidgetItem * SslError2TreeItem(const QSslError &error)
Builds a tree widget representation of the given SSL error.