blob: 2e6ea1cf24675f594eb44c6628c482d7b00dff43 [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
114 void
115 updateLabels();
116
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700117 void
Yingdi Yu42372442013-11-06 18:43:31 -0800118 setWrapper(bool trial);
Yingdi Yu42f66462013-10-31 17:38:22 -0700119
120 void
121 initializeSync();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700122
123 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700124 onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700125
126 void
127 onInviteTimeout(ndn::Ptr<ndn::Closure> closure,
128 ndn::Ptr<ndn::Interest> interest,
129 const ndn::Name& identity,
130 int retry);
131
132 void
133 invitationRejected(const ndn::Name& identity);
134
135 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700136 invitationAccepted(const ndn::Name& identity,
137 ndn::Ptr<ndn::Data> data,
138 const std::string& inviteePrefix,
139 bool isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700140
141 void
142 onUnverified(ndn::Ptr<ndn::Data> data);
Yingdi Yu42f66462013-10-31 17:38:22 -0700143
Yingdi Yu42372442013-11-06 18:43:31 -0800144 void
145 onTimeout(ndn::Ptr<ndn::Closure> closure,
146 ndn::Ptr<ndn::Interest> interest);
Yingdi Yu42f66462013-10-31 17:38:22 -0700147
148
149 // void
150 // fetchHistory(std::string name);
151
152 void
153 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
154
155 void
156 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
157
158 void
159 sendMsg(SyncDemo::ChatMessage &msg);
160
161 void
162 resizeEvent(QResizeEvent *);
163
164 void
165 showEvent(QShowEvent *);
166
167 void
168 fitView();
169
Yingdi Yua0594092013-11-06 22:07:38 -0800170 void
171 createActions();
172
173 void
174 createTrayIcon();
175
Yingdi Yu42f66462013-10-31 17:38:22 -0700176 QString
177 formatTime(time_t);
178
179 void
180 printTimeInCell(QTextTable *, time_t);
181
182 void
183 disableTreeDisplay();
184
185signals:
186 void
187 dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
188
189 void
190 treeUpdated(const std::vector<Sync::MissingDataInfo>);
191
192 void
193 removeReceived(QString prefix);
194
Yingdi Yu42372442013-11-06 18:43:31 -0800195 void
196 closeChatDialog(const ndn::Name& chatroomPrefix);
197
Yingdi Yub29f78c2013-11-09 20:12:31 -0800198 void
199 noNdnConnection(const QString& msg);
200
Yingdi Yu42f66462013-10-31 17:38:22 -0700201public slots:
202 void
203 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
204
205 void
206 processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
207
208 void
209 processRemove(QString prefix);
210
211private slots:
212 void
213 returnPressed();
214
215 void
216 treeButtonPressed();
217
218 void
219 sendJoin();
220
221 void
222 sendHello();
223
224 void
225 sendLeave();
226
227 void
228 replot();
229
230 void
231 updateRosterList(QStringList);
232
233 void
234 enableTreeDisplay();
235
236 void
237 summonReaper();
238
239 void
240 reap();
241
242 void
Yingdi Yua0594092013-11-06 22:07:38 -0800243 iconActivated(QSystemTrayIcon::ActivationReason reason);
244
245 void
246 messageClicked();
247
248 void
Yingdi Yu42f66462013-10-31 17:38:22 -0700249 showMessage(QString, QString);
250
251 void
252 openInviteListDialog();
253
254 void
255 sendInvitationWrapper(QString, bool);
Yingdi Yu07b5b092013-11-07 17:00:54 -0800256
257 void
258 quit();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700259
260private:
Yingdi Yu04842232013-10-23 14:03:09 -0700261 Ui::ChatDialog *ui;
Yingdi Yu42f66462013-10-31 17:38:22 -0700262 ndn::Ptr<ContactManager> m_contactManager;
Yingdi Yu04842232013-10-23 14:03:09 -0700263 ndn::Name m_chatroomPrefix;
264 ndn::Name m_localPrefix;
Yingdi Yu42f66462013-10-31 17:38:22 -0700265 ndn::Name m_localChatPrefix;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700266 ndn::Name m_defaultIdentity;
Yingdi Yu42f66462013-10-31 17:38:22 -0700267 ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager;
268 ndn::Ptr<SyncPolicyManager> m_syncPolicyManager;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700269 ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
270 ndn::Ptr<ndn::security::Keychain> m_keychain;
271 ndn::Ptr<ndn::Wrapper> m_handler;
Yingdi Yu42f66462013-10-31 17:38:22 -0700272
273 User m_user;
Yingdi Yu42372442013-11-06 18:43:31 -0800274 std::string m_nick;
Yingdi Yu42f66462013-10-31 17:38:22 -0700275 Sync::SyncSocket *m_sock;
276 uint32_t m_session;
277 DigestTreeScene *m_scene;
278 boost::recursive_mutex m_msgMutex;
279 boost::recursive_mutex m_sceneMutex;
280 time_t m_lastMsgTime;
281 int m_randomizedInterval;
282 QTimer *m_timer;
283 QStringListModel *m_rosterModel;
Yingdi Yua0594092013-11-06 22:07:38 -0800284 QSystemTrayIcon *trayIcon;
Yingdi Yu42f66462013-10-31 17:38:22 -0700285
Yingdi Yu07b5b092013-11-07 17:00:54 -0800286 QAction *minimizeAction;
287 QAction *maximizeAction;
288 QAction *restoreAction;
289 QAction *quitAction;
290 QMenu *trayIconMenu;
291
Yingdi Yu42f66462013-10-31 17:38:22 -0700292 // QQueue<SyncDemo::ChatMessage> m_history;
293 // bool m_historyInitialized;
294 bool m_joined;
295
296 QList<QString> m_zombieList;
297 int m_zombieIndex;
298
299 InviteListDialog* m_inviteListDialog;
Yingdi Yu04842232013-10-23 14:03:09 -0700300};
301
302#endif // ChatDIALOG_H