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