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