Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 26 | #include <Qt> |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 27 | #include <QMenu> |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 29 | |
| 30 | #ifdef WAF |
| 31 | #include "key-viewer-dialog.moc" |
| 32 | #endif |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace ncc { |
| 36 | |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 37 | static int N_ROWS = 30; |
| 38 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 39 | KeyViewerDialog::KeyViewerDialog(QWidget *parent) |
| 40 | : QDialog(parent) |
| 41 | , ui(new Ui::KeyViewerDialog) |
| 42 | , m_model(new KeyTreeModel) |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 43 | , m_keyChain(new KeyChain) |
| 44 | , m_timer(parent) |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 45 | { |
| 46 | ui->setupUi(this); |
| 47 | |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 48 | // present(); |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 49 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 50 | connect(ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(displayCert(const QModelIndex&))); |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 51 | |
| 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | KeyViewerDialog::~KeyViewerDialog() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | KeyViewerDialog::updateModel() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 74 | KeyViewerDialog::display() |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 75 | { |
| 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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 82 | auto idItem = createIdentityNode(identity); |
| 83 | idItem->setDefault(true); |
| 84 | newModel->addChild(idItem); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 85 | } |
| 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 Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 97 | 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 | |
| 105 | void |
| 106 | KeyViewerDialog::present() |
| 107 | { |
| 108 | display(); |
| 109 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 110 | show(); |
| 111 | raise(); |
| 112 | } |
| 113 | |
| 114 | void |
Yingdi Yu | addb142 | 2016-03-21 11:01:04 -0700 | [diff] [blame^] | 115 | KeyViewerDialog::getSelection(const QModelIndex& index) |
| 116 | { |
| 117 | m_selectedIndex = index; |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | KeyViewerDialog::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 | |
| 130 | void |
| 131 | KeyViewerDialog::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 | |
| 163 | void |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 164 | KeyViewerDialog::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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 181 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 185 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 192 | tableModel->setItem(0, 0, new QStandardItem(QString("Name"))); |
| 193 | tableModel->setItem(0, 1, new QStandardItem(item->name().toString())); |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 194 | // tableModel->setData(tableModel->index(0, 0), Qt::red, Qt::BackgroundRole); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 195 | |
| 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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 210 | tableModel->setItem(0, 0, new QStandardItem(QString(""))); |
| 211 | tableModel->setItem(0, 1, new QStandardItem(QString(""))); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 212 | } |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 213 | ui->certView->setModel(tableModel.get()); |
| 214 | ui->certView->show(); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 215 | m_tableModel = tableModel; |
| 216 | } |
| 217 | |
| 218 | KeyTreeItem* |
| 219 | KeyViewerDialog::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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 228 | auto keyItem = createKeyNode(keyName, idItem); |
| 229 | keyItem->setDefault(true); |
| 230 | idItem->appendChild(keyItem); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 231 | } |
| 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 | |
| 242 | KeyTreeItem* |
| 243 | KeyViewerDialog::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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 253 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 259 | } |
| 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 |