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