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