blob: 78a6ab4dfe9adc76f71802f306d93ab6e76d049e [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
115 std::stringbuf buffer;
116 std::ostream os (&buffer);
117
118 os << request_stream.rdbuf();
119
120 using boost::tokenizer;
121 using boost::escaped_list_separator;
122
123 tokenizer<escaped_list_separator<char> > certItems(buffer.str(), escaped_list_separator<char> ('\\', '\n', '"'));
124 tokenizer<escaped_list_separator<char> >::iterator it = certItems.begin();
125 try{
126 while (it != certItems.end())
127 {
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800128 if (!it->empty())
129 {
130 names.push_back(*it);
131 }
Yingdi Yu908f8412013-11-09 00:03:26 -0800132 it++;
133 }
134 }catch (exception &e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800135 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #5"));
Yingdi Yu908f8412013-11-09 00:03:26 -0800136 }
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800137
Yingdi Yu908f8412013-11-09 00:03:26 -0800138 }catch(std::exception &e){
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800139 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #N"));
Yingdi Yu908f8412013-11-09 00:03:26 -0800140 }
141}
142
143void
144BrowseContactDialog::updateCertificateMap(bool filter)
145{
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800146 vector<string> certNameList;
147 getCertNames(certNameList);
148
Yingdi Yu908f8412013-11-09 00:03:26 -0800149 if(filter)
150 {
151 map<Name, Name> certificateMap;
152
153 vector<string>::iterator it = certNameList.begin();
154
155 for(; it != certNameList.end(); it++)
156 {
157 Name newCertName(*it);
158 Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(newCertName, true);
159 Name identity = keyName.getPrefix(keyName.size()-1);
160
161 map<Name, Name>::iterator map_it = certificateMap.find(identity);
162 if(map_it != certificateMap.end())
163 {
164 Name oldCertName = map_it->second;
165 Name oldKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(oldCertName, true);
166 if(keyName > oldKeyName)
167 map_it->second = newCertName;
168 else if(keyName == oldKeyName && newCertName > oldCertName)
169 map_it->second = newCertName;
170 }
171 else
172 {
173 certificateMap.insert(pair<Name, Name>(identity, newCertName));
174 }
175 }
176 map<Name, Name>::iterator map_it = certificateMap.begin();
177 for(; map_it != certificateMap.end(); map_it++)
178 m_certificateNameList.push_back(map_it->second);
179 }
180 else
181 {
182 vector<string>::iterator it = certNameList.begin();
183
184 for(; it != certNameList.end(); it++)
185 {
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800186 try{
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800187 Name name(*it);
188 m_certificateNameList.push_back(name);
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800189 }
190 catch(error::Name)
191 {
192 ; // do nothing
193 }
Yingdi Yu908f8412013-11-09 00:03:26 -0800194 }
195 }
196}
197
198void
199BrowseContactDialog::fetchCertificate()
200{
201 vector<Name>::iterator it = m_certificateNameList.begin();
202 int count = 0;
203 for(; it != m_certificateNameList.end(); it++)
204 {
205 m_contactManager->fetchIdCertificate(*it);
206 }
207}
208
209void
210BrowseContactDialog::onCertificateFetched(const security::IdentityCertificate& identityCertificate)
211{
212 Name certName = identityCertificate.getName();
213 Name certNameNoVersion = certName.getPrefix(certName.size()-1);
214 m_certificateMap.insert(pair<Name, security::IdentityCertificate>(certNameNoVersion, identityCertificate));
215 m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate)));
216 string name(m_profileMap[certNameNoVersion].getProfileEntry("name")->buf(), m_profileMap[certNameNoVersion].getProfileEntry("name")->size());
217 // Name contactName = m_profileMap[certNameNoVersion].getIdentityName();
218 {
219 UniqueRecLock lock(m_mutex);
220 m_contactList << QString::fromStdString(name);
221 m_contactListModel->setStringList(m_contactList);
222 m_contactNameList.push_back(certNameNoVersion);
223 }
224}
225
226void
227BrowseContactDialog::onCertificateFetchFailed(const Name& identity)
228{}
229
230void
231BrowseContactDialog::refreshList()
232{
233 {
234 UniqueRecLock lock(m_mutex);
235 m_contactList.clear();
236 m_contactNameList.clear();
237 }
238 m_certificateNameList.clear();
239 m_certificateMap.clear();
240 m_profileMap.clear();
241
242 updateCertificateMap();
243
244 fetchCertificate();
245}
246
247void
248BrowseContactDialog::updateSelection(const QItemSelection &selected,
249 const QItemSelection &deselected)
250{
251 QModelIndexList items = selected.indexes();
252 Name certName = m_contactNameList[items.first().row()];
253
254 ui->InfoTable->clear();
255 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
256 ui->InfoTable->removeRow(i);
257
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800258 ui->InfoTable->horizontalHeader()->show();
259
Yingdi Yu908f8412013-11-09 00:03:26 -0800260 map<Name, Profile>::iterator it = m_profileMap.find(certName);
261 if(it != m_profileMap.end())
262 {
263 ui->InfoTable->setColumnCount(2);
264
265 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
266 ui->InfoTable->setHorizontalHeaderItem(0, typeHeader);
267 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
268 ui->InfoTable->setHorizontalHeaderItem(1, valueHeader);
269
270 Profile::const_iterator pro_it = it->second.begin();
271 int rowCount = 0;
272
273 for(; pro_it != it->second.end(); pro_it++, rowCount++)
274 {
275 ui->InfoTable->insertRow(rowCount);
276 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first));
277 ui->InfoTable->setItem(rowCount, 0, type);
278
279 string valueString(pro_it->second.buf(), pro_it->second.size());
280 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(valueString));
281 ui->InfoTable->setItem(rowCount, 1, value);
282 }
283 }
284}
285
286void
287BrowseContactDialog::onWarning(QString msg)
288{
289 m_warningDialog->setMsg(msg.toStdString());
290 m_warningDialog->show();
291}
292
293void
294BrowseContactDialog::onAddClicked()
295{
296 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
297 QModelIndexList selectedList = selectionModel->selectedIndexes();
298 QModelIndexList::iterator it = selectedList.begin();
299 for(; it != selectedList.end(); it++)
300 {
301 Name certName = m_contactNameList[it->row()];
302 if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end())
303 m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]);
304 else
305 {
306 m_warningDialog->setMsg("Not enough information to add contact!");
307 m_warningDialog->show();
308 }
309 }
310 emit newContactAdded();
311 this->close();
312}
313
314void
315BrowseContactDialog::onCancelClicked()
316{ this->close(); }
317
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800318void
319BrowseContactDialog::closeEvent(QCloseEvent *e)
320{
321 ui->InfoTable->clear();
322 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
323 ui->InfoTable->removeRow(i);
324 ui->InfoTable->horizontalHeader()->hide();
325
326 hide();
327 e->ignore();
328}
329
Yingdi Yu908f8412013-11-09 00:03:26 -0800330#if WAF
331#include "browsecontactdialog.moc"
332#include "browsecontactdialog.cpp.moc"
333#endif