Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [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 | |
| 12 | #include "browse-contact-dialog.h" |
| 13 | #include "ui_browse-contact-dialog.h" |
| 14 | |
| 15 | #ifndef Q_MOC_RUN |
| 16 | #include "profile.h" |
| 17 | #endif |
| 18 | |
| 19 | using namespace std; |
| 20 | using namespace ndn; |
| 21 | using namespace chronos; |
| 22 | |
| 23 | BrowseContactDialog::BrowseContactDialog(QWidget *parent) |
| 24 | : QDialog(parent) |
| 25 | , ui(new Ui::BrowseContactDialog) |
| 26 | , m_contactListModel(new QStringListModel) |
| 27 | { |
| 28 | ui->setupUi(this); |
| 29 | |
| 30 | m_typeHeader = new QTableWidgetItem(QString("Type")); |
| 31 | m_valueHeader = new QTableWidgetItem(QString("Value")); |
| 32 | ui->InfoTable->setHorizontalHeaderItem(0, m_typeHeader); |
| 33 | ui->InfoTable->setHorizontalHeaderItem(1, m_valueHeader); |
| 34 | |
| 35 | ui->ContactList->setModel(m_contactListModel); |
| 36 | |
| 37 | connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 38 | this, SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &))); |
| 39 | connect(ui->AddButton, SIGNAL(clicked()), |
| 40 | this, SLOT(onAddClicked())); |
| 41 | connect(ui->DirectAddButton, SIGNAL(clicked()), |
| 42 | this, SLOT(onDirectAddClicked())); |
| 43 | } |
| 44 | |
| 45 | BrowseContactDialog::~BrowseContactDialog() |
| 46 | { |
| 47 | delete m_typeHeader; |
| 48 | delete m_valueHeader; |
| 49 | delete ui; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | BrowseContactDialog::onSelectionChanged(const QItemSelection& selected, |
| 54 | const QItemSelection& deselected) |
| 55 | { |
| 56 | QModelIndexList items = selected.indexes(); |
| 57 | QString certName = m_contactCertNameList[items.first().row()]; |
| 58 | emit fetchIdCert(certName); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | BrowseContactDialog::onAddClicked() |
| 63 | { |
| 64 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 65 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
| 66 | QModelIndexList::iterator it = selectedList.begin(); |
| 67 | for(; it != selectedList.end(); it++) |
| 68 | emit addContact(m_contactNameList[it->row()]); |
| 69 | |
| 70 | this->close(); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | BrowseContactDialog::onDirectAddClicked() |
| 75 | { |
| 76 | emit directAddClicked(); |
| 77 | this->close(); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | BrowseContactDialog::closeEvent(QCloseEvent *e) |
| 82 | { |
| 83 | ui->InfoTable->clear(); |
| 84 | for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--) |
| 85 | ui->InfoTable->removeRow(i); |
| 86 | ui->InfoTable->horizontalHeader()->hide(); |
| 87 | |
| 88 | hide(); |
| 89 | e->ignore(); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | BrowseContactDialog::onIdCertNameListReady(const QStringList& qCertNameList) |
| 94 | { |
| 95 | m_contactCertNameList = qCertNameList; |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | BrowseContactDialog::onNameListReady(const QStringList& qNameList) |
| 100 | { |
| 101 | m_contactNameList = qNameList; |
| 102 | m_contactListModel->setStringList(m_contactNameList); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | BrowseContactDialog::onIdCertReady(const IdentityCertificate& idCert) |
| 107 | { |
| 108 | Profile profile(idCert); |
| 109 | |
| 110 | ui->InfoTable->clear(); |
| 111 | |
| 112 | for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--) |
| 113 | ui->InfoTable->removeRow(i); |
| 114 | |
| 115 | ui->InfoTable->horizontalHeader()->show(); |
| 116 | ui->InfoTable->setColumnCount(2); |
| 117 | |
| 118 | Profile::const_iterator proIt = profile.begin(); |
| 119 | Profile::const_iterator proEnd = profile.end(); |
| 120 | int rowCount = 0; |
| 121 | |
| 122 | for(; proIt != proEnd; proIt++, rowCount++) |
| 123 | { |
| 124 | ui->InfoTable->insertRow(rowCount); |
| 125 | QTableWidgetItem* type = new QTableWidgetItem(QString::fromStdString(proIt->first)); |
| 126 | ui->InfoTable->setItem(rowCount, 0, type); |
| 127 | |
| 128 | QTableWidgetItem* value = new QTableWidgetItem(QString::fromStdString(proIt->second)); |
| 129 | ui->InfoTable->setItem(rowCount, 1, value); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #if WAF |
| 134 | #include "browse-contact-dialog.moc" |
| 135 | #include "browse-contact-dialog.cpp.moc" |
| 136 | #endif |