blob: ee783e5f72fe0236b6d42b9854d8cb2f5a652ecc [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 &)));
48 connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::security::IdentityCertificate &)),
49 this, SLOT(onCertificateFetched(const ndn::security::IdentityCertificate &)));
50 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;
74 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
75 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);
140
Yingdi Yuae8217c2013-11-09 00:03:26 -0800141 if(filter)
142 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800143 map<Name, Name, Name::BreadthFirstLess> certificateMap;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800144
145 vector<string>::iterator it = certNameList.begin();
146
147 for(; it != certNameList.end(); it++)
148 {
149 Name newCertName(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800150 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(newCertName);
151 Name identity = keyName.getPrefix(-1);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800152
153 map<Name, Name>::iterator map_it = certificateMap.find(identity);
154 if(map_it != certificateMap.end())
155 {
156 Name oldCertName = map_it->second;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800157 Name oldKeyName = IdentityCertificate::certificateNameToPublicKeyName(oldCertName);
158 if(keyName.get(-1).toEscapedString() > oldKeyName.get(-1).toEscapedString())
Yingdi Yuae8217c2013-11-09 00:03:26 -0800159 map_it->second = newCertName;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800160 else if(keyName == oldKeyName && newCertName.get(-1).toVersion() > oldCertName.get(-1).toVersion())
Yingdi Yuae8217c2013-11-09 00:03:26 -0800161 map_it->second = newCertName;
162 }
163 else
164 {
165 certificateMap.insert(pair<Name, Name>(identity, newCertName));
166 }
167 }
168 map<Name, Name>::iterator map_it = certificateMap.begin();
169 for(; map_it != certificateMap.end(); map_it++)
170 m_certificateNameList.push_back(map_it->second);
171 }
172 else
173 {
174 vector<string>::iterator it = certNameList.begin();
175
176 for(; it != certNameList.end(); it++)
177 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800178 m_certificateNameList.push_back(Name (*it));
179 // try {
180 // m_certificateNameList.push_back(Name (*it));
181 // }
182 // catch(error::Name)
183 // {
184 // _LOG_ERROR ("Error parsing: [" << *it << "]");
185 // }
186 // catch(error::name::Component)
187 // {
188 // _LOG_ERROR ("Error parsing: [" << *it << "]");
189 // }
Yingdi Yuae8217c2013-11-09 00:03:26 -0800190 }
191 }
192}
193
194void
195BrowseContactDialog::fetchCertificate()
196{
197 vector<Name>::iterator it = m_certificateNameList.begin();
198 int count = 0;
199 for(; it != m_certificateNameList.end(); it++)
200 {
201 m_contactManager->fetchIdCertificate(*it);
202 }
203}
204
205void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800206BrowseContactDialog::onCertificateFetched(const IdentityCertificate& identityCertificate)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800207{
208 Name certName = identityCertificate.getName();
209 Name certNameNoVersion = certName.getPrefix(certName.size()-1);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800210 m_certificateMap.insert(pair<Name, IdentityCertificate>(certNameNoVersion, identityCertificate));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800211 m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate)));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800212 string name = m_profileMap[certNameNoVersion].getProfileEntry("name");
Yingdi Yuae8217c2013-11-09 00:03:26 -0800213 // Name contactName = m_profileMap[certNameNoVersion].getIdentityName();
214 {
215 UniqueRecLock lock(m_mutex);
216 m_contactList << QString::fromStdString(name);
217 m_contactListModel->setStringList(m_contactList);
218 m_contactNameList.push_back(certNameNoVersion);
219 }
220}
221
222void
223BrowseContactDialog::onCertificateFetchFailed(const Name& identity)
224{}
225
226void
227BrowseContactDialog::refreshList()
228{
229 {
230 UniqueRecLock lock(m_mutex);
231 m_contactList.clear();
232 m_contactNameList.clear();
233 }
234 m_certificateNameList.clear();
235 m_certificateMap.clear();
236 m_profileMap.clear();
237
238 updateCertificateMap();
239
240 fetchCertificate();
241}
242
243void
244BrowseContactDialog::updateSelection(const QItemSelection &selected,
245 const QItemSelection &deselected)
246{
247 QModelIndexList items = selected.indexes();
248 Name certName = m_contactNameList[items.first().row()];
249
250 ui->InfoTable->clear();
251 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
252 ui->InfoTable->removeRow(i);
253
Yingdi Yue433a502013-11-10 01:34:57 -0800254 ui->InfoTable->horizontalHeader()->show();
255
Yingdi Yuae8217c2013-11-09 00:03:26 -0800256 map<Name, Profile>::iterator it = m_profileMap.find(certName);
257 if(it != m_profileMap.end())
258 {
259 ui->InfoTable->setColumnCount(2);
260
261 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
262 ui->InfoTable->setHorizontalHeaderItem(0, typeHeader);
263 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
264 ui->InfoTable->setHorizontalHeaderItem(1, valueHeader);
265
266 Profile::const_iterator pro_it = it->second.begin();
267 int rowCount = 0;
268
269 for(; pro_it != it->second.end(); pro_it++, rowCount++)
270 {
271 ui->InfoTable->insertRow(rowCount);
272 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first));
273 ui->InfoTable->setItem(rowCount, 0, type);
274
Yingdi Yu76dd8002013-12-24 11:16:32 +0800275 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(pro_it->second));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800276 ui->InfoTable->setItem(rowCount, 1, value);
277 }
278 }
279}
280
281void
282BrowseContactDialog::onWarning(QString msg)
283{
284 m_warningDialog->setMsg(msg.toStdString());
285 m_warningDialog->show();
286}
287
288void
289BrowseContactDialog::onAddClicked()
290{
291 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
292 QModelIndexList selectedList = selectionModel->selectedIndexes();
293 QModelIndexList::iterator it = selectedList.begin();
294 for(; it != selectedList.end(); it++)
295 {
296 Name certName = m_contactNameList[it->row()];
297 if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end())
298 m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]);
299 else
300 {
301 m_warningDialog->setMsg("Not enough information to add contact!");
302 m_warningDialog->show();
303 }
304 }
305 emit newContactAdded();
306 this->close();
307}
308
309void
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800310BrowseContactDialog::onDirectAddClicked()
311{
312 emit directAddClicked();
313 this->close();
314}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800315
Yingdi Yue433a502013-11-10 01:34:57 -0800316void
317BrowseContactDialog::closeEvent(QCloseEvent *e)
318{
319 ui->InfoTable->clear();
320 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
321 ui->InfoTable->removeRow(i);
322 ui->InfoTable->horizontalHeader()->hide();
323
324 hide();
325 e->ignore();
326}
327
Yingdi Yuae8217c2013-11-09 00:03:26 -0800328#if WAF
329#include "browsecontactdialog.moc"
330#include "browsecontactdialog.cpp.moc"
331#endif