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