Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -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 | |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 11 | #include "contactpanel.h" |
| 12 | #include "ui_contactpanel.h" |
| 13 | |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 14 | |
| 15 | #include <QStringList> |
| 16 | #include <QItemSelectionModel> |
| 17 | #include <QModelIndex> |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 18 | #include <QDir> |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 19 | #include <QtSql/QSqlRecord> |
| 20 | #include <QtSql/QSqlField> |
| 21 | #include <QtSql/QSqlError> |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 22 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 23 | #ifndef Q_MOC_RUN |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 24 | #include <ndn.cxx/security/keychain.h> |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 25 | #include <ndn.cxx/security/identity/identity-manager.h> |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 26 | #include <ndn.cxx/common.h> |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
Yingdi Yu | 53eb8a7 | 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 | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 30 | #include "panel-policy-manager.h" |
Yingdi Yu | 0a6b6c5 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 38 | using namespace std; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 39 | |
| 40 | INIT_LOGGER("ContactPanel"); |
| 41 | |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 42 | Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 43 | Q_DECLARE_METATYPE(ChronosInvitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 45 | ContactPanel::ContactPanel(QWidget *parent) |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 46 | : QDialog(parent) |
| 47 | , ui(new Ui::ContactPanel) |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 48 | , m_warningDialog(new WarningDialog) |
Yingdi Yu | a50c325 | 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 | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 53 | { |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 54 | qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 55 | qRegisterMetaType<ChronosInvitation>("ChronosInvitation"); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 56 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 57 | createAction(); |
| 58 | |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 59 | m_contactManager = Ptr<ContactManager>::Create(); |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 60 | |
Yingdi Yu | b29f78c | 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 | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 66 | openDB(); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 67 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 68 | refreshContactList(); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 70 | setKeychain(); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 71 | |
| 72 | m_defaultIdentity = m_keychain->getDefaultIdentity(); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 73 | m_contactManager->setDefaultIdentity(m_defaultIdentity); |
Yingdi Yu | 4237244 | 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 | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 76 | Name defaultCertName = m_keychain->getIdentityManager()->getDefaultCertificateNameByIdentity(m_defaultIdentity); |
| 77 | if(defaultCertName.size() == 0) |
Yingdi Yu | cf1bdcb | 2013-11-10 10:54:16 -0800 | [diff] [blame] | 78 | showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?"); |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 79 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 80 | |
Yingdi Yu | b35b865 | 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 | b29f78c | 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 | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 87 | |
| 88 | ui->setupUi(this); |
| 89 | |
Yingdi Yu | b29f78c | 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 | 0a953c3 | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 96 | m_localPrefix = Name("/private/local"); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 97 | setLocalPrefix(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 99 | setInvitationListener(); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 100 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 101 | collectEndorsement(); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 102 | |
| 103 | ui->ContactList->setModel(m_contactListModel); |
| 104 | |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 105 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 106 | connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 107 | this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 108 | |
Yingdi Yu | c4d08d2 | 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 | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 111 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 112 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 113 | this, SLOT(openProfileEditor())); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 114 | |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 115 | connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)), |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 116 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 117 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 118 | // connect(ui->AddContactButton, SIGNAL(clicked()), |
| 119 | // this, SLOT(openAddContactPanel())); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 120 | connect(ui->AddContactButton, SIGNAL(clicked()), |
Yingdi Yu | 908f841 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 128 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 129 | connect(ui->settingButton, SIGNAL(clicked()), |
| 130 | this, SLOT(openSettingDialog())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 132 | connect(m_browseContactDialog, SIGNAL(newContactAdded()), |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 133 | this, SLOT(refreshContactList())); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 134 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 135 | this, SLOT(refreshContactList())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 136 | |
Yingdi Yu | c4d08d2 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 139 | |
Yingdi Yu | 42f6646 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 144 | |
Yingdi Yu | 4237244 | 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 | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 147 | |
| 148 | connect(this, SIGNAL(newInvitationReady()), |
| 149 | this, SLOT(openInvitationDialog())); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 150 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 151 | connect(ui->isIntroducer, SIGNAL(stateChanged(int)), |
| 152 | this, SLOT(isIntroducerChanged(int))); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 153 | |
| 154 | connect(ui->addScope, SIGNAL(clicked()), |
| 155 | this, SLOT(addScopeClicked())); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 156 | connect(ui->deleteScope, SIGNAL(clicked()), |
| 157 | this, SLOT(deleteScopeClicked())); |
Yingdi Yu | 8dacdf2 | 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 | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ContactPanel::~ContactPanel() |
| 166 | { |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 167 | delete ui; |
| 168 | delete m_contactListModel; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 169 | delete m_startChatDialog; |
| 170 | delete m_invitationDialog; |
| 171 | delete m_settingDialog; |
| 172 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 173 | delete m_profileEditor; |
| 174 | delete m_addContactPanel; |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 175 | delete m_browseContactDialog; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 176 | delete m_setAliasDialog; |
| 177 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 178 | delete m_trustScopeModel; |
| 179 | delete m_endorseDataModel; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 180 | delete m_endorseComboBoxDelegate; |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 181 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 182 | delete m_menuInvite; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 183 | delete m_menuAlias; |
Yingdi Yu | c4d08d2 | 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 | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 188 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 189 | |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 190 | void |
Yingdi Yu | 4237244 | 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 | 53eb8a7 | 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 | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 205 | _LOG_DEBUG("DB opened: " << std::boolalpha << ok ); |
Yingdi Yu | 8dacdf2 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
| 213 | ContactPanel::setKeychain() |
| 214 | { |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 215 | m_panelPolicyManager = Ptr<PanelPolicyManager>::Create(); |
Yingdi Yu | 53eb8a7 | 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 | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 219 | { |
| 220 | m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate()); |
| 221 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 222 | |
Yingdi Yu | ed8cfc4 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
Yingdi Yu | 2ab22e7 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 229 | ContactPanel::setLocalPrefix(int retry) |
Yingdi Yu | 53eb8a7 | 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 | 2ab22e7 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 240 | _2, |
| 241 | 10), |
Yingdi Yu | 53eb8a7 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 255 | m_localPrefix = Name(prefix); |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 256 | |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
Yingdi Yu | 2ab22e7 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 260 | ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int retry) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 261 | { |
Yingdi Yu | 2ab22e7 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 262 | if(retry > 0) |
Yingdi Yu | 0a953c3 | 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 | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 270 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 271 | |
| 272 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 273 | ContactPanel::setInvitationListener() |
| 274 | { |
Yingdi Yu | 4237244 | 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 | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 279 | boost::bind(&ContactPanel::onInvitation, |
| 280 | this, |
| 281 | _1)); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void |
| 285 | ContactPanel::onInvitation(Ptr<Interest> interest) |
| 286 | { |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 287 | _LOG_DEBUG("Receive invitation!" << interest->getName().toUri()); |
Yingdi Yu | b35b865 | 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 | 4237244 | 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 | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 302 | _LOG_ERROR("Exisiting chatroom!"); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 303 | return; |
| 304 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 305 | |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 306 | Ptr<security::Publickey> keyPtr = m_panelPolicyManager->getTrustedKey(invitation->getInviterCertificateName()); |
| 307 | if(NULL != keyPtr && security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), *keyPtr)) |
| 308 | { |
| 309 | Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>::Create(); |
Yingdi Yu | 9f65724 | 2013-11-10 12:25:43 -0800 | [diff] [blame^] | 310 | // hack: incomplete certificate, we don't send it to the wire nor store it anywhere, we only use it to carry information |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 311 | certificate->setName(invitation->getInviterCertificateName()); |
Yingdi Yu | 9f65724 | 2013-11-10 12:25:43 -0800 | [diff] [blame^] | 312 | bool findCert = false; |
| 313 | vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin(); |
| 314 | for(; it != m_contactList.end(); it++) |
| 315 | { |
| 316 | if((*it)->getNameSpace() == invitation->getInviterNameSpace()) |
| 317 | { |
| 318 | certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore()); |
| 319 | certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter()); |
| 320 | findCert = true; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if(findCert == false) |
| 325 | { |
| 326 | _LOG_ERROR("No SelfEndorseCertificate found!"); |
| 327 | return; |
| 328 | } |
Yingdi Yu | 6b56f09 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 329 | certificate->setPublicKeyInfo(*keyPtr); |
| 330 | popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate); |
| 331 | return; |
| 332 | } |
| 333 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 334 | Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 335 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified, |
| 336 | this, |
| 337 | _1, |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 338 | invitation), |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 339 | boost::bind(&ContactPanel::onTimeout, |
| 340 | this, |
| 341 | _1, |
| 342 | _2), |
| 343 | boost::bind(&ContactPanel::onUnverified, |
| 344 | this, |
| 345 | _1))); |
| 346 | m_handler->sendInterest(newInterest, closure); |
| 347 | } |
| 348 | |
| 349 | void |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 350 | ContactPanel::onInvitationCertVerified(Ptr<Data> data, |
| 351 | Ptr<ChronosInvitation> invitation) |
| 352 | { |
| 353 | using namespace ndn::security; |
| 354 | Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data)); |
| 355 | |
| 356 | if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo())) |
| 357 | { |
| 358 | Name keyName = certificate->getPublicKeyName(); |
| 359 | Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1); |
| 360 | popChatInvitation(invitation, inviterNameSpace, certificate); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void |
| 365 | ContactPanel::onUnverified(Ptr<Data> data) |
| 366 | {} |
| 367 | |
| 368 | void |
| 369 | ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
| 370 | {} |
| 371 | |
| 372 | void |
| 373 | ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation, |
| 374 | const Name& inviterNameSpace, |
| 375 | Ptr<security::IdentityCertificate> certificate) |
| 376 | { |
| 377 | string alias; |
| 378 | vector<Ptr<ContactItem> >::iterator it = m_contactList.begin(); |
| 379 | for(; it != m_contactList.end(); it++) |
| 380 | if((*it)->getNameSpace() == inviterNameSpace) |
| 381 | alias = (*it)->getAlias(); |
| 382 | |
| 383 | if(it != m_contactList.end()) |
| 384 | return; |
| 385 | |
| 386 | m_invitationDialog->setInvitation(alias, invitation, certificate); |
| 387 | emit newInvitationReady(); |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | ContactPanel::collectEndorsement() |
| 392 | { |
| 393 | m_collectStatus = Ptr<vector<bool> >::Create(); |
| 394 | m_collectStatus->assign(m_contactList.size(), false); |
| 395 | |
| 396 | vector<Ptr<ContactItem> >::iterator it = m_contactList.begin(); |
| 397 | int count = 0; |
| 398 | for(; it != m_contactList.end(); it++, count++) |
| 399 | { |
| 400 | Name interestName = (*it)->getNameSpace(); |
| 401 | interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE"); |
| 402 | Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName)); |
| 403 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 404 | interest->setInterestLifetime(1); |
| 405 | |
| 406 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified, |
| 407 | this, |
| 408 | _1, |
| 409 | count), |
| 410 | boost::bind(&ContactPanel::onDnsEndorseeTimeout, |
| 411 | this, |
| 412 | _1, |
| 413 | _2, |
| 414 | count), |
| 415 | boost::bind(&ContactPanel::onDnsEndorseeUnverified, |
| 416 | this, |
| 417 | _1, |
| 418 | count))); |
| 419 | |
| 420 | m_handler->sendInterest(interest, closure); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count) |
| 426 | { |
| 427 | Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size())); |
| 428 | Ptr<Data> endorseData = Data::decodeFromWire(contentBlob); |
| 429 | EndorseCertificate endorseCertificate(*endorseData); |
| 430 | |
| 431 | m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate); |
| 432 | |
| 433 | updateCollectStatus(count); |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count) |
| 438 | { updateCollectStatus(count); } |
| 439 | |
| 440 | void |
| 441 | ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count) |
| 442 | { updateCollectStatus(count); } |
| 443 | |
| 444 | void |
| 445 | ContactPanel::updateCollectStatus(int count) |
| 446 | { |
| 447 | m_collectStatus->at(count) = true; |
| 448 | vector<bool>::const_iterator it = m_collectStatus->begin(); |
| 449 | for(; it != m_collectStatus->end(); it++) |
| 450 | if(*it == false) |
| 451 | return; |
| 452 | |
| 453 | m_contactManager->publishEndorsedDataInDns(m_defaultIdentity); |
| 454 | } |
| 455 | |
| 456 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 457 | |
| 458 | string |
| 459 | ContactPanel::getRandomString() |
| 460 | { |
| 461 | string randStr; |
| 462 | boost::random::random_device rng; |
| 463 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 464 | for (int i = 0; i < 10; i ++) |
| 465 | { |
| 466 | randStr += chars[index_dist(rng)]; |
| 467 | } |
| 468 | return randStr; |
| 469 | } |
| 470 | |
| 471 | void |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 472 | ContactPanel::showError(const QString& msg){ |
| 473 | QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok); |
| 474 | exit(1); |
| 475 | } |
| 476 | |
| 477 | void |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 478 | ContactPanel::showWarning(const QString& msg){ |
| 479 | QMessageBox::information(this, tr("Chronos"), msg); |
| 480 | } |
| 481 | |
| 482 | void |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 483 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 484 | const QItemSelection &deselected) |
| 485 | { |
| 486 | QModelIndexList items = selected.indexes(); |
| 487 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 488 | string alias = text.toStdString(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 489 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 490 | int i = 0; |
| 491 | for(; i < m_contactList.size(); i++) |
| 492 | { |
| 493 | if(alias == m_contactList[i]->getAlias()) |
| 494 | break; |
| 495 | } |
| 496 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 497 | m_currentSelectedContact = m_contactList[i]; |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 498 | ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName())); |
| 499 | ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri())); |
| 500 | ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution())); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 501 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 502 | if(m_currentSelectedContact->isIntroducer()) |
| 503 | { |
| 504 | ui->isIntroducer->setChecked(true); |
| 505 | ui->addScope->setEnabled(true); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 506 | ui->deleteScope->setEnabled(true); |
| 507 | ui->trustScopeList->setEnabled(true); |
| 508 | |
| 509 | string filter("contact_namespace = '"); |
| 510 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 511 | |
| 512 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 513 | m_trustScopeModel->setTable("TrustScope"); |
| 514 | m_trustScopeModel->setFilter(filter.c_str()); |
| 515 | m_trustScopeModel->select(); |
| 516 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 517 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 518 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 519 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 520 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 521 | ui->trustScopeList->setColumnHidden(0, true); |
| 522 | ui->trustScopeList->setColumnHidden(1, true); |
| 523 | ui->trustScopeList->show(); |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 524 | } |
| 525 | else |
| 526 | { |
| 527 | ui->isIntroducer->setChecked(false); |
| 528 | ui->addScope->setEnabled(false); |
| 529 | ui->deleteScope->setEnabled(false); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 530 | |
| 531 | string filter("contact_namespace = '"); |
| 532 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 533 | |
| 534 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 535 | m_trustScopeModel->setTable("TrustScope"); |
| 536 | m_trustScopeModel->setFilter(filter.c_str()); |
| 537 | m_trustScopeModel->select(); |
| 538 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 539 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 540 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 541 | |
| 542 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 543 | ui->trustScopeList->setColumnHidden(0, true); |
| 544 | ui->trustScopeList->setColumnHidden(1, true); |
| 545 | ui->trustScopeList->show(); |
| 546 | |
| 547 | ui->trustScopeList->setEnabled(false); |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 548 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 549 | |
| 550 | string filter("profile_identity = '"); |
| 551 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 552 | |
| 553 | m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 554 | m_endorseDataModel->setTable("ContactProfile"); |
| 555 | m_endorseDataModel->setFilter(filter.c_str()); |
| 556 | m_endorseDataModel->select(); |
| 557 | |
| 558 | m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity")); |
| 559 | m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 560 | m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 561 | m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse")); |
| 562 | |
| 563 | ui->endorseList->setModel(m_endorseDataModel); |
| 564 | ui->endorseList->setColumnHidden(0, true); |
| 565 | ui->endorseList->resizeColumnToContents(1); |
| 566 | ui->endorseList->resizeColumnToContents(2); |
| 567 | ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate); |
| 568 | ui->endorseList->show(); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 569 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 570 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 571 | void |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 572 | ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 573 | { |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 574 | // _LOG_DEBUG(identity.toStdString()); |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 575 | Name defaultIdentity = Name(identity.toStdString()); |
| 576 | Name defaultKeyName = m_keychain->getIdentityManager()->getPublicStorage()->getDefaultKeyNameForIdentity(defaultIdentity); |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 577 | if(defaultKeyName.size() == 0) |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 578 | { |
| 579 | showWarning(QString::fromStdString("Corresponding key is missing!\nHave you created the key?")); |
| 580 | return; |
| 581 | } |
Yingdi Yu | 71ec965 | 2013-11-09 22:18:26 -0800 | [diff] [blame] | 582 | Name defaultCertName = m_keychain->getIdentityManager()->getPublicStorage()->getDefaultCertificateNameForKey(defaultKeyName); |
| 583 | if(defaultCertName.size() == 0) |
Yingdi Yu | eb98f7d | 2013-11-10 01:34:57 -0800 | [diff] [blame] | 584 | { |
| 585 | showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?")); |
| 586 | return; |
| 587 | } |
| 588 | m_defaultIdentity = defaultIdentity; |
Yingdi Yu | b35b865 | 2013-11-07 11:32:40 -0800 | [diff] [blame] | 589 | m_profileEditor->setCurrentIdentity(m_defaultIdentity); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 590 | m_nickName = nickName.toStdString(); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 591 | m_handler->clearInterestFilter(m_inviteListenPrefix); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 592 | m_contactManager->setDefaultIdentity(m_defaultIdentity); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 593 | setInvitationListener(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 594 | collectEndorsement(); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 595 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 596 | |
| 597 | void |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 598 | ContactPanel::openProfileEditor() |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 599 | { m_profileEditor->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 600 | |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 601 | void |
| 602 | ContactPanel::openAddContactPanel() |
| 603 | { m_addContactPanel->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 604 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 605 | void |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 606 | ContactPanel::openBrowseContactDialog() |
| 607 | { |
| 608 | m_browseContactDialog->show(); |
| 609 | emit refreshCertDirectory(); |
| 610 | } |
| 611 | |
| 612 | void |
| 613 | ContactPanel::removeContactButton() |
| 614 | { |
| 615 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 616 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
| 617 | QModelIndexList::iterator it = selectedList.begin(); |
| 618 | for(; it != selectedList.end(); it++) |
| 619 | { |
| 620 | string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString(); |
| 621 | vector<Ptr<ContactItem> >::iterator contactIt = m_contactList.begin(); |
| 622 | for(; contactIt != m_contactList.end(); contactIt++) |
| 623 | { |
| 624 | if((*contactIt)->getAlias() == alias) |
| 625 | { |
| 626 | m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace()); |
| 627 | m_contactList.erase(contactIt); |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | refreshContactList(); |
| 633 | } |
| 634 | |
| 635 | void |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 636 | ContactPanel::openInvitationDialog() |
| 637 | { m_invitationDialog->show(); } |
| 638 | |
| 639 | void |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 640 | ContactPanel::refreshContactList() |
| 641 | { |
| 642 | m_contactList = m_contactManager->getContactItemList(); |
| 643 | QStringList contactNameList; |
| 644 | for(int i = 0; i < m_contactList.size(); i++) |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 645 | contactNameList << QString::fromStdString(m_contactList[i]->getAlias()); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 646 | |
| 647 | m_contactListModel->setStringList(contactNameList); |
| 648 | } |
| 649 | |
| 650 | void |
| 651 | ContactPanel::showContextMenu(const QPoint& pos) |
| 652 | { |
| 653 | QMenu menu(ui->ContactList); |
| 654 | menu.addAction(m_menuInvite); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 655 | connect(m_menuInvite, SIGNAL(triggered()), |
| 656 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 657 | menu.addSeparator(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 658 | menu.addAction(m_menuAlias); |
| 659 | connect(m_menuAlias, SIGNAL(triggered()), |
| 660 | this, SLOT(openSetAliasDialog())); |
| 661 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 662 | |
| 663 | } |
| 664 | |
| 665 | void |
| 666 | ContactPanel::openSetAliasDialog() |
| 667 | { |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 668 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri()); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 669 | m_setAliasDialog->show(); |
| 670 | } |
| 671 | |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 672 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 673 | ContactPanel::openSettingDialog() |
| 674 | { |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 675 | m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 676 | m_settingDialog->show(); |
| 677 | } |
| 678 | |
| 679 | void |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 680 | ContactPanel::openStartChatDialog() |
| 681 | { |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 682 | Name chatroom("/ndn/broadcast/chronos"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 683 | chatroom.append(string("chatroom-") + getRandomString()); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 684 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 685 | m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri()); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 686 | m_startChatDialog->show(); |
| 687 | } |
| 688 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 689 | // For inviter |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 690 | void |
| 691 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 692 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 693 | Name chatroomName(chatroom.toUtf8().constData()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 694 | |
| 695 | Name inviteeNamespace(invitee.toStdString()); |
| 696 | Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 697 | |
Yingdi Yu | c90deb1 | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 698 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 699 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 700 | |
| 701 | connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)), |
| 702 | this, SLOT(removeChatDialog(const ndn::Name&))); |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 703 | connect(chatDialog, SIGNAL(noNdnConnection(const QString&)), |
| 704 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 3e87bd8 | 2013-11-10 10:47:44 -0800 | [diff] [blame] | 705 | connect(chatDialog, SIGNAL(inivationRejection(const QString&)), |
| 706 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 707 | |
Yingdi Yu | c90deb1 | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 708 | // send invitation |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 709 | chatDialog->sendInvitation(inviteeItem, isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 710 | |
| 711 | chatDialog->show(); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 714 | // For Invitee |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 715 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 716 | ContactPanel::startChatroom2(const ChronosInvitation& invitation, |
| 717 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 718 | { |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 719 | Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace()); |
| 720 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 721 | Name chatroomName("/ndn/broadcast/chronos"); |
| 722 | chatroomName.append(invitation.getChatroom()); |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 723 | |
Yingdi Yu | c90deb1 | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 724 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true); |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 725 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 726 | connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)), |
| 727 | this, SLOT(removeChatDialog(const ndn::Name&))); |
Yingdi Yu | b29f78c | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 728 | connect(chatDialog, SIGNAL(noNdnConnection(const QString&)), |
| 729 | this, SLOT(showError(const QString&))); |
Yingdi Yu | 3e87bd8 | 2013-11-10 10:47:44 -0800 | [diff] [blame] | 730 | connect(chatDialog, SIGNAL(inivationRejection(const QString&)), |
| 731 | this, SLOT(showWarning(const QString&))); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 732 | |
Yingdi Yu | c90deb1 | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 733 | chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true); |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 734 | chatDialog->publishIntroCert(identityCertificate, true); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 735 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 736 | chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate()); |
| 737 | |
| 738 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 739 | |
| 740 | chatDialog->show(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 744 | ContactPanel::acceptInvitation(const ChronosInvitation& invitation, |
| 745 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 746 | { |
| 747 | string prefix = m_localPrefix.toUri(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 748 | m_handler->publishDataByIdentity (invitation.getInterestName(), prefix); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 749 | startChatroom2(invitation, identityCertificate); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 753 | ContactPanel::rejectInvitation(const ChronosInvitation& invitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 754 | { |
Yingdi Yu | 9793635 | 2013-11-08 14:13:42 -0800 | [diff] [blame] | 755 | string empty("nack"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 756 | m_handler->publishDataByIdentity (invitation.getInterestName(), empty); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 759 | void |
| 760 | ContactPanel::isIntroducerChanged(int state) |
| 761 | { |
| 762 | if(state == Qt::Checked) |
| 763 | { |
| 764 | ui->addScope->setEnabled(true); |
| 765 | ui->deleteScope->setEnabled(true); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 766 | ui->trustScopeList->setEnabled(true); |
| 767 | |
| 768 | string filter("contact_namespace = '"); |
| 769 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 770 | |
| 771 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 772 | m_trustScopeModel->setTable("TrustScope"); |
| 773 | m_trustScopeModel->setFilter(filter.c_str()); |
| 774 | m_trustScopeModel->select(); |
| 775 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 776 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 777 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 778 | |
| 779 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 780 | ui->trustScopeList->setColumnHidden(0, true); |
| 781 | ui->trustScopeList->setColumnHidden(1, true); |
| 782 | ui->trustScopeList->show(); |
| 783 | |
| 784 | m_currentSelectedContact->setIsIntroducer(true); |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 785 | } |
| 786 | else |
| 787 | { |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 788 | ui->addScope->setEnabled(false); |
| 789 | ui->deleteScope->setEnabled(false); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 790 | |
| 791 | string filter("contact_namespace = '"); |
| 792 | filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'"); |
| 793 | |
| 794 | m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 795 | m_trustScopeModel->setTable("TrustScope"); |
| 796 | m_trustScopeModel->setFilter(filter.c_str()); |
| 797 | m_trustScopeModel->select(); |
| 798 | m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); |
| 799 | m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact")); |
| 800 | m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope")); |
| 801 | |
| 802 | ui->trustScopeList->setModel(m_trustScopeModel); |
| 803 | ui->trustScopeList->setColumnHidden(0, true); |
| 804 | ui->trustScopeList->setColumnHidden(1, true); |
| 805 | ui->trustScopeList->show(); |
| 806 | |
| 807 | ui->trustScopeList->setEnabled(false); |
| 808 | |
| 809 | m_currentSelectedContact->setIsIntroducer(false); |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 810 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 811 | m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer()); |
| 812 | } |
| 813 | |
| 814 | void |
| 815 | ContactPanel::addScopeClicked() |
| 816 | { |
| 817 | int rowCount = m_trustScopeModel->rowCount(); |
| 818 | QSqlRecord record; |
| 819 | QSqlField identityField("contact_namespace", QVariant::String); |
| 820 | record.append(identityField); |
| 821 | record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str())); |
| 822 | m_trustScopeModel->insertRow(rowCount); |
| 823 | m_trustScopeModel->setRecord(rowCount, record); |
| 824 | } |
| 825 | |
| 826 | void |
| 827 | ContactPanel::deleteScopeClicked() |
| 828 | { |
| 829 | QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel(); |
| 830 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 831 | |
| 832 | int i = indexList.size() - 1; |
| 833 | for(; i >= 0; i--) |
| 834 | m_trustScopeModel->removeRow(indexList[i].row()); |
| 835 | |
| 836 | m_trustScopeModel->submitAll(); |
| 837 | } |
| 838 | |
| 839 | void |
| 840 | ContactPanel::saveScopeClicked() |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 841 | { m_trustScopeModel->submitAll(); } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 842 | |
| 843 | void |
| 844 | ContactPanel::endorseButtonClicked() |
| 845 | { |
| 846 | m_endorseDataModel->submitAll(); |
| 847 | m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity); |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 848 | } |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 849 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 850 | void |
| 851 | ContactPanel::removeChatDialog(const ndn::Name& chatroomName) |
| 852 | { |
| 853 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName); |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 854 | |
| 855 | ChatDialog* deletedChat = NULL; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 856 | if(it != m_chatDialogs.end()) |
| 857 | { |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 858 | deletedChat = it->second; |
| 859 | m_chatDialogs.erase(it); |
| 860 | } |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 861 | if (deletedChat != NULL) |
| 862 | delete deletedChat; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 865 | #if WAF |
| 866 | #include "contactpanel.moc" |
| 867 | #include "contactpanel.cpp.moc" |
| 868 | #endif |