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