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