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