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" |
| 14 | |
| 15 | #ifndef Q_MOC_RUN |
| 16 | #include <boost/asio.hpp> |
| 17 | #include <boost/tokenizer.hpp> |
| 18 | #include "logging.h" |
| 19 | #include "exception.h" |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 20 | #include "ndn.cxx/error.h" |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | using namespace std; |
| 24 | using namespace ndn; |
| 25 | |
| 26 | INIT_LOGGER("BrowseContactDialog"); |
| 27 | |
| 28 | // Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
| 29 | |
| 30 | BrowseContactDialog::BrowseContactDialog(Ptr<ContactManager> contactManager, |
| 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 &))); |
| 46 | connect(&*m_contactManager, SIGNAL(contactCertificateFetched(const ndn::security::IdentityCertificate &)), |
| 47 | this, SLOT(onCertificateFetched(const ndn::security::IdentityCertificate &))); |
| 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 | |
| 56 | connect(ui->CancelButton, SIGNAL(clicked()), |
| 57 | this, SLOT(onCancelClicked())); |
| 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 |
| 67 | BrowseContactDialog::getCertNames(std::vector<std::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; |
| 72 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
| 73 | request_stream.connect("ndncert.named-data.net","80"); |
| 74 | if(!request_stream) |
| 75 | { |
| 76 | m_warningDialog->setMsg("Fail to fetch certificate directory! #1"); |
| 77 | m_warningDialog->show(); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 78 | return; |
Yingdi Yu | ae8217c | 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 | { |
| 88 | m_warningDialog->setMsg("Fail to fetch certificate directory! #2"); |
| 89 | m_warningDialog->show(); |
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 | { |
| 103 | m_warningDialog->setMsg("Fail to fetch certificate directory! #3"); |
| 104 | m_warningDialog->show(); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 105 | return; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 106 | } |
| 107 | if (status_code!=200) |
| 108 | { |
| 109 | m_warningDialog->setMsg("Fail to fetch certificate directory! #4"); |
| 110 | m_warningDialog->show(); |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 111 | return; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 112 | } |
| 113 | vector<string> headers; |
| 114 | std::string header; |
| 115 | while (std::getline(request_stream, header) && header != "\r") |
| 116 | headers.push_back(header); |
| 117 | |
| 118 | std::stringbuf buffer; |
| 119 | std::ostream os (&buffer); |
| 120 | |
| 121 | os << request_stream.rdbuf(); |
| 122 | |
| 123 | using boost::tokenizer; |
| 124 | using boost::escaped_list_separator; |
| 125 | |
| 126 | tokenizer<escaped_list_separator<char> > certItems(buffer.str(), escaped_list_separator<char> ('\\', '\n', '"')); |
| 127 | tokenizer<escaped_list_separator<char> >::iterator it = certItems.begin(); |
| 128 | try{ |
| 129 | while (it != certItems.end()) |
| 130 | { |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 131 | if (!it->empty()) |
| 132 | { |
| 133 | names.push_back(*it); |
| 134 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 135 | it++; |
| 136 | } |
| 137 | }catch (exception &e){ |
| 138 | m_warningDialog->setMsg("Fail to fetch certificate directory! #5"); |
| 139 | m_warningDialog->show(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 140 | } |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 141 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 142 | }catch(std::exception &e){ |
| 143 | m_warningDialog->setMsg("Fail to fetch certificate directory! #N"); |
| 144 | m_warningDialog->show(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | BrowseContactDialog::updateCertificateMap(bool filter) |
| 150 | { |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 151 | vector<string> certNameList; |
| 152 | getCertNames(certNameList); |
| 153 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 154 | if(filter) |
| 155 | { |
| 156 | map<Name, Name> certificateMap; |
| 157 | |
| 158 | vector<string>::iterator it = certNameList.begin(); |
| 159 | |
| 160 | for(; it != certNameList.end(); it++) |
| 161 | { |
| 162 | Name newCertName(*it); |
| 163 | Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(newCertName, true); |
| 164 | Name identity = keyName.getPrefix(keyName.size()-1); |
| 165 | |
| 166 | map<Name, Name>::iterator map_it = certificateMap.find(identity); |
| 167 | if(map_it != certificateMap.end()) |
| 168 | { |
| 169 | Name oldCertName = map_it->second; |
| 170 | Name oldKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(oldCertName, true); |
| 171 | if(keyName > oldKeyName) |
| 172 | map_it->second = newCertName; |
| 173 | else if(keyName == oldKeyName && newCertName > oldCertName) |
| 174 | map_it->second = newCertName; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | certificateMap.insert(pair<Name, Name>(identity, newCertName)); |
| 179 | } |
| 180 | } |
| 181 | map<Name, Name>::iterator map_it = certificateMap.begin(); |
| 182 | for(; map_it != certificateMap.end(); map_it++) |
| 183 | m_certificateNameList.push_back(map_it->second); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | vector<string>::iterator it = certNameList.begin(); |
| 188 | |
| 189 | for(; it != certNameList.end(); it++) |
| 190 | { |
Alexander Afanasyev | e5b5513 | 2013-11-09 23:07:48 -0800 | [diff] [blame^] | 191 | try{ |
| 192 | m_certificateNameList.push_back(Name (*it)); |
| 193 | } |
| 194 | catch(error::Name) |
| 195 | { |
| 196 | ; // do nothing |
| 197 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | BrowseContactDialog::fetchCertificate() |
| 204 | { |
| 205 | vector<Name>::iterator it = m_certificateNameList.begin(); |
| 206 | int count = 0; |
| 207 | for(; it != m_certificateNameList.end(); it++) |
| 208 | { |
| 209 | m_contactManager->fetchIdCertificate(*it); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | BrowseContactDialog::onCertificateFetched(const security::IdentityCertificate& identityCertificate) |
| 215 | { |
| 216 | Name certName = identityCertificate.getName(); |
| 217 | Name certNameNoVersion = certName.getPrefix(certName.size()-1); |
| 218 | m_certificateMap.insert(pair<Name, security::IdentityCertificate>(certNameNoVersion, identityCertificate)); |
| 219 | m_profileMap.insert(pair<Name, Profile>(certNameNoVersion, Profile(identityCertificate))); |
| 220 | string name(m_profileMap[certNameNoVersion].getProfileEntry("name")->buf(), m_profileMap[certNameNoVersion].getProfileEntry("name")->size()); |
| 221 | // Name contactName = m_profileMap[certNameNoVersion].getIdentityName(); |
| 222 | { |
| 223 | UniqueRecLock lock(m_mutex); |
| 224 | m_contactList << QString::fromStdString(name); |
| 225 | m_contactListModel->setStringList(m_contactList); |
| 226 | m_contactNameList.push_back(certNameNoVersion); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | BrowseContactDialog::onCertificateFetchFailed(const Name& identity) |
| 232 | {} |
| 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 | |
| 262 | map<Name, Profile>::iterator it = m_profileMap.find(certName); |
| 263 | if(it != m_profileMap.end()) |
| 264 | { |
| 265 | ui->InfoTable->setColumnCount(2); |
| 266 | |
| 267 | QTableWidgetItem *typeHeader = new QTableWidgetItem(QString::fromUtf8("Type")); |
| 268 | ui->InfoTable->setHorizontalHeaderItem(0, typeHeader); |
| 269 | QTableWidgetItem *valueHeader = new QTableWidgetItem(QString::fromUtf8("Value")); |
| 270 | ui->InfoTable->setHorizontalHeaderItem(1, valueHeader); |
| 271 | |
| 272 | Profile::const_iterator pro_it = it->second.begin(); |
| 273 | int rowCount = 0; |
| 274 | |
| 275 | for(; pro_it != it->second.end(); pro_it++, rowCount++) |
| 276 | { |
| 277 | ui->InfoTable->insertRow(rowCount); |
| 278 | QTableWidgetItem *type = new QTableWidgetItem(QString::fromStdString(pro_it->first)); |
| 279 | ui->InfoTable->setItem(rowCount, 0, type); |
| 280 | |
| 281 | string valueString(pro_it->second.buf(), pro_it->second.size()); |
| 282 | QTableWidgetItem *value = new QTableWidgetItem(QString::fromStdString(valueString)); |
| 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 |
| 317 | BrowseContactDialog::onCancelClicked() |
| 318 | { this->close(); } |
| 319 | |
| 320 | #if WAF |
| 321 | #include "browsecontactdialog.moc" |
| 322 | #include "browsecontactdialog.cpp.moc" |
| 323 | #endif |