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