Yingdi Yu | 908f841 | 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 | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 14 | #include <QMessageBox> |
Yingdi Yu | 908f841 | 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" |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 20 | // #include "ndn.cxx/error.h" |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | using namespace std; |
| 24 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 25 | using namespace ndn::ptr_lib; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 26 | |
| 27 | INIT_LOGGER("BrowseContactDialog"); |
| 28 | |
| 29 | // Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
| 30 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 31 | BrowseContactDialog::BrowseContactDialog(shared_ptr<ContactManager> contactManager, |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 32 | 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 Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 47 | connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::IdentityCertificate &)), |
| 48 | this, SLOT(onCertificateFetched(const ndn::IdentityCertificate &))); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 49 | 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 Yu | 3cd30df | 2013-11-10 17:59:58 -0800 | [diff] [blame] | 57 | connect(ui->DirectAddButton, SIGNAL(clicked()), |
| 58 | this, SLOT(onDirectAddClicked())); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | BrowseContactDialog::~BrowseContactDialog() |
| 62 | { |
| 63 | delete ui; |
| 64 | } |
| 65 | |
| 66 | |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 67 | void |
| 68 | BrowseContactDialog::getCertNames(std::vector<std::string> &names) |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 69 | { |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 70 | try{ |
| 71 | using namespace boost::asio::ip; |
| 72 | tcp::iostream request_stream; |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 73 | request_stream.expires_from_now(boost::posix_time::milliseconds(5000)); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 74 | request_stream.connect("ndncert.named-data.net","80"); |
| 75 | if(!request_stream) |
| 76 | { |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 77 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #1")); |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 78 | return; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 79 | } |
| 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 Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 88 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #2")); |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 89 | return; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 91 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 92 | 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 Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 102 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #3")); |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 103 | return; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 104 | } |
| 105 | if (status_code!=200) |
| 106 | { |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 107 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #4")); |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 108 | return; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 109 | } |
| 110 | vector<string> headers; |
| 111 | std::string header; |
| 112 | while (std::getline(request_stream, header) && header != "\r") |
| 113 | headers.push_back(header); |
| 114 | |
Alexander Afanasyev | 6dd5385 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 115 | std::istreambuf_iterator<char> stream_iter (request_stream); |
| 116 | std::istreambuf_iterator<char> end_of_stream; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | 6dd5385 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 118 | 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 Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 6dd5385 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 121 | 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 Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 129 | }catch(std::exception &e){ |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 130 | QMessageBox::information(this, tr("Chronos"), QString::fromStdString("Fail to fetch certificate directory! #N")); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | BrowseContactDialog::updateCertificateMap(bool filter) |
| 136 | { |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 137 | vector<string> certNameList; |
| 138 | getCertNames(certNameList); |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 139 | _LOG_DEBUG("Get Certificate Name List"); |
| 140 | for(int i = 0; i < certNameList.size(); i++) |
| 141 | { |
| 142 | _LOG_DEBUG(" " << certNameList[i]); |
| 143 | } |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 144 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 145 | if(filter) |
| 146 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 147 | map<Name, Name, Name::BreadthFirstLess> certificateMap; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 148 | |
| 149 | vector<string>::iterator it = certNameList.begin(); |
| 150 | |
| 151 | for(; it != certNameList.end(); it++) |
| 152 | { |
| 153 | Name newCertName(*it); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 154 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(newCertName); |
| 155 | Name identity = keyName.getPrefix(-1); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 156 | |
| 157 | map<Name, Name>::iterator map_it = certificateMap.find(identity); |
| 158 | if(map_it != certificateMap.end()) |
| 159 | { |
| 160 | Name oldCertName = map_it->second; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 161 | Name oldKeyName = IdentityCertificate::certificateNameToPublicKeyName(oldCertName); |
| 162 | if(keyName.get(-1).toEscapedString() > oldKeyName.get(-1).toEscapedString()) |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 163 | map_it->second = newCertName; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 164 | else if(keyName == oldKeyName && newCertName.get(-1).toVersion() > oldCertName.get(-1).toVersion()) |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 165 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 182 | 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 Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | BrowseContactDialog::fetchCertificate() |
| 200 | { |
| 201 | vector<Name>::iterator it = m_certificateNameList.begin(); |
| 202 | int count = 0; |
| 203 | for(; it != m_certificateNameList.end(); it++) |
| 204 | { |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 205 | _LOG_DEBUG("fetch " << it->toUri()); |
| 206 | usleep(1000); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 207 | m_contactManager->fetchIdCertificate(*it); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 212 | BrowseContactDialog::onCertificateFetched(const IdentityCertificate& identityCertificate) |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 213 | { |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 214 | Name certNameNoVersion = identityCertificate.getName().getPrefix(-1); |
| 215 | _LOG_DEBUG("Fetch: " << certNameNoVersion.toUri()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 216 | m_certificateMap.insert(pair<Name, IdentityCertificate>(certNameNoVersion, identityCertificate)); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 217 | m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate))); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 218 | string name = m_profileMap[certNameNoVersion].getProfileEntry("name"); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 219 | // 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 | |
| 228 | void |
| 229 | BrowseContactDialog::onCertificateFetchFailed(const Name& identity) |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 230 | { |
| 231 | _LOG_DEBUG("Cannot fetch " << identity.toUri()); |
| 232 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 233 | |
| 234 | void |
| 235 | BrowseContactDialog::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 | |
| 251 | void |
| 252 | BrowseContactDialog::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 Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 262 | ui->InfoTable->horizontalHeader()->show(); |
| 263 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 264 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 283 | QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(pro_it->second)); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 284 | ui->InfoTable->setItem(rowCount, 1, value); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | BrowseContactDialog::onWarning(QString msg) |
| 291 | { |
| 292 | m_warningDialog->setMsg(msg.toStdString()); |
| 293 | m_warningDialog->show(); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | BrowseContactDialog::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 | |
| 317 | void |
Yingdi Yu | 3cd30df | 2013-11-10 17:59:58 -0800 | [diff] [blame] | 318 | BrowseContactDialog::onDirectAddClicked() |
| 319 | { |
| 320 | emit directAddClicked(); |
| 321 | this->close(); |
| 322 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 323 | |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 324 | void |
| 325 | BrowseContactDialog::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 Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 336 | #if WAF |
| 337 | #include "browsecontactdialog.moc" |
| 338 | #include "browsecontactdialog.cpp.moc" |
| 339 | #endif |