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