blob: 0c06783bb8238ba050e8fc30840d08d99f867a55 [file] [log] [blame]
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
Yingdi Yu5b989132013-10-23 14:03:09 -07004 * Yingdi Yu
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07005 *
Yingdi Yu5b989132013-10-23 14:03:09 -07006 * BSD license, See the LICENSE file for more information
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07007 *
Yingdi Yu7989eb22013-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>
Alexander Afanasyevb4b92292013-07-09 13:54:59 -070011 */
12
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070013#ifndef CHATDIALOG_H
14#define CHATDIALOG_H
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070015
Yingdi Yu5b989132013-10-23 14:03:09 -070016#include <QDialog>
Yingdi Yu7989eb22013-10-31 17:38:22 -070017#include <QTextTable>
18#include <QStringListModel>
19#include <QTimer>
Yingdi Yu83eae842013-11-06 22:07:38 -080020#include <QSystemTrayIcon>
Yingdi Yu702d6f12013-11-07 17:00:54 -080021#include <QMenu>
Yingdi Yu7989eb22013-10-31 17:38:22 -070022
23#include "invitelistdialog.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070024
Yingdi Yu5b989132013-10-23 14:03:09 -070025#ifndef Q_MOC_RUN
Yingdi Yu76dd8002013-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 Yu7989eb22013-10-31 17:38:22 -070029#include "invitation-policy-manager.h"
Yingdi Yueda39aa2013-10-23 23:07:29 -070030#include "contact-item.h"
Yingdi Yu7989eb22013-10-31 17:38:22 -070031
Alexander Afanasyev4a979312013-11-07 15:30:05 -080032#include <sync-socket.h>
Yingdi Yu7989eb22013-10-31 17:38:22 -070033#include <sync-seq-no.h>
34#include "chatbuf.pb.h"
35#include "digesttreescene.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070036#endif
37
Yingdi Yu76dd8002013-12-24 11:16:32 +080038typedef ndn::func_lib::function<void()> OnEventualTimeout;
39
Yingdi Yu7989eb22013-10-31 17:38:22 -070040#define MAX_HISTORY_ENTRY 20
41
Yingdi Yu5b989132013-10-23 14:03:09 -070042namespace Ui {
43class ChatDialog;
44}
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070045
Yingdi Yu5b989132013-10-23 14:03:09 -070046class ChatDialog : public QDialog
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070047{
Yingdi Yu5b989132013-10-23 14:03:09 -070048 Q_OBJECT
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070049
50public:
Yingdi Yu76dd8002013-12-24 11:16:32 +080051 explicit ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager,
Yingdi Yu7989eb22013-10-31 17:38:22 -070052 const ndn::Name& chatroomPrefix,
Yingdi Yu5b989132013-10-23 14:03:09 -070053 const ndn::Name& localPrefix,
Yingdi Yueda39aa2013-10-23 23:07:29 -070054 const ndn::Name& defaultIdentity,
Yingdi Yu46948282013-11-06 18:43:31 -080055 const std::string& nick,
56 bool trial = false,
Yingdi Yu5b989132013-10-23 14:03:09 -070057 QWidget *parent = 0);
Yingdi Yu7989eb22013-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
Zhenkai Zhu82a62752012-06-04 17:11:04 -070065 ~ChatDialog();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070066
Yingdi Yu5b989132013-10-23 14:03:09 -070067 const ndn::Name&
68 getChatroomPrefix() const
69 { return m_chatroomPrefix; }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070070
Yingdi Yu5b989132013-10-23 14:03:09 -070071 const ndn::Name&
72 getLocalPrefix() const
73 { return m_localPrefix; }
74
75 void
Yingdi Yu76dd8002013-12-24 11:16:32 +080076 sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool isIntroducer);
Yingdi Yu7989eb22013-10-31 17:38:22 -070077
78 void
79 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
80
81 void
82 addChatDataRule(const ndn::Name& prefix,
Yingdi Yu76dd8002013-12-24 11:16:32 +080083 const ndn::IdentityCertificate& identityCertificate,
Yingdi Yu7989eb22013-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 Yu76dd8002013-12-24 11:16:32 +080093 processDataWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -070094
95 void
Yingdi Yu76dd8002013-12-24 11:16:32 +080096 processDataNoShowWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu7989eb22013-10-31 17:38:22 -070097
98 void
99 processRemoveWrapper(std::string);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700100
Yingdi Yu83eae842013-11-06 22:07:38 -0800101 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800102 publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer);
Yingdi Yu83eae842013-11-06 22:07:38 -0800103
Yingdi Yu46948282013-11-06 18:43:31 -0800104protected:
Yingdi Yu83eae842013-11-06 22:07:38 -0800105 void
106 closeEvent(QCloseEvent *e);
107
108 void
109 changeEvent(QEvent *e);
Yingdi Yu46948282013-11-06 18:43:31 -0800110
Zhenkai Zhub45e38a2012-06-01 15:44:36 -0700111private:
Yingdi Yu7989eb22013-10-31 17:38:22 -0700112
113 void
Yingdi Yu76dd8002013-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 Yu7989eb22013-10-31 17:38:22 -0700124 initializeSetting();
125
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800126 QString
127 getRandomString();
128
Yingdi Yu7989eb22013-10-31 17:38:22 -0700129 void
130 updateLabels();
131
Yingdi Yueda39aa2013-10-23 23:07:29 -0700132 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800133 initializeSync();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700134
135 void
Yingdi Yu76dd8002013-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 Yueda39aa2013-10-23 23:07:29 -0700174
175 void
Yingdi Yu76dd8002013-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 Yueda39aa2013-10-23 23:07:29 -0700183
184 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800185 onInviteReplyTimeout(const ndn::Name& identity);
186
Yingdi Yueda39aa2013-10-23 23:07:29 -0700187
188 void
189 invitationRejected(const ndn::Name& identity);
190
191 void
Yingdi Yu76dd8002013-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 Yu7989eb22013-10-31 17:38:22 -0700195 bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700196
197 void
Yingdi Yu76dd8002013-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 Yu7989eb22013-10-31 17:38:22 -0700200
Yingdi Yu46948282013-11-06 18:43:31 -0800201 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800202 onLocalPrefixTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu7989eb22013-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 Yu83eae842013-11-06 22:07:38 -0800225 void
226 createActions();
227
228 void
229 createTrayIcon();
230
Yingdi Yu7989eb22013-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 Yu46948282013-11-06 18:43:31 -0800250 void
251 closeChatDialog(const ndn::Name& chatroomPrefix);
252
Yingdi Yu8e135832013-11-09 20:12:31 -0800253 void
254 noNdnConnection(const QString& msg);
Yingdi Yu0aca2652013-11-10 10:47:44 -0800255
256 void
257 inivationRejection(const QString& msg);
Yingdi Yu8e135832013-11-09 20:12:31 -0800258
Yingdi Yu7989eb22013-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 Yufdb8ab82013-11-10 01:38:21 -0800276 void
277 settingUpdated(QString, QString, QString);
278
Yingdi Yu7989eb22013-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 Yufdb8ab82013-11-10 01:38:21 -0800297 void
298 updateLocalPrefix();
299
Yingdi Yu7989eb22013-10-31 17:38:22 -0700300 void
301 summonReaper();
302
303 void
304 reap();
305
306 void
Yingdi Yu83eae842013-11-06 22:07:38 -0800307 iconActivated(QSystemTrayIcon::ActivationReason reason);
308
309 void
310 messageClicked();
311
312 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700313 showMessage(QString, QString);
314
315 void
316 openInviteListDialog();
317
318 void
319 sendInvitationWrapper(QString, bool);
Yingdi Yu702d6f12013-11-07 17:00:54 -0800320
321 void
322 quit();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700323
324private:
Yingdi Yu5b989132013-10-23 14:03:09 -0700325 Ui::ChatDialog *ui;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800326 ndn::ptr_lib::shared_ptr<ContactManager> m_contactManager;
Yingdi Yu5b989132013-10-23 14:03:09 -0700327 ndn::Name m_chatroomPrefix;
328 ndn::Name m_localPrefix;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700329 ndn::Name m_localChatPrefix;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700330 ndn::Name m_defaultIdentity;
Yingdi Yu76dd8002013-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 Yua66183f2013-11-10 10:32:18 -0800336
Yingdi Yua66183f2013-11-10 10:32:18 -0800337 ndn::Name m_newLocalPrefix;
338 bool m_newLocalPrefixReady;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700339
340 User m_user;
Yingdi Yu46948282013-11-06 18:43:31 -0800341 std::string m_nick;
Yingdi Yu7989eb22013-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 Yu83eae842013-11-06 22:07:38 -0800351 QSystemTrayIcon *trayIcon;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700352
Yingdi Yu702d6f12013-11-07 17:00:54 -0800353 QAction *minimizeAction;
354 QAction *maximizeAction;
355 QAction *restoreAction;
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800356 QAction *updateLocalPrefixAction;
Yingdi Yu702d6f12013-11-07 17:00:54 -0800357 QAction *quitAction;
358 QMenu *trayIconMenu;
359
Yingdi Yu7989eb22013-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;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700368};
Yingdi Yu5b989132013-10-23 14:03:09 -0700369
370#endif // ChatDIALOG_H