Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017, Regents of the University of California, |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame^] | 21 | #include "key-viewer-dialog.moc" |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 22 | #include "ui_key-viewer-dialog.h" |
| 23 | #include "key-tree-item.hpp" |
| 24 | |
| 25 | #include <iostream> |
| 26 | |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 27 | #include <Qt> |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 28 | #include <QMenu> |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ncc { |
| 32 | |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 33 | static int N_ROWS = 30; |
| 34 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 35 | KeyViewerDialog::KeyViewerDialog(QWidget *parent) |
| 36 | : QDialog(parent) |
| 37 | , ui(new Ui::KeyViewerDialog) |
| 38 | , m_model(new KeyTreeModel) |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 39 | , m_keyChain(new KeyChain) |
| 40 | , m_timer(parent) |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 41 | { |
| 42 | ui->setupUi(this); |
| 43 | |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 44 | // present(); |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 45 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 46 | connect(ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(displayCert(const QModelIndex&))); |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 47 | |
| 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | KeyViewerDialog::~KeyViewerDialog() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | KeyViewerDialog::updateModel() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 70 | KeyViewerDialog::display() |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 71 | { |
| 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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 78 | auto idItem = createIdentityNode(identity); |
| 79 | idItem->setDefault(true); |
| 80 | newModel->addChild(idItem); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 81 | } |
| 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 Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 93 | 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 | |
| 101 | void |
| 102 | KeyViewerDialog::present() |
| 103 | { |
| 104 | display(); |
| 105 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 106 | show(); |
| 107 | raise(); |
| 108 | } |
| 109 | |
| 110 | void |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame] | 111 | KeyViewerDialog::getSelection(const QModelIndex& index) |
| 112 | { |
| 113 | m_selectedIndex = index; |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | KeyViewerDialog::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 | |
| 126 | void |
| 127 | KeyViewerDialog::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 | |
| 159 | void |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 160 | KeyViewerDialog::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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 177 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 181 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 188 | tableModel->setItem(0, 0, new QStandardItem(QString("Name"))); |
| 189 | tableModel->setItem(0, 1, new QStandardItem(item->name().toString())); |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 190 | // tableModel->setData(tableModel->index(0, 0), Qt::red, Qt::BackgroundRole); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 191 | |
| 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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 206 | tableModel->setItem(0, 0, new QStandardItem(QString(""))); |
| 207 | tableModel->setItem(0, 1, new QStandardItem(QString(""))); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 208 | } |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 209 | ui->certView->setModel(tableModel.get()); |
| 210 | ui->certView->show(); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 211 | m_tableModel = tableModel; |
| 212 | } |
| 213 | |
| 214 | KeyTreeItem* |
| 215 | KeyViewerDialog::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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 224 | auto keyItem = createKeyNode(keyName, idItem); |
| 225 | keyItem->setDefault(true); |
| 226 | idItem->appendChild(keyItem); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 227 | } |
| 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 | |
| 238 | KeyTreeItem* |
| 239 | KeyViewerDialog::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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 249 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 255 | } |
| 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 |