blob: 742b4ee696e5ed548ca58232b47562ff16e83df4 [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>
19#include <QTimer>
Yingdi Yu83eae842013-11-06 22:07:38 -080020#include <QSystemTrayIcon>
Yingdi Yu702d6f12013-11-07 17:00:54 -080021#include <QMenu>
Yingdi Yu7989eb22013-10-31 17:38:22 -070022
23#include "invitelistdialog.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070024
Yingdi Yu5b989132013-10-23 14:03:09 -070025#ifndef Q_MOC_RUN
Yingdi Yueda39aa2013-10-23 23:07:29 -070026#include "contact-item.h"
Yingdi Yu7989eb22013-10-31 17:38:22 -070027#include "chatbuf.pb.h"
28#include "digesttreescene.h"
Yingdi Yufa4ce792014-02-06 18:09:22 -080029#include <sync-socket.h>
30#include <sync-seq-no.h>
31#include <ndn-cpp-dev/security/key-chain.hpp>
32#ifdef WITH_SECURITY
33#include "validator-invitation.h"
34#include <validator-sync.h>
35#else
36#include <ndn-cpp-dev/security/validator-null.hpp>
37#endif
Yingdi Yu6eabbd72013-12-27 08:44:12 +080038
39#include <boost/thread/locks.hpp>
40#include <boost/thread/recursive_mutex.hpp>
41#include <boost/thread/thread.hpp>
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070042#endif
43
Yingdi Yufa4ce792014-02-06 18:09:22 -080044typedef ndn::function<void()> OnEventualTimeout;
Yingdi Yu76dd8002013-12-24 11:16:32 +080045
Yingdi Yu7989eb22013-10-31 17:38:22 -070046#define MAX_HISTORY_ENTRY 20
47
Yingdi Yu5b989132013-10-23 14:03:09 -070048namespace Ui {
49class ChatDialog;
50}
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070051
Yingdi Yu5b989132013-10-23 14:03:09 -070052class ChatDialog : public QDialog
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070053{
Yingdi Yu5b989132013-10-23 14:03:09 -070054 Q_OBJECT
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070055
56public:
Yingdi Yufa4ce792014-02-06 18:09:22 -080057 explicit ChatDialog(ndn::shared_ptr<chronos::ContactManager> contactManager,
58 ndn::shared_ptr<ndn::Face> face,
Yingdi Yu7989eb22013-10-31 17:38:22 -070059 const ndn::Name& chatroomPrefix,
Yingdi Yu5b989132013-10-23 14:03:09 -070060 const ndn::Name& localPrefix,
Yingdi Yueda39aa2013-10-23 23:07:29 -070061 const ndn::Name& defaultIdentity,
Yingdi Yu46948282013-11-06 18:43:31 -080062 const std::string& nick,
Yingdi Yu5b989132013-10-23 14:03:09 -070063 QWidget *parent = 0);
Yingdi Yu7989eb22013-10-31 17:38:22 -070064
Zhenkai Zhu82a62752012-06-04 17:11:04 -070065 ~ChatDialog();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070066
Yingdi Yu5b989132013-10-23 14:03:09 -070067 const ndn::Name&
68 getChatroomPrefix() const
69 { return m_chatroomPrefix; }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070070
Yingdi Yu5b989132013-10-23 14:03:09 -070071 const ndn::Name&
72 getLocalPrefix() const
73 { return m_localPrefix; }
74
75 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080076 sendInvitation(ndn::shared_ptr<chronos::ContactItem> contact, bool isIntroducer);
Yingdi Yu7989eb22013-10-31 17:38:22 -070077
78 void
79 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu76dd8002013-12-24 11:16:32 +080080 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu7989eb22013-10-31 17:38:22 -070081 bool isIntroducer);
82
Yingdi Yufa4ce792014-02-06 18:09:22 -080083 void
84 addTrustAnchor(const chronos::EndorseCertificate& selfEndorseCertificate);
85
Yingdi Yu7989eb22013-10-31 17:38:22 -070086 void
87 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
88
89 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080090 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
Yingdi Yu7989eb22013-10-31 17:38:22 -070091
92 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080093 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -070094
95 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080096 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -070097
98 void
99 processRemoveWrapper(std::string);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700100
Yingdi Yu83eae842013-11-06 22:07:38 -0800101 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800102 publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer);
Yingdi Yu83eae842013-11-06 22:07:38 -0800103
Yingdi Yu46948282013-11-06 18:43:31 -0800104protected:
Yingdi Yu83eae842013-11-06 22:07:38 -0800105 void
106 closeEvent(QCloseEvent *e);
107
108 void
109 changeEvent(QEvent *e);
Yingdi Yu46948282013-11-06 18:43:31 -0800110
Zhenkai Zhub45e38a2012-06-01 15:44:36 -0700111private:
Yingdi Yu7989eb22013-10-31 17:38:22 -0700112
Yingdi Yu76dd8002013-12-24 11:16:32 +0800113 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700114 initializeSetting();
115
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800116 QString
117 getRandomString();
118
Yingdi Yu7989eb22013-10-31 17:38:22 -0700119 void
120 updateLabels();
121
Yingdi Yueda39aa2013-10-23 23:07:29 -0700122 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800123 initializeSync();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700124
125 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800126 onTargetData(const ndn::Interest& interest,
127 const ndn::Data& data,
128 const ndn::OnDataValidated& onValidated,
129 const ndn::OnDataValidationFailed& onValidationFailed)
130 { m_invitationValidator->validate(data, onValidated, onValidationFailed); }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800131
132 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800133 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800134 int retry,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800135 const ndn::OnDataValidated& onValidated,
136 const ndn::OnDataValidationFailed& onValidationFailed,
137 const OnEventualTimeout& timeoutNotify)
138 {
139 if(retry > 0)
140 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
141 else
142 timeoutNotify();
143 }
Yingdi Yu76dd8002013-12-24 11:16:32 +0800144
145 void
146 sendInterest(const ndn::Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800147 const ndn::OnDataValidated& onValidated,
148 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800149 const OnEventualTimeout& timeoutNotify,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800150 int retry = 1);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700151
152 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800153 onInviteReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
154 const ndn::Name& identity,
155 bool isIntroduce);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800156
157 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800158 onInviteReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800159 const ndn::Name& identity);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700160
161 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800162 onInviteReplyTimeout(const ndn::Name& identity);
163
Yingdi Yueda39aa2013-10-23 23:07:29 -0700164
165 void
166 invitationRejected(const ndn::Name& identity);
167
168 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800169 invitationAccepted(const ndn::Name& identity,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800170 ndn::shared_ptr<const ndn::Data> data,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800171 const std::string& inviteePrefix,
Yingdi Yu7989eb22013-10-31 17:38:22 -0700172 bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700173
174 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800175 onLocalPrefix(const ndn::Interest& interest,
176 ndn::Data& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700177
Yingdi Yu46948282013-11-06 18:43:31 -0800178 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800179 onLocalPrefixTimeout(const ndn::Interest& interest);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700180
181 // void
182 // fetchHistory(std::string name);
183
184 void
185 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
186
187 void
188 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
189
190 void
191 sendMsg(SyncDemo::ChatMessage &msg);
192
193 void
194 resizeEvent(QResizeEvent *);
195
196 void
197 showEvent(QShowEvent *);
198
199 void
200 fitView();
201
Yingdi Yu83eae842013-11-06 22:07:38 -0800202 void
203 createActions();
204
205 void
206 createTrayIcon();
207
Yingdi Yu7989eb22013-10-31 17:38:22 -0700208 QString
209 formatTime(time_t);
210
211 void
212 printTimeInCell(QTextTable *, time_t);
213
214 void
215 disableTreeDisplay();
216
Yingdi Yufa4ce792014-02-06 18:09:22 -0800217signals:
218 void
219 dataReceived(ndn::shared_ptr<const ndn::Data> data, bool show, bool isHistory);
220
Yingdi Yu7989eb22013-10-31 17:38:22 -0700221 void
222 treeUpdated(const std::vector<Sync::MissingDataInfo>);
223
224 void
225 removeReceived(QString prefix);
226
Yingdi Yu46948282013-11-06 18:43:31 -0800227 void
228 closeChatDialog(const ndn::Name& chatroomPrefix);
229
Yingdi Yu8e135832013-11-09 20:12:31 -0800230 void
231 noNdnConnection(const QString& msg);
Yingdi Yu0aca2652013-11-10 10:47:44 -0800232
233 void
234 inivationRejection(const QString& msg);
Yingdi Yu8e135832013-11-09 20:12:31 -0800235
Yingdi Yu7989eb22013-10-31 17:38:22 -0700236public slots:
237 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800238 processTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700239
240 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800241 processData(ndn::shared_ptr<const ndn::Data> data, bool show, bool isHistory);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700242
243 void
244 processRemove(QString prefix);
245
246private slots:
247 void
248 returnPressed();
249
250 void
251 treeButtonPressed();
252
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800253 void
254 settingUpdated(QString, QString, QString);
255
Yingdi Yu7989eb22013-10-31 17:38:22 -0700256 void
257 sendJoin();
258
259 void
260 sendHello();
261
262 void
263 sendLeave();
264
265 void
266 replot();
267
268 void
269 updateRosterList(QStringList);
270
271 void
272 enableTreeDisplay();
273
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800274 void
275 updateLocalPrefix();
276
Yingdi Yu7989eb22013-10-31 17:38:22 -0700277 void
278 summonReaper();
279
280 void
281 reap();
282
283 void
Yingdi Yu83eae842013-11-06 22:07:38 -0800284 iconActivated(QSystemTrayIcon::ActivationReason reason);
285
286 void
287 messageClicked();
288
289 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700290 showMessage(QString, QString);
291
292 void
293 openInviteListDialog();
294
295 void
296 sendInvitationWrapper(QString, bool);
Yingdi Yu702d6f12013-11-07 17:00:54 -0800297
298 void
299 quit();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700300
301private:
Yingdi Yu5b989132013-10-23 14:03:09 -0700302 Ui::ChatDialog *ui;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800303 ndn::shared_ptr<chronos::ContactManager> m_contactManager;
304 ndn::shared_ptr<ndn::Face> m_face;
305 ndn::shared_ptr<boost::asio::io_service> m_ioService;
306
Yingdi Yu5b989132013-10-23 14:03:09 -0700307 ndn::Name m_chatroomPrefix;
308 ndn::Name m_localPrefix;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700309 ndn::Name m_localChatPrefix;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700310 ndn::Name m_defaultIdentity;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800311 User m_user;
312 std::string m_nick;
Yingdi Yua66183f2013-11-10 10:32:18 -0800313
Yingdi Yufa4ce792014-02-06 18:09:22 -0800314 ndn::Scheduler m_scheduler;
315 ndn::EventId m_replotEventId;
316
317#ifndef WITH_SECURITY
318 ndn::shared_ptr<ndn::Validator> m_invitationValidator;
319 ndn::shared_ptr<ndn::Validator> m_syncValidator;
320#else
321 ndn::shared_ptr<chronos::ValidatorInvitation> m_invitationValidator;
322 ndn::shared_ptr<Sync::ValidatorSync> m_syncValidator;
323#endif
324 ndn::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800325
Yingdi Yua66183f2013-11-10 10:32:18 -0800326 ndn::Name m_newLocalPrefix;
327 bool m_newLocalPrefixReady;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700328
Yingdi Yufa4ce792014-02-06 18:09:22 -0800329
Yingdi Yu7989eb22013-10-31 17:38:22 -0700330 Sync::SyncSocket *m_sock;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800331 uint64_t m_session;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700332 DigestTreeScene *m_scene;
333 boost::recursive_mutex m_msgMutex;
334 boost::recursive_mutex m_sceneMutex;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800335 int64_t m_lastMsgTime;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700336 int m_randomizedInterval;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800337
338
Yingdi Yu7989eb22013-10-31 17:38:22 -0700339 QStringListModel *m_rosterModel;
Yingdi Yu83eae842013-11-06 22:07:38 -0800340 QSystemTrayIcon *trayIcon;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700341
Yingdi Yu702d6f12013-11-07 17:00:54 -0800342 QAction *minimizeAction;
343 QAction *maximizeAction;
344 QAction *restoreAction;
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800345 QAction *updateLocalPrefixAction;
Yingdi Yu702d6f12013-11-07 17:00:54 -0800346 QAction *quitAction;
347 QMenu *trayIconMenu;
348
Yingdi Yu7989eb22013-10-31 17:38:22 -0700349 bool m_joined;
350
351 QList<QString> m_zombieList;
352 int m_zombieIndex;
353
354 InviteListDialog* m_inviteListDialog;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700355};
Yingdi Yu5b989132013-10-23 14:03:09 -0700356
357#endif // ChatDIALOG_H