blob: d7a1520fd5d7c7eaa4cc05813ee98d461299c0d5 [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 Yu7989eb22013-10-31 17:38:22 -070021
22#include "invitelistdialog.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070023
Yingdi Yu5b989132013-10-23 14:03:09 -070024#ifndef Q_MOC_RUN
25#include <ndn.cxx/data.h>
Yingdi Yueda39aa2013-10-23 23:07:29 -070026#include <ndn.cxx/security/keychain.h>
27#include <ndn.cxx/wrapper/wrapper.h>
Yingdi Yu7989eb22013-10-31 17:38:22 -070028#include "invitation-policy-manager.h"
Yingdi Yueda39aa2013-10-23 23:07:29 -070029#include "contact-item.h"
Yingdi Yu7989eb22013-10-31 17:38:22 -070030
Alexander Afanasyev4a979312013-11-07 15:30:05 -080031#include <sync-socket.h>
Yingdi Yu7989eb22013-10-31 17:38:22 -070032#include <sync-seq-no.h>
33#include "chatbuf.pb.h"
34#include "digesttreescene.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070035#endif
36
Yingdi Yu7989eb22013-10-31 17:38:22 -070037#define MAX_HISTORY_ENTRY 20
38
Yingdi Yu5b989132013-10-23 14:03:09 -070039namespace Ui {
40class ChatDialog;
41}
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070042
Yingdi Yu5b989132013-10-23 14:03:09 -070043class ChatDialog : public QDialog
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070044{
Yingdi Yu5b989132013-10-23 14:03:09 -070045 Q_OBJECT
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070046
47public:
Yingdi Yu7989eb22013-10-31 17:38:22 -070048 explicit ChatDialog(ndn::Ptr<ContactManager> contactManager,
49 const ndn::Name& chatroomPrefix,
Yingdi Yu5b989132013-10-23 14:03:09 -070050 const ndn::Name& localPrefix,
Yingdi Yueda39aa2013-10-23 23:07:29 -070051 const ndn::Name& defaultIdentity,
Yingdi Yu46948282013-11-06 18:43:31 -080052 const std::string& nick,
53 bool trial = false,
Yingdi Yu5b989132013-10-23 14:03:09 -070054 QWidget *parent = 0);
Yingdi Yu7989eb22013-10-31 17:38:22 -070055
56 // explicit ChatDialog(const ndn::Name& chatroomPrefix,
57 // const ndn::Name& localPrefix,
58 // const ndn::Name& defaultIdentity,
59 // const ndn::security::IdentityCertificate& identityCertificate,
60 // QWidget *parent = 0);
61
Zhenkai Zhu82a62752012-06-04 17:11:04 -070062 ~ChatDialog();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070063
Yingdi Yu5b989132013-10-23 14:03:09 -070064 const ndn::Name&
65 getChatroomPrefix() const
66 { return m_chatroomPrefix; }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070067
Yingdi Yu5b989132013-10-23 14:03:09 -070068 const ndn::Name&
69 getLocalPrefix() const
70 { return m_localPrefix; }
71
72 void
Yingdi Yu7989eb22013-10-31 17:38:22 -070073 sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer);
74
75 void
76 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
77
78 void
79 addChatDataRule(const ndn::Name& prefix,
80 const ndn::security::IdentityCertificate& identityCertificate,
81 bool isIntroducer);
82
83 void
84 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
85
86 void
87 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
88
89 void
90 processDataWrapper(ndn::Ptr<ndn::Data> data);
91
92 void
93 processDataNoShowWrapper(ndn::Ptr<ndn::Data> data);
94
95 void
96 processRemoveWrapper(std::string);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070097
Yingdi Yu83eae842013-11-06 22:07:38 -080098 void
99 publishIntroCert(const ndn::security::IdentityCertificate& dskCertificate, bool isIntroducer);
100
Yingdi Yu46948282013-11-06 18:43:31 -0800101protected:
Yingdi Yu83eae842013-11-06 22:07:38 -0800102 void
103 closeEvent(QCloseEvent *e);
104
105 void
106 changeEvent(QEvent *e);
Yingdi Yu46948282013-11-06 18:43:31 -0800107
Zhenkai Zhub45e38a2012-06-01 15:44:36 -0700108private:
Yingdi Yu7989eb22013-10-31 17:38:22 -0700109
110 void
111 initializeSetting();
112
113 void
114 updateLabels();
115
Yingdi Yueda39aa2013-10-23 23:07:29 -0700116 void
Yingdi Yu46948282013-11-06 18:43:31 -0800117 setWrapper(bool trial);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700118
119 void
120 initializeSync();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700121
122 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700123 onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700124
125 void
126 onInviteTimeout(ndn::Ptr<ndn::Closure> closure,
127 ndn::Ptr<ndn::Interest> interest,
128 const ndn::Name& identity,
129 int retry);
130
131 void
132 invitationRejected(const ndn::Name& identity);
133
134 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700135 invitationAccepted(const ndn::Name& identity,
136 ndn::Ptr<ndn::Data> data,
137 const std::string& inviteePrefix,
138 bool isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700139
140 void
141 onUnverified(ndn::Ptr<ndn::Data> data);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700142
Yingdi Yu46948282013-11-06 18:43:31 -0800143 void
144 onTimeout(ndn::Ptr<ndn::Closure> closure,
145 ndn::Ptr<ndn::Interest> interest);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700146
147
148 // void
149 // fetchHistory(std::string name);
150
151 void
152 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
153
154 void
155 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
156
157 void
158 sendMsg(SyncDemo::ChatMessage &msg);
159
160 void
161 resizeEvent(QResizeEvent *);
162
163 void
164 showEvent(QShowEvent *);
165
166 void
167 fitView();
168
Yingdi Yu83eae842013-11-06 22:07:38 -0800169 void
170 createActions();
171
172 void
173 createTrayIcon();
174
Yingdi Yu7989eb22013-10-31 17:38:22 -0700175 QString
176 formatTime(time_t);
177
178 void
179 printTimeInCell(QTextTable *, time_t);
180
181 void
182 disableTreeDisplay();
183
184signals:
185 void
186 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
187
188 void
189 treeUpdated(const std::vector<Sync::MissingDataInfo>);
190
191 void
192 removeReceived(QString prefix);
193
Yingdi Yu46948282013-11-06 18:43:31 -0800194 void
195 closeChatDialog(const ndn::Name& chatroomPrefix);
196
Yingdi Yu7989eb22013-10-31 17:38:22 -0700197public slots:
198 void
199 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
200
201 void
202 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
203
204 void
205 processRemove(QString prefix);
206
207private slots:
208 void
209 returnPressed();
210
211 void
212 treeButtonPressed();
213
214 void
215 sendJoin();
216
217 void
218 sendHello();
219
220 void
221 sendLeave();
222
223 void
224 replot();
225
226 void
227 updateRosterList(QStringList);
228
229 void
230 enableTreeDisplay();
231
232 void
233 summonReaper();
234
235 void
236 reap();
237
238 void
Yingdi Yu83eae842013-11-06 22:07:38 -0800239 iconActivated(QSystemTrayIcon::ActivationReason reason);
240
241 void
242 messageClicked();
243
244 void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700245 showMessage(QString, QString);
246
247 void
248 openInviteListDialog();
249
250 void
251 sendInvitationWrapper(QString, bool);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700252
253private:
Yingdi Yu5b989132013-10-23 14:03:09 -0700254 Ui::ChatDialog *ui;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700255 ndn::Ptr<ContactManager> m_contactManager;
Yingdi Yu5b989132013-10-23 14:03:09 -0700256 ndn::Name m_chatroomPrefix;
257 ndn::Name m_localPrefix;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700258 ndn::Name m_localChatPrefix;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700259 ndn::Name m_defaultIdentity;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700260 ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager;
261 ndn::Ptr<SyncPolicyManager> m_syncPolicyManager;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700262 ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
263 ndn::Ptr<ndn::security::Keychain> m_keychain;
264 ndn::Ptr<ndn::Wrapper> m_handler;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700265
266 User m_user;
Yingdi Yu46948282013-11-06 18:43:31 -0800267 std::string m_nick;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700268 Sync::SyncSocket *m_sock;
269 uint32_t m_session;
270 DigestTreeScene *m_scene;
271 boost::recursive_mutex m_msgMutex;
272 boost::recursive_mutex m_sceneMutex;
273 time_t m_lastMsgTime;
274 int m_randomizedInterval;
275 QTimer *m_timer;
276 QStringListModel *m_rosterModel;
Yingdi Yu83eae842013-11-06 22:07:38 -0800277 QSystemTrayIcon *trayIcon;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700278
279 // QQueue<SyncDemo::ChatMessage> m_history;
280 // bool m_historyInitialized;
281 bool m_joined;
282
283 QList<QString> m_zombieList;
284 int m_zombieIndex;
285
286 InviteListDialog* m_inviteListDialog;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700287};
Yingdi Yu5b989132013-10-23 14:03:09 -0700288
289#endif // ChatDIALOG_H