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> |
| 20 | |
| 21 | #include "invitelistdialog.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 22 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 23 | #ifndef Q_MOC_RUN |
| 24 | #include <ndn.cxx/data.h> |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 25 | #include <ndn.cxx/security/keychain.h> |
| 26 | #include <ndn.cxx/wrapper/wrapper.h> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 27 | #include "invitation-policy-manager.h" |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 28 | #include "contact-item.h" |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 29 | |
| 30 | #include <ccnx/sync-socket.h> |
| 31 | #include <sync-seq-no.h> |
| 32 | #include "chatbuf.pb.h" |
| 33 | #include "digesttreescene.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 34 | #endif |
| 35 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 36 | #define MAX_HISTORY_ENTRY 20 |
| 37 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 38 | namespace Ui { |
| 39 | class ChatDialog; |
| 40 | } |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 42 | class ChatDialog : public QDialog |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 43 | { |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 44 | Q_OBJECT |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 45 | |
| 46 | public: |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 47 | explicit ChatDialog(ndn::Ptr<ContactManager> contactManager, |
| 48 | const ndn::Name& chatroomPrefix, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 49 | const ndn::Name& localPrefix, |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 50 | const ndn::Name& defaultIdentity, |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame^] | 51 | const std::string& nick, |
| 52 | bool trial = false, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 53 | QWidget *parent = 0); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 54 | |
| 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 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 61 | ~ChatDialog(); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 62 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 63 | const ndn::Name& |
| 64 | getChatroomPrefix() const |
| 65 | { return m_chatroomPrefix; } |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 67 | const ndn::Name& |
| 68 | getLocalPrefix() const |
| 69 | { return m_localPrefix; } |
| 70 | |
| 71 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 72 | 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); |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 96 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame^] | 97 | protected: |
| 98 | void closeEvent(QCloseEvent *e); |
| 99 | |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 100 | private: |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 101 | |
| 102 | void |
| 103 | initializeSetting(); |
| 104 | |
| 105 | void |
| 106 | updateLabels(); |
| 107 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 108 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame^] | 109 | setWrapper(bool trial); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 110 | |
| 111 | void |
| 112 | initializeSync(); |
| 113 | |
| 114 | void |
| 115 | publishIntroCert(ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate, bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 116 | |
| 117 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 118 | onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 119 | |
| 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 Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 130 | invitationAccepted(const ndn::Name& identity, |
| 131 | ndn::Ptr<ndn::Data> data, |
| 132 | const std::string& inviteePrefix, |
| 133 | bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 134 | |
| 135 | void |
| 136 | onUnverified(ndn::Ptr<ndn::Data> data); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 137 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame^] | 138 | void |
| 139 | onTimeout(ndn::Ptr<ndn::Closure> closure, |
| 140 | ndn::Ptr<ndn::Interest> interest); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 141 | |
| 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 | |
| 173 | signals: |
| 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 Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame^] | 183 | void |
| 184 | closeChatDialog(const ndn::Name& chatroomPrefix); |
| 185 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 186 | public 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 | |
| 196 | private 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 Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 235 | |
| 236 | private: |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 237 | Ui::ChatDialog *ui; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 238 | ndn::Ptr<ContactManager> m_contactManager; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 239 | ndn::Name m_chatroomPrefix; |
| 240 | ndn::Name m_localPrefix; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 241 | ndn::Name m_localChatPrefix; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 242 | ndn::Name m_defaultIdentity; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 243 | ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager; |
| 244 | ndn::Ptr<SyncPolicyManager> m_syncPolicyManager; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 245 | ndn::Ptr<ndn::security::IdentityManager> m_identityManager; |
| 246 | ndn::Ptr<ndn::security::Keychain> m_keychain; |
| 247 | ndn::Ptr<ndn::Wrapper> m_handler; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 248 | |
| 249 | User m_user; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame^] | 250 | std::string m_nick; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 251 | 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; |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 270 | }; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 271 | |
| 272 | #endif // ChatDIALOG_H |