blob: a4b168fd4c383d389e44842d96e2f9bdee8779e4 [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 Yu64206112013-12-24 11:16:32 +080026#include <ndn-cpp/data.hpp>
27#include <ndn-cpp/face.hpp>
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080028#include <ndn-cpp/security/key-chain.hpp>
Yingdi Yu42f66462013-10-31 17:38:22 -070029#include "invitation-policy-manager.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,
151 int stepCount,
152 const ndn::OnVerified& onVerified,
153 const ndn::OnVerifyFailed& onVerifyFailed,
154 const OnEventualTimeout& timeoutNotify,
155 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
156
157 void
158 onTargetTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
159 int retry,
160 int stepCount,
161 const ndn::OnVerified& onVerified,
162 const ndn::OnVerifyFailed& onVerifyFailed,
163 const OnEventualTimeout& timeoutNotify,
164 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
165
166 void
167 onCertData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
168 const ndn::ptr_lib::shared_ptr<ndn::Data>& cert,
169 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep,
170 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
171
172 void
173 onCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
174 const ndn::OnVerifyFailed& onVerifyFailed,
175 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
176 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep,
177 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
178
179 void
180 sendInterest(const ndn::Interest& interest,
181 const ndn::OnVerified& onVerified,
182 const ndn::OnVerifyFailed& onVerifyFailed,
183 const OnEventualTimeout& timeoutNotify,
184 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager,
185 int retry = 1,
186 int stepCount = 0);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700187
188 void
Yingdi Yu64206112013-12-24 11:16:32 +0800189 onInviteReplyVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
190 const ndn::Name& identity,
191 bool isIntroduce);
192
193 void
194 onInviteReplyVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
195 const ndn::Name& identity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700196
197 void
Yingdi Yu64206112013-12-24 11:16:32 +0800198 onInviteReplyTimeout(const ndn::Name& identity);
199
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700200
201 void
202 invitationRejected(const ndn::Name& identity);
203
204 void
Yingdi Yu64206112013-12-24 11:16:32 +0800205 invitationAccepted(const ndn::Name& identity,
206 ndn::ptr_lib::shared_ptr<ndn::Data> data,
207 const std::string& inviteePrefix,
Yingdi Yu42f66462013-10-31 17:38:22 -0700208 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700209
210 void
Yingdi Yu64206112013-12-24 11:16:32 +0800211 onLocalPrefix(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
212 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700213
Yingdi Yu42372442013-11-06 18:43:31 -0800214 void
Yingdi Yu64206112013-12-24 11:16:32 +0800215 onLocalPrefixTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700216
217 // void
218 // fetchHistory(std::string name);
219
220 void
221 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
222
223 void
224 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
225
226 void
227 sendMsg(SyncDemo::ChatMessage &msg);
228
229 void
230 resizeEvent(QResizeEvent *);
231
232 void
233 showEvent(QShowEvent *);
234
235 void
236 fitView();
237
Yingdi Yua0594092013-11-06 22:07:38 -0800238 void
239 createActions();
240
241 void
242 createTrayIcon();
243
Yingdi Yu42f66462013-10-31 17:38:22 -0700244 QString
245 formatTime(time_t);
246
247 void
248 printTimeInCell(QTextTable *, time_t);
249
250 void
251 disableTreeDisplay();
252
253signals:
254 void
255 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
256
257 void
258 treeUpdated(const std::vector<Sync::MissingDataInfo>);
259
260 void
261 removeReceived(QString prefix);
262
Yingdi Yu42372442013-11-06 18:43:31 -0800263 void
264 closeChatDialog(const ndn::Name& chatroomPrefix);
265
Yingdi Yub29f78c2013-11-09 20:12:31 -0800266 void
267 noNdnConnection(const QString& msg);
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800268
269 void
270 inivationRejection(const QString& msg);
Yingdi Yub29f78c2013-11-09 20:12:31 -0800271
Yingdi Yu42f66462013-10-31 17:38:22 -0700272public slots:
273 void
274 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
275
276 void
277 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
278
279 void
280 processRemove(QString prefix);
281
282private slots:
283 void
284 returnPressed();
285
286 void
287 treeButtonPressed();
288
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800289 void
290 settingUpdated(QString, QString, QString);
291
Yingdi Yu42f66462013-10-31 17:38:22 -0700292 void
293 sendJoin();
294
295 void
296 sendHello();
297
298 void
299 sendLeave();
300
301 void
302 replot();
303
304 void
305 updateRosterList(QStringList);
306
307 void
308 enableTreeDisplay();
309
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800310 void
311 updateLocalPrefix();
312
Yingdi Yu42f66462013-10-31 17:38:22 -0700313 void
314 summonReaper();
315
316 void
317 reap();
318
319 void
Yingdi Yua0594092013-11-06 22:07:38 -0800320 iconActivated(QSystemTrayIcon::ActivationReason reason);
321
322 void
323 messageClicked();
324
325 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700326 showMessage(QString, QString);
327
328 void
329 openInviteListDialog();
330
331 void
332 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800333
334 void
335 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700336
337private:
Yingdi Yu04842232013-10-23 14:03:09 -0700338 Ui::ChatDialog *ui;
Yingdi Yu64206112013-12-24 11:16:32 +0800339 ndn::ptr_lib::shared_ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700340 ndn::Name m_chatroomPrefix;
341 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700342 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700343 ndn::Name m_defaultIdentity;
Yingdi Yu64206112013-12-24 11:16:32 +0800344 ndn::ptr_lib::shared_ptr<InvitationPolicyManager> m_invitationPolicyManager;
345 ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800346 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu64206112013-12-24 11:16:32 +0800347 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800348
Yingdi Yuaccbda92013-12-27 08:44:12 +0800349 boost::recursive_mutex m_mutex;
350 boost::thread m_thread;
351 bool m_running;
352
Yingdi Yu0a953c32013-11-10 10:32:18 -0800353 ndn::Name m_newLocalPrefix;
354 bool m_newLocalPrefixReady;
Yingdi Yu42f66462013-10-31 17:38:22 -0700355
356 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800357 std::string m_nick;
Yingdi Yu42f66462013-10-31 17:38:22 -0700358 Sync::SyncSocket *m_sock;
359 uint32_t m_session;
360 DigestTreeScene *m_scene;
361 boost::recursive_mutex m_msgMutex;
362 boost::recursive_mutex m_sceneMutex;
363 time_t m_lastMsgTime;
364 int m_randomizedInterval;
365 QTimer *m_timer;
366 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800367 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700368
Yingdi Yu07b5b092013-11-07 17:00:54 -0800369 QAction *minimizeAction;
370 QAction *maximizeAction;
371 QAction *restoreAction;
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800372 QAction *updateLocalPrefixAction;
Yingdi Yu07b5b092013-11-07 17:00:54 -0800373 QAction *quitAction;
374 QMenu *trayIconMenu;
375
Yingdi Yu42f66462013-10-31 17:38:22 -0700376 // QQueue<SyncDemo::ChatMessage> m_history;
377 // bool m_historyInitialized;
378 bool m_joined;
379
380 QList<QString> m_zombieList;
381 int m_zombieIndex;
382
383 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700384};
385
386#endif // ChatDIALOG_H