blob: 0ddbc85e07f1abaeb372c9dbdb8bc40b581c8fd1 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- 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 *
8 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
9 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
10 * Yingdi Yu <yingdi@cs.ucla.edu>
11 */
12
13#ifndef CHAT_DIALOG_H
14#define CHAT_DIALOG_H
15
16#include <QDialog>
17#include <QTextTable>
18#include <QStringListModel>
19#include <QSystemTrayIcon>
20#include <QMenu>
21#include <QTimer>
22
23#include "invite-list-dialog.h"
24
25#ifndef Q_MOC_RUN
26#include "contact-manager.h"
27#include "invitation.h"
28#include "contact.h"
29#include "chatbuf.pb.h"
30#include "intro-cert-list.pb.h"
31#include "digesttreescene.h"
32#include <sync-socket.h>
33#include <sync-seq-no.h>
34#include <ndn-cpp-dev/security/key-chain.hpp>
35#include "validator-invitation.h"
36
37#include <boost/thread/locks.hpp>
38#include <boost/thread/recursive_mutex.hpp>
39#include <boost/thread/thread.hpp>
40#endif
41
42#define MAX_HISTORY_ENTRY 20
43
44namespace Ui {
45class ChatDialog;
46}
47
48class ChatDialog : public QDialog
49{
50 Q_OBJECT
51
52public:
53 explicit
54 ChatDialog(chronos::ContactManager* contactManager,
55 ndn::shared_ptr<ndn::Face> face,
56 const ndn::IdentityCertificate& myCertificate,
57 const ndn::Name& chatroomPrefix,
58 const ndn::Name& localPrefix,
59 const std::string& nick,
60 bool witSecurity,
61 QWidget* parent = 0);
62
63 ~ChatDialog();
64
65 void
66 addSyncAnchor(const chronos::Invitation& invitation);
67
68 void
69 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
70
71 void
72 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
73
74 void
75 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
76
77 void
78 processRemoveWrapper(std::string);
79
80protected:
81 void
82 closeEvent(QCloseEvent *e);
83
Yingdi Yu233a9722014-03-07 15:47:09 -080084 void
85 changeEvent(QEvent *e);
86
Yingdi Yu348f5ea2014-03-01 14:47:25 -080087 void
88 resizeEvent(QResizeEvent *);
89
90 void
91 showEvent(QShowEvent *);
92
93private:
94 void
95 updatePrefix();
96
97 void
98 updateLabels();
99
100 void
101 initializeSync();
102
103 void
104 sendInvitation(ndn::shared_ptr<chronos::Contact> contact, bool isIntroducer);
105
106 void
107 replyWrapper(const ndn::Interest& interest,
108 ndn::Data& data,
109 size_t routablePrefixOffset,
110 bool isIntroducer);
111
112 void
113 replyTimeoutWrapper(const ndn::Interest& interest,
114 size_t routablePrefixOffset);
115
116 void
117 onReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
118 size_t inviteeRoutablePrefixOffset,
119 bool isIntroduce);
120
121 void
122 onReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
123 const std::string& failureInfo);
124
125 void
126 invitationRejected(const ndn::Name& identity);
127
128 void
129 invitationAccepted(const ndn::IdentityCertificate& inviteeCert,
130 const ndn::Name& inviteePrefix,
131 bool isIntroducer);
132
133 void
134 fetchIntroCert(const ndn::Name& identity, const ndn::Name& prefix);
135
136 void
137 onIntroCertList(const ndn::Interest& interest, const ndn::Data& data);
138
139 void
140 onIntroCertListTimeout(const ndn::Interest& interest, int retry, const std::string& msg);
141
142 void
143 introCertWrapper(const ndn::Interest& interest, ndn::Data& data);
144
145 void
146 introCertTimeoutWrapper(const ndn::Interest& interest, int retry, const QString& msg);
147
148 void
149 onCertListInterest(const ndn::Name& prefix, const ndn::Interest& interest);
150
151 void
152 onCertListRegisterFailed(const ndn::Name& prefix, const std::string& msg);
153
154 void
155 onCertSingleInterest(const ndn::Name& prefix, const ndn::Interest& interest);
156
157 void
158 onCertSingleRegisterFailed(const ndn::Name& prefix, const std::string& msg);
159
160
161 void
162 sendMsg(SyncDemo::ChatMessage &msg);
163
164 void
165 disableTreeDisplay();
166
167 void
168 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
169
170 void
171 processRemove(QString prefix);
172
173 ndn::Name
174 getInviteeRoutablePrefix(const ndn::Name& invitee);
175
176 void
177 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
178
179 void
180 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
181
182 QString
183 formatTime(time_t);
184
185 void
186 printTimeInCell(QTextTable *, time_t);
187
188 std::string
189 getRandomString();
190
191 void
192 showMessage(const QString&, const QString&);
193
194 void
195 fitView();
196
197 void
198 summonReaper();
199
200signals:
201 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800202 processData(const ndn::shared_ptr<const ndn::Data>& data, bool show, bool isHistory);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800203
204 void
205 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
206
207 void
208 closeChatDialog(const QString& chatroomName);
209
210 void
211 inivationRejection(const QString& msg);
212
213 void
214 showChatMessage(const QString& chatroomName, const QString& from, const QString& data);
215
216 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800217 resetIcon();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800218
219 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800220 reply(const ndn::Interest& interest,
221 const ndn::shared_ptr<const ndn::Data>& data,
222 size_t routablePrefixOffset, bool isIntroducer);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800223
224 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800225 replyTimeout(const ndn::Interest& interest,
226 size_t routablePrefixOffset);
227
228 void
229 introCert(const ndn::Interest& interest,
230 const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800231
232 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800233 introCertTimeout(const ndn::Interest& interest,
234 int retry, const QString& msg);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800235
Yingdi Yu233a9722014-03-07 15:47:09 -0800236 void
237 waitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800238
239public slots:
240 void
241 onLocalPrefixUpdated(const QString& localPrefix);
242
243 void
244 onClose();
245
246private slots:
247 void
248 onReturnPressed();
249
250 void
251 onTreeButtonPressed();
252
253 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800254 onProcessData(const ndn::shared_ptr<const ndn::Data>& data,
255 bool show, bool isHistory);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800256
257 void
258 onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
259
260 void
261 onReplot();
262
263 void
264 onRosterChanged(QStringList);
265
266 void
267 onInviteListDialogRequested();
268
269 void
270 sendJoin();
271
272 void
273 sendHello();
274
275 void
276 sendLeave();
277
278 void
279 enableTreeDisplay();
280
281 void
282 reap();
283
284 void
285 onSendInvitation(QString);
286
287 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800288 onReply(const ndn::Interest& interest,
289 const ndn::shared_ptr<const ndn::Data>& data,
290 size_t routablePrefixOffset, bool isIntroducer);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800291
292 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800293 onReplyTimeout(const ndn::Interest& interest,
294 size_t routablePrefixOffset);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800295
296 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800297 onIntroCert(const ndn::Interest& interest,
298 const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800299
300 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800301 onIntroCertTimeout(const ndn::Interest& interest,
302 int retry, const QString& msg);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800303
304private:
305 Ui::ChatDialog *ui;
306 ndn::KeyChain m_keyChain;
307
308 chronos::ContactManager* m_contactManager;
309 ndn::shared_ptr<ndn::Face> m_face;
310
311 ndn::IdentityCertificate m_myCertificate;
312 ndn::Name m_identity;
313
314 ndn::Name m_certListPrefix;
315 const ndn::RegisteredPrefixId* m_certListPrefixId;
316 ndn::Name m_certSinglePrefix;
317 const ndn::RegisteredPrefixId* m_certSinglePrefixId;
318
319 std::string m_chatroomName;
320 ndn::Name m_chatroomPrefix;
321 ndn::Name m_localPrefix;
322 bool m_useRoutablePrefix;
323 ndn::Name m_chatPrefix;
324 ndn::Name m_localChatPrefix;
325 std::string m_nick;
326 DigestTreeScene *m_scene;
327 QStringListModel *m_rosterModel;
328 QTimer* m_timer;
329
330
331 int64_t m_lastMsgTime;
332 int m_randomizedInterval;
333 bool m_joined;
334
335 Sync::SyncSocket *m_sock;
336 uint64_t m_session;
337 ndn::shared_ptr<ndn::SecRuleRelative> m_dataRule;
338
339 InviteListDialog* m_inviteListDialog;
340 ndn::shared_ptr<chronos::ValidatorInvitation> m_invitationValidator;
341
342 boost::recursive_mutex m_msgMutex;
343 boost::recursive_mutex m_sceneMutex;
344 QList<QString> m_zombieList;
345 int m_zombieIndex;
346};
347
348#endif // CHAT_DIALOG_H