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 | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 82 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t)), this, SLOT(processData(QString, const char *, size_t))); |
| 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 | |
| 158 | QTextCursor cursor(textEdit->textCursor()); |
| 159 | cursor.movePosition(QTextCursor::End); |
| 160 | QTextTableFormat tableFormat; |
| 161 | tableFormat.setBorder(0); |
| 162 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 163 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 164 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 165 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 166 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 167 | bar->setValue(bar->maximum()); |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 168 | showMessage(from, msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 171 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 172 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock) |
| 173 | { |
| 174 | emit treeUpdated(v); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 175 | #ifdef __DEBUG |
| 176 | std::cout << "<<< Tree update signal emitted" << std::endl; |
| 177 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
| 181 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 182 | { |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 183 | #ifdef __DEBUG |
| 184 | std::cout << "<<< processing Tree Update" << std::endl; |
| 185 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 186 | if (v.empty()) |
| 187 | { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // reflect the changes on digest tree |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 192 | { |
| 193 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 194 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 195 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 196 | |
| 197 | int n = v.size(); |
| 198 | int totalMissingPackets = 0; |
| 199 | for (int i = 0; i < n; i++) |
| 200 | { |
| 201 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 202 | } |
| 203 | |
| 204 | if (totalMissingPackets < 10) { |
| 205 | for (int i = 0; i < n; i++) |
| 206 | { |
| 207 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 208 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 209 | 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] | 210 | #ifdef __DEBUG |
| 211 | std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl; |
| 212 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | // too bad; too many missing packets |
| 219 | // we may just join a new chatroom |
| 220 | // or some network patition just healed |
| 221 | // we don't try to fetch any data in this case (for now) |
| 222 | } |
| 223 | |
| 224 | // adjust the view |
| 225 | fitView(); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 230 | ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len) |
| 231 | { |
| 232 | emit dataReceived(name.c_str(), buf, len); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 233 | #ifdef __DEBUG |
| 234 | std::cout <<"<<< " << name << " fetched" << std::endl; |
| 235 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void |
| 239 | ChatDialog::processData(QString name, const char *buf, size_t len) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 240 | { |
| 241 | SyncDemo::ChatMessage msg; |
| 242 | if (!msg.ParseFromArray(buf, len)) |
| 243 | { |
| 244 | std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 245 | abort(); |
| 246 | } |
| 247 | |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 248 | // display msg received from network |
| 249 | // we have to do so; this function is called by ccnd thread |
| 250 | // so if we call appendMsg directly |
| 251 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 252 | // the "cannonical" way to is use signal-slot |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 253 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 254 | |
| 255 | // update the tree view |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 256 | std::string stdStrName = name.toStdString(); |
| 257 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 258 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
| 259 | #ifdef __DEBUG |
| 260 | std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl; |
| 261 | #endif |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 262 | { |
| 263 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 264 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 265 | } |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 266 | fitView(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 270 | ChatDialog::processRemoveWrapper(std::string prefix) |
| 271 | { |
| 272 | emit removeReceived(prefix.c_str()); |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | ChatDialog::processRemove(QString prefix) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 277 | { |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 278 | #ifdef __DEBUG |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 279 | std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl; |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 280 | #endif |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 281 | bool removed = m_scene->removeNode(prefix); |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 282 | if (removed) |
| 283 | { |
| 284 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 285 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 289 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 290 | msg.set_from(m_user.getNick().toStdString()); |
| 291 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 292 | msg.set_data(text.toStdString()); |
| 293 | time_t seconds = time(NULL); |
| 294 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 295 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 298 | void |
| 299 | ChatDialog::formHelloMessage(SyncDemo::ChatMessage &msg) |
| 300 | { |
| 301 | msg.set_from(m_user.getNick().toStdString()); |
| 302 | msg.set_to(m_user.getChatroom().toStdString()); |
| 303 | time_t seconds = time(NULL); |
| 304 | msg.set_timestamp(seconds); |
| 305 | msg.set_type(SyncDemo::ChatMessage::HELLO); |
| 306 | } |
| 307 | |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 308 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 309 | |
| 310 | QString |
| 311 | ChatDialog::getRandomString() |
| 312 | { |
| 313 | std::string randStr; |
| 314 | boost::random::random_device rng; |
| 315 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 316 | for (int i = 0; i < 10; i ++) |
| 317 | { |
| 318 | randStr += chars[index_dist(rng)]; |
| 319 | } |
| 320 | return randStr.c_str(); |
| 321 | } |
| 322 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 323 | bool |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 324 | ChatDialog::readSettings() |
| 325 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 326 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 327 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 328 | QString nick = s.value("nick", "").toString(); |
| 329 | QString chatroom = s.value("chatroom", "").toString(); |
| 330 | QString prefix = s.value("prefix", "").toString(); |
| 331 | if (nick == "" || chatroom == "" || prefix == "") { |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 332 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 333 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 334 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 335 | else { |
| 336 | m_user.setNick(nick); |
| 337 | m_user.setChatroom(chatroom); |
| 338 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 339 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 340 | } |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 341 | #else |
| 342 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 343 | return false; |
| 344 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void |
| 348 | ChatDialog::writeSettings() |
| 349 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 350 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 351 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 352 | s.setValue("nick", m_user.getNick()); |
| 353 | s.setValue("chatroom", m_user.getChatroom()); |
| 354 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 355 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void |
| 359 | ChatDialog::updateLabels() |
| 360 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 361 | 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] | 362 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 363 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 364 | prefixLabel->setText(prefixDisp); |
| 365 | } |
| 366 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 367 | void |
| 368 | ChatDialog::returnPressed() |
| 369 | { |
| 370 | QString text = lineEdit->text(); |
| 371 | if (text.isEmpty()) |
| 372 | return; |
| 373 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 374 | lineEdit->clear(); |
| 375 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 376 | SyncDemo::ChatMessage msg; |
| 377 | formChatMessage(text, msg); |
| 378 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 379 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 380 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 381 | sendMsg(msg); |
| 382 | |
| 383 | fitView(); |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 388 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 389 | // send msg |
| 390 | size_t size = msg.ByteSize(); |
| 391 | char *buf = new char[size]; |
| 392 | msg.SerializeToArray(buf, size); |
| 393 | if (!msg.IsInitialized()) |
| 394 | { |
| 395 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 396 | abort(); |
| 397 | } |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame^] | 398 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 399 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 400 | delete buf; |
| 401 | |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame^] | 402 | m_lastMsgTime = time(NULL); |
| 403 | |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 404 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 405 | 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] | 406 | std::vector<Sync::MissingDataInfo> v; |
| 407 | v.push_back(mdi); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 408 | { |
| 409 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 410 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 411 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 412 | } |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void |
| 416 | ChatDialog::sendHello() |
| 417 | { |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame^] | 418 | time_t now = time(NULL); |
| 419 | int elapsed = now - m_lastMsgTime; |
| 420 | if (elapsed >= HELLO_INTERVAL) |
| 421 | { |
| 422 | SyncDemo::ChatMessage msg; |
| 423 | formHelloMessage(msg); |
| 424 | sendMsg(msg); |
| 425 | QTimer::singleShot(HELLO_INTERVAL * 1000, this, SLOT(sendHello())); |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | QTimer::singleShot((HELLO_INTERVAL - elapsed) * 1000, this, SLOT(sendHello())); |
| 430 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 434 | ChatDialog::buttonPressed() |
| 435 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 436 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 437 | 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] | 438 | dialog.exec(); |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 439 | QTimer::singleShot(100, this, SLOT(checkSetting())); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | ChatDialog::checkSetting() |
| 444 | { |
| 445 | if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty()) |
| 446 | { |
| 447 | buttonPressed(); |
| 448 | } |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 449 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 450 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 451 | void |
| 452 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 453 | { |
| 454 | bool needWrite = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 455 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 456 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 457 | needWrite = true; |
| 458 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 459 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 460 | m_user.setPrefix(prefix + "/" + getRandomString()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 461 | needWrite = true; |
| 462 | // TODO: set the previous prefix as left? |
| 463 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 464 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 465 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 466 | needWrite = true; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 467 | |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 468 | { |
| 469 | boost::mutex::scoped_lock lock(m_sceneMutex); |
| 470 | m_scene->clearAll(); |
| 471 | m_scene->plot("Empty"); |
| 472 | } |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 473 | // 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] | 474 | if (m_sock != NULL) |
| 475 | { |
| 476 | delete m_sock; |
| 477 | m_sock = NULL; |
| 478 | } |
| 479 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 480 | syncPrefix += "/"; |
| 481 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 482 | try |
| 483 | { |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 484 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 485 | sendHello(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 486 | } |
| 487 | catch (Sync::CcnxOperationException ex) |
| 488 | { |
| 489 | QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok); |
| 490 | std::exit(1); |
| 491 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 492 | |
| 493 | fitView(); |
| 494 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 495 | } |
| 496 | if (needWrite) { |
| 497 | writeSettings(); |
| 498 | updateLabels(); |
| 499 | } |
| 500 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 501 | |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 502 | void |
| 503 | ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 504 | { |
| 505 | switch (reason) |
| 506 | { |
| 507 | case QSystemTrayIcon::Trigger: |
| 508 | case QSystemTrayIcon::DoubleClick: |
| 509 | break; |
| 510 | case QSystemTrayIcon::MiddleClick: |
| 511 | // showMessage(); |
| 512 | break; |
| 513 | default:; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void |
| 518 | ChatDialog::showMessage(QString from, QString data) |
| 519 | { |
| 520 | // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl; |
| 521 | if (!isActiveWindow()) |
| 522 | { |
| 523 | trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000); |
| 524 | trayIcon->setIcon(QIcon(":/images/note.png")); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void |
| 529 | ChatDialog::messageClicked() |
| 530 | { |
| 531 | this->showMaximized(); |
| 532 | } |
| 533 | |
| 534 | void |
| 535 | ChatDialog::createActions() |
| 536 | { |
| 537 | minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 538 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); |
| 539 | |
| 540 | maximizeAction = new QAction(tr("Ma&ximize"), this); |
| 541 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); |
| 542 | |
| 543 | restoreAction = new QAction(tr("&Restore"), this); |
| 544 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); |
| 545 | |
| 546 | quitAction = new QAction(tr("Quit"), this); |
| 547 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
| 548 | } |
| 549 | |
| 550 | void |
| 551 | ChatDialog::createTrayIcon() |
| 552 | { |
| 553 | trayIconMenu = new QMenu(this); |
| 554 | trayIconMenu->addAction(minimizeAction); |
| 555 | trayIconMenu->addAction(maximizeAction); |
| 556 | trayIconMenu->addAction(restoreAction); |
| 557 | trayIconMenu->addSeparator(); |
| 558 | trayIconMenu->addAction(quitAction); |
| 559 | |
| 560 | trayIcon = new QSystemTrayIcon(this); |
| 561 | trayIcon->setContextMenu(trayIconMenu); |
| 562 | |
| 563 | QIcon icon(":/images/icon_small.png"); |
| 564 | trayIcon->setIcon(icon); |
| 565 | setWindowIcon(icon); |
| 566 | trayIcon->setToolTip("Sync-Demo System Tray Icon"); |
| 567 | trayIcon->setVisible(true); |
| 568 | } |
| 569 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 570 | void |
| 571 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 572 | { |
| 573 | fitView(); |
| 574 | } |
| 575 | |
| 576 | void |
| 577 | ChatDialog::showEvent(QShowEvent *e) |
| 578 | { |
| 579 | fitView(); |
| 580 | } |
| 581 | |
| 582 | void |
| 583 | ChatDialog::fitView() |
| 584 | { |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 585 | boost::mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 586 | QRectF rect = m_scene->itemsBoundingRect(); |
| 587 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 588 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 589 | } |