blob: 5b7eea24b0d0de7ee50d363ee7dcd0b851188a88 [file] [log] [blame]
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
Yingdi Yu5b989132013-10-23 14:03:09 -07004 * Yingdi Yu
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07005 *
Yingdi Yu5b989132013-10-23 14:03:09 -07006 * BSD license, See the LICENSE file for more information
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07007 *
Yingdi Yu7989eb22013-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>
Alexander Afanasyevb4b92292013-07-09 13:54:59 -070011 */
12
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070013#ifndef CHATDIALOG_H
14#define CHATDIALOG_H
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070015
Yingdi Yu5b989132013-10-23 14:03:09 -070016#include <QDialog>
Yingdi Yu7989eb22013-10-31 17:38:22 -070017#include <QTextTable>
18#include <QStringListModel>
Yingdi Yu83eae842013-11-06 22:07:38 -080019#include <QSystemTrayIcon>
Yingdi Yu702d6f12013-11-07 17:00:54 -080020#include <QMenu>
Yingdi Yu7989eb22013-10-31 17:38:22 -070021
22#include "invitelistdialog.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070023
Yingdi Yu5b989132013-10-23 14:03:09 -070024#ifndef Q_MOC_RUN
Yingdi Yueda39aa2013-10-23 23:07:29 -070025#include "contact-item.h"
Yingdi Yu7989eb22013-10-31 17:38:22 -070026#include "chatbuf.pb.h"
27#include "digesttreescene.h"
Yingdi Yufa4ce792014-02-06 18:09:22 -080028#include <sync-socket.h>
29#include <sync-seq-no.h>
30#include <ndn-cpp-dev/security/key-chain.hpp>
31#ifdef WITH_SECURITY
32#include "validator-invitation.h"
33#include <validator-sync.h>
34#else
35#include <ndn-cpp-dev/security/validator-null.hpp>
36#endif
Yingdi Yu6eabbd72013-12-27 08:44:12 +080037
38#include <boost/thread/locks.hpp>
39#include <boost/thread/recursive_mutex.hpp>
40#include <boost/thread/thread.hpp>
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070041#endif
42
Yingdi Yufa4ce792014-02-06 18:09:22 -080043typedef ndn::function<void()> OnEventualTimeout;
Yingdi Yu76dd8002013-12-24 11:16:32 +080044
Yingdi Yu7989eb22013-10-31 17:38:22 -070045#define MAX_HISTORY_ENTRY 20
46
Yingdi Yu5b989132013-10-23 14:03:09 -070047namespace Ui {
48class ChatDialog;
49}
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070050
Yingdi Yu5b989132013-10-23 14:03:09 -070051class ChatDialog : public QDialog
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070052{
Yingdi Yu5b989132013-10-23 14:03:09 -070053 Q_OBJECT
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070054
55public:
Yingdi Yufa4ce792014-02-06 18:09:22 -080056 explicit ChatDialog(ndn::shared_ptr<chronos::ContactManager> contactManager,
57 ndn::shared_ptr<ndn::Face> face,
Yingdi Yu7989eb22013-10-31 17:38:22 -070058 const ndn::Name& chatroomPrefix,
Yingdi Yu5b989132013-10-23 14:03:09 -070059 const ndn::Name& localPrefix,
Yingdi Yueda39aa2013-10-23 23:07:29 -070060 const ndn::Name& defaultIdentity,
Yingdi Yu46948282013-11-06 18:43:31 -080061 const std::string& nick,
Yingdi Yu5b989132013-10-23 14:03:09 -070062 QWidget *parent = 0);
Yingdi Yu7989eb22013-10-31 17:38:22 -070063
Zhenkai Zhu82a62752012-06-04 17:11:04 -070064 ~ChatDialog();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070065
Yingdi Yu5b989132013-10-23 14:03:09 -070066 const ndn::Name&
67 getChatroomPrefix() const
68 { return m_chatroomPrefix; }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070069
Yingdi Yu5b989132013-10-23 14:03:09 -070070 const ndn::Name&
71 getLocalPrefix() const
72 { return m_localPrefix; }
73
74 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080075 sendInvitation(ndn::shared_ptr<chronos::ContactItem> contact, bool isIntroducer);
Yingdi Yu7989eb22013-10-31 17:38:22 -070076
77 void
78 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu76dd8002013-12-24 11:16:32 +080079 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu7989eb22013-10-31 17:38:22 -070080 bool isIntroducer);
81
Yingdi Yufa4ce792014-02-06 18:09:22 -080082 void
83 addTrustAnchor(const chronos::EndorseCertificate& selfEndorseCertificate);
84
Yingdi Yu7989eb22013-10-31 17:38:22 -070085 void
86 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
87
88 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080089 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
Yingdi Yu7989eb22013-10-31 17:38:22 -070090
91 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080092 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -070093
94 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080095 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -070096
97 void
98 processRemoveWrapper(std::string);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070099
Yingdi Yu83eae842013-11-06 22:07:38 -0800100 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800101 publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer);
Yingdi Yu83eae842013-11-06 22:07:38 -0800102
Yingdi Yu46948282013-11-06 18:43:31 -0800103protected:
Yingdi Yu83eae842013-11-06 22:07:38 -0800104 void
105 closeEvent(QCloseEvent *e);
106
107 void
108 changeEvent(QEvent *e);
Yingdi Yu46948282013-11-06 18:43:31 -0800109
Zhenkai Zhub45e38a2012-06-01 15:44:36 -0700110private:
Yingdi Yu7989eb22013-10-31 17:38:22 -0700111
Yingdi Yu76dd8002013-12-24 11:16:32 +0800112 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700113 initializeSetting();
114
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800115 QString
116 getRandomString();
117
Yingdi Yu7989eb22013-10-31 17:38:22 -0700118 void
119 updateLabels();
120
Yingdi Yueda39aa2013-10-23 23:07:29 -0700121 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800122 initializeSync();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700123
124 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800125 onTargetData(const ndn::Interest& interest,
126 const ndn::Data& data,
127 const ndn::OnDataValidated& onValidated,
128 const ndn::OnDataValidationFailed& onValidationFailed)
129 { m_invitationValidator->validate(data, onValidated, onValidationFailed); }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800130
131 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800132 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800133 int retry,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800134 const ndn::OnDataValidated& onValidated,
135 const ndn::OnDataValidationFailed& onValidationFailed,
136 const OnEventualTimeout& timeoutNotify)
137 {
138 if(retry > 0)
139 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
140 else
141 timeoutNotify();
142 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800143
144 void
145 sendInterest(const ndn::Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800146 const ndn::OnDataValidated& onValidated,
147 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800148 const OnEventualTimeout& timeoutNotify,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800149 int retry = 1);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700150
151 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800152 onInviteReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
153 const ndn::Name& identity,
154 bool isIntroduce);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800155
156 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800157 onInviteReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800158 const ndn::Name& identity);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700159
160 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800161 onInviteReplyTimeout(const ndn::Name& identity);
162
Yingdi Yueda39aa2013-10-23 23:07:29 -0700163
164 void
165 invitationRejected(const ndn::Name& identity);
166
167 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800168 invitationAccepted(const ndn::Name& identity,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800169 ndn::shared_ptr<const ndn::Data> data,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800170 const std::string& inviteePrefix,
Yingdi Yu7989eb22013-10-31 17:38:22 -0700171 bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700172
173 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800174 onLocalPrefix(const ndn::Interest& interest,
175 ndn::Data& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700176
Yingdi Yu46948282013-11-06 18:43:31 -0800177 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800178 onLocalPrefixTimeout(const ndn::Interest& interest);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700179
180 // void
181 // fetchHistory(std::string name);
182
183 void
184 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
185
186 void
187 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
188
189 void
190 sendMsg(SyncDemo::ChatMessage &msg);
191
192 void
193 resizeEvent(QResizeEvent *);
194
195 void
196 showEvent(QShowEvent *);
197
198 void
199 fitView();
200
Yingdi Yu83eae842013-11-06 22:07:38 -0800201 void
202 createActions();
203
204 void
205 createTrayIcon();
206
Yingdi Yu7989eb22013-10-31 17:38:22 -0700207 QString
208 formatTime(time_t);
209
210 void
211 printTimeInCell(QTextTable *, time_t);
212
213 void
214 disableTreeDisplay();
215
Yingdi Yufa4ce792014-02-06 18:09:22 -0800216signals:
217 void
218 dataReceived(ndn::shared_ptr<const ndn::Data> data, bool show, bool isHistory);
219
Yingdi Yu7989eb22013-10-31 17:38:22 -0700220 void
221 treeUpdated(const std::vector<Sync::MissingDataInfo>);
222
223 void
224 removeReceived(QString prefix);
225
Yingdi Yu46948282013-11-06 18:43:31 -0800226 void
227 closeChatDialog(const ndn::Name& chatroomPrefix);
228
Yingdi Yu8e135832013-11-09 20:12:31 -0800229 void
230 noNdnConnection(const QString& msg);
Yingdi Yu0aca2652013-11-10 10:47:44 -0800231
232 void
233 inivationRejection(const QString& msg);
Yingdi Yu8e135832013-11-09 20:12:31 -0800234
Yingdi Yu3a014852014-02-11 22:54:11 -0800235 void
236 triggerHello();
237
238 void
239 triggerJoin();
240
241 void
242 triggerLeave();
243
244 void
245 triggerReplot();
246
247 void
248 triggerEnableTreeDisplay();
249
250 void
251 triggerReap();
252
Yingdi Yu7989eb22013-10-31 17:38:22 -0700253public slots:
254 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800255 processTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700256
257 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800258 processData(ndn::shared_ptr<const ndn::Data> data, bool show, bool isHistory);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700259
260 void
261 processRemove(QString prefix);
262
263private slots:
264 void
265 returnPressed();
266
267 void
268 treeButtonPressed();
269
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800270 void
271 settingUpdated(QString, QString, QString);
272
Yingdi Yu3a014852014-02-11 22:54:11 -0800273 void
274 sendJoinWrapper();
275
Yingdi Yu7989eb22013-10-31 17:38:22 -0700276 void
277 sendJoin();
278
279 void
Yingdi Yu3a014852014-02-11 22:54:11 -0800280 sendHelloWrapper();
281
282 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700283 sendHello();
284
285 void
286 sendLeave();
287
Yingdi Yu3a014852014-02-11 22:54:11 -0800288 void
289 sendLeaveWrapper();
290
Yingdi Yu7989eb22013-10-31 17:38:22 -0700291 void
292 replot();
293
Yingdi Yu3a014852014-02-11 22:54:11 -0800294 void
295 replotWrapper();
296
Yingdi Yu7989eb22013-10-31 17:38:22 -0700297 void
298 updateRosterList(QStringList);
299
300 void
301 enableTreeDisplay();
302
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800303 void
Yingdi Yu3a014852014-02-11 22:54:11 -0800304 enableTreeDisplayWrapper();
305
306 void
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800307 updateLocalPrefix();
308
Yingdi Yu7989eb22013-10-31 17:38:22 -0700309 void
310 summonReaper();
311
312 void
313 reap();
314
Yingdi Yu3a014852014-02-11 22:54:11 -0800315 void
316 reapWrapper();
317
Yingdi Yu7989eb22013-10-31 17:38:22 -0700318 void
Yingdi Yu83eae842013-11-06 22:07:38 -0800319 iconActivated(QSystemTrayIcon::ActivationReason reason);
320
321 void
322 messageClicked();
323
324 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700325 showMessage(QString, QString);
326
327 void
328 openInviteListDialog();
329
330 void
331 sendInvitationWrapper(QString, bool);
Yingdi Yu702d6f12013-11-07 17:00:54 -0800332
333 void
334 quit();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700335
336private:
Yingdi Yu5b989132013-10-23 14:03:09 -0700337 Ui::ChatDialog *ui;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800338 ndn::shared_ptr<chronos::ContactManager> m_contactManager;
339 ndn::shared_ptr<ndn::Face> m_face;
340 ndn::shared_ptr<boost::asio::io_service> m_ioService;
341
Yingdi Yu5b989132013-10-23 14:03:09 -0700342 ndn::Name m_chatroomPrefix;
343 ndn::Name m_localPrefix;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700344 ndn::Name m_localChatPrefix;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700345 ndn::Name m_defaultIdentity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800346 User m_user;
347 std::string m_nick;
Yingdi Yua66183f2013-11-10 10:32:18 -0800348
Yingdi Yufa4ce792014-02-06 18:09:22 -0800349 ndn::Scheduler m_scheduler;
350 ndn::EventId m_replotEventId;
351
352#ifndef WITH_SECURITY
353 ndn::shared_ptr<ndn::Validator> m_invitationValidator;
354 ndn::shared_ptr<ndn::Validator> m_syncValidator;
355#else
356 ndn::shared_ptr<chronos::ValidatorInvitation> m_invitationValidator;
357 ndn::shared_ptr<Sync::ValidatorSync> m_syncValidator;
358#endif
359 ndn::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800360
Yingdi Yua66183f2013-11-10 10:32:18 -0800361 ndn::Name m_newLocalPrefix;
362 bool m_newLocalPrefixReady;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700363
Yingdi Yufa4ce792014-02-06 18:09:22 -0800364
Yingdi Yu7989eb22013-10-31 17:38:22 -0700365 Sync::SyncSocket *m_sock;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800366 uint64_t m_session;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700367 DigestTreeScene *m_scene;
368 boost::recursive_mutex m_msgMutex;
369 boost::recursive_mutex m_sceneMutex;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800370 int64_t m_lastMsgTime;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700371 int m_randomizedInterval;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800372
373
Yingdi Yu7989eb22013-10-31 17:38:22 -0700374 QStringListModel *m_rosterModel;
Yingdi Yu83eae842013-11-06 22:07:38 -0800375 QSystemTrayIcon *trayIcon;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700376
Yingdi Yu702d6f12013-11-07 17:00:54 -0800377 QAction *minimizeAction;
378 QAction *maximizeAction;
379 QAction *restoreAction;
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800380 QAction *updateLocalPrefixAction;
Yingdi Yu702d6f12013-11-07 17:00:54 -0800381 QAction *quitAction;
382 QMenu *trayIconMenu;
383
Yingdi Yu7989eb22013-10-31 17:38:22 -0700384 bool m_joined;
385
386 QList<QString> m_zombieList;
387 int m_zombieIndex;
388
389 InviteListDialog* m_inviteListDialog;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700390};
Yingdi Yu5b989132013-10-23 14:03:09 -0700391
392#endif // ChatDIALOG_H