blob: 530fdb25db1cfcf6a8c2294ff3d77f3cd1c88c5f [file] [log] [blame]
Yingdi Yu1f824642016-03-20 17:07:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California,
4 *
5 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "key-viewer-dialog.hpp"
21#include "ui_key-viewer-dialog.h"
22#include "key-tree-item.hpp"
23
24#include <iostream>
25
Yingdi Yu95dbc712016-03-21 09:56:57 -070026#include <Qt>
27
Yingdi Yu1f824642016-03-20 17:07:22 -070028
29#ifdef WAF
30#include "key-viewer-dialog.moc"
31#endif
32
33namespace ndn {
34namespace ncc {
35
Yingdi Yu95dbc712016-03-21 09:56:57 -070036static int N_ROWS = 30;
37
Yingdi Yu1f824642016-03-20 17:07:22 -070038KeyViewerDialog::KeyViewerDialog(QWidget *parent)
39 : QDialog(parent)
40 , ui(new Ui::KeyViewerDialog)
41 , m_model(new KeyTreeModel)
42{
43 ui->setupUi(this);
44
45 ui->treeView->setModel(m_model.get());
46 ui->treeView->show();
47
Yingdi Yu95dbc712016-03-21 09:56:57 -070048 m_tableModel = make_shared<CertTreeModel>(N_ROWS, 2);
49 m_tableModel->setHeaderData(0, Qt::Horizontal,"Field");
50 m_tableModel->setHeaderData(1, Qt::Horizontal,"Value");
51 ui->certView->setModel(m_tableModel.get());
52 ui->certView->show();
53
Yingdi Yu1f824642016-03-20 17:07:22 -070054 connect(ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(displayCert(const QModelIndex&)));
55}
56
57KeyViewerDialog::~KeyViewerDialog()
58{
59}
60
61void
62KeyViewerDialog::updateModel()
63{
64}
65
66void
67KeyViewerDialog::present()
68{
69 m_keyChain = make_shared<KeyChain>();
70 auto newModel = make_shared<KeyTreeModel>();
71
72 std::vector<Name> defaultIdentities;
73 m_keyChain->getAllIdentities(defaultIdentities, true);
74 for (const auto& identity : defaultIdentities) {
Yingdi Yu95dbc712016-03-21 09:56:57 -070075 auto idItem = createIdentityNode(identity);
76 idItem->setDefault(true);
77 newModel->addChild(idItem);
Yingdi Yu1f824642016-03-20 17:07:22 -070078 }
79
80 std::vector<Name> otherIdentities;
81 m_keyChain->getAllIdentities(otherIdentities, false);
82 for (const auto& identity : otherIdentities) {
83 newModel->addChild(createIdentityNode(identity));
84 }
85
86 ui->treeView->setModel(newModel.get());
87 ui->treeView->show();
88 m_model = newModel;
89
90 show();
91 raise();
92}
93
94void
95KeyViewerDialog::displayCert(const QModelIndex& index)
96{
97 std::cerr << "clicked" << std::endl;
98
99 QModelIndex parent = index;
100 KeyTreeItem* item = static_cast<KeyTreeItem*>(parent.internalPointer());
101 if (item->type() == KeyTreeItem::Type::ID) {
102 std::cerr << item->name().toString().toStdString() << std::endl;
103 parent = m_model->index(0, 0, parent);
104 }
105
106 item = static_cast<KeyTreeItem*>(parent.internalPointer());
107 if (item->type() == KeyTreeItem::Type::KEY) {
108 std::cerr << item->name().toString().toStdString() << std::endl;
109 parent = m_model->index(0, 0, parent);
110 }
111
Yingdi Yu95dbc712016-03-21 09:56:57 -0700112 shared_ptr<CertTreeModel> tableModel = make_shared<CertTreeModel>(N_ROWS, 2);
113 tableModel->setHeaderData(0, Qt::Horizontal,"Field");
114 tableModel->setHeaderData(1, Qt::Horizontal,"Value");
115
Yingdi Yu1f824642016-03-20 17:07:22 -0700116 item = static_cast<KeyTreeItem*>(parent.internalPointer());
117 if (item->type() == KeyTreeItem::Type::CERT) {
118 std::cerr << item->name().toString().toStdString() << std::endl;
119
120 shared_ptr<IdentityCertificate> cert =
121 m_keyChain->getCertificate(Name(item->name().toString().toStdString()));
122
Yingdi Yu1f824642016-03-20 17:07:22 -0700123 tableModel->setItem(0, 0, new QStandardItem(QString("Name")));
124 tableModel->setItem(0, 1, new QStandardItem(item->name().toString()));
Yingdi Yu95dbc712016-03-21 09:56:57 -0700125 // tableModel->setData(tableModel->index(0, 0), Qt::red, Qt::BackgroundRole);
Yingdi Yu1f824642016-03-20 17:07:22 -0700126
127 tableModel->setItem(1, 0, new QStandardItem(QString("NotBefore")));
128 tableModel->setItem(1, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotBefore()))));
129
130 tableModel->setItem(2, 0, new QStandardItem(QString("NotAfter")));
131 tableModel->setItem(2, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotAfter()))));
132
133 tableModel->setItem(3, 0, new QStandardItem(QString("FingerPrint")));
134 tableModel->setItem(3, 1, new QStandardItem(QString("FFFF")));
135
136 Name signerName = cert->getSignature().getKeyLocator().getName();
137 tableModel->setItem(4, 0, new QStandardItem(QString("KeyLocator")));
138 tableModel->setItem(4, 1, new QStandardItem(QString::fromStdString(signerName.toUri())));
139 }
140 else {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700141 tableModel->setItem(0, 0, new QStandardItem(QString("")));
142 tableModel->setItem(0, 1, new QStandardItem(QString("")));
Yingdi Yu1f824642016-03-20 17:07:22 -0700143 }
Yingdi Yu95dbc712016-03-21 09:56:57 -0700144 ui->certView->setModel(tableModel.get());
145 ui->certView->show();
Yingdi Yu1f824642016-03-20 17:07:22 -0700146 m_tableModel = tableModel;
147}
148
149KeyTreeItem*
150KeyViewerDialog::createIdentityNode(const Name& identity)
151{
152 KeyTreeItem* idItem = new KeyTreeItem(QVariant(QString::fromStdString(identity.toUri())),
153 QVariant(QString::fromStdString(identity.toUri())),
154 KeyTreeItem::Type::ID);
155
156 std::vector<Name> defaultKeys;
157 m_keyChain->getAllKeyNamesOfIdentity(identity, defaultKeys, true);
158 for (const auto& keyName : defaultKeys) {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700159 auto keyItem = createKeyNode(keyName, idItem);
160 keyItem->setDefault(true);
161 idItem->appendChild(keyItem);
Yingdi Yu1f824642016-03-20 17:07:22 -0700162 }
163
164 std::vector<ndn::Name> otherKeys;
165 m_keyChain->getAllKeyNamesOfIdentity(identity, otherKeys, false);
166 for (const auto& keyName : otherKeys) {
167 idItem->appendChild(createKeyNode(keyName, idItem));
168 }
169
170 return idItem;
171}
172
173KeyTreeItem*
174KeyViewerDialog::createKeyNode(const Name& keyName, KeyTreeItem* idItem)
175{
176 KeyTreeItem* keyItem = new KeyTreeItem(QVariant(QString::fromStdString(keyName.toUri())),
177 QVariant(QString::fromStdString(keyName[-1].toUri())),
178 KeyTreeItem::Type::KEY,
179 idItem);
180
181 std::vector<Name> defaultCertificates;
182 m_keyChain->getAllCertificateNamesOfKey(keyName, defaultCertificates, true);
183 for (const auto& certName : defaultCertificates) {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700184 auto certItem = new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())),
185 QVariant(QString::fromStdString(certName[-1].toUri())),
186 KeyTreeItem::Type::CERT,
187 keyItem);
188 certItem->setDefault(true);
189 keyItem->appendChild(certItem);
Yingdi Yu1f824642016-03-20 17:07:22 -0700190 }
191
192 std::vector<ndn::Name> otherCertificates;
193 m_keyChain->getAllCertificateNamesOfKey(keyName, otherCertificates, false);
194 for (const auto& certName : otherCertificates) {
195 keyItem->appendChild(new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())),
196 QVariant(QString::fromStdString(certName[-1].toUri())),
197 KeyTreeItem::Type::CERT,
198 keyItem));
199 }
200
201 return keyItem;
202}
203
204} // namespace ncc
205} // namespace ndn