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" |
| 20 | #include "exception.h" |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 21 | #include "ndn.cxx/error.h" |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | using namespace std; |
| 25 | using namespace ndn; |
| 26 | |
| 27 | INIT_LOGGER("BrowseContactDialog"); |
| 28 | |
| 29 | // Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
| 30 | |
| 31 | BrowseContactDialog::BrowseContactDialog(Ptr<ContactManager> contactManager, |
| 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 &))); |
| 47 | connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::security::IdentityCertificate &)), |
| 48 | this, SLOT(onCertificateFetched(const ndn::security::IdentityCertificate &))); |
| 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 | |
| 57 | connect(ui->CancelButton, SIGNAL(clicked()), |
| 58 | this, SLOT(onCancelClicked())); |
| 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; |
| 73 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
| 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); |
| 139 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 140 | if(filter) |
| 141 | { |
| 142 | map<Name, Name> certificateMap; |
| 143 | |
| 144 | vector<string>::iterator it = certNameList.begin(); |
| 145 | |
| 146 | for(; it != certNameList.end(); it++) |
| 147 | { |
| 148 | Name newCertName(*it); |
| 149 | Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(newCertName, true); |
| 150 | Name identity = keyName.getPrefix(keyName.size()-1); |
| 151 | |
| 152 | map<Name, Name>::iterator map_it = certificateMap.find(identity); |
| 153 | if(map_it != certificateMap.end()) |
| 154 | { |
| 155 | Name oldCertName = map_it->second; |
| 156 | Name oldKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(oldCertName, true); |
| 157 | if(keyName > oldKeyName) |
| 158 | map_it->second = newCertName; |
| 159 | else if(keyName == oldKeyName && newCertName > oldCertName) |
| 160 | map_it->second = newCertName; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | certificateMap.insert(pair<Name, Name>(identity, newCertName)); |
| 165 | } |
| 166 | } |
| 167 | map<Name, Name>::iterator map_it = certificateMap.begin(); |
| 168 | for(; map_it != certificateMap.end(); map_it++) |
| 169 | m_certificateNameList.push_back(map_it->second); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | vector<string>::iterator it = certNameList.begin(); |
| 174 | |
| 175 | for(; it != certNameList.end(); it++) |
| 176 | { |
Alexander Afanasyev | 6dd5385 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 177 | try { |
| 178 | m_certificateNameList.push_back(Name (*it)); |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 179 | } |
| 180 | catch(error::Name) |
| 181 | { |
Alexander Afanasyev | 6dd5385 | 2013-11-10 16:58:13 -0800 | [diff] [blame] | 182 | _LOG_ERROR ("Error parsing: [" << *it << "]"); |
| 183 | } |
| 184 | catch(error::name::Component) |
| 185 | { |
| 186 | _LOG_ERROR ("Error parsing: [" << *it << "]"); |
Alexander Afanasyev | 19aec3c | 2013-11-09 23:07:48 -0800 | [diff] [blame] | 187 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | BrowseContactDialog::fetchCertificate() |
| 194 | { |
| 195 | vector<Name>::iterator it = m_certificateNameList.begin(); |
| 196 | int count = 0; |
| 197 | for(; it != m_certificateNameList.end(); it++) |
| 198 | { |
| 199 | m_contactManager->fetchIdCertificate(*it); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | BrowseContactDialog::onCertificateFetched(const security::IdentityCertificate& identityCertificate) |
| 205 | { |
| 206 | Name certName = identityCertificate.getName(); |
| 207 | Name certNameNoVersion = certName.getPrefix(certName.size()-1); |
| 208 | m_certificateMap.insert(pair<Name, security::IdentityCertificate>(certNameNoVersion, identityCertificate)); |
| 209 | m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate))); |
| 210 | string name(m_profileMap[certNameNoVersion].getProfileEntry("name")->buf(), m_profileMap[certNameNoVersion].getProfileEntry("name")->size()); |
| 211 | // Name contactName = m_profileMap[certNameNoVersion].getIdentityName(); |
| 212 | { |
| 213 | UniqueRecLock lock(m_mutex); |
| 214 | m_contactList << QString::fromStdString(name); |
| 215 | m_contactListModel->setStringList(m_contactList); |
| 216 | m_contactNameList.push_back(certNameNoVersion); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | BrowseContactDialog::onCertificateFetchFailed(const Name& identity) |
| 222 | {} |
| 223 | |
| 224 | void |
| 225 | BrowseContactDialog::refreshList() |
| 226 | { |
| 227 | { |
| 228 | UniqueRecLock lock(m_mutex); |
| 229 | m_contactList.clear(); |
| 230 | m_contactNameList.clear(); |
| 231 | } |
| 232 | m_certificateNameList.clear(); |
| 233 | m_certificateMap.clear(); |
| 234 | m_profileMap.clear(); |
| 235 | |
| 236 | updateCertificateMap(); |
| 237 | |
| 238 | fetchCertificate(); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | BrowseContactDialog::updateSelection(const QItemSelection &selected, |
| 243 | const QItemSelection &deselected) |
| 244 | { |
| 245 | QModelIndexList items = selected.indexes(); |
| 246 | Name certName = m_contactNameList[items.first().row()]; |
| 247 | |
| 248 | ui->InfoTable->clear(); |
| 249 | for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--) |
| 250 | ui->InfoTable->removeRow(i); |
| 251 | |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 252 | ui->InfoTable->horizontalHeader()->show(); |
| 253 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 254 | map<Name, Profile>::iterator it = m_profileMap.find(certName); |
| 255 | if(it != m_profileMap.end()) |
| 256 | { |
| 257 | ui->InfoTable->setColumnCount(2); |
| 258 | |
| 259 | QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type")); |
| 260 | ui->InfoTable->setHorizontalHeaderItem(0, typeHeader); |
| 261 | QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value")); |
| 262 | ui->InfoTable->setHorizontalHeaderItem(1, valueHeader); |
| 263 | |
| 264 | Profile::const_iterator pro_it = it->second.begin(); |
| 265 | int rowCount = 0; |
| 266 | |
| 267 | for(; pro_it != it->second.end(); pro_it++, rowCount++) |
| 268 | { |
| 269 | ui->InfoTable->insertRow(rowCount); |
| 270 | QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first)); |
| 271 | ui->InfoTable->setItem(rowCount, 0, type); |
| 272 | |
| 273 | string valueString(pro_it->second.buf(), pro_it->second.size()); |
| 274 | QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(valueString)); |
| 275 | ui->InfoTable->setItem(rowCount, 1, value); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | BrowseContactDialog::onWarning(QString msg) |
| 282 | { |
| 283 | m_warningDialog->setMsg(msg.toStdString()); |
| 284 | m_warningDialog->show(); |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | BrowseContactDialog::onAddClicked() |
| 289 | { |
| 290 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 291 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
| 292 | QModelIndexList::iterator it = selectedList.begin(); |
| 293 | for(; it != selectedList.end(); it++) |
| 294 | { |
| 295 | Name certName = m_contactNameList[it->row()]; |
| 296 | if(m_certificateMap.find(certName) != m_certificateMap.end() && m_profileMap.find(certName) != m_profileMap.end()) |
| 297 | m_contactManager->addContact(m_certificateMap[certName], m_profileMap[certName]); |
| 298 | else |
| 299 | { |
| 300 | m_warningDialog->setMsg("Not enough information to add contact!"); |
| 301 | m_warningDialog->show(); |
| 302 | } |
| 303 | } |
| 304 | emit newContactAdded(); |
| 305 | this->close(); |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | BrowseContactDialog::onCancelClicked() |
| 310 | { this->close(); } |
| 311 | |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 312 | void |
| 313 | BrowseContactDialog::closeEvent(QCloseEvent *e) |
| 314 | { |
| 315 | ui->InfoTable->clear(); |
| 316 | for(int i = ui->InfoTable->rowCount() - 1; i >= 0 ; i--) |
| 317 | ui->InfoTable->removeRow(i); |
| 318 | ui->InfoTable->horizontalHeader()->hide(); |
| 319 | |
| 320 | hide(); |
| 321 | e->ignore(); |
| 322 | } |
| 323 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 324 | #if WAF |
| 325 | #include "browsecontactdialog.moc" |
| 326 | #include "browsecontactdialog.cpp.moc" |
| 327 | #endif |