blob: 7c354ad2d79e338b4ce05973d0a886be289ee105 [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 Zhuae29f5a2012-10-05 17:28:04 -0700360 char *tempBuf = new char[len];
361 memcpy(tempBuf, buf, len);
362 emit dataReceived(name.c_str(), tempBuf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700363#ifdef __DEBUG
364 std::cout <<"<<< " << name << " fetched" << std::endl;
365#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700366}
367
368void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700369ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
370{
Zhenkai Zhuae29f5a2012-10-05 17:28:04 -0700371 char *tempBuf = new char[len];
372 memcpy(tempBuf, buf, len);
373 emit dataReceived(name.c_str(), tempBuf, len, false);
Zhenkai Zhubb198112012-09-27 11:31:42 -0700374}
375
376void
377ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700378{
379 SyncDemo::ChatMessage msg;
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700380 bool corrupted = false;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700381 if (!msg.ParseFromArray(buf, len))
382 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700383 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700384 // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
385 msg.set_from("inconnu");
386 msg.set_type(SyncDemo::ChatMessage::OTHER);
387 corrupted = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700388 }
389
Zhenkai Zhuae29f5a2012-10-05 17:28:04 -0700390 delete [] buf;
391 buf = NULL;
392
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700393 // display msg received from network
394 // we have to do so; this function is called by ccnd thread
395 // so if we call appendMsg directly
396 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
397 // the "cannonical" way to is use signal-slot
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700398 if (show && !corrupted)
Zhenkai Zhubb198112012-09-27 11:31:42 -0700399 {
400 appendMessage(msg);
401 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700402
403 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700404 std::string stdStrName = name.toStdString();
405 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
406 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
407#ifdef __DEBUG
408 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
409#endif
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700410 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
411 {
412 processRemove(prefix.c_str());
413 }
414 else
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700415 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700416 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700417 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
418 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700419 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700420}
421
422void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700423ChatDialog::processRemoveWrapper(std::string prefix)
424{
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700425#ifdef __DEBUG
426 std::cout << "Sync REMOVE signal received for prefix: " << prefix << std::endl;
427#endif
428 //emit removeReceived(prefix.c_str());
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700429}
430
431void
432ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700433{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700434#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700435 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700436#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700437 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700438 if (removed)
439 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700440 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700441 m_scene->plot(m_sock->getRootDigest().c_str());
442 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700443}
444
445void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700446ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700447 msg.set_from(m_user.getNick().toStdString());
448 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700449 msg.set_data(text.toStdString());
450 time_t seconds = time(NULL);
451 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700452 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700453}
454
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700455void
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700456ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700457{
458 msg.set_from(m_user.getNick().toStdString());
459 msg.set_to(m_user.getChatroom().toStdString());
460 time_t seconds = time(NULL);
461 msg.set_timestamp(seconds);
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700462 msg.set_type(type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700463}
464
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700465static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
466
467QString
468ChatDialog::getRandomString()
469{
470 std::string randStr;
471 boost::random::random_device rng;
472 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
473 for (int i = 0; i < 10; i ++)
474 {
475 randStr += chars[index_dist(rng)];
476 }
477 return randStr.c_str();
478}
479
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700480bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700481ChatDialog::readSettings()
482{
483 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700484 QString nick = s.value("nick", "").toString();
485 QString chatroom = s.value("chatroom", "").toString();
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700486 QString originPrefix = s.value("originPrefix", "").toString();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700487 m_minimaniho = s.value("minimaniho", false).toBool();
488 if (nick == "" || chatroom == "" || originPrefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700489 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700490 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700491 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700492 else {
493 m_user.setNick(nick);
494 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700495 m_user.setOriginPrefix(originPrefix);
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700496 m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700497 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700498 }
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700499
500// QTimer::singleShot(500, this, SLOT(buttonPressed()));
501 // return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700502}
503
504void
505ChatDialog::writeSettings()
506{
507 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700508 s.setValue("nick", m_user.getNick());
509 s.setValue("chatroom", m_user.getChatroom());
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700510 s.setValue("originPrefix", m_user.getOriginPrefix());
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700511 s.setValue("minimaniho", m_minimaniho);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700512}
513
514void
515ChatDialog::updateLabels()
516{
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700517 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
518 infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700519 infoLabel->setText(settingDisp);
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700520 //QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
521 //prefixLabel->setText(prefixDisp);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700522}
523
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700524void
525ChatDialog::returnPressed()
526{
527 QString text = lineEdit->text();
528 if (text.isEmpty())
529 return;
530
Zhenkai Zhub6338822012-05-31 13:27:24 -0700531 lineEdit->clear();
532
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700533 SyncDemo::ChatMessage msg;
534 formChatMessage(text, msg);
535
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700536 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700537
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700538 sendMsg(msg);
539
540 fitView();
541}
542
543void
544ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
545{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700546 // send msg
547 size_t size = msg.ByteSize();
548 char *buf = new char[size];
549 msg.SerializeToArray(buf, size);
550 if (!msg.IsInitialized())
551 {
552 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
553 abort();
554 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700555 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700556
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700557 delete buf;
558
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700559 m_lastMsgTime = time(NULL);
560
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700561 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700562 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700563 std::vector<Sync::MissingDataInfo> v;
564 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700565 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700566 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700567 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
568 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
569 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700570}
571
572void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700573ChatDialog::sendJoin()
574{
575 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700576 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700577 sendMsg(msg);
578 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
579}
580
581void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700582ChatDialog::sendHello()
583{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700584 time_t now = time(NULL);
585 int elapsed = now - m_lastMsgTime;
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700586 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700587 {
588 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700589 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700590 sendMsg(msg);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700591 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700592 }
593 else
594 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700595 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700596 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700597}
598
599void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700600ChatDialog::buttonPressed()
601{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700602 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700603 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700604 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700605 QTimer::singleShot(100, this, SLOT(checkSetting()));
606}
607
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700608void ChatDialog::treeButtonPressed()
609{
610 if (treeViewer->isVisible())
611 {
612 treeViewer->hide();
613 treeButton->setText("Show Sync Tree");
614 }
615 else
616 {
617 treeViewer->show();
618 treeButton->setText("Hide Sync Tree");
619 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700620
621 fitView();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700622}
623
Zhenkai Zhue837f792012-06-05 20:47:54 -0700624void
625ChatDialog::checkSetting()
626{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700627 if (m_user.getOriginPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
Zhenkai Zhue837f792012-06-05 20:47:54 -0700628 {
629 buttonPressed();
630 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700631}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700632
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700633void
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700634ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700635{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700636 QString randString = getRandomString();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700637 bool needWrite = false;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700638 bool needFresh = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700639 if (!nick.isEmpty() && nick != m_user.getNick()) {
640 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700641 needWrite = true;
642 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700643 if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) {
644 m_user.setOriginPrefix(originPrefix);
645 m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700646 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700647 needFresh = true;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700648 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700649 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
650 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700651 m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700652 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700653 needFresh = true;
654 }
655
656 if (needFresh)
657 {
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700658
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700659 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700660 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700661 m_scene->clearAll();
662 m_scene->plot("Empty");
663 }
Zhenkai Zhucc4c2c02012-09-27 21:24:37 -0700664
665 textEdit->clear();
666
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700667 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700668 if (m_sock != NULL)
669 {
Zhenkai Zhucf024442012-10-05 10:33:08 -0700670 sendLeave();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700671 delete m_sock;
672 m_sock = NULL;
673 }
674 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
675 syncPrefix += "/";
676 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700677 try
678 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700679 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Alexander Afanasyev288edd82012-10-04 17:07:56 -0700680 QTimer::singleShot(1000, this, SLOT(sendJoin()));
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700681 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700682 }
683 catch (Sync::CcnxOperationException ex)
684 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700685 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 -0700686 std::exit(1);
687 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700688
689 fitView();
690
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700691 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700692
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700693 if (needWrite) {
694 writeSettings();
695 updateLabels();
696 }
697}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700698
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700699void
700ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
701{
702 switch (reason)
703 {
704 case QSystemTrayIcon::Trigger:
705 case QSystemTrayIcon::DoubleClick:
706 break;
707 case QSystemTrayIcon::MiddleClick:
708 // showMessage();
709 break;
710 default:;
711 }
712}
713
714void
715ChatDialog::showMessage(QString from, QString data)
716{
717 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
718 if (!isActiveWindow())
719 {
720 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
721 trayIcon->setIcon(QIcon(":/images/note.png"));
722 }
723}
724
725void
726ChatDialog::messageClicked()
727{
728 this->showMaximized();
729}
730
731void
732ChatDialog::createActions()
733{
734 minimizeAction = new QAction(tr("Mi&nimize"), this);
735 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
736
737 maximizeAction = new QAction(tr("Ma&ximize"), this);
738 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
739
740 restoreAction = new QAction(tr("&Restore"), this);
741 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
742
743 quitAction = new QAction(tr("Quit"), this);
744 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
745}
746
747void
748ChatDialog::createTrayIcon()
749{
750 trayIconMenu = new QMenu(this);
751 trayIconMenu->addAction(minimizeAction);
752 trayIconMenu->addAction(maximizeAction);
753 trayIconMenu->addAction(restoreAction);
754 trayIconMenu->addSeparator();
755 trayIconMenu->addAction(quitAction);
756
757 trayIcon = new QSystemTrayIcon(this);
758 trayIcon->setContextMenu(trayIconMenu);
759
760 QIcon icon(":/images/icon_small.png");
761 trayIcon->setIcon(icon);
762 setWindowIcon(icon);
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700763 trayIcon->setToolTip("Chronos System Tray Icon");
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700764 trayIcon->setVisible(true);
765}
766
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700767void
768ChatDialog::resizeEvent(QResizeEvent *e)
769{
770 fitView();
771}
772
773void
774ChatDialog::showEvent(QShowEvent *e)
775{
776 fitView();
777}
778
779void
780ChatDialog::fitView()
781{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700782 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700783 QRectF rect = m_scene->itemsBoundingRect();
784 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700785 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700786}