blob: 07604dd3e316b8885ec0e796b5a5775b7e3e44cd [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>
Yingdi Yuaddb1422016-03-21 11:01:04 -070027#include <QMenu>
Yingdi Yu95dbc712016-03-21 09:56:57 -070028
Yingdi Yu1f824642016-03-20 17:07:22 -070029
30#ifdef WAF
31#include "key-viewer-dialog.moc"
32#endif
33
34namespace ndn {
35namespace ncc {
36
Yingdi Yu95dbc712016-03-21 09:56:57 -070037static int N_ROWS = 30;
38
Yingdi Yu1f824642016-03-20 17:07:22 -070039KeyViewerDialog::KeyViewerDialog(QWidget *parent)
40 : QDialog(parent)
41 , ui(new Ui::KeyViewerDialog)
42 , m_model(new KeyTreeModel)
Yingdi Yuaddb1422016-03-21 11:01:04 -070043 , m_keyChain(new KeyChain)
44 , m_timer(parent)
Yingdi Yu1f824642016-03-20 17:07:22 -070045{
46 ui->setupUi(this);
47
Yingdi Yuaddb1422016-03-21 11:01:04 -070048 // present();
Yingdi Yu95dbc712016-03-21 09:56:57 -070049
Yingdi Yu1f824642016-03-20 17:07:22 -070050 connect(ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(displayCert(const QModelIndex&)));
Yingdi Yuaddb1422016-03-21 11:01:04 -070051
52 m_timer.setInterval(600000);
53 connect(&m_timer, SIGNAL(timeout()), this, SLOT(display()));
54 m_timer.start();
55
56 this->setContextMenuPolicy(Qt::CustomContextMenu);
57
58 connect(ui->treeView, SIGNAL(pressed(const QModelIndex&)),
59 this, SLOT(getSelection(const QModelIndex&)));
60 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
61 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu1f824642016-03-20 17:07:22 -070062}
63
64KeyViewerDialog::~KeyViewerDialog()
65{
66}
67
68void
69KeyViewerDialog::updateModel()
70{
71}
72
73void
Yingdi Yuaddb1422016-03-21 11:01:04 -070074KeyViewerDialog::display()
Yingdi Yu1f824642016-03-20 17:07:22 -070075{
76 m_keyChain = make_shared<KeyChain>();
77 auto newModel = make_shared<KeyTreeModel>();
78
79 std::vector<Name> defaultIdentities;
80 m_keyChain->getAllIdentities(defaultIdentities, true);
81 for (const auto& identity : defaultIdentities) {
Yingdi Yu95dbc712016-03-21 09:56:57 -070082 auto idItem = createIdentityNode(identity);
83 idItem->setDefault(true);
84 newModel->addChild(idItem);
Yingdi Yu1f824642016-03-20 17:07:22 -070085 }
86
87 std::vector<Name> otherIdentities;
88 m_keyChain->getAllIdentities(otherIdentities, false);
89 for (const auto& identity : otherIdentities) {
90 newModel->addChild(createIdentityNode(identity));
91 }
92
93 ui->treeView->setModel(newModel.get());
94 ui->treeView->show();
95 m_model = newModel;
96
Yingdi Yuaddb1422016-03-21 11:01:04 -070097 auto newTableModel = make_shared<CertTreeModel>(N_ROWS, 2);
98 newTableModel->setHeaderData(0, Qt::Horizontal,"Field");
99 newTableModel->setHeaderData(1, Qt::Horizontal,"Value");
100 ui->certView->setModel(newTableModel.get());
101 ui->certView->show();
102 m_tableModel = newTableModel;
103}
104
105void
106KeyViewerDialog::present()
107{
108 display();
109
Yingdi Yu1f824642016-03-20 17:07:22 -0700110 show();
111 raise();
112}
113
114void
Yingdi Yuaddb1422016-03-21 11:01:04 -0700115KeyViewerDialog::getSelection(const QModelIndex& index)
116{
117 m_selectedIndex = index;
118}
119
120void
121KeyViewerDialog::showContextMenu(const QPoint& point)
122{
123 QMenu contextMenu(tr("Key Management"), this);
124 QAction action1("Set Default", this);
125 connect(&action1, SIGNAL(triggered()), this, SLOT(setDefault()));
126 contextMenu.addAction(&action1);
127 contextMenu.exec(ui->treeView->mapToGlobal(point));
128}
129
130void
131KeyViewerDialog::setDefault()
132{
133 std::cerr << "default" << std::endl;
134 KeyTreeItem* item = static_cast<KeyTreeItem*>(m_selectedIndex.internalPointer());
135 if (item == nullptr)
136 return;
137
138 Name name(item->name().toString().toStdString());
139 std::cerr << name << std::endl;
140
141 switch (item->type()) {
142 case KeyTreeItem::Type::ID:
143 {
144 m_keyChain->setDefaultIdentity(name);
145 break;
146 }
147 case KeyTreeItem::Type::KEY:
148 {
149 m_keyChain->setDefaultKeyNameForIdentity(name);
150 break;
151 }
152 case KeyTreeItem::Type::CERT:
153 {
154 m_keyChain->setDefaultCertificateNameForKey(name);
155 break;
156 }
157 default:
158 return;
159 }
160 present();
161}
162
163void
Yingdi Yu1f824642016-03-20 17:07:22 -0700164KeyViewerDialog::displayCert(const QModelIndex& index)
165{
166 std::cerr << "clicked" << std::endl;
167
168 QModelIndex parent = index;
169 KeyTreeItem* item = static_cast<KeyTreeItem*>(parent.internalPointer());
170 if (item->type() == KeyTreeItem::Type::ID) {
171 std::cerr << item->name().toString().toStdString() << std::endl;
172 parent = m_model->index(0, 0, parent);
173 }
174
175 item = static_cast<KeyTreeItem*>(parent.internalPointer());
176 if (item->type() == KeyTreeItem::Type::KEY) {
177 std::cerr << item->name().toString().toStdString() << std::endl;
178 parent = m_model->index(0, 0, parent);
179 }
180
Yingdi Yu95dbc712016-03-21 09:56:57 -0700181 shared_ptr<CertTreeModel> tableModel = make_shared<CertTreeModel>(N_ROWS, 2);
182 tableModel->setHeaderData(0, Qt::Horizontal,"Field");
183 tableModel->setHeaderData(1, Qt::Horizontal,"Value");
184
Yingdi Yu1f824642016-03-20 17:07:22 -0700185 item = static_cast<KeyTreeItem*>(parent.internalPointer());
186 if (item->type() == KeyTreeItem::Type::CERT) {
187 std::cerr << item->name().toString().toStdString() << std::endl;
188
189 shared_ptr<IdentityCertificate> cert =
190 m_keyChain->getCertificate(Name(item->name().toString().toStdString()));
191
Yingdi Yu1f824642016-03-20 17:07:22 -0700192 tableModel->setItem(0, 0, new QStandardItem(QString("Name")));
193 tableModel->setItem(0, 1, new QStandardItem(item->name().toString()));
Yingdi Yu95dbc712016-03-21 09:56:57 -0700194 // tableModel->setData(tableModel->index(0, 0), Qt::red, Qt::BackgroundRole);
Yingdi Yu1f824642016-03-20 17:07:22 -0700195
196 tableModel->setItem(1, 0, new QStandardItem(QString("NotBefore")));
197 tableModel->setItem(1, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotBefore()))));
198
199 tableModel->setItem(2, 0, new QStandardItem(QString("NotAfter")));
200 tableModel->setItem(2, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotAfter()))));
201
202 tableModel->setItem(3, 0, new QStandardItem(QString("FingerPrint")));
203 tableModel->setItem(3, 1, new QStandardItem(QString("FFFF")));
204
205 Name signerName = cert->getSignature().getKeyLocator().getName();
206 tableModel->setItem(4, 0, new QStandardItem(QString("KeyLocator")));
207 tableModel->setItem(4, 1, new QStandardItem(QString::fromStdString(signerName.toUri())));
208 }
209 else {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700210 tableModel->setItem(0, 0, new QStandardItem(QString("")));
211 tableModel->setItem(0, 1, new QStandardItem(QString("")));
Yingdi Yu1f824642016-03-20 17:07:22 -0700212 }
Yingdi Yu95dbc712016-03-21 09:56:57 -0700213 ui->certView->setModel(tableModel.get());
214 ui->certView->show();
Yingdi Yu1f824642016-03-20 17:07:22 -0700215 m_tableModel = tableModel;
216}
217
218KeyTreeItem*
219KeyViewerDialog::createIdentityNode(const Name& identity)
220{
221 KeyTreeItem* idItem = new KeyTreeItem(QVariant(QString::fromStdString(identity.toUri())),
222 QVariant(QString::fromStdString(identity.toUri())),
223 KeyTreeItem::Type::ID);
224
225 std::vector<Name> defaultKeys;
226 m_keyChain->getAllKeyNamesOfIdentity(identity, defaultKeys, true);
227 for (const auto& keyName : defaultKeys) {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700228 auto keyItem = createKeyNode(keyName, idItem);
229 keyItem->setDefault(true);
230 idItem->appendChild(keyItem);
Yingdi Yu1f824642016-03-20 17:07:22 -0700231 }
232
233 std::vector<ndn::Name> otherKeys;
234 m_keyChain->getAllKeyNamesOfIdentity(identity, otherKeys, false);
235 for (const auto& keyName : otherKeys) {
236 idItem->appendChild(createKeyNode(keyName, idItem));
237 }
238
239 return idItem;
240}
241
242KeyTreeItem*
243KeyViewerDialog::createKeyNode(const Name& keyName, KeyTreeItem* idItem)
244{
245 KeyTreeItem* keyItem = new KeyTreeItem(QVariant(QString::fromStdString(keyName.toUri())),
246 QVariant(QString::fromStdString(keyName[-1].toUri())),
247 KeyTreeItem::Type::KEY,
248 idItem);
249
250 std::vector<Name> defaultCertificates;
251 m_keyChain->getAllCertificateNamesOfKey(keyName, defaultCertificates, true);
252 for (const auto& certName : defaultCertificates) {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700253 auto certItem = new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())),
254 QVariant(QString::fromStdString(certName[-1].toUri())),
255 KeyTreeItem::Type::CERT,
256 keyItem);
257 certItem->setDefault(true);
258 keyItem->appendChild(certItem);
Yingdi Yu1f824642016-03-20 17:07:22 -0700259 }
260
261 std::vector<ndn::Name> otherCertificates;
262 m_keyChain->getAllCertificateNamesOfKey(keyName, otherCertificates, false);
263 for (const auto& certName : otherCertificates) {
264 keyItem->appendChild(new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())),
265 QVariant(QString::fromStdString(certName[-1].toUri())),
266 KeyTreeItem::Type::CERT,
267 keyItem));
268 }
269
270 return keyItem;
271}
272
273} // namespace ncc
274} // namespace ndn