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