blob: 1e30f87050c4dca1571f64bada82b557cf3b5cc1 [file] [log] [blame]
Yingdi Yu04842232013-10-23 14:03:09 -07001/* -*- 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 Yu42f66462013-10-31 17:38:22 -07008 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
9 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
10 * Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu04842232013-10-23 14:03:09 -070011 */
12
13#include "chatdialog.h"
14#include "ui_chatdialog.h"
15
Yingdi Yu42f66462013-10-31 17:38:22 -070016#include <QScrollBar>
17
Yingdi Yuc4d08d22013-10-23 23:07:29 -070018#ifndef Q_MOC_RUN
19#include <ndn.cxx/security/identity/identity-manager.h>
Yingdi Yuc4d08d22013-10-23 23:07:29 -070020#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
Yingdi Yu42f66462013-10-31 17:38:22 -070021#include <sync-intro-certificate.h>
22#include <boost/random/random_device.hpp>
23#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yuc4d08d22013-10-23 23:07:29 -070024#include "logging.h"
25#endif
26
Yingdi Yu04842232013-10-23 14:03:09 -070027using namespace std;
Yingdi Yu04842232013-10-23 14:03:09 -070028
Yingdi Yuc4d08d22013-10-23 23:07:29 -070029INIT_LOGGER("ChatDialog");
30
Yingdi Yu42f66462013-10-31 17:38:22 -070031static const int HELLO_INTERVAL = FRESHNESS * 3 / 4;
32
33Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> )
34Q_DECLARE_METATYPE(size_t)
35
36ChatDialog::ChatDialog(ndn::Ptr<ContactManager> contactManager,
37 const ndn::Name& chatroomPrefix,
38 const ndn::Name& localPrefix,
39 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080040 const std::string& nick,
41 bool trial,
Yingdi Yu04842232013-10-23 14:03:09 -070042 QWidget *parent)
Yingdi Yu42f66462013-10-31 17:38:22 -070043: 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 Yu42372442013-11-06 18:43:31 -080050 , m_nick(nick)
Yingdi Yu42f66462013-10-31 17:38:22 -070051 , m_sock(NULL)
52 , m_lastMsgTime(0)
53 // , m_historyInitialized(false)
54 , m_joined(false)
55 , m_inviteListDialog(new InviteListDialog(m_contactManager))
Yingdi Yu04842232013-10-23 14:03:09 -070056{
Yingdi Yu42f66462013-10-31 17:38:22 -070057 qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
58 qRegisterMetaType<size_t>("size_t");
59
Yingdi Yuc4d08d22013-10-23 23:07:29 -070060 ui->setupUi(this);
61
Yingdi Yu42f66462013-10-31 17:38:22 -070062 m_localChatPrefix = m_localPrefix;
Yingdi Yu42372442013-11-06 18:43:31 -080063 m_localChatPrefix.append("%F0.").append(m_defaultIdentity);
Yingdi Yu42f66462013-10-31 17:38:22 -070064 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 Yua0594092013-11-06 22:07:38 -080081 createActions();
82 createTrayIcon();
83
Yingdi Yu42f66462013-10-31 17:38:22 -070084 m_timer = new QTimer(this);
85
Yingdi Yu42372442013-11-06 18:43:31 -080086 setWrapper(trial);
Yingdi Yu42f66462013-10-31 17:38:22 -070087
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 Yua0594092013-11-06 22:07:38 -0800104 connect(trayIcon, SIGNAL(messageClicked()),
105 this, SLOT(showNormal()));
106 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
107 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Yingdi Yu42f66462013-10-31 17:38:22 -0700108 connect(m_scene, SIGNAL(rosterChanged(QStringList)),
109 this, SLOT(updateRosterList(QStringList)));
110
Yingdi Yu42f66462013-10-31 17:38:22 -0700111
112 initializeSync();
Yingdi Yu04842232013-10-23 14:03:09 -0700113}
114
Yingdi Yu42f66462013-10-31 17:38:22 -0700115
Yingdi Yu04842232013-10-23 14:03:09 -0700116ChatDialog::~ChatDialog()
117{
Yingdi Yu42372442013-11-06 18:43:31 -0800118 if(m_sock != NULL)
119 {
120 sendLeave();
121 delete m_sock;
122 m_sock = NULL;
123 }
Yingdi Yua0594092013-11-06 22:07:38 -0800124 m_handler->shutdown();
Yingdi Yu04842232013-10-23 14:03:09 -0700125}
126
127void
Yingdi Yu42372442013-11-06 18:43:31 -0800128ChatDialog::setWrapper(bool trial)
Yingdi Yu04842232013-10-23 14:03:09 -0700129{
Yingdi Yu42f66462013-10-31 17:38:22 -0700130 m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700131
Yingdi Yu42f66462013-10-31 17:38:22 -0700132 ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
133 m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700134
Yingdi Yu42f66462013-10-31 17:38:22 -0700135 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 Yu42372442013-11-06 18:43:31 -0800138
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 Yu04842232013-10-23 14:03:09 -0700155}
156
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700157void
Yingdi Yu42f66462013-10-31 17:38:22 -0700158ChatDialog::initializeSetting()
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700159{
Yingdi Yu42372442013-11-06 18:43:31 -0800160 m_user.setNick(QString::fromStdString(m_nick));
Yingdi Yu42f66462013-10-31 17:38:22 -0700161 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 Yuc4d08d22013-10-23 23:07:29 -0700166
Yingdi Yu42f66462013-10-31 17:38:22 -0700167void
168ChatDialog::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 Yuc4d08d22013-10-23 23:07:29 -0700186
Yingdi Yu42f66462013-10-31 17:38:22 -0700187void
188ChatDialog::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 Yuc4d08d22013-10-23 23:07:29 -0700195 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 Yu42f66462013-10-31 17:38:22 -0700204 ndn::Blob signedBlob(signedUri.c_str(), signedUri.size());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700205
Yingdi Yu42f66462013-10-31 17:38:22 -0700206 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 Yuc4d08d22013-10-23 23:07:29 -0700208
209 interestName.append(sigBits.buf(), sigBits.size());
Yingdi Yu42372442013-11-06 18:43:31 -0800210 interestName.appendVersion();
211
Yingdi Yu42f66462013-10-31 17:38:22 -0700212 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 Yuc4d08d22013-10-23 23:07:29 -0700227
228 m_handler->sendInterest(interest, closure);
229}
230
231void
Yingdi Yu42f66462013-10-31 17:38:22 -0700232ChatDialog::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate)
233{ m_invitationPolicyManager->addTrustAnchor(selfEndorseCertificate); }
234
235void
236ChatDialog::addChatDataRule(const ndn::Name& prefix,
237 const ndn::security::IdentityCertificate& identityCertificate,
238 bool isIntroducer)
239{ m_syncPolicyManager->addChatDataRule(prefix, identityCertificate, isIntroducer); }
240
241void
Yingdi Yua0594092013-11-06 22:07:38 -0800242ChatDialog::publishIntroCert(const ndn::security::IdentityCertificate& dskCertificate, bool isIntroducer)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700243{
Yingdi Yu42f66462013-10-31 17:38:22 -0700244 SyncIntroCertificate syncIntroCertificate(m_chatroomPrefix,
Yingdi Yua0594092013-11-06 22:07:38 -0800245 dskCertificate.getPublicKeyName(),
Yingdi Yu42f66462013-10-31 17:38:22 -0700246 m_identityManager->getDefaultKeyNameForIdentity(m_defaultIdentity),
Yingdi Yua0594092013-11-06 22:07:38 -0800247 dskCertificate.getNotBefore(),
248 dskCertificate.getNotAfter(),
249 dskCertificate.getPublicKeyInfo(),
Yingdi Yu42f66462013-10-31 17:38:22 -0700250 (isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER));
251 ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800252 _LOG_DEBUG("Publish Intro Certificate: " << syncIntroCertificate.getName());
Yingdi Yu42f66462013-10-31 17:38:22 -0700253 m_identityManager->signByCertificate(syncIntroCertificate, certName);
254 m_handler->putToNdnd(*syncIntroCertificate.encodeToWire());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700255}
256
257void
Yingdi Yu42f66462013-10-31 17:38:22 -0700258ChatDialog::invitationRejected(const ndn::Name& identity)
259{
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800260 _LOG_DEBUG(" " << identity.toUri() << " Rejected your invitation!");
Yingdi Yu42f66462013-10-31 17:38:22 -0700261}
262
263void
264ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::Ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700265{
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800266 _LOG_DEBUG(" " << identity.toUri() << " Accepted your invitation!");
Yingdi Yu42f66462013-10-31 17:38:22 -0700267 ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const ndn::signature::Sha256WithRsa> (data->getSignature());
268 const ndn::Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
269 ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate = m_invitationPolicyManager->getValidatedDskCertificate(keyLocatorName);
Yingdi Yuc90deb12013-11-06 18:51:19 -0800270 m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer);
Yingdi Yua0594092013-11-06 22:07:38 -0800271 publishIntroCert(*dskCertificate, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700272}
273
274void
Yingdi Yu42f66462013-10-31 17:38:22 -0700275ChatDialog::onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700276{
277 string content(data->content().buf(), data->content().size());
278 if(content.empty())
279 invitationRejected(identity);
280 else
Yingdi Yu42f66462013-10-31 17:38:22 -0700281 invitationAccepted(identity, data, content, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700282}
283
284void
Yingdi Yu42f66462013-10-31 17:38:22 -0700285ChatDialog::onInviteTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest, const ndn::Name& identity, int retry)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700286{
287 if(retry > 0)
288 {
Yingdi Yu42f66462013-10-31 17:38:22 -0700289 ndn::Ptr<ndn::Closure> newClosure = ndn::Ptr<ndn::Closure>(new ndn::Closure(closure->m_dataCallback,
290 boost::bind(&ChatDialog::onInviteTimeout,
291 this,
292 _1,
293 _2,
294 identity,
295 retry - 1),
296 closure->m_unverifiedCallback,
297 closure->m_stepCount)
298 );
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700299 m_handler->sendInterest(interest, newClosure);
300 }
301 else
302 invitationRejected(identity);
303}
304
305void
Yingdi Yu42f66462013-10-31 17:38:22 -0700306ChatDialog::onUnverified(ndn::Ptr<ndn::Data> data)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700307{}
308
Yingdi Yu42f66462013-10-31 17:38:22 -0700309void
Yingdi Yu42372442013-11-06 18:43:31 -0800310ChatDialog::onTimeout(ndn::Ptr<ndn::Closure> closure,
311 ndn::Ptr<ndn::Interest> interest)
312{}
313
314void
Yingdi Yu42f66462013-10-31 17:38:22 -0700315ChatDialog::initializeSync()
316{
317
318 m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(),
319 m_syncPolicyManager,
320 bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
321 bind(&ChatDialog::processRemoveWrapper, this, _1));
322
323 usleep(100000);
324
325 QTimer::singleShot(600, this, SLOT(sendJoin()));
326 m_timer->start(FRESHNESS * 1000);
327 disableTreeDisplay();
328 QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
329 // Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
330 // handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
331 // _LOG_DEBUG("initializeSync is done!");
332}
333
334void
335ChatDialog::returnPressed()
336{
337 QString text = ui->lineEdit->text();
338 if (text.isEmpty())
339 return;
340
341 ui->lineEdit->clear();
342
343 if (text.startsWith("boruoboluomi"))
344 {
345 summonReaper ();
346 // reapButton->show();
347 fitView();
348 return;
349 }
350
351 if (text.startsWith("minimanihong"))
352 {
353 // reapButton->hide();
354 fitView();
355 return;
356 }
357
358 SyncDemo::ChatMessage msg;
359 formChatMessage(text, msg);
360
361 appendMessage(msg);
362
363 sendMsg(msg);
364
365 fitView();
366}
367
368void
369ChatDialog::treeButtonPressed()
370{
371 if (ui->treeViewer->isVisible())
372 {
373 ui->treeViewer->hide();
374 ui->treeButton->setText("Show ChronoSync Tree");
375 }
376 else
377 {
378 ui->treeViewer->show();
379 ui->treeButton->setText("Hide ChronoSync Tree");
380 }
381
382 fitView();
383}
384
385void ChatDialog::disableTreeDisplay()
386{
387 ui->treeButton->setEnabled(false);
388 ui->treeViewer->hide();
389 fitView();
390}
391
392void ChatDialog::enableTreeDisplay()
393{
394 ui->treeButton->setEnabled(true);
395 // treeViewer->show();
396 // fitView();
397}
398
399void
400ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncSocket *sock)
401{
402 emit treeUpdated(v);
403 _LOG_DEBUG("<<< Tree update signal emitted");
404}
405
406void
407ChatDialog::processRemoveWrapper(std::string prefix)
408{
409 _LOG_DEBUG("Sync REMOVE signal received for prefix: " << prefix);
410}
411
412void
413ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
414{
415 _LOG_DEBUG("<<< processing Tree Update");
416
417 if (v.empty())
418 {
419 return;
420 }
421
422 // reflect the changes on digest tree
423 {
424 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
425 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
426 }
427
428 int n = v.size();
429 int totalMissingPackets = 0;
430 for (int i = 0; i < n; i++)
431 {
432 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
433 }
434
435 for (int i = 0; i < n; i++)
436 {
437 if (totalMissingPackets < 4)
438 {
439 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
440 {
441 m_sock->fetchData(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1), 2);
442 _LOG_DEBUG("<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq());
443 }
444 }
445 else
446 {
447 m_sock->fetchData(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1), 2);
448 }
449 }
450
451 // adjust the view
452 fitView();
453
454}
455
456void
457ChatDialog::processDataWrapper(ndn::Ptr<ndn::Data> data)
458{
459 string name = data->getName().toUri();
460 const char* buf = data->content().buf();
461 size_t len = data->content().size();
462
463 char *tempBuf = new char[len];
464 memcpy(tempBuf, buf, len);
465 emit dataReceived(name.c_str(), tempBuf, len, true, false);
466 _LOG_DEBUG("<<< " << name << " fetched");
467}
468
469void
470ChatDialog::processDataNoShowWrapper(ndn::Ptr<ndn::Data> data)
471{
472 string name = data->getName().toUri();
473 const char* buf = data->content().buf();
474 size_t len = data->content().size();
475
476 char *tempBuf = new char[len];
477 memcpy(tempBuf, buf, len);
478 emit dataReceived(name.c_str(), tempBuf, len, false, false);
479
480 // if (!m_historyInitialized)
481 // {
482 // fetchHistory(name);
483 // m_historyInitialized = true;
484 // }
485}
486
487// void
488// ChatDialog::fetchHistory(std::string name)
489// {
490
491// /****************************/
492// /* TODO: fix following part */
493// /****************************/
494// string nameWithoutSeq = name.substr(0, name.find_last_of('/'));
495// string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/'));
496// prefix += "/history";
497// // Ptr<Wrapper>CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
498// // QString randomString = getRandomString();
499// // for (int i = 0; i < MAX_HISTORY_ENTRY; i++)
500// // {
501// // QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i);
502// // handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3));
503// // }
504// }
505
506void
507ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory)
508{
509 SyncDemo::ChatMessage msg;
510 bool corrupted = false;
511 if (!msg.ParseFromArray(buf, len))
512 {
513 _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?");
514 // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
515 msg.set_from("inconnu");
516 msg.set_type(SyncDemo::ChatMessage::OTHER);
517 corrupted = true;
518 }
519
520 delete [] buf;
521 buf = NULL;
522
523 // display msg received from network
524 // we have to do so; this function is called by ccnd thread
525 // so if we call appendMsg directly
526 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
527 // the "cannonical" way to is use signal-slot
528 if (show && !corrupted)
529 {
530 appendMessage(msg, isHistory);
531 }
532
533 if (!isHistory)
534 {
535 // update the tree view
536 std::string stdStrName = name.toStdString();
537 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
538 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
539 _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from());
540 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
541 {
542 processRemove(prefix.c_str());
543 }
544 else
545 {
546 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
547 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
548 }
549 }
550 fitView();
551}
552
553void
554ChatDialog::processRemove(QString prefix)
555{
556 _LOG_DEBUG("<<< remove node for prefix" << prefix.toStdString());
557
558 bool removed = m_scene->removeNode(prefix);
559 if (removed)
560 {
561 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
562 m_scene->plot(m_sock->getRootDigest().c_str());
563 }
564}
565
566void
567ChatDialog::sendJoin()
568{
569 m_joined = true;
570 SyncDemo::ChatMessage msg;
571 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
572 sendMsg(msg);
573 boost::random::random_device rng;
574 boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
575 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
576 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
577}
578
579void
580ChatDialog::sendHello()
581{
582 time_t now = time(NULL);
583 int elapsed = now - m_lastMsgTime;
584 if (elapsed >= m_randomizedInterval / 1000)
585 {
586 SyncDemo::ChatMessage msg;
587 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
588 sendMsg(msg);
589 boost::random::random_device rng;
590 boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
591 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
592 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
593 }
594 else
595 {
596 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
597 }
598}
599
600void
601ChatDialog::sendLeave()
602{
603 SyncDemo::ChatMessage msg;
604 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
605 sendMsg(msg);
606 usleep(500000);
607 m_sock->remove(m_user.getPrefix().toStdString());
608 usleep(5000);
609 m_joined = false;
610 _LOG_DEBUG("Sync REMOVE signal sent");
611}
612
613void
614ChatDialog::replot()
615{
616 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
617 m_scene->plot(m_sock->getRootDigest().c_str());
618 fitView();
619}
620
621void
622ChatDialog::summonReaper()
623{
624 Sync::SyncLogic &logic = m_sock->getLogic ();
625 map<string, bool> branches = logic.getBranchPrefixes();
626 QMap<QString, DisplayUserPtr> roster = m_scene->getRosterFull();
627
628 m_zombieList.clear();
629
630 QMapIterator<QString, DisplayUserPtr> it(roster);
631 map<string, bool>::iterator mapIt;
632 while(it.hasNext())
633 {
634 it.next();
635 DisplayUserPtr p = it.value();
636 if (p != DisplayUserNullPtr)
637 {
638 mapIt = branches.find(p->getPrefix().toStdString());
639 if (mapIt != branches.end())
640 {
641 mapIt->second = true;
642 }
643 }
644 }
645
646 for (mapIt = branches.begin(); mapIt != branches.end(); ++mapIt)
647 {
648 // this is zombie. all active users should have been marked true
649 if (! mapIt->second)
650 {
651 m_zombieList.append(mapIt->first.c_str());
652 }
653 }
654
655 m_zombieIndex = 0;
656
657 // start reaping
658 reap();
659}
660
661void
662ChatDialog::reap()
663{
664 if (m_zombieIndex < m_zombieList.size())
665 {
666 string prefix = m_zombieList.at(m_zombieIndex).toStdString();
667 m_sock->remove(prefix);
668 _LOG_DEBUG("Reaped: prefix = " << prefix);
669 m_zombieIndex++;
670 // reap again in 10 seconds
671 QTimer::singleShot(10000, this, SLOT(reap()));
672 }
673}
674
675void
676ChatDialog::updateRosterList(QStringList staleUserList)
677{
678 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
679 QStringList rosterList = m_scene->getRosterList();
680 m_rosterModel->setStringList(rosterList);
681 QString user;
682 QStringListIterator it(staleUserList);
683 while(it.hasNext())
684 {
685 std::string nick = it.next().toStdString();
686 if (nick.empty())
687 continue;
688
689 SyncDemo::ChatMessage msg;
690 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
691 msg.set_from(nick);
692 appendMessage(msg);
693 }
694}
695
696void
Yingdi Yua0594092013-11-06 22:07:38 -0800697ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
698{
699 switch (reason)
700 {
701 case QSystemTrayIcon::Trigger:
702 case QSystemTrayIcon::DoubleClick:
703 break;
704 case QSystemTrayIcon::MiddleClick:
705 // showMessage();
706 break;
707 default:;
708 }
709}
710
711
712void
713ChatDialog::messageClicked()
714{
715 this->showMaximized();
716}
717
718
719void
720ChatDialog::createActions()
721{
722 // minimizeAction = new QAction(tr("Mi&nimize"), this);
723 // connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
724
725 // maximizeAction = new QAction(tr("Ma&ximize"), this);
726 // connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
727
728 // restoreAction = new QAction(tr("&Restore"), this);
729 // connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
730
731 // settingsAction = new QAction(tr("Settings"), this);
732 // connect (settingsAction, SIGNAL(triggered()), this, SLOT(buttonPressed()));
733
734 // settingsAction->setMenuRole (QAction::PreferencesRole);
735
736 // updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
737 // connect (updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(updateLocalPrefix()));
738
739 // quitAction = new QAction(tr("Quit"), this);
740 // connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
741}
742
743void
744ChatDialog::createTrayIcon()
745{
746 // trayIconMenu = new QMenu(this);
747 // trayIconMenu->addAction(minimizeAction);
748 // trayIconMenu->addAction(maximizeAction);
749 // trayIconMenu->addAction(restoreAction);
750 // trayIconMenu->addSeparator();
751 // trayIconMenu->addAction(settingsAction);
752 // trayIconMenu->addSeparator();
753 // trayIconMenu->addAction(updateLocalPrefixAction);
754 // trayIconMenu->addSeparator();
755 // trayIconMenu->addAction(quitAction);
756
757 trayIcon = new QSystemTrayIcon(this);
758 // trayIcon->setContextMenu(trayIconMenu);
759
760 QIcon icon(":/images/icon_small.png");
761 trayIcon->setIcon(icon);
762 setWindowIcon(icon);
763 trayIcon->setToolTip("ChronoChat System Tray Icon");
764 trayIcon->setVisible(true);
765
766 // // QApplication::getMenu ()->addMenu (trayIconMenu);
767 // QMenuBar *bar = new QMenuBar ();
768 // bar->setMenu (trayIconMenu);
769 // setMenuBar (bar);
770}
771
772
773void
Yingdi Yu42f66462013-10-31 17:38:22 -0700774ChatDialog::resizeEvent(QResizeEvent *e)
775{
776 fitView();
777}
778
779void
780ChatDialog::showEvent(QShowEvent *e)
781{
782 fitView();
783}
784
785void
786ChatDialog::fitView()
787{
788 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
789 QRectF rect = m_scene->itemsBoundingRect();
790 m_scene->setSceneRect(rect);
791 ui->treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
792}
793
794void
795ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
796 msg.set_from(m_user.getNick().toStdString());
797 msg.set_to(m_user.getChatroom().toStdString());
798 msg.set_data(text.toUtf8().constData());
799 time_t seconds = time(NULL);
800 msg.set_timestamp(seconds);
801 msg.set_type(SyncDemo::ChatMessage::CHAT);
802}
803
804void
805ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
806{
807 msg.set_from(m_user.getNick().toStdString());
808 msg.set_to(m_user.getChatroom().toStdString());
809 time_t seconds = time(NULL);
810 msg.set_timestamp(seconds);
811 msg.set_type(type);
812}
813
814void
Yingdi Yua0594092013-11-06 22:07:38 -0800815ChatDialog::changeEvent(QEvent *e)
816{
817 switch(e->type())
818 {
819 case QEvent::ActivationChange:
820 if (isActiveWindow())
821 {
822 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
823 }
824 break;
825 default:
826 break;
827 }
828}
829
830void
Yingdi Yu42f66462013-10-31 17:38:22 -0700831ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory)
832{
833 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
834
835 if (msg.type() == SyncDemo::ChatMessage::CHAT)
836 {
837
838 if (!msg.has_data())
839 {
840 return;
841 }
842
843 if (msg.from().empty() || msg.data().empty())
844 {
845 return;
846 }
847
848 if (!msg.has_timestamp())
849 {
850 return;
851 }
852
853 // if (m_history.size() == MAX_HISTORY_ENTRY)
854 // {
855 // m_history.dequeue();
856 // }
857
858 // m_history.enqueue(msg);
859
860 QTextCharFormat nickFormat;
861 nickFormat.setForeground(Qt::darkGreen);
862 nickFormat.setFontWeight(QFont::Bold);
863 nickFormat.setFontUnderline(true);
864 nickFormat.setUnderlineColor(Qt::gray);
865
866 QTextCursor cursor(ui->textEdit->textCursor());
867 cursor.movePosition(QTextCursor::End);
868 QTextTableFormat tableFormat;
869 tableFormat.setBorder(0);
870 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
871 QString from = QString("%1 ").arg(msg.from().c_str());
872 QTextTableCell fromCell = table->cellAt(0, 0);
873 fromCell.setFormat(nickFormat);
874 fromCell.firstCursorPosition().insertText(from);
875
876 time_t timestamp = msg.timestamp();
877 printTimeInCell(table, timestamp);
878
879 QTextCursor nextCursor(ui->textEdit->textCursor());
880 nextCursor.movePosition(QTextCursor::End);
881 table = nextCursor.insertTable(1, 1, tableFormat);
882 table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str()));
883 if (!isHistory)
884 {
885 showMessage(from, QString::fromUtf8(msg.data().c_str()));
886 }
887 }
888
889 if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
890 {
891 QTextCharFormat nickFormat;
892 nickFormat.setForeground(Qt::gray);
893 nickFormat.setFontWeight(QFont::Bold);
894 nickFormat.setFontUnderline(true);
895 nickFormat.setUnderlineColor(Qt::gray);
896
897 QTextCursor cursor(ui->textEdit->textCursor());
898 cursor.movePosition(QTextCursor::End);
899 QTextTableFormat tableFormat;
900 tableFormat.setBorder(0);
901 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
902 QString action;
903 if (msg.type() == SyncDemo::ChatMessage::JOIN)
904 {
905 action = "enters room";
906 }
907 else
908 {
909 action = "leaves room";
910 }
911
912 QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action);
913 QTextTableCell fromCell = table->cellAt(0, 0);
914 fromCell.setFormat(nickFormat);
915 fromCell.firstCursorPosition().insertText(from);
916
917 time_t timestamp = msg.timestamp();
918 printTimeInCell(table, timestamp);
919 }
920
921 QScrollBar *bar = ui->textEdit->verticalScrollBar();
922 bar->setValue(bar->maximum());
923}
924
925QString
926ChatDialog::formatTime(time_t timestamp)
927{
928 struct tm *tm_time = localtime(&timestamp);
929 int hour = tm_time->tm_hour;
930 QString amOrPM;
931 if (hour > 12)
932 {
933 hour -= 12;
934 amOrPM = "PM";
935 }
936 else
937 {
938 amOrPM = "AM";
939 if (hour == 0)
940 {
941 hour = 12;
942 }
943 }
944
945 char textTime[12];
946 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
947 return QString(textTime);
948}
949
950void
951ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
952{
953 QTextCharFormat timeFormat;
954 timeFormat.setForeground(Qt::gray);
955 timeFormat.setFontUnderline(true);
956 timeFormat.setUnderlineColor(Qt::gray);
957 QTextTableCell timeCell = table->cellAt(0, 1);
958 timeCell.setFormat(timeFormat);
959 timeCell.firstCursorPosition().insertText(formatTime(timestamp));
960}
961
962void
963ChatDialog::showMessage(QString from, QString data)
964{
965 if (!isActiveWindow())
966 {
Yingdi Yua0594092013-11-06 22:07:38 -0800967 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
968 trayIcon->setIcon(QIcon(":/images/note.png"));
Yingdi Yu42f66462013-10-31 17:38:22 -0700969 }
970}
971
972void
973ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
974{
975 // send msg
976 size_t size = msg.ByteSize();
977 char *buf = new char[size];
978 msg.SerializeToArray(buf, size);
979 if (!msg.IsInitialized())
980 {
981 _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?");
982 abort();
983 }
984 m_sock->publishData(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
985
986 delete buf;
987
988 m_lastMsgTime = time(NULL);
989
990 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
991 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
992 std::vector<Sync::MissingDataInfo> v;
993 v.push_back(mdi);
994 {
995 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
996 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
997 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
998 }
999}
1000
1001void
1002ChatDialog::openInviteListDialog()
1003{
1004 m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri());
1005 m_inviteListDialog->show();
1006}
1007
1008void
1009ChatDialog::sendInvitationWrapper(QString invitee, bool isIntroducer)
1010{
1011 ndn::Name inviteeNamespace(invitee.toUtf8().constData());
1012 ndn::Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
1013 sendInvitation(inviteeItem, isIntroducer);
1014}
1015
Yingdi Yu42372442013-11-06 18:43:31 -08001016void
1017ChatDialog::closeEvent(QCloseEvent *e)
1018{
1019 hide();
Yingdi Yu42372442013-11-06 18:43:31 -08001020 emit closeChatDialog(m_chatroomPrefix);
1021}
1022
1023
1024
Yingdi Yu42f66462013-10-31 17:38:22 -07001025
Yingdi Yu04842232013-10-23 14:03:09 -07001026#if WAF
1027#include "chatdialog.moc"
1028#include "chatdialog.cpp.moc"
1029#endif