blob: 0ec1e37cd18c3d371a44d15fdc8d1a97597dc6c8 [file] [log] [blame]
Yingdi Yu908f8412013-11-09 00:03:26 -08001/* -*- 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 "browsecontactdialog.h"
13#include "ui_browsecontactdialog.h"
Yingdi Yueb98f7d2013-11-10 01:34:57 -080014#include <QMessageBox>
Yingdi Yu908f8412013-11-09 00:03:26 -080015
16#ifndef Q_MOC_RUN
17#include <boost/asio.hpp>
18#include <boost/tokenizer.hpp>
19#include "logging.h"
20#include "exception.h"
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -080021#include "ndn.cxx/error.h"
Yingdi Yu908f8412013-11-09 00:03:26 -080022#endif
23
24using namespace std;
25using namespace ndn;
26
27INIT_LOGGER("BrowseContactDialog");
28
29// Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
30
31BrowseContactDialog::BrowseContactDialog(Ptr<ContactManager> contactManager,
32 QWidget *parent)
33 : QDialog(parent)
34 , ui(new Ui::BrowseContactDialog)
35 , m_contactManager(contactManager)
36 , m_warningDialog(new WarningDialog)
37 , m_contactListModel(new QStringListModel)
38{
39 // qRegisterMetaType<ndn::security::IdentityCertificate>("NDNIdentityCertificate");
40
41 ui->setupUi(this);
42
43 ui->ContactList->setModel(m_contactListModel);
44
45 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
46 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
47 connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::security::IdentityCertificate &)),
48 this, SLOT(onCertificateFetched(const ndn::security::IdentityCertificate &)));
49 connect(&*m_contactManager, SIGNAL(contactCertificateFetchFailed(const ndn::Name&)),
50 this, SLOT(onCertificateFetchFailed(const ndn::Name&)));
51 connect(&*m_contactManager, SIGNAL(warning(QString)),
52 this, SLOT(onWarning(QString)));
53
54 connect(ui->AddButton, SIGNAL(clicked()),
55 this, SLOT(onAddClicked()));
56
57 connect(ui->CancelButton, SIGNAL(clicked()),
58 this, SLOT(onCancelClicked()));
59}
60
61BrowseContactDialog::~BrowseContactDialog()
62{
63 delete ui;
64}
65
66
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -080067void
68BrowseContactDialog::getCertNames(std::vector<std::string> &names)
Yingdi Yu908f8412013-11-09 00:03:26 -080069{
Yingdi Yu908f8412013-11-09 00:03:26 -080070 try{
71 using namespace boost::asio::ip;
72 tcp::iostream request_stream;
73 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
74 request_stream.connect("ndncert.named-data.net","80");
75 if(!request_stream)
76 {
Yingdi Yueb98f7d2013-11-10 01:34:57 -080077 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #1"));
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -080078 return;
Yingdi Yu908f8412013-11-09 00:03:26 -080079 }
80 request_stream << "GET /cert/list/ HTTP/1.0\r\n";
81 request_stream << "Host: ndncert.named-data.net\r\n\r\n";
82 request_stream.flush();
83
84 string line1;
85 std::getline(request_stream,line1);
86 if (!request_stream)
87 {
Yingdi Yueb98f7d2013-11-10 01:34:57 -080088 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #2"));
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -080089 return;
Yingdi Yu908f8412013-11-09 00:03:26 -080090 }
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -080091
Yingdi Yu908f8412013-11-09 00:03:26 -080092 std::stringstream response_stream(line1);
93 std::string http_version;
94 response_stream >> http_version;
95 unsigned int status_code;
96 response_stream >> status_code;
97 std::string status_message;
98 std::getline(response_stream,status_message);
99
100 if (!response_stream||http_version.substr(0,5)!="HTTP/")
101 {
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800102 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #3"));
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800103 return;
Yingdi Yu908f8412013-11-09 00:03:26 -0800104 }
105 if (status_code!=200)
106 {
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800107 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #4"));
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800108 return;
Yingdi Yu908f8412013-11-09 00:03:26 -0800109 }
110 vector<string> headers;
111 std::string header;
112 while (std::getline(request_stream, header) && header != "\r")
113 headers.push_back(header);
114
Alexander Afanasyev6dd53852013-11-10 16:58:13 -0800115 std::istreambuf_iterator<char> stream_iter (request_stream);
116 std::istreambuf_iterator<char> end_of_stream;
Yingdi Yu908f8412013-11-09 00:03:26 -0800117
Alexander Afanasyev6dd53852013-11-10 16:58:13 -0800118 typedef boost::tokenizer< boost::escaped_list_separator<char>, std::istreambuf_iterator<char> > tokenizer_t;
119 tokenizer_t certItems (stream_iter, end_of_stream,boost::escaped_list_separator<char>('\\', '\n', '"'));
Yingdi Yu908f8412013-11-09 00:03:26 -0800120
Alexander Afanasyev6dd53852013-11-10 16:58:13 -0800121 for (tokenizer_t::iterator it = certItems.begin();
122 it != certItems.end (); it++)
123 {
124 if (!it->empty())
125 {
126 names.push_back(*it);
127 }
128 }
Yingdi Yu908f8412013-11-09 00:03:26 -0800129 }catch(std::exception &e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800130 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #N"));
Yingdi Yu908f8412013-11-09 00:03:26 -0800131 }
132}
133
134void
135BrowseContactDialog::updateCertificateMap(bool filter)
136{
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800137 vector<string> certNameList;
138 getCertNames(certNameList);
139
Yingdi Yu908f8412013-11-09 00:03:26 -0800140 if(filter)
141 {
142 map<Name, Name> certificateMap;
143
144 vector<string>::iterator it = certNameList.begin();
145
146 for(; it != certNameList.end(); it++)
147 {
148 Name newCertName(*it);
149 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(newCertName, true);
150 Name identity = keyName.getPrefix(keyName.size()-1);
151
152 map<Name, Name>::iterator map_it = certificateMap.find(identity);
153 if(map_it != certificateMap.end())
154 {
155 Name oldCertName = map_it->second;
156 Name oldKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(oldCertName, true);
157 if(keyName > oldKeyName)
158 map_it->second = newCertName;
159 else if(keyName == oldKeyName && newCertName > oldCertName)
160 map_it->second = newCertName;
161 }
162 else
163 {
164 certificateMap.insert(pair<Name, Name>(identity, newCertName));
165 }
166 }
167 map<Name, Name>::iterator map_it = certificateMap.begin();
168 for(; map_it != certificateMap.end(); map_it++)
169 m_certificateNameList.push_back(map_it->second);
170 }
171 else
172 {
173 vector<string>::iterator it = certNameList.begin();
174
175 for(; it != certNameList.end(); it++)
176 {
Alexander Afanasyev6dd53852013-11-10 16:58:13 -0800177 try {
178 m_certificateNameList.push_back(Name (*it));
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800179 }
180 catch(error::Name)
181 {
Alexander Afanasyev6dd53852013-11-10 16:58:13 -0800182 _LOG_ERROR ("Error parsing: [" << *it << "]");
183 }
184 catch(error::name::Component)
185 {
186 _LOG_ERROR ("Error parsing: [" << *it << "]");
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800187 }
Yingdi Yu908f8412013-11-09 00:03:26 -0800188 }
189 }
190}
191
192void
193BrowseContactDialog::fetchCertificate()
194{
195 vector<Name>::iterator it = m_certificateNameList.begin();
196 int count = 0;
197 for(; it != m_certificateNameList.end(); it++)
198 {
199 m_contactManager->fetchIdCertificate(*it);
200 }
201}
202
203void
204BrowseContactDialog::onCertificateFetched(const security::IdentityCertificate& identityCertificate)
205{
206 Name certName = identityCertificate.getName();
207 Name certNameNoVersion = certName.getPrefix(certName.size()-1);
208 m_certificateMap.insert(pair<Name, security::IdentityCertificate>(certNameNoVersion, identityCertificate));
209 m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate)));
210 string name(m_profileMap[certNameNoVersion].getProfileEntry("name")->buf(), m_profileMap[certNameNoVersion].getProfileEntry("name")->size());
211 // Name contactName = m_profileMap[certNameNoVersion].getIdentityName();
212 {
213 UniqueRecLock lock(m_mutex);
214 m_contactList << QString::fromStdString(name);
215 m_contactListModel->setStringList(m_contactList);
216 m_contactNameList.push_back(certNameNoVersion);
217 }
218}
219
220void
221BrowseContactDialog::onCertificateFetchFailed(const Name& identity)
222{}
223
224void
225BrowseContactDialog::refreshList()
226{
227 {
228 UniqueRecLock lock(m_mutex);
229 m_contactList.clear();
230 m_contactNameList.clear();
231 }
232 m_certificateNameList.clear();
233 m_certificateMap.clear();
234 m_profileMap.clear();
235
236 updateCertificateMap();
237
238 fetchCertificate();
239}
240
241void
242BrowseContactDialog::updateSelection(const QItemSelection &selected,
243 const QItemSelection &deselected)
244{
245 QModelIndexList items = selected.indexes();
246 Name certName = m_contactNameList[items.first().row()];
247
248 ui->InfoTable->clear();
249 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
250 ui->InfoTable->removeRow(i);
251
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800252 ui->InfoTable->horizontalHeader()->show();
253
Yingdi Yu908f8412013-11-09 00:03:26 -0800254 map<Name, Profile>::iterator it = m_profileMap.find(certName);
255 if(it != m_profileMap.end())
256 {
257 ui->InfoTable->setColumnCount(2);
258
259 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
260 ui->InfoTable->setHorizontalHeaderItem(0, typeHeader);
261 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
262 ui->InfoTable->setHorizontalHeaderItem(1, valueHeader);
263
264 Profile::const_iterator pro_it = it->second.begin();
265 int rowCount = 0;
266
267 for(; pro_it != it->second.end(); pro_it++, rowCount++)
268 {
269 ui->InfoTable->insertRow(rowCount);
270 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first));
271 ui->InfoTable->setItem(rowCount, 0, type);
272
273 string valueString(pro_it->second.buf(), pro_it->second.size());
274 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(valueString));
275 ui->InfoTable->setItem(rowCount, 1, value);
276 }
277 }
278}
279
280void
281BrowseContactDialog::onWarning(QString msg)
282{
283 m_warningDialog->setMsg(msg.toStdString());
284 m_warningDialog->show();
285}
286
287void
288BrowseContactDialog::onAddClicked()
289{
290 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
291 QModelIndexList selectedList = selectionModel->selectedIndexes();
292 QModelIndexList::iterator it = selectedList.begin();
293 for(; it != selectedList.end(); it++)
294 {
295 Name certName = m_contactNameList[it->row()];
296 if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end())
297 m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]);
298 else
299 {
300 m_warningDialog->setMsg("Not enough information to add contact!");
301 m_warningDialog->show();
302 }
303 }
304 emit newContactAdded();
305 this->close();
306}
307
308void
309BrowseContactDialog::onCancelClicked()
310{ this->close(); }
311
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800312void
313BrowseContactDialog::closeEvent(QCloseEvent *e)
314{
315 ui->InfoTable->clear();
316 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
317 ui->InfoTable->removeRow(i);
318 ui->InfoTable->horizontalHeader()->hide();
319
320 hide();
321 e->ignore();
322}
323
Yingdi Yu908f8412013-11-09 00:03:26 -0800324#if WAF
325#include "browsecontactdialog.moc"
326#include "browsecontactdialog.cpp.moc"
327#endif