Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 4 | * Yingdi Yu |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 5 | * |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 6 | * BSD license, See the LICENSE file for more information |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 8 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 9 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Yingdi Yu <yingdi@cs.ucla.edu> |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 13 | #ifndef CHATDIALOG_H |
| 14 | #define CHATDIALOG_H |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 15 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 16 | #include <QDialog> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 17 | #include <QTextTable> |
| 18 | #include <QStringListModel> |
| 19 | #include <QTimer> |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 20 | #include <QSystemTrayIcon> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 21 | |
| 22 | #include "invitelistdialog.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 24 | #ifndef Q_MOC_RUN |
| 25 | #include <ndn.cxx/data.h> |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 26 | #include <ndn.cxx/security/keychain.h> |
| 27 | #include <ndn.cxx/wrapper/wrapper.h> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 28 | #include "invitation-policy-manager.h" |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 29 | #include "contact-item.h" |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 4a97931 | 2013-11-07 15:30:05 -0800 | [diff] [blame^] | 31 | #include <sync-socket.h> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 32 | #include <sync-seq-no.h> |
| 33 | #include "chatbuf.pb.h" |
| 34 | #include "digesttreescene.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 35 | #endif |
| 36 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 37 | #define MAX_HISTORY_ENTRY 20 |
| 38 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 39 | namespace Ui { |
| 40 | class ChatDialog; |
| 41 | } |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 42 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 43 | class ChatDialog : public QDialog |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 44 | { |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 45 | Q_OBJECT |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 46 | |
| 47 | public: |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 48 | explicit ChatDialog(ndn::Ptr<ContactManager> contactManager, |
| 49 | const ndn::Name& chatroomPrefix, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 50 | const ndn::Name& localPrefix, |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 51 | const ndn::Name& defaultIdentity, |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 52 | const std::string& nick, |
| 53 | bool trial = false, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 54 | QWidget *parent = 0); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 55 | |
| 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 Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 62 | ~ChatDialog(); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 63 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 64 | const ndn::Name& |
| 65 | getChatroomPrefix() const |
| 66 | { return m_chatroomPrefix; } |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 68 | const ndn::Name& |
| 69 | getLocalPrefix() const |
| 70 | { return m_localPrefix; } |
| 71 | |
| 72 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 73 | 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 Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 98 | void |
| 99 | publishIntroCert(const ndn::security::IdentityCertificate& dskCertificate, bool isIntroducer); |
| 100 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 101 | protected: |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 102 | void |
| 103 | closeEvent(QCloseEvent *e); |
| 104 | |
| 105 | void |
| 106 | changeEvent(QEvent *e); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 107 | |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 108 | private: |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 109 | |
| 110 | void |
| 111 | initializeSetting(); |
| 112 | |
| 113 | void |
| 114 | updateLabels(); |
| 115 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 116 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 117 | setWrapper(bool trial); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 118 | |
| 119 | void |
| 120 | initializeSync(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 121 | |
| 122 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 123 | onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 124 | |
| 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 Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 135 | invitationAccepted(const ndn::Name& identity, |
| 136 | ndn::Ptr<ndn::Data> data, |
| 137 | const std::string& inviteePrefix, |
| 138 | bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 139 | |
| 140 | void |
| 141 | onUnverified(ndn::Ptr<ndn::Data> data); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 142 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 143 | void |
| 144 | onTimeout(ndn::Ptr<ndn::Closure> closure, |
| 145 | ndn::Ptr<ndn::Interest> interest); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 146 | |
| 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 Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 169 | void |
| 170 | createActions(); |
| 171 | |
| 172 | void |
| 173 | createTrayIcon(); |
| 174 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 175 | QString |
| 176 | formatTime(time_t); |
| 177 | |
| 178 | void |
| 179 | printTimeInCell(QTextTable *, time_t); |
| 180 | |
| 181 | void |
| 182 | disableTreeDisplay(); |
| 183 | |
| 184 | signals: |
| 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 Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 194 | void |
| 195 | closeChatDialog(const ndn::Name& chatroomPrefix); |
| 196 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 197 | public 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 | |
| 207 | private 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 Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 239 | iconActivated(QSystemTrayIcon::ActivationReason reason); |
| 240 | |
| 241 | void |
| 242 | messageClicked(); |
| 243 | |
| 244 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 245 | showMessage(QString, QString); |
| 246 | |
| 247 | void |
| 248 | openInviteListDialog(); |
| 249 | |
| 250 | void |
| 251 | sendInvitationWrapper(QString, bool); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 252 | |
| 253 | private: |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 254 | Ui::ChatDialog *ui; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 255 | ndn::Ptr<ContactManager> m_contactManager; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 256 | ndn::Name m_chatroomPrefix; |
| 257 | ndn::Name m_localPrefix; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 258 | ndn::Name m_localChatPrefix; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 259 | ndn::Name m_defaultIdentity; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 260 | ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager; |
| 261 | ndn::Ptr<SyncPolicyManager> m_syncPolicyManager; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 262 | ndn::Ptr<ndn::security::IdentityManager> m_identityManager; |
| 263 | ndn::Ptr<ndn::security::Keychain> m_keychain; |
| 264 | ndn::Ptr<ndn::Wrapper> m_handler; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 265 | |
| 266 | User m_user; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 267 | std::string m_nick; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 268 | 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 Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 277 | QSystemTrayIcon *trayIcon; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 278 | |
| 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 Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 287 | }; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 288 | |
| 289 | #endif // ChatDIALOG_H |