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