Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "contactpanel.h" |
| 12 | #include "ui_contactpanel.h" |
| 13 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 14 | |
| 15 | #include <QStringList> |
| 16 | #include <QItemSelectionModel> |
| 17 | #include <QModelIndex> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 18 | #include <QDir> |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 20 | #ifndef Q_MOC_RUN |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 21 | #include <ndn.cxx/security/keychain.h> |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 22 | #include <ndn.cxx/security/identity/identity-manager.h> |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 23 | #include <ndn.cxx/common.h> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 24 | #include <boost/filesystem.hpp> |
Yingdi Yu | 978b3ae | 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 | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 27 | #include "panel-policy-manager.h" |
Yingdi Yu | 3b318c1 | 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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 34 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 35 | using namespace std; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 36 | |
| 37 | INIT_LOGGER("ContactPanel"); |
| 38 | |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 39 | Q_DECLARE_METATYPE(ndn::security::IdentityCertificate) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 40 | Q_DECLARE_METATYPE(ChronosInvitation) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 42 | ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) |
Yingdi Yu | ec5e72a | 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 | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 51 | { |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 52 | qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 53 | qRegisterMetaType<ChronosInvitation>("ChronosInvitation"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 54 | |
Yingdi Yu | 7989eb2 | 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 | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 62 | ui->setupUi(this); |
| 63 | refreshContactList(); |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 64 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 65 | setKeychain(); |
| 66 | m_handler = Ptr<Wrapper>(new Wrapper(m_keychain)); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 68 | setLocalPrefix(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | eda39aa | 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 | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 78 | |
Yingdi Yu | eda39aa | 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 | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | eda39aa | 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 | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 84 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 85 | connect(ui->EditProfileButton, SIGNAL(clicked()), |
| 86 | this, SLOT(openProfileEditor())); |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 87 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 88 | connect(ui->AddContactButton, SIGNAL(clicked()), |
| 89 | this, SLOT(openAddContactPanel())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 91 | connect(ui->settingButton, SIGNAL(clicked()), |
| 92 | this, SLOT(openSettingDialog())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 94 | connect(m_addContactPanel, SIGNAL(newContactAdded()), |
| 95 | this, SLOT(refreshContactList())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 96 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 97 | connect(m_setAliasDialog, SIGNAL(aliasChanged()), |
| 98 | this, SLOT(refreshContactList())); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 99 | |
Yingdi Yu | eda39aa | 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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 102 | |
Yingdi Yu | 7989eb2 | 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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | eda39aa | 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 | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 114 | connect(ui->isIntroducer, SIGNAL(stateChanged(int)), |
| 115 | this, SLOT(isIntroducerChanged(int))); |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ContactPanel::~ContactPanel() |
| 119 | { |
Yingdi Yu | eda39aa | 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 | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 124 | if(NULL != m_currentContactTrustScopeListModel) |
| 125 | delete m_currentContactTrustScopeListModel; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 126 | |
Yingdi Yu | eda39aa | 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 | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 134 | void |
Yingdi Yu | 978b3ae | 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 | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 142 | _LOG_DEBUG("db opened: " << std::boolalpha << ok ); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
| 146 | ContactPanel::setKeychain() |
| 147 | { |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 148 | m_panelPolicyManager = Ptr<PanelPolicyManager>::Create(); |
Yingdi Yu | 7989eb2 | 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 | 978b3ae | 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 | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 153 | m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate()); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | 9b34b1f | 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 | 978b3ae | 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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 186 | m_localPrefix = Name(prefix); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 187 | m_localPrefix.append(randomSuffix); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest) |
Yingdi Yu | 7989eb2 | 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 | 978b3ae | 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 | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 208 | Ptr<ChronosInvitation> invitation) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 209 | { |
| 210 | Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data)); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 211 | |
| 212 | if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo())) |
Yingdi Yu | 978b3ae | 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 | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 216 | popChatInvitation(invitation, inviterNameSpace, certificate); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 221 | ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation, |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 222 | const Name& inviterNameSpace, |
| 223 | Ptr<security::IdentityCertificate> certificate) |
| 224 | { |
Yingdi Yu | 7989eb2 | 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 | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 235 | return; |
| 236 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 237 | m_invitationDialog->setInvitation(alias, invitation, certificate); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 238 | emit newInvitationReady(); |
Yingdi Yu | 978b3ae | 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 | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 262 | _LOG_DEBUG("prefix: " << prefix.toUri()); |
| 263 | m_inviteListenPrefix = prefix; |
Yingdi Yu | 978b3ae | 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 | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 274 | _LOG_DEBUG("receive interest!" << interest->getName().toUri()); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 275 | const Name& interestName = interest->getName(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 276 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 277 | Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName)); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 278 | |
Yingdi Yu | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 279 | Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName())); |
Yingdi Yu | 978b3ae | 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 | 9b34b1f | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 283 | invitation), |
Yingdi Yu | 978b3ae | 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 | 40eca75 | 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 | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 300 | string alias = text.toUtf8().constData(); |
| 301 | |
Yingdi Yu | 2ac40fb | 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 | ec5e72a | 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 | 2ac40fb | 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 | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 316 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 317 | // m_currentSelectedContactAlias = alias; |
| 318 | // m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 319 | // _LOG_DEBUG("current Alias: " << m_currentSelectedContact); |
Yingdi Yu | ec5e72a | 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); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 326 | m_currentContactTrustScopeListModel = new QStringListModel; |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | ui->isIntroducer->setChecked(false); |
| 331 | ui->addScope->setEnabled(false); |
| 332 | ui->deleteScope->setEnabled(false); |
| 333 | delete m_currentContactTrustScopeListModel; |
| 334 | } |
Yingdi Yu | 40eca75 | 2013-10-10 15:00:58 -0700 | [diff] [blame] | 335 | } |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 336 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 337 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 338 | ContactPanel::updateDefaultIdentity(const QString& identity) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 339 | { |
| 340 | m_defaultIdentity = Name(identity.toUtf8().constData()); |
| 341 | Name prefix("/ndn/broadcast/chronos/invitation"); |
| 342 | prefix.append(m_defaultIdentity); |
| 343 | |
| 344 | _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri()); |
| 345 | |
| 346 | m_handler->clearInterestFilter(m_inviteListenPrefix); |
| 347 | m_handler->setInterestFilter(prefix, |
| 348 | boost::bind(&ContactPanel::onInvitation, |
| 349 | this, |
| 350 | _1)); |
| 351 | m_inviteListenPrefix = prefix; |
| 352 | } |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 353 | |
| 354 | void |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 355 | ContactPanel::openProfileEditor() |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 356 | { m_profileEditor->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 357 | |
Yingdi Yu | 0269c87 | 2013-10-16 13:16:49 -0700 | [diff] [blame] | 358 | void |
| 359 | ContactPanel::openAddContactPanel() |
| 360 | { m_addContactPanel->show(); } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 361 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 362 | void |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 363 | ContactPanel::openInvitationDialog() |
| 364 | { m_invitationDialog->show(); } |
| 365 | |
| 366 | void |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 367 | ContactPanel::refreshContactList() |
| 368 | { |
| 369 | m_contactList = m_contactManager->getContactItemList(); |
| 370 | QStringList contactNameList; |
| 371 | for(int i = 0; i < m_contactList.size(); i++) |
| 372 | contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str()); |
| 373 | |
| 374 | m_contactListModel->setStringList(contactNameList); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | ContactPanel::showContextMenu(const QPoint& pos) |
| 379 | { |
| 380 | QMenu menu(ui->ContactList); |
| 381 | menu.addAction(m_menuInvite); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 382 | connect(m_menuInvite, SIGNAL(triggered()), |
| 383 | this, SLOT(openStartChatDialog())); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 384 | menu.addAction(m_menuAlias); |
| 385 | connect(m_menuAlias, SIGNAL(triggered()), |
| 386 | this, SLOT(openSetAliasDialog())); |
| 387 | menu.exec(ui->ContactList->mapToGlobal(pos)); |
| 388 | |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | ContactPanel::openSetAliasDialog() |
| 393 | { |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 394 | m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri()); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 395 | m_setAliasDialog->show(); |
| 396 | } |
| 397 | |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 398 | void |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 399 | ContactPanel::openSettingDialog() |
| 400 | { |
| 401 | m_settingDialog->setIdentity(m_defaultIdentity.toUri()); |
| 402 | m_settingDialog->show(); |
| 403 | } |
| 404 | |
| 405 | void |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 406 | ContactPanel::openStartChatDialog() |
| 407 | { |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 408 | // TimeInterval ti = time::NowUnixTimestamp(); |
| 409 | // ostringstream oss; |
| 410 | // oss << ti.total_seconds(); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 411 | |
| 412 | Name chatroom("/ndn/broadcast/chronos"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 413 | chatroom.append(string("chatroom-") + getRandomString()); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 414 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 415 | m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri()); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 416 | m_startChatDialog->show(); |
| 417 | } |
| 418 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 419 | // For inviter |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 420 | void |
| 421 | ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer) |
| 422 | { |
| 423 | _LOG_DEBUG("room: " << chatroom.toUtf8().constData()); |
| 424 | _LOG_DEBUG("invitee: " << invitee.toUtf8().constData()); |
| 425 | _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 426 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 427 | Name chatroomName(chatroom.toUtf8().constData()); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 428 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 429 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 430 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 431 | |
| 432 | //TODO: send invitation |
| 433 | Name inviteeNamespace(invitee.toUtf8().constData()); |
| 434 | Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 435 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 436 | chatDialog->sendInvitation(inviteeItem, isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 437 | |
| 438 | chatDialog->show(); |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 441 | // For Invitee |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 442 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 443 | ContactPanel::startChatroom2(const ChronosInvitation& invitation, |
| 444 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 445 | { |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 446 | _LOG_DEBUG("room: " << invitation.getChatroom().toUri()); |
| 447 | _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri()); |
| 448 | |
| 449 | Name chatroomName("/ndn/broadcast/chronos"); |
| 450 | chatroomName.append(invitation.getChatroom()); |
| 451 | |
| 452 | ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity); |
| 453 | chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true); |
| 454 | |
| 455 | Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace()); |
| 456 | chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate()); |
| 457 | |
| 458 | m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog)); |
| 459 | |
| 460 | chatDialog->show(); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 464 | ContactPanel::acceptInvitation(const ChronosInvitation& invitation, |
| 465 | const security::IdentityCertificate& identityCertificate) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 466 | { |
| 467 | string prefix = m_localPrefix.toUri(); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 468 | |
| 469 | m_handler->publishDataByIdentity (invitation.getInterestName(), prefix); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 470 | //TODO:: open chat dialog |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 471 | _LOG_DEBUG("TO open chat dialog"); |
| 472 | startChatroom2(invitation, identityCertificate); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 476 | ContactPanel::rejectInvitation(const ChronosInvitation& invitation) |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 477 | { |
| 478 | string empty; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 479 | m_handler->publishDataByIdentity (invitation.getInterestName(), empty); |
Yingdi Yu | 978b3ae | 2013-10-23 11:50:51 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 482 | void |
| 483 | ContactPanel::isIntroducerChanged(int state) |
| 484 | { |
| 485 | if(state == Qt::Checked) |
| 486 | { |
| 487 | ui->addScope->setEnabled(true); |
| 488 | ui->deleteScope->setEnabled(true); |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | { |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 492 | ui->addScope->setEnabled(false); |
| 493 | ui->deleteScope->setEnabled(false); |
| 494 | } |
| 495 | } |
Yingdi Yu | 595aadc | 2013-10-21 15:01:40 -0700 | [diff] [blame] | 496 | |
Yingdi Yu | 847aa86 | 2013-10-09 16:35:53 -0700 | [diff] [blame] | 497 | #if WAF |
| 498 | #include "contactpanel.moc" |
| 499 | #include "contactpanel.cpp.moc" |
| 500 | #endif |