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