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