blob: c84b5f973c23d62811f689a68f9f89d4e5343785 [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"
Yingdi Yu64206112013-12-24 11:16:32 +080020// #include "ndn.cxx/error.h"
Yingdi Yu908f8412013-11-09 00:03:26 -080021#endif
22
23using namespace std;
24using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080025using namespace ndn::ptr_lib;
Yingdi Yu908f8412013-11-09 00:03:26 -080026
27INIT_LOGGER("BrowseContactDialog");
28
29// Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
30
Yingdi Yu64206112013-12-24 11:16:32 +080031BrowseContactDialog::BrowseContactDialog(shared_ptr<ContactManager> contactManager,
Yingdi Yu908f8412013-11-09 00:03:26 -080032 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 &)));
Yingdi Yuaccbda92013-12-27 08:44:12 +080047 connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::IdentityCertificate &)),
48 this, SLOT(onCertificateFetched(const ndn::IdentityCertificate &)));
Yingdi Yu908f8412013-11-09 00:03:26 -080049 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
Yingdi Yu3cd30df2013-11-10 17:59:58 -080057 connect(ui->DirectAddButton, SIGNAL(clicked()),
58 this, SLOT(onDirectAddClicked()));
Yingdi Yu908f8412013-11-09 00:03:26 -080059}
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;
Yingdi Yuaccbda92013-12-27 08:44:12 +080073 request_stream.expires_from_now(boost::posix_time::milliseconds(5000));
Yingdi Yu908f8412013-11-09 00:03:26 -080074 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);
Yingdi Yuaccbda92013-12-27 08:44:12 +0800139 _LOG_DEBUG("Get Certificate Name List");
140 for(int i = 0; i < certNameList.size(); i++)
141 {
142 _LOG_DEBUG(" " << certNameList[i]);
143 }
Alexander Afanasyev19aec3c2013-11-09 23:07:48 -0800144
Yingdi Yu908f8412013-11-09 00:03:26 -0800145 if(filter)
146 {
Yingdi Yu64206112013-12-24 11:16:32 +0800147 map<Name, Name, Name::BreadthFirstLess> certificateMap;
Yingdi Yu908f8412013-11-09 00:03:26 -0800148
149 vector<string>::iterator it = certNameList.begin();
150
151 for(; it != certNameList.end(); it++)
152 {
153 Name newCertName(*it);
Yingdi Yu64206112013-12-24 11:16:32 +0800154 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(newCertName);
155 Name identity = keyName.getPrefix(-1);
Yingdi Yu908f8412013-11-09 00:03:26 -0800156
157 map<Name, Name>::iterator map_it = certificateMap.find(identity);
158 if(map_it != certificateMap.end())
159 {
160 Name oldCertName = map_it->second;
Yingdi Yu64206112013-12-24 11:16:32 +0800161 Name oldKeyName = IdentityCertificate::certificateNameToPublicKeyName(oldCertName);
162 if(keyName.get(-1).toEscapedString() > oldKeyName.get(-1).toEscapedString())
Yingdi Yu908f8412013-11-09 00:03:26 -0800163 map_it->second = newCertName;
Yingdi Yu64206112013-12-24 11:16:32 +0800164 else if(keyName == oldKeyName && newCertName.get(-1).toVersion() > oldCertName.get(-1).toVersion())
Yingdi Yu908f8412013-11-09 00:03:26 -0800165 map_it->second = newCertName;
166 }
167 else
168 {
169 certificateMap.insert(pair<Name, Name>(identity, newCertName));
170 }
171 }
172 map<Name, Name>::iterator map_it = certificateMap.begin();
173 for(; map_it != certificateMap.end(); map_it++)
174 m_certificateNameList.push_back(map_it->second);
175 }
176 else
177 {
178 vector<string>::iterator it = certNameList.begin();
179
180 for(; it != certNameList.end(); it++)
181 {
Yingdi Yu64206112013-12-24 11:16:32 +0800182 m_certificateNameList.push_back(Name (*it));
183 // try {
184 // m_certificateNameList.push_back(Name (*it));
185 // }
186 // catch(error::Name)
187 // {
188 // _LOG_ERROR ("Error parsing: [" << *it << "]");
189 // }
190 // catch(error::name::Component)
191 // {
192 // _LOG_ERROR ("Error parsing: [" << *it << "]");
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 {
Yingdi Yuaccbda92013-12-27 08:44:12 +0800205 _LOG_DEBUG("fetch " << it->toUri());
206 usleep(1000);
Yingdi Yu908f8412013-11-09 00:03:26 -0800207 m_contactManager->fetchIdCertificate(*it);
208 }
209}
210
211void
Yingdi Yu64206112013-12-24 11:16:32 +0800212BrowseContactDialog::onCertificateFetched(const IdentityCertificate& identityCertificate)
Yingdi Yu908f8412013-11-09 00:03:26 -0800213{
Yingdi Yuaccbda92013-12-27 08:44:12 +0800214 Name certNameNoVersion = identityCertificate.getName().getPrefix(-1);
215 _LOG_DEBUG("Fetch: " << certNameNoVersion.toUri());
Yingdi Yu64206112013-12-24 11:16:32 +0800216 m_certificateMap.insert(pair<Name, IdentityCertificate>(certNameNoVersion, identityCertificate));
Yingdi Yu908f8412013-11-09 00:03:26 -0800217 m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate)));
Yingdi Yu64206112013-12-24 11:16:32 +0800218 string name = m_profileMap[certNameNoVersion].getProfileEntry("name");
Yingdi Yu908f8412013-11-09 00:03:26 -0800219 // Name contactName = m_profileMap[certNameNoVersion].getIdentityName();
220 {
221 UniqueRecLock lock(m_mutex);
222 m_contactList << QString::fromStdString(name);
223 m_contactListModel->setStringList(m_contactList);
224 m_contactNameList.push_back(certNameNoVersion);
225 }
226}
227
228void
229BrowseContactDialog::onCertificateFetchFailed(const Name& identity)
Yingdi Yuaccbda92013-12-27 08:44:12 +0800230{
231 _LOG_DEBUG("Cannot fetch " << identity.toUri());
232}
Yingdi Yu908f8412013-11-09 00:03:26 -0800233
234void
235BrowseContactDialog::refreshList()
236{
237 {
238 UniqueRecLock lock(m_mutex);
239 m_contactList.clear();
240 m_contactNameList.clear();
241 }
242 m_certificateNameList.clear();
243 m_certificateMap.clear();
244 m_profileMap.clear();
245
246 updateCertificateMap();
247
248 fetchCertificate();
249}
250
251void
252BrowseContactDialog::updateSelection(const QItemSelection &selected,
253 const QItemSelection &deselected)
254{
255 QModelIndexList items = selected.indexes();
256 Name certName = m_contactNameList[items.first().row()];
257
258 ui->InfoTable->clear();
259 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
260 ui->InfoTable->removeRow(i);
261
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800262 ui->InfoTable->horizontalHeader()->show();
263
Yingdi Yu908f8412013-11-09 00:03:26 -0800264 map<Name, Profile>::iterator it = m_profileMap.find(certName);
265 if(it != m_profileMap.end())
266 {
267 ui->InfoTable->setColumnCount(2);
268
269 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
270 ui->InfoTable->setHorizontalHeaderItem(0, typeHeader);
271 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
272 ui->InfoTable->setHorizontalHeaderItem(1, valueHeader);
273
274 Profile::const_iterator pro_it = it->second.begin();
275 int rowCount = 0;
276
277 for(; pro_it != it->second.end(); pro_it++, rowCount++)
278 {
279 ui->InfoTable->insertRow(rowCount);
280 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first));
281 ui->InfoTable->setItem(rowCount, 0, type);
282
Yingdi Yu64206112013-12-24 11:16:32 +0800283 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(pro_it->second));
Yingdi Yu908f8412013-11-09 00:03:26 -0800284 ui->InfoTable->setItem(rowCount, 1, value);
285 }
286 }
287}
288
289void
290BrowseContactDialog::onWarning(QString msg)
291{
292 m_warningDialog->setMsg(msg.toStdString());
293 m_warningDialog->show();
294}
295
296void
297BrowseContactDialog::onAddClicked()
298{
299 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
300 QModelIndexList selectedList = selectionModel->selectedIndexes();
301 QModelIndexList::iterator it = selectedList.begin();
302 for(; it != selectedList.end(); it++)
303 {
304 Name certName = m_contactNameList[it->row()];
305 if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end())
306 m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]);
307 else
308 {
309 m_warningDialog->setMsg("Not enough information to add contact!");
310 m_warningDialog->show();
311 }
312 }
313 emit newContactAdded();
314 this->close();
315}
316
317void
Yingdi Yu3cd30df2013-11-10 17:59:58 -0800318BrowseContactDialog::onDirectAddClicked()
319{
320 emit directAddClicked();
321 this->close();
322}
Yingdi Yu908f8412013-11-09 00:03:26 -0800323
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800324void
325BrowseContactDialog::closeEvent(QCloseEvent *e)
326{
327 ui->InfoTable->clear();
328 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
329 ui->InfoTable->removeRow(i);
330 ui->InfoTable->horizontalHeader()->hide();
331
332 hide();
333 e->ignore();
334}
335
Yingdi Yu908f8412013-11-09 00:03:26 -0800336#if WAF
337#include "browsecontactdialog.moc"
338#include "browsecontactdialog.cpp.moc"
339#endif