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 | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 7 | #include <QMetaType> |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 8 | #include <QMessageBox> |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 9 | #include <boost/random/random_device.hpp> |
| 10 | #include <boost/random/uniform_int_distribution.hpp> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 11 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 12 | #define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo" |
| 13 | |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame^] | 14 | static const int FRESHNESS = 120; // seconds |
| 15 | static const int HELLO_INTERVAL = 90; // seconds |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 16 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 17 | void |
| 18 | ChatDialog::testDraw() |
| 19 | { |
| 20 | std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"}; |
| 21 | std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"}; |
| 22 | std::vector<Sync::MissingDataInfo> v; |
| 23 | for (int i = 0; i < 5; i++) |
| 24 | { |
| 25 | Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )}; |
| 26 | v.push_back(mdi); |
| 27 | } |
| 28 | |
| 29 | m_scene->processUpdate(v, "12341234@!#%!@"); |
| 30 | |
| 31 | for (int i = 0; i < 5; i++) |
| 32 | { |
| 33 | m_scene-> msgReceived(prefix[i].c_str(), nick[i].c_str()); |
| 34 | } |
| 35 | |
| 36 | fitView(); |
| 37 | } |
| 38 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 39 | ChatDialog::ChatDialog(QWidget *parent) |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 40 | : QDialog(parent), m_sock(NULL), m_lastMsgTime(0) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 41 | { |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 42 | // have to register this, otherwise |
| 43 | // the signal-slot system won't recognize this type |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 44 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 45 | qRegisterMetaType<size_t>("size_t"); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 46 | setupUi(this); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 47 | m_session = time(NULL); |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame^] | 48 | boost::random::random_device rng; |
| 49 | boost::random::uniform_int_distribution<> uniform(1, 29000); |
| 50 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 51 | |
| 52 | readSettings(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 53 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 54 | updateLabels(); |
| 55 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 56 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 57 | m_scene = new DigestTreeScene(this); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 58 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 59 | treeViewer->setScene(m_scene); |
| 60 | m_scene->plot("Empty"); |
| 61 | QRectF rect = m_scene->itemsBoundingRect(); |
| 62 | m_scene->setSceneRect(rect); |
| 63 | |
| 64 | // create sync socket |
| 65 | if(!m_user.getChatroom().isEmpty()) { |
| 66 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 67 | syncPrefix += "/"; |
| 68 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 69 | try |
| 70 | { |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 71 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 72 | sendHello(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 73 | } |
| 74 | catch (Sync::CcnxOperationException ex) |
| 75 | { |
| 76 | QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok); |
| 77 | std::exit(1); |
| 78 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 79 | } |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 80 | |
| 81 | createActions(); |
| 82 | createTrayIcon(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 83 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 84 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 85 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool)), this, SLOT(processData(QString, const char *, size_t, bool))); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 86 | connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 87 | connect(this, SIGNAL(removeReceived(QString)), this, SLOT(processRemove(QString))); |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 88 | connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal())); |
| 89 | connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
| 90 | |
| 91 | //testDraw(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 94 | ChatDialog::~ChatDialog() |
| 95 | { |
| 96 | if (m_sock != NULL) |
| 97 | { |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 98 | m_sock->remove(m_user.getPrefix().toStdString()); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 99 | delete m_sock; |
| 100 | m_sock = NULL; |
| 101 | } |
| 102 | } |
| 103 | |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 104 | void |
| 105 | ChatDialog::setVisible(bool visible) |
| 106 | { |
| 107 | minimizeAction->setEnabled(visible); |
| 108 | maximizeAction->setEnabled(!isMaximized()); |
| 109 | restoreAction->setEnabled(isMaximized() || !visible); |
| 110 | QDialog::setVisible(visible); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | ChatDialog::closeEvent(QCloseEvent *e) |
| 115 | { |
| 116 | if (trayIcon->isVisible()) |
| 117 | { |
| 118 | QMessageBox::information(this, tr("Sync-Demo"), |
| 119 | tr("The program will keep running in the " |
| 120 | "system tray. To terminate the program" |
| 121 | "choose <b>Quit</b> in the context memu" |
| 122 | "of the system tray entry.")); |
| 123 | hide(); |
| 124 | e->ignore(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | ChatDialog::changeEvent(QEvent *e) |
| 130 | { |
| 131 | switch(e->type()) |
| 132 | { |
| 133 | case QEvent::ActivationChange: |
| 134 | if (isActiveWindow()) |
| 135 | { |
| 136 | trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 137 | } |
| 138 | break; |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 144 | void |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 145 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg) |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 146 | { |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 147 | boost::mutex::scoped_lock lock(m_msgMutex); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 148 | |
| 149 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 150 | return; |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (!msg.has_data()) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (msg.from().empty() || msg.data().empty()) { |
| 158 | return; |
| 159 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 160 | |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 161 | if (!msg.has_timestamp()) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | QTextCharFormat nickFormat; |
| 167 | nickFormat.setForeground(Qt::darkGreen); |
| 168 | nickFormat.setFontWeight(QFont::Bold); |
| 169 | nickFormat.setFontUnderline(true); |
| 170 | nickFormat.setUnderlineColor(Qt::gray); |
| 171 | QTextCharFormat timeFormat; |
| 172 | timeFormat.setForeground(Qt::gray); |
| 173 | timeFormat.setFontUnderline(true); |
| 174 | timeFormat.setUnderlineColor(Qt::gray); |
| 175 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 176 | QTextCursor cursor(textEdit->textCursor()); |
| 177 | cursor.movePosition(QTextCursor::End); |
| 178 | QTextTableFormat tableFormat; |
| 179 | tableFormat.setBorder(0); |
| 180 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 181 | QString from = QString("%1 ").arg(msg.from().c_str()); |
| 182 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 183 | fromCell.setFormat(nickFormat); |
| 184 | fromCell.firstCursorPosition().insertText(from); |
| 185 | QTextTableCell timeCell = table->cellAt(0, 1); |
| 186 | timeCell.setFormat(timeFormat); |
| 187 | time_t timestamp = msg.timestamp(); |
| 188 | struct tm *tm_time = localtime(×tamp); |
| 189 | int hour = tm_time->tm_hour; |
| 190 | QString amOrPM; |
| 191 | if (hour > 12) |
| 192 | { |
| 193 | hour -= 12; |
| 194 | amOrPM = "PM"; |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | amOrPM = "AM"; |
| 199 | if (hour == 0) |
| 200 | { |
| 201 | hour = 12; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | char textTime[12]; |
| 206 | sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str()); |
| 207 | timeCell.firstCursorPosition().insertText(textTime); |
| 208 | |
| 209 | |
| 210 | QTextCursor nextCursor(textEdit->textCursor()); |
| 211 | table = nextCursor.insertTable(1, 1, tableFormat); |
| 212 | table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 213 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 214 | bar->setValue(bar->maximum()); |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 215 | showMessage(from, msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 218 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 219 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock) |
| 220 | { |
| 221 | emit treeUpdated(v); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 222 | #ifdef __DEBUG |
| 223 | std::cout << "<<< Tree update signal emitted" << std::endl; |
| 224 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void |
| 228 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 229 | { |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 230 | #ifdef __DEBUG |
| 231 | std::cout << "<<< processing Tree Update" << std::endl; |
| 232 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 233 | if (v.empty()) |
| 234 | { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // reflect the changes on digest tree |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 239 | { |
| 240 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 241 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 242 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 243 | |
| 244 | int n = v.size(); |
| 245 | int totalMissingPackets = 0; |
| 246 | for (int i = 0; i < n; i++) |
| 247 | { |
| 248 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 249 | } |
| 250 | |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 251 | for (int i = 0; i < n; i++) |
| 252 | { |
| 253 | if (totalMissingPackets < 4) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 254 | { |
| 255 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 256 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 257 | m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 258 | #ifdef __DEBUG |
| 259 | std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl; |
| 260 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 263 | else |
| 264 | { |
| 265 | m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2); |
| 266 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // adjust the view |
| 270 | fitView(); |
| 271 | |
| 272 | } |
| 273 | |
| 274 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 275 | ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len) |
| 276 | { |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 277 | emit dataReceived(name.c_str(), buf, len, true); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 278 | #ifdef __DEBUG |
| 279 | std::cout <<"<<< " << name << " fetched" << std::endl; |
| 280 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 284 | ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len) |
| 285 | { |
| 286 | emit dataReceived(name.c_str(), buf, len, false); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | ChatDialog::processData(QString name, const char *buf, size_t len, bool show) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 291 | { |
| 292 | SyncDemo::ChatMessage msg; |
| 293 | if (!msg.ParseFromArray(buf, len)) |
| 294 | { |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 295 | std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 298 | // display msg received from network |
| 299 | // we have to do so; this function is called by ccnd thread |
| 300 | // so if we call appendMsg directly |
| 301 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 302 | // the "cannonical" way to is use signal-slot |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 303 | if (show) |
| 304 | { |
| 305 | appendMessage(msg); |
| 306 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 307 | |
| 308 | // update the tree view |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 309 | std::string stdStrName = name.toStdString(); |
| 310 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 311 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
| 312 | #ifdef __DEBUG |
| 313 | std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl; |
| 314 | #endif |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 315 | { |
| 316 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 317 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 318 | } |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 319 | fitView(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 323 | ChatDialog::processRemoveWrapper(std::string prefix) |
| 324 | { |
| 325 | emit removeReceived(prefix.c_str()); |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | ChatDialog::processRemove(QString prefix) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 330 | { |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 331 | #ifdef __DEBUG |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 332 | std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl; |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 333 | #endif |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 334 | bool removed = m_scene->removeNode(prefix); |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 335 | if (removed) |
| 336 | { |
| 337 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 338 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 342 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 343 | msg.set_from(m_user.getNick().toStdString()); |
| 344 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 345 | msg.set_data(text.toStdString()); |
| 346 | time_t seconds = time(NULL); |
| 347 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 348 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 351 | void |
| 352 | ChatDialog::formHelloMessage(SyncDemo::ChatMessage &msg) |
| 353 | { |
| 354 | msg.set_from(m_user.getNick().toStdString()); |
| 355 | msg.set_to(m_user.getChatroom().toStdString()); |
| 356 | time_t seconds = time(NULL); |
| 357 | msg.set_timestamp(seconds); |
| 358 | msg.set_type(SyncDemo::ChatMessage::HELLO); |
| 359 | } |
| 360 | |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 361 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 362 | |
| 363 | QString |
| 364 | ChatDialog::getRandomString() |
| 365 | { |
| 366 | std::string randStr; |
| 367 | boost::random::random_device rng; |
| 368 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 369 | for (int i = 0; i < 10; i ++) |
| 370 | { |
| 371 | randStr += chars[index_dist(rng)]; |
| 372 | } |
| 373 | return randStr.c_str(); |
| 374 | } |
| 375 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 376 | bool |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 377 | ChatDialog::readSettings() |
| 378 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 379 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 380 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 381 | QString nick = s.value("nick", "").toString(); |
| 382 | QString chatroom = s.value("chatroom", "").toString(); |
| 383 | QString prefix = s.value("prefix", "").toString(); |
| 384 | if (nick == "" || chatroom == "" || prefix == "") { |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 385 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 386 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 387 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 388 | else { |
| 389 | m_user.setNick(nick); |
| 390 | m_user.setChatroom(chatroom); |
| 391 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 392 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 393 | } |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 394 | #else |
| 395 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 396 | return false; |
| 397 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void |
| 401 | ChatDialog::writeSettings() |
| 402 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 403 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 404 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 405 | s.setValue("nick", m_user.getNick()); |
| 406 | s.setValue("chatroom", m_user.getChatroom()); |
| 407 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 408 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void |
| 412 | ChatDialog::updateLabels() |
| 413 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 414 | 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] | 415 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 416 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 417 | prefixLabel->setText(prefixDisp); |
| 418 | } |
| 419 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 420 | void |
| 421 | ChatDialog::returnPressed() |
| 422 | { |
| 423 | QString text = lineEdit->text(); |
| 424 | if (text.isEmpty()) |
| 425 | return; |
| 426 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 427 | lineEdit->clear(); |
| 428 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 429 | SyncDemo::ChatMessage msg; |
| 430 | formChatMessage(text, msg); |
| 431 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 432 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 433 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 434 | sendMsg(msg); |
| 435 | |
| 436 | fitView(); |
| 437 | } |
| 438 | |
| 439 | void |
| 440 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 441 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 442 | // send msg |
| 443 | size_t size = msg.ByteSize(); |
| 444 | char *buf = new char[size]; |
| 445 | msg.SerializeToArray(buf, size); |
| 446 | if (!msg.IsInitialized()) |
| 447 | { |
| 448 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 449 | abort(); |
| 450 | } |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 451 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 452 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 453 | delete buf; |
| 454 | |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 455 | m_lastMsgTime = time(NULL); |
| 456 | |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 457 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 458 | Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)}; |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 459 | std::vector<Sync::MissingDataInfo> v; |
| 460 | v.push_back(mdi); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 461 | { |
| 462 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 463 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 464 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 465 | } |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void |
| 469 | ChatDialog::sendHello() |
| 470 | { |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 471 | time_t now = time(NULL); |
| 472 | int elapsed = now - m_lastMsgTime; |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame^] | 473 | if (elapsed >= m_randomizedInterval / 1000) |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 474 | { |
| 475 | SyncDemo::ChatMessage msg; |
| 476 | formHelloMessage(msg); |
| 477 | sendMsg(msg); |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame^] | 478 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame^] | 482 | QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello())); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 483 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 487 | ChatDialog::buttonPressed() |
| 488 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 489 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 490 | 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] | 491 | dialog.exec(); |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 492 | QTimer::singleShot(100, this, SLOT(checkSetting())); |
| 493 | } |
| 494 | |
| 495 | void |
| 496 | ChatDialog::checkSetting() |
| 497 | { |
| 498 | if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty()) |
| 499 | { |
| 500 | buttonPressed(); |
| 501 | } |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 502 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 503 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 504 | void |
| 505 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 506 | { |
| 507 | bool needWrite = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 508 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 509 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 510 | needWrite = true; |
| 511 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 512 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 513 | m_user.setPrefix(prefix + "/" + getRandomString()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 514 | needWrite = true; |
| 515 | // TODO: set the previous prefix as left? |
| 516 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 517 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 518 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 519 | needWrite = true; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 520 | |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 521 | { |
| 522 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 523 | m_scene->clearAll(); |
| 524 | m_scene->plot("Empty"); |
| 525 | } |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 526 | // 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] | 527 | if (m_sock != NULL) |
| 528 | { |
| 529 | delete m_sock; |
| 530 | m_sock = NULL; |
| 531 | } |
| 532 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 533 | syncPrefix += "/"; |
| 534 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 535 | try |
| 536 | { |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 537 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 538 | sendHello(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 539 | } |
| 540 | catch (Sync::CcnxOperationException ex) |
| 541 | { |
| 542 | QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok); |
| 543 | std::exit(1); |
| 544 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 545 | |
| 546 | fitView(); |
| 547 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 548 | } |
| 549 | if (needWrite) { |
| 550 | writeSettings(); |
| 551 | updateLabels(); |
| 552 | } |
| 553 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 554 | |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 555 | void |
| 556 | ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 557 | { |
| 558 | switch (reason) |
| 559 | { |
| 560 | case QSystemTrayIcon::Trigger: |
| 561 | case QSystemTrayIcon::DoubleClick: |
| 562 | break; |
| 563 | case QSystemTrayIcon::MiddleClick: |
| 564 | // showMessage(); |
| 565 | break; |
| 566 | default:; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void |
| 571 | ChatDialog::showMessage(QString from, QString data) |
| 572 | { |
| 573 | // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl; |
| 574 | if (!isActiveWindow()) |
| 575 | { |
| 576 | trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000); |
| 577 | trayIcon->setIcon(QIcon(":/images/note.png")); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void |
| 582 | ChatDialog::messageClicked() |
| 583 | { |
| 584 | this->showMaximized(); |
| 585 | } |
| 586 | |
| 587 | void |
| 588 | ChatDialog::createActions() |
| 589 | { |
| 590 | minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 591 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); |
| 592 | |
| 593 | maximizeAction = new QAction(tr("Ma&ximize"), this); |
| 594 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); |
| 595 | |
| 596 | restoreAction = new QAction(tr("&Restore"), this); |
| 597 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); |
| 598 | |
| 599 | quitAction = new QAction(tr("Quit"), this); |
| 600 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | ChatDialog::createTrayIcon() |
| 605 | { |
| 606 | trayIconMenu = new QMenu(this); |
| 607 | trayIconMenu->addAction(minimizeAction); |
| 608 | trayIconMenu->addAction(maximizeAction); |
| 609 | trayIconMenu->addAction(restoreAction); |
| 610 | trayIconMenu->addSeparator(); |
| 611 | trayIconMenu->addAction(quitAction); |
| 612 | |
| 613 | trayIcon = new QSystemTrayIcon(this); |
| 614 | trayIcon->setContextMenu(trayIconMenu); |
| 615 | |
| 616 | QIcon icon(":/images/icon_small.png"); |
| 617 | trayIcon->setIcon(icon); |
| 618 | setWindowIcon(icon); |
| 619 | trayIcon->setToolTip("Sync-Demo System Tray Icon"); |
| 620 | trayIcon->setVisible(true); |
| 621 | } |
| 622 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 623 | void |
| 624 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 625 | { |
| 626 | fitView(); |
| 627 | } |
| 628 | |
| 629 | void |
| 630 | ChatDialog::showEvent(QShowEvent *e) |
| 631 | { |
| 632 | fitView(); |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | ChatDialog::fitView() |
| 637 | { |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 638 | boost::mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 639 | QRectF rect = m_scene->itemsBoundingRect(); |
| 640 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 641 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 642 | } |