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