blob: 742b4ee696e5ed548ca58232b47562ff16e83df4 [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#ifndef CHATDIALOG_H
14#define CHATDIALOG_H
15
16#include <QDialog>
Yingdi Yu42f66462013-10-31 17:38:22 -070017#include <QTextTable>
18#include <QStringListModel>
19#include <QTimer>
Yingdi Yua0594092013-11-06 22:07:38 -080020#include <QSystemTrayIcon>
Yingdi Yu07b5b092013-11-07 17:00:54 -080021#include <QMenu>
Yingdi Yu42f66462013-10-31 17:38:22 -070022
23#include "invitelistdialog.h"
Yingdi Yu04842232013-10-23 14:03:09 -070024
25#ifndef Q_MOC_RUN
Yingdi Yuc4d08d22013-10-23 23:07:29 -070026#include "contact-item.h"
Yingdi Yu42f66462013-10-31 17:38:22 -070027#include "chatbuf.pb.h"
28#include "digesttreescene.h"
Yingdi Yua1a688f2014-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 Yuaccbda92013-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>
Yingdi Yu04842232013-10-23 14:03:09 -070042#endif
43
Yingdi Yua1a688f2014-02-06 18:09:22 -080044typedef ndn::function<void()> OnEventualTimeout;
Yingdi Yu64206112013-12-24 11:16:32 +080045
Yingdi Yu42f66462013-10-31 17:38:22 -070046#define MAX_HISTORY_ENTRY 20
47
Yingdi Yu04842232013-10-23 14:03:09 -070048namespace Ui {
49class ChatDialog;
50}
51
52class ChatDialog : public QDialog
53{
54 Q_OBJECT
55
56public:
Yingdi Yua1a688f2014-02-06 18:09:22 -080057 explicit ChatDialog(ndn::shared_ptr<chronos::ContactManager> contactManager,
58 ndn::shared_ptr<ndn::Face> face,
Yingdi Yu42f66462013-10-31 17:38:22 -070059 const ndn::Name& chatroomPrefix,
Yingdi Yu04842232013-10-23 14:03:09 -070060 const ndn::Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070061 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080062 const std::string& nick,
Yingdi Yu04842232013-10-23 14:03:09 -070063 QWidget *parent = 0);
Yingdi Yu42f66462013-10-31 17:38:22 -070064
Yingdi Yu04842232013-10-23 14:03:09 -070065 ~ChatDialog();
66
67 const ndn::Name&
68 getChatroomPrefix() const
69 { return m_chatroomPrefix; }
70
71 const ndn::Name&
72 getLocalPrefix() const
73 { return m_localPrefix; }
74
75 void
Yingdi Yua1a688f2014-02-06 18:09:22 -080076 sendInvitation(ndn::shared_ptr<chronos::ContactItem> contact, bool isIntroducer);
Yingdi Yu42f66462013-10-31 17:38:22 -070077
78 void
79 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu64206112013-12-24 11:16:32 +080080 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu42f66462013-10-31 17:38:22 -070081 bool isIntroducer);
82
Yingdi Yua1a688f2014-02-06 18:09:22 -080083 void
84 addTrustAnchor(const chronos::EndorseCertificate& selfEndorseCertificate);
85
Yingdi Yu42f66462013-10-31 17:38:22 -070086 void
87 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
88
89 void
Yingdi Yua1a688f2014-02-06 18:09:22 -080090 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
Yingdi Yu42f66462013-10-31 17:38:22 -070091
92 void
Yingdi Yua1a688f2014-02-06 18:09:22 -080093 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -070094
95 void
Yingdi Yua1a688f2014-02-06 18:09:22 -080096 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -070097
98 void
99 processRemoveWrapper(std::string);
Yingdi Yu04842232013-10-23 14:03:09 -0700100
Yingdi Yua0594092013-11-06 22:07:38 -0800101 void
Yingdi Yu64206112013-12-24 11:16:32 +0800102 publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer);
Yingdi Yua0594092013-11-06 22:07:38 -0800103
Yingdi Yu42372442013-11-06 18:43:31 -0800104protected:
Yingdi Yua0594092013-11-06 22:07:38 -0800105 void
106 closeEvent(QCloseEvent *e);
107
108 void
109 changeEvent(QEvent *e);
Yingdi Yu42372442013-11-06 18:43:31 -0800110
Yingdi Yu04842232013-10-23 14:03:09 -0700111private:
Yingdi Yu42f66462013-10-31 17:38:22 -0700112
Yingdi Yu64206112013-12-24 11:16:32 +0800113 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700114 initializeSetting();
115
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800116 QString
117 getRandomString();
118
Yingdi Yu42f66462013-10-31 17:38:22 -0700119 void
120 updateLabels();
121
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700122 void
Yingdi Yu64206112013-12-24 11:16:32 +0800123 initializeSync();
Yingdi Yu42f66462013-10-31 17:38:22 -0700124
125 void
Yingdi Yua1a688f2014-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 Yu64206112013-12-24 11:16:32 +0800131
132 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800133 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu64206112013-12-24 11:16:32 +0800134 int retry,
Yingdi Yua1a688f2014-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 Yu64206112013-12-24 11:16:32 +0800144
145 void
146 sendInterest(const ndn::Interest& interest,
Yingdi Yua1a688f2014-02-06 18:09:22 -0800147 const ndn::OnDataValidated& onValidated,
148 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu64206112013-12-24 11:16:32 +0800149 const OnEventualTimeout& timeoutNotify,
Yingdi Yu7630f642014-01-16 19:13:03 -0800150 int retry = 1);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700151
152 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800153 onInviteReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
154 const ndn::Name& identity,
155 bool isIntroduce);
Yingdi Yu64206112013-12-24 11:16:32 +0800156
157 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800158 onInviteReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
Yingdi Yu64206112013-12-24 11:16:32 +0800159 const ndn::Name& identity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700160
161 void
Yingdi Yu64206112013-12-24 11:16:32 +0800162 onInviteReplyTimeout(const ndn::Name& identity);
163
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700164
165 void
166 invitationRejected(const ndn::Name& identity);
167
168 void
Yingdi Yu64206112013-12-24 11:16:32 +0800169 invitationAccepted(const ndn::Name& identity,
Yingdi Yua1a688f2014-02-06 18:09:22 -0800170 ndn::shared_ptr<const ndn::Data> data,
Yingdi Yu64206112013-12-24 11:16:32 +0800171 const std::string& inviteePrefix,
Yingdi Yu42f66462013-10-31 17:38:22 -0700172 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700173
174 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800175 onLocalPrefix(const ndn::Interest& interest,
176 ndn::Data& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700177
Yingdi Yu42372442013-11-06 18:43:31 -0800178 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800179 onLocalPrefixTimeout(const ndn::Interest& interest);
Yingdi Yu42f66462013-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 Yua0594092013-11-06 22:07:38 -0800202 void
203 createActions();
204
205 void
206 createTrayIcon();
207
Yingdi Yu42f66462013-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 Yua1a688f2014-02-06 18:09:22 -0800217signals:
218 void
219 dataReceived(ndn::shared_ptr<const ndn::Data> data, bool show, bool isHistory);
220
Yingdi Yu42f66462013-10-31 17:38:22 -0700221 void
222 treeUpdated(const std::vector<Sync::MissingDataInfo>);
223
224 void
225 removeReceived(QString prefix);
226
Yingdi Yu42372442013-11-06 18:43:31 -0800227 void
228 closeChatDialog(const ndn::Name& chatroomPrefix);
229
Yingdi Yub29f78c2013-11-09 20:12:31 -0800230 void
231 noNdnConnection(const QString& msg);
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800232
233 void
234 inivationRejection(const QString& msg);
Yingdi Yub29f78c2013-11-09 20:12:31 -0800235
Yingdi Yu42f66462013-10-31 17:38:22 -0700236public slots:
237 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800238 processTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
Yingdi Yu42f66462013-10-31 17:38:22 -0700239
240 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800241 processData(ndn::shared_ptr<const ndn::Data> data, bool show, bool isHistory);
Yingdi Yu42f66462013-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 Yu2ab22e72013-11-10 01:38:21 -0800253 void
254 settingUpdated(QString, QString, QString);
255
Yingdi Yu42f66462013-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 Yu2ab22e72013-11-10 01:38:21 -0800274 void
275 updateLocalPrefix();
276
Yingdi Yu42f66462013-10-31 17:38:22 -0700277 void
278 summonReaper();
279
280 void
281 reap();
282
283 void
Yingdi Yua0594092013-11-06 22:07:38 -0800284 iconActivated(QSystemTrayIcon::ActivationReason reason);
285
286 void
287 messageClicked();
288
289 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700290 showMessage(QString, QString);
291
292 void
293 openInviteListDialog();
294
295 void
296 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800297
298 void
299 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700300
301private:
Yingdi Yu04842232013-10-23 14:03:09 -0700302 Ui::ChatDialog *ui;
Yingdi Yua1a688f2014-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 Yu04842232013-10-23 14:03:09 -0700307 ndn::Name m_chatroomPrefix;
308 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700309 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700310 ndn::Name m_defaultIdentity;
Yingdi Yua1a688f2014-02-06 18:09:22 -0800311 User m_user;
312 std::string m_nick;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800313
Yingdi Yua1a688f2014-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 Yuaccbda92013-12-27 08:44:12 +0800325
Yingdi Yu0a953c32013-11-10 10:32:18 -0800326 ndn::Name m_newLocalPrefix;
327 bool m_newLocalPrefixReady;
Yingdi Yu42f66462013-10-31 17:38:22 -0700328
Yingdi Yua1a688f2014-02-06 18:09:22 -0800329
Yingdi Yu42f66462013-10-31 17:38:22 -0700330 Sync::SyncSocket *m_sock;
Yingdi Yua1a688f2014-02-06 18:09:22 -0800331 uint64_t m_session;
Yingdi Yu42f66462013-10-31 17:38:22 -0700332 DigestTreeScene *m_scene;
333 boost::recursive_mutex m_msgMutex;
334 boost::recursive_mutex m_sceneMutex;
Yingdi Yua1a688f2014-02-06 18:09:22 -0800335 int64_t m_lastMsgTime;
Yingdi Yu42f66462013-10-31 17:38:22 -0700336 int m_randomizedInterval;
Yingdi Yua1a688f2014-02-06 18:09:22 -0800337
338
Yingdi Yu42f66462013-10-31 17:38:22 -0700339 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800340 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700341
Yingdi Yu07b5b092013-11-07 17:00:54 -0800342 QAction *minimizeAction;
343 QAction *maximizeAction;
344 QAction *restoreAction;
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800345 QAction *updateLocalPrefixAction;
Yingdi Yu07b5b092013-11-07 17:00:54 -0800346 QAction *quitAction;
347 QMenu *trayIconMenu;
348
Yingdi Yu42f66462013-10-31 17:38:22 -0700349 bool m_joined;
350
351 QList<QString> m_zombieList;
352 int m_zombieIndex;
353
354 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700355};
356
357#endif // ChatDIALOG_H