Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -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 | |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 11 | #include "addcontactpanel.h" |
| 12 | #include "ui_addcontactpanel.h" |
| 13 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 14 | #ifndef Q_MOC_RUN |
| 15 | #include "logging.h" |
| 16 | #endif |
| 17 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 18 | using namespace ndn; |
| 19 | using namespace std; |
| 20 | |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 21 | INIT_LOGGER("AddContactPanel"); |
| 22 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 23 | AddContactPanel::AddContactPanel(Ptr<ContactManager> contactManager, |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 24 | QWidget *parent) |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 25 | : QDialog(parent) |
| 26 | , ui(new Ui::AddContactPanel) |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 27 | , m_contactManager(contactManager) |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 28 | , m_warningDialog(new WarningDialog()) |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 29 | { |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 30 | ui->setupUi(this); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 31 | |
| 32 | qRegisterMetaType<ndn::Name>("NdnName"); |
| 33 | qRegisterMetaType<EndorseCertificate>("EndorseCertificate"); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 34 | |
| 35 | connect(ui->cancelButton, SIGNAL(clicked()), |
| 36 | this, SLOT(onCancelClicked())); |
| 37 | connect(ui->searchButton, SIGNAL(clicked()), |
| 38 | this, SLOT(onSearchClicked())); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 39 | connect(ui->addButton, SIGNAL(clicked()), |
| 40 | this, SLOT(onAddClicked())); |
| 41 | connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)), |
| 42 | this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&))); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 43 | connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)), |
| 44 | this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&))); |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | AddContactPanel::~AddContactPanel() |
| 48 | { |
| 49 | delete ui; |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 50 | delete m_warningDialog; |
Yingdi Yu | a489875 | 2013-10-15 20:31:37 -0700 | [diff] [blame] | 51 | } |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 52 | |
| 53 | void |
| 54 | AddContactPanel::onCancelClicked() |
| 55 | { this->close(); } |
| 56 | |
| 57 | void |
| 58 | AddContactPanel::onSearchClicked() |
| 59 | { |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 60 | ui->infoView->clear(); |
| 61 | for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--) |
| 62 | ui->infoView->removeRow(i); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 63 | QString inputIdentity = ui->contactInput->text(); |
| 64 | m_searchIdentity = Name(inputIdentity.toUtf8().constData()); |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 65 | |
| 66 | m_contactManager->fetchSelfEndorseCertificate(m_searchIdentity); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
| 70 | AddContactPanel::onAddClicked() |
| 71 | { |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 72 | ContactItem contactItem(*m_currentEndorseCertificate); |
| 73 | m_contactManager->getContactStorage()->addNormalContact(contactItem); |
| 74 | emit newContactAdded(); |
| 75 | this->close(); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 78 | void |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 79 | AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 80 | { |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame^] | 81 | m_currentEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(endorseCertificate)); |
| 82 | const Profile& profile = endorseCertificate.getProfileData()->getProfile(); |
| 83 | ui->infoView->setColumnCount(3); |
| 84 | Profile::const_iterator it = profile.begin(); |
| 85 | int rowCount = 0; |
| 86 | for(; it != profile.end(); it++) |
| 87 | { |
| 88 | ui->infoView->insertRow(rowCount); |
| 89 | QTableWidgetItem *type = new QTableWidgetItem(QString::fromUtf8(it->first.c_str())); |
| 90 | ui->infoView->setItem(rowCount, 0, type); |
| 91 | string valueString(it->second.buf(), it->second.size()); |
| 92 | QTableWidgetItem *value = new QTableWidgetItem(QString::fromUtf8(valueString.c_str())); |
| 93 | ui->infoView->setItem(rowCount, 1, value); |
| 94 | rowCount++; |
| 95 | } |
Yingdi Yu | c29fb98 | 2013-10-20 19:43:10 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity) |
| 100 | { |
| 101 | m_warningDialog->setMsg("Cannot fetch contact profile"); |
| 102 | m_warningDialog->show(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 105 | #if WAF |
| 106 | #include "addcontactpanel.moc" |
| 107 | #include "addcontactpanel.cpp.moc" |
| 108 | #endif |