Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 1 | /* -*- 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 Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 14 | #include <QMessageBox> |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include <boost/asio.hpp> |
| 18 | #include <boost/tokenizer.hpp> |
| 19 | #include "logging.h" |
| 20 | #include "exception.h" |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 21 | // #include "ndn.cxx/error.h" |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | using namespace std; |
| 25 | using namespace ndn; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 26 | using namespace ndn::ptr_lib; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 27 | |
| 28 | INIT_LOGGER("BrowseContactDialog"); |
| 29 | |
| 30 | // Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
| 31 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 32 | BrowseContactDialog::BrowseContactDialog(shared_ptr<ContactManager> contactManager, |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 33 | 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 Yu | 9d7dfc2 | 2013-11-10 17:59:58 -0800 | [diff] [blame] | 58 | connect(ui->DirectAddButton, SIGNAL(clicked()), |
| 59 | this, SLOT(onDirectAddClicked())); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | BrowseContactDialog::~BrowseContactDialog() |
| 63 | { |
| 64 | delete ui; |
| 65 | } |
| 66 | |
| 67 | |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 68 | void |
| 69 | BrowseContactDialog::getCertNames(std::vector<std::string> &names) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 70 | { |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 71 | 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 Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 78 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #1")); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 79 | return; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 80 | } |
| 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 Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 89 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #2")); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 90 | return; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 91 | } |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 92 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 93 | 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 Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 103 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #3")); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 104 | return; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 105 | } |
| 106 | if (status_code!=200) |
| 107 | { |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 108 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #4")); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 109 | return; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 110 | } |
| 111 | vector<string> headers; |
| 112 | std::string header; |
| 113 | while (std::getline(request_stream, header) && header != "\r") |
| 114 | headers.push_back(header); |
| 115 | |
Alexander Afanasyev | 4294a29 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 116 | std::istreambuf_iterator<char> stream_iter (request_stream); |
| 117 | std::istreambuf_iterator<char> end_of_stream; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 4294a29 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 119 | 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 Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | 4294a29 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 122 | 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 Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 130 | }catch(std::exception &e){ |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 131 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #N")); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | BrowseContactDialog::updateCertificateMap(bool filter) |
| 137 | { |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 138 | vector<string> certNameList; |
| 139 | getCertNames(certNameList); |
| 140 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 141 | if(filter) |
| 142 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 143 | map<Name, Name, Name::BreadthFirstLess> certificateMap; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 144 | |
| 145 | vector<string>::iterator it = certNameList.begin(); |
| 146 | |
| 147 | for(; it != certNameList.end(); it++) |
| 148 | { |
| 149 | Name newCertName(*it); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 150 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(newCertName); |
| 151 | Name identity = keyName.getPrefix(-1); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 152 | |
| 153 | map<Name, Name>::iterator map_it = certificateMap.find(identity); |
| 154 | if(map_it != certificateMap.end()) |
| 155 | { |
| 156 | Name oldCertName = map_it->second; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 157 | Name oldKeyName = IdentityCertificate::certificateNameToPublicKeyName(oldCertName); |
| 158 | if(keyName.get(-1).toEscapedString() > oldKeyName.get(-1).toEscapedString()) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 159 | map_it->second = newCertName; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 160 | else if(keyName == oldKeyName && newCertName.get(-1).toVersion() > oldCertName.get(-1).toVersion()) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 161 | 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 Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 178 | 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 Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | BrowseContactDialog::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 | |
| 205 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 206 | BrowseContactDialog::onCertificateFetched(const IdentityCertificate& identityCertificate) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 207 | { |
| 208 | Name certName = identityCertificate.getName(); |
| 209 | Name certNameNoVersion = certName.getPrefix(certName.size()-1); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 210 | m_certificateMap.insert(pair<Name, IdentityCertificate>(certNameNoVersion, identityCertificate)); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 211 | m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate))); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 212 | string name = m_profileMap[certNameNoVersion].getProfileEntry("name"); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 213 | // 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 | |
| 222 | void |
| 223 | BrowseContactDialog::onCertificateFetchFailed(const Name& identity) |
| 224 | {} |
| 225 | |
| 226 | void |
| 227 | BrowseContactDialog::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 | |
| 243 | void |
| 244 | BrowseContactDialog::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 Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 254 | ui->InfoTable->horizontalHeader()->show(); |
| 255 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 256 | 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 Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 275 | QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(pro_it->second)); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 276 | ui->InfoTable->setItem(rowCount, 1, value); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | BrowseContactDialog::onWarning(QString msg) |
| 283 | { |
| 284 | m_warningDialog->setMsg(msg.toStdString()); |
| 285 | m_warningDialog->show(); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | BrowseContactDialog::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 | |
| 309 | void |
Yingdi Yu | 9d7dfc2 | 2013-11-10 17:59:58 -0800 | [diff] [blame] | 310 | BrowseContactDialog::onDirectAddClicked() |
| 311 | { |
| 312 | emit directAddClicked(); |
| 313 | this->close(); |
| 314 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 315 | |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 316 | void |
| 317 | BrowseContactDialog::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 Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 328 | #if WAF |
| 329 | #include "browsecontactdialog.moc" |
| 330 | #include "browsecontactdialog.cpp.moc" |
| 331 | #endif |