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 | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 21 | #include <QMenu> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 22 | |
| 23 | #include "invitelistdialog.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 25 | #ifndef Q_MOC_RUN |
| 26 | #include <ndn.cxx/data.h> |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 27 | #include <ndn.cxx/security/keychain.h> |
| 28 | #include <ndn.cxx/wrapper/wrapper.h> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 29 | #include "invitation-policy-manager.h" |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 30 | #include "contact-item.h" |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 4a97931 | 2013-11-07 15:30:05 -0800 | [diff] [blame] | 32 | #include <sync-socket.h> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 33 | #include <sync-seq-no.h> |
| 34 | #include "chatbuf.pb.h" |
| 35 | #include "digesttreescene.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 38 | #define MAX_HISTORY_ENTRY 20 |
| 39 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 40 | namespace Ui { |
| 41 | class ChatDialog; |
| 42 | } |
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 | class ChatDialog : public QDialog |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 45 | { |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 46 | Q_OBJECT |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 47 | |
| 48 | public: |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 49 | explicit ChatDialog(ndn::Ptr<ContactManager> contactManager, |
| 50 | const ndn::Name& chatroomPrefix, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 51 | const ndn::Name& localPrefix, |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 52 | const ndn::Name& defaultIdentity, |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 53 | const std::string& nick, |
| 54 | bool trial = false, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 55 | QWidget *parent = 0); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 56 | |
| 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 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 63 | ~ChatDialog(); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 64 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 65 | const ndn::Name& |
| 66 | getChatroomPrefix() const |
| 67 | { return m_chatroomPrefix; } |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 69 | const ndn::Name& |
| 70 | getLocalPrefix() const |
| 71 | { return m_localPrefix; } |
| 72 | |
| 73 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 74 | 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); |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 99 | void |
| 100 | publishIntroCert(const ndn::security::IdentityCertificate& dskCertificate, bool isIntroducer); |
| 101 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 102 | protected: |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 103 | void |
| 104 | closeEvent(QCloseEvent *e); |
| 105 | |
| 106 | void |
| 107 | changeEvent(QEvent *e); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 108 | |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 109 | private: |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 110 | |
| 111 | void |
| 112 | initializeSetting(); |
| 113 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 114 | QString |
| 115 | getRandomString(); |
| 116 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 117 | void |
| 118 | updateLabels(); |
| 119 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 120 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 121 | setWrapper(bool trial); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 122 | |
| 123 | void |
| 124 | initializeSync(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 125 | |
| 126 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 127 | onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 128 | |
| 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 Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 139 | invitationAccepted(const ndn::Name& identity, |
| 140 | ndn::Ptr<ndn::Data> data, |
| 141 | const std::string& inviteePrefix, |
| 142 | bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 143 | |
| 144 | void |
| 145 | onUnverified(ndn::Ptr<ndn::Data> data); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 146 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 147 | void |
| 148 | onTimeout(ndn::Ptr<ndn::Closure> closure, |
| 149 | ndn::Ptr<ndn::Interest> interest); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 150 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 151 | void |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame^] | 152 | onLocalPrefix(ndn::Ptr<ndn::Data> data); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 153 | |
| 154 | void |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame^] | 155 | onLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure, |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 156 | ndn::Ptr<ndn::Interest> interest); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 157 | |
| 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 Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 179 | void |
| 180 | createActions(); |
| 181 | |
| 182 | void |
| 183 | createTrayIcon(); |
| 184 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 185 | QString |
| 186 | formatTime(time_t); |
| 187 | |
| 188 | void |
| 189 | printTimeInCell(QTextTable *, time_t); |
| 190 | |
| 191 | void |
| 192 | disableTreeDisplay(); |
| 193 | |
| 194 | signals: |
| 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 Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 204 | void |
| 205 | closeChatDialog(const ndn::Name& chatroomPrefix); |
| 206 | |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 207 | void |
| 208 | noNdnConnection(const QString& msg); |
| 209 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 210 | public slots: |
| 211 | void |
| 212 | processTreeUpdate(const std::vector<Sync::MissingDataInfo>); |
| 213 | |
| 214 | void |
| 215 | processData(QString name, const char *buf, size_t len, bool show, bool isHistory); |
| 216 | |
| 217 | void |
| 218 | processRemove(QString prefix); |
| 219 | |
| 220 | private slots: |
| 221 | void |
| 222 | returnPressed(); |
| 223 | |
| 224 | void |
| 225 | treeButtonPressed(); |
| 226 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 227 | void |
| 228 | settingUpdated(QString, QString, QString); |
| 229 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 230 | void |
| 231 | sendJoin(); |
| 232 | |
| 233 | void |
| 234 | sendHello(); |
| 235 | |
| 236 | void |
| 237 | sendLeave(); |
| 238 | |
| 239 | void |
| 240 | replot(); |
| 241 | |
| 242 | void |
| 243 | updateRosterList(QStringList); |
| 244 | |
| 245 | void |
| 246 | enableTreeDisplay(); |
| 247 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 248 | void |
| 249 | updateLocalPrefix(); |
| 250 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 251 | void |
| 252 | summonReaper(); |
| 253 | |
| 254 | void |
| 255 | reap(); |
| 256 | |
| 257 | void |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 258 | iconActivated(QSystemTrayIcon::ActivationReason reason); |
| 259 | |
| 260 | void |
| 261 | messageClicked(); |
| 262 | |
| 263 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 264 | showMessage(QString, QString); |
| 265 | |
| 266 | void |
| 267 | openInviteListDialog(); |
| 268 | |
| 269 | void |
| 270 | sendInvitationWrapper(QString, bool); |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 271 | |
| 272 | void |
| 273 | quit(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 274 | |
| 275 | private: |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 276 | Ui::ChatDialog *ui; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 277 | ndn::Ptr<ContactManager> m_contactManager; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 278 | ndn::Name m_chatroomPrefix; |
| 279 | ndn::Name m_localPrefix; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 280 | ndn::Name m_localChatPrefix; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 281 | ndn::Name m_defaultIdentity; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 282 | ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager; |
| 283 | ndn::Ptr<SyncPolicyManager> m_syncPolicyManager; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 284 | ndn::Ptr<ndn::security::IdentityManager> m_identityManager; |
| 285 | ndn::Ptr<ndn::security::Keychain> m_keychain; |
| 286 | ndn::Ptr<ndn::Wrapper> m_handler; |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame^] | 287 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 288 | ndn::Ptr<ndn::Wrapper> m_localPrefixHandler; |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame^] | 289 | ndn::Name m_newLocalPrefix; |
| 290 | bool m_newLocalPrefixReady; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 291 | |
| 292 | User m_user; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 293 | std::string m_nick; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 294 | Sync::SyncSocket *m_sock; |
| 295 | uint32_t m_session; |
| 296 | DigestTreeScene *m_scene; |
| 297 | boost::recursive_mutex m_msgMutex; |
| 298 | boost::recursive_mutex m_sceneMutex; |
| 299 | time_t m_lastMsgTime; |
| 300 | int m_randomizedInterval; |
| 301 | QTimer *m_timer; |
| 302 | QStringListModel *m_rosterModel; |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 303 | QSystemTrayIcon *trayIcon; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 304 | |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 305 | QAction *minimizeAction; |
| 306 | QAction *maximizeAction; |
| 307 | QAction *restoreAction; |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 308 | QAction *updateLocalPrefixAction; |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 309 | QAction *quitAction; |
| 310 | QMenu *trayIconMenu; |
| 311 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 312 | // QQueue<SyncDemo::ChatMessage> m_history; |
| 313 | // bool m_historyInitialized; |
| 314 | bool m_joined; |
| 315 | |
| 316 | QList<QString> m_zombieList; |
| 317 | int m_zombieIndex; |
| 318 | |
| 319 | InviteListDialog* m_inviteListDialog; |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 320 | }; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 321 | |
| 322 | #endif // ChatDIALOG_H |