blob: ba0664d9f36838245994f4560bd672eeb540993e [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"
Yingdi Yuae8217c2013-11-09 00:03:26 -080020#endif
21
22using namespace std;
23using namespace ndn;
Yingdi Yufa4ce792014-02-06 18:09:22 -080024using namespace chronos;
Yingdi Yuae8217c2013-11-09 00:03:26 -080025
26INIT_LOGGER("BrowseContactDialog");
27
28// Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
29
Yingdi Yu76dd8002013-12-24 11:16:32 +080030BrowseContactDialog::BrowseContactDialog(shared_ptr<ContactManager> contactManager,
Yingdi Yuae8217c2013-11-09 00:03:26 -080031 QWidget *parent)
32 : QDialog(parent)
33 , ui(new Ui::BrowseContactDialog)
34 , m_contactManager(contactManager)
35 , m_warningDialog(new WarningDialog)
36 , m_contactListModel(new QStringListModel)
37{
38 // qRegisterMetaType<ndn::security::IdentityCertificate>("NDNIdentityCertificate");
39
40 ui->setupUi(this);
41
42 ui->ContactList->setModel(m_contactListModel);
43
44 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
45 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu6eabbd72013-12-27 08:44:12 +080046 connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::IdentityCertificate &)),
47 this, SLOT(onCertificateFetched(const ndn::IdentityCertificate &)));
Yingdi Yuae8217c2013-11-09 00:03:26 -080048 connect(&*m_contactManager, SIGNAL(contactCertificateFetchFailed(const ndn::Name&)),
49 this, SLOT(onCertificateFetchFailed(const ndn::Name&)));
50 connect(&*m_contactManager, SIGNAL(warning(QString)),
51 this, SLOT(onWarning(QString)));
52
53 connect(ui->AddButton, SIGNAL(clicked()),
54 this, SLOT(onAddClicked()));
55
Yingdi Yu9d7dfc22013-11-10 17:59:58 -080056 connect(ui->DirectAddButton, SIGNAL(clicked()),
57 this, SLOT(onDirectAddClicked()));
Yingdi Yuae8217c2013-11-09 00:03:26 -080058}
59
60BrowseContactDialog::~BrowseContactDialog()
61{
62 delete ui;
63}
64
65
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080066void
Yingdi Yufa4ce792014-02-06 18:09:22 -080067BrowseContactDialog::getCertNames(vector<string> &names)
Yingdi Yuae8217c2013-11-09 00:03:26 -080068{
Yingdi Yuae8217c2013-11-09 00:03:26 -080069 try{
70 using namespace boost::asio::ip;
71 tcp::iostream request_stream;
Yingdi Yu6eabbd72013-12-27 08:44:12 +080072 request_stream.expires_from_now(boost::posix_time::milliseconds(5000));
Yingdi Yuae8217c2013-11-09 00:03:26 -080073 request_stream.connect("ndncert.named-data.net","80");
74 if(!request_stream)
75 {
Yingdi Yue433a502013-11-10 01:34:57 -080076 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #1"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080077 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -080078 }
79 request_stream << "GET /cert/list/ HTTP/1.0\r\n";
80 request_stream << "Host: ndncert.named-data.net\r\n\r\n";
81 request_stream.flush();
82
83 string line1;
84 std::getline(request_stream,line1);
85 if (!request_stream)
86 {
Yingdi Yue433a502013-11-10 01:34:57 -080087 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #2"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080088 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -080089 }
Alexander Afanasyeve5b55132013-11-09 23:07:48 -080090
Yingdi Yuae8217c2013-11-09 00:03:26 -080091 std::stringstream response_stream(line1);
92 std::string http_version;
93 response_stream >> http_version;
94 unsigned int status_code;
95 response_stream >> status_code;
96 std::string status_message;
97 std::getline(response_stream,status_message);
98
99 if (!response_stream||http_version.substr(0,5)!="HTTP/")
100 {
Yingdi Yue433a502013-11-10 01:34:57 -0800101 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #3"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800102 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800103 }
104 if (status_code!=200)
105 {
Yingdi Yue433a502013-11-10 01:34:57 -0800106 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #4"));
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800107 return;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800108 }
109 vector<string> headers;
110 std::string header;
111 while (std::getline(request_stream, header) && header != "\r")
112 headers.push_back(header);
113
Alexander Afanasyev4294a292013-11-10 16:58:13 -0800114 std::istreambuf_iterator<char> stream_iter (request_stream);
115 std::istreambuf_iterator<char> end_of_stream;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800116
Alexander Afanasyev4294a292013-11-10 16:58:13 -0800117 typedef boost::tokenizer< boost::escaped_list_separator<char>, std::istreambuf_iterator<char> > tokenizer_t;
118 tokenizer_t certItems (stream_iter, end_of_stream,boost::escaped_list_separator<char>('\\', '\n', '"'));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800119
Alexander Afanasyev4294a292013-11-10 16:58:13 -0800120 for (tokenizer_t::iterator it = certItems.begin();
121 it != certItems.end (); it++)
122 {
123 if (!it->empty())
124 {
125 names.push_back(*it);
126 }
127 }
Yingdi Yuae8217c2013-11-09 00:03:26 -0800128 }catch(std::exception &e){
Yingdi Yue433a502013-11-10 01:34:57 -0800129 QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #N"));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800130 }
131}
132
133void
134BrowseContactDialog::updateCertificateMap(bool filter)
135{
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800136 vector<string> certNameList;
137 getCertNames(certNameList);
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800138 _LOG_DEBUG("Get Certificate Name List");
139 for(int i = 0; i < certNameList.size(); i++)
140 {
141 _LOG_DEBUG(" " << certNameList[i]);
142 }
Alexander Afanasyeve5b55132013-11-09 23:07:48 -0800143
Yingdi Yuae8217c2013-11-09 00:03:26 -0800144 if(filter)
145 {
Yingdi Yub6fb0302014-01-21 11:05:11 -0800146 map<Name, Name> certificateMap;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800147
148 vector<string>::iterator it = certNameList.begin();
149
150 for(; it != certNameList.end(); it++)
151 {
152 Name newCertName(*it);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800153 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(newCertName);
154 Name identity = keyName.getPrefix(-1);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800155
156 map<Name, Name>::iterator map_it = certificateMap.find(identity);
157 if(map_it != certificateMap.end())
158 {
159 Name oldCertName = map_it->second;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800160 Name oldKeyName = IdentityCertificate::certificateNameToPublicKeyName(oldCertName);
161 if(keyName.get(-1).toEscapedString() > oldKeyName.get(-1).toEscapedString())
Yingdi Yuae8217c2013-11-09 00:03:26 -0800162 map_it->second = newCertName;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800163 else if(keyName == oldKeyName && newCertName.get(-1).toVersion() > oldCertName.get(-1).toVersion())
Yingdi Yuae8217c2013-11-09 00:03:26 -0800164 map_it->second = newCertName;
165 }
166 else
167 {
168 certificateMap.insert(pair<Name, Name>(identity, newCertName));
169 }
170 }
171 map<Name, Name>::iterator map_it = certificateMap.begin();
172 for(; map_it != certificateMap.end(); map_it++)
173 m_certificateNameList.push_back(map_it->second);
174 }
175 else
176 {
177 vector<string>::iterator it = certNameList.begin();
178
179 for(; it != certNameList.end(); it++)
180 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800181 m_certificateNameList.push_back(Name (*it));
182 // try {
183 // m_certificateNameList.push_back(Name (*it));
184 // }
185 // catch(error::Name)
186 // {
187 // _LOG_ERROR ("Error parsing: [" << *it << "]");
188 // }
189 // catch(error::name::Component)
190 // {
191 // _LOG_ERROR ("Error parsing: [" << *it << "]");
192 // }
Yingdi Yuae8217c2013-11-09 00:03:26 -0800193 }
194 }
195}
196
197void
198BrowseContactDialog::fetchCertificate()
199{
200 vector<Name>::iterator it = m_certificateNameList.begin();
201 int count = 0;
202 for(; it != m_certificateNameList.end(); it++)
203 {
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800204 _LOG_DEBUG("fetch " << it->toUri());
205 usleep(1000);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800206 m_contactManager->fetchIdCertificate(*it);
207 }
208}
209
210void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800211BrowseContactDialog::onCertificateFetched(const IdentityCertificate& identityCertificate)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800212{
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800213 Name certNameNoVersion = identityCertificate.getName().getPrefix(-1);
214 _LOG_DEBUG("Fetch: " << certNameNoVersion.toUri());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800215 m_certificateMap.insert(pair<Name, IdentityCertificate>(certNameNoVersion, identityCertificate));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800216 m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate)));
Yingdi Yufa4ce792014-02-06 18:09:22 -0800217 string name = m_profileMap[certNameNoVersion].get("name");
Yingdi Yuae8217c2013-11-09 00:03:26 -0800218 // Name contactName = m_profileMap[certNameNoVersion].getIdentityName();
219 {
220 UniqueRecLock lock(m_mutex);
221 m_contactList << QString::fromStdString(name);
222 m_contactListModel->setStringList(m_contactList);
223 m_contactNameList.push_back(certNameNoVersion);
224 }
225}
226
227void
228BrowseContactDialog::onCertificateFetchFailed(const Name& identity)
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800229{
230 _LOG_DEBUG("Cannot fetch " << identity.toUri());
231}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800232
233void
234BrowseContactDialog::refreshList()
235{
236 {
237 UniqueRecLock lock(m_mutex);
238 m_contactList.clear();
239 m_contactNameList.clear();
240 }
241 m_certificateNameList.clear();
242 m_certificateMap.clear();
243 m_profileMap.clear();
244
245 updateCertificateMap();
246
247 fetchCertificate();
248}
249
250void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800251BrowseContactDialog::updateSelection(const QItemSelection& selected,
252 const QItemSelection& deselected)
Yingdi Yuae8217c2013-11-09 00:03:26 -0800253{
254 QModelIndexList items = selected.indexes();
255 Name certName = m_contactNameList[items.first().row()];
256
257 ui->InfoTable->clear();
258 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
259 ui->InfoTable->removeRow(i);
260
Yingdi Yue433a502013-11-10 01:34:57 -0800261 ui->InfoTable->horizontalHeader()->show();
262
Yingdi Yuae8217c2013-11-09 00:03:26 -0800263 map<Name, Profile>::iterator it = m_profileMap.find(certName);
264 if(it != m_profileMap.end())
265 {
266 ui->InfoTable->setColumnCount(2);
267
268 QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type"));
269 ui->InfoTable->setHorizontalHeaderItem(0, typeHeader);
270 QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value"));
271 ui->InfoTable->setHorizontalHeaderItem(1, valueHeader);
272
273 Profile::const_iterator pro_it = it->second.begin();
274 int rowCount = 0;
275
276 for(; pro_it != it->second.end(); pro_it++, rowCount++)
277 {
278 ui->InfoTable->insertRow(rowCount);
279 QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first));
280 ui->InfoTable->setItem(rowCount, 0, type);
281
Yingdi Yu76dd8002013-12-24 11:16:32 +0800282 QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(pro_it->second));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800283 ui->InfoTable->setItem(rowCount, 1, value);
284 }
285 }
286}
287
288void
289BrowseContactDialog::onWarning(QString msg)
290{
291 m_warningDialog->setMsg(msg.toStdString());
292 m_warningDialog->show();
293}
294
295void
296BrowseContactDialog::onAddClicked()
297{
298 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
299 QModelIndexList selectedList = selectionModel->selectedIndexes();
300 QModelIndexList::iterator it = selectedList.begin();
301 for(; it != selectedList.end(); it++)
302 {
303 Name certName = m_contactNameList[it->row()];
304 if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end())
305 m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]);
306 else
307 {
308 m_warningDialog->setMsg("Not enough information to add contact!");
309 m_warningDialog->show();
310 }
311 }
312 emit newContactAdded();
313 this->close();
314}
315
316void
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800317BrowseContactDialog::onDirectAddClicked()
318{
319 emit directAddClicked();
320 this->close();
321}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800322
Yingdi Yue433a502013-11-10 01:34:57 -0800323void
324BrowseContactDialog::closeEvent(QCloseEvent *e)
325{
326 ui->InfoTable->clear();
327 for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--)
328 ui->InfoTable->removeRow(i);
329 ui->InfoTable->horizontalHeader()->hide();
330
331 hide();
332 e->ignore();
333}
334
Yingdi Yuae8217c2013-11-09 00:03:26 -0800335#if WAF
336#include "browsecontactdialog.moc"
337#include "browsecontactdialog.cpp.moc"
338#endif