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