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