Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -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 | * |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 8 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 9 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "chatdialog.h" |
| 14 | #include "ui_chatdialog.h" |
| 15 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 16 | #include <QScrollBar> |
| 17 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 18 | #ifndef Q_MOC_RUN |
| 19 | #include <ndn.cxx/security/identity/identity-manager.h> |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 20 | #include <ndn.cxx/security/encryption/basic-encryption-manager.h> |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 21 | #include <sync-intro-certificate.h> |
| 22 | #include <boost/random/random_device.hpp> |
| 23 | #include <boost/random/uniform_int_distribution.hpp> |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 24 | #include "logging.h" |
| 25 | #endif |
| 26 | |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 27 | using namespace std; |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 29 | INIT_LOGGER("ChatDialog"); |
| 30 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 31 | static const int HELLO_INTERVAL = FRESHNESS * 3 / 4; |
| 32 | |
| 33 | Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> ) |
| 34 | Q_DECLARE_METATYPE(size_t) |
| 35 | |
| 36 | ChatDialog::ChatDialog(ndn::Ptr<ContactManager> contactManager, |
| 37 | const ndn::Name& chatroomPrefix, |
| 38 | const ndn::Name& localPrefix, |
| 39 | const ndn::Name& defaultIdentity, |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 40 | const std::string& nick, |
| 41 | bool trial, |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 42 | QWidget *parent) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 43 | : QDialog(parent) |
| 44 | , ui(new Ui::ChatDialog) |
| 45 | , m_contactManager(contactManager) |
| 46 | , m_chatroomPrefix(chatroomPrefix) |
| 47 | , m_localPrefix(localPrefix) |
| 48 | , m_defaultIdentity(defaultIdentity) |
| 49 | , m_invitationPolicyManager(ndn::Ptr<InvitationPolicyManager>(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toUri()))) |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 50 | , m_nick(nick) |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 51 | , m_sock(NULL) |
| 52 | , m_lastMsgTime(0) |
| 53 | // , m_historyInitialized(false) |
| 54 | , m_joined(false) |
| 55 | , m_inviteListDialog(new InviteListDialog(m_contactManager)) |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 56 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 57 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 58 | qRegisterMetaType<size_t>("size_t"); |
| 59 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 60 | ui->setupUi(this); |
| 61 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 62 | m_localChatPrefix = m_localPrefix; |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 63 | m_localChatPrefix.append("%F0.").append(m_defaultIdentity); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 64 | m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1)); |
| 65 | |
| 66 | m_session = time(NULL); |
| 67 | m_scene = new DigestTreeScene(this); |
| 68 | |
| 69 | initializeSetting(); |
| 70 | updateLabels(); |
| 71 | |
| 72 | ui->treeViewer->setScene(m_scene); |
| 73 | ui->treeViewer->hide(); |
| 74 | m_scene->plot("Empty"); |
| 75 | QRectF rect = m_scene->itemsBoundingRect(); |
| 76 | m_scene->setSceneRect(rect); |
| 77 | |
| 78 | m_rosterModel = new QStringListModel(this); |
| 79 | ui->listView->setModel(m_rosterModel); |
| 80 | |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 81 | createActions(); |
| 82 | createTrayIcon(); |
| 83 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 84 | m_timer = new QTimer(this); |
| 85 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 86 | setWrapper(trial); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 87 | |
| 88 | connect(ui->inviteButton, SIGNAL(clicked()), |
| 89 | this, SLOT(openInviteListDialog())); |
| 90 | connect(m_inviteListDialog, SIGNAL(invitionDetermined(QString, bool)), |
| 91 | this, SLOT(sendInvitationWrapper(QString, bool))); |
| 92 | connect(ui->lineEdit, SIGNAL(returnPressed()), |
| 93 | this, SLOT(returnPressed())); |
| 94 | connect(ui->treeButton, SIGNAL(pressed()), |
| 95 | this, SLOT(treeButtonPressed())); |
| 96 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), |
| 97 | this, SLOT(processData(QString, const char *, size_t, bool, bool))); |
| 98 | connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), |
| 99 | this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
| 100 | connect(m_timer, SIGNAL(timeout()), |
| 101 | this, SLOT(replot())); |
| 102 | connect(m_scene, SIGNAL(replot()), |
| 103 | this, SLOT(replot())); |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 104 | connect(trayIcon, SIGNAL(messageClicked()), |
| 105 | this, SLOT(showNormal())); |
| 106 | connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), |
| 107 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 108 | connect(m_scene, SIGNAL(rosterChanged(QStringList)), |
| 109 | this, SLOT(updateRosterList(QStringList))); |
| 110 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 111 | |
| 112 | initializeSync(); |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 115 | |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 116 | ChatDialog::~ChatDialog() |
| 117 | { |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 118 | if(m_sock != NULL) |
| 119 | { |
| 120 | sendLeave(); |
| 121 | delete m_sock; |
| 122 | m_sock = NULL; |
| 123 | } |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 124 | m_handler->shutdown(); |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 128 | ChatDialog::setWrapper(bool trial) |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 129 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 130 | m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create(); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 131 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 132 | ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity); |
| 133 | m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix)); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 134 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 135 | m_keychain = ndn::Ptr<ndn::security::Keychain>(new ndn::security::Keychain(m_identityManager, m_invitationPolicyManager, NULL)); |
| 136 | |
| 137 | m_handler = ndn::Ptr<ndn::Wrapper>(new ndn::Wrapper(m_keychain)); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 138 | |
| 139 | if(trial == true) |
| 140 | { |
| 141 | ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(ndn::Name("/local/ndn/prefix"))); |
| 142 | ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onUnverified, |
| 143 | this, |
| 144 | _1), |
| 145 | boost::bind(&ChatDialog::onTimeout, |
| 146 | this, |
| 147 | _1, |
| 148 | _2), |
| 149 | boost::bind(&ChatDialog::onUnverified, |
| 150 | this, |
| 151 | _1))); |
| 152 | |
| 153 | m_handler->sendInterest(interest, closure); |
| 154 | } |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 157 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 158 | ChatDialog::initializeSetting() |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 159 | { |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 160 | m_user.setNick(QString::fromStdString(m_nick)); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 161 | m_user.setChatroom(QString::fromStdString(m_chatroomPrefix.get(-1).toUri())); |
| 162 | m_user.setOriginPrefix(QString::fromStdString(m_localPrefix.toUri())); |
| 163 | m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri())); |
| 164 | m_scene->setCurrentPrefix(QString::fromStdString(m_localChatPrefix.toUri())); |
| 165 | } |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 166 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 167 | void |
| 168 | ChatDialog::updateLabels() |
| 169 | { |
| 170 | QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom()); |
| 171 | ui->infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}"); |
| 172 | ui->infoLabel->setText(settingDisp); |
| 173 | QString prefixDisp; |
| 174 | if (m_user.getPrefix().startsWith("/private/local")) |
| 175 | { |
| 176 | prefixDisp = QString("<Warning: no connection to hub or hub does not support prefix autoconfig.>\n <Prefix = %1>").arg(m_user.getPrefix()); |
| 177 | ui->prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}"); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix()); |
| 182 | ui->prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}"); |
| 183 | } |
| 184 | ui->prefixLabel->setText(prefixDisp); |
| 185 | } |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 186 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 187 | void |
| 188 | ChatDialog::sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer) |
| 189 | { |
| 190 | m_invitationPolicyManager->addTrustAnchor(contact->getSelfEndorseCertificate()); |
| 191 | |
| 192 | ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity); |
| 193 | |
| 194 | ndn::Name interestName("/ndn/broadcast/chronos/invitation"); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 195 | interestName.append(contact->getNameSpace()); |
| 196 | interestName.append("chatroom"); |
| 197 | interestName.append(m_chatroomPrefix.get(-1)); |
| 198 | interestName.append("inviter-prefix"); |
| 199 | interestName.append(m_localPrefix); |
| 200 | interestName.append("inviter"); |
| 201 | interestName.append(certificateName); |
| 202 | |
| 203 | string signedUri = interestName.toUri(); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 204 | ndn::Blob signedBlob(signedUri.c_str(), signedUri.size()); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 205 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 206 | ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = ndn::DynamicCast<const ndn::signature::Sha256WithRsa>(m_identityManager->signByCertificate(signedBlob, certificateName)); |
| 207 | const ndn::Blob& sigBits = sha256sig->getSignatureBits(); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 208 | |
| 209 | interestName.append(sigBits.buf(), sigBits.size()); |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 210 | interestName.appendVersion(); |
| 211 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 212 | ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(interestName)); |
| 213 | ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onInviteReplyVerified, |
| 214 | this, |
| 215 | _1, |
| 216 | contact->getNameSpace(), |
| 217 | isIntroducer), |
| 218 | boost::bind(&ChatDialog::onInviteTimeout, |
| 219 | this, |
| 220 | _1, |
| 221 | _2, |
| 222 | contact->getNameSpace(), |
| 223 | 7), |
| 224 | boost::bind(&ChatDialog::onUnverified, |
| 225 | this, |
| 226 | _1))); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 227 | |
| 228 | m_handler->sendInterest(interest, closure); |
| 229 | } |
| 230 | |
| 231 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 232 | ChatDialog::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate) |
| 233 | { m_invitationPolicyManager->addTrustAnchor(selfEndorseCertificate); } |
| 234 | |
| 235 | void |
| 236 | ChatDialog::addChatDataRule(const ndn::Name& prefix, |
| 237 | const ndn::security::IdentityCertificate& identityCertificate, |
| 238 | bool isIntroducer) |
| 239 | { m_syncPolicyManager->addChatDataRule(prefix, identityCertificate, isIntroducer); } |
| 240 | |
| 241 | void |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 242 | ChatDialog::publishIntroCert(const ndn::security::IdentityCertificate& dskCertificate, bool isIntroducer) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 243 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 244 | SyncIntroCertificate syncIntroCertificate(m_chatroomPrefix, |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 245 | dskCertificate.getPublicKeyName(), |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 246 | m_identityManager->getDefaultKeyNameForIdentity(m_defaultIdentity), |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 247 | dskCertificate.getNotBefore(), |
| 248 | dskCertificate.getNotAfter(), |
| 249 | dskCertificate.getPublicKeyInfo(), |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 250 | (isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER)); |
| 251 | ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity); |
Yingdi Yu | ed8cfc4 | 2013-11-01 17:37:51 -0700 | [diff] [blame] | 252 | _LOG_DEBUG("publishIntroCert: " << syncIntroCertificate.getName()); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 253 | m_identityManager->signByCertificate(syncIntroCertificate, certName); |
| 254 | m_handler->putToNdnd(*syncIntroCertificate.encodeToWire()); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 258 | ChatDialog::invitationRejected(const ndn::Name& identity) |
| 259 | { |
| 260 | _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!"); |
| 261 | //TODO: |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::Ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 266 | { |
| 267 | _LOG_DEBUG(" " << identity.toUri() << " accepted your invitation!"); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 268 | ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const ndn::signature::Sha256WithRsa> (data->getSignature()); |
| 269 | const ndn::Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName(); |
| 270 | ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate = m_invitationPolicyManager->getValidatedDskCertificate(keyLocatorName); |
Yingdi Yu | c90deb1 | 2013-11-06 18:51:19 -0800 | [diff] [blame] | 271 | m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer); |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 272 | publishIntroCert(*dskCertificate, isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 276 | ChatDialog::onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 277 | { |
| 278 | string content(data->content().buf(), data->content().size()); |
| 279 | if(content.empty()) |
| 280 | invitationRejected(identity); |
| 281 | else |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 282 | invitationAccepted(identity, data, content, isIntroducer); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 286 | ChatDialog::onInviteTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest, const ndn::Name& identity, int retry) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 287 | { |
| 288 | if(retry > 0) |
| 289 | { |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 290 | ndn::Ptr<ndn::Closure> newClosure = ndn::Ptr<ndn::Closure>(new ndn::Closure(closure->m_dataCallback, |
| 291 | boost::bind(&ChatDialog::onInviteTimeout, |
| 292 | this, |
| 293 | _1, |
| 294 | _2, |
| 295 | identity, |
| 296 | retry - 1), |
| 297 | closure->m_unverifiedCallback, |
| 298 | closure->m_stepCount) |
| 299 | ); |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 300 | m_handler->sendInterest(interest, newClosure); |
| 301 | } |
| 302 | else |
| 303 | invitationRejected(identity); |
| 304 | } |
| 305 | |
| 306 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 307 | ChatDialog::onUnverified(ndn::Ptr<ndn::Data> data) |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 308 | {} |
| 309 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 310 | void |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 311 | ChatDialog::onTimeout(ndn::Ptr<ndn::Closure> closure, |
| 312 | ndn::Ptr<ndn::Interest> interest) |
| 313 | {} |
| 314 | |
| 315 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 316 | ChatDialog::initializeSync() |
| 317 | { |
| 318 | |
| 319 | m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(), |
| 320 | m_syncPolicyManager, |
| 321 | bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), |
| 322 | bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 323 | |
| 324 | usleep(100000); |
| 325 | |
| 326 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
| 327 | m_timer->start(FRESHNESS * 1000); |
| 328 | disableTreeDisplay(); |
| 329 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
| 330 | // Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> (); |
| 331 | // handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1)); |
| 332 | // _LOG_DEBUG("initializeSync is done!"); |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | ChatDialog::returnPressed() |
| 337 | { |
| 338 | QString text = ui->lineEdit->text(); |
| 339 | if (text.isEmpty()) |
| 340 | return; |
| 341 | |
| 342 | ui->lineEdit->clear(); |
| 343 | |
| 344 | if (text.startsWith("boruoboluomi")) |
| 345 | { |
| 346 | summonReaper (); |
| 347 | // reapButton->show(); |
| 348 | fitView(); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | if (text.startsWith("minimanihong")) |
| 353 | { |
| 354 | // reapButton->hide(); |
| 355 | fitView(); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | SyncDemo::ChatMessage msg; |
| 360 | formChatMessage(text, msg); |
| 361 | |
| 362 | appendMessage(msg); |
| 363 | |
| 364 | sendMsg(msg); |
| 365 | |
| 366 | fitView(); |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | ChatDialog::treeButtonPressed() |
| 371 | { |
| 372 | if (ui->treeViewer->isVisible()) |
| 373 | { |
| 374 | ui->treeViewer->hide(); |
| 375 | ui->treeButton->setText("Show ChronoSync Tree"); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | ui->treeViewer->show(); |
| 380 | ui->treeButton->setText("Hide ChronoSync Tree"); |
| 381 | } |
| 382 | |
| 383 | fitView(); |
| 384 | } |
| 385 | |
| 386 | void ChatDialog::disableTreeDisplay() |
| 387 | { |
| 388 | ui->treeButton->setEnabled(false); |
| 389 | ui->treeViewer->hide(); |
| 390 | fitView(); |
| 391 | } |
| 392 | |
| 393 | void ChatDialog::enableTreeDisplay() |
| 394 | { |
| 395 | ui->treeButton->setEnabled(true); |
| 396 | // treeViewer->show(); |
| 397 | // fitView(); |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncSocket *sock) |
| 402 | { |
| 403 | emit treeUpdated(v); |
| 404 | _LOG_DEBUG("<<< Tree update signal emitted"); |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | ChatDialog::processRemoveWrapper(std::string prefix) |
| 409 | { |
| 410 | _LOG_DEBUG("Sync REMOVE signal received for prefix: " << prefix); |
| 411 | } |
| 412 | |
| 413 | void |
| 414 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
| 415 | { |
| 416 | _LOG_DEBUG("<<< processing Tree Update"); |
| 417 | |
| 418 | if (v.empty()) |
| 419 | { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | // reflect the changes on digest tree |
| 424 | { |
| 425 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 426 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 427 | } |
| 428 | |
| 429 | int n = v.size(); |
| 430 | int totalMissingPackets = 0; |
| 431 | for (int i = 0; i < n; i++) |
| 432 | { |
| 433 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 434 | } |
| 435 | |
| 436 | for (int i = 0; i < n; i++) |
| 437 | { |
| 438 | if (totalMissingPackets < 4) |
| 439 | { |
| 440 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 441 | { |
| 442 | m_sock->fetchData(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1), 2); |
| 443 | _LOG_DEBUG("<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq()); |
| 444 | } |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | m_sock->fetchData(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1), 2); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // adjust the view |
| 453 | fitView(); |
| 454 | |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | ChatDialog::processDataWrapper(ndn::Ptr<ndn::Data> data) |
| 459 | { |
| 460 | string name = data->getName().toUri(); |
| 461 | const char* buf = data->content().buf(); |
| 462 | size_t len = data->content().size(); |
| 463 | |
| 464 | char *tempBuf = new char[len]; |
| 465 | memcpy(tempBuf, buf, len); |
| 466 | emit dataReceived(name.c_str(), tempBuf, len, true, false); |
| 467 | _LOG_DEBUG("<<< " << name << " fetched"); |
| 468 | } |
| 469 | |
| 470 | void |
| 471 | ChatDialog::processDataNoShowWrapper(ndn::Ptr<ndn::Data> data) |
| 472 | { |
| 473 | string name = data->getName().toUri(); |
| 474 | const char* buf = data->content().buf(); |
| 475 | size_t len = data->content().size(); |
| 476 | |
| 477 | char *tempBuf = new char[len]; |
| 478 | memcpy(tempBuf, buf, len); |
| 479 | emit dataReceived(name.c_str(), tempBuf, len, false, false); |
| 480 | |
| 481 | // if (!m_historyInitialized) |
| 482 | // { |
| 483 | // fetchHistory(name); |
| 484 | // m_historyInitialized = true; |
| 485 | // } |
| 486 | } |
| 487 | |
| 488 | // void |
| 489 | // ChatDialog::fetchHistory(std::string name) |
| 490 | // { |
| 491 | |
| 492 | // /****************************/ |
| 493 | // /* TODO: fix following part */ |
| 494 | // /****************************/ |
| 495 | // string nameWithoutSeq = name.substr(0, name.find_last_of('/')); |
| 496 | // string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/')); |
| 497 | // prefix += "/history"; |
| 498 | // // Ptr<Wrapper>CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> (); |
| 499 | // // QString randomString = getRandomString(); |
| 500 | // // for (int i = 0; i < MAX_HISTORY_ENTRY; i++) |
| 501 | // // { |
| 502 | // // QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i); |
| 503 | // // handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3)); |
| 504 | // // } |
| 505 | // } |
| 506 | |
| 507 | void |
| 508 | ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory) |
| 509 | { |
| 510 | SyncDemo::ChatMessage msg; |
| 511 | bool corrupted = false; |
| 512 | if (!msg.ParseFromArray(buf, len)) |
| 513 | { |
| 514 | _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?"); |
| 515 | // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs |
| 516 | msg.set_from("inconnu"); |
| 517 | msg.set_type(SyncDemo::ChatMessage::OTHER); |
| 518 | corrupted = true; |
| 519 | } |
| 520 | |
| 521 | delete [] buf; |
| 522 | buf = NULL; |
| 523 | |
| 524 | // display msg received from network |
| 525 | // we have to do so; this function is called by ccnd thread |
| 526 | // so if we call appendMsg directly |
| 527 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 528 | // the "cannonical" way to is use signal-slot |
| 529 | if (show && !corrupted) |
| 530 | { |
| 531 | appendMessage(msg, isHistory); |
| 532 | } |
| 533 | |
| 534 | if (!isHistory) |
| 535 | { |
| 536 | // update the tree view |
| 537 | std::string stdStrName = name.toStdString(); |
| 538 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 539 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
| 540 | _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from()); |
| 541 | if (msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 542 | { |
| 543 | processRemove(prefix.c_str()); |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 548 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 549 | } |
| 550 | } |
| 551 | fitView(); |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | ChatDialog::processRemove(QString prefix) |
| 556 | { |
| 557 | _LOG_DEBUG("<<< remove node for prefix" << prefix.toStdString()); |
| 558 | |
| 559 | bool removed = m_scene->removeNode(prefix); |
| 560 | if (removed) |
| 561 | { |
| 562 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 563 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | ChatDialog::sendJoin() |
| 569 | { |
| 570 | m_joined = true; |
| 571 | SyncDemo::ChatMessage msg; |
| 572 | formControlMessage(msg, SyncDemo::ChatMessage::JOIN); |
| 573 | sendMsg(msg); |
| 574 | boost::random::random_device rng; |
| 575 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 576 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
| 577 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 578 | } |
| 579 | |
| 580 | void |
| 581 | ChatDialog::sendHello() |
| 582 | { |
| 583 | time_t now = time(NULL); |
| 584 | int elapsed = now - m_lastMsgTime; |
| 585 | if (elapsed >= m_randomizedInterval / 1000) |
| 586 | { |
| 587 | SyncDemo::ChatMessage msg; |
| 588 | formControlMessage(msg, SyncDemo::ChatMessage::HELLO); |
| 589 | sendMsg(msg); |
| 590 | boost::random::random_device rng; |
| 591 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 592 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
| 593 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello())); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void |
| 602 | ChatDialog::sendLeave() |
| 603 | { |
| 604 | SyncDemo::ChatMessage msg; |
| 605 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 606 | sendMsg(msg); |
| 607 | usleep(500000); |
| 608 | m_sock->remove(m_user.getPrefix().toStdString()); |
| 609 | usleep(5000); |
| 610 | m_joined = false; |
| 611 | _LOG_DEBUG("Sync REMOVE signal sent"); |
| 612 | } |
| 613 | |
| 614 | void |
| 615 | ChatDialog::replot() |
| 616 | { |
| 617 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 618 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 619 | fitView(); |
| 620 | } |
| 621 | |
| 622 | void |
| 623 | ChatDialog::summonReaper() |
| 624 | { |
| 625 | Sync::SyncLogic &logic = m_sock->getLogic (); |
| 626 | map<string, bool> branches = logic.getBranchPrefixes(); |
| 627 | QMap<QString, DisplayUserPtr> roster = m_scene->getRosterFull(); |
| 628 | |
| 629 | m_zombieList.clear(); |
| 630 | |
| 631 | QMapIterator<QString, DisplayUserPtr> it(roster); |
| 632 | map<string, bool>::iterator mapIt; |
| 633 | while(it.hasNext()) |
| 634 | { |
| 635 | it.next(); |
| 636 | DisplayUserPtr p = it.value(); |
| 637 | if (p != DisplayUserNullPtr) |
| 638 | { |
| 639 | mapIt = branches.find(p->getPrefix().toStdString()); |
| 640 | if (mapIt != branches.end()) |
| 641 | { |
| 642 | mapIt->second = true; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | for (mapIt = branches.begin(); mapIt != branches.end(); ++mapIt) |
| 648 | { |
| 649 | // this is zombie. all active users should have been marked true |
| 650 | if (! mapIt->second) |
| 651 | { |
| 652 | m_zombieList.append(mapIt->first.c_str()); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | m_zombieIndex = 0; |
| 657 | |
| 658 | // start reaping |
| 659 | reap(); |
| 660 | } |
| 661 | |
| 662 | void |
| 663 | ChatDialog::reap() |
| 664 | { |
| 665 | if (m_zombieIndex < m_zombieList.size()) |
| 666 | { |
| 667 | string prefix = m_zombieList.at(m_zombieIndex).toStdString(); |
| 668 | m_sock->remove(prefix); |
| 669 | _LOG_DEBUG("Reaped: prefix = " << prefix); |
| 670 | m_zombieIndex++; |
| 671 | // reap again in 10 seconds |
| 672 | QTimer::singleShot(10000, this, SLOT(reap())); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | void |
| 677 | ChatDialog::updateRosterList(QStringList staleUserList) |
| 678 | { |
| 679 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 680 | QStringList rosterList = m_scene->getRosterList(); |
| 681 | m_rosterModel->setStringList(rosterList); |
| 682 | QString user; |
| 683 | QStringListIterator it(staleUserList); |
| 684 | while(it.hasNext()) |
| 685 | { |
| 686 | std::string nick = it.next().toStdString(); |
| 687 | if (nick.empty()) |
| 688 | continue; |
| 689 | |
| 690 | SyncDemo::ChatMessage msg; |
| 691 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 692 | msg.set_from(nick); |
| 693 | appendMessage(msg); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 698 | ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 699 | { |
| 700 | switch (reason) |
| 701 | { |
| 702 | case QSystemTrayIcon::Trigger: |
| 703 | case QSystemTrayIcon::DoubleClick: |
| 704 | break; |
| 705 | case QSystemTrayIcon::MiddleClick: |
| 706 | // showMessage(); |
| 707 | break; |
| 708 | default:; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | |
| 713 | void |
| 714 | ChatDialog::messageClicked() |
| 715 | { |
| 716 | this->showMaximized(); |
| 717 | } |
| 718 | |
| 719 | |
| 720 | void |
| 721 | ChatDialog::createActions() |
| 722 | { |
| 723 | // minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 724 | // connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); |
| 725 | |
| 726 | // maximizeAction = new QAction(tr("Ma&ximize"), this); |
| 727 | // connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); |
| 728 | |
| 729 | // restoreAction = new QAction(tr("&Restore"), this); |
| 730 | // connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); |
| 731 | |
| 732 | // settingsAction = new QAction(tr("Settings"), this); |
| 733 | // connect (settingsAction, SIGNAL(triggered()), this, SLOT(buttonPressed())); |
| 734 | |
| 735 | // settingsAction->setMenuRole (QAction::PreferencesRole); |
| 736 | |
| 737 | // updateLocalPrefixAction = new QAction(tr("Update local prefix"), this); |
| 738 | // connect (updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(updateLocalPrefix())); |
| 739 | |
| 740 | // quitAction = new QAction(tr("Quit"), this); |
| 741 | // connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
| 742 | } |
| 743 | |
| 744 | void |
| 745 | ChatDialog::createTrayIcon() |
| 746 | { |
| 747 | // trayIconMenu = new QMenu(this); |
| 748 | // trayIconMenu->addAction(minimizeAction); |
| 749 | // trayIconMenu->addAction(maximizeAction); |
| 750 | // trayIconMenu->addAction(restoreAction); |
| 751 | // trayIconMenu->addSeparator(); |
| 752 | // trayIconMenu->addAction(settingsAction); |
| 753 | // trayIconMenu->addSeparator(); |
| 754 | // trayIconMenu->addAction(updateLocalPrefixAction); |
| 755 | // trayIconMenu->addSeparator(); |
| 756 | // trayIconMenu->addAction(quitAction); |
| 757 | |
| 758 | trayIcon = new QSystemTrayIcon(this); |
| 759 | // trayIcon->setContextMenu(trayIconMenu); |
| 760 | |
| 761 | QIcon icon(":/images/icon_small.png"); |
| 762 | trayIcon->setIcon(icon); |
| 763 | setWindowIcon(icon); |
| 764 | trayIcon->setToolTip("ChronoChat System Tray Icon"); |
| 765 | trayIcon->setVisible(true); |
| 766 | |
| 767 | // // QApplication::getMenu ()->addMenu (trayIconMenu); |
| 768 | // QMenuBar *bar = new QMenuBar (); |
| 769 | // bar->setMenu (trayIconMenu); |
| 770 | // setMenuBar (bar); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 775 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 776 | { |
| 777 | fitView(); |
| 778 | } |
| 779 | |
| 780 | void |
| 781 | ChatDialog::showEvent(QShowEvent *e) |
| 782 | { |
| 783 | fitView(); |
| 784 | } |
| 785 | |
| 786 | void |
| 787 | ChatDialog::fitView() |
| 788 | { |
| 789 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 790 | QRectF rect = m_scene->itemsBoundingRect(); |
| 791 | m_scene->setSceneRect(rect); |
| 792 | ui->treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
| 793 | } |
| 794 | |
| 795 | void |
| 796 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
| 797 | msg.set_from(m_user.getNick().toStdString()); |
| 798 | msg.set_to(m_user.getChatroom().toStdString()); |
| 799 | msg.set_data(text.toUtf8().constData()); |
| 800 | time_t seconds = time(NULL); |
| 801 | msg.set_timestamp(seconds); |
| 802 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
| 803 | } |
| 804 | |
| 805 | void |
| 806 | ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type) |
| 807 | { |
| 808 | msg.set_from(m_user.getNick().toStdString()); |
| 809 | msg.set_to(m_user.getChatroom().toStdString()); |
| 810 | time_t seconds = time(NULL); |
| 811 | msg.set_timestamp(seconds); |
| 812 | msg.set_type(type); |
| 813 | } |
| 814 | |
| 815 | void |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 816 | ChatDialog::changeEvent(QEvent *e) |
| 817 | { |
| 818 | switch(e->type()) |
| 819 | { |
| 820 | case QEvent::ActivationChange: |
| 821 | if (isActiveWindow()) |
| 822 | { |
| 823 | trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 824 | } |
| 825 | break; |
| 826 | default: |
| 827 | break; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | void |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 832 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory) |
| 833 | { |
| 834 | boost::recursive_mutex::scoped_lock lock(m_msgMutex); |
| 835 | |
| 836 | if (msg.type() == SyncDemo::ChatMessage::CHAT) |
| 837 | { |
| 838 | |
| 839 | if (!msg.has_data()) |
| 840 | { |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | if (msg.from().empty() || msg.data().empty()) |
| 845 | { |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | if (!msg.has_timestamp()) |
| 850 | { |
| 851 | return; |
| 852 | } |
| 853 | |
| 854 | // if (m_history.size() == MAX_HISTORY_ENTRY) |
| 855 | // { |
| 856 | // m_history.dequeue(); |
| 857 | // } |
| 858 | |
| 859 | // m_history.enqueue(msg); |
| 860 | |
| 861 | QTextCharFormat nickFormat; |
| 862 | nickFormat.setForeground(Qt::darkGreen); |
| 863 | nickFormat.setFontWeight(QFont::Bold); |
| 864 | nickFormat.setFontUnderline(true); |
| 865 | nickFormat.setUnderlineColor(Qt::gray); |
| 866 | |
| 867 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 868 | cursor.movePosition(QTextCursor::End); |
| 869 | QTextTableFormat tableFormat; |
| 870 | tableFormat.setBorder(0); |
| 871 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 872 | QString from = QString("%1 ").arg(msg.from().c_str()); |
| 873 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 874 | fromCell.setFormat(nickFormat); |
| 875 | fromCell.firstCursorPosition().insertText(from); |
| 876 | |
| 877 | time_t timestamp = msg.timestamp(); |
| 878 | printTimeInCell(table, timestamp); |
| 879 | |
| 880 | QTextCursor nextCursor(ui->textEdit->textCursor()); |
| 881 | nextCursor.movePosition(QTextCursor::End); |
| 882 | table = nextCursor.insertTable(1, 1, tableFormat); |
| 883 | table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str())); |
| 884 | if (!isHistory) |
| 885 | { |
| 886 | showMessage(from, QString::fromUtf8(msg.data().c_str())); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 891 | { |
| 892 | QTextCharFormat nickFormat; |
| 893 | nickFormat.setForeground(Qt::gray); |
| 894 | nickFormat.setFontWeight(QFont::Bold); |
| 895 | nickFormat.setFontUnderline(true); |
| 896 | nickFormat.setUnderlineColor(Qt::gray); |
| 897 | |
| 898 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 899 | cursor.movePosition(QTextCursor::End); |
| 900 | QTextTableFormat tableFormat; |
| 901 | tableFormat.setBorder(0); |
| 902 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 903 | QString action; |
| 904 | if (msg.type() == SyncDemo::ChatMessage::JOIN) |
| 905 | { |
| 906 | action = "enters room"; |
| 907 | } |
| 908 | else |
| 909 | { |
| 910 | action = "leaves room"; |
| 911 | } |
| 912 | |
| 913 | QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action); |
| 914 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 915 | fromCell.setFormat(nickFormat); |
| 916 | fromCell.firstCursorPosition().insertText(from); |
| 917 | |
| 918 | time_t timestamp = msg.timestamp(); |
| 919 | printTimeInCell(table, timestamp); |
| 920 | } |
| 921 | |
| 922 | QScrollBar *bar = ui->textEdit->verticalScrollBar(); |
| 923 | bar->setValue(bar->maximum()); |
| 924 | } |
| 925 | |
| 926 | QString |
| 927 | ChatDialog::formatTime(time_t timestamp) |
| 928 | { |
| 929 | struct tm *tm_time = localtime(×tamp); |
| 930 | int hour = tm_time->tm_hour; |
| 931 | QString amOrPM; |
| 932 | if (hour > 12) |
| 933 | { |
| 934 | hour -= 12; |
| 935 | amOrPM = "PM"; |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | amOrPM = "AM"; |
| 940 | if (hour == 0) |
| 941 | { |
| 942 | hour = 12; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | char textTime[12]; |
| 947 | sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str()); |
| 948 | return QString(textTime); |
| 949 | } |
| 950 | |
| 951 | void |
| 952 | ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp) |
| 953 | { |
| 954 | QTextCharFormat timeFormat; |
| 955 | timeFormat.setForeground(Qt::gray); |
| 956 | timeFormat.setFontUnderline(true); |
| 957 | timeFormat.setUnderlineColor(Qt::gray); |
| 958 | QTextTableCell timeCell = table->cellAt(0, 1); |
| 959 | timeCell.setFormat(timeFormat); |
| 960 | timeCell.firstCursorPosition().insertText(formatTime(timestamp)); |
| 961 | } |
| 962 | |
| 963 | void |
| 964 | ChatDialog::showMessage(QString from, QString data) |
| 965 | { |
| 966 | if (!isActiveWindow()) |
| 967 | { |
Yingdi Yu | a059409 | 2013-11-06 22:07:38 -0800 | [diff] [blame^] | 968 | trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000); |
| 969 | trayIcon->setIcon(QIcon(":/images/note.png")); |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | void |
| 974 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 975 | { |
| 976 | // send msg |
| 977 | size_t size = msg.ByteSize(); |
| 978 | char *buf = new char[size]; |
| 979 | msg.SerializeToArray(buf, size); |
| 980 | if (!msg.IsInitialized()) |
| 981 | { |
| 982 | _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?"); |
| 983 | abort(); |
| 984 | } |
| 985 | m_sock->publishData(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS); |
| 986 | |
| 987 | delete buf; |
| 988 | |
| 989 | m_lastMsgTime = time(NULL); |
| 990 | |
| 991 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
| 992 | Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)}; |
| 993 | std::vector<Sync::MissingDataInfo> v; |
| 994 | v.push_back(mdi); |
| 995 | { |
| 996 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 997 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 998 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | ChatDialog::openInviteListDialog() |
| 1004 | { |
| 1005 | m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri()); |
| 1006 | m_inviteListDialog->show(); |
| 1007 | } |
| 1008 | |
| 1009 | void |
| 1010 | ChatDialog::sendInvitationWrapper(QString invitee, bool isIntroducer) |
| 1011 | { |
| 1012 | ndn::Name inviteeNamespace(invitee.toUtf8().constData()); |
| 1013 | ndn::Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 1014 | sendInvitation(inviteeItem, isIntroducer); |
| 1015 | } |
| 1016 | |
Yingdi Yu | 4237244 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1017 | void |
| 1018 | ChatDialog::closeEvent(QCloseEvent *e) |
| 1019 | { |
| 1020 | hide(); |
| 1021 | _LOG_DEBUG("about to leave 1"); |
| 1022 | emit closeChatDialog(m_chatroomPrefix); |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | |
Yingdi Yu | 42f6646 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 1027 | |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 1028 | #if WAF |
| 1029 | #include "chatdialog.moc" |
| 1030 | #include "chatdialog.cpp.moc" |
| 1031 | #endif |