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 |
Zhenkai Zhu | 8e180ae | 2012-10-10 14:01:05 -0700 | [diff] [blame^] | 77 | if(!m_user.getChatroom().isEmpty() && !m_user.getNick().isEmpty()) { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 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 | 8e180ae | 2012-10-10 14:01:05 -0700 | [diff] [blame^] | 702 | m_user.setChatroom("retreat2012"); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 703 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 704 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 705 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 706 | else { |
| 707 | m_user.setNick(nick); |
| 708 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 709 | m_user.setOriginPrefix(originPrefix); |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 710 | m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString()); |
Zhenkai Zhu | 716fe85 | 2012-10-08 18:27:55 -0700 | [diff] [blame] | 711 | m_scene->setCurrentPrefix(originPrefix + "/" + chatroom + "/" + getRandomString()); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 712 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 713 | } |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 714 | |
| 715 | // QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 716 | // return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void |
| 720 | ChatDialog::writeSettings() |
| 721 | { |
| 722 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 723 | s.setValue("nick", m_user.getNick()); |
| 724 | s.setValue("chatroom", m_user.getChatroom()); |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 725 | //s.setValue("originPrefix", m_user.getOriginPrefix()); |
Zhenkai Zhu | 3974a49 | 2012-09-28 14:39:45 -0700 | [diff] [blame] | 726 | s.setValue("minimaniho", m_minimaniho); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void |
| 730 | ChatDialog::updateLabels() |
| 731 | { |
Zhenkai Zhu | 76ff02b | 2012-09-27 21:11:03 -0700 | [diff] [blame] | 732 | QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom()); |
| 733 | infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}"); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 734 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 6bda0bd | 2012-10-06 23:50:39 -0700 | [diff] [blame] | 735 | QString prefixDisp; |
| 736 | if (m_user.getPrefix().startsWith(DEFAULT_LOCAL_PREFIX)) |
| 737 | { |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 738 | 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] | 739 | prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}"); |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix()); |
| 744 | prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}"); |
| 745 | } |
| 746 | prefixLabel->setText(prefixDisp); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 749 | void |
| 750 | ChatDialog::returnPressed() |
| 751 | { |
| 752 | QString text = lineEdit->text(); |
| 753 | if (text.isEmpty()) |
| 754 | return; |
| 755 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 756 | lineEdit->clear(); |
| 757 | |
Zhenkai Zhu | 22c7d4d | 2012-10-09 12:29:32 -0700 | [diff] [blame] | 758 | if (text.startsWith("boruoboluomi")) |
| 759 | { |
| 760 | reapButton->show(); |
| 761 | fitView(); |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | if (text.startsWith("minimanihong")) |
| 766 | { |
| 767 | reapButton->hide(); |
| 768 | fitView(); |
| 769 | return; |
| 770 | } |
| 771 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 772 | SyncDemo::ChatMessage msg; |
| 773 | formChatMessage(text, msg); |
| 774 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 775 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 776 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 777 | sendMsg(msg); |
| 778 | |
| 779 | fitView(); |
| 780 | } |
| 781 | |
| 782 | void |
| 783 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 784 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 785 | // send msg |
| 786 | size_t size = msg.ByteSize(); |
| 787 | char *buf = new char[size]; |
| 788 | msg.SerializeToArray(buf, size); |
| 789 | if (!msg.IsInitialized()) |
| 790 | { |
| 791 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 792 | abort(); |
| 793 | } |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 794 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 795 | |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 796 | delete buf; |
| 797 | |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 798 | m_lastMsgTime = time(NULL); |
| 799 | |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 800 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 801 | 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] | 802 | std::vector<Sync::MissingDataInfo> v; |
| 803 | v.push_back(mdi); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 804 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 805 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 806 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 807 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 808 | } |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | void |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 812 | ChatDialog::sendJoin() |
| 813 | { |
Zhenkai Zhu | 0fe2b8c | 2012-10-09 04:49:28 -0700 | [diff] [blame] | 814 | m_joined = true; |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 815 | SyncDemo::ChatMessage msg; |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 816 | formControlMessage(msg, SyncDemo::ChatMessage::JOIN); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 817 | sendMsg(msg); |
Zhenkai Zhu | c61dbc2 | 2012-10-08 11:36:37 -0700 | [diff] [blame] | 818 | boost::random::random_device rng; |
| 819 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 820 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 821 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 822 | } |
| 823 | |
| 824 | void |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 825 | ChatDialog::sendHello() |
| 826 | { |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 827 | time_t now = time(NULL); |
| 828 | int elapsed = now - m_lastMsgTime; |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 829 | if (elapsed >= m_randomizedInterval / 1000) |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 830 | { |
| 831 | SyncDemo::ChatMessage msg; |
Zhenkai Zhu | b5b7846 | 2012-09-28 14:10:37 -0700 | [diff] [blame] | 832 | formControlMessage(msg, SyncDemo::ChatMessage::HELLO); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 833 | sendMsg(msg); |
Zhenkai Zhu | c61dbc2 | 2012-10-08 11:36:37 -0700 | [diff] [blame] | 834 | boost::random::random_device rng; |
| 835 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 836 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame] | 837 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 838 | } |
| 839 | else |
| 840 | { |
Zhenkai Zhu | ee5c90f | 2012-09-27 14:05:41 -0700 | [diff] [blame] | 841 | QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello())); |
Zhenkai Zhu | 3e26bb4 | 2012-09-27 11:04:09 -0700 | [diff] [blame] | 842 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 846 | ChatDialog::buttonPressed() |
| 847 | { |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 848 | if (m_sock != NULL) |
| 849 | { |
| 850 | Sync::SyncLogic &logic = m_sock->getLogic (); |
| 851 | logic.printState (); |
| 852 | } |
Alexander Afanasyev | 5ddc68b | 2012-10-08 00:21:40 -0700 | [diff] [blame] | 853 | |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 854 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 855 | 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] | 856 | dialog.exec(); |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 857 | QTimer::singleShot(100, this, SLOT(checkSetting())); |
| 858 | } |
| 859 | |
Zhenkai Zhu | b60b7e1 | 2012-09-28 11:34:21 -0700 | [diff] [blame] | 860 | void ChatDialog::treeButtonPressed() |
| 861 | { |
| 862 | if (treeViewer->isVisible()) |
| 863 | { |
| 864 | treeViewer->hide(); |
| 865 | treeButton->setText("Show Sync Tree"); |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | treeViewer->show(); |
| 870 | treeButton->setText("Hide Sync Tree"); |
| 871 | } |
Zhenkai Zhu | 25e33e5 | 2012-09-28 13:00:07 -0700 | [diff] [blame] | 872 | |
| 873 | fitView(); |
Zhenkai Zhu | b60b7e1 | 2012-09-28 11:34:21 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Zhenkai Zhu | 0e7a9b2 | 2012-10-05 17:55:17 -0700 | [diff] [blame] | 876 | void ChatDialog::enableTreeDisplay() |
| 877 | { |
| 878 | treeButton->setEnabled(true); |
| 879 | treeViewer->show(); |
| 880 | fitView(); |
| 881 | } |
| 882 | |
| 883 | void ChatDialog::disableTreeDisplay() |
| 884 | { |
| 885 | treeButton->setEnabled(false); |
| 886 | treeViewer->hide(); |
| 887 | fitView(); |
| 888 | } |
| 889 | |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 890 | void |
| 891 | ChatDialog::checkSetting() |
| 892 | { |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 893 | 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] | 894 | { |
| 895 | buttonPressed(); |
| 896 | } |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 897 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 898 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 899 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 900 | void |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 901 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix) |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 902 | { |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 903 | QString randString = getRandomString(); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 904 | bool needWrite = false; |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 905 | bool needFresh = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 906 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 907 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 908 | needWrite = true; |
| 909 | } |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 910 | QString oldPrefix = m_user.getPrefix(); |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 911 | if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) { |
| 912 | m_user.setOriginPrefix(originPrefix); |
| 913 | m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString); |
Zhenkai Zhu | 716fe85 | 2012-10-08 18:27:55 -0700 | [diff] [blame] | 914 | m_scene->setCurrentPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 915 | needWrite = true; |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 916 | needFresh = true; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 917 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 918 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 919 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 920 | m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString); |
Zhenkai Zhu | 716fe85 | 2012-10-08 18:27:55 -0700 | [diff] [blame] | 921 | m_scene->setCurrentPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 922 | needWrite = true; |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 923 | needFresh = true; |
| 924 | } |
| 925 | |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 926 | if (needWrite) { |
| 927 | writeSettings(); |
| 928 | updateLabels(); |
| 929 | } |
| 930 | |
| 931 | if (needFresh && m_sock != NULL) |
Zhenkai Zhu | e95c64a | 2012-09-27 21:46:44 -0700 | [diff] [blame] | 932 | { |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 933 | |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 934 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 935 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 44b4375 | 2012-06-05 21:18:37 -0700 | [diff] [blame] | 936 | m_scene->clearAll(); |
| 937 | m_scene->plot("Empty"); |
| 938 | } |
Zhenkai Zhu | cc4c2c0 | 2012-09-27 21:24:37 -0700 | [diff] [blame] | 939 | |
| 940 | textEdit->clear(); |
| 941 | |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 942 | // keep the new prefix |
| 943 | QString newPrefix = m_user.getPrefix(); |
| 944 | // send leave for the old |
| 945 | m_user.setPrefix(oldPrefix); |
| 946 | // there is no point to send leave if we haven't joined yet |
| 947 | if (m_joined) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 948 | { |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 949 | sendLeave(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 950 | } |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 951 | // resume new prefix |
| 952 | m_user.setPrefix(newPrefix); |
| 953 | // Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 954 | // handle->clearInterestFilter(oldPrefix.toStdString()); |
| 955 | m_history.clear(); |
| 956 | m_historyInitialized = false; |
| 957 | delete m_sock; |
| 958 | m_sock = NULL; |
| 959 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 960 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 961 | syncPrefix += "/"; |
| 962 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 963 | try |
| 964 | { |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 965 | usleep(100000); |
Zhenkai Zhu | 2425b6a | 2012-09-26 17:11:44 -0700 | [diff] [blame] | 966 | 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] | 967 | usleep(100000); |
Zhenkai Zhu | 6c4fc11 | 2012-10-07 17:07:43 -0700 | [diff] [blame] | 968 | Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 969 | handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1)); |
Zhenkai Zhu | 0fe2b8c | 2012-10-09 04:49:28 -0700 | [diff] [blame] | 970 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
Zhenkai Zhu | 78799ea | 2012-10-08 11:51:56 -0700 | [diff] [blame] | 971 | m_timer->start(FRESHNESS * 1000); |
Zhenkai Zhu | 0e7a9b2 | 2012-10-05 17:55:17 -0700 | [diff] [blame] | 972 | disableTreeDisplay(); |
| 973 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 974 | } |
| 975 | catch (Sync::CcnxOperationException ex) |
| 976 | { |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 977 | 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] | 978 | std::exit(1); |
| 979 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 980 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 981 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 982 | } |
Zhenkai Zhu | 6bb65e9 | 2012-10-10 10:34:39 -0700 | [diff] [blame] | 983 | else if (needFresh && m_sock == NULL) |
| 984 | { |
| 985 | m_history.clear(); |
| 986 | m_historyInitialized = false; |
| 987 | initializeSync(); |
| 988 | } |
Zhenkai Zhu | 8e180ae | 2012-10-10 14:01:05 -0700 | [diff] [blame^] | 989 | else if (m_sock == NULL) |
| 990 | { |
| 991 | initializeSync(); |
| 992 | } |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 993 | else |
| 994 | { |
Zhenkai Zhu | 6bb65e9 | 2012-10-10 10:34:39 -0700 | [diff] [blame] | 995 | #ifdef __DEBUG |
| 996 | std::cout << "Just changing nicks, we're good. " << std::endl; |
| 997 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 998 | } |
Zhenkai Zhu | d616b58 | 2012-10-10 00:04:07 -0700 | [diff] [blame] | 999 | |
| 1000 | fitView(); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 1001 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 1002 | |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 1003 | void |
| 1004 | ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 1005 | { |
| 1006 | switch (reason) |
| 1007 | { |
| 1008 | case QSystemTrayIcon::Trigger: |
| 1009 | case QSystemTrayIcon::DoubleClick: |
| 1010 | break; |
| 1011 | case QSystemTrayIcon::MiddleClick: |
| 1012 | // showMessage(); |
| 1013 | break; |
| 1014 | default:; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | void |
| 1019 | ChatDialog::showMessage(QString from, QString data) |
| 1020 | { |
| 1021 | // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl; |
| 1022 | if (!isActiveWindow()) |
| 1023 | { |
| 1024 | trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000); |
| 1025 | trayIcon->setIcon(QIcon(":/images/note.png")); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | void |
| 1030 | ChatDialog::messageClicked() |
| 1031 | { |
| 1032 | this->showMaximized(); |
| 1033 | } |
| 1034 | |
| 1035 | void |
| 1036 | ChatDialog::createActions() |
| 1037 | { |
| 1038 | minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 1039 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); |
| 1040 | |
| 1041 | maximizeAction = new QAction(tr("Ma&ximize"), this); |
| 1042 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); |
| 1043 | |
| 1044 | restoreAction = new QAction(tr("&Restore"), this); |
| 1045 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); |
| 1046 | |
| 1047 | quitAction = new QAction(tr("Quit"), this); |
| 1048 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
| 1049 | } |
| 1050 | |
| 1051 | void |
| 1052 | ChatDialog::createTrayIcon() |
| 1053 | { |
| 1054 | trayIconMenu = new QMenu(this); |
| 1055 | trayIconMenu->addAction(minimizeAction); |
| 1056 | trayIconMenu->addAction(maximizeAction); |
| 1057 | trayIconMenu->addAction(restoreAction); |
| 1058 | trayIconMenu->addSeparator(); |
| 1059 | trayIconMenu->addAction(quitAction); |
| 1060 | |
| 1061 | trayIcon = new QSystemTrayIcon(this); |
| 1062 | trayIcon->setContextMenu(trayIconMenu); |
| 1063 | |
| 1064 | QIcon icon(":/images/icon_small.png"); |
| 1065 | trayIcon->setIcon(icon); |
| 1066 | setWindowIcon(icon); |
Zhenkai Zhu | 0b3fa33 | 2012-09-27 21:58:43 -0700 | [diff] [blame] | 1067 | trayIcon->setToolTip("Chronos System Tray Icon"); |
Zhenkai Zhu | 3a008fc | 2012-06-08 17:36:39 -0700 | [diff] [blame] | 1068 | trayIcon->setVisible(true); |
| 1069 | } |
| 1070 | |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 1071 | void |
| 1072 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 1073 | { |
| 1074 | fitView(); |
| 1075 | } |
| 1076 | |
| 1077 | void |
| 1078 | ChatDialog::showEvent(QShowEvent *e) |
| 1079 | { |
| 1080 | fitView(); |
| 1081 | } |
| 1082 | |
| 1083 | void |
| 1084 | ChatDialog::fitView() |
| 1085 | { |
Zhenkai Zhu | 9036e03 | 2012-09-27 20:59:33 -0700 | [diff] [blame] | 1086 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 1087 | QRectF rect = m_scene->itemsBoundingRect(); |
| 1088 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 1089 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 1090 | } |