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