blob: 951edbbccf77edbf22c89b0491635e934a452e30 [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>
20
21#include "invitelistdialog.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070022
Yingdi Yu5b989132013-10-23 14:03:09 -070023#ifndef Q_MOC_RUN
24#include <ndn.cxx/data.h>
Yingdi Yueda39aa2013-10-23 23:07:29 -070025#include <ndn.cxx/security/keychain.h>
26#include <ndn.cxx/wrapper/wrapper.h>
Yingdi Yu7989eb22013-10-31 17:38:22 -070027#include "invitation-policy-manager.h"
Yingdi Yueda39aa2013-10-23 23:07:29 -070028#include "contact-item.h"
Yingdi Yu7989eb22013-10-31 17:38:22 -070029
30#include <ccnx/sync-socket.h>
31#include <sync-seq-no.h>
32#include "chatbuf.pb.h"
33#include "digesttreescene.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070034#endif
35
Yingdi Yu7989eb22013-10-31 17:38:22 -070036#define MAX_HISTORY_ENTRY 20
37
Yingdi Yu5b989132013-10-23 14:03:09 -070038namespace Ui {
39class ChatDialog;
40}
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070041
Yingdi Yu5b989132013-10-23 14:03:09 -070042class ChatDialog : public QDialog
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070043{
Yingdi Yu5b989132013-10-23 14:03:09 -070044 Q_OBJECT
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070045
46public:
Yingdi Yu7989eb22013-10-31 17:38:22 -070047 explicit ChatDialog(ndn::Ptr<ContactManager> contactManager,
48 const ndn::Name& chatroomPrefix,
Yingdi Yu5b989132013-10-23 14:03:09 -070049 const ndn::Name& localPrefix,
Yingdi Yueda39aa2013-10-23 23:07:29 -070050 const ndn::Name& defaultIdentity,
Yingdi Yu46948282013-11-06 18:43:31 -080051 const std::string& nick,
52 bool trial = false,
Yingdi Yu5b989132013-10-23 14:03:09 -070053 QWidget *parent = 0);
Yingdi Yu7989eb22013-10-31 17:38:22 -070054
55 // explicit ChatDialog(const ndn::Name& chatroomPrefix,
56 // const ndn::Name& localPrefix,
57 // const ndn::Name& defaultIdentity,
58 // const ndn::security::IdentityCertificate& identityCertificate,
59 // QWidget *parent = 0);
60
Zhenkai Zhu82a62752012-06-04 17:11:04 -070061 ~ChatDialog();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070062
Yingdi Yu5b989132013-10-23 14:03:09 -070063 const ndn::Name&
64 getChatroomPrefix() const
65 { return m_chatroomPrefix; }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070066
Yingdi Yu5b989132013-10-23 14:03:09 -070067 const ndn::Name&
68 getLocalPrefix() const
69 { return m_localPrefix; }
70
71 void
Yingdi Yu7989eb22013-10-31 17:38:22 -070072 sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer);
73
74 void
75 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
76
77 void
78 addChatDataRule(const ndn::Name& prefix,
79 const ndn::security::IdentityCertificate& identityCertificate,
80 bool isIntroducer);
81
82 void
83 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
84
85 void
86 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
87
88 void
89 processDataWrapper(ndn::Ptr<ndn::Data> data);
90
91 void
92 processDataNoShowWrapper(ndn::Ptr<ndn::Data> data);
93
94 void
95 processRemoveWrapper(std::string);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070096
Yingdi Yu46948282013-11-06 18:43:31 -080097protected:
98 void closeEvent(QCloseEvent *e);
99
Zhenkai Zhub45e38a2012-06-01 15:44:36 -0700100private:
Yingdi Yu7989eb22013-10-31 17:38:22 -0700101
102 void
103 initializeSetting();
104
105 void
106 updateLabels();
107
Yingdi Yueda39aa2013-10-23 23:07:29 -0700108 void
Yingdi Yu46948282013-11-06 18:43:31 -0800109 setWrapper(bool trial);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700110
111 void
112 initializeSync();
113
114 void
115 publishIntroCert(ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate, bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700116
117 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700118 onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700119
120 void
121 onInviteTimeout(ndn::Ptr<ndn::Closure> closure,
122 ndn::Ptr<ndn::Interest> interest,
123 const ndn::Name& identity,
124 int retry);
125
126 void
127 invitationRejected(const ndn::Name& identity);
128
129 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700130 invitationAccepted(const ndn::Name& identity,
131 ndn::Ptr<ndn::Data> data,
132 const std::string& inviteePrefix,
133 bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700134
135 void
136 onUnverified(ndn::Ptr<ndn::Data> data);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700137
Yingdi Yu46948282013-11-06 18:43:31 -0800138 void
139 onTimeout(ndn::Ptr<ndn::Closure> closure,
140 ndn::Ptr<ndn::Interest> interest);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700141
142
143 // void
144 // fetchHistory(std::string name);
145
146 void
147 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
148
149 void
150 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
151
152 void
153 sendMsg(SyncDemo::ChatMessage &msg);
154
155 void
156 resizeEvent(QResizeEvent *);
157
158 void
159 showEvent(QShowEvent *);
160
161 void
162 fitView();
163
164 QString
165 formatTime(time_t);
166
167 void
168 printTimeInCell(QTextTable *, time_t);
169
170 void
171 disableTreeDisplay();
172
173signals:
174 void
175 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
176
177 void
178 treeUpdated(const std::vector<Sync::MissingDataInfo>);
179
180 void
181 removeReceived(QString prefix);
182
Yingdi Yu46948282013-11-06 18:43:31 -0800183 void
184 closeChatDialog(const ndn::Name& chatroomPrefix);
185
Yingdi Yu7989eb22013-10-31 17:38:22 -0700186public slots:
187 void
188 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
189
190 void
191 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
192
193 void
194 processRemove(QString prefix);
195
196private slots:
197 void
198 returnPressed();
199
200 void
201 treeButtonPressed();
202
203 void
204 sendJoin();
205
206 void
207 sendHello();
208
209 void
210 sendLeave();
211
212 void
213 replot();
214
215 void
216 updateRosterList(QStringList);
217
218 void
219 enableTreeDisplay();
220
221 void
222 summonReaper();
223
224 void
225 reap();
226
227 void
228 showMessage(QString, QString);
229
230 void
231 openInviteListDialog();
232
233 void
234 sendInvitationWrapper(QString, bool);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700235
236private:
Yingdi Yu5b989132013-10-23 14:03:09 -0700237 Ui::ChatDialog *ui;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700238 ndn::Ptr<ContactManager> m_contactManager;
Yingdi Yu5b989132013-10-23 14:03:09 -0700239 ndn::Name m_chatroomPrefix;
240 ndn::Name m_localPrefix;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700241 ndn::Name m_localChatPrefix;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700242 ndn::Name m_defaultIdentity;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700243 ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager;
244 ndn::Ptr<SyncPolicyManager> m_syncPolicyManager;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700245 ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
246 ndn::Ptr<ndn::security::Keychain> m_keychain;
247 ndn::Ptr<ndn::Wrapper> m_handler;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700248
249 User m_user;
Yingdi Yu46948282013-11-06 18:43:31 -0800250 std::string m_nick;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700251 Sync::SyncSocket *m_sock;
252 uint32_t m_session;
253 DigestTreeScene *m_scene;
254 boost::recursive_mutex m_msgMutex;
255 boost::recursive_mutex m_sceneMutex;
256 time_t m_lastMsgTime;
257 int m_randomizedInterval;
258 QTimer *m_timer;
259 QStringListModel *m_rosterModel;
260
261
262 // QQueue<SyncDemo::ChatMessage> m_history;
263 // bool m_historyInitialized;
264 bool m_joined;
265
266 QList<QString> m_zombieList;
267 int m_zombieIndex;
268
269 InviteListDialog* m_inviteListDialog;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700270};
Yingdi Yu5b989132013-10-23 14:03:09 -0700271
272#endif // ChatDIALOG_H