Yingdi Yu | 3b318c1 | 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" |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | using namespace ndn; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 22 | using namespace std; |
| 23 | using namespace ndn::ptr_lib; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 24 | |
| 25 | INIT_LOGGER("ProfileEditor"); |
| 26 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 27 | ProfileEditor::ProfileEditor(shared_ptr<ContactManager> contactManager, |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 28 | QWidget *parent) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 29 | : QDialog(parent) |
| 30 | , ui(new Ui::ProfileEditor) |
| 31 | , m_tableModel(new QSqlTableModel()) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 32 | , m_contactManager(contactManager) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 33 | , m_keyChain(new KeyChain()) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 34 | { |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 35 | ui->setupUi(this); |
| 36 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 37 | m_currentIdentity = contactManager->getDefaultIdentity(); |
| 38 | ui->identityInput->setText(m_currentIdentity.toUri().c_str()); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 40 | connect(ui->addRowButton, SIGNAL(clicked()), |
| 41 | this, SLOT(onAddClicked())); |
| 42 | connect(ui->deleteRowButton, SIGNAL(clicked()), |
| 43 | this, SLOT(onDeleteClicked())); |
| 44 | connect(ui->okButton, SIGNAL(clicked()), |
| 45 | this, SLOT(onOkClicked())); |
| 46 | connect(ui->getButton, SIGNAL(clicked()), |
| 47 | this, SLOT(onGetClicked())); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 49 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 50 | |
| 51 | } |
| 52 | |
| 53 | ProfileEditor::~ProfileEditor() |
| 54 | { |
| 55 | delete ui; |
| 56 | delete m_tableModel; |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | ProfileEditor::onAddClicked() |
| 61 | { |
| 62 | int rowCount = m_tableModel->rowCount(); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 63 | QSqlRecord record; |
| 64 | QSqlField identityField("profile_identity", QVariant::String); |
| 65 | record.append(identityField); |
| 66 | record.setValue("profile_identity", QString(m_currentIdentity.toUri().c_str())); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 67 | m_tableModel->insertRow(rowCount); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 68 | m_tableModel->setRecord(rowCount, record); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | ProfileEditor::onDeleteClicked() |
| 73 | { |
| 74 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 75 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 76 | |
| 77 | int i = indexList.size() - 1; |
| 78 | for(; i >= 0; i--) |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 79 | m_tableModel->removeRow(indexList[i].row()); |
| 80 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 81 | m_tableModel->submitAll(); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | ProfileEditor::onOkClicked() |
| 86 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 87 | Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(m_currentIdentity); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 88 | if(defaultCertName.size() == 0) |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 89 | { |
| 90 | emit noKeyOrCert(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?")); |
| 91 | return; |
| 92 | } |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 93 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 94 | m_tableModel->submitAll(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 95 | m_contactManager->updateProfileData(m_currentIdentity); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 96 | this->hide(); |
| 97 | } |
| 98 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 99 | void |
| 100 | ProfileEditor::onGetClicked() |
| 101 | { |
| 102 | QString inputIdentity = ui->identityInput->text(); |
| 103 | m_currentIdentity = Name(inputIdentity.toUtf8().constData()); |
| 104 | string filter("profile_identity = '"); |
| 105 | filter.append(m_currentIdentity.toUri()).append("'"); |
| 106 | |
| 107 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 108 | m_tableModel->setTable("SelfProfile"); |
| 109 | m_tableModel->setFilter(filter.c_str()); |
| 110 | m_tableModel->select(); |
| 111 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity")); |
| 112 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 113 | m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 114 | |
| 115 | ui->profileTable->setModel(m_tableModel); |
| 116 | ui->profileTable->setColumnHidden(0, true); |
| 117 | ui->profileTable->show(); |
| 118 | } |
| 119 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 120 | #if WAF |
| 121 | #include "profileeditor.moc" |
| 122 | #include "profileeditor.cpp.moc" |
| 123 | #endif |