blob: 4dc50614a9e9e7156b6e102592c77ca32aa0e4a5 [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 Yu6df61252014-01-21 11:05:11 -080026#include <ndn-cpp-dev/data.hpp>
27#include <ndn-cpp-dev/face.hpp>
28#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yueaa84e22014-01-16 10:30:26 -080029#include "sec-policy-chrono-chat-invitation.h"
Yingdi Yuc4d08d22013-10-23 23:07:29 -070030#include "contact-item.h"
Yingdi Yu42f66462013-10-31 17:38:22 -070031
Alexander Afanasyev4f04c552013-11-07 15:30:05 -080032#include <sync-socket.h>
Yingdi Yu42f66462013-10-31 17:38:22 -070033#include <sync-seq-no.h>
34#include "chatbuf.pb.h"
35#include "digesttreescene.h"
Yingdi Yuaccbda92013-12-27 08:44:12 +080036
37#include <boost/thread/locks.hpp>
38#include <boost/thread/recursive_mutex.hpp>
39#include <boost/thread/thread.hpp>
Yingdi Yu04842232013-10-23 14:03:09 -070040#endif
41
Yingdi Yu64206112013-12-24 11:16:32 +080042typedef ndn::func_lib::function<void()> OnEventualTimeout;
43
Yingdi Yu42f66462013-10-31 17:38:22 -070044#define MAX_HISTORY_ENTRY 20
45
Yingdi Yu04842232013-10-23 14:03:09 -070046namespace Ui {
47class ChatDialog;
48}
49
50class ChatDialog : public QDialog
51{
52 Q_OBJECT
53
54public:
Yingdi Yu64206112013-12-24 11:16:32 +080055 explicit ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,
Yingdi Yu42f66462013-10-31 17:38:22 -070056 const ndn::Name& chatroomPrefix,
Yingdi Yu04842232013-10-23 14:03:09 -070057 const ndn::Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070058 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080059 const std::string& nick,
60 bool trial = false,
Yingdi Yu04842232013-10-23 14:03:09 -070061 QWidget *parent = 0);
Yingdi Yu42f66462013-10-31 17:38:22 -070062
63 // explicit ChatDialog(const ndn::Name& chatroomPrefix,
64 // const ndn::Name& localPrefix,
65 // const ndn::Name& defaultIdentity,
66 // const ndn::security::IdentityCertificate& identityCertificate,
67 // QWidget *parent = 0);
68
Yingdi Yu04842232013-10-23 14:03:09 -070069 ~ChatDialog();
70
71 const ndn::Name&
72 getChatroomPrefix() const
73 { return m_chatroomPrefix; }
74
75 const ndn::Name&
76 getLocalPrefix() const
77 { return m_localPrefix; }
78
79 void
Yingdi Yu64206112013-12-24 11:16:32 +080080 sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool isIntroducer);
Yingdi Yu42f66462013-10-31 17:38:22 -070081
82 void
83 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
84
85 void
86 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu64206112013-12-24 11:16:32 +080087 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu42f66462013-10-31 17:38:22 -070088 bool isIntroducer);
89
90 void
91 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
92
93 void
94 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
95
96 void
Yingdi Yu64206112013-12-24 11:16:32 +080097 processDataWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -070098
99 void
Yingdi Yu64206112013-12-24 11:16:32 +0800100 processDataNoShowWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700101
102 void
103 processRemoveWrapper(std::string);
Yingdi Yu04842232013-10-23 14:03:09 -0700104
Yingdi Yua0594092013-11-06 22:07:38 -0800105 void
Yingdi Yu64206112013-12-24 11:16:32 +0800106 publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer);
Yingdi Yua0594092013-11-06 22:07:38 -0800107
Yingdi Yu42372442013-11-06 18:43:31 -0800108protected:
Yingdi Yua0594092013-11-06 22:07:38 -0800109 void
110 closeEvent(QCloseEvent *e);
111
112 void
113 changeEvent(QEvent *e);
Yingdi Yu42372442013-11-06 18:43:31 -0800114
Yingdi Yu04842232013-10-23 14:03:09 -0700115private:
Yingdi Yu42f66462013-10-31 17:38:22 -0700116
Yingdi Yuaccbda92013-12-27 08:44:12 +0800117 void
118 startFace();
119
120 void
121 shutdownFace();
122
123 void
124 eventLoop();
125
Yingdi Yu42f66462013-10-31 17:38:22 -0700126 void
Yingdi Yu64206112013-12-24 11:16:32 +0800127 connectToDaemon();
128
129 void
130 onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
131 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
132
133 void
134 onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
135
136 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700137 initializeSetting();
138
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800139 QString
140 getRandomString();
141
Yingdi Yu42f66462013-10-31 17:38:22 -0700142 void
143 updateLabels();
144
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700145 void
Yingdi Yu64206112013-12-24 11:16:32 +0800146 initializeSync();
Yingdi Yu42f66462013-10-31 17:38:22 -0700147
148 void
Yingdi Yu64206112013-12-24 11:16:32 +0800149 onTargetData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
150 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
Yingdi Yu64206112013-12-24 11:16:32 +0800151 const ndn::OnVerified& onVerified,
Yingdi Yu7630f642014-01-16 19:13:03 -0800152 const ndn::OnVerifyFailed& onVerifyFailed);
Yingdi Yu64206112013-12-24 11:16:32 +0800153
154 void
155 onTargetTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
156 int retry,
Yingdi Yu64206112013-12-24 11:16:32 +0800157 const ndn::OnVerified& onVerified,
158 const ndn::OnVerifyFailed& onVerifyFailed,
Yingdi Yu7630f642014-01-16 19:13:03 -0800159 const OnEventualTimeout& timeoutNotify);
Yingdi Yu64206112013-12-24 11:16:32 +0800160
161 void
162 sendInterest(const ndn::Interest& interest,
163 const ndn::OnVerified& onVerified,
164 const ndn::OnVerifyFailed& onVerifyFailed,
165 const OnEventualTimeout& timeoutNotify,
Yingdi Yu7630f642014-01-16 19:13:03 -0800166 int retry = 1);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700167
168 void
Yingdi Yu64206112013-12-24 11:16:32 +0800169 onInviteReplyVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
170 const ndn::Name& identity,
171 bool isIntroduce);
172
173 void
174 onInviteReplyVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
175 const ndn::Name& identity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700176
177 void
Yingdi Yu64206112013-12-24 11:16:32 +0800178 onInviteReplyTimeout(const ndn::Name& identity);
179
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700180
181 void
182 invitationRejected(const ndn::Name& identity);
183
184 void
Yingdi Yu64206112013-12-24 11:16:32 +0800185 invitationAccepted(const ndn::Name& identity,
186 ndn::ptr_lib::shared_ptr<ndn::Data> data,
187 const std::string& inviteePrefix,
Yingdi Yu42f66462013-10-31 17:38:22 -0700188 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700189
190 void
Yingdi Yu64206112013-12-24 11:16:32 +0800191 onLocalPrefix(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
192 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700193
Yingdi Yu42372442013-11-06 18:43:31 -0800194 void
Yingdi Yu64206112013-12-24 11:16:32 +0800195 onLocalPrefixTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700196
197 // void
198 // fetchHistory(std::string name);
199
200 void
201 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
202
203 void
204 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
205
206 void
207 sendMsg(SyncDemo::ChatMessage &msg);
208
209 void
210 resizeEvent(QResizeEvent *);
211
212 void
213 showEvent(QShowEvent *);
214
215 void
216 fitView();
217
Yingdi Yua0594092013-11-06 22:07:38 -0800218 void
219 createActions();
220
221 void
222 createTrayIcon();
223
Yingdi Yu42f66462013-10-31 17:38:22 -0700224 QString
225 formatTime(time_t);
226
227 void
228 printTimeInCell(QTextTable *, time_t);
229
230 void
231 disableTreeDisplay();
232
233signals:
234 void
235 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
236
237 void
238 treeUpdated(const std::vector<Sync::MissingDataInfo>);
239
240 void
241 removeReceived(QString prefix);
242
Yingdi Yu42372442013-11-06 18:43:31 -0800243 void
244 closeChatDialog(const ndn::Name& chatroomPrefix);
245
Yingdi Yub29f78c2013-11-09 20:12:31 -0800246 void
247 noNdnConnection(const QString& msg);
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800248
249 void
250 inivationRejection(const QString& msg);
Yingdi Yub29f78c2013-11-09 20:12:31 -0800251
Yingdi Yu42f66462013-10-31 17:38:22 -0700252public slots:
253 void
254 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
255
256 void
257 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
258
259 void
260 processRemove(QString prefix);
261
262private slots:
263 void
264 returnPressed();
265
266 void
267 treeButtonPressed();
268
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800269 void
270 settingUpdated(QString, QString, QString);
271
Yingdi Yu42f66462013-10-31 17:38:22 -0700272 void
273 sendJoin();
274
275 void
276 sendHello();
277
278 void
279 sendLeave();
280
281 void
282 replot();
283
284 void
285 updateRosterList(QStringList);
286
287 void
288 enableTreeDisplay();
289
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800290 void
291 updateLocalPrefix();
292
Yingdi Yu42f66462013-10-31 17:38:22 -0700293 void
294 summonReaper();
295
296 void
297 reap();
298
299 void
Yingdi Yua0594092013-11-06 22:07:38 -0800300 iconActivated(QSystemTrayIcon::ActivationReason reason);
301
302 void
303 messageClicked();
304
305 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700306 showMessage(QString, QString);
307
308 void
309 openInviteListDialog();
310
311 void
312 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800313
314 void
315 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700316
317private:
Yingdi Yu04842232013-10-23 14:03:09 -0700318 Ui::ChatDialog *ui;
Yingdi Yu64206112013-12-24 11:16:32 +0800319 ndn::ptr_lib::shared_ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700320 ndn::Name m_chatroomPrefix;
321 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700322 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700323 ndn::Name m_defaultIdentity;
Yingdi Yueaa84e22014-01-16 10:30:26 -0800324 ndn::ptr_lib::shared_ptr<SecPolicyChronoChatInvitation> m_invitationPolicy;
325 ndn::ptr_lib::shared_ptr<SecPolicySync> m_syncPolicy;
Yingdi Yu7630f642014-01-16 19:13:03 -0800326 ndn::ptr_lib::shared_ptr<ndn::Verifier> m_verifier;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800327 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu64206112013-12-24 11:16:32 +0800328 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800329
Yingdi Yuaccbda92013-12-27 08:44:12 +0800330 boost::recursive_mutex m_mutex;
331 boost::thread m_thread;
332 bool m_running;
333
Yingdi Yu0a953c32013-11-10 10:32:18 -0800334 ndn::Name m_newLocalPrefix;
335 bool m_newLocalPrefixReady;
Yingdi Yu42f66462013-10-31 17:38:22 -0700336
337 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800338 std::string m_nick;
Yingdi Yu42f66462013-10-31 17:38:22 -0700339 Sync::SyncSocket *m_sock;
340 uint32_t m_session;
341 DigestTreeScene *m_scene;
342 boost::recursive_mutex m_msgMutex;
343 boost::recursive_mutex m_sceneMutex;
344 time_t m_lastMsgTime;
345 int m_randomizedInterval;
346 QTimer *m_timer;
347 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800348 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700349
Yingdi Yu07b5b092013-11-07 17:00:54 -0800350 QAction *minimizeAction;
351 QAction *maximizeAction;
352 QAction *restoreAction;
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800353 QAction *updateLocalPrefixAction;
Yingdi Yu07b5b092013-11-07 17:00:54 -0800354 QAction *quitAction;
355 QMenu *trayIconMenu;
356
Yingdi Yu42f66462013-10-31 17:38:22 -0700357 // QQueue<SyncDemo::ChatMessage> m_history;
358 // bool m_historyInitialized;
359 bool m_joined;
360
361 QList<QString> m_zombieList;
362 int m_zombieIndex;
363
364 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700365};
366
367#endif // ChatDIALOG_H