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 | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 20 | #ifndef Q_MOC_RUN |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 21 | #include <ndn.cxx/security/keychain.h> |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 22 | #include <ndn.cxx/security/identity/identity-manager.h> |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 23 | #include <ndn.cxx/common.h> |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 24 | #include <boost/filesystem.hpp> |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 25 | #include <boost/random/random_device.hpp> |
| 26 | #include <boost/random/uniform_int_distribution.hpp> |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 27 | #include "panel-policy-manager.h" |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 28 | #include "logging.h" |
| 29 | #include "exception.h" |
| 30 | #endif |
| 31 | |
| 32 | namespace fs = boost::filesystem; |
| 33 | using namespace ndn; |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 34 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 35 | using namespace std; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 36 | |
| 37 | INIT_LOGGER("ContactPanel"); |
| 38 | |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 39 | Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 40 | Q_DECLARE_METATYPE(ChronosInvitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 0b82a4e | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 42 | ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 43 | : QDialog(parent) |
| 44 | , ui(new Ui::ContactPanel) |
| 45 | , m_contactListModel(new QStringListModel) |
| 46 | , m_startChatDialog(new StartChatDialog) |
| 47 | , m_invitationDialog(new InvitationDialog) |
| 48 | , m_settingDialog(new SettingDialog) |
| 49 | , m_menuInvite(new QAction("&Chat", this)) |
| 50 | , m_menuAlias(new QAction("&Set Alias", this)) |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 51 | { |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 52 | qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 53 | qRegisterMetaType<ChronosInvitation>("ChronosInvitation"); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 54 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 55 | openDB(); |
| 56 | |
| 57 | m_contactManager = contactManager; |
| 58 | m_profileEditor = new ProfileEditor(m_contactManager); |
| 59 | m_addContactPanel = new AddContactPanel(contactManager); |
| 60 | m_setAliasDialog = new SetAliasDialog(contactManager); |
| 61 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 62 | ui->setupUi(this); |
| 63 | refreshContactList(); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 64 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 65 | setKeychain(); |
| 66 | m_handler = Ptr<Wrapper>(new Wrapper(m_keychain)); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 68 | setLocalPrefix(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 70 | // Set Identity, TODO: through user interface |
| 71 | m_defaultIdentity = m_keychain->getDefaultIdentity(); |
| 72 | m_settingDialog->setIdentity(m_defaultIdentity.toUri()); |
| 73 | setInvitationListener(); |
| 74 | |
| 75 | ui->ContactList->setModel(m_contactListModel); |
| 76 | |
| 77 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
Yingdi Yu | c26af3c | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 78 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 79 | connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 80 | this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 82 | connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)), |
| 83 | this, SLOT(showContextMenu(const QPoint&))); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 84 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 85 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 86 | this, SLOT(openProfileEditor())); |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 87 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 88 | connect(ui->AddContactButton, SIGNAL(clicked()), |
| 89 | this, SLOT(openAddContactPanel())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 91 | connect(ui->settingButton, SIGNAL(clicked()), |
| 92 | this, SLOT(openSettingDialog())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 94 | connect(m_addContactPanel, SIGNAL(newContactAdded()), |
| 95 | this, SLOT(refreshContactList())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 96 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 97 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 98 | this, SLOT(refreshContactList())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 99 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 100 | connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)), |
| 101 | this, SLOT(startChatroom(const QString&, const QString&, bool))); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 102 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 103 | connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)), |
| 104 | this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&))); |
| 105 | connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)), |
| 106 | this, SLOT(rejectInvitation(const ChronosInvitation&))); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 108 | connect(m_settingDialog, SIGNAL(identitySet(const QString&)), |
| 109 | this, SLOT(updateDefaultIdentity(const QString&))); |
| 110 | |
| 111 | connect(this, SIGNAL(newInvitationReady()), |
| 112 | this, SLOT(openInvitationDialog())); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 114 | connect(ui->isIntroducer, SIGNAL(stateChanged(int)), |
| 115 | this, SLOT(isIntroducerChanged(int))); |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ContactPanel::~ContactPanel() |
| 119 | { |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 120 | delete ui; |
| 121 | delete m_contactListModel; |
| 122 | delete m_profileEditor; |
| 123 | delete m_addContactPanel; |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 124 | if(NULL != m_currentContactTrustScopeListModel) |
| 125 | delete m_currentContactTrustScopeListModel; |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 127 | delete m_menuInvite; |
| 128 | |
| 129 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin(); |
| 130 | for(; it != m_chatDialogs.end(); it++) |
| 131 | delete it->second; |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 132 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 133 | |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 134 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 135 | ContactPanel::openDB() |
| 136 | { |
| 137 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
| 138 | QString path = (QDir::home().path()); |
| 139 | path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db"); |
| 140 | db.setDatabaseName(path); |
| 141 | bool ok = db.open(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 142 | _LOG_DEBUG("db opened: " << std::boolalpha << ok ); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
| 146 | ContactPanel::setKeychain() |
| 147 | { |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 148 | m_panelPolicyManager = Ptr<PanelPolicyManager>::Create(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 149 | // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db")); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 150 | |
| 151 | vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin(); |
| 152 | for(; it != m_contactList.end(); it++) |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 153 | m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate()); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 155 | m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), |
| 156 | m_panelPolicyManager, |
| 157 | NULL)); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
| 161 | ContactPanel::setLocalPrefix() |
| 162 | { |
| 163 | Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix"))); |
| 164 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 165 | |
| 166 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified, |
| 167 | this, |
| 168 | _1), |
| 169 | boost::bind(&ContactPanel::onLocalPrefixTimeout, |
| 170 | this, |
| 171 | _1, |
| 172 | _2), |
| 173 | boost::bind(&ContactPanel::onLocalPrefixVerified, |
| 174 | this, |
| 175 | _1))); |
| 176 | |
| 177 | m_handler->sendInterest(interest, closure); |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | ContactPanel::onLocalPrefixVerified(Ptr<Data> data) |
| 182 | { |
| 183 | string originPrefix(data->content().buf(), data->content().size()); |
| 184 | string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData(); |
| 185 | string randomSuffix = getRandomString(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 186 | m_localPrefix = Name(prefix); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 187 | m_localPrefix.append(randomSuffix); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 192 | { |
| 193 | string randomSuffix = getRandomString(); |
| 194 | m_localPrefix = Name("/private/local"); |
| 195 | m_localPrefix.append(randomSuffix); |
| 196 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 197 | |
| 198 | void |
| 199 | ContactPanel::onUnverified(Ptr<Data> data) |
| 200 | {} |
| 201 | |
| 202 | void |
| 203 | ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
| 204 | {} |
| 205 | |
| 206 | void |
| 207 | ContactPanel::onInvitationCertVerified(Ptr<Data> data, |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 208 | Ptr<ChronosInvitation> invitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 209 | { |
| 210 | Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data)); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 211 | |
| 212 | if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo())) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 213 | { |
| 214 | Name keyName = certificate->getPublicKeyName(); |
| 215 | Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 216 | popChatInvitation(invitation, inviterNameSpace, certificate); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 221 | ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation, |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 222 | const Name& inviterNameSpace, |
| 223 | Ptr<security::IdentityCertificate> certificate) |
| 224 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 225 | string chatroom = invitation->getChatroom().get(0).toUri(); |
| 226 | string inviter = inviterNameSpace.toUri(); |
| 227 | |
| 228 | string alias; |
| 229 | vector<Ptr<ContactItem> >::iterator it = m_contactList.begin(); |
| 230 | for(; it != m_contactList.end(); it++) |
| 231 | if((*it)->getNameSpace() == inviterNameSpace) |
| 232 | alias = (*it)->getAlias(); |
| 233 | |
| 234 | if(it != m_contactList.end()) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 235 | return; |
| 236 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 237 | m_invitationDialog->setInvitation(alias, invitation, certificate); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 238 | emit newInvitationReady(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 242 | |
| 243 | string |
| 244 | ContactPanel::getRandomString() |
| 245 | { |
| 246 | string randStr; |
| 247 | boost::random::random_device rng; |
| 248 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 249 | for (int i = 0; i < 10; i ++) |
| 250 | { |
| 251 | randStr += chars[index_dist(rng)]; |
| 252 | } |
| 253 | return randStr; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void |
| 258 | ContactPanel::setInvitationListener() |
| 259 | { |
| 260 | Name prefix("/ndn/broadcast/chronos/invitation"); |
| 261 | prefix.append(m_defaultIdentity); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 262 | _LOG_DEBUG("prefix: " << prefix.toUri()); |
| 263 | m_inviteListenPrefix = prefix; |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 264 | m_handler->setInterestFilter (prefix, |
| 265 | boost::bind(&ContactPanel::onInvitation, |
| 266 | this, |
| 267 | _1)); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | ContactPanel::onInvitation(Ptr<Interest> interest) |
| 273 | { |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 274 | _LOG_DEBUG("receive interest!" << interest->getName().toUri()); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 275 | const Name& interestName = interest->getName(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 276 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 277 | Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName)); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 278 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 279 | Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 280 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified, |
| 281 | this, |
| 282 | _1, |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 283 | invitation), |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 284 | boost::bind(&ContactPanel::onTimeout, |
| 285 | this, |
| 286 | _1, |
| 287 | _2), |
| 288 | boost::bind(&ContactPanel::onUnverified, |
| 289 | this, |
| 290 | _1))); |
| 291 | m_handler->sendInterest(newInterest, closure); |
| 292 | } |
| 293 | |
| 294 | void |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 295 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 296 | const QItemSelection &deselected) |
| 297 | { |
| 298 | QModelIndexList items = selected.indexes(); |
| 299 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 300 | string alias = text.toUtf8().constData(); |
| 301 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 302 | int i = 0; |
| 303 | for(; i < m_contactList.size(); i++) |
| 304 | { |
| 305 | if(alias == m_contactList[i]->getAlias()) |
| 306 | break; |
| 307 | } |
| 308 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 309 | m_currentSelectedContact = m_contactList[i]; |
| 310 | QString name = QString::fromUtf8(m_currentSelectedContact->getName().c_str()); |
| 311 | QString institution = QString::fromUtf8(m_currentSelectedContact->getInstitution().c_str()); |
| 312 | QString nameSpace = QString::fromUtf8(m_currentSelectedContact->getNameSpace().toUri().c_str()); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 313 | ui->NameData->setText(name); |
| 314 | ui->NameSpaceData->setText(nameSpace); |
| 315 | ui->InstitutionData->setText(institution); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 316 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 317 | // m_currentSelectedContactAlias = alias; |
| 318 | // m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 319 | // _LOG_DEBUG("current Alias: " << m_currentSelectedContact); |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 320 | |
| 321 | if(m_currentSelectedContact->isIntroducer()) |
| 322 | { |
| 323 | ui->isIntroducer->setChecked(true); |
| 324 | ui->addScope->setEnabled(true); |
| 325 | ui->deleteScope->setEnabled(true); |
| 326 | Ptr<TrustedContact> trustedContact = boost::dynamic_pointer_cast<TrustedContact>(m_currentSelectedContact); |
| 327 | m_currentContactTrustScopeListModel = new QStringListModel; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | ui->isIntroducer->setChecked(false); |
| 332 | ui->addScope->setEnabled(false); |
| 333 | ui->deleteScope->setEnabled(false); |
| 334 | delete m_currentContactTrustScopeListModel; |
| 335 | } |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 336 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 337 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 338 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 339 | ContactPanel::updateDefaultIdentity(const QString& identity) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 340 | { |
| 341 | m_defaultIdentity = Name(identity.toUtf8().constData()); |
| 342 | Name prefix("/ndn/broadcast/chronos/invitation"); |
| 343 | prefix.append(m_defaultIdentity); |
| 344 | |
| 345 | _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri()); |
| 346 | |
| 347 | m_handler->clearInterestFilter(m_inviteListenPrefix); |
| 348 | m_handler->setInterestFilter(prefix, |
| 349 | boost::bind(&ContactPanel::onInvitation, |
| 350 | this, |
| 351 | _1)); |
| 352 | m_inviteListenPrefix = prefix; |
| 353 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 354 | |
| 355 | void |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 356 | ContactPanel::openProfileEditor() |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 357 | { m_profileEditor->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 358 | |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 359 | void |
| 360 | ContactPanel::openAddContactPanel() |
| 361 | { m_addContactPanel->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 362 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 363 | void |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 364 | ContactPanel::openInvitationDialog() |
| 365 | { m_invitationDialog->show(); } |
| 366 | |
| 367 | void |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 368 | ContactPanel::refreshContactList() |
| 369 | { |
| 370 | m_contactList = m_contactManager->getContactItemList(); |
| 371 | QStringList contactNameList; |
| 372 | for(int i = 0; i < m_contactList.size(); i++) |
| 373 | contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str()); |
| 374 | |
| 375 | m_contactListModel->setStringList(contactNameList); |
| 376 | } |
| 377 | |
| 378 | void |
| 379 | ContactPanel::showContextMenu(const QPoint& pos) |
| 380 | { |
| 381 | QMenu menu(ui->ContactList); |
| 382 | menu.addAction(m_menuInvite); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 383 | connect(m_menuInvite, SIGNAL(triggered()), |
| 384 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 385 | menu.addAction(m_menuAlias); |
| 386 | connect(m_menuAlias, SIGNAL(triggered()), |
| 387 | this, SLOT(openSetAliasDialog())); |
| 388 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 389 | |
| 390 | } |
| 391 | |
| 392 | void |
| 393 | ContactPanel::openSetAliasDialog() |
| 394 | { |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 395 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri()); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 396 | m_setAliasDialog->show(); |
| 397 | } |
| 398 | |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 399 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 400 | ContactPanel::openSettingDialog() |
| 401 | { |
| 402 | m_settingDialog->setIdentity(m_defaultIdentity.toUri()); |
| 403 | m_settingDialog->show(); |
| 404 | } |
| 405 | |
| 406 | void |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 407 | ContactPanel::openStartChatDialog() |
| 408 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 409 | // TimeInterval ti = time::NowUnixTimestamp(); |
| 410 | // ostringstream oss; |
| 411 | // oss << ti.total_seconds(); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 412 | |
| 413 | Name chatroom("/ndn/broadcast/chronos"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 414 | chatroom.append(string("chatroom-") + getRandomString()); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 415 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 416 | m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri()); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 417 | m_startChatDialog->show(); |
| 418 | } |
| 419 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 420 | // For inviter |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 421 | void |
| 422 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 423 | { |
| 424 | _LOG_DEBUG("room: " << chatroom.toUtf8().constData()); |
| 425 | _LOG_DEBUG("invitee: " << invitee.toUtf8().constData()); |
| 426 | _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 427 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 428 | Name chatroomName(chatroom.toUtf8().constData()); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 429 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 430 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 431 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 432 | |
| 433 | //TODO: send invitation |
| 434 | Name inviteeNamespace(invitee.toUtf8().constData()); |
| 435 | Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 436 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 437 | chatDialog->sendInvitation(inviteeItem, isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 438 | |
| 439 | chatDialog->show(); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 442 | // For Invitee |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 443 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 444 | ContactPanel::startChatroom2(const ChronosInvitation& invitation, |
| 445 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 446 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 447 | _LOG_DEBUG("room: " << invitation.getChatroom().toUri()); |
| 448 | _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri()); |
| 449 | |
| 450 | Name chatroomName("/ndn/broadcast/chronos"); |
| 451 | chatroomName.append(invitation.getChatroom()); |
| 452 | |
| 453 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity); |
| 454 | chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true); |
| 455 | |
| 456 | Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace()); |
| 457 | chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate()); |
| 458 | |
| 459 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 460 | |
| 461 | chatDialog->show(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 465 | ContactPanel::acceptInvitation(const ChronosInvitation& invitation, |
| 466 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 467 | { |
| 468 | string prefix = m_localPrefix.toUri(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 469 | |
| 470 | m_handler->publishDataByIdentity (invitation.getInterestName(), prefix); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 471 | //TODO:: open chat dialog |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 472 | _LOG_DEBUG("TO open chat dialog"); |
| 473 | startChatroom2(invitation, identityCertificate); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 477 | ContactPanel::rejectInvitation(const ChronosInvitation& invitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 478 | { |
| 479 | string empty; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 480 | m_handler->publishDataByIdentity (invitation.getInterestName(), empty); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Yingdi Yu | a50c325 | 2013-11-03 15:05:26 -0800 | [diff] [blame^] | 483 | void |
| 484 | ContactPanel::isIntroducerChanged(int state) |
| 485 | { |
| 486 | if(state == Qt::Checked) |
| 487 | { |
| 488 | ui->addScope->setEnabled(true); |
| 489 | ui->deleteScope->setEnabled(true); |
| 490 | Ptr<TrustedContact> trustedContact = boost::dynamic_pointer_cast<TrustedContact>(m_currentSelectedContact); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | ui->isIntroducer->setChecked(false); |
| 495 | ui->addScope->setEnabled(false); |
| 496 | ui->deleteScope->setEnabled(false); |
| 497 | } |
| 498 | } |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 499 | |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 500 | #if WAF |
| 501 | #include "contactpanel.moc" |
| 502 | #include "contactpanel.cpp.moc" |
| 503 | #endif |