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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 24 | #include <ndn.cxx/security/keychain.h> |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 25 | #include <ndn.cxx/security/identity/identity-manager.h> |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 26 | #include <ndn.cxx/common.h> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 28 | #include <boost/random/random_device.hpp> |
| 29 | #include <boost/random/uniform_int_distribution.hpp> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 30 | #include "panel-policy-manager.h" |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 31 | #include "logging.h" |
| 32 | #include "exception.h" |
| 33 | #endif |
| 34 | |
| 35 | namespace fs = boost::filesystem; |
| 36 | using namespace ndn; |
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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 42 | Q_DECLARE_METATYPE(ndn::security::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 | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 53 | { |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 54 | qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 55 | qRegisterMetaType<ChronosInvitation>("ChronosInvitation"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 56 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 57 | createAction(); |
| 58 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 59 | m_contactManager = Ptr<ContactManager>::Create(); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 60 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 61 | connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)), |
| 62 | this, SLOT(showError(const QString&))); |
| 63 | |
| 64 | m_contactManager->setWrapper(); |
| 65 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 66 | openDB(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 67 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 68 | refreshContactList(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 70 | setKeychain(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 71 | |
| 72 | m_defaultIdentity = m_keychain->getDefaultIdentity(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 73 | m_contactManager->setDefaultIdentity(m_defaultIdentity); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 74 | m_nickName = m_defaultIdentity.get(-1).toUri(); |
| 75 | m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 76 | Name defaultCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(m_defaultIdentity); |
| 77 | if(defaultCertName.size() == 0) |
| 78 | showError(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?")); |
| 79 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 80 | |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 81 | m_profileEditor = new ProfileEditor(m_contactManager); |
| 82 | m_profileEditor->setCurrentIdentity(m_defaultIdentity); |
| 83 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 84 | m_addContactPanel = new AddContactPanel(m_contactManager); |
| 85 | m_browseContactDialog = new BrowseContactDialog(m_contactManager); |
| 86 | m_setAliasDialog = new SetAliasDialog(m_contactManager); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 87 | |
| 88 | ui->setupUi(this); |
| 89 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 90 | try{ |
| 91 | m_handler = Ptr<Wrapper>(new Wrapper(m_keychain)); |
| 92 | }catch(ndn::Error::ndnOperation& e){ |
| 93 | showError(QString::fromStdString("Cannot conect to ndnd!\n Have you started your ndnd?")); |
| 94 | } |
| 95 | |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 96 | m_localPrefix = Name("/private/local"); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 97 | setLocalPrefix(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 99 | setInvitationListener(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 100 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 101 | collectEndorsement(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 102 | |
| 103 | ui->ContactList->setModel(m_contactListModel); |
| 104 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 105 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 106 | connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 107 | this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 108 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 109 | connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 110 | this, SLOT(showContextMenu(const QPoint&))); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 111 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 112 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 113 | this, SLOT(openProfileEditor())); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 114 | |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 115 | connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)), |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 116 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 117 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 118 | // connect(ui->AddContactButton, SIGNAL(clicked()), |
| 119 | // this, SLOT(openAddContactPanel())); |
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 | |
| 123 | connect(this, SIGNAL(refreshCertDirectory()), |
| 124 | m_browseContactDialog, SLOT(refreshList())); |
| 125 | |
| 126 | connect(ui->DeleteContactButton, SIGNAL(clicked()), |
| 127 | this, SLOT(removeContactButton())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 128 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 129 | connect(ui->settingButton, SIGNAL(clicked()), |
| 130 | this, SLOT(openSettingDialog())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 132 | connect(m_browseContactDialog, SIGNAL(newContactAdded()), |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 133 | this, SLOT(refreshContactList())); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 134 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 135 | this, SLOT(refreshContactList())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 136 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 137 | connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)), |
| 138 | this, SLOT(startChatroom(const QString&, const QString&, bool))); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 139 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 140 | connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)), |
| 141 | this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&))); |
| 142 | connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)), |
| 143 | this, SLOT(rejectInvitation(const ChronosInvitation&))); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 144 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 145 | connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)), |
| 146 | this, SLOT(updateDefaultIdentity(const QString&, const QString&))); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 147 | |
| 148 | connect(this, SIGNAL(newInvitationReady()), |
| 149 | this, SLOT(openInvitationDialog())); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 150 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 151 | connect(ui->isIntroducer, SIGNAL(stateChanged(int)), |
| 152 | this, SLOT(isIntroducerChanged(int))); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 153 | |
| 154 | connect(ui->addScope, SIGNAL(clicked()), |
| 155 | this, SLOT(addScopeClicked())); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 156 | connect(ui->deleteScope, SIGNAL(clicked()), |
| 157 | this, SLOT(deleteScopeClicked())); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 158 | connect(ui->saveButton, SIGNAL(clicked()), |
| 159 | this, SLOT(saveScopeClicked())); |
| 160 | |
| 161 | connect(ui->endorseButton, SIGNAL(clicked()), |
| 162 | this, SLOT(endorseButtonClicked())); |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ContactPanel::~ContactPanel() |
| 166 | { |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 167 | delete ui; |
| 168 | delete m_contactListModel; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 169 | delete m_startChatDialog; |
| 170 | delete m_invitationDialog; |
| 171 | delete m_settingDialog; |
| 172 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 173 | delete m_profileEditor; |
| 174 | delete m_addContactPanel; |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 175 | delete m_browseContactDialog; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 176 | delete m_setAliasDialog; |
| 177 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 178 | delete m_trustScopeModel; |
| 179 | delete m_endorseDataModel; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 180 | delete m_endorseComboBoxDelegate; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 181 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 182 | delete m_menuInvite; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 183 | delete m_menuAlias; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 184 | |
| 185 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin(); |
| 186 | for(; it != m_chatDialogs.end(); it++) |
| 187 | delete it->second; |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 190 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 191 | ContactPanel::createAction() |
| 192 | { |
| 193 | m_menuInvite = new QAction("&Chat", this); |
| 194 | m_menuAlias = new QAction("&Set Alias", this); |
| 195 | } |
| 196 | |
| 197 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 198 | ContactPanel::openDB() |
| 199 | { |
| 200 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
| 201 | QString path = (QDir::home().path()); |
| 202 | path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db"); |
| 203 | db.setDatabaseName(path); |
| 204 | bool ok = db.open(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 205 | _LOG_DEBUG("DB opened: " << std::boolalpha << ok ); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 206 | |
| 207 | m_trustScopeModel = new QSqlTableModel; |
| 208 | m_endorseDataModel = new QSqlTableModel; |
| 209 | m_endorseComboBoxDelegate = new EndorseComboBoxDelegate; |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
| 213 | ContactPanel::setKeychain() |
| 214 | { |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 215 | m_panelPolicyManager = Ptr<PanelPolicyManager>::Create(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 216 | |
| 217 | vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin(); |
| 218 | for(; it != m_contactList.end(); it++) |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 219 | { |
| 220 | m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate()); |
| 221 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 222 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 223 | m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), |
| 224 | m_panelPolicyManager, |
| 225 | NULL)); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 229 | ContactPanel::setLocalPrefix(int retry) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 230 | { |
| 231 | Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix"))); |
| 232 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 233 | |
| 234 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified, |
| 235 | this, |
| 236 | _1), |
| 237 | boost::bind(&ContactPanel::onLocalPrefixTimeout, |
| 238 | this, |
| 239 | _1, |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 240 | _2, |
| 241 | 10), |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 242 | boost::bind(&ContactPanel::onLocalPrefixVerified, |
| 243 | this, |
| 244 | _1))); |
| 245 | |
| 246 | m_handler->sendInterest(interest, closure); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | ContactPanel::onLocalPrefixVerified(Ptr<Data> data) |
| 251 | { |
| 252 | string originPrefix(data->content().buf(), data->content().size()); |
| 253 | string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData(); |
| 254 | string randomSuffix = getRandomString(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 255 | m_localPrefix = Name(prefix); |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 256 | |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 260 | ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int retry) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 261 | { |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 262 | if(retry > 0) |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 263 | { |
| 264 | setLocalPrefix(retry - 1); |
| 265 | return; |
| 266 | } |
| 267 | else{ |
| 268 | m_localPrefix = Name("/private/local"); |
| 269 | } |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 270 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 271 | |
| 272 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 273 | ContactPanel::setInvitationListener() |
| 274 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 275 | m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation"); |
| 276 | m_inviteListenPrefix.append(m_defaultIdentity); |
| 277 | _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri()); |
| 278 | m_handler->setInterestFilter (m_inviteListenPrefix, |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 279 | boost::bind(&ContactPanel::onInvitation, |
| 280 | this, |
| 281 | _1)); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void |
| 285 | ContactPanel::onInvitation(Ptr<Interest> interest) |
| 286 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 287 | _LOG_DEBUG("Receive invitation!" << interest->getName().toUri()); |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 288 | |
| 289 | Ptr<ChronosInvitation> invitation = NULL; |
| 290 | try{ |
| 291 | invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interest->getName())); |
| 292 | }catch(exception& e){ |
| 293 | _LOG_ERROR("Exception: " << e.what()); |
| 294 | return; |
| 295 | } |
| 296 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 297 | Name chatroomName("/ndn/broadcast/chronos"); |
| 298 | chatroomName.append(invitation->getChatroom()); |
| 299 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName); |
| 300 | if(it != m_chatDialogs.end()) |
| 301 | { |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 302 | _LOG_ERROR("Exisiting chatroom!"); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 303 | return; |
| 304 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 305 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 306 | Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 307 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified, |
| 308 | this, |
| 309 | _1, |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 310 | invitation), |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 311 | boost::bind(&ContactPanel::onTimeout, |
| 312 | this, |
| 313 | _1, |
| 314 | _2), |
| 315 | boost::bind(&ContactPanel::onUnverified, |
| 316 | this, |
| 317 | _1))); |
| 318 | m_handler->sendInterest(newInterest, closure); |
| 319 | } |
| 320 | |
| 321 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 322 | ContactPanel::onInvitationCertVerified(Ptr<Data> data, |
| 323 | Ptr<ChronosInvitation> invitation) |
| 324 | { |
| 325 | using namespace ndn::security; |
| 326 | Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data)); |
| 327 | |
| 328 | if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo())) |
| 329 | { |
| 330 | Name keyName = certificate->getPublicKeyName(); |
| 331 | Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1); |
| 332 | popChatInvitation(invitation, inviterNameSpace, certificate); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | ContactPanel::onUnverified(Ptr<Data> data) |
| 338 | {} |
| 339 | |
| 340 | void |
| 341 | ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
| 342 | {} |
| 343 | |
| 344 | void |
| 345 | ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation, |
| 346 | const Name& inviterNameSpace, |
| 347 | Ptr<security::IdentityCertificate> certificate) |
| 348 | { |
| 349 | string alias; |
| 350 | vector<Ptr<ContactItem> >::iterator it = m_contactList.begin(); |
| 351 | for(; it != m_contactList.end(); it++) |
| 352 | if((*it)->getNameSpace() == inviterNameSpace) |
| 353 | alias = (*it)->getAlias(); |
| 354 | |
| 355 | if(it != m_contactList.end()) |
| 356 | return; |
| 357 | |
| 358 | m_invitationDialog->setInvitation(alias, invitation, certificate); |
| 359 | emit newInvitationReady(); |
| 360 | } |
| 361 | |
| 362 | void |
| 363 | ContactPanel::collectEndorsement() |
| 364 | { |
| 365 | m_collectStatus = Ptr<vector<bool> >::Create(); |
| 366 | m_collectStatus->assign(m_contactList.size(), false); |
| 367 | |
| 368 | vector<Ptr<ContactItem> >::iterator it = m_contactList.begin(); |
| 369 | int count = 0; |
| 370 | for(; it != m_contactList.end(); it++, count++) |
| 371 | { |
| 372 | Name interestName = (*it)->getNameSpace(); |
| 373 | interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE"); |
| 374 | Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName)); |
| 375 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 376 | interest->setInterestLifetime(1); |
| 377 | |
| 378 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified, |
| 379 | this, |
| 380 | _1, |
| 381 | count), |
| 382 | boost::bind(&ContactPanel::onDnsEndorseeTimeout, |
| 383 | this, |
| 384 | _1, |
| 385 | _2, |
| 386 | count), |
| 387 | boost::bind(&ContactPanel::onDnsEndorseeUnverified, |
| 388 | this, |
| 389 | _1, |
| 390 | count))); |
| 391 | |
| 392 | m_handler->sendInterest(interest, closure); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void |
| 397 | ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count) |
| 398 | { |
| 399 | Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size())); |
| 400 | Ptr<Data> endorseData = Data::decodeFromWire(contentBlob); |
| 401 | EndorseCertificate endorseCertificate(*endorseData); |
| 402 | |
| 403 | m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate); |
| 404 | |
| 405 | updateCollectStatus(count); |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count) |
| 410 | { updateCollectStatus(count); } |
| 411 | |
| 412 | void |
| 413 | ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count) |
| 414 | { updateCollectStatus(count); } |
| 415 | |
| 416 | void |
| 417 | ContactPanel::updateCollectStatus(int count) |
| 418 | { |
| 419 | m_collectStatus->at(count) = true; |
| 420 | vector<bool>::const_iterator it = m_collectStatus->begin(); |
| 421 | for(; it != m_collectStatus->end(); it++) |
| 422 | if(*it == false) |
| 423 | return; |
| 424 | |
| 425 | m_contactManager->publishEndorsedDataInDns(m_defaultIdentity); |
| 426 | } |
| 427 | |
| 428 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 429 | |
| 430 | string |
| 431 | ContactPanel::getRandomString() |
| 432 | { |
| 433 | string randStr; |
| 434 | boost::random::random_device rng; |
| 435 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 436 | for (int i = 0; i < 10; i ++) |
| 437 | { |
| 438 | randStr += chars[index_dist(rng)]; |
| 439 | } |
| 440 | return randStr; |
| 441 | } |
| 442 | |
| 443 | void |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 444 | ContactPanel::showError(const QString& msg){ |
| 445 | QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok); |
| 446 | exit(1); |
| 447 | } |
| 448 | |
| 449 | void |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 450 | ContactPanel::showWarning(const QString& msg){ |
| 451 | QMessageBox::information(this, tr("Chronos"), msg); |
| 452 | } |
| 453 | |
| 454 | void |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 455 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 456 | const QItemSelection &deselected) |
| 457 | { |
| 458 | QModelIndexList items = selected.indexes(); |
| 459 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 460 | string alias = text.toStdString(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 461 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 462 | int i = 0; |
| 463 | for(; i < m_contactList.size(); i++) |
| 464 | { |
| 465 | if(alias == m_contactList[i]->getAlias()) |
| 466 | break; |
| 467 | } |
| 468 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 469 | m_currentSelectedContact = m_contactList[i]; |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 470 | ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName())); |
| 471 | ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri())); |
| 472 | ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution())); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 473 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 474 | if(m_currentSelectedContact->isIntroducer()) |
| 475 | { |
| 476 | ui->isIntroducer->setChecked(true); |
| 477 | ui->addScope->setEnabled(true); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 478 | ui->deleteScope->setEnabled(true); |
| 479 | ui->trustScopeList->setEnabled(true); |
| 480 | |
| 481 | string filter("contact_namespace = '"); |
| 482 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 483 | |
| 484 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 485 | m_trustScopeModel->setTable("TrustScope"); |
| 486 | m_trustScopeModel->setFilter(filter.c_str()); |
| 487 | m_trustScopeModel->select(); |
| 488 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 489 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 490 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 491 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 492 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 493 | ui->trustScopeList->setColumnHidden(0, true); |
| 494 | ui->trustScopeList->setColumnHidden(1, true); |
| 495 | ui->trustScopeList->show(); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 496 | } |
| 497 | else |
| 498 | { |
| 499 | ui->isIntroducer->setChecked(false); |
| 500 | ui->addScope->setEnabled(false); |
| 501 | ui->deleteScope->setEnabled(false); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 502 | |
| 503 | string filter("contact_namespace = '"); |
| 504 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 505 | |
| 506 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 507 | m_trustScopeModel->setTable("TrustScope"); |
| 508 | m_trustScopeModel->setFilter(filter.c_str()); |
| 509 | m_trustScopeModel->select(); |
| 510 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 511 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 512 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 513 | |
| 514 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 515 | ui->trustScopeList->setColumnHidden(0, true); |
| 516 | ui->trustScopeList->setColumnHidden(1, true); |
| 517 | ui->trustScopeList->show(); |
| 518 | |
| 519 | ui->trustScopeList->setEnabled(false); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 520 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 521 | |
| 522 | string filter("profile_identity = '"); |
| 523 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 524 | |
| 525 | m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 526 | m_endorseDataModel->setTable("ContactProfile"); |
| 527 | m_endorseDataModel->setFilter(filter.c_str()); |
| 528 | m_endorseDataModel->select(); |
| 529 | |
| 530 | m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity")); |
| 531 | m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 532 | m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 533 | m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse")); |
| 534 | |
| 535 | ui->endorseList->setModel(m_endorseDataModel); |
| 536 | ui->endorseList->setColumnHidden(0, true); |
| 537 | ui->endorseList->resizeColumnToContents(1); |
| 538 | ui->endorseList->resizeColumnToContents(2); |
| 539 | ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate); |
| 540 | ui->endorseList->show(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 541 | } |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 542 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 543 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 544 | ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 545 | { |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 546 | // _LOG_DEBUG(identity.toStdString()); |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 547 | Name defaultIdentity = Name(identity.toStdString()); |
| 548 | Name defaultKeyName = m_keychain->getIdentityManager()->getPublicStorage()->getDefaultKeyNameForIdentity(defaultIdentity); |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 549 | if(defaultKeyName.size() == 0) |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 550 | { |
| 551 | showWarning(QString::fromStdString("Corresponding key is missing!\nHave you created the key?")); |
| 552 | return; |
| 553 | } |
Yingdi Yu | 711f589 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 554 | Name defaultCertName = m_keychain->getIdentityManager()->getPublicStorage()->getDefaultCertificateNameForKey(defaultKeyName); |
| 555 | if(defaultCertName.size() == 0) |
Yingdi Yu | e433a50 | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 556 | { |
| 557 | showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?")); |
| 558 | return; |
| 559 | } |
| 560 | m_defaultIdentity = defaultIdentity; |
Yingdi Yu | e35bdb8 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 561 | m_profileEditor->setCurrentIdentity(m_defaultIdentity); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 562 | m_nickName = nickName.toStdString(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 563 | m_handler->clearInterestFilter(m_inviteListenPrefix); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 564 | m_contactManager->setDefaultIdentity(m_defaultIdentity); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 565 | setInvitationListener(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 566 | collectEndorsement(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 567 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 568 | |
| 569 | void |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 570 | ContactPanel::openProfileEditor() |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 571 | { m_profileEditor->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 572 | |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 573 | void |
| 574 | ContactPanel::openAddContactPanel() |
| 575 | { m_addContactPanel->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 576 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 577 | void |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 578 | ContactPanel::openBrowseContactDialog() |
| 579 | { |
| 580 | m_browseContactDialog->show(); |
| 581 | emit refreshCertDirectory(); |
| 582 | } |
| 583 | |
| 584 | void |
| 585 | ContactPanel::removeContactButton() |
| 586 | { |
| 587 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 588 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
| 589 | QModelIndexList::iterator it = selectedList.begin(); |
| 590 | for(; it != selectedList.end(); it++) |
| 591 | { |
| 592 | string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString(); |
| 593 | vector<Ptr<ContactItem> >::iterator contactIt = m_contactList.begin(); |
| 594 | for(; contactIt != m_contactList.end(); contactIt++) |
| 595 | { |
| 596 | if((*contactIt)->getAlias() == alias) |
| 597 | { |
| 598 | m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace()); |
| 599 | m_contactList.erase(contactIt); |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | refreshContactList(); |
| 605 | } |
| 606 | |
| 607 | void |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 608 | ContactPanel::openInvitationDialog() |
| 609 | { m_invitationDialog->show(); } |
| 610 | |
| 611 | void |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 612 | ContactPanel::refreshContactList() |
| 613 | { |
| 614 | m_contactList = m_contactManager->getContactItemList(); |
| 615 | QStringList contactNameList; |
| 616 | for(int i = 0; i < m_contactList.size(); i++) |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 617 | contactNameList << QString::fromStdString(m_contactList[i]->getAlias()); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 618 | |
| 619 | m_contactListModel->setStringList(contactNameList); |
| 620 | } |
| 621 | |
| 622 | void |
| 623 | ContactPanel::showContextMenu(const QPoint& pos) |
| 624 | { |
| 625 | QMenu menu(ui->ContactList); |
| 626 | menu.addAction(m_menuInvite); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 627 | connect(m_menuInvite, SIGNAL(triggered()), |
| 628 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 629 | menu.addSeparator(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 630 | menu.addAction(m_menuAlias); |
| 631 | connect(m_menuAlias, SIGNAL(triggered()), |
| 632 | this, SLOT(openSetAliasDialog())); |
| 633 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 634 | |
| 635 | } |
| 636 | |
| 637 | void |
| 638 | ContactPanel::openSetAliasDialog() |
| 639 | { |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 640 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri()); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 641 | m_setAliasDialog->show(); |
| 642 | } |
| 643 | |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 644 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 645 | ContactPanel::openSettingDialog() |
| 646 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 647 | m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 648 | m_settingDialog->show(); |
| 649 | } |
| 650 | |
| 651 | void |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 652 | ContactPanel::openStartChatDialog() |
| 653 | { |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 654 | Name chatroom("/ndn/broadcast/chronos"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 655 | chatroom.append(string("chatroom-") + getRandomString()); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 656 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 657 | m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri()); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 658 | m_startChatDialog->show(); |
| 659 | } |
| 660 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 661 | // For inviter |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 662 | void |
| 663 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 664 | { |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 665 | Name chatroomName(chatroom.toUtf8().constData()); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 666 | |
| 667 | Name inviteeNamespace(invitee.toStdString()); |
| 668 | Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 669 | |
Yingdi Yu | e6476cd | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 670 | 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] | 671 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 672 | |
| 673 | connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)), |
| 674 | this, SLOT(removeChatDialog(const ndn::Name&))); |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 675 | connect(chatDialog, SIGNAL(noNdnConnection(const QString&)), |
| 676 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 0aca265 | 2013-11-10 10:47:44 -0800 | [diff] [blame^] | 677 | connect(chatDialog, SIGNAL(inivationRejection(const QString&)), |
| 678 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 679 | |
Yingdi Yu | e6476cd | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 680 | // send invitation |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 681 | chatDialog->sendInvitation(inviteeItem, isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 682 | |
| 683 | chatDialog->show(); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 686 | // For Invitee |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 687 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 688 | ContactPanel::startChatroom2(const ChronosInvitation& invitation, |
| 689 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 690 | { |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 691 | Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace()); |
| 692 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 693 | Name chatroomName("/ndn/broadcast/chronos"); |
| 694 | chatroomName.append(invitation.getChatroom()); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 695 | |
Yingdi Yu | e6476cd | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 696 | 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] | 697 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 698 | connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)), |
| 699 | this, SLOT(removeChatDialog(const ndn::Name&))); |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 700 | connect(chatDialog, SIGNAL(noNdnConnection(const QString&)), |
| 701 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 0aca265 | 2013-11-10 10:47:44 -0800 | [diff] [blame^] | 702 | connect(chatDialog, SIGNAL(inivationRejection(const QString&)), |
| 703 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 704 | |
Yingdi Yu | e6476cd | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 705 | chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 706 | chatDialog->publishIntroCert(identityCertificate, true); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 707 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 708 | chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate()); |
| 709 | |
| 710 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 711 | |
| 712 | chatDialog->show(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 716 | ContactPanel::acceptInvitation(const ChronosInvitation& invitation, |
| 717 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 718 | { |
| 719 | string prefix = m_localPrefix.toUri(); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 720 | m_handler->publishDataByIdentity (invitation.getInterestName(), prefix); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 721 | startChatroom2(invitation, identityCertificate); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 725 | ContactPanel::rejectInvitation(const ChronosInvitation& invitation) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 726 | { |
Yingdi Yu | e9195fd | 2013-11-08 14:13:42 -0800 | [diff] [blame] | 727 | string empty("nack"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 728 | m_handler->publishDataByIdentity (invitation.getInterestName(), empty); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 731 | void |
| 732 | ContactPanel::isIntroducerChanged(int state) |
| 733 | { |
| 734 | if(state == Qt::Checked) |
| 735 | { |
| 736 | ui->addScope->setEnabled(true); |
| 737 | ui->deleteScope->setEnabled(true); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 738 | ui->trustScopeList->setEnabled(true); |
| 739 | |
| 740 | string filter("contact_namespace = '"); |
| 741 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 742 | |
| 743 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 744 | m_trustScopeModel->setTable("TrustScope"); |
| 745 | m_trustScopeModel->setFilter(filter.c_str()); |
| 746 | m_trustScopeModel->select(); |
| 747 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 748 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 749 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 750 | |
| 751 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 752 | ui->trustScopeList->setColumnHidden(0, true); |
| 753 | ui->trustScopeList->setColumnHidden(1, true); |
| 754 | ui->trustScopeList->show(); |
| 755 | |
| 756 | m_currentSelectedContact->setIsIntroducer(true); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 757 | } |
| 758 | else |
| 759 | { |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 760 | ui->addScope->setEnabled(false); |
| 761 | ui->deleteScope->setEnabled(false); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 762 | |
| 763 | string filter("contact_namespace = '"); |
| 764 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 765 | |
| 766 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 767 | m_trustScopeModel->setTable("TrustScope"); |
| 768 | m_trustScopeModel->setFilter(filter.c_str()); |
| 769 | m_trustScopeModel->select(); |
| 770 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 771 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 772 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 773 | |
| 774 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 775 | ui->trustScopeList->setColumnHidden(0, true); |
| 776 | ui->trustScopeList->setColumnHidden(1, true); |
| 777 | ui->trustScopeList->show(); |
| 778 | |
| 779 | ui->trustScopeList->setEnabled(false); |
| 780 | |
| 781 | m_currentSelectedContact->setIsIntroducer(false); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 782 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 783 | m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer()); |
| 784 | } |
| 785 | |
| 786 | void |
| 787 | ContactPanel::addScopeClicked() |
| 788 | { |
| 789 | int rowCount = m_trustScopeModel->rowCount(); |
| 790 | QSqlRecord record; |
| 791 | QSqlField identityField("contact_namespace", QVariant::String); |
| 792 | record.append(identityField); |
| 793 | record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str())); |
| 794 | m_trustScopeModel->insertRow(rowCount); |
| 795 | m_trustScopeModel->setRecord(rowCount, record); |
| 796 | } |
| 797 | |
| 798 | void |
| 799 | ContactPanel::deleteScopeClicked() |
| 800 | { |
| 801 | QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel(); |
| 802 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 803 | |
| 804 | int i = indexList.size() - 1; |
| 805 | for(; i >= 0; i--) |
| 806 | m_trustScopeModel->removeRow(indexList[i].row()); |
| 807 | |
| 808 | m_trustScopeModel->submitAll(); |
| 809 | } |
| 810 | |
| 811 | void |
| 812 | ContactPanel::saveScopeClicked() |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 813 | { m_trustScopeModel->submitAll(); } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 814 | |
| 815 | void |
| 816 | ContactPanel::endorseButtonClicked() |
| 817 | { |
| 818 | m_endorseDataModel->submitAll(); |
| 819 | m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 820 | } |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 821 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 822 | void |
| 823 | ContactPanel::removeChatDialog(const ndn::Name& chatroomName) |
| 824 | { |
| 825 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 826 | |
| 827 | ChatDialog* deletedChat = NULL; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 828 | if(it != m_chatDialogs.end()) |
| 829 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 830 | deletedChat = it->second; |
| 831 | m_chatDialogs.erase(it); |
| 832 | } |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 833 | if (deletedChat != NULL) |
| 834 | delete deletedChat; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 837 | #if WAF |
| 838 | #include "contactpanel.moc" |
| 839 | #include "contactpanel.cpp.moc" |
| 840 | #endif |