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 | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 11 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 12 | #include <stdio.h> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 13 | |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 14 | #define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/chronos" |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 15 | #define LOCAL_PREFIX_QUERY "/local/ndn/prefix" |
| 16 | #define DEFAULT_LOCAL_PREFIX "/private/local" |
Zhenkai Zhu | 756666a | 2012-10-07 00:24:35 -0700 | [diff] [blame] | 17 | #define CCN_EXEC "/usr/local/bin/ccnpeek" |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 18 | |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame] | 19 | static const int HELLO_INTERVAL = 90; // seconds |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 20 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 21 | ChatDialog::ChatDialog(QWidget *parent) |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 22 | : QDialog(parent), m_sock(NULL), m_lastMsgTime(0), m_historyInitialized(false) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 23 | { |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 24 | // have to register this, otherwise |
| 25 | // the signal-slot system won't recognize this type |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 26 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 27 | qRegisterMetaType<size_t>("size_t"); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 28 | setupUi(this); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 29 | m_session = time(NULL); |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame] | 30 | boost::random::random_device rng; |
| 31 | boost::random::uniform_int_distribution<> uniform(1, 29000); |
| 32 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 33 | |
| 34 | readSettings(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 35 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 36 | updateLabels(); |
| 37 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 38 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 39 | m_scene = new DigestTreeScene(this); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 40 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 41 | treeViewer->setScene(m_scene); |
| 42 | m_scene->plot("Empty"); |
| 43 | QRectF rect = m_scene->itemsBoundingRect(); |
| 44 | m_scene->setSceneRect(rect); |
| 45 | |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 46 | listView->setStyleSheet("QListView { alternate-background-color: white; background: #F0F0F0; color: darkGreen; font: bold large; }"); |
Zhenkai Zhu | f55f438 | 2012-09-28 10:58:54 -0700 | [diff] [blame] | 47 | listView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 48 | listView->setDragDropMode(QAbstractItemView::NoDragDrop); |
| 49 | listView->setSelectionMode(QAbstractItemView::NoSelection); |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 50 | m_rosterModel = new QStringListModel(this); |
| 51 | listView->setModel(m_rosterModel); |
| 52 | |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 53 | createActions(); |
| 54 | createTrayIcon(); |
| 55 | m_timer = new QTimer(this); |
| 56 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
| 57 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | b60b7e1 | 2012-09-28 11:34:21 -0700 | [diff] [blame] | 58 | connect(treeButton, SIGNAL(pressed()), this, SLOT(treeButtonPressed())); |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 59 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), this, SLOT(processData(QString, const char *, size_t, bool, bool))); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 60 | connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 61 | connect(m_timer, SIGNAL(timeout()), this, SLOT(replot())); |
| 62 | connect(m_scene, SIGNAL(replot()), this, SLOT(replot())); |
| 63 | connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal())); |
| 64 | connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 65 | connect(m_scene, SIGNAL(rosterChanged(QStringList)), this, SLOT(updateRosterList(QStringList))); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 66 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 67 | // create sync socket |
| 68 | if(!m_user.getChatroom().isEmpty()) { |
| 69 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 70 | syncPrefix += "/"; |
| 71 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 72 | try |
| 73 | { |
Alexander Afanasyev | 288edd8 | 2012-10-04 17:07:56 -0700 | [diff] [blame] | 74 | m_sock = new Sync::SyncAppSocket(syncPrefix, |
| 75 | bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), |
| 76 | bind(&ChatDialog::processRemoveWrapper, this, _1)); |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 77 | Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 78 | handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1)); |
Zhenkai Zhu | cf02444 | 2012-10-05 10:33:08 -0700 | [diff] [blame] | 79 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 80 | m_timer->start(FRESHNESS * 2000); |
Zhenkai Zhu | 0e7a9b2 | 2012-10-05 17:55:17 -0700 | [diff] [blame] | 81 | disableTreeDisplay(); |
| 82 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 83 | } |
| 84 | catch (Sync::CcnxOperationException ex) |
| 85 | { |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 86 | 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] | 87 | std::exit(1); |
| 88 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 89 | } |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 90 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 93 | ChatDialog::~ChatDialog() |
| 94 | { |
| 95 | if (m_sock != NULL) |
| 96 | { |
Zhenkai Zhu | cf02444 | 2012-10-05 10:33:08 -0700 | [diff] [blame] | 97 | sendLeave(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 98 | delete m_sock; |
| 99 | m_sock = NULL; |
| 100 | } |
| 101 | } |
| 102 | |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 103 | void |
Zhenkai Zhu | cf02444 | 2012-10-05 10:33:08 -0700 | [diff] [blame] | 104 | ChatDialog::sendLeave() |
| 105 | { |
| 106 | SyncDemo::ChatMessage msg; |
| 107 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 108 | sendMsg(msg); |
| 109 | usleep(500000); |
| 110 | m_sock->remove(m_user.getPrefix().toStdString()); |
| 111 | usleep(5000); |
| 112 | #ifdef __DEBUG |
| 113 | std::cout << "Sync REMOVE signal sent" << std::endl; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | void |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 118 | ChatDialog::replot() |
| 119 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 120 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 121 | m_scene->plot(m_sock->getRootDigest().c_str()); |
Zhenkai Zhu | c9e4e3c | 2012-10-02 11:47:31 -0700 | [diff] [blame] | 122 | fitView(); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 125 | void |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 126 | ChatDialog::updateRosterList(QStringList staleUserList) |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 127 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 128 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 129 | QStringList rosterList = m_scene->getRosterList(); |
| 130 | m_rosterModel->setStringList(rosterList); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 131 | QString user; |
| 132 | QStringListIterator it(staleUserList); |
| 133 | while(it.hasNext()) |
| 134 | { |
Zhenkai Zhu | 32cbdce | 2012-10-02 11:09:38 -0700 | [diff] [blame] | 135 | std::string nick = it.next().toStdString(); |
| 136 | if (nick.empty()) |
| 137 | continue; |
| 138 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 139 | SyncDemo::ChatMessage msg; |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 140 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
Zhenkai Zhu | 32cbdce | 2012-10-02 11:09:38 -0700 | [diff] [blame] | 141 | msg.set_from(nick); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 142 | appendMessage(msg); |
| 143 | } |
Zhenkai Zhu | 6082ede | 2012-09-27 17:28:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 146 | void |
| 147 | ChatDialog::setVisible(bool visible) |
| 148 | { |
| 149 | minimizeAction->setEnabled(visible); |
| 150 | maximizeAction->setEnabled(!isMaximized()); |
| 151 | restoreAction->setEnabled(isMaximized() || !visible); |
| 152 | QDialog::setVisible(visible); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | ChatDialog::closeEvent(QCloseEvent *e) |
| 157 | { |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 158 | if (trayIcon->isVisible() && !m_minimaniho) |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 159 | { |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 160 | QMessageBox::information(this, tr("Chronos"), |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 161 | tr("The program will keep running in the " |
| 162 | "system tray. To terminate the program" |
| 163 | "choose <b>Quit</b> in the context memu" |
| 164 | "of the system tray entry.")); |
| 165 | hide(); |
| 166 | e->ignore(); |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 167 | m_minimaniho = true; |
| 168 | writeSettings(); |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | ChatDialog::changeEvent(QEvent *e) |
| 174 | { |
| 175 | switch(e->type()) |
| 176 | { |
| 177 | case QEvent::ActivationChange: |
| 178 | if (isActiveWindow()) |
| 179 | { |
| 180 | trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 181 | } |
| 182 | break; |
| 183 | default: |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 188 | void |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 189 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory) |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 190 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 191 | boost::recursive_mutex::scoped_lock lock(m_msgMutex); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 192 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 193 | if (msg.type() == SyncDemo::ChatMessage::CHAT) |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 194 | { |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 195 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 196 | if (!msg.has_data()) |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 197 | { |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 198 | return; |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 199 | } |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 200 | |
| 201 | if (msg.from().empty() || msg.data().empty()) |
| 202 | { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (!msg.has_timestamp()) |
| 207 | { |
| 208 | return; |
| 209 | } |
| 210 | |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 211 | if (m_history.size() == MAX_HISTORY_ENTRY) |
| 212 | { |
| 213 | m_history.dequeue(); |
| 214 | } |
| 215 | |
| 216 | m_history.enqueue(msg); |
| 217 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 218 | QTextCharFormat nickFormat; |
| 219 | nickFormat.setForeground(Qt::darkGreen); |
| 220 | nickFormat.setFontWeight(QFont::Bold); |
| 221 | nickFormat.setFontUnderline(true); |
| 222 | nickFormat.setUnderlineColor(Qt::gray); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 223 | |
| 224 | QTextCursor cursor(textEdit->textCursor()); |
| 225 | cursor.movePosition(QTextCursor::End); |
| 226 | QTextTableFormat tableFormat; |
| 227 | tableFormat.setBorder(0); |
| 228 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 229 | QString from = QString("%1 ").arg(msg.from().c_str()); |
| 230 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 231 | fromCell.setFormat(nickFormat); |
| 232 | fromCell.firstCursorPosition().insertText(from); |
Zhenkai Zhu | 560ef1b | 2012-09-28 14:23:33 -0700 | [diff] [blame] | 233 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 234 | time_t timestamp = msg.timestamp(); |
Zhenkai Zhu | 560ef1b | 2012-09-28 14:23:33 -0700 | [diff] [blame] | 235 | printTimeInCell(table, timestamp); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 236 | |
| 237 | QTextCursor nextCursor(textEdit->textCursor()); |
| 238 | nextCursor.movePosition(QTextCursor::End); |
| 239 | table = nextCursor.insertTable(1, 1, tableFormat); |
| 240 | table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 241 | if (!isHistory) |
| 242 | { |
| 243 | showMessage(from, msg.data().c_str()); |
| 244 | } |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 247 | if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 248 | { |
| 249 | QTextCharFormat nickFormat; |
| 250 | nickFormat.setForeground(Qt::gray); |
| 251 | nickFormat.setFontWeight(QFont::Bold); |
| 252 | nickFormat.setFontUnderline(true); |
| 253 | nickFormat.setUnderlineColor(Qt::gray); |
Zhenkai Zhu | b98e602 | 2012-09-27 13:48:23 -0700 | [diff] [blame] | 254 | |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 255 | QTextCursor cursor(textEdit->textCursor()); |
| 256 | cursor.movePosition(QTextCursor::End); |
| 257 | QTextTableFormat tableFormat; |
| 258 | tableFormat.setBorder(0); |
| 259 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 260 | QString action; |
| 261 | if (msg.type() == SyncDemo::ChatMessage::JOIN) |
| 262 | { |
| 263 | action = "enters room"; |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | action = "leaves room"; |
| 268 | } |
| 269 | |
| 270 | QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action); |
| 271 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 272 | fromCell.setFormat(nickFormat); |
| 273 | fromCell.firstCursorPosition().insertText(from); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 274 | |
Zhenkai Zhu | 560ef1b | 2012-09-28 14:23:33 -0700 | [diff] [blame] | 275 | time_t timestamp = msg.timestamp(); |
| 276 | printTimeInCell(table, timestamp); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 279 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 280 | bar->setValue(bar->maximum()); |
| 281 | } |
| 282 | |
Zhenkai Zhu | 560ef1b | 2012-09-28 14:23:33 -0700 | [diff] [blame] | 283 | void |
| 284 | ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp) |
| 285 | { |
| 286 | QTextCharFormat timeFormat; |
| 287 | timeFormat.setForeground(Qt::gray); |
| 288 | timeFormat.setFontUnderline(true); |
| 289 | timeFormat.setUnderlineColor(Qt::gray); |
| 290 | QTextTableCell timeCell = table->cellAt(0, 1); |
| 291 | timeCell.setFormat(timeFormat); |
| 292 | timeCell.firstCursorPosition().insertText(formatTime(timestamp)); |
| 293 | } |
| 294 | |
| 295 | QString |
| 296 | ChatDialog::formatTime(time_t timestamp) |
| 297 | { |
| 298 | struct tm *tm_time = localtime(×tamp); |
| 299 | int hour = tm_time->tm_hour; |
| 300 | QString amOrPM; |
| 301 | if (hour > 12) |
| 302 | { |
| 303 | hour -= 12; |
| 304 | amOrPM = "PM"; |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | amOrPM = "AM"; |
| 309 | if (hour == 0) |
| 310 | { |
| 311 | hour = 12; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | char textTime[12]; |
| 316 | sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str()); |
| 317 | return QString(textTime); |
| 318 | } |
| 319 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 320 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 321 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock) |
| 322 | { |
| 323 | emit treeUpdated(v); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 324 | #ifdef __DEBUG |
| 325 | std::cout << "<<< Tree update signal emitted" << std::endl; |
| 326 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void |
| 330 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 331 | { |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 332 | #ifdef __DEBUG |
| 333 | std::cout << "<<< processing Tree Update" << std::endl; |
| 334 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 335 | if (v.empty()) |
| 336 | { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | // reflect the changes on digest tree |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 341 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 342 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 343 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 344 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 345 | |
| 346 | int n = v.size(); |
| 347 | int totalMissingPackets = 0; |
| 348 | for (int i = 0; i < n; i++) |
| 349 | { |
| 350 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 351 | } |
| 352 | |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 353 | for (int i = 0; i < n; i++) |
| 354 | { |
| 355 | if (totalMissingPackets < 4) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 356 | { |
| 357 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 358 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 359 | 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] | 360 | #ifdef __DEBUG |
| 361 | std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl; |
| 362 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 365 | else |
| 366 | { |
| 367 | m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2); |
| 368 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // adjust the view |
| 372 | fitView(); |
| 373 | |
| 374 | } |
| 375 | |
| 376 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 377 | ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len) |
| 378 | { |
Zhenkai Zhu | ae29f5a | 2012-10-05 17:28:04 -0700 | [diff] [blame] | 379 | char *tempBuf = new char[len]; |
| 380 | memcpy(tempBuf, buf, len); |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 381 | emit dataReceived(name.c_str(), tempBuf, len, true, false); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 382 | #ifdef __DEBUG |
| 383 | std::cout <<"<<< " << name << " fetched" << std::endl; |
| 384 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 388 | ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len) |
| 389 | { |
Zhenkai Zhu | ae29f5a | 2012-10-05 17:28:04 -0700 | [diff] [blame] | 390 | char *tempBuf = new char[len]; |
| 391 | memcpy(tempBuf, buf, len); |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 392 | emit dataReceived(name.c_str(), tempBuf, len, false, false); |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 393 | |
| 394 | if (!m_historyInitialized) |
| 395 | { |
| 396 | fetchHistory(name); |
| 397 | m_historyInitialized = true; |
| 398 | } |
| 399 | } |
| 400 | |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 401 | void |
| 402 | ChatDialog::processDataHistoryWrapper(std::string name, const char *buf, size_t len) |
| 403 | { |
| 404 | char *tempBuf = new char[len]; |
| 405 | memcpy(tempBuf, buf, len); |
| 406 | emit dataReceived(name.c_str(), tempBuf, len, true, true); |
| 407 | } |
| 408 | |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 409 | void |
| 410 | ChatDialog::fetchHistory(std::string name) |
| 411 | { |
| 412 | std::string nameWithoutSeq = name.substr(0, name.find_last_of('/')); |
| 413 | std::string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/')); |
| 414 | prefix += "/history"; |
| 415 | Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 416 | QString randomString = getRandomString(); |
| 417 | for (int i = 0; i < MAX_HISTORY_ENTRY; i++) |
| 418 | { |
| 419 | QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i); |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 420 | handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3)); |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | ChatDialog::respondHistoryRequest(std::string interest) |
| 426 | { |
| 427 | std::string seqStr = interest.substr(interest.find_last_of('/') + 1); |
| 428 | int seq = boost::lexical_cast<int>(seqStr); |
| 429 | if (seq >= 0 && seq < m_history.size()) |
| 430 | { |
| 431 | Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 432 | SyncDemo::ChatMessage msg = m_history.at(seq); |
| 433 | size_t size = msg.ByteSize(); |
| 434 | char *buf = new char[size]; |
| 435 | msg.SerializeToArray(buf, size); |
| 436 | handle->publishRawData(interest, buf, size, 1); |
| 437 | delete buf; |
| 438 | } |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 442 | ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 443 | { |
| 444 | SyncDemo::ChatMessage msg; |
Zhenkai Zhu | ef8d063 | 2012-10-05 10:24:55 -0700 | [diff] [blame] | 445 | bool corrupted = false; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 446 | if (!msg.ParseFromArray(buf, len)) |
| 447 | { |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 448 | std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl; |
Zhenkai Zhu | ef8d063 | 2012-10-05 10:24:55 -0700 | [diff] [blame] | 449 | // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs |
| 450 | msg.set_from("inconnu"); |
| 451 | msg.set_type(SyncDemo::ChatMessage::OTHER); |
| 452 | corrupted = true; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Zhenkai Zhu | ae29f5a | 2012-10-05 17:28:04 -0700 | [diff] [blame] | 455 | delete [] buf; |
| 456 | buf = NULL; |
| 457 | |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 458 | // display msg received from network |
| 459 | // we have to do so; this function is called by ccnd thread |
| 460 | // so if we call appendMsg directly |
| 461 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 462 | // the "cannonical" way to is use signal-slot |
Zhenkai Zhu | ef8d063 | 2012-10-05 10:24:55 -0700 | [diff] [blame] | 463 | if (show && !corrupted) |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 464 | { |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 465 | appendMessage(msg, isHistory); |
Zhenkai Zhu | bb19811 | 2012-09-27 11:31:42 -0700 | [diff] [blame] | 466 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 467 | |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 468 | if (!isHistory) |
| 469 | { |
| 470 | // update the tree view |
| 471 | std::string stdStrName = name.toStdString(); |
| 472 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 473 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 474 | #ifdef __DEBUG |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 475 | std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl; |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 476 | #endif |
Zhenkai Zhu | 24ec472 | 2012-10-07 17:56:38 -0700 | [diff] [blame] | 477 | if (msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 478 | { |
| 479 | processRemove(prefix.c_str()); |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 484 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 485 | } |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 486 | } |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 487 | fitView(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 491 | ChatDialog::processRemoveWrapper(std::string prefix) |
| 492 | { |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 493 | #ifdef __DEBUG |
| 494 | std::cout << "Sync REMOVE signal received for prefix: " << prefix << std::endl; |
| 495 | #endif |
| 496 | //emit removeReceived(prefix.c_str()); |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void |
| 500 | ChatDialog::processRemove(QString prefix) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 501 | { |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 502 | #ifdef __DEBUG |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 503 | std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl; |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 504 | #endif |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 505 | bool removed = m_scene->removeNode(prefix); |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 506 | if (removed) |
| 507 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 508 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 591e8c3 | 2012-09-26 11:57:50 -0700 | [diff] [blame] | 509 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 510 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 514 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 515 | msg.set_from(m_user.getNick().toStdString()); |
| 516 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 517 | msg.set_data(text.toStdString()); |
| 518 | time_t seconds = time(NULL); |
| 519 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 520 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 523 | void |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 524 | ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type) |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 525 | { |
| 526 | msg.set_from(m_user.getNick().toStdString()); |
| 527 | msg.set_to(m_user.getChatroom().toStdString()); |
| 528 | time_t seconds = time(NULL); |
| 529 | msg.set_timestamp(seconds); |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 530 | msg.set_type(type); |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Zhenkai Zhu | 59245aa | 2012-09-26 16:07:04 -0700 | [diff] [blame] | 533 | static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 534 | |
| 535 | QString |
| 536 | ChatDialog::getRandomString() |
| 537 | { |
| 538 | std::string randStr; |
| 539 | boost::random::random_device rng; |
| 540 | boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1); |
| 541 | for (int i = 0; i < 10; i ++) |
| 542 | { |
| 543 | randStr += chars[index_dist(rng)]; |
| 544 | } |
| 545 | return randStr.c_str(); |
| 546 | } |
| 547 | |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 548 | void |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 549 | ChatDialog::getLocalPrefix() |
| 550 | { |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 551 | // /* |
| 552 | // * this method tries to use ccncat |
| 553 | // * however, it does not work in Mac OS X app bundle |
| 554 | // * it works well in command line though |
| 555 | // */ |
Zhenkai Zhu | 756666a | 2012-10-07 00:24:35 -0700 | [diff] [blame] | 556 | |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 557 | // std::string cmd = CCN_EXEC; |
| 558 | // cmd += " -c -v "; |
| 559 | // cmd += LOCAL_PREFIX_QUERY; |
| 560 | // QString localPrefix; |
| 561 | // #define MAX_PREFIX_LEN 100 |
| 562 | // FILE *fp = popen(cmd.c_str(), "r"); |
| 563 | // if (fp != NULL) |
| 564 | // { |
| 565 | // char prefix[MAX_PREFIX_LEN]; |
| 566 | // if (fgets(prefix, MAX_PREFIX_LEN, fp) != NULL) |
| 567 | // { |
| 568 | // localPrefix = prefix; |
| 569 | // localPrefix.remove('\n'); |
| 570 | // } |
| 571 | // else |
| 572 | // { |
| 573 | // localPrefix = DEFAULT_LOCAL_PREFIX; |
| 574 | // } |
| 575 | // pclose(fp); |
| 576 | // } |
| 577 | // else |
| 578 | // { |
| 579 | // localPrefix = DEFAULT_LOCAL_PREFIX; |
| 580 | // } |
| 581 | // return localPrefix; |
| 582 | |
| 583 | if (m_sock != NULL) |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 584 | { |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 585 | QString originPrefix = QString::fromStdString (m_sock->getLocalPrefix()); |
| 586 | if (originPrefix != "" && m_user.getOriginPrefix () != originPrefix) |
| 587 | { |
| 588 | m_user.setOriginPrefix(originPrefix); |
| 589 | emit settingUpdated(m_user.getNick (), m_user.getChatroom (), m_user.getOriginPrefix ()); |
| 590 | // connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT()); |
| 591 | } |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 592 | } |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 593 | else |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 594 | { |
| 595 | // QTimer::singleShot(1000, this, SLOT(getLocalPrefix())); // try again |
| 596 | } |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 599 | bool |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 600 | ChatDialog::readSettings() |
| 601 | { |
| 602 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 603 | QString nick = s.value("nick", "").toString(); |
| 604 | QString chatroom = s.value("chatroom", "").toString(); |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 605 | QString originPrefix = s.value("originPrefix", "").toString(); |
| 606 | |
| 607 | // Sync::CcnxWrapperPtr wrapper = Sync::CcnxWrapper::Create (); |
| 608 | // QString originPrefix = QString::fromStdString (wrapper->getLocalPrefix()); |
| 609 | // Sync::CcnxWrapper::Destroy (); |
| 610 | // QString originPrefix = DEFAULT_LOCAL_PREFIX; |
| 611 | |
| 612 | QTimer::singleShot(500, this, SLOT(getLocalPrefix())); |
| 613 | |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 614 | m_minimaniho = s.value("minimaniho", false).toBool(); |
| 615 | if (nick == "" || chatroom == "" || originPrefix == "") { |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 616 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 617 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 618 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 619 | else { |
| 620 | m_user.setNick(nick); |
| 621 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 622 | m_user.setOriginPrefix(originPrefix); |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 623 | m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString()); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 624 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 625 | } |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 626 | |
| 627 | // QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 628 | // return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void |
| 632 | ChatDialog::writeSettings() |
| 633 | { |
| 634 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 635 | s.setValue("nick", m_user.getNick()); |
| 636 | s.setValue("chatroom", m_user.getChatroom()); |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 637 | //s.setValue("originPrefix", m_user.getOriginPrefix()); |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 638 | s.setValue("minimaniho", m_minimaniho); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void |
| 642 | ChatDialog::updateLabels() |
| 643 | { |
Zhenkai Zhu | 76ff02b | 2012-09-27 21:11:03 -0700 | [diff] [blame] | 644 | QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom()); |
| 645 | infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}"); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 646 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 647 | QString prefixDisp; |
| 648 | if (m_user.getPrefix().startsWith(DEFAULT_LOCAL_PREFIX)) |
| 649 | { |
| 650 | prefixDisp = QString("<Warning: Auto config prefix failed.>\n <Prefix = %1>").arg(m_user.getPrefix()); |
| 651 | prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}"); |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix()); |
| 656 | prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}"); |
| 657 | } |
| 658 | prefixLabel->setText(prefixDisp); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 661 | void |
| 662 | ChatDialog::returnPressed() |
| 663 | { |
| 664 | QString text = lineEdit->text(); |
| 665 | if (text.isEmpty()) |
| 666 | return; |
| 667 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 668 | lineEdit->clear(); |
| 669 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 670 | SyncDemo::ChatMessage msg; |
| 671 | formChatMessage(text, msg); |
| 672 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 673 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 674 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 675 | sendMsg(msg); |
| 676 | |
| 677 | fitView(); |
| 678 | } |
| 679 | |
| 680 | void |
| 681 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 682 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 683 | // send msg |
| 684 | size_t size = msg.ByteSize(); |
| 685 | char *buf = new char[size]; |
| 686 | msg.SerializeToArray(buf, size); |
| 687 | if (!msg.IsInitialized()) |
| 688 | { |
| 689 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 690 | abort(); |
| 691 | } |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 692 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 693 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 694 | delete buf; |
| 695 | |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 696 | m_lastMsgTime = time(NULL); |
| 697 | |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 698 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 699 | 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] | 700 | std::vector<Sync::MissingDataInfo> v; |
| 701 | v.push_back(mdi); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 702 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 703 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 704 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 705 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 706 | } |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 710 | ChatDialog::sendJoin() |
| 711 | { |
| 712 | SyncDemo::ChatMessage msg; |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 713 | formControlMessage(msg, SyncDemo::ChatMessage::JOIN); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 714 | sendMsg(msg); |
| 715 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 716 | } |
| 717 | |
| 718 | void |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 719 | ChatDialog::sendHello() |
| 720 | { |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 721 | time_t now = time(NULL); |
| 722 | int elapsed = now - m_lastMsgTime; |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 723 | if (elapsed >= m_randomizedInterval / 1000) |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 724 | { |
| 725 | SyncDemo::ChatMessage msg; |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 726 | formControlMessage(msg, SyncDemo::ChatMessage::HELLO); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 727 | sendMsg(msg); |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame] | 728 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 729 | } |
| 730 | else |
| 731 | { |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame] | 732 | QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello())); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 733 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 737 | ChatDialog::buttonPressed() |
| 738 | { |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame^] | 739 | Sync::SyncLogic &logic = m_sock->getLogic (); |
| 740 | logic.printState (); |
| 741 | |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 742 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 743 | 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] | 744 | dialog.exec(); |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 745 | QTimer::singleShot(100, this, SLOT(checkSetting())); |
| 746 | } |
| 747 | |
Zhenkai Zhu | b60b7e1 | 2012-09-28 11:34:21 -0700 | [diff] [blame] | 748 | void ChatDialog::treeButtonPressed() |
| 749 | { |
| 750 | if (treeViewer->isVisible()) |
| 751 | { |
| 752 | treeViewer->hide(); |
| 753 | treeButton->setText("Show Sync Tree"); |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | treeViewer->show(); |
| 758 | treeButton->setText("Hide Sync Tree"); |
| 759 | } |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 760 | |
| 761 | fitView(); |
Zhenkai Zhu | b60b7e1 | 2012-09-28 11:34:21 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Zhenkai Zhu | 0e7a9b2 | 2012-10-05 17:55:17 -0700 | [diff] [blame] | 764 | void ChatDialog::enableTreeDisplay() |
| 765 | { |
| 766 | treeButton->setEnabled(true); |
| 767 | treeViewer->show(); |
| 768 | fitView(); |
| 769 | } |
| 770 | |
| 771 | void ChatDialog::disableTreeDisplay() |
| 772 | { |
| 773 | treeButton->setEnabled(false); |
| 774 | treeViewer->hide(); |
| 775 | fitView(); |
| 776 | } |
| 777 | |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 778 | void |
| 779 | ChatDialog::checkSetting() |
| 780 | { |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 781 | 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] | 782 | { |
| 783 | buttonPressed(); |
| 784 | } |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 785 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 786 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 787 | void |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 788 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix) |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 789 | { |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 790 | QString randString = getRandomString(); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 791 | bool needWrite = false; |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 792 | bool needFresh = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 793 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 794 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 795 | needWrite = true; |
| 796 | } |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 797 | QString oldPrefix = m_user.getPrefix(); |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 798 | if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) { |
| 799 | m_user.setOriginPrefix(originPrefix); |
| 800 | m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 801 | needWrite = true; |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 802 | needFresh = true; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 803 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 804 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 805 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 806 | m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 807 | needWrite = true; |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 808 | needFresh = true; |
| 809 | } |
| 810 | |
| 811 | if (needFresh) |
| 812 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 813 | |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 814 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 815 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 816 | m_scene->clearAll(); |
| 817 | m_scene->plot("Empty"); |
| 818 | } |
Zhenkai Zhu | cc4c2c0 | 2012-09-27 21:24:37 -0700 | [diff] [blame] | 819 | |
| 820 | textEdit->clear(); |
| 821 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 822 | // 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] | 823 | if (m_sock != NULL) |
| 824 | { |
Zhenkai Zhu | cf02444 | 2012-10-05 10:33:08 -0700 | [diff] [blame] | 825 | sendLeave(); |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 826 | Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 827 | handle->clearInterestFilter(oldPrefix.toStdString()); |
| 828 | m_history.clear(); |
| 829 | m_historyInitialized = false; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 830 | delete m_sock; |
| 831 | m_sock = NULL; |
| 832 | } |
| 833 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 834 | syncPrefix += "/"; |
| 835 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 836 | try |
| 837 | { |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 838 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1)); |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 839 | Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 840 | handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1)); |
Alexander Afanasyev | 288edd8 | 2012-10-04 17:07:56 -0700 | [diff] [blame] | 841 | QTimer::singleShot(1000, this, SLOT(sendJoin())); |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 842 | m_timer->start(FRESHNESS * 2000); |
Zhenkai Zhu | 0e7a9b2 | 2012-10-05 17:55:17 -0700 | [diff] [blame] | 843 | disableTreeDisplay(); |
| 844 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 845 | } |
| 846 | catch (Sync::CcnxOperationException ex) |
| 847 | { |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 848 | 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] | 849 | std::exit(1); |
| 850 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 851 | |
| 852 | fitView(); |
| 853 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 854 | } |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 855 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 856 | if (needWrite) { |
| 857 | writeSettings(); |
| 858 | updateLabels(); |
| 859 | } |
| 860 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 861 | |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 862 | void |
| 863 | ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 864 | { |
| 865 | switch (reason) |
| 866 | { |
| 867 | case QSystemTrayIcon::Trigger: |
| 868 | case QSystemTrayIcon::DoubleClick: |
| 869 | break; |
| 870 | case QSystemTrayIcon::MiddleClick: |
| 871 | // showMessage(); |
| 872 | break; |
| 873 | default:; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | void |
| 878 | ChatDialog::showMessage(QString from, QString data) |
| 879 | { |
| 880 | // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl; |
| 881 | if (!isActiveWindow()) |
| 882 | { |
| 883 | trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000); |
| 884 | trayIcon->setIcon(QIcon(":/images/note.png")); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | ChatDialog::messageClicked() |
| 890 | { |
| 891 | this->showMaximized(); |
| 892 | } |
| 893 | |
| 894 | void |
| 895 | ChatDialog::createActions() |
| 896 | { |
| 897 | minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 898 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); |
| 899 | |
| 900 | maximizeAction = new QAction(tr("Ma&ximize"), this); |
| 901 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); |
| 902 | |
| 903 | restoreAction = new QAction(tr("&Restore"), this); |
| 904 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); |
| 905 | |
| 906 | quitAction = new QAction(tr("Quit"), this); |
| 907 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
| 908 | } |
| 909 | |
| 910 | void |
| 911 | ChatDialog::createTrayIcon() |
| 912 | { |
| 913 | trayIconMenu = new QMenu(this); |
| 914 | trayIconMenu->addAction(minimizeAction); |
| 915 | trayIconMenu->addAction(maximizeAction); |
| 916 | trayIconMenu->addAction(restoreAction); |
| 917 | trayIconMenu->addSeparator(); |
| 918 | trayIconMenu->addAction(quitAction); |
| 919 | |
| 920 | trayIcon = new QSystemTrayIcon(this); |
| 921 | trayIcon->setContextMenu(trayIconMenu); |
| 922 | |
| 923 | QIcon icon(":/images/icon_small.png"); |
| 924 | trayIcon->setIcon(icon); |
| 925 | setWindowIcon(icon); |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 926 | trayIcon->setToolTip("Chronos System Tray Icon"); |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 927 | trayIcon->setVisible(true); |
| 928 | } |
| 929 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 930 | void |
| 931 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 932 | { |
| 933 | fitView(); |
| 934 | } |
| 935 | |
| 936 | void |
| 937 | ChatDialog::showEvent(QShowEvent *e) |
| 938 | { |
| 939 | fitView(); |
| 940 | } |
| 941 | |
| 942 | void |
| 943 | ChatDialog::fitView() |
| 944 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 945 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 946 | QRectF rect = m_scene->itemsBoundingRect(); |
| 947 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 948 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 949 | } |