Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [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 | #include "contactpanel.h" |
| 12 | #include "ui_contactpanel.h" |
| 13 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 14 | |
| 15 | #include <QStringList> |
| 16 | #include <QItemSelectionModel> |
| 17 | #include <QModelIndex> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 18 | #include <QDir> |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 19 | #include <QtSql/QSqlRecord> |
| 20 | #include <QtSql/QSqlField> |
| 21 | #include <QtSql/QSqlError> |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 22 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 23 | #ifndef Q_MOC_RUN |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 24 | #include <ndn-cpp/security/verifier.hpp> |
| 25 | #include <ndn-cpp/security/signature/signature-sha256-with-rsa.hpp> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 26 | #include <boost/filesystem.hpp> |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 27 | #include <boost/random/random_device.hpp> |
| 28 | #include <boost/random/uniform_int_distribution.hpp> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 29 | #include "panel-policy-manager.h" |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 30 | #include "null-ptrs.h" |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 31 | #include "logging.h" |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | namespace fs = boost::filesystem; |
| 35 | using namespace ndn; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 36 | using namespace ndn::ptr_lib; |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 38 | using namespace std; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 39 | |
| 40 | INIT_LOGGER("ContactPanel"); |
| 41 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 42 | Q_DECLARE_METATYPE(ndn::IdentityCertificate) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 43 | Q_DECLARE_METATYPE(ChronosInvitation) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 45 | ContactPanel::ContactPanel(QWidget *parent) |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 46 | : QDialog(parent) |
| 47 | , ui(new Ui::ContactPanel) |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 48 | , m_warningDialog(new WarningDialog) |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 49 | , m_contactListModel(new QStringListModel) |
| 50 | , m_startChatDialog(new StartChatDialog) |
| 51 | , m_invitationDialog(new InvitationDialog) |
| 52 | , m_settingDialog(new SettingDialog) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 53 | , m_policyManager(new PanelPolicyManager()) |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 54 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 55 | qRegisterMetaType<IdentityCertificate>("IdentityCertificate"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 56 | qRegisterMetaType<ChronosInvitation>("ChronosInvitation"); |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 57 | |
| 58 | startFace(); |
| 59 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 60 | createAction(); |
| 61 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 62 | m_keyChain = make_shared<KeyChain>(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 63 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 64 | m_contactManager = make_shared<ContactManager>(m_keyChain, m_face); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 65 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 66 | connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)), |
| 67 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 68 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 69 | openDB(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 70 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 71 | refreshContactList(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 73 | loadTrustAnchor(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 74 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 75 | m_defaultIdentity = m_keyChain->getDefaultIdentity(); |
Yingdi Yu | b5a0fd3 | 2013-11-10 16:26:52 -0800 | [diff] [blame] | 76 | if(m_defaultIdentity.size() == 0) |
| 77 | showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?"); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 78 | Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 79 | if(defaultCertName.size() == 0) |
Yingdi Yu | f0b9ea9 | 2013-11-10 10:54:16 -0800 | [diff] [blame] | 80 | showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?"); |
Yingdi Yu | b5a0fd3 | 2013-11-10 16:26:52 -0800 | [diff] [blame] | 81 | |
| 82 | |
| 83 | m_contactManager->setDefaultIdentity(m_defaultIdentity); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 84 | m_nickName = m_defaultIdentity.get(-1).toEscapedString(); |
Yingdi Yu | b5a0fd3 | 2013-11-10 16:26:52 -0800 | [diff] [blame] | 85 | m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 86 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 87 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 88 | m_profileEditor = new ProfileEditor(m_contactManager); |
| 89 | m_profileEditor->setCurrentIdentity(m_defaultIdentity); |
| 90 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 91 | m_addContactPanel = new AddContactPanel(m_contactManager); |
| 92 | m_browseContactDialog = new BrowseContactDialog(m_contactManager); |
| 93 | m_setAliasDialog = new SetAliasDialog(m_contactManager); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 94 | |
| 95 | ui->setupUi(this); |
| 96 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 97 | |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 98 | m_localPrefix = Name("/private/local"); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 99 | setLocalPrefix(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 100 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 101 | setInvitationListener(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 102 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 103 | collectEndorsement(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 104 | |
| 105 | ui->ContactList->setModel(m_contactListModel); |
| 106 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 108 | connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 109 | this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 110 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 111 | connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 112 | this, SLOT(showContextMenu(const QPoint&))); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 114 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 115 | this, SLOT(openProfileEditor())); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 116 | |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 117 | connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)), |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 118 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 119 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 120 | connect(ui->AddContactButton, SIGNAL(clicked()), |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 121 | this, SLOT(openBrowseContactDialog())); |
| 122 | |
Yingdi Yu | 9d7dfc2 | 2013-11-10 17:59:58 -0800 | [diff] [blame] | 123 | connect(m_browseContactDialog, SIGNAL(directAddClicked()), |
| 124 | this, SLOT(openAddContactPanel())); |
| 125 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 126 | connect(this, SIGNAL(refreshCertDirectory()), |
| 127 | m_browseContactDialog, SLOT(refreshList())); |
| 128 | |
| 129 | connect(ui->DeleteContactButton, SIGNAL(clicked()), |
| 130 | this, SLOT(removeContactButton())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 132 | connect(ui->settingButton, SIGNAL(clicked()), |
| 133 | this, SLOT(openSettingDialog())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 134 | |
Yingdi Yu | 9d7dfc2 | 2013-11-10 17:59:58 -0800 | [diff] [blame] | 135 | connect(m_addContactPanel, SIGNAL(newContactAdded()), |
| 136 | this, SLOT(refreshContactList())); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 137 | connect(m_browseContactDialog, SIGNAL(newContactAdded()), |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 138 | this, SLOT(refreshContactList())); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 139 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 140 | this, SLOT(refreshContactList())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 141 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 142 | connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)), |
| 143 | this, SLOT(startChatroom(const QString&, const QString&, bool))); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 144 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 145 | connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::IdentityCertificate&)), |
| 146 | this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::IdentityCertificate&))); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 147 | connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)), |
| 148 | this, SLOT(rejectInvitation(const ChronosInvitation&))); |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 149 | |
| 150 | connect(&*m_contactManager, SIGNAL(contactAdded(const ndn::Name&)), |
| 151 | this, SLOT(addContactIntoPanelPolicy(const ndn::Name&))); |
| 152 | connect(&*m_contactManager, SIGNAL(contactRemoved(const ndn::Name&)), |
| 153 | this, SLOT(removeContactFromPanelPolicy(const ndn::Name&))); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 155 | connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)), |
| 156 | this, SLOT(updateDefaultIdentity(const QString&, const QString&))); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 157 | |
| 158 | connect(this, SIGNAL(newInvitationReady()), |
| 159 | this, SLOT(openInvitationDialog())); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 160 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 161 | connect(ui->isIntroducer, SIGNAL(stateChanged(int)), |
| 162 | this, SLOT(isIntroducerChanged(int))); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 163 | |
| 164 | connect(ui->addScope, SIGNAL(clicked()), |
| 165 | this, SLOT(addScopeClicked())); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 166 | connect(ui->deleteScope, SIGNAL(clicked()), |
| 167 | this, SLOT(deleteScopeClicked())); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 168 | connect(ui->saveButton, SIGNAL(clicked()), |
| 169 | this, SLOT(saveScopeClicked())); |
| 170 | |
| 171 | connect(ui->endorseButton, SIGNAL(clicked()), |
| 172 | this, SLOT(endorseButtonClicked())); |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | ContactPanel::~ContactPanel() |
| 176 | { |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 177 | delete ui; |
| 178 | delete m_contactListModel; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 179 | delete m_startChatDialog; |
| 180 | delete m_invitationDialog; |
| 181 | delete m_settingDialog; |
| 182 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 183 | delete m_profileEditor; |
| 184 | delete m_addContactPanel; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 185 | delete m_browseContactDialog; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 186 | delete m_setAliasDialog; |
| 187 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 188 | delete m_trustScopeModel; |
| 189 | delete m_endorseDataModel; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 190 | delete m_endorseComboBoxDelegate; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 191 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 192 | delete m_menuInvite; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 193 | delete m_menuAlias; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 194 | |
| 195 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin(); |
| 196 | for(; it != m_chatDialogs.end(); it++) |
| 197 | delete it->second; |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 198 | |
| 199 | shutdownFace(); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | ContactPanel::startFace() |
| 204 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 205 | m_face = make_shared<Face>(); |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 206 | |
| 207 | connectToDaemon(); |
| 208 | |
| 209 | m_running = true; |
| 210 | m_thread = boost::thread (&ContactPanel::eventLoop, this); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | ContactPanel::shutdownFace() |
| 215 | { |
| 216 | { |
| 217 | boost::unique_lock<boost::recursive_mutex> lock(m_mutex); |
| 218 | m_running = false; |
| 219 | } |
| 220 | |
| 221 | m_thread.join(); |
| 222 | m_face->shutdown(); |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | ContactPanel::eventLoop() |
| 227 | { |
| 228 | while (m_running) |
| 229 | { |
| 230 | try{ |
| 231 | m_face->processEvents(); |
| 232 | usleep(100); |
| 233 | }catch(std::exception& e){ |
| 234 | _LOG_DEBUG(" " << e.what() ); |
| 235 | } |
| 236 | } |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 239 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 240 | ContactPanel::connectToDaemon() |
| 241 | { |
| 242 | //Hack! transport does not connect to daemon unless an interest is expressed. |
| 243 | Name name("/ndn"); |
| 244 | ndn::Interest interest(name); |
| 245 | m_face->expressInterest(interest, |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 246 | func_lib::bind(&ContactPanel::onConnectionData, this, _1, _2), |
| 247 | func_lib::bind(&ContactPanel::onConnectionDataTimeout, this, _1)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void |
| 251 | ContactPanel::onConnectionData(const shared_ptr<const ndn::Interest>& interest, |
| 252 | const shared_ptr<Data>& data) |
| 253 | { _LOG_DEBUG("onConnectionData"); } |
| 254 | |
| 255 | void |
| 256 | ContactPanel::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest) |
| 257 | { _LOG_DEBUG("onConnectionDataTimeout"); } |
| 258 | |
| 259 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 260 | ContactPanel::createAction() |
| 261 | { |
| 262 | m_menuInvite = new QAction("&Chat", this); |
| 263 | m_menuAlias = new QAction("&Set Alias", this); |
| 264 | } |
| 265 | |
| 266 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 267 | ContactPanel::openDB() |
| 268 | { |
| 269 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
| 270 | QString path = (QDir::home().path()); |
| 271 | path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db"); |
| 272 | db.setDatabaseName(path); |
| 273 | bool ok = db.open(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 274 | _LOG_DEBUG("DB opened: " << std::boolalpha << ok ); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 275 | |
| 276 | m_trustScopeModel = new QSqlTableModel; |
| 277 | m_endorseDataModel = new QSqlTableModel; |
| 278 | m_endorseComboBoxDelegate = new EndorseComboBoxDelegate; |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 282 | ContactPanel::loadTrustAnchor() |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 283 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 284 | vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 285 | for(; it != m_contactList.end(); it++) |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 286 | { |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 287 | _LOG_DEBUG("load contact: " << (*it)->getNameSpace().toUri()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 288 | m_policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate()); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 289 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 293 | ContactPanel::setLocalPrefix(int retry) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 294 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 295 | Name interestName("/local/ndn/prefix"); |
| 296 | Interest interest(interestName); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 297 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 298 | m_face->expressInterest(interest, |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 299 | func_lib::bind(&ContactPanel::onLocalPrefix, this, _1, _2), |
| 300 | func_lib::bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 301 | |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 305 | ContactPanel::onLocalPrefix(const shared_ptr<const Interest>& interest, |
| 306 | const shared_ptr<Data>& data) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 307 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 308 | string originPrefix((const char*)data->getContent().value(), data->getContent().value_size()); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 309 | string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData(); |
| 310 | string randomSuffix = getRandomString(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 311 | m_localPrefix = Name(prefix); |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 312 | |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 316 | ContactPanel::onLocalPrefixTimeout(const shared_ptr<const Interest>& interest, |
| 317 | int retry) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 318 | { |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 319 | if(retry > 0) |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 320 | { |
| 321 | setLocalPrefix(retry - 1); |
| 322 | return; |
| 323 | } |
| 324 | else{ |
| 325 | m_localPrefix = Name("/private/local"); |
| 326 | } |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 327 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 328 | |
| 329 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 330 | ContactPanel::setInvitationListener() |
| 331 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 332 | m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation"); |
| 333 | m_inviteListenPrefix.append(m_defaultIdentity); |
| 334 | _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri()); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 335 | m_invitationListenerId = m_face->setInterestFilter(m_inviteListenPrefix, |
| 336 | func_lib::bind(&ContactPanel::onInvitation, this, _1, _2, _3, _4), |
| 337 | func_lib::bind(&ContactPanel::onInvitationRegisterFailed, this, _1)); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 341 | ContactPanel::sendInterest(const Interest& interest, |
| 342 | const OnVerified& onVerified, |
| 343 | const OnVerifyFailed& onVerifyFailed, |
| 344 | const TimeoutNotify& timeoutNotify, |
| 345 | int retry /* = 1 */, |
| 346 | int stepCount /* = 0 */) |
| 347 | { |
| 348 | m_face->expressInterest(interest, |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 349 | func_lib::bind(&ContactPanel::onTargetData, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 350 | this, |
| 351 | _1, |
| 352 | _2, |
| 353 | stepCount, |
| 354 | onVerified, |
| 355 | onVerifyFailed, |
| 356 | timeoutNotify), |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 357 | func_lib::bind(&ContactPanel::onTargetTimeout, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 358 | this, |
| 359 | _1, |
| 360 | retry, |
| 361 | stepCount, |
| 362 | onVerified, |
| 363 | onVerifyFailed, |
| 364 | timeoutNotify)); |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | ContactPanel::onTargetData(const shared_ptr<const ndn::Interest>& interest, |
| 369 | const shared_ptr<Data>& data, |
| 370 | int stepCount, |
| 371 | const OnVerified& onVerified, |
| 372 | const OnVerifyFailed& onVerifyFailed, |
| 373 | const TimeoutNotify& timeoutNotify) |
| 374 | { |
| 375 | shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed); |
| 376 | |
| 377 | if (nextStep) |
| 378 | m_face->expressInterest |
| 379 | (*nextStep->interest_, |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 380 | func_lib::bind(&ContactPanel::onCertData, this, _1, _2, nextStep), |
| 381 | func_lib::bind(&ContactPanel::onCertTimeout, this, _1, onVerifyFailed, data, nextStep)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 382 | |
| 383 | } |
| 384 | |
| 385 | void |
| 386 | ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 387 | int retry, |
| 388 | int stepCount, |
| 389 | const OnVerified& onVerified, |
| 390 | const OnVerifyFailed& onVerifyFailed, |
| 391 | const TimeoutNotify& timeoutNotify) |
| 392 | { |
| 393 | if(retry > 0) |
| 394 | sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount); |
| 395 | else |
| 396 | { |
| 397 | _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!"); |
| 398 | timeoutNotify(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | ContactPanel::onCertData(const shared_ptr<const ndn::Interest>& interest, |
| 404 | const shared_ptr<Data>& cert, |
| 405 | shared_ptr<ValidationRequest> previousStep) |
| 406 | { |
| 407 | shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert, |
| 408 | previousStep->stepCount_, |
| 409 | previousStep->onVerified_, |
| 410 | previousStep->onVerifyFailed_); |
| 411 | |
| 412 | if (nextStep) |
| 413 | m_face->expressInterest |
| 414 | (*nextStep->interest_, |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 415 | func_lib::bind(&ContactPanel::onCertData, this, _1, _2, nextStep), |
| 416 | func_lib::bind(&ContactPanel::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void |
| 420 | ContactPanel::onCertTimeout(const shared_ptr<const ndn::Interest>& interest, |
| 421 | const OnVerifyFailed& onVerifyFailed, |
| 422 | const shared_ptr<Data>& data, |
| 423 | shared_ptr<ValidationRequest> nextStep) |
| 424 | { |
| 425 | if(nextStep->retry_ > 0) |
| 426 | m_face->expressInterest(*interest, |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 427 | func_lib::bind(&ContactPanel::onCertData, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 428 | this, |
| 429 | _1, |
| 430 | _2, |
| 431 | nextStep), |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 432 | func_lib::bind(&ContactPanel::onCertTimeout, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 433 | this, |
| 434 | _1, |
| 435 | onVerifyFailed, |
| 436 | data, |
| 437 | nextStep)); |
| 438 | else |
| 439 | onVerifyFailed(data); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | ContactPanel::onInvitationRegisterFailed(const shared_ptr<const Name>& prefix) |
| 444 | { |
| 445 | showError(QString::fromStdString("Cannot register invitation listening prefix")); |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | ContactPanel::onInvitation(const shared_ptr<const Name>& prefix, |
| 450 | const shared_ptr<const Interest>& interest, |
| 451 | Transport& transport, |
| 452 | uint64_t registeredPrefixId) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 453 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 454 | _LOG_DEBUG("Receive invitation!" << interest->getName().toUri()); |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 455 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 456 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 457 | shared_ptr<ChronosInvitation> invitation; |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 458 | try{ |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 459 | invitation = make_shared<ChronosInvitation>(interest->getName()); |
| 460 | }catch(std::exception& e){ |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 461 | _LOG_ERROR("Exception: " << e.what()); |
| 462 | return; |
| 463 | } |
| 464 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 465 | Name chatroomName("/ndn/broadcast/chronos"); |
| 466 | chatroomName.append(invitation->getChatroom()); |
| 467 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName); |
| 468 | if(it != m_chatDialogs.end()) |
| 469 | { |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 470 | _LOG_ERROR("Exisiting chatroom!"); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 471 | return; |
| 472 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 473 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 474 | shared_ptr<PublicKey> keyPtr = m_policyManager->getTrustedKey(invitation->getInviterCertificateName()); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 475 | |
| 476 | SignatureSha256WithRsa invitationSig; |
| 477 | Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size()); |
| 478 | invitationSig.setValue(sigBlock); |
| 479 | if(CHRONOCHAT_NULL_PUBLICKEY_PTR != keyPtr && Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, *keyPtr)) |
Yingdi Yu | af305d7 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 480 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 481 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(); |
Yingdi Yu | 1ecf81f | 2013-11-10 12:25:43 -0800 | [diff] [blame] | 482 | // hack: incomplete certificate, we don't send it to the wire nor store it anywhere, we only use it to carry information |
Yingdi Yu | af305d7 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 483 | certificate->setName(invitation->getInviterCertificateName()); |
Yingdi Yu | 1ecf81f | 2013-11-10 12:25:43 -0800 | [diff] [blame] | 484 | bool findCert = false; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 485 | vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin(); |
Yingdi Yu | 1ecf81f | 2013-11-10 12:25:43 -0800 | [diff] [blame] | 486 | for(; it != m_contactList.end(); it++) |
| 487 | { |
| 488 | if((*it)->getNameSpace() == invitation->getInviterNameSpace()) |
| 489 | { |
| 490 | certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore()); |
| 491 | certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter()); |
| 492 | findCert = true; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | if(findCert == false) |
| 497 | { |
| 498 | _LOG_ERROR("No SelfEndorseCertificate found!"); |
| 499 | return; |
| 500 | } |
Yingdi Yu | af305d7 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 501 | certificate->setPublicKeyInfo(*keyPtr); |
| 502 | popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate); |
| 503 | return; |
| 504 | } |
| 505 | |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 506 | _LOG_DEBUG("Cannot find the inviter's key in trust anchors"); |
| 507 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 508 | Interest newInterest(invitation->getInviterCertificateName()); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 509 | OnVerified onVerified = func_lib::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation); |
| 510 | OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1); |
| 511 | TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onInvitationCertTimeoutNotify, this); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 512 | |
| 513 | sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 517 | ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data, |
| 518 | shared_ptr<ChronosInvitation> invitation) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 519 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 520 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 521 | |
| 522 | SignatureSha256WithRsa invitationSig; |
| 523 | Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size()); |
| 524 | invitationSig.setValue(sigBlock); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 525 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 526 | if(Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, certificate->getPublicKeyInfo())) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 527 | { |
| 528 | Name keyName = certificate->getPublicKeyName(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 529 | Name inviterNameSpace = keyName.getPrefix(-1); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 530 | popChatInvitation(invitation, inviterNameSpace, certificate); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 535 | ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data) |
| 536 | { _LOG_DEBUG("Cannot verify invitation certificate!"); } |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 537 | |
| 538 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 539 | ContactPanel::onInvitationCertTimeoutNotify() |
| 540 | { _LOG_DEBUG("interest for invitation certificate times out eventually!"); } |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 541 | |
| 542 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 543 | ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation, |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 544 | const Name& inviterNameSpace, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 545 | shared_ptr<IdentityCertificate> certificate) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 546 | { |
| 547 | string alias; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 548 | vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 549 | for(; it != m_contactList.end(); it++) |
| 550 | if((*it)->getNameSpace() == inviterNameSpace) |
| 551 | alias = (*it)->getAlias(); |
| 552 | |
| 553 | if(it != m_contactList.end()) |
| 554 | return; |
| 555 | |
| 556 | m_invitationDialog->setInvitation(alias, invitation, certificate); |
| 557 | emit newInvitationReady(); |
| 558 | } |
| 559 | |
| 560 | void |
| 561 | ContactPanel::collectEndorsement() |
| 562 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 563 | m_collectStatus = make_shared<vector<bool> >(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 564 | m_collectStatus->assign(m_contactList.size(), false); |
| 565 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 566 | vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 567 | int count = 0; |
| 568 | for(; it != m_contactList.end(); it++, count++) |
| 569 | { |
| 570 | Name interestName = (*it)->getNameSpace(); |
| 571 | interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE"); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 572 | Interest interest(interestName); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 573 | interest.setInterestLifetime(1000); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 574 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 575 | OnVerified onVerified = func_lib::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count); |
| 576 | OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count); |
| 577 | TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 578 | |
| 579 | sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 584 | ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 585 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 586 | Data endorseData; |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 587 | endorseData.wireDecode(Block(data->getContent().value(), data->getContent().value_size())); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 588 | EndorseCertificate endorseCertificate(endorseData); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 589 | |
| 590 | m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate); |
| 591 | |
| 592 | updateCollectStatus(count); |
| 593 | } |
| 594 | |
| 595 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 596 | ContactPanel::onDnsEndorseeTimeoutNotify(int count) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 597 | { updateCollectStatus(count); } |
| 598 | |
| 599 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 600 | ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 601 | { updateCollectStatus(count); } |
| 602 | |
| 603 | void |
| 604 | ContactPanel::updateCollectStatus(int count) |
| 605 | { |
| 606 | m_collectStatus->at(count) = true; |
| 607 | vector<bool>::const_iterator it = m_collectStatus->begin(); |
| 608 | for(; it != m_collectStatus->end(); it++) |
| 609 | if(*it == false) |
| 610 | return; |
| 611 | |
| 612 | m_contactManager->publishEndorsedDataInDns(m_defaultIdentity); |
| 613 | } |
| 614 | |
| 615 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 616 | |
| 617 | string |
| 618 | ContactPanel::getRandomString() |
| 619 | { |
| 620 | string randStr; |
| 621 | boost::random::random_device rng; |
| 622 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 623 | for (int i = 0; i < 10; i ++) |
| 624 | { |
| 625 | randStr += chars[index_dist(rng)]; |
| 626 | } |
| 627 | return randStr; |
| 628 | } |
| 629 | |
| 630 | void |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 631 | ContactPanel::showError(const QString& msg){ |
| 632 | QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok); |
| 633 | exit(1); |
| 634 | } |
| 635 | |
| 636 | void |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 637 | ContactPanel::showWarning(const QString& msg){ |
| 638 | QMessageBox::information(this, tr("Chronos"), msg); |
| 639 | } |
| 640 | |
| 641 | void |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 642 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 643 | const QItemSelection &deselected) |
| 644 | { |
| 645 | QModelIndexList items = selected.indexes(); |
| 646 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 647 | string alias = text.toStdString(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 648 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 649 | int i = 0; |
| 650 | for(; i < m_contactList.size(); i++) |
| 651 | { |
| 652 | if(alias == m_contactList[i]->getAlias()) |
| 653 | break; |
| 654 | } |
| 655 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 656 | m_currentSelectedContact = m_contactList[i]; |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 657 | ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName())); |
| 658 | ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri())); |
| 659 | ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution())); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 660 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 661 | if(m_currentSelectedContact->isIntroducer()) |
| 662 | { |
| 663 | ui->isIntroducer->setChecked(true); |
| 664 | ui->addScope->setEnabled(true); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 665 | ui->deleteScope->setEnabled(true); |
| 666 | ui->trustScopeList->setEnabled(true); |
| 667 | |
| 668 | string filter("contact_namespace = '"); |
| 669 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 670 | |
| 671 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 672 | m_trustScopeModel->setTable("TrustScope"); |
| 673 | m_trustScopeModel->setFilter(filter.c_str()); |
| 674 | m_trustScopeModel->select(); |
| 675 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 676 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 677 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 678 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 679 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 680 | ui->trustScopeList->setColumnHidden(0, true); |
| 681 | ui->trustScopeList->setColumnHidden(1, true); |
| 682 | ui->trustScopeList->show(); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 683 | } |
| 684 | else |
| 685 | { |
| 686 | ui->isIntroducer->setChecked(false); |
| 687 | ui->addScope->setEnabled(false); |
| 688 | ui->deleteScope->setEnabled(false); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 689 | |
| 690 | string filter("contact_namespace = '"); |
| 691 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 692 | |
| 693 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 694 | m_trustScopeModel->setTable("TrustScope"); |
| 695 | m_trustScopeModel->setFilter(filter.c_str()); |
| 696 | m_trustScopeModel->select(); |
| 697 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 698 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 699 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 700 | |
| 701 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 702 | ui->trustScopeList->setColumnHidden(0, true); |
| 703 | ui->trustScopeList->setColumnHidden(1, true); |
| 704 | ui->trustScopeList->show(); |
| 705 | |
| 706 | ui->trustScopeList->setEnabled(false); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 707 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 708 | |
| 709 | string filter("profile_identity = '"); |
| 710 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 711 | |
| 712 | m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 713 | m_endorseDataModel->setTable("ContactProfile"); |
| 714 | m_endorseDataModel->setFilter(filter.c_str()); |
| 715 | m_endorseDataModel->select(); |
| 716 | |
| 717 | m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity")); |
| 718 | m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 719 | m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 720 | m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse")); |
| 721 | |
| 722 | ui->endorseList->setModel(m_endorseDataModel); |
| 723 | ui->endorseList->setColumnHidden(0, true); |
| 724 | ui->endorseList->resizeColumnToContents(1); |
| 725 | ui->endorseList->resizeColumnToContents(2); |
| 726 | ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate); |
| 727 | ui->endorseList->show(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 728 | } |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 729 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 730 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 731 | ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 732 | { |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 733 | // _LOG_DEBUG(identity.toStdString()); |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 734 | Name defaultIdentity = Name(identity.toStdString()); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 735 | Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(defaultIdentity); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 736 | if(defaultCertName.size() == 0) |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 737 | { |
| 738 | showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?")); |
| 739 | return; |
| 740 | } |
| 741 | m_defaultIdentity = defaultIdentity; |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 742 | m_profileEditor->setCurrentIdentity(m_defaultIdentity); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 743 | m_nickName = nickName.toStdString(); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 744 | m_face->unsetInterestFilter(m_invitationListenerId); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 745 | m_contactManager->setDefaultIdentity(m_defaultIdentity); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 746 | setInvitationListener(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 747 | collectEndorsement(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 748 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 749 | |
| 750 | void |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 751 | ContactPanel::openProfileEditor() |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 752 | { m_profileEditor->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 753 | |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 754 | void |
| 755 | ContactPanel::openAddContactPanel() |
| 756 | { m_addContactPanel->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 757 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 758 | void |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 759 | ContactPanel::openBrowseContactDialog() |
| 760 | { |
| 761 | m_browseContactDialog->show(); |
| 762 | emit refreshCertDirectory(); |
| 763 | } |
| 764 | |
| 765 | void |
| 766 | ContactPanel::removeContactButton() |
| 767 | { |
| 768 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 769 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
| 770 | QModelIndexList::iterator it = selectedList.begin(); |
| 771 | for(; it != selectedList.end(); it++) |
| 772 | { |
| 773 | string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 774 | vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 775 | for(; contactIt != m_contactList.end(); contactIt++) |
| 776 | { |
| 777 | if((*contactIt)->getAlias() == alias) |
| 778 | { |
| 779 | m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace()); |
| 780 | m_contactList.erase(contactIt); |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | refreshContactList(); |
| 786 | } |
| 787 | |
| 788 | void |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 789 | ContactPanel::openInvitationDialog() |
| 790 | { m_invitationDialog->show(); } |
| 791 | |
| 792 | void |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 793 | ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace) |
| 794 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 795 | shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace); |
| 796 | if(contact != CHRONOCHAT_NULL_CONTACTITEM_PTR) |
| 797 | m_policyManager->addTrustAnchor(contact->getSelfEndorseCertificate()); |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | void |
| 801 | ContactPanel::removeContactFromPanelPolicy(const Name& keyName) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 802 | { m_policyManager->removeTrustAnchor(keyName); } |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 803 | |
| 804 | void |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 805 | ContactPanel::refreshContactList() |
| 806 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 807 | m_contactList.clear(); |
| 808 | m_contactManager->getContactItemList(m_contactList); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 809 | QStringList contactNameList; |
| 810 | for(int i = 0; i < m_contactList.size(); i++) |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 811 | contactNameList << QString::fromStdString(m_contactList[i]->getAlias()); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 812 | |
| 813 | m_contactListModel->setStringList(contactNameList); |
| 814 | } |
| 815 | |
| 816 | void |
| 817 | ContactPanel::showContextMenu(const QPoint& pos) |
| 818 | { |
| 819 | QMenu menu(ui->ContactList); |
| 820 | menu.addAction(m_menuInvite); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 821 | connect(m_menuInvite, SIGNAL(triggered()), |
| 822 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 823 | menu.addSeparator(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 824 | menu.addAction(m_menuAlias); |
| 825 | connect(m_menuAlias, SIGNAL(triggered()), |
| 826 | this, SLOT(openSetAliasDialog())); |
| 827 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 828 | |
| 829 | } |
| 830 | |
| 831 | void |
| 832 | ContactPanel::openSetAliasDialog() |
| 833 | { |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 834 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri()); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 835 | m_setAliasDialog->show(); |
| 836 | } |
| 837 | |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 838 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 839 | ContactPanel::openSettingDialog() |
| 840 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 841 | m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 842 | m_settingDialog->show(); |
| 843 | } |
| 844 | |
| 845 | void |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 846 | ContactPanel::openStartChatDialog() |
| 847 | { |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 848 | Name chatroom("/ndn/broadcast/chronos"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 849 | chatroom.append(string("chatroom-") + getRandomString()); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 850 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 851 | m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri()); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 852 | m_startChatDialog->show(); |
| 853 | } |
| 854 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 855 | // For inviter |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 856 | void |
| 857 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 858 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 859 | Name chatroomName(chatroom.toStdString()); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 860 | |
| 861 | Name inviteeNamespace(invitee.toStdString()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 862 | shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 863 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 864 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 865 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 866 | |
| 867 | connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)), |
| 868 | this, SLOT(removeChatDialog(const ndn::Name&))); |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 869 | connect(chatDialog, SIGNAL(noNdnConnection(const QString&)), |
| 870 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 0aca265 | 2013-11-10 10:47:44 -0800 | [diff] [blame] | 871 | connect(chatDialog, SIGNAL(inivationRejection(const QString&)), |
| 872 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 873 | |
Yingdi Yu | e6476cd | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 874 | // send invitation |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 875 | chatDialog->sendInvitation(inviteeItem, isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 876 | |
| 877 | chatDialog->show(); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 880 | // For Invitee |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 881 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 882 | ContactPanel::startChatroom2(const ChronosInvitation& invitation, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 883 | const IdentityCertificate& identityCertificate) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 884 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 885 | shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace()); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 886 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 887 | Name chatroomName("/ndn/broadcast/chronos"); |
| 888 | chatroomName.append(invitation.getChatroom()); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 889 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 890 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 891 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 892 | connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)), |
| 893 | this, SLOT(removeChatDialog(const ndn::Name&))); |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 894 | connect(chatDialog, SIGNAL(noNdnConnection(const QString&)), |
| 895 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 0aca265 | 2013-11-10 10:47:44 -0800 | [diff] [blame] | 896 | connect(chatDialog, SIGNAL(inivationRejection(const QString&)), |
| 897 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 898 | |
Yingdi Yu | e6476cd | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 899 | chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 900 | chatDialog->publishIntroCert(identityCertificate, true); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 901 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 902 | chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate()); |
| 903 | |
| 904 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 905 | |
| 906 | chatDialog->show(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 910 | ContactPanel::acceptInvitation(const ChronosInvitation& invitation, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 911 | const IdentityCertificate& identityCertificate) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 912 | { |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 913 | Name dataName = invitation.getInterestName(); |
| 914 | time_t nowSeconds = time(NULL); |
| 915 | struct tm current = *gmtime(&nowSeconds); |
| 916 | MillisecondsSince1970 version = timegm(¤t) * 1000.0; |
| 917 | dataName.appendVersion(version); |
| 918 | Data data(dataName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 919 | string content = m_localPrefix.toUri(); |
| 920 | data.setContent((const uint8_t *)&content[0], content.size()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 921 | |
| 922 | Name certificateName; |
| 923 | Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName()); |
| 924 | |
| 925 | if(inferredIdentity.getComponentCount() == 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 926 | certificateName = m_keyChain->getDefaultCertificateName(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 927 | else |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 928 | certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity); |
| 929 | m_keyChain->sign(data, certificateName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 930 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 931 | m_face->put(data); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 932 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 933 | startChatroom2(invitation, identityCertificate); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 937 | ContactPanel::rejectInvitation(const ChronosInvitation& invitation) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 938 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 939 | Data data(invitation.getInterestName()); |
| 940 | string content("nack"); |
| 941 | data.setContent((const uint8_t *)&content[0], content.size()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 942 | |
| 943 | Name certificateName; |
| 944 | Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName()); |
| 945 | if(inferredIdentity.getComponentCount() == 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 946 | certificateName = m_keyChain->getDefaultCertificateName(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 947 | else |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 948 | certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity); |
| 949 | m_keyChain->sign(data, certificateName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 950 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame^] | 951 | m_face->put(data); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 954 | void |
| 955 | ContactPanel::isIntroducerChanged(int state) |
| 956 | { |
| 957 | if(state == Qt::Checked) |
| 958 | { |
| 959 | ui->addScope->setEnabled(true); |
| 960 | ui->deleteScope->setEnabled(true); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 961 | ui->trustScopeList->setEnabled(true); |
| 962 | |
| 963 | string filter("contact_namespace = '"); |
| 964 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 965 | |
| 966 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 967 | m_trustScopeModel->setTable("TrustScope"); |
| 968 | m_trustScopeModel->setFilter(filter.c_str()); |
| 969 | m_trustScopeModel->select(); |
| 970 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 971 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 972 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 973 | |
| 974 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 975 | ui->trustScopeList->setColumnHidden(0, true); |
| 976 | ui->trustScopeList->setColumnHidden(1, true); |
| 977 | ui->trustScopeList->show(); |
| 978 | |
| 979 | m_currentSelectedContact->setIsIntroducer(true); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 980 | } |
| 981 | else |
| 982 | { |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 983 | ui->addScope->setEnabled(false); |
| 984 | ui->deleteScope->setEnabled(false); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 985 | |
| 986 | string filter("contact_namespace = '"); |
| 987 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 988 | |
| 989 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 990 | m_trustScopeModel->setTable("TrustScope"); |
| 991 | m_trustScopeModel->setFilter(filter.c_str()); |
| 992 | m_trustScopeModel->select(); |
| 993 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 994 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 995 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 996 | |
| 997 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 998 | ui->trustScopeList->setColumnHidden(0, true); |
| 999 | ui->trustScopeList->setColumnHidden(1, true); |
| 1000 | ui->trustScopeList->show(); |
| 1001 | |
| 1002 | ui->trustScopeList->setEnabled(false); |
| 1003 | |
| 1004 | m_currentSelectedContact->setIsIntroducer(false); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 1005 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 1006 | m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer()); |
| 1007 | } |
| 1008 | |
| 1009 | void |
| 1010 | ContactPanel::addScopeClicked() |
| 1011 | { |
| 1012 | int rowCount = m_trustScopeModel->rowCount(); |
| 1013 | QSqlRecord record; |
| 1014 | QSqlField identityField("contact_namespace", QVariant::String); |
| 1015 | record.append(identityField); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 1016 | record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri())); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 1017 | m_trustScopeModel->insertRow(rowCount); |
| 1018 | m_trustScopeModel->setRecord(rowCount, record); |
| 1019 | } |
| 1020 | |
| 1021 | void |
| 1022 | ContactPanel::deleteScopeClicked() |
| 1023 | { |
| 1024 | QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel(); |
| 1025 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 1026 | |
| 1027 | int i = indexList.size() - 1; |
| 1028 | for(; i >= 0; i--) |
| 1029 | m_trustScopeModel->removeRow(indexList[i].row()); |
| 1030 | |
| 1031 | m_trustScopeModel->submitAll(); |
| 1032 | } |
| 1033 | |
| 1034 | void |
| 1035 | ContactPanel::saveScopeClicked() |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 1036 | { m_trustScopeModel->submitAll(); } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 1037 | |
| 1038 | void |
| 1039 | ContactPanel::endorseButtonClicked() |
| 1040 | { |
| 1041 | m_endorseDataModel->submitAll(); |
| 1042 | m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 1043 | } |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 1044 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1045 | void |
| 1046 | ContactPanel::removeChatDialog(const ndn::Name& chatroomName) |
| 1047 | { |
| 1048 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 1049 | |
| 1050 | ChatDialog* deletedChat = NULL; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1051 | if(it != m_chatDialogs.end()) |
| 1052 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1053 | deletedChat = it->second; |
| 1054 | m_chatDialogs.erase(it); |
| 1055 | } |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 1056 | if (deletedChat != NULL) |
| 1057 | delete deletedChat; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 1060 | #if WAF |
| 1061 | #include "contactpanel.moc" |
| 1062 | #include "contactpanel.cpp.moc" |
| 1063 | #endif |