blob: be2c89b407350f20342ca990842e17c6089fa263 [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>
28#include <ndn-cpp/security/identity/identity-manager.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 Yuaccbda92013-12-27 08:44:12 +080056 ndn::ptr_lib::shared_ptr<ndn::IdentityManager> identityManager,
Yingdi Yu42f66462013-10-31 17:38:22 -070057 const ndn::Name& chatroomPrefix,
Yingdi Yu04842232013-10-23 14:03:09 -070058 const ndn::Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070059 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080060 const std::string& nick,
61 bool trial = false,
Yingdi Yu04842232013-10-23 14:03:09 -070062 QWidget *parent = 0);
Yingdi Yu42f66462013-10-31 17:38:22 -070063
64 // explicit ChatDialog(const ndn::Name& chatroomPrefix,
65 // const ndn::Name& localPrefix,
66 // const ndn::Name& defaultIdentity,
67 // const ndn::security::IdentityCertificate& identityCertificate,
68 // QWidget *parent = 0);
69
Yingdi Yu04842232013-10-23 14:03:09 -070070 ~ChatDialog();
71
72 const ndn::Name&
73 getChatroomPrefix() const
74 { return m_chatroomPrefix; }
75
76 const ndn::Name&
77 getLocalPrefix() const
78 { return m_localPrefix; }
79
80 void
Yingdi Yu64206112013-12-24 11:16:32 +080081 sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool isIntroducer);
Yingdi Yu42f66462013-10-31 17:38:22 -070082
83 void
84 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
85
86 void
87 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu64206112013-12-24 11:16:32 +080088 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu42f66462013-10-31 17:38:22 -070089 bool isIntroducer);
90
91 void
92 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
93
94 void
95 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
96
97 void
Yingdi Yu64206112013-12-24 11:16:32 +080098 processDataWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -070099
100 void
Yingdi Yu64206112013-12-24 11:16:32 +0800101 processDataNoShowWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700102
103 void
104 processRemoveWrapper(std::string);
Yingdi Yu04842232013-10-23 14:03:09 -0700105
Yingdi Yua0594092013-11-06 22:07:38 -0800106 void
Yingdi Yu64206112013-12-24 11:16:32 +0800107 publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer);
Yingdi Yua0594092013-11-06 22:07:38 -0800108
Yingdi Yu42372442013-11-06 18:43:31 -0800109protected:
Yingdi Yua0594092013-11-06 22:07:38 -0800110 void
111 closeEvent(QCloseEvent *e);
112
113 void
114 changeEvent(QEvent *e);
Yingdi Yu42372442013-11-06 18:43:31 -0800115
Yingdi Yu04842232013-10-23 14:03:09 -0700116private:
Yingdi Yu42f66462013-10-31 17:38:22 -0700117
Yingdi Yuaccbda92013-12-27 08:44:12 +0800118 void
119 startFace();
120
121 void
122 shutdownFace();
123
124 void
125 eventLoop();
126
Yingdi Yu42f66462013-10-31 17:38:22 -0700127 void
Yingdi Yu64206112013-12-24 11:16:32 +0800128 connectToDaemon();
129
130 void
131 onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
132 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
133
134 void
135 onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
136
137 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700138 initializeSetting();
139
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800140 QString
141 getRandomString();
142
Yingdi Yu42f66462013-10-31 17:38:22 -0700143 void
144 updateLabels();
145
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700146 void
Yingdi Yu64206112013-12-24 11:16:32 +0800147 initializeSync();
Yingdi Yu42f66462013-10-31 17:38:22 -0700148
149 void
Yingdi Yu64206112013-12-24 11:16:32 +0800150 onTargetData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
151 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
152 int stepCount,
153 const ndn::OnVerified& onVerified,
154 const ndn::OnVerifyFailed& onVerifyFailed,
155 const OnEventualTimeout& timeoutNotify,
156 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
157
158 void
159 onTargetTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
160 int retry,
161 int stepCount,
162 const ndn::OnVerified& onVerified,
163 const ndn::OnVerifyFailed& onVerifyFailed,
164 const OnEventualTimeout& timeoutNotify,
165 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
166
167 void
168 onCertData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
169 const ndn::ptr_lib::shared_ptr<ndn::Data>& cert,
170 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep,
171 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
172
173 void
174 onCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
175 const ndn::OnVerifyFailed& onVerifyFailed,
176 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
177 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep,
178 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
179
180 void
181 sendInterest(const ndn::Interest& interest,
182 const ndn::OnVerified& onVerified,
183 const ndn::OnVerifyFailed& onVerifyFailed,
184 const OnEventualTimeout& timeoutNotify,
185 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager,
186 int retry = 1,
187 int stepCount = 0);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700188
189 void
Yingdi Yu64206112013-12-24 11:16:32 +0800190 onInviteReplyVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
191 const ndn::Name& identity,
192 bool isIntroduce);
193
194 void
195 onInviteReplyVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
196 const ndn::Name& identity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700197
198 void
Yingdi Yu64206112013-12-24 11:16:32 +0800199 onInviteReplyTimeout(const ndn::Name& identity);
200
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700201
202 void
203 invitationRejected(const ndn::Name& identity);
204
205 void
Yingdi Yu64206112013-12-24 11:16:32 +0800206 invitationAccepted(const ndn::Name& identity,
207 ndn::ptr_lib::shared_ptr<ndn::Data> data,
208 const std::string& inviteePrefix,
Yingdi Yu42f66462013-10-31 17:38:22 -0700209 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700210
211 void
Yingdi Yu64206112013-12-24 11:16:32 +0800212 onLocalPrefix(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
213 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700214
Yingdi Yu42372442013-11-06 18:43:31 -0800215 void
Yingdi Yu64206112013-12-24 11:16:32 +0800216 onLocalPrefixTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700217
218 // void
219 // fetchHistory(std::string name);
220
221 void
222 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
223
224 void
225 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
226
227 void
228 sendMsg(SyncDemo::ChatMessage &msg);
229
230 void
231 resizeEvent(QResizeEvent *);
232
233 void
234 showEvent(QShowEvent *);
235
236 void
237 fitView();
238
Yingdi Yua0594092013-11-06 22:07:38 -0800239 void
240 createActions();
241
242 void
243 createTrayIcon();
244
Yingdi Yu42f66462013-10-31 17:38:22 -0700245 QString
246 formatTime(time_t);
247
248 void
249 printTimeInCell(QTextTable *, time_t);
250
251 void
252 disableTreeDisplay();
253
254signals:
255 void
256 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
257
258 void
259 treeUpdated(const std::vector<Sync::MissingDataInfo>);
260
261 void
262 removeReceived(QString prefix);
263
Yingdi Yu42372442013-11-06 18:43:31 -0800264 void
265 closeChatDialog(const ndn::Name& chatroomPrefix);
266
Yingdi Yub29f78c2013-11-09 20:12:31 -0800267 void
268 noNdnConnection(const QString& msg);
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800269
270 void
271 inivationRejection(const QString& msg);
Yingdi Yub29f78c2013-11-09 20:12:31 -0800272
Yingdi Yu42f66462013-10-31 17:38:22 -0700273public slots:
274 void
275 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
276
277 void
278 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
279
280 void
281 processRemove(QString prefix);
282
283private slots:
284 void
285 returnPressed();
286
287 void
288 treeButtonPressed();
289
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800290 void
291 settingUpdated(QString, QString, QString);
292
Yingdi Yu42f66462013-10-31 17:38:22 -0700293 void
294 sendJoin();
295
296 void
297 sendHello();
298
299 void
300 sendLeave();
301
302 void
303 replot();
304
305 void
306 updateRosterList(QStringList);
307
308 void
309 enableTreeDisplay();
310
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800311 void
312 updateLocalPrefix();
313
Yingdi Yu42f66462013-10-31 17:38:22 -0700314 void
315 summonReaper();
316
317 void
318 reap();
319
320 void
Yingdi Yua0594092013-11-06 22:07:38 -0800321 iconActivated(QSystemTrayIcon::ActivationReason reason);
322
323 void
324 messageClicked();
325
326 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700327 showMessage(QString, QString);
328
329 void
330 openInviteListDialog();
331
332 void
333 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800334
335 void
336 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700337
338private:
Yingdi Yu04842232013-10-23 14:03:09 -0700339 Ui::ChatDialog *ui;
Yingdi Yu64206112013-12-24 11:16:32 +0800340 ndn::ptr_lib::shared_ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700341 ndn::Name m_chatroomPrefix;
342 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700343 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700344 ndn::Name m_defaultIdentity;
Yingdi Yu64206112013-12-24 11:16:32 +0800345 ndn::ptr_lib::shared_ptr<InvitationPolicyManager> m_invitationPolicyManager;
346 ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager;
347 ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager;
348 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
349 ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800350
Yingdi Yuaccbda92013-12-27 08:44:12 +0800351 boost::recursive_mutex m_mutex;
352 boost::thread m_thread;
353 bool m_running;
354
Yingdi Yu0a953c32013-11-10 10:32:18 -0800355 ndn::Name m_newLocalPrefix;
356 bool m_newLocalPrefixReady;
Yingdi Yu42f66462013-10-31 17:38:22 -0700357
358 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800359 std::string m_nick;
Yingdi Yu42f66462013-10-31 17:38:22 -0700360 Sync::SyncSocket *m_sock;
361 uint32_t m_session;
362 DigestTreeScene *m_scene;
363 boost::recursive_mutex m_msgMutex;
364 boost::recursive_mutex m_sceneMutex;
365 time_t m_lastMsgTime;
366 int m_randomizedInterval;
367 QTimer *m_timer;
368 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800369 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700370
Yingdi Yu07b5b092013-11-07 17:00:54 -0800371 QAction *minimizeAction;
372 QAction *maximizeAction;
373 QAction *restoreAction;
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800374 QAction *updateLocalPrefixAction;
Yingdi Yu07b5b092013-11-07 17:00:54 -0800375 QAction *quitAction;
376 QMenu *trayIconMenu;
377
Yingdi Yu42f66462013-10-31 17:38:22 -0700378 // QQueue<SyncDemo::ChatMessage> m_history;
379 // bool m_historyInitialized;
380 bool m_joined;
381
382 QList<QString> m_zombieList;
383 int m_zombieIndex;
384
385 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700386};
387
388#endif // ChatDIALOG_H