blob: 336447ca07d71705ff7d628f210bfe3a4b1af238 [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 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070069 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070070 sendJoin();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070071 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070072 }
73 catch (Sync::CcnxOperationException ex)
74 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -070075 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 -070076 std::exit(1);
77 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -070078 }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070079
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070080}
81
Zhenkai Zhu82a62752012-06-04 17:11:04 -070082ChatDialog::~ChatDialog()
83{
84 if (m_sock != NULL)
85 {
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070086 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -070087 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070088 sendMsg(msg);
Zhenkai Zhu3974a492012-09-28 14:39:45 -070089 usleep(500000);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -070090 m_sock->remove(m_user.getPrefix().toStdString());
Zhenkai Zhu3974a492012-09-28 14:39:45 -070091 usleep(5000);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070092#ifdef __DEBUG
93 std::cout << "Sync REMOVE signal sent" << std::endl;
94#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -070095 delete m_sock;
96 m_sock = NULL;
97 }
98}
99
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700100void
101ChatDialog::replot()
102{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700103 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700104 m_scene->plot(m_sock->getRootDigest().c_str());
Zhenkai Zhuc9e4e3c2012-10-02 11:47:31 -0700105 fitView();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700106}
107
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700108void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700109ChatDialog::updateRosterList(QStringList staleUserList)
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700110{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700111 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700112 QStringList rosterList = m_scene->getRosterList();
113 m_rosterModel->setStringList(rosterList);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700114 QString user;
115 QStringListIterator it(staleUserList);
116 while(it.hasNext())
117 {
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700118 std::string nick = it.next().toStdString();
119 if (nick.empty())
120 continue;
121
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700122 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700123 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700124 msg.set_from(nick);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700125 appendMessage(msg);
126 }
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700127}
128
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700129void
130ChatDialog::setVisible(bool visible)
131{
132 minimizeAction->setEnabled(visible);
133 maximizeAction->setEnabled(!isMaximized());
134 restoreAction->setEnabled(isMaximized() || !visible);
135 QDialog::setVisible(visible);
136}
137
138void
139ChatDialog::closeEvent(QCloseEvent *e)
140{
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700141 if (trayIcon->isVisible() && !m_minimaniho)
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700142 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700143 QMessageBox::information(this, tr("Chronos"),
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700144 tr("The program will keep running in the "
145 "system tray. To terminate the program"
146 "choose <b>Quit</b> in the context memu"
147 "of the system tray entry."));
148 hide();
149 e->ignore();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700150 m_minimaniho = true;
151 writeSettings();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700152 }
153}
154
155void
156ChatDialog::changeEvent(QEvent *e)
157{
158 switch(e->type())
159 {
160 case QEvent::ActivationChange:
161 if (isActiveWindow())
162 {
163 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
164 }
165 break;
166 default:
167 break;
168 }
169}
170
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700171void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700172ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700173{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700174 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700175
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700176 if (msg.type() == SyncDemo::ChatMessage::CHAT)
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700177 {
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700178
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700179 if (!msg.has_data())
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700180 {
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700181 return;
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700182 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700183
184 if (msg.from().empty() || msg.data().empty())
185 {
186 return;
187 }
188
189 if (!msg.has_timestamp())
190 {
191 return;
192 }
193
194 QTextCharFormat nickFormat;
195 nickFormat.setForeground(Qt::darkGreen);
196 nickFormat.setFontWeight(QFont::Bold);
197 nickFormat.setFontUnderline(true);
198 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700199
200 QTextCursor cursor(textEdit->textCursor());
201 cursor.movePosition(QTextCursor::End);
202 QTextTableFormat tableFormat;
203 tableFormat.setBorder(0);
204 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
205 QString from = QString("%1 ").arg(msg.from().c_str());
206 QTextTableCell fromCell = table->cellAt(0, 0);
207 fromCell.setFormat(nickFormat);
208 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700209
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700210 time_t timestamp = msg.timestamp();
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700211 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700212
213 QTextCursor nextCursor(textEdit->textCursor());
214 nextCursor.movePosition(QTextCursor::End);
215 table = nextCursor.insertTable(1, 1, tableFormat);
216 table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str());
217 showMessage(from, msg.data().c_str());
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700218 }
219
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700220 if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
221 {
222 QTextCharFormat nickFormat;
223 nickFormat.setForeground(Qt::gray);
224 nickFormat.setFontWeight(QFont::Bold);
225 nickFormat.setFontUnderline(true);
226 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700227
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700228 QTextCursor cursor(textEdit->textCursor());
229 cursor.movePosition(QTextCursor::End);
230 QTextTableFormat tableFormat;
231 tableFormat.setBorder(0);
232 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
233 QString action;
234 if (msg.type() == SyncDemo::ChatMessage::JOIN)
235 {
236 action = "enters room";
237 }
238 else
239 {
240 action = "leaves room";
241 }
242
243 QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action);
244 QTextTableCell fromCell = table->cellAt(0, 0);
245 fromCell.setFormat(nickFormat);
246 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700247
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700248 time_t timestamp = msg.timestamp();
249 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700250 }
251
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700252 QScrollBar *bar = textEdit->verticalScrollBar();
253 bar->setValue(bar->maximum());
254}
255
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700256void
257ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
258{
259 QTextCharFormat timeFormat;
260 timeFormat.setForeground(Qt::gray);
261 timeFormat.setFontUnderline(true);
262 timeFormat.setUnderlineColor(Qt::gray);
263 QTextTableCell timeCell = table->cellAt(0, 1);
264 timeCell.setFormat(timeFormat);
265 timeCell.firstCursorPosition().insertText(formatTime(timestamp));
266}
267
268QString
269ChatDialog::formatTime(time_t timestamp)
270{
271 struct tm *tm_time = localtime(&timestamp);
272 int hour = tm_time->tm_hour;
273 QString amOrPM;
274 if (hour > 12)
275 {
276 hour -= 12;
277 amOrPM = "PM";
278 }
279 else
280 {
281 amOrPM = "AM";
282 if (hour == 0)
283 {
284 hour = 12;
285 }
286 }
287
288 char textTime[12];
289 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
290 return QString(textTime);
291}
292
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700293void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700294ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
295{
296 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700297#ifdef __DEBUG
298 std::cout << "<<< Tree update signal emitted" << std::endl;
299#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700300}
301
302void
303ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700304{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700305#ifdef __DEBUG
306 std::cout << "<<< processing Tree Update" << std::endl;
307#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700308 if (v.empty())
309 {
310 return;
311 }
312
313 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700314 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700315 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700316 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
317 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700318
319 int n = v.size();
320 int totalMissingPackets = 0;
321 for (int i = 0; i < n; i++)
322 {
323 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
324 }
325
Zhenkai Zhubb198112012-09-27 11:31:42 -0700326 for (int i = 0; i < n; i++)
327 {
328 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700329 {
330 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
331 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700332 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700333#ifdef __DEBUG
334 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
335#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700336 }
337 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700338 else
339 {
340 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
341 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700342 }
343
344 // adjust the view
345 fitView();
346
347}
348
349void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700350ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
351{
Zhenkai Zhubb198112012-09-27 11:31:42 -0700352 emit dataReceived(name.c_str(), buf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700353#ifdef __DEBUG
354 std::cout <<"<<< " << name << " fetched" << std::endl;
355#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700356}
357
358void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700359ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
360{
361 emit dataReceived(name.c_str(), buf, len, false);
362}
363
364void
365ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700366{
367 SyncDemo::ChatMessage msg;
368 if (!msg.ParseFromArray(buf, len))
369 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700370 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700371 }
372
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700373 // display msg received from network
374 // we have to do so; this function is called by ccnd thread
375 // so if we call appendMsg directly
376 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
377 // the "cannonical" way to is use signal-slot
Zhenkai Zhubb198112012-09-27 11:31:42 -0700378 if (show)
379 {
380 appendMessage(msg);
381 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700382
383 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700384 std::string stdStrName = name.toStdString();
385 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
386 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
387#ifdef __DEBUG
388 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
389#endif
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700390 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
391 {
392 processRemove(prefix.c_str());
393 }
394 else
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700395 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700396 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700397 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
398 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700399 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700400}
401
402void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700403ChatDialog::processRemoveWrapper(std::string prefix)
404{
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700405#ifdef __DEBUG
406 std::cout << "Sync REMOVE signal received for prefix: " << prefix << std::endl;
407#endif
408 //emit removeReceived(prefix.c_str());
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700409}
410
411void
412ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700413{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700414#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700415 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700416#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700417 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700418 if (removed)
419 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700420 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700421 m_scene->plot(m_sock->getRootDigest().c_str());
422 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700423}
424
425void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700426ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700427 msg.set_from(m_user.getNick().toStdString());
428 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700429 msg.set_data(text.toStdString());
430 time_t seconds = time(NULL);
431 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700432 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700433}
434
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700435void
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700436ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700437{
438 msg.set_from(m_user.getNick().toStdString());
439 msg.set_to(m_user.getChatroom().toStdString());
440 time_t seconds = time(NULL);
441 msg.set_timestamp(seconds);
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700442 msg.set_type(type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700443}
444
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700445static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
446
447QString
448ChatDialog::getRandomString()
449{
450 std::string randStr;
451 boost::random::random_device rng;
452 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
453 for (int i = 0; i < 10; i ++)
454 {
455 randStr += chars[index_dist(rng)];
456 }
457 return randStr.c_str();
458}
459
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700460bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700461ChatDialog::readSettings()
462{
463 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700464 QString nick = s.value("nick", "").toString();
465 QString chatroom = s.value("chatroom", "").toString();
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700466 QString originPrefix = s.value("originPrefix", "").toString();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700467 m_minimaniho = s.value("minimaniho", false).toBool();
468 if (nick == "" || chatroom == "" || originPrefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700469 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700470 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700471 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700472 else {
473 m_user.setNick(nick);
474 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700475 m_user.setOriginPrefix(originPrefix);
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700476 m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700477 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700478 }
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700479
480// QTimer::singleShot(500, this, SLOT(buttonPressed()));
481 // return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700482}
483
484void
485ChatDialog::writeSettings()
486{
487 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700488 s.setValue("nick", m_user.getNick());
489 s.setValue("chatroom", m_user.getChatroom());
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700490 s.setValue("originPrefix", m_user.getOriginPrefix());
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700491 s.setValue("minimaniho", m_minimaniho);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700492}
493
494void
495ChatDialog::updateLabels()
496{
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700497 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
498 infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700499 infoLabel->setText(settingDisp);
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700500 //QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
501 //prefixLabel->setText(prefixDisp);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700502}
503
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700504void
505ChatDialog::returnPressed()
506{
507 QString text = lineEdit->text();
508 if (text.isEmpty())
509 return;
510
Zhenkai Zhub6338822012-05-31 13:27:24 -0700511 lineEdit->clear();
512
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700513 SyncDemo::ChatMessage msg;
514 formChatMessage(text, msg);
515
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700516 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700517
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700518 sendMsg(msg);
519
520 fitView();
521}
522
523void
524ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
525{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700526 // send msg
527 size_t size = msg.ByteSize();
528 char *buf = new char[size];
529 msg.SerializeToArray(buf, size);
530 if (!msg.IsInitialized())
531 {
532 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
533 abort();
534 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700535 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700536
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700537 delete buf;
538
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700539 m_lastMsgTime = time(NULL);
540
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700541 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700542 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700543 std::vector<Sync::MissingDataInfo> v;
544 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700545 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700546 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700547 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
548 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
549 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700550}
551
552void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700553ChatDialog::sendJoin()
554{
555 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700556 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700557 sendMsg(msg);
558 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
559}
560
561void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700562ChatDialog::sendHello()
563{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700564 time_t now = time(NULL);
565 int elapsed = now - m_lastMsgTime;
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700566 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700567 {
568 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700569 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700570 sendMsg(msg);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700571 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700572 }
573 else
574 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700575 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700576 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700577}
578
579void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700580ChatDialog::buttonPressed()
581{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700582 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700583 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700584 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700585 QTimer::singleShot(100, this, SLOT(checkSetting()));
586}
587
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700588void ChatDialog::treeButtonPressed()
589{
590 if (treeViewer->isVisible())
591 {
592 treeViewer->hide();
593 treeButton->setText("Show Sync Tree");
594 }
595 else
596 {
597 treeViewer->show();
598 treeButton->setText("Hide Sync Tree");
599 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700600
601 fitView();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700602}
603
Zhenkai Zhue837f792012-06-05 20:47:54 -0700604void
605ChatDialog::checkSetting()
606{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700607 if (m_user.getOriginPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
Zhenkai Zhue837f792012-06-05 20:47:54 -0700608 {
609 buttonPressed();
610 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700611}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700612
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700613void
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700614ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700615{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700616 QString randString = getRandomString();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700617 bool needWrite = false;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700618 bool needFresh = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700619 if (!nick.isEmpty() && nick != m_user.getNick()) {
620 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700621 needWrite = true;
622 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700623 if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) {
624 m_user.setOriginPrefix(originPrefix);
625 m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700626 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700627 needFresh = true;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700628 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700629 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
630 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700631 m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700632 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700633 needFresh = true;
634 }
635
636 if (needFresh)
637 {
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700638
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700639 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700640 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700641 m_scene->clearAll();
642 m_scene->plot("Empty");
643 }
Zhenkai Zhucc4c2c02012-09-27 21:24:37 -0700644
645 textEdit->clear();
646
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700647 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700648 if (m_sock != NULL)
649 {
650 delete m_sock;
651 m_sock = NULL;
652 }
653 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
654 syncPrefix += "/";
655 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700656 try
657 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700658 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700659 sendJoin();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700660 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700661 }
662 catch (Sync::CcnxOperationException ex)
663 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700664 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 -0700665 std::exit(1);
666 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700667
668 fitView();
669
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700670 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700671
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700672 if (needWrite) {
673 writeSettings();
674 updateLabels();
675 }
676}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700677
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700678void
679ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
680{
681 switch (reason)
682 {
683 case QSystemTrayIcon::Trigger:
684 case QSystemTrayIcon::DoubleClick:
685 break;
686 case QSystemTrayIcon::MiddleClick:
687 // showMessage();
688 break;
689 default:;
690 }
691}
692
693void
694ChatDialog::showMessage(QString from, QString data)
695{
696 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
697 if (!isActiveWindow())
698 {
699 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
700 trayIcon->setIcon(QIcon(":/images/note.png"));
701 }
702}
703
704void
705ChatDialog::messageClicked()
706{
707 this->showMaximized();
708}
709
710void
711ChatDialog::createActions()
712{
713 minimizeAction = new QAction(tr("Mi&nimize"), this);
714 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
715
716 maximizeAction = new QAction(tr("Ma&ximize"), this);
717 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
718
719 restoreAction = new QAction(tr("&Restore"), this);
720 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
721
722 quitAction = new QAction(tr("Quit"), this);
723 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
724}
725
726void
727ChatDialog::createTrayIcon()
728{
729 trayIconMenu = new QMenu(this);
730 trayIconMenu->addAction(minimizeAction);
731 trayIconMenu->addAction(maximizeAction);
732 trayIconMenu->addAction(restoreAction);
733 trayIconMenu->addSeparator();
734 trayIconMenu->addAction(quitAction);
735
736 trayIcon = new QSystemTrayIcon(this);
737 trayIcon->setContextMenu(trayIconMenu);
738
739 QIcon icon(":/images/icon_small.png");
740 trayIcon->setIcon(icon);
741 setWindowIcon(icon);
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700742 trayIcon->setToolTip("Chronos System Tray Icon");
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700743 trayIcon->setVisible(true);
744}
745
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700746void
747ChatDialog::resizeEvent(QResizeEvent *e)
748{
749 fitView();
750}
751
752void
753ChatDialog::showEvent(QShowEvent *e)
754{
755 fitView();
756}
757
758void
759ChatDialog::fitView()
760{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700761 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700762 QRectF rect = m_scene->itemsBoundingRect();
763 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700764 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700765}