Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "profileeditor.h" |
| 12 | #include "ui_profileeditor.h" |
| 13 | #include <QtSql/QSqlRecord> |
| 14 | #include <QtSql/QSqlField> |
| 15 | #include <QtSql/QSqlError> |
| 16 | |
| 17 | #ifndef Q_MOC_RUN |
| 18 | #include "logging.h" |
| 19 | #include "exception.h" |
| 20 | #endif |
| 21 | |
| 22 | using namespace ndn; |
| 23 | |
| 24 | INIT_LOGGER("ProfileEditor"); |
| 25 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 26 | ProfileEditor::ProfileEditor(Ptr<ContactManager> contactManager, |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 27 | QWidget *parent) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 28 | : QDialog(parent) |
| 29 | , ui(new Ui::ProfileEditor) |
| 30 | , m_tableModel(new QSqlTableModel()) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 31 | , m_contactManager(contactManager) |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 32 | , m_identityManager(ndn::Ptr<ndn::security::IdentityManager>::Create()) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 33 | { |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 34 | ui->setupUi(this); |
| 35 | |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 36 | m_currentIdentity = contactManager->getDefaultIdentity(); |
| 37 | ui->identityInput->setText(m_currentIdentity.toUri().c_str()); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 39 | connect(ui->addRowButton, SIGNAL(clicked()), |
| 40 | this, SLOT(onAddClicked())); |
| 41 | connect(ui->deleteRowButton, SIGNAL(clicked()), |
| 42 | this, SLOT(onDeleteClicked())); |
| 43 | connect(ui->okButton, SIGNAL(clicked()), |
| 44 | this, SLOT(onOkClicked())); |
| 45 | connect(ui->getButton, SIGNAL(clicked()), |
| 46 | this, SLOT(onGetClicked())); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 49 | |
| 50 | } |
| 51 | |
| 52 | ProfileEditor::~ProfileEditor() |
| 53 | { |
| 54 | delete ui; |
| 55 | delete m_tableModel; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | ProfileEditor::onAddClicked() |
| 60 | { |
| 61 | int rowCount = m_tableModel->rowCount(); |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 62 | QSqlRecord record; |
| 63 | QSqlField identityField("profile_identity", QVariant::String); |
| 64 | record.append(identityField); |
| 65 | record.setValue("profile_identity", QString(m_currentIdentity.toUri().c_str())); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 66 | m_tableModel->insertRow(rowCount); |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 67 | m_tableModel->setRecord(rowCount, record); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | ProfileEditor::onDeleteClicked() |
| 72 | { |
| 73 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 74 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 75 | |
| 76 | int i = indexList.size() - 1; |
| 77 | for(; i >= 0; i--) |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 78 | m_tableModel->removeRow(indexList[i].row()); |
| 79 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 80 | m_tableModel->submitAll(); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | ProfileEditor::onOkClicked() |
| 85 | { |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 86 | Name defaultKeyName = m_identityManager->getPublicStorage()->getDefaultKeyNameForIdentity(m_currentIdentity); |
| 87 | if(defaultKeyName.size() == 0) |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 88 | { |
| 89 | emit noKeyOrCert(QString::fromStdString("Corresponding key is missing!\nHave you created the key?")); |
| 90 | return; |
| 91 | } |
| 92 | |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 93 | Name defaultCertName = m_identityManager->getPublicStorage()->getDefaultCertificateNameForKey(defaultKeyName); |
| 94 | if(defaultCertName.size() == 0) |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 95 | { |
| 96 | emit noKeyOrCert(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?")); |
| 97 | return; |
| 98 | } |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 99 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 100 | m_tableModel->submitAll(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 101 | m_contactManager->updateProfileData(m_currentIdentity); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 102 | this->hide(); |
| 103 | } |
| 104 | |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 105 | void |
| 106 | ProfileEditor::onGetClicked() |
| 107 | { |
| 108 | QString inputIdentity = ui->identityInput->text(); |
| 109 | m_currentIdentity = Name(inputIdentity.toUtf8().constData()); |
| 110 | string filter("profile_identity = '"); |
| 111 | filter.append(m_currentIdentity.toUri()).append("'"); |
| 112 | |
| 113 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 114 | m_tableModel->setTable("SelfProfile"); |
| 115 | m_tableModel->setFilter(filter.c_str()); |
| 116 | m_tableModel->select(); |
| 117 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity")); |
| 118 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 119 | m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 120 | |
| 121 | ui->profileTable->setModel(m_tableModel); |
| 122 | ui->profileTable->setColumnHidden(0, true); |
| 123 | ui->profileTable->show(); |
| 124 | } |
| 125 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 126 | #if WAF |
| 127 | #include "profileeditor.moc" |
| 128 | #include "profileeditor.cpp.moc" |
| 129 | #endif |