Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 6 | #include <QTimer> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 7 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 8 | #define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo" |
| 9 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 10 | ChatDialog::ChatDialog(QWidget *parent) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 11 | : QDialog(parent), m_sock(NULL) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 12 | { |
| 13 | setupUi(this); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 14 | m_session = time(NULL); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 15 | |
| 16 | readSettings(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 17 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 18 | updateLabels(); |
| 19 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 20 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 21 | m_scene = new DigestTreeScene(this); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 22 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 23 | treeViewer->setScene(m_scene); |
| 24 | m_scene->plot("Empty"); |
| 25 | QRectF rect = m_scene->itemsBoundingRect(); |
| 26 | m_scene->setSceneRect(rect); |
| 27 | |
| 28 | // create sync socket |
| 29 | if(!m_user.getChatroom().isEmpty()) { |
| 30 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 31 | syncPrefix += "/"; |
| 32 | syncPrefix += m_user.getChatroom().toStdString(); |
| 33 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdate, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
| 34 | } |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 35 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 36 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 37 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 40 | ChatDialog::~ChatDialog() |
| 41 | { |
| 42 | if (m_sock != NULL) |
| 43 | { |
| 44 | delete m_sock; |
| 45 | m_sock = NULL; |
| 46 | } |
| 47 | } |
| 48 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 49 | void |
| 50 | ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg) |
| 51 | { |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 52 | |
| 53 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 54 | return; |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (!msg.has_data()) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if (msg.from().empty() || msg.data().empty()) { |
| 62 | return; |
| 63 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 64 | |
| 65 | QTextCursor cursor(textEdit->textCursor()); |
| 66 | cursor.movePosition(QTextCursor::End); |
| 67 | QTextTableFormat tableFormat; |
| 68 | tableFormat.setBorder(0); |
| 69 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 70 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 71 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 72 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 73 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 74 | bar->setValue(bar->maximum()); |
| 75 | } |
| 76 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 77 | void |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 78 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> &v, Sync::SyncAppSocket *) |
| 79 | { |
| 80 | if (v.empty()) |
| 81 | { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // reflect the changes on digest tree |
| 86 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 87 | |
| 88 | int n = v.size(); |
| 89 | int totalMissingPackets = 0; |
| 90 | for (int i = 0; i < n; i++) |
| 91 | { |
| 92 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 93 | } |
| 94 | |
| 95 | if (totalMissingPackets < 10) { |
| 96 | for (int i = 0; i < n; i++) |
| 97 | { |
| 98 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 99 | { |
| 100 | m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processData, this, _1, _2, _3), 2); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | // too bad; too many missing packets |
| 107 | // we may just join a new chatroom |
| 108 | // or some network patition just healed |
| 109 | // we don't try to fetch any data in this case (for now) |
| 110 | } |
| 111 | |
| 112 | // adjust the view |
| 113 | fitView(); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | ChatDialog::processData(std::string name, const char *buf, size_t len) |
| 119 | { |
| 120 | SyncDemo::ChatMessage msg; |
| 121 | if (!msg.ParseFromArray(buf, len)) |
| 122 | { |
| 123 | std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 124 | abort(); |
| 125 | } |
| 126 | |
| 127 | // display |
| 128 | appendMessage(msg); |
| 129 | |
| 130 | // update the tree view |
| 131 | std::string prefix = name.substr(0, name.find_last_of('/')); |
| 132 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | ChatDialog::processRemove(std::string prefix) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 141 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 142 | msg.set_from(m_user.getNick().toStdString()); |
| 143 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 144 | msg.set_data(text.toStdString()); |
| 145 | time_t seconds = time(NULL); |
| 146 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 147 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 150 | bool |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 151 | ChatDialog::readSettings() |
| 152 | { |
| 153 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 154 | QString nick = s.value("nick", "").toString(); |
| 155 | QString chatroom = s.value("chatroom", "").toString(); |
| 156 | QString prefix = s.value("prefix", "").toString(); |
| 157 | if (nick == "" || chatroom == "" || prefix == "") { |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 158 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 159 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 160 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 161 | else { |
| 162 | m_user.setNick(nick); |
| 163 | m_user.setChatroom(chatroom); |
| 164 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 165 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 166 | } |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void |
| 170 | ChatDialog::writeSettings() |
| 171 | { |
| 172 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 173 | s.setValue("nick", m_user.getNick()); |
| 174 | s.setValue("chatroom", m_user.getChatroom()); |
| 175 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void |
| 179 | ChatDialog::updateLabels() |
| 180 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 181 | QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 182 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 183 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 184 | prefixLabel->setText(prefixDisp); |
| 185 | } |
| 186 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 187 | void |
| 188 | ChatDialog::returnPressed() |
| 189 | { |
| 190 | QString text = lineEdit->text(); |
| 191 | if (text.isEmpty()) |
| 192 | return; |
| 193 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 194 | lineEdit->clear(); |
| 195 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 196 | SyncDemo::ChatMessage msg; |
| 197 | formChatMessage(text, msg); |
| 198 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 199 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 200 | |
| 201 | // send msg |
| 202 | size_t size = msg.ByteSize(); |
| 203 | char *buf = new char[size]; |
| 204 | msg.SerializeToArray(buf, size); |
| 205 | if (!msg.IsInitialized()) |
| 206 | { |
| 207 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 208 | abort(); |
| 209 | } |
| 210 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 211 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 215 | ChatDialog::buttonPressed() |
| 216 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 217 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 218 | connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString))); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 219 | dialog.exec(); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 220 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 221 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 222 | void |
| 223 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 224 | { |
| 225 | bool needWrite = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 226 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 227 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 228 | needWrite = true; |
| 229 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 230 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
| 231 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 232 | needWrite = true; |
| 233 | // TODO: set the previous prefix as left? |
| 234 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 235 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 236 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 237 | needWrite = true; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 238 | |
| 239 | m_scene->clearAll(); |
| 240 | m_scene->plot("Empty"); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 241 | // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 242 | if (m_sock != NULL) |
| 243 | { |
| 244 | delete m_sock; |
| 245 | m_sock = NULL; |
| 246 | } |
| 247 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 248 | syncPrefix += "/"; |
| 249 | syncPrefix += m_user.getChatroom().toStdString(); |
| 250 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdate, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
| 251 | |
| 252 | fitView(); |
| 253 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 254 | } |
| 255 | if (needWrite) { |
| 256 | writeSettings(); |
| 257 | updateLabels(); |
| 258 | } |
| 259 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 260 | |
| 261 | void |
| 262 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 263 | { |
| 264 | fitView(); |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | ChatDialog::showEvent(QShowEvent *e) |
| 269 | { |
| 270 | fitView(); |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | ChatDialog::fitView() |
| 275 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 276 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 277 | } |