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