blob: 58396baf6ca9171b15792a5f7d66ce6b109ed012 [file] [log] [blame]
Yingdi Yuae8217c2013-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 Yue433a502013-11-10 01:34:57 -080014#include <QMessageBox>
Yingdi Yuae8217c2013-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"
Yingdi Yu76dd8002013-12-24 11:16:32 +080021// #include "ndn.cxx/error.h"
Yingdi Yuae8217c2013-11-09 00:03:26 -080022#endif
23
24using namespace std;
25using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080026using namespace ndn::ptr_lib;
Yingdi Yuae8217c2013-11-09 00:03:26 -080027
28INIT_LOGGER("BrowseContactDialog");
29
30// Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
31
Yingdi Yu76dd8002013-12-24 11:16:32 +080032BrowseContactDialog::BrowseContactDialog(shared_ptr<ContactManager> contactManager,
Yingdi Yuae8217c2013-11-09 00:03:26 -080033 QWidget *parent)
34 : QDialog(parent)
35 , ui(new Ui::BrowseContactDialog)
36 , m_contactManager(contactManager)
37 , m_warningDialog(new WarningDialog)
38 , m_contactListModel(new QStringListModel)
39{
40 // qRegisterMetaType<ndn::security::IdentityCertificate>("NDNIdentityCertificate");
41
42 ui->setupUi(this);
43
44 ui->ContactList->setModel(m_contactListModel);
45
46 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
47 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu6eabbd72013-12-27 08:44:12 +080048 connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::IdentityCertificate &)),
49 this, SLOT(onCertificateFetched(const ndn::IdentityCertificate &)));
Yingdi Yuae8217c2013-11-09 00:03:26 -080050 connect(&*m_contactManager, SIGNAL(contactCertificateFetchFailed(const ndn::Name&)),
51 this, SLOT(onCertificateFetchFailed(const ndn::Name&)));
52 connect(&*m_contactManager, SIGNAL(warning(QString)),
53 this, SLOT(onWarning(QString)));
54
55 connect(ui->AddButton, SIGNAL(clicked()),
56 this, SLOT(onAddClicked()));
57
Yingdi Yu9d7dfc22013-11-10 17:59:58 -080058 connect(ui->DirectAddButton, SIGNAL(clicked()),
59 this, SLOT(onDirectAddClicked()));
Yingdi Yuae8217c2013-11-09 00:03:26 -080060}
61
62BrowseContactDialog::~BrowseContactDialog()
63{
64 delete ui;
65}
66
67
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080068void
69BrowseContactDialog::getCertNames(std::vector<std::string> &names)
Yingdi Yuae8217c2013-11-09 00:03:26 -080070{
Yingdi Yuae8217c2013-11-09 00:03:26 -080071 try{
72 using namespace boost::asio::ip;
73 tcp::iostream request_stream;
Yingdi Yu6eabbd72013-12-27 08:44:12 +080074 request_stream.expires_from_now(boost::posix_time::milliseconds(5000));
Yingdi Yuae8217c2013-11-09 00:03:26 -080075 request_stream.connect("ndncert.named-data.net","80");
76 if(!request_stream)
77 {
Yingdi Yue433a502013-11-10 01:34:57 -080078 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #1"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080079 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -080080 }
81 request_stream << "GET /cert/list/ HTTP/1.0\r\n";
82 request_stream << "Host: ndncert.named-data.net\r\n\r\n";
83 request_stream.flush();
84
85 string line1;
86 std::getline(request_stream,line1);
87 if (!request_stream)
88 {
Yingdi Yue433a502013-11-10 01:34:57 -080089 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #2"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080090 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -080091 }
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080092
Yingdi Yuae8217c2013-11-09 00:03:26 -080093 std::stringstream response_stream(line1);
94 std::string http_version;
95 response_stream >> http_version;
96 unsigned int status_code;
97 response_stream >> status_code;
98 std::string status_message;
99 std::getline(response_stream,status_message);
100
101 if (!response_stream||http_version.substr(0,5)!="HTTP/")
102 {
Yingdi Yue433a502013-11-10 01:34:57 -0800103 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #3"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800104 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800105 }
106 if (status_code!=200)
107 {
Yingdi Yue433a502013-11-10 01:34:57 -0800108 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #4"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800109 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800110 }
111 vector<string> headers;
112 std::string header;
113 while (std::getline(request_stream, header) && header != "\r")
114 headers.push_back(header);
115
Alexander Afanasyev4294a292013-11-10 16:58:13 -0800116 std::istreambuf_iterator<char> stream_iter (request_stream);
117 std::istreambuf_iterator<char> end_of_stream;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800118
Alexander Afanasyev4294a292013-11-10 16:58:13 -0800119 typedef boost::tokenizer< boost::escaped_list_separator<char>, std::istreambuf_iterator<char> > tokenizer_t;
120 tokenizer_t certItems (stream_iter, end_of_stream,boost::escaped_list_separator<char>('\\', '\n', '"'));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800121
Alexander Afanasyev4294a292013-11-10 16:58:13 -0800122 for (tokenizer_t::iterator it = certItems.begin();
123 it != certItems.end (); it++)
124 {
125 if (!it->empty())
126 {
127 names.push_back(*it);
128 }
129 }
Yingdi Yuae8217c2013-11-09 00:03:26 -0800130 }catch(std::exception &e){
Yingdi Yue433a502013-11-10 01:34:57 -0800131 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #N"));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800132 }
133}
134
135void
136BrowseContactDialog::updateCertificateMap(bool filter)
137{
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800138 vector<string> certNameList;
139 getCertNames(certNameList);
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800140 _LOG_DEBUG("Get Certificate Name List");
141 for(int i = 0; i < certNameList.size(); i++)
142 {
143 _LOG_DEBUG(" " << certNameList[i]);
144 }
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800145
Yingdi Yuae8217c2013-11-09 00:03:26 -0800146 if(filter)
147 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800148 map<Name, Name, Name::BreadthFirstLess> certificateMap;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800149
150 vector<string>::iterator it = certNameList.begin();
151
152 for(; it != certNameList.end(); it++)
153 {
154 Name newCertName(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800155 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(newCertName);
156 Name identity = keyName.getPrefix(-1);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800157
158 map<Name, Name>::iterator map_it = certificateMap.find(identity);
159 if(map_it != certificateMap.end())
160 {
161 Name oldCertName = map_it->second;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800162 Name oldKeyName = IdentityCertificate::certificateNameToPublicKeyName(oldCertName);
163 if(keyName.get(-1).toEscapedString() > oldKeyName.get(-1).toEscapedString())
Yingdi Yuae8217c2013-11-09 00:03:26 -0800164 map_it->second = newCertName;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800165 else if(keyName == oldKeyName && newCertName.get(-1).toVersion() > oldCertName.get(-1).toVersion())
Yingdi Yuae8217c2013-11-09 00:03:26 -0800166 map_it->second = newCertName;
167 }
168 else
169 {
170 certificateMap.insert(pair<Name, Name>(identity, newCertName));
171 }
172 }
173 map<Name, Name>::iterator map_it = certificateMap.begin();
174 for(; map_it != certificateMap.end(); map_it++)
175 m_certificateNameList.push_back(map_it->second);
176 }
177 else
178 {
179 vector<string>::iterator it = certNameList.begin();
180
181 for(; it != certNameList.end(); it++)
182 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800183 m_certificateNameList.push_back(Name (*it));
184 // try {
185 // m_certificateNameList.push_back(Name (*it));
186 // }
187 // catch(error::Name)
188 // {
189 // _LOG_ERROR ("Error parsing: [" << *it << "]");
190 // }
191 // catch(error::name::Component)
192 // {
193 // _LOG_ERROR ("Error parsing: [" << *it << "]");
194 // }
Yingdi Yuae8217c2013-11-09 00:03:26 -0800195 }
196 }
197}
198
199void
200BrowseContactDialog::fetchCertificate()
201{
202 vector<Name>::iterator it = m_certificateNameList.begin();
203 int count = 0;
204 for(; it != m_certificateNameList.end(); it++)
205 {
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800206 _LOG_DEBUG("fetch " << it->toUri());
207 usleep(1000);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800208 m_contactManager->fetchIdCertificate(*it);
209 }
210}
211
212void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800213BrowseContactDialog::onCertificateFetched(const IdentityCertificate& identityCertificate)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800214{
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800215 Name certNameNoVersion = identityCertificate.getName().getPrefix(-1);
216 _LOG_DEBUG("Fetch: " << certNameNoVersion.toUri());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800217 m_certificateMap.insert(pair<Name, IdentityCertificate>(certNameNoVersion, identityCertificate));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800218 m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate)));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800219 string name = m_profileMap[certNameNoVersion].getProfileEntry("name");
Yingdi Yuae8217c2013-11-09 00:03:26 -0800220 // Name contactName = m_profileMap[certNameNoVersion].getIdentityName();
221 {
222 UniqueRecLock lock(m_mutex);
223 m_contactList << QString::fromStdString(name);
224 m_contactListModel->setStringList(m_contactList);
225 m_contactNameList.push_back(certNameNoVersion);
226 }
227}
228
229void
230BrowseContactDialog::onCertificateFetchFailed(const Name& identity)
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800231{
232 _LOG_DEBUG("Cannot fetch " << identity.toUri());
233}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800234
235void
236BrowseContactDialog::refreshList()
237{
238 {
239 UniqueRecLock lock(m_mutex);
240 m_contactList.clear();
241 m_contactNameList.clear();
242 }
243 m_certificateNameList.clear();
244 m_certificateMap.clear();
245 m_profileMap.clear();
246
247 updateCertificateMap();
248
249 fetchCertificate();
250}
251
252void
253BrowseContactDialog::updateSelection(const QItemSelection &selected,
254 const QItemSelection &deselected)
255{
256 QModelIndexList items = selected.indexes();
257 Name certName = m_contactNameList[items.first().row()];
258
259 ui->InfoTable->clear();
260 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
261 ui->InfoTable->removeRow(i);
262
Yingdi Yue433a502013-11-10 01:34:57 -0800263 ui->InfoTable->horizontalHeader()->show();
264
Yingdi Yuae8217c2013-11-09 00:03:26 -0800265 map<Name, Profile>::iterator it = m_profileMap.find(certName);
266 if(it != m_profileMap.end())
267 {
268 ui->InfoTable->setColumnCount(2);
269
270 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
271 ui->InfoTable->setHorizontalHeaderItem(0, typeHeader);
272 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
273 ui->InfoTable->setHorizontalHeaderItem(1, valueHeader);
274
275 Profile::const_iterator pro_it = it->second.begin();
276 int rowCount = 0;
277
278 for(; pro_it != it->second.end(); pro_it++, rowCount++)
279 {
280 ui->InfoTable->insertRow(rowCount);
281 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first));
282 ui->InfoTable->setItem(rowCount, 0, type);
283
Yingdi Yu76dd8002013-12-24 11:16:32 +0800284 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(pro_it->second));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800285 ui->InfoTable->setItem(rowCount, 1, value);
286 }
287 }
288}
289
290void
291BrowseContactDialog::onWarning(QString msg)
292{
293 m_warningDialog->setMsg(msg.toStdString());
294 m_warningDialog->show();
295}
296
297void
298BrowseContactDialog::onAddClicked()
299{
300 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
301 QModelIndexList selectedList = selectionModel->selectedIndexes();
302 QModelIndexList::iterator it = selectedList.begin();
303 for(; it != selectedList.end(); it++)
304 {
305 Name certName = m_contactNameList[it->row()];
306 if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end())
307 m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]);
308 else
309 {
310 m_warningDialog->setMsg("Not enough information to add contact!");
311 m_warningDialog->show();
312 }
313 }
314 emit newContactAdded();
315 this->close();
316}
317
318void
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800319BrowseContactDialog::onDirectAddClicked()
320{
321 emit directAddClicked();
322 this->close();
323}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800324
Yingdi Yue433a502013-11-10 01:34:57 -0800325void
326BrowseContactDialog::closeEvent(QCloseEvent *e)
327{
328 ui->InfoTable->clear();
329 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
330 ui->InfoTable->removeRow(i);
331 ui->InfoTable->horizontalHeader()->hide();
332
333 hide();
334 e->ignore();
335}
336
Yingdi Yuae8217c2013-11-09 00:03:26 -0800337#if WAF
338#include "browsecontactdialog.moc"
339#include "browsecontactdialog.cpp.moc"
340#endif