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