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