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