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 | |
| 114 | void |
| 115 | updateLabels(); |
| 116 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 117 | void |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 118 | setWrapper(bool trial); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 119 | |
| 120 | void |
| 121 | initializeSync(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 122 | |
| 123 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 124 | onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 125 | |
| 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 Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 136 | invitationAccepted(const ndn::Name& identity, |
| 137 | ndn::Ptr<ndn::Data> data, |
| 138 | const std::string& inviteePrefix, |
| 139 | bool isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 140 | |
| 141 | void |
| 142 | onUnverified(ndn::Ptr<ndn::Data> data); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 143 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 144 | void |
| 145 | onTimeout(ndn::Ptr<ndn::Closure> closure, |
| 146 | ndn::Ptr<ndn::Interest> interest); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 147 | |
| 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 Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 170 | void |
| 171 | createActions(); |
| 172 | |
| 173 | void |
| 174 | createTrayIcon(); |
| 175 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 176 | QString |
| 177 | formatTime(time_t); |
| 178 | |
| 179 | void |
| 180 | printTimeInCell(QTextTable *, time_t); |
| 181 | |
| 182 | void |
| 183 | disableTreeDisplay(); |
| 184 | |
| 185 | signals: |
| 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 Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 195 | void |
| 196 | closeChatDialog(const ndn::Name& chatroomPrefix); |
| 197 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 198 | public slots: |
| 199 | void |
| 200 | processTreeUpdate(const std::vector<Sync::MissingDataInfo>); |
| 201 | |
| 202 | void |
| 203 | processData(QString name, const char *buf, size_t len, bool show, bool isHistory); |
| 204 | |
| 205 | void |
| 206 | processRemove(QString prefix); |
| 207 | |
| 208 | private slots: |
| 209 | void |
| 210 | returnPressed(); |
| 211 | |
| 212 | void |
| 213 | treeButtonPressed(); |
| 214 | |
| 215 | void |
| 216 | sendJoin(); |
| 217 | |
| 218 | void |
| 219 | sendHello(); |
| 220 | |
| 221 | void |
| 222 | sendLeave(); |
| 223 | |
| 224 | void |
| 225 | replot(); |
| 226 | |
| 227 | void |
| 228 | updateRosterList(QStringList); |
| 229 | |
| 230 | void |
| 231 | enableTreeDisplay(); |
| 232 | |
| 233 | void |
| 234 | summonReaper(); |
| 235 | |
| 236 | void |
| 237 | reap(); |
| 238 | |
| 239 | void |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 240 | iconActivated(QSystemTrayIcon::ActivationReason reason); |
| 241 | |
| 242 | void |
| 243 | messageClicked(); |
| 244 | |
| 245 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 246 | showMessage(QString, QString); |
| 247 | |
| 248 | void |
| 249 | openInviteListDialog(); |
| 250 | |
| 251 | void |
| 252 | sendInvitationWrapper(QString, bool); |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame^] | 253 | |
| 254 | void |
| 255 | quit(); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 256 | |
| 257 | private: |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 258 | Ui::ChatDialog *ui; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 259 | ndn::Ptr<ContactManager> m_contactManager; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 260 | ndn::Name m_chatroomPrefix; |
| 261 | ndn::Name m_localPrefix; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 262 | ndn::Name m_localChatPrefix; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 263 | ndn::Name m_defaultIdentity; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 264 | ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager; |
| 265 | ndn::Ptr<SyncPolicyManager> m_syncPolicyManager; |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 266 | ndn::Ptr<ndn::security::IdentityManager> m_identityManager; |
| 267 | ndn::Ptr<ndn::security::Keychain> m_keychain; |
| 268 | ndn::Ptr<ndn::Wrapper> m_handler; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 269 | |
| 270 | User m_user; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 271 | std::string m_nick; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 272 | Sync::SyncSocket *m_sock; |
| 273 | uint32_t m_session; |
| 274 | DigestTreeScene *m_scene; |
| 275 | boost::recursive_mutex m_msgMutex; |
| 276 | boost::recursive_mutex m_sceneMutex; |
| 277 | time_t m_lastMsgTime; |
| 278 | int m_randomizedInterval; |
| 279 | QTimer *m_timer; |
| 280 | QStringListModel *m_rosterModel; |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 281 | QSystemTrayIcon *trayIcon; |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 282 | |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame^] | 283 | QAction *minimizeAction; |
| 284 | QAction *maximizeAction; |
| 285 | QAction *restoreAction; |
| 286 | QAction *quitAction; |
| 287 | QMenu *trayIconMenu; |
| 288 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 289 | // QQueue<SyncDemo::ChatMessage> m_history; |
| 290 | // bool m_historyInitialized; |
| 291 | bool m_joined; |
| 292 | |
| 293 | QList<QString> m_zombieList; |
| 294 | int m_zombieIndex; |
| 295 | |
| 296 | InviteListDialog* m_inviteListDialog; |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 297 | }; |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 298 | |
| 299 | #endif // ChatDIALOG_H |