blob: 951edbbccf77edbf22c89b0491635e934a452e30 [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>
20
21#include "invitelistdialog.h"
Yingdi Yu04842232013-10-23 14:03:09 -070022
23#ifndef Q_MOC_RUN
24#include <ndn.cxx/data.h>
Yingdi Yuc4d08d22013-10-23 23:07:29 -070025#include <ndn.cxx/security/keychain.h>
26#include <ndn.cxx/wrapper/wrapper.h>
Yingdi Yu42f66462013-10-31 17:38:22 -070027#include "invitation-policy-manager.h"
Yingdi Yuc4d08d22013-10-23 23:07:29 -070028#include "contact-item.h"
Yingdi Yu42f66462013-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"
Yingdi Yu04842232013-10-23 14:03:09 -070034#endif
35
Yingdi Yu42f66462013-10-31 17:38:22 -070036#define MAX_HISTORY_ENTRY 20
37
Yingdi Yu04842232013-10-23 14:03:09 -070038namespace Ui {
39class ChatDialog;
40}
41
42class ChatDialog : public QDialog
43{
44 Q_OBJECT
45
46public:
Yingdi Yu42f66462013-10-31 17:38:22 -070047 explicit ChatDialog(ndn::Ptr<ContactManager> contactManager,
48 const ndn::Name& chatroomPrefix,
Yingdi Yu04842232013-10-23 14:03:09 -070049 const ndn::Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070050 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080051 const std::string& nick,
52 bool trial = false,
Yingdi Yu04842232013-10-23 14:03:09 -070053 QWidget *parent = 0);
Yingdi Yu42f66462013-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
Yingdi Yu04842232013-10-23 14:03:09 -070061 ~ChatDialog();
62
63 const ndn::Name&
64 getChatroomPrefix() const
65 { return m_chatroomPrefix; }
66
67 const ndn::Name&
68 getLocalPrefix() const
69 { return m_localPrefix; }
70
71 void
Yingdi Yu42f66462013-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);
Yingdi Yu04842232013-10-23 14:03:09 -070096
Yingdi Yu42372442013-11-06 18:43:31 -080097protected:
98 void closeEvent(QCloseEvent *e);
99
Yingdi Yu04842232013-10-23 14:03:09 -0700100private:
Yingdi Yu42f66462013-10-31 17:38:22 -0700101
102 void
103 initializeSetting();
104
105 void
106 updateLabels();
107
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700108 void
Yingdi Yu42372442013-11-06 18:43:31 -0800109 setWrapper(bool trial);
Yingdi Yu42f66462013-10-31 17:38:22 -0700110
111 void
112 initializeSync();
113
114 void
115 publishIntroCert(ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate, bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700116
117 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700118 onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer);
Yingdi Yuc4d08d22013-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 Yu42f66462013-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 Yuc4d08d22013-10-23 23:07:29 -0700134
135 void
136 onUnverified(ndn::Ptr<ndn::Data> data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700137
Yingdi Yu42372442013-11-06 18:43:31 -0800138 void
139 onTimeout(ndn::Ptr<ndn::Closure> closure,
140 ndn::Ptr<ndn::Interest> interest);
Yingdi Yu42f66462013-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 Yu42372442013-11-06 18:43:31 -0800183 void
184 closeChatDialog(const ndn::Name& chatroomPrefix);
185
Yingdi Yu42f66462013-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 Yuc4d08d22013-10-23 23:07:29 -0700235
236private:
Yingdi Yu04842232013-10-23 14:03:09 -0700237 Ui::ChatDialog *ui;
Yingdi Yu42f66462013-10-31 17:38:22 -0700238 ndn::Ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700239 ndn::Name m_chatroomPrefix;
240 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700241 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700242 ndn::Name m_defaultIdentity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700243 ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager;
244 ndn::Ptr<SyncPolicyManager> m_syncPolicyManager;
Yingdi Yuc4d08d22013-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 Yu42f66462013-10-31 17:38:22 -0700248
249 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800250 std::string m_nick;
Yingdi Yu42f66462013-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;
Yingdi Yu04842232013-10-23 14:03:09 -0700270};
271
272#endif // ChatDIALOG_H