blob: 80675ea65d43cbef76d05e8c66a6a7b0c1fb45ff [file] [log] [blame]
Yingdi Yud59b5932013-10-16 13:16:49 -07001/* -*- 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 Yua4898752013-10-15 20:31:37 -070011#include "addcontactpanel.h"
12#include "ui_addcontactpanel.h"
13
Yingdi Yuc29fb982013-10-20 19:43:10 -070014#ifndef Q_MOC_RUN
15#include "logging.h"
16#endif
17
Yingdi Yuaa8d7692013-10-18 17:05:02 -070018using namespace ndn;
19using namespace std;
20
Yingdi Yuc29fb982013-10-20 19:43:10 -070021INIT_LOGGER("AddContactPanel");
22
Yingdi Yuaa8d7692013-10-18 17:05:02 -070023AddContactPanel::AddContactPanel(Ptr<ContactManager> contactManager,
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070024 QWidget *parent)
Yingdi Yud59b5932013-10-16 13:16:49 -070025 : QDialog(parent)
26 , ui(new Ui::AddContactPanel)
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070027 , m_contactManager(contactManager)
Yingdi Yuc29fb982013-10-20 19:43:10 -070028 , m_warningDialog(new WarningDialog())
Yingdi Yua4898752013-10-15 20:31:37 -070029{
Yingdi Yud59b5932013-10-16 13:16:49 -070030 ui->setupUi(this);
Yingdi Yuc29fb982013-10-20 19:43:10 -070031
32 qRegisterMetaType<ndn::Name>("NdnName");
33 qRegisterMetaType<EndorseCertificate>("EndorseCertificate");
Yingdi Yud59b5932013-10-16 13:16:49 -070034
35 connect(ui->cancelButton, SIGNAL(clicked()),
36 this, SLOT(onCancelClicked()));
37 connect(ui->searchButton, SIGNAL(clicked()),
38 this, SLOT(onSearchClicked()));
Yingdi Yu79c25a22013-10-21 13:38:38 -070039 connect(ui->addButton, SIGNAL(clicked()),
40 this, SLOT(onAddClicked()));
41 connect(&*m_contactManager, SIGNAL(contactFetched(const EndorseCertificate&)),
42 this, SLOT(selfEndorseCertificateFetched(const EndorseCertificate&)));
Yingdi Yuc29fb982013-10-20 19:43:10 -070043 connect(&*m_contactManager, SIGNAL(contactFetchFailed(const ndn::Name&)),
44 this, SLOT(selfEndorseCertificateFetchFailed(const ndn::Name&)));
Yingdi Yua4898752013-10-15 20:31:37 -070045}
46
47AddContactPanel::~AddContactPanel()
48{
49 delete ui;
Yingdi Yuc29fb982013-10-20 19:43:10 -070050 delete m_warningDialog;
Yingdi Yua4898752013-10-15 20:31:37 -070051}
Yingdi Yud59b5932013-10-16 13:16:49 -070052
53void
54AddContactPanel::onCancelClicked()
55{ this->close(); }
56
57void
58AddContactPanel::onSearchClicked()
59{
Yingdi Yu79c25a22013-10-21 13:38:38 -070060 ui->infoView->clear();
61 for(int i = ui->infoView->rowCount() - 1; i >= 0 ; i--)
62 ui->infoView->removeRow(i);
Yingdi Yuaa8d7692013-10-18 17:05:02 -070063 QString inputIdentity = ui->contactInput->text();
64 m_searchIdentity = Name(inputIdentity.toUtf8().constData());
Yingdi Yuc29fb982013-10-20 19:43:10 -070065
66 m_contactManager->fetchSelfEndorseCertificate(m_searchIdentity);
Yingdi Yud59b5932013-10-16 13:16:49 -070067}
68
69void
70AddContactPanel::onAddClicked()
71{
Yingdi Yu79c25a22013-10-21 13:38:38 -070072 ContactItem contactItem(*m_currentEndorseCertificate);
73 m_contactManager->getContactStorage()->addNormalContact(contactItem);
74 emit newContactAdded();
75 this->close();
Yingdi Yud59b5932013-10-16 13:16:49 -070076}
77
Yingdi Yuaa8d7692013-10-18 17:05:02 -070078void
Yingdi Yuc29fb982013-10-20 19:43:10 -070079AddContactPanel::selfEndorseCertificateFetched(const EndorseCertificate& endorseCertificate)
Yingdi Yuaa8d7692013-10-18 17:05:02 -070080{
Yingdi Yu79c25a22013-10-21 13:38:38 -070081 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 Yuc29fb982013-10-20 19:43:10 -070096}
97
98void
99AddContactPanel::selfEndorseCertificateFetchFailed(const Name& identity)
100{
101 m_warningDialog->setMsg("Cannot fetch contact profile");
102 m_warningDialog->show();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700103}
104
Yingdi Yud59b5932013-10-16 13:16:49 -0700105#if WAF
106#include "addcontactpanel.moc"
107#include "addcontactpanel.cpp.moc"
108#endif