blob: 540adec21267ddb83d5931f73630df958011caaa [file] [log] [blame]
Yingdi Yu1f824642016-03-20 17:07:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev37667752017-02-02 13:52:12 -08003 * Copyright (c) 2013-2017, Regents of the University of California,
Yingdi Yu1f824642016-03-20 17:07:22 -07004 *
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"
Alexander Afanasyev37667752017-02-02 13:52:12 -080021#include "key-viewer-dialog.moc"
Yingdi Yu1f824642016-03-20 17:07:22 -070022#include "ui_key-viewer-dialog.h"
23#include "key-tree-item.hpp"
24
25#include <iostream>
26
Yingdi Yu95dbc712016-03-21 09:56:57 -070027#include <Qt>
Yingdi Yuaddb1422016-03-21 11:01:04 -070028#include <QMenu>
Yingdi Yu95dbc712016-03-21 09:56:57 -070029
Yingdi Yu1f824642016-03-20 17:07:22 -070030namespace ndn {
31namespace ncc {
32
Yingdi Yu95dbc712016-03-21 09:56:57 -070033static int N_ROWS = 30;
34
Yingdi Yu1f824642016-03-20 17:07:22 -070035KeyViewerDialog::KeyViewerDialog(QWidget *parent)
36 : QDialog(parent)
37 , ui(new Ui::KeyViewerDialog)
38 , m_model(new KeyTreeModel)
Yingdi Yuaddb1422016-03-21 11:01:04 -070039 , m_keyChain(new KeyChain)
40 , m_timer(parent)
Yingdi Yu1f824642016-03-20 17:07:22 -070041{
42 ui->setupUi(this);
43
Yingdi Yuaddb1422016-03-21 11:01:04 -070044 // present();
Yingdi Yu95dbc712016-03-21 09:56:57 -070045
Yingdi Yu1f824642016-03-20 17:07:22 -070046 connect(ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(displayCert(const QModelIndex&)));
Yingdi Yuaddb1422016-03-21 11:01:04 -070047
48 m_timer.setInterval(600000);
49 connect(&m_timer, SIGNAL(timeout()), this, SLOT(display()));
50 m_timer.start();
51
52 this->setContextMenuPolicy(Qt::CustomContextMenu);
53
54 connect(ui->treeView, SIGNAL(pressed(const QModelIndex&)),
55 this, SLOT(getSelection(const QModelIndex&)));
56 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
57 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu1f824642016-03-20 17:07:22 -070058}
59
60KeyViewerDialog::~KeyViewerDialog()
61{
62}
63
64void
65KeyViewerDialog::updateModel()
66{
67}
68
69void
Yingdi Yuaddb1422016-03-21 11:01:04 -070070KeyViewerDialog::display()
Yingdi Yu1f824642016-03-20 17:07:22 -070071{
72 m_keyChain = make_shared<KeyChain>();
73 auto newModel = make_shared<KeyTreeModel>();
74
75 std::vector<Name> defaultIdentities;
76 m_keyChain->getAllIdentities(defaultIdentities, true);
77 for (const auto& identity : defaultIdentities) {
Yingdi Yu95dbc712016-03-21 09:56:57 -070078 auto idItem = createIdentityNode(identity);
79 idItem->setDefault(true);
80 newModel->addChild(idItem);
Yingdi Yu1f824642016-03-20 17:07:22 -070081 }
82
83 std::vector<Name> otherIdentities;
84 m_keyChain->getAllIdentities(otherIdentities, false);
85 for (const auto& identity : otherIdentities) {
86 newModel->addChild(createIdentityNode(identity));
87 }
88
89 ui->treeView->setModel(newModel.get());
90 ui->treeView->show();
91 m_model = newModel;
92
Yingdi Yuaddb1422016-03-21 11:01:04 -070093 auto newTableModel = make_shared<CertTreeModel>(N_ROWS, 2);
94 newTableModel->setHeaderData(0, Qt::Horizontal,"Field");
95 newTableModel->setHeaderData(1, Qt::Horizontal,"Value");
96 ui->certView->setModel(newTableModel.get());
97 ui->certView->show();
98 m_tableModel = newTableModel;
99}
100
101void
102KeyViewerDialog::present()
103{
104 display();
105
Yingdi Yu1f824642016-03-20 17:07:22 -0700106 show();
107 raise();
108}
109
110void
Yingdi Yuaddb1422016-03-21 11:01:04 -0700111KeyViewerDialog::getSelection(const QModelIndex& index)
112{
113 m_selectedIndex = index;
114}
115
116void
117KeyViewerDialog::showContextMenu(const QPoint& point)
118{
119 QMenu contextMenu(tr("Key Management"), this);
120 QAction action1("Set Default", this);
121 connect(&action1, SIGNAL(triggered()), this, SLOT(setDefault()));
122 contextMenu.addAction(&action1);
123 contextMenu.exec(ui->treeView->mapToGlobal(point));
124}
125
126void
127KeyViewerDialog::setDefault()
128{
129 std::cerr << "default" << std::endl;
130 KeyTreeItem* item = static_cast<KeyTreeItem*>(m_selectedIndex.internalPointer());
131 if (item == nullptr)
132 return;
133
134 Name name(item->name().toString().toStdString());
135 std::cerr << name << std::endl;
136
137 switch (item->type()) {
138 case KeyTreeItem::Type::ID:
139 {
140 m_keyChain->setDefaultIdentity(name);
141 break;
142 }
143 case KeyTreeItem::Type::KEY:
144 {
145 m_keyChain->setDefaultKeyNameForIdentity(name);
146 break;
147 }
148 case KeyTreeItem::Type::CERT:
149 {
150 m_keyChain->setDefaultCertificateNameForKey(name);
151 break;
152 }
153 default:
154 return;
155 }
156 present();
157}
158
159void
Yingdi Yu1f824642016-03-20 17:07:22 -0700160KeyViewerDialog::displayCert(const QModelIndex& index)
161{
162 std::cerr << "clicked" << std::endl;
163
164 QModelIndex parent = index;
165 KeyTreeItem* item = static_cast<KeyTreeItem*>(parent.internalPointer());
166 if (item->type() == KeyTreeItem::Type::ID) {
167 std::cerr << item->name().toString().toStdString() << std::endl;
168 parent = m_model->index(0, 0, parent);
169 }
170
171 item = static_cast<KeyTreeItem*>(parent.internalPointer());
172 if (item->type() == KeyTreeItem::Type::KEY) {
173 std::cerr << item->name().toString().toStdString() << std::endl;
174 parent = m_model->index(0, 0, parent);
175 }
176
Yingdi Yu95dbc712016-03-21 09:56:57 -0700177 shared_ptr<CertTreeModel> tableModel = make_shared<CertTreeModel>(N_ROWS, 2);
178 tableModel->setHeaderData(0, Qt::Horizontal,"Field");
179 tableModel->setHeaderData(1, Qt::Horizontal,"Value");
180
Yingdi Yu1f824642016-03-20 17:07:22 -0700181 item = static_cast<KeyTreeItem*>(parent.internalPointer());
182 if (item->type() == KeyTreeItem::Type::CERT) {
183 std::cerr << item->name().toString().toStdString() << std::endl;
184
185 shared_ptr<IdentityCertificate> cert =
186 m_keyChain->getCertificate(Name(item->name().toString().toStdString()));
187
Yingdi Yu1f824642016-03-20 17:07:22 -0700188 tableModel->setItem(0, 0, new QStandardItem(QString("Name")));
189 tableModel->setItem(0, 1, new QStandardItem(item->name().toString()));
Yingdi Yu95dbc712016-03-21 09:56:57 -0700190 // tableModel->setData(tableModel->index(0, 0), Qt::red, Qt::BackgroundRole);
Yingdi Yu1f824642016-03-20 17:07:22 -0700191
192 tableModel->setItem(1, 0, new QStandardItem(QString("NotBefore")));
193 tableModel->setItem(1, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotBefore()))));
194
195 tableModel->setItem(2, 0, new QStandardItem(QString("NotAfter")));
196 tableModel->setItem(2, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotAfter()))));
197
198 tableModel->setItem(3, 0, new QStandardItem(QString("FingerPrint")));
199 tableModel->setItem(3, 1, new QStandardItem(QString("FFFF")));
200
201 Name signerName = cert->getSignature().getKeyLocator().getName();
202 tableModel->setItem(4, 0, new QStandardItem(QString("KeyLocator")));
203 tableModel->setItem(4, 1, new QStandardItem(QString::fromStdString(signerName.toUri())));
204 }
205 else {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700206 tableModel->setItem(0, 0, new QStandardItem(QString("")));
207 tableModel->setItem(0, 1, new QStandardItem(QString("")));
Yingdi Yu1f824642016-03-20 17:07:22 -0700208 }
Yingdi Yu95dbc712016-03-21 09:56:57 -0700209 ui->certView->setModel(tableModel.get());
210 ui->certView->show();
Yingdi Yu1f824642016-03-20 17:07:22 -0700211 m_tableModel = tableModel;
212}
213
214KeyTreeItem*
215KeyViewerDialog::createIdentityNode(const Name& identity)
216{
217 KeyTreeItem* idItem = new KeyTreeItem(QVariant(QString::fromStdString(identity.toUri())),
218 QVariant(QString::fromStdString(identity.toUri())),
219 KeyTreeItem::Type::ID);
220
221 std::vector<Name> defaultKeys;
222 m_keyChain->getAllKeyNamesOfIdentity(identity, defaultKeys, true);
223 for (const auto& keyName : defaultKeys) {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700224 auto keyItem = createKeyNode(keyName, idItem);
225 keyItem->setDefault(true);
226 idItem->appendChild(keyItem);
Yingdi Yu1f824642016-03-20 17:07:22 -0700227 }
228
229 std::vector<ndn::Name> otherKeys;
230 m_keyChain->getAllKeyNamesOfIdentity(identity, otherKeys, false);
231 for (const auto& keyName : otherKeys) {
232 idItem->appendChild(createKeyNode(keyName, idItem));
233 }
234
235 return idItem;
236}
237
238KeyTreeItem*
239KeyViewerDialog::createKeyNode(const Name& keyName, KeyTreeItem* idItem)
240{
241 KeyTreeItem* keyItem = new KeyTreeItem(QVariant(QString::fromStdString(keyName.toUri())),
242 QVariant(QString::fromStdString(keyName[-1].toUri())),
243 KeyTreeItem::Type::KEY,
244 idItem);
245
246 std::vector<Name> defaultCertificates;
247 m_keyChain->getAllCertificateNamesOfKey(keyName, defaultCertificates, true);
248 for (const auto& certName : defaultCertificates) {
Yingdi Yu95dbc712016-03-21 09:56:57 -0700249 auto certItem = new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())),
250 QVariant(QString::fromStdString(certName[-1].toUri())),
251 KeyTreeItem::Type::CERT,
252 keyItem);
253 certItem->setDefault(true);
254 keyItem->appendChild(certItem);
Yingdi Yu1f824642016-03-20 17:07:22 -0700255 }
256
257 std::vector<ndn::Name> otherCertificates;
258 m_keyChain->getAllCertificateNamesOfKey(keyName, otherCertificates, false);
259 for (const auto& certName : otherCertificates) {
260 keyItem->appendChild(new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())),
261 QVariant(QString::fromStdString(certName[-1].toUri())),
262 KeyTreeItem::Type::CERT,
263 keyItem));
264 }
265
266 return keyItem;
267}
268
269} // namespace ncc
270} // namespace ndn