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