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