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