blob: 70f67f55fa51fac23d47bb05343e3325cbed791c [file] [log] [blame]
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhu85845d22012-06-01 23:10:43 -07003#include "settingdialog.h"
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -07004#include <ctime>
Zhenkai Zhub6338822012-05-31 13:27:24 -07005#include <iostream>
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -07006#include <QTimer>
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -07007#include <QMetaType>
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -07008#include <QMessageBox>
Zhenkai Zhu59245aa2012-09-26 16:07:04 -07009#include <boost/random/random_device.hpp>
10#include <boost/random/uniform_int_distribution.hpp>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070011
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -070012#define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/chronos"
Zhenkai Zhu82a62752012-06-04 17:11:04 -070013
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -070014static const int HELLO_INTERVAL = 90; // seconds
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070015
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070016ChatDialog::ChatDialog(QWidget *parent)
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070017 : QDialog(parent), m_sock(NULL), m_lastMsgTime(0)
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070018{
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070019 // have to register this, otherwise
20 // the signal-slot system won't recognize this type
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070021 qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
22 qRegisterMetaType<size_t>("size_t");
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070023 setupUi(this);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070024 m_session = time(NULL);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -070025 boost::random::random_device rng;
26 boost::random::uniform_int_distribution<> uniform(1, 29000);
27 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070028
29 readSettings();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070030
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070031 updateLabels();
32
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070033 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070034 m_scene = new DigestTreeScene(this);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070035
Zhenkai Zhu82a62752012-06-04 17:11:04 -070036 treeViewer->setScene(m_scene);
37 m_scene->plot("Empty");
38 QRectF rect = m_scene->itemsBoundingRect();
39 m_scene->setSceneRect(rect);
40
Zhenkai Zhu9036e032012-09-27 20:59:33 -070041 listView->setStyleSheet("QListView { alternate-background-color: white; background: #F0F0F0; color: darkGreen; font: bold large; }");
Zhenkai Zhuf55f4382012-09-28 10:58:54 -070042 listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
43 listView->setDragDropMode(QAbstractItemView::NoDragDrop);
44 listView->setSelectionMode(QAbstractItemView::NoSelection);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070045 m_rosterModel = new QStringListModel(this);
46 listView->setModel(m_rosterModel);
47
Zhenkai Zhu86df7412012-09-27 16:30:20 -070048 createActions();
49 createTrayIcon();
50 m_timer = new QTimer(this);
51 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
52 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhub60b7e12012-09-28 11:34:21 -070053 connect(treeButton, SIGNAL(pressed()), this, SLOT(treeButtonPressed()));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070054 connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool)), this, SLOT(processData(QString, const char *, size_t, bool)));
55 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070056 connect(m_timer, SIGNAL(timeout()), this, SLOT(replot()));
57 connect(m_scene, SIGNAL(replot()), this, SLOT(replot()));
58 connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal()));
59 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070060 connect(m_scene, SIGNAL(rosterChanged(QStringList)), this, SLOT(updateRosterList(QStringList)));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070061
Zhenkai Zhu82a62752012-06-04 17:11:04 -070062 // create sync socket
63 if(!m_user.getChatroom().isEmpty()) {
64 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
65 syncPrefix += "/";
66 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070067 try
68 {
Alexander Afanasyev288edd82012-10-04 17:07:56 -070069 m_sock = new Sync::SyncAppSocket(syncPrefix,
70 bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
71 bind(&ChatDialog::processRemoveWrapper, this, _1));
Zhenkai Zhucf024442012-10-05 10:33:08 -070072 QTimer::singleShot(600, this, SLOT(sendJoin()));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070073 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070074 }
75 catch (Sync::CcnxOperationException ex)
76 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -070077 QMessageBox::critical(this, tr("Chronos"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070078 std::exit(1);
79 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -070080 }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070081
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070082}
83
Zhenkai Zhu82a62752012-06-04 17:11:04 -070084ChatDialog::~ChatDialog()
85{
86 if (m_sock != NULL)
87 {
Zhenkai Zhucf024442012-10-05 10:33:08 -070088 sendLeave();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070089 delete m_sock;
90 m_sock = NULL;
91 }
92}
93
Zhenkai Zhu86df7412012-09-27 16:30:20 -070094void
Zhenkai Zhucf024442012-10-05 10:33:08 -070095ChatDialog::sendLeave()
96{
97 SyncDemo::ChatMessage msg;
98 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
99 sendMsg(msg);
100 usleep(500000);
101 m_sock->remove(m_user.getPrefix().toStdString());
102 usleep(5000);
103#ifdef __DEBUG
104 std::cout << "Sync REMOVE signal sent" << std::endl;
105#endif
106}
107
108void
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700109ChatDialog::replot()
110{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700111 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700112 m_scene->plot(m_sock->getRootDigest().c_str());
Zhenkai Zhuc9e4e3c2012-10-02 11:47:31 -0700113 fitView();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700114}
115
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700116void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700117ChatDialog::updateRosterList(QStringList staleUserList)
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700118{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700119 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700120 QStringList rosterList = m_scene->getRosterList();
121 m_rosterModel->setStringList(rosterList);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700122 QString user;
123 QStringListIterator it(staleUserList);
124 while(it.hasNext())
125 {
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700126 std::string nick = it.next().toStdString();
127 if (nick.empty())
128 continue;
129
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700130 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700131 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700132 msg.set_from(nick);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700133 appendMessage(msg);
134 }
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700135}
136
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700137void
138ChatDialog::setVisible(bool visible)
139{
140 minimizeAction->setEnabled(visible);
141 maximizeAction->setEnabled(!isMaximized());
142 restoreAction->setEnabled(isMaximized() || !visible);
143 QDialog::setVisible(visible);
144}
145
146void
147ChatDialog::closeEvent(QCloseEvent *e)
148{
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700149 if (trayIcon->isVisible() && !m_minimaniho)
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700150 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700151 QMessageBox::information(this, tr("Chronos"),
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700152 tr("The program will keep running in the "
153 "system tray. To terminate the program"
154 "choose <b>Quit</b> in the context memu"
155 "of the system tray entry."));
156 hide();
157 e->ignore();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700158 m_minimaniho = true;
159 writeSettings();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700160 }
161}
162
163void
164ChatDialog::changeEvent(QEvent *e)
165{
166 switch(e->type())
167 {
168 case QEvent::ActivationChange:
169 if (isActiveWindow())
170 {
171 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
172 }
173 break;
174 default:
175 break;
176 }
177}
178
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700179void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700180ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700181{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700182 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700183
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700184 if (msg.type() == SyncDemo::ChatMessage::CHAT)
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700185 {
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700186
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700187 if (!msg.has_data())
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700188 {
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700189 return;
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700190 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700191
192 if (msg.from().empty() || msg.data().empty())
193 {
194 return;
195 }
196
197 if (!msg.has_timestamp())
198 {
199 return;
200 }
201
202 QTextCharFormat nickFormat;
203 nickFormat.setForeground(Qt::darkGreen);
204 nickFormat.setFontWeight(QFont::Bold);
205 nickFormat.setFontUnderline(true);
206 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700207
208 QTextCursor cursor(textEdit->textCursor());
209 cursor.movePosition(QTextCursor::End);
210 QTextTableFormat tableFormat;
211 tableFormat.setBorder(0);
212 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
213 QString from = QString("%1 ").arg(msg.from().c_str());
214 QTextTableCell fromCell = table->cellAt(0, 0);
215 fromCell.setFormat(nickFormat);
216 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700217
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700218 time_t timestamp = msg.timestamp();
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700219 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700220
221 QTextCursor nextCursor(textEdit->textCursor());
222 nextCursor.movePosition(QTextCursor::End);
223 table = nextCursor.insertTable(1, 1, tableFormat);
224 table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str());
225 showMessage(from, msg.data().c_str());
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700226 }
227
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700228 if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
229 {
230 QTextCharFormat nickFormat;
231 nickFormat.setForeground(Qt::gray);
232 nickFormat.setFontWeight(QFont::Bold);
233 nickFormat.setFontUnderline(true);
234 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700235
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700236 QTextCursor cursor(textEdit->textCursor());
237 cursor.movePosition(QTextCursor::End);
238 QTextTableFormat tableFormat;
239 tableFormat.setBorder(0);
240 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
241 QString action;
242 if (msg.type() == SyncDemo::ChatMessage::JOIN)
243 {
244 action = "enters room";
245 }
246 else
247 {
248 action = "leaves room";
249 }
250
251 QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action);
252 QTextTableCell fromCell = table->cellAt(0, 0);
253 fromCell.setFormat(nickFormat);
254 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700255
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700256 time_t timestamp = msg.timestamp();
257 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700258 }
259
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700260 QScrollBar *bar = textEdit->verticalScrollBar();
261 bar->setValue(bar->maximum());
262}
263
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700264void
265ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
266{
267 QTextCharFormat timeFormat;
268 timeFormat.setForeground(Qt::gray);
269 timeFormat.setFontUnderline(true);
270 timeFormat.setUnderlineColor(Qt::gray);
271 QTextTableCell timeCell = table->cellAt(0, 1);
272 timeCell.setFormat(timeFormat);
273 timeCell.firstCursorPosition().insertText(formatTime(timestamp));
274}
275
276QString
277ChatDialog::formatTime(time_t timestamp)
278{
279 struct tm *tm_time = localtime(&timestamp);
280 int hour = tm_time->tm_hour;
281 QString amOrPM;
282 if (hour > 12)
283 {
284 hour -= 12;
285 amOrPM = "PM";
286 }
287 else
288 {
289 amOrPM = "AM";
290 if (hour == 0)
291 {
292 hour = 12;
293 }
294 }
295
296 char textTime[12];
297 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
298 return QString(textTime);
299}
300
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700301void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700302ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
303{
304 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700305#ifdef __DEBUG
306 std::cout << "<<< Tree update signal emitted" << std::endl;
307#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700308}
309
310void
311ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700312{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700313#ifdef __DEBUG
314 std::cout << "<<< processing Tree Update" << std::endl;
315#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700316 if (v.empty())
317 {
318 return;
319 }
320
321 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700322 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700323 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700324 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
325 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700326
327 int n = v.size();
328 int totalMissingPackets = 0;
329 for (int i = 0; i < n; i++)
330 {
331 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
332 }
333
Zhenkai Zhubb198112012-09-27 11:31:42 -0700334 for (int i = 0; i < n; i++)
335 {
336 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700337 {
338 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
339 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700340 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700341#ifdef __DEBUG
342 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
343#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700344 }
345 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700346 else
347 {
348 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
349 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700350 }
351
352 // adjust the view
353 fitView();
354
355}
356
357void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700358ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
359{
Zhenkai Zhubb198112012-09-27 11:31:42 -0700360 emit dataReceived(name.c_str(), buf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700361#ifdef __DEBUG
362 std::cout <<"<<< " << name << " fetched" << std::endl;
363#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700364}
365
366void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700367ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
368{
369 emit dataReceived(name.c_str(), buf, len, false);
370}
371
372void
373ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700374{
375 SyncDemo::ChatMessage msg;
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700376 bool corrupted = false;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700377 if (!msg.ParseFromArray(buf, len))
378 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700379 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700380 // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
381 msg.set_from("inconnu");
382 msg.set_type(SyncDemo::ChatMessage::OTHER);
383 corrupted = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700384 }
385
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700386 // display msg received from network
387 // we have to do so; this function is called by ccnd thread
388 // so if we call appendMsg directly
389 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
390 // the "cannonical" way to is use signal-slot
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700391 if (show && !corrupted)
Zhenkai Zhubb198112012-09-27 11:31:42 -0700392 {
393 appendMessage(msg);
394 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700395
396 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700397 std::string stdStrName = name.toStdString();
398 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
399 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
400#ifdef __DEBUG
401 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
402#endif
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700403 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
404 {
405 processRemove(prefix.c_str());
406 }
407 else
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700408 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700409 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700410 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
411 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700412 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700413}
414
415void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700416ChatDialog::processRemoveWrapper(std::string prefix)
417{
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700418#ifdef __DEBUG
419 std::cout << "Sync REMOVE signal received for prefix: " << prefix << std::endl;
420#endif
421 //emit removeReceived(prefix.c_str());
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700422}
423
424void
425ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700426{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700427#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700428 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700429#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700430 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700431 if (removed)
432 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700433 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700434 m_scene->plot(m_sock->getRootDigest().c_str());
435 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700436}
437
438void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700439ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700440 msg.set_from(m_user.getNick().toStdString());
441 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700442 msg.set_data(text.toStdString());
443 time_t seconds = time(NULL);
444 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700445 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700446}
447
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700448void
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700449ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700450{
451 msg.set_from(m_user.getNick().toStdString());
452 msg.set_to(m_user.getChatroom().toStdString());
453 time_t seconds = time(NULL);
454 msg.set_timestamp(seconds);
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700455 msg.set_type(type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700456}
457
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700458static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
459
460QString
461ChatDialog::getRandomString()
462{
463 std::string randStr;
464 boost::random::random_device rng;
465 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
466 for (int i = 0; i < 10; i ++)
467 {
468 randStr += chars[index_dist(rng)];
469 }
470 return randStr.c_str();
471}
472
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700473bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700474ChatDialog::readSettings()
475{
476 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700477 QString nick = s.value("nick", "").toString();
478 QString chatroom = s.value("chatroom", "").toString();
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700479 QString originPrefix = s.value("originPrefix", "").toString();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700480 m_minimaniho = s.value("minimaniho", false).toBool();
481 if (nick == "" || chatroom == "" || originPrefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700482 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700483 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700484 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700485 else {
486 m_user.setNick(nick);
487 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700488 m_user.setOriginPrefix(originPrefix);
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700489 m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700490 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700491 }
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700492
493// QTimer::singleShot(500, this, SLOT(buttonPressed()));
494 // return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700495}
496
497void
498ChatDialog::writeSettings()
499{
500 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700501 s.setValue("nick", m_user.getNick());
502 s.setValue("chatroom", m_user.getChatroom());
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700503 s.setValue("originPrefix", m_user.getOriginPrefix());
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700504 s.setValue("minimaniho", m_minimaniho);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700505}
506
507void
508ChatDialog::updateLabels()
509{
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700510 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
511 infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700512 infoLabel->setText(settingDisp);
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700513 //QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
514 //prefixLabel->setText(prefixDisp);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700515}
516
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700517void
518ChatDialog::returnPressed()
519{
520 QString text = lineEdit->text();
521 if (text.isEmpty())
522 return;
523
Zhenkai Zhub6338822012-05-31 13:27:24 -0700524 lineEdit->clear();
525
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700526 SyncDemo::ChatMessage msg;
527 formChatMessage(text, msg);
528
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700529 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700530
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700531 sendMsg(msg);
532
533 fitView();
534}
535
536void
537ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
538{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700539 // send msg
540 size_t size = msg.ByteSize();
541 char *buf = new char[size];
542 msg.SerializeToArray(buf, size);
543 if (!msg.IsInitialized())
544 {
545 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
546 abort();
547 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700548 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700549
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700550 delete buf;
551
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700552 m_lastMsgTime = time(NULL);
553
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700554 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700555 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700556 std::vector<Sync::MissingDataInfo> v;
557 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700558 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700559 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700560 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
561 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
562 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700563}
564
565void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700566ChatDialog::sendJoin()
567{
568 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700569 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700570 sendMsg(msg);
571 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
572}
573
574void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700575ChatDialog::sendHello()
576{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700577 time_t now = time(NULL);
578 int elapsed = now - m_lastMsgTime;
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700579 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700580 {
581 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700582 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700583 sendMsg(msg);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700584 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700585 }
586 else
587 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700588 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700589 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700590}
591
592void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700593ChatDialog::buttonPressed()
594{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700595 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700596 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700597 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700598 QTimer::singleShot(100, this, SLOT(checkSetting()));
599}
600
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700601void ChatDialog::treeButtonPressed()
602{
603 if (treeViewer->isVisible())
604 {
605 treeViewer->hide();
606 treeButton->setText("Show Sync Tree");
607 }
608 else
609 {
610 treeViewer->show();
611 treeButton->setText("Hide Sync Tree");
612 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700613
614 fitView();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700615}
616
Zhenkai Zhue837f792012-06-05 20:47:54 -0700617void
618ChatDialog::checkSetting()
619{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700620 if (m_user.getOriginPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
Zhenkai Zhue837f792012-06-05 20:47:54 -0700621 {
622 buttonPressed();
623 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700624}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700625
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700626void
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700627ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700628{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700629 QString randString = getRandomString();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700630 bool needWrite = false;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700631 bool needFresh = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700632 if (!nick.isEmpty() && nick != m_user.getNick()) {
633 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700634 needWrite = true;
635 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700636 if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) {
637 m_user.setOriginPrefix(originPrefix);
638 m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700639 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700640 needFresh = true;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700641 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700642 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
643 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700644 m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700645 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700646 needFresh = true;
647 }
648
649 if (needFresh)
650 {
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700651
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700652 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700653 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700654 m_scene->clearAll();
655 m_scene->plot("Empty");
656 }
Zhenkai Zhucc4c2c02012-09-27 21:24:37 -0700657
658 textEdit->clear();
659
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700660 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700661 if (m_sock != NULL)
662 {
Zhenkai Zhucf024442012-10-05 10:33:08 -0700663 sendLeave();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700664 delete m_sock;
665 m_sock = NULL;
666 }
667 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
668 syncPrefix += "/";
669 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700670 try
671 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700672 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Alexander Afanasyev288edd82012-10-04 17:07:56 -0700673 QTimer::singleShot(1000, this, SLOT(sendJoin()));
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700674 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700675 }
676 catch (Sync::CcnxOperationException ex)
677 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700678 QMessageBox::critical(this, tr("Chronos"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700679 std::exit(1);
680 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700681
682 fitView();
683
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700684 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700685
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700686 if (needWrite) {
687 writeSettings();
688 updateLabels();
689 }
690}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700691
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700692void
693ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
694{
695 switch (reason)
696 {
697 case QSystemTrayIcon::Trigger:
698 case QSystemTrayIcon::DoubleClick:
699 break;
700 case QSystemTrayIcon::MiddleClick:
701 // showMessage();
702 break;
703 default:;
704 }
705}
706
707void
708ChatDialog::showMessage(QString from, QString data)
709{
710 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
711 if (!isActiveWindow())
712 {
713 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
714 trayIcon->setIcon(QIcon(":/images/note.png"));
715 }
716}
717
718void
719ChatDialog::messageClicked()
720{
721 this->showMaximized();
722}
723
724void
725ChatDialog::createActions()
726{
727 minimizeAction = new QAction(tr("Mi&nimize"), this);
728 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
729
730 maximizeAction = new QAction(tr("Ma&ximize"), this);
731 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
732
733 restoreAction = new QAction(tr("&Restore"), this);
734 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
735
736 quitAction = new QAction(tr("Quit"), this);
737 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
738}
739
740void
741ChatDialog::createTrayIcon()
742{
743 trayIconMenu = new QMenu(this);
744 trayIconMenu->addAction(minimizeAction);
745 trayIconMenu->addAction(maximizeAction);
746 trayIconMenu->addAction(restoreAction);
747 trayIconMenu->addSeparator();
748 trayIconMenu->addAction(quitAction);
749
750 trayIcon = new QSystemTrayIcon(this);
751 trayIcon->setContextMenu(trayIconMenu);
752
753 QIcon icon(":/images/icon_small.png");
754 trayIcon->setIcon(icon);
755 setWindowIcon(icon);
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700756 trayIcon->setToolTip("Chronos System Tray Icon");
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700757 trayIcon->setVisible(true);
758}
759
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700760void
761ChatDialog::resizeEvent(QResizeEvent *e)
762{
763 fitView();
764}
765
766void
767ChatDialog::showEvent(QShowEvent *e)
768{
769 fitView();
770}
771
772void
773ChatDialog::fitView()
774{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700775 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700776 QRectF rect = m_scene->itemsBoundingRect();
777 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700778 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700779}