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 | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 43 | : QDialog(parent) |
| 44 | , ui(new Ui::ContactPanel) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 45 | , m_contactListModel(new QStringListModel) |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 46 | , m_startChatDialog(new StartChatDialog) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 47 | , m_invitationDialog(new InvitationDialog) |
| 48 | , m_settingDialog(new SettingDialog) |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 49 | , m_menuInvite(new QAction("&Chat", this)) |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 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 | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 114 | |
| 115 | |
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 | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 124 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 125 | delete m_menuInvite; |
| 126 | |
| 127 | map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin(); |
| 128 | for(; it != m_chatDialogs.end(); it++) |
| 129 | delete it->second; |
Yingdi Yu | def9061 | 2013-10-09 22:34:42 -0700 | [diff] [blame] | 130 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 132 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 133 | ContactPanel::openDB() |
| 134 | { |
| 135 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
| 136 | QString path = (QDir::home().path()); |
| 137 | path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db"); |
| 138 | db.setDatabaseName(path); |
| 139 | bool ok = db.open(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 140 | _LOG_DEBUG("db opened: " << std::boolalpha << ok ); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | ContactPanel::setKeychain() |
| 145 | { |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 146 | m_panelPolicyManager = Ptr<PanelPolicyManager>::Create(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 147 | // 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] | 148 | |
| 149 | vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin(); |
| 150 | for(; it != m_contactList.end(); it++) |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 151 | m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate()); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 152 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 153 | m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), |
| 154 | m_panelPolicyManager, |
| 155 | NULL)); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void |
| 159 | ContactPanel::setLocalPrefix() |
| 160 | { |
| 161 | Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix"))); |
| 162 | interest->setChildSelector(Interest::CHILD_RIGHT); |
| 163 | |
| 164 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified, |
| 165 | this, |
| 166 | _1), |
| 167 | boost::bind(&ContactPanel::onLocalPrefixTimeout, |
| 168 | this, |
| 169 | _1, |
| 170 | _2), |
| 171 | boost::bind(&ContactPanel::onLocalPrefixVerified, |
| 172 | this, |
| 173 | _1))); |
| 174 | |
| 175 | m_handler->sendInterest(interest, closure); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | ContactPanel::onLocalPrefixVerified(Ptr<Data> data) |
| 180 | { |
| 181 | string originPrefix(data->content().buf(), data->content().size()); |
| 182 | string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData(); |
| 183 | string randomSuffix = getRandomString(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 184 | m_localPrefix = Name(prefix); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 185 | m_localPrefix.append(randomSuffix); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void |
| 189 | ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 190 | { |
| 191 | string randomSuffix = getRandomString(); |
| 192 | m_localPrefix = Name("/private/local"); |
| 193 | m_localPrefix.append(randomSuffix); |
| 194 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 195 | |
| 196 | void |
| 197 | ContactPanel::onUnverified(Ptr<Data> data) |
| 198 | {} |
| 199 | |
| 200 | void |
| 201 | ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
| 202 | {} |
| 203 | |
| 204 | void |
| 205 | ContactPanel::onInvitationCertVerified(Ptr<Data> data, |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 206 | Ptr<ChronosInvitation> invitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 207 | { |
| 208 | Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data)); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 209 | |
| 210 | if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo())) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 211 | { |
| 212 | Name keyName = certificate->getPublicKeyName(); |
| 213 | Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 214 | popChatInvitation(invitation, inviterNameSpace, certificate); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 219 | ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation, |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 220 | const Name& inviterNameSpace, |
| 221 | Ptr<security::IdentityCertificate> certificate) |
| 222 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 223 | string chatroom = invitation->getChatroom().get(0).toUri(); |
| 224 | string inviter = inviterNameSpace.toUri(); |
| 225 | |
| 226 | string alias; |
| 227 | vector<Ptr<ContactItem> >::iterator it = m_contactList.begin(); |
| 228 | for(; it != m_contactList.end(); it++) |
| 229 | if((*it)->getNameSpace() == inviterNameSpace) |
| 230 | alias = (*it)->getAlias(); |
| 231 | |
| 232 | if(it != m_contactList.end()) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 233 | return; |
| 234 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 235 | m_invitationDialog->setInvitation(alias, invitation, certificate); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 236 | emit newInvitationReady(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 240 | |
| 241 | string |
| 242 | ContactPanel::getRandomString() |
| 243 | { |
| 244 | string randStr; |
| 245 | boost::random::random_device rng; |
| 246 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 247 | for (int i = 0; i < 10; i ++) |
| 248 | { |
| 249 | randStr += chars[index_dist(rng)]; |
| 250 | } |
| 251 | return randStr; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void |
| 256 | ContactPanel::setInvitationListener() |
| 257 | { |
| 258 | Name prefix("/ndn/broadcast/chronos/invitation"); |
| 259 | prefix.append(m_defaultIdentity); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 260 | _LOG_DEBUG("prefix: " << prefix.toUri()); |
| 261 | m_inviteListenPrefix = prefix; |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 262 | m_handler->setInterestFilter (prefix, |
| 263 | boost::bind(&ContactPanel::onInvitation, |
| 264 | this, |
| 265 | _1)); |
| 266 | |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | ContactPanel::onInvitation(Ptr<Interest> interest) |
| 271 | { |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 272 | _LOG_DEBUG("receive interest!" << interest->getName().toUri()); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 273 | const Name& interestName = interest->getName(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 274 | |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 275 | Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName)); |
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<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName())); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 278 | Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified, |
| 279 | this, |
| 280 | _1, |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame^] | 281 | invitation), |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 282 | boost::bind(&ContactPanel::onTimeout, |
| 283 | this, |
| 284 | _1, |
| 285 | _2), |
| 286 | boost::bind(&ContactPanel::onUnverified, |
| 287 | this, |
| 288 | _1))); |
| 289 | m_handler->sendInterest(newInterest, closure); |
| 290 | } |
| 291 | |
| 292 | void |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 293 | ContactPanel::updateSelection(const QItemSelection &selected, |
| 294 | const QItemSelection &deselected) |
| 295 | { |
| 296 | QModelIndexList items = selected.indexes(); |
| 297 | QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 298 | string alias = text.toUtf8().constData(); |
| 299 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 300 | int i = 0; |
| 301 | for(; i < m_contactList.size(); i++) |
| 302 | { |
| 303 | if(alias == m_contactList[i]->getAlias()) |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | QString name = QString::fromUtf8(m_contactList[i]->getName().c_str()); |
| 308 | QString institution = QString::fromUtf8(m_contactList[i]->getInstitution().c_str()); |
| 309 | QString nameSpace = QString::fromUtf8(m_contactList[i]->getNameSpace().toUri().c_str()); |
| 310 | ui->NameData->setText(name); |
| 311 | ui->NameSpaceData->setText(nameSpace); |
| 312 | ui->InstitutionData->setText(institution); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 313 | |
| 314 | m_currentSelectedContactAlias = alias; |
| 315 | m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 316 | // _LOG_DEBUG("current Alias: " << m_currentSelectedContact); |
Yingdi Yu | 01a942b | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 317 | } |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 318 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 319 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 320 | ContactPanel::updateDefaultIdentity(const QString& identity) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 321 | { |
| 322 | m_defaultIdentity = Name(identity.toUtf8().constData()); |
| 323 | Name prefix("/ndn/broadcast/chronos/invitation"); |
| 324 | prefix.append(m_defaultIdentity); |
| 325 | |
| 326 | _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri()); |
| 327 | |
| 328 | m_handler->clearInterestFilter(m_inviteListenPrefix); |
| 329 | m_handler->setInterestFilter(prefix, |
| 330 | boost::bind(&ContactPanel::onInvitation, |
| 331 | this, |
| 332 | _1)); |
| 333 | m_inviteListenPrefix = prefix; |
| 334 | } |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 335 | |
| 336 | void |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 337 | ContactPanel::openProfileEditor() |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 338 | { m_profileEditor->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 339 | |
Yingdi Yu | d59b593 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 340 | void |
| 341 | ContactPanel::openAddContactPanel() |
| 342 | { m_addContactPanel->show(); } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 343 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 344 | void |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 345 | ContactPanel::openInvitationDialog() |
| 346 | { m_invitationDialog->show(); } |
| 347 | |
| 348 | void |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 349 | ContactPanel::refreshContactList() |
| 350 | { |
| 351 | m_contactList = m_contactManager->getContactItemList(); |
| 352 | QStringList contactNameList; |
| 353 | for(int i = 0; i < m_contactList.size(); i++) |
| 354 | contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str()); |
| 355 | |
| 356 | m_contactListModel->setStringList(contactNameList); |
| 357 | } |
| 358 | |
| 359 | void |
| 360 | ContactPanel::showContextMenu(const QPoint& pos) |
| 361 | { |
| 362 | QMenu menu(ui->ContactList); |
| 363 | menu.addAction(m_menuInvite); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 364 | connect(m_menuInvite, SIGNAL(triggered()), |
| 365 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 366 | menu.addAction(m_menuAlias); |
| 367 | connect(m_menuAlias, SIGNAL(triggered()), |
| 368 | this, SLOT(openSetAliasDialog())); |
| 369 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 370 | |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | ContactPanel::openSetAliasDialog() |
| 375 | { |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 376 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContactNamespace); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 377 | m_setAliasDialog->show(); |
| 378 | } |
| 379 | |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 380 | void |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 381 | ContactPanel::openSettingDialog() |
| 382 | { |
| 383 | m_settingDialog->setIdentity(m_defaultIdentity.toUri()); |
| 384 | m_settingDialog->show(); |
| 385 | } |
| 386 | |
| 387 | void |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 388 | ContactPanel::openStartChatDialog() |
| 389 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 390 | // TimeInterval ti = time::NowUnixTimestamp(); |
| 391 | // ostringstream oss; |
| 392 | // oss << ti.total_seconds(); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 393 | |
| 394 | Name chatroom("/ndn/broadcast/chronos"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 395 | chatroom.append(string("chatroom-") + getRandomString()); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 396 | |
| 397 | m_startChatDialog->setInvitee(m_currentSelectedContactNamespace, chatroom.toUri()); |
| 398 | m_startChatDialog->show(); |
| 399 | } |
| 400 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 401 | // For inviter |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 402 | void |
| 403 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 404 | { |
| 405 | _LOG_DEBUG("room: " << chatroom.toUtf8().constData()); |
| 406 | _LOG_DEBUG("invitee: " << invitee.toUtf8().constData()); |
| 407 | _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 408 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 409 | Name chatroomName(chatroom.toUtf8().constData()); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 410 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 411 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 412 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 413 | |
| 414 | //TODO: send invitation |
| 415 | Name inviteeNamespace(invitee.toUtf8().constData()); |
| 416 | Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 417 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 418 | chatDialog->sendInvitation(inviteeItem, isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 419 | |
| 420 | chatDialog->show(); |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 423 | // For Invitee |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 424 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 425 | ContactPanel::startChatroom2(const ChronosInvitation& invitation, |
| 426 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 427 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 428 | _LOG_DEBUG("room: " << invitation.getChatroom().toUri()); |
| 429 | _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri()); |
| 430 | |
| 431 | Name chatroomName("/ndn/broadcast/chronos"); |
| 432 | chatroomName.append(invitation.getChatroom()); |
| 433 | |
| 434 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity); |
| 435 | chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true); |
| 436 | |
| 437 | Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace()); |
| 438 | chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate()); |
| 439 | |
| 440 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 441 | |
| 442 | chatDialog->show(); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 446 | ContactPanel::acceptInvitation(const ChronosInvitation& invitation, |
| 447 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 448 | { |
| 449 | string prefix = m_localPrefix.toUri(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 450 | |
| 451 | m_handler->publishDataByIdentity (invitation.getInterestName(), prefix); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 452 | //TODO:: open chat dialog |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 453 | _LOG_DEBUG("TO open chat dialog"); |
| 454 | startChatroom2(invitation, identityCertificate); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 458 | ContactPanel::rejectInvitation(const ChronosInvitation& invitation) |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 459 | { |
| 460 | string empty; |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 461 | m_handler->publishDataByIdentity (invitation.getInterestName(), empty); |
Yingdi Yu | 53eb8a7 | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Yingdi Yu | 100267f | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 464 | |
Yingdi Yu | 9e0dc29 | 2013-10-10 11:39:45 -0700 | [diff] [blame] | 465 | #if WAF |
| 466 | #include "contactpanel.moc" |
| 467 | #include "contactpanel.cpp.moc" |
| 468 | #endif |