blob: 504d60fac2917f152756112bfd9cbd576486981a [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>
Yingdi Yua0594092013-11-06 22:07:38 -080020#include <QSystemTrayIcon>
Yingdi Yu07b5b092013-11-07 17:00:54 -080021#include <QMenu>
Yingdi Yu42f66462013-10-31 17:38:22 -070022
23#include "invitelistdialog.h"
Yingdi Yu04842232013-10-23 14:03:09 -070024
25#ifndef Q_MOC_RUN
26#include <ndn.cxx/data.h>
Yingdi Yuc4d08d22013-10-23 23:07:29 -070027#include <ndn.cxx/security/keychain.h>
28#include <ndn.cxx/wrapper/wrapper.h>
Yingdi Yu42f66462013-10-31 17:38:22 -070029#include "invitation-policy-manager.h"
Yingdi Yuc4d08d22013-10-23 23:07:29 -070030#include "contact-item.h"
Yingdi Yu42f66462013-10-31 17:38:22 -070031
Alexander Afanasyev4f04c552013-11-07 15:30:05 -080032#include <sync-socket.h>
Yingdi Yu42f66462013-10-31 17:38:22 -070033#include <sync-seq-no.h>
34#include "chatbuf.pb.h"
35#include "digesttreescene.h"
Yingdi Yu04842232013-10-23 14:03:09 -070036#endif
37
Yingdi Yu42f66462013-10-31 17:38:22 -070038#define MAX_HISTORY_ENTRY 20
39
Yingdi Yu04842232013-10-23 14:03:09 -070040namespace Ui {
41class ChatDialog;
42}
43
44class ChatDialog : public QDialog
45{
46 Q_OBJECT
47
48public:
Yingdi Yu42f66462013-10-31 17:38:22 -070049 explicit ChatDialog(ndn::Ptr<ContactManager> contactManager,
50 const ndn::Name& chatroomPrefix,
Yingdi Yu04842232013-10-23 14:03:09 -070051 const ndn::Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070052 const ndn::Name& defaultIdentity,
Yingdi Yu42372442013-11-06 18:43:31 -080053 const std::string& nick,
54 bool trial = false,
Yingdi Yu04842232013-10-23 14:03:09 -070055 QWidget *parent = 0);
Yingdi Yu42f66462013-10-31 17:38:22 -070056
57 // explicit ChatDialog(const ndn::Name& chatroomPrefix,
58 // const ndn::Name& localPrefix,
59 // const ndn::Name& defaultIdentity,
60 // const ndn::security::IdentityCertificate& identityCertificate,
61 // QWidget *parent = 0);
62
Yingdi Yu04842232013-10-23 14:03:09 -070063 ~ChatDialog();
64
65 const ndn::Name&
66 getChatroomPrefix() const
67 { return m_chatroomPrefix; }
68
69 const ndn::Name&
70 getLocalPrefix() const
71 { return m_localPrefix; }
72
73 void
Yingdi Yu42f66462013-10-31 17:38:22 -070074 sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer);
75
76 void
77 addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
78
79 void
80 addChatDataRule(const ndn::Name& prefix,
81 const ndn::security::IdentityCertificate& identityCertificate,
82 bool isIntroducer);
83
84 void
85 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
86
87 void
88 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
89
90 void
91 processDataWrapper(ndn::Ptr<ndn::Data> data);
92
93 void
94 processDataNoShowWrapper(ndn::Ptr<ndn::Data> data);
95
96 void
97 processRemoveWrapper(std::string);
Yingdi Yu04842232013-10-23 14:03:09 -070098
Yingdi Yua0594092013-11-06 22:07:38 -080099 void
100 publishIntroCert(const ndn::security::IdentityCertificate& dskCertificate, bool isIntroducer);
101
Yingdi Yu42372442013-11-06 18:43:31 -0800102protected:
Yingdi Yua0594092013-11-06 22:07:38 -0800103 void
104 closeEvent(QCloseEvent *e);
105
106 void
107 changeEvent(QEvent *e);
Yingdi Yu42372442013-11-06 18:43:31 -0800108
Yingdi Yu04842232013-10-23 14:03:09 -0700109private:
Yingdi Yu42f66462013-10-31 17:38:22 -0700110
111 void
112 initializeSetting();
113
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800114 QString
115 getRandomString();
116
Yingdi Yu42f66462013-10-31 17:38:22 -0700117 void
118 updateLabels();
119
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700120 void
Yingdi Yu42372442013-11-06 18:43:31 -0800121 setWrapper(bool trial);
Yingdi Yu42f66462013-10-31 17:38:22 -0700122
123 void
124 initializeSync();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700125
126 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700127 onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700128
129 void
130 onInviteTimeout(ndn::Ptr<ndn::Closure> closure,
131 ndn::Ptr<ndn::Interest> interest,
132 const ndn::Name& identity,
133 int retry);
134
135 void
136 invitationRejected(const ndn::Name& identity);
137
138 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700139 invitationAccepted(const ndn::Name& identity,
140 ndn::Ptr<ndn::Data> data,
141 const std::string& inviteePrefix,
142 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700143
144 void
145 onUnverified(ndn::Ptr<ndn::Data> data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700146
Yingdi Yu42372442013-11-06 18:43:31 -0800147 void
148 onTimeout(ndn::Ptr<ndn::Closure> closure,
149 ndn::Ptr<ndn::Interest> interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700150
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800151 void
Yingdi Yu0a953c32013-11-10 10:32:18 -0800152 onLocalPrefix(ndn::Ptr<ndn::Data> data);
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800153
154 void
Yingdi Yu0a953c32013-11-10 10:32:18 -0800155 onLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure,
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800156 ndn::Ptr<ndn::Interest> interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700157
158 // void
159 // fetchHistory(std::string name);
160
161 void
162 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
163
164 void
165 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
166
167 void
168 sendMsg(SyncDemo::ChatMessage &msg);
169
170 void
171 resizeEvent(QResizeEvent *);
172
173 void
174 showEvent(QShowEvent *);
175
176 void
177 fitView();
178
Yingdi Yua0594092013-11-06 22:07:38 -0800179 void
180 createActions();
181
182 void
183 createTrayIcon();
184
Yingdi Yu42f66462013-10-31 17:38:22 -0700185 QString
186 formatTime(time_t);
187
188 void
189 printTimeInCell(QTextTable *, time_t);
190
191 void
192 disableTreeDisplay();
193
194signals:
195 void
196 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
197
198 void
199 treeUpdated(const std::vector<Sync::MissingDataInfo>);
200
201 void
202 removeReceived(QString prefix);
203
Yingdi Yu42372442013-11-06 18:43:31 -0800204 void
205 closeChatDialog(const ndn::Name& chatroomPrefix);
206
Yingdi Yub29f78c2013-11-09 20:12:31 -0800207 void
208 noNdnConnection(const QString& msg);
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800209
210 void
211 inivationRejection(const QString& msg);
Yingdi Yub29f78c2013-11-09 20:12:31 -0800212
Yingdi Yu42f66462013-10-31 17:38:22 -0700213public slots:
214 void
215 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
216
217 void
218 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
219
220 void
221 processRemove(QString prefix);
222
223private slots:
224 void
225 returnPressed();
226
227 void
228 treeButtonPressed();
229
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800230 void
231 settingUpdated(QString, QString, QString);
232
Yingdi Yu42f66462013-10-31 17:38:22 -0700233 void
234 sendJoin();
235
236 void
237 sendHello();
238
239 void
240 sendLeave();
241
242 void
243 replot();
244
245 void
246 updateRosterList(QStringList);
247
248 void
249 enableTreeDisplay();
250
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800251 void
252 updateLocalPrefix();
253
Yingdi Yu42f66462013-10-31 17:38:22 -0700254 void
255 summonReaper();
256
257 void
258 reap();
259
260 void
Yingdi Yua0594092013-11-06 22:07:38 -0800261 iconActivated(QSystemTrayIcon::ActivationReason reason);
262
263 void
264 messageClicked();
265
266 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700267 showMessage(QString, QString);
268
269 void
270 openInviteListDialog();
271
272 void
273 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800274
275 void
276 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700277
278private:
Yingdi Yu04842232013-10-23 14:03:09 -0700279 Ui::ChatDialog *ui;
Yingdi Yu42f66462013-10-31 17:38:22 -0700280 ndn::Ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700281 ndn::Name m_chatroomPrefix;
282 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700283 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700284 ndn::Name m_defaultIdentity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700285 ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager;
286 ndn::Ptr<SyncPolicyManager> m_syncPolicyManager;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700287 ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
288 ndn::Ptr<ndn::security::Keychain> m_keychain;
289 ndn::Ptr<ndn::Wrapper> m_handler;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800290
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800291 ndn::Ptr<ndn::Wrapper> m_localPrefixHandler;
Yingdi Yu0a953c32013-11-10 10:32:18 -0800292 ndn::Name m_newLocalPrefix;
293 bool m_newLocalPrefixReady;
Yingdi Yu42f66462013-10-31 17:38:22 -0700294
295 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800296 std::string m_nick;
Yingdi Yu42f66462013-10-31 17:38:22 -0700297 Sync::SyncSocket *m_sock;
298 uint32_t m_session;
299 DigestTreeScene *m_scene;
300 boost::recursive_mutex m_msgMutex;
301 boost::recursive_mutex m_sceneMutex;
302 time_t m_lastMsgTime;
303 int m_randomizedInterval;
304 QTimer *m_timer;
305 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800306 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700307
Yingdi Yu07b5b092013-11-07 17:00:54 -0800308 QAction *minimizeAction;
309 QAction *maximizeAction;
310 QAction *restoreAction;
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800311 QAction *updateLocalPrefixAction;
Yingdi Yu07b5b092013-11-07 17:00:54 -0800312 QAction *quitAction;
313 QMenu *trayIconMenu;
314
Yingdi Yu42f66462013-10-31 17:38:22 -0700315 // QQueue<SyncDemo::ChatMessage> m_history;
316 // bool m_historyInitialized;
317 bool m_joined;
318
319 QList<QString> m_zombieList;
320 int m_zombieIndex;
321
322 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700323};
324
325#endif // ChatDIALOG_H