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 | |
| 26 | |
| 27 | #ifdef WAF |
| 28 | #include "key-viewer-dialog.moc" |
| 29 | #endif |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace ncc { |
| 33 | |
| 34 | KeyViewerDialog::KeyViewerDialog(QWidget *parent) |
| 35 | : QDialog(parent) |
| 36 | , ui(new Ui::KeyViewerDialog) |
| 37 | , m_model(new KeyTreeModel) |
| 38 | { |
| 39 | ui->setupUi(this); |
| 40 | |
| 41 | ui->treeView->setModel(m_model.get()); |
| 42 | ui->treeView->show(); |
| 43 | |
| 44 | connect(ui->treeView, SIGNAL(clicked(const QModelIndex&)), this, SLOT(displayCert(const QModelIndex&))); |
| 45 | } |
| 46 | |
| 47 | KeyViewerDialog::~KeyViewerDialog() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | KeyViewerDialog::updateModel() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | KeyViewerDialog::present() |
| 58 | { |
| 59 | m_keyChain = make_shared<KeyChain>(); |
| 60 | auto newModel = make_shared<KeyTreeModel>(); |
| 61 | |
| 62 | std::vector<Name> defaultIdentities; |
| 63 | m_keyChain->getAllIdentities(defaultIdentities, true); |
| 64 | for (const auto& identity : defaultIdentities) { |
| 65 | newModel->addChild(createIdentityNode(identity)); |
| 66 | } |
| 67 | |
| 68 | std::vector<Name> otherIdentities; |
| 69 | m_keyChain->getAllIdentities(otherIdentities, false); |
| 70 | for (const auto& identity : otherIdentities) { |
| 71 | newModel->addChild(createIdentityNode(identity)); |
| 72 | } |
| 73 | |
| 74 | ui->treeView->setModel(newModel.get()); |
| 75 | ui->treeView->show(); |
| 76 | m_model = newModel; |
| 77 | |
| 78 | show(); |
| 79 | raise(); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | KeyViewerDialog::displayCert(const QModelIndex& index) |
| 84 | { |
| 85 | std::cerr << "clicked" << std::endl; |
| 86 | |
| 87 | QModelIndex parent = index; |
| 88 | KeyTreeItem* item = static_cast<KeyTreeItem*>(parent.internalPointer()); |
| 89 | if (item->type() == KeyTreeItem::Type::ID) { |
| 90 | std::cerr << item->name().toString().toStdString() << std::endl; |
| 91 | parent = m_model->index(0, 0, parent); |
| 92 | } |
| 93 | |
| 94 | item = static_cast<KeyTreeItem*>(parent.internalPointer()); |
| 95 | if (item->type() == KeyTreeItem::Type::KEY) { |
| 96 | std::cerr << item->name().toString().toStdString() << std::endl; |
| 97 | parent = m_model->index(0, 0, parent); |
| 98 | } |
| 99 | |
| 100 | shared_ptr<QStandardItemModel> tableModel; |
| 101 | item = static_cast<KeyTreeItem*>(parent.internalPointer()); |
| 102 | if (item->type() == KeyTreeItem::Type::CERT) { |
| 103 | std::cerr << item->name().toString().toStdString() << std::endl; |
| 104 | |
| 105 | shared_ptr<IdentityCertificate> cert = |
| 106 | m_keyChain->getCertificate(Name(item->name().toString().toStdString())); |
| 107 | |
| 108 | tableModel = make_shared<QStandardItemModel>(5, 2); |
| 109 | tableModel->setItem(0, 0, new QStandardItem(QString("Name"))); |
| 110 | tableModel->setItem(0, 1, new QStandardItem(item->name().toString())); |
| 111 | |
| 112 | tableModel->setItem(1, 0, new QStandardItem(QString("NotBefore"))); |
| 113 | tableModel->setItem(1, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotBefore())))); |
| 114 | |
| 115 | tableModel->setItem(2, 0, new QStandardItem(QString("NotAfter"))); |
| 116 | tableModel->setItem(2, 1, new QStandardItem(QString::fromStdString(time::toIsoString(cert->getNotAfter())))); |
| 117 | |
| 118 | tableModel->setItem(3, 0, new QStandardItem(QString("FingerPrint"))); |
| 119 | tableModel->setItem(3, 1, new QStandardItem(QString("FFFF"))); |
| 120 | |
| 121 | Name signerName = cert->getSignature().getKeyLocator().getName(); |
| 122 | tableModel->setItem(4, 0, new QStandardItem(QString("KeyLocator"))); |
| 123 | tableModel->setItem(4, 1, new QStandardItem(QString::fromStdString(signerName.toUri()))); |
| 124 | } |
| 125 | else { |
| 126 | tableModel = make_shared<QStandardItemModel>(5, 2); |
| 127 | } |
| 128 | ui->tableView->setModel(tableModel.get()); |
| 129 | ui->tableView->resizeColumnsToContents(); |
| 130 | QHeaderView* header = ui->tableView->horizontalHeader(); |
| 131 | header->setStretchLastSection(true); |
| 132 | ui->tableView->setHorizontalHeader(header); |
| 133 | ui->tableView->show(); |
| 134 | m_tableModel = tableModel; |
| 135 | } |
| 136 | |
| 137 | KeyTreeItem* |
| 138 | KeyViewerDialog::createIdentityNode(const Name& identity) |
| 139 | { |
| 140 | KeyTreeItem* idItem = new KeyTreeItem(QVariant(QString::fromStdString(identity.toUri())), |
| 141 | QVariant(QString::fromStdString(identity.toUri())), |
| 142 | KeyTreeItem::Type::ID); |
| 143 | |
| 144 | std::vector<Name> defaultKeys; |
| 145 | m_keyChain->getAllKeyNamesOfIdentity(identity, defaultKeys, true); |
| 146 | for (const auto& keyName : defaultKeys) { |
| 147 | idItem->appendChild(createKeyNode(keyName, idItem)); |
| 148 | } |
| 149 | |
| 150 | std::vector<ndn::Name> otherKeys; |
| 151 | m_keyChain->getAllKeyNamesOfIdentity(identity, otherKeys, false); |
| 152 | for (const auto& keyName : otherKeys) { |
| 153 | idItem->appendChild(createKeyNode(keyName, idItem)); |
| 154 | } |
| 155 | |
| 156 | return idItem; |
| 157 | } |
| 158 | |
| 159 | KeyTreeItem* |
| 160 | KeyViewerDialog::createKeyNode(const Name& keyName, KeyTreeItem* idItem) |
| 161 | { |
| 162 | KeyTreeItem* keyItem = new KeyTreeItem(QVariant(QString::fromStdString(keyName.toUri())), |
| 163 | QVariant(QString::fromStdString(keyName[-1].toUri())), |
| 164 | KeyTreeItem::Type::KEY, |
| 165 | idItem); |
| 166 | |
| 167 | std::vector<Name> defaultCertificates; |
| 168 | m_keyChain->getAllCertificateNamesOfKey(keyName, defaultCertificates, true); |
| 169 | for (const auto& certName : defaultCertificates) { |
| 170 | keyItem->appendChild(new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())), |
| 171 | QVariant(QString::fromStdString(certName[-1].toUri())), |
| 172 | KeyTreeItem::Type::CERT, |
| 173 | keyItem)); |
| 174 | } |
| 175 | |
| 176 | std::vector<ndn::Name> otherCertificates; |
| 177 | m_keyChain->getAllCertificateNamesOfKey(keyName, otherCertificates, false); |
| 178 | for (const auto& certName : otherCertificates) { |
| 179 | keyItem->appendChild(new KeyTreeItem(QVariant(QString::fromStdString(certName.toUri())), |
| 180 | QVariant(QString::fromStdString(certName[-1].toUri())), |
| 181 | KeyTreeItem::Type::CERT, |
| 182 | keyItem)); |
| 183 | } |
| 184 | |
| 185 | return keyItem; |
| 186 | } |
| 187 | |
| 188 | } // namespace ncc |
| 189 | } // namespace ndn |