blob: 0c06783bb8238ba050e8fc30840d08d99f867a55 [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 Yu04842232013-10-23 14:03:09 -070036#endif
37
Yingdi Yu64206112013-12-24 11:16:32 +080038typedef ndn::func_lib::function<void()> OnEventualTimeout;
39
Yingdi Yu42f66462013-10-31 17:38:22 -070040#define MAX_HISTORY_ENTRY 20
41
Yingdi Yu04842232013-10-23 14:03:09 -070042namespace Ui {
43class ChatDialog;
44}
45
46class ChatDialog : public QDialog
47{
48 Q_OBJECT
49
50public:
Yingdi Yu64206112013-12-24 11:16:32 +080051 explicit ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,
Yingdi Yu42f66462013-10-31 17:38:22 -070052 const ndn::Name& chatroomPrefix,
Yingdi Yu04842232013-10-23 14:03:09 -070053 const ndn::Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070054 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080055 const std::string& nick,
56 bool trial = false,
Yingdi Yu04842232013-10-23 14:03:09 -070057 QWidget *parent = 0);
Yingdi Yu42f66462013-10-31 17:38:22 -070058
59 // explicit ChatDialog(const ndn::Name& chatroomPrefix,
60 // const ndn::Name& localPrefix,
61 // const ndn::Name& defaultIdentity,
62 // const ndn::security::IdentityCertificate& identityCertificate,
63 // QWidget *parent = 0);
64
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 Yu64206112013-12-24 11:16:32 +080076 sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool isIntroducer);
Yingdi Yu42f66462013-10-31 17:38:22 -070077
78 void
79 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
80
81 void
82 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu64206112013-12-24 11:16:32 +080083 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu42f66462013-10-31 17:38:22 -070084 bool isIntroducer);
85
86 void
87 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
88
89 void
90 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
91
92 void
Yingdi Yu64206112013-12-24 11:16:32 +080093 processDataWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -070094
95 void
Yingdi Yu64206112013-12-24 11:16:32 +080096 processDataNoShowWrapper(const ndn::ptr_lib::shared_ptr<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
113 void
Yingdi Yu64206112013-12-24 11:16:32 +0800114 connectToDaemon();
115
116 void
117 onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
118 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
119
120 void
121 onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
122
123 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700124 initializeSetting();
125
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800126 QString
127 getRandomString();
128
Yingdi Yu42f66462013-10-31 17:38:22 -0700129 void
130 updateLabels();
131
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700132 void
Yingdi Yu64206112013-12-24 11:16:32 +0800133 initializeSync();
Yingdi Yu42f66462013-10-31 17:38:22 -0700134
135 void
Yingdi Yu64206112013-12-24 11:16:32 +0800136 onTargetData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
137 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
138 int stepCount,
139 const ndn::OnVerified& onVerified,
140 const ndn::OnVerifyFailed& onVerifyFailed,
141 const OnEventualTimeout& timeoutNotify,
142 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
143
144 void
145 onTargetTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
146 int retry,
147 int stepCount,
148 const ndn::OnVerified& onVerified,
149 const ndn::OnVerifyFailed& onVerifyFailed,
150 const OnEventualTimeout& timeoutNotify,
151 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
152
153 void
154 onCertData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
155 const ndn::ptr_lib::shared_ptr<ndn::Data>& cert,
156 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep,
157 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
158
159 void
160 onCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
161 const ndn::OnVerifyFailed& onVerifyFailed,
162 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
163 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep,
164 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager);
165
166 void
167 sendInterest(const ndn::Interest& interest,
168 const ndn::OnVerified& onVerified,
169 const ndn::OnVerifyFailed& onVerifyFailed,
170 const OnEventualTimeout& timeoutNotify,
171 const ndn::ptr_lib::shared_ptr<ndn::PolicyManager>& policyManager,
172 int retry = 1,
173 int stepCount = 0);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700174
175 void
Yingdi Yu64206112013-12-24 11:16:32 +0800176 onInviteReplyVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
177 const ndn::Name& identity,
178 bool isIntroduce);
179
180 void
181 onInviteReplyVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
182 const ndn::Name& identity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700183
184 void
Yingdi Yu64206112013-12-24 11:16:32 +0800185 onInviteReplyTimeout(const ndn::Name& identity);
186
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700187
188 void
189 invitationRejected(const ndn::Name& identity);
190
191 void
Yingdi Yu64206112013-12-24 11:16:32 +0800192 invitationAccepted(const ndn::Name& identity,
193 ndn::ptr_lib::shared_ptr<ndn::Data> data,
194 const std::string& inviteePrefix,
Yingdi Yu42f66462013-10-31 17:38:22 -0700195 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700196
197 void
Yingdi Yu64206112013-12-24 11:16:32 +0800198 onLocalPrefix(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
199 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700200
Yingdi Yu42372442013-11-06 18:43:31 -0800201 void
Yingdi Yu64206112013-12-24 11:16:32 +0800202 onLocalPrefixTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700203
204 // void
205 // fetchHistory(std::string name);
206
207 void
208 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
209
210 void
211 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
212
213 void
214 sendMsg(SyncDemo::ChatMessage &msg);
215
216 void
217 resizeEvent(QResizeEvent *);
218
219 void
220 showEvent(QShowEvent *);
221
222 void
223 fitView();
224
Yingdi Yua0594092013-11-06 22:07:38 -0800225 void
226 createActions();
227
228 void
229 createTrayIcon();
230
Yingdi Yu42f66462013-10-31 17:38:22 -0700231 QString
232 formatTime(time_t);
233
234 void
235 printTimeInCell(QTextTable *, time_t);
236
237 void
238 disableTreeDisplay();
239
240signals:
241 void
242 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
243
244 void
245 treeUpdated(const std::vector<Sync::MissingDataInfo>);
246
247 void
248 removeReceived(QString prefix);
249
Yingdi Yu42372442013-11-06 18:43:31 -0800250 void
251 closeChatDialog(const ndn::Name& chatroomPrefix);
252
Yingdi Yub29f78c2013-11-09 20:12:31 -0800253 void
254 noNdnConnection(const QString& msg);
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800255
256 void
257 inivationRejection(const QString& msg);
Yingdi Yub29f78c2013-11-09 20:12:31 -0800258
Yingdi Yu42f66462013-10-31 17:38:22 -0700259public slots:
260 void
261 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
262
263 void
264 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
265
266 void
267 processRemove(QString prefix);
268
269private slots:
270 void
271 returnPressed();
272
273 void
274 treeButtonPressed();
275
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800276 void
277 settingUpdated(QString, QString, QString);
278
Yingdi Yu42f66462013-10-31 17:38:22 -0700279 void
280 sendJoin();
281
282 void
283 sendHello();
284
285 void
286 sendLeave();
287
288 void
289 replot();
290
291 void
292 updateRosterList(QStringList);
293
294 void
295 enableTreeDisplay();
296
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800297 void
298 updateLocalPrefix();
299
Yingdi Yu42f66462013-10-31 17:38:22 -0700300 void
301 summonReaper();
302
303 void
304 reap();
305
306 void
Yingdi Yua0594092013-11-06 22:07:38 -0800307 iconActivated(QSystemTrayIcon::ActivationReason reason);
308
309 void
310 messageClicked();
311
312 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700313 showMessage(QString, QString);
314
315 void
316 openInviteListDialog();
317
318 void
319 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800320
321 void
322 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700323
324private:
Yingdi Yu04842232013-10-23 14:03:09 -0700325 Ui::ChatDialog *ui;
Yingdi Yu64206112013-12-24 11:16:32 +0800326 ndn::ptr_lib::shared_ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700327 ndn::Name m_chatroomPrefix;
328 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700329 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700330 ndn::Name m_defaultIdentity;
Yingdi Yu64206112013-12-24 11:16:32 +0800331 ndn::ptr_lib::shared_ptr<InvitationPolicyManager> m_invitationPolicyManager;
332 ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager;
333 ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager;
334 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
335 ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800336
Yingdi Yu0a953c32013-11-10 10:32:18 -0800337 ndn::Name m_newLocalPrefix;
338 bool m_newLocalPrefixReady;
Yingdi Yu42f66462013-10-31 17:38:22 -0700339
340 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800341 std::string m_nick;
Yingdi Yu42f66462013-10-31 17:38:22 -0700342 Sync::SyncSocket *m_sock;
343 uint32_t m_session;
344 DigestTreeScene *m_scene;
345 boost::recursive_mutex m_msgMutex;
346 boost::recursive_mutex m_sceneMutex;
347 time_t m_lastMsgTime;
348 int m_randomizedInterval;
349 QTimer *m_timer;
350 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800351 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700352
Yingdi Yu07b5b092013-11-07 17:00:54 -0800353 QAction *minimizeAction;
354 QAction *maximizeAction;
355 QAction *restoreAction;
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800356 QAction *updateLocalPrefixAction;
Yingdi Yu07b5b092013-11-07 17:00:54 -0800357 QAction *quitAction;
358 QMenu *trayIconMenu;
359
Yingdi Yu42f66462013-10-31 17:38:22 -0700360 // QQueue<SyncDemo::ChatMessage> m_history;
361 // bool m_historyInitialized;
362 bool m_joined;
363
364 QList<QString> m_zombieList;
365 int m_zombieIndex;
366
367 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700368};
369
370#endif // ChatDIALOG_H