blob: 558f40a30f9e1b9e42ca9671a3f44e944c8cd492 [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());
105}
106
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700107void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700108ChatDialog::updateRosterList(QStringList staleUserList)
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700109{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700110 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700111 QStringList rosterList = m_scene->getRosterList();
112 m_rosterModel->setStringList(rosterList);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700113 QString user;
114 QStringListIterator it(staleUserList);
115 while(it.hasNext())
116 {
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700117 std::string nick = it.next().toStdString();
118 if (nick.empty())
119 continue;
120
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700121 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700122 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700123 msg.set_from(nick);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700124 appendMessage(msg);
125 }
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700126}
127
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700128void
129ChatDialog::setVisible(bool visible)
130{
131 minimizeAction->setEnabled(visible);
132 maximizeAction->setEnabled(!isMaximized());
133 restoreAction->setEnabled(isMaximized() || !visible);
134 QDialog::setVisible(visible);
135}
136
137void
138ChatDialog::closeEvent(QCloseEvent *e)
139{
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700140 if (trayIcon->isVisible() && !m_minimaniho)
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700141 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700142 QMessageBox::information(this, tr("Chronos"),
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700143 tr("The program will keep running in the "
144 "system tray. To terminate the program"
145 "choose <b>Quit</b> in the context memu"
146 "of the system tray entry."));
147 hide();
148 e->ignore();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700149 m_minimaniho = true;
150 writeSettings();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700151 }
152}
153
154void
155ChatDialog::changeEvent(QEvent *e)
156{
157 switch(e->type())
158 {
159 case QEvent::ActivationChange:
160 if (isActiveWindow())
161 {
162 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
163 }
164 break;
165 default:
166 break;
167 }
168}
169
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700170void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700171ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700172{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700173 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700174
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700175 if (msg.type() == SyncDemo::ChatMessage::CHAT)
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700176 {
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700177
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700178 if (!msg.has_data())
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700179 {
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700180 return;
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700181 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700182
183 if (msg.from().empty() || msg.data().empty())
184 {
185 return;
186 }
187
188 if (!msg.has_timestamp())
189 {
190 return;
191 }
192
193 QTextCharFormat nickFormat;
194 nickFormat.setForeground(Qt::darkGreen);
195 nickFormat.setFontWeight(QFont::Bold);
196 nickFormat.setFontUnderline(true);
197 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700198
199 QTextCursor cursor(textEdit->textCursor());
200 cursor.movePosition(QTextCursor::End);
201 QTextTableFormat tableFormat;
202 tableFormat.setBorder(0);
203 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
204 QString from = QString("%1 ").arg(msg.from().c_str());
205 QTextTableCell fromCell = table->cellAt(0, 0);
206 fromCell.setFormat(nickFormat);
207 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700208
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700209 time_t timestamp = msg.timestamp();
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700210 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700211
212 QTextCursor nextCursor(textEdit->textCursor());
213 nextCursor.movePosition(QTextCursor::End);
214 table = nextCursor.insertTable(1, 1, tableFormat);
215 table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str());
216 showMessage(from, msg.data().c_str());
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700217 }
218
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700219 if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
220 {
221 QTextCharFormat nickFormat;
222 nickFormat.setForeground(Qt::gray);
223 nickFormat.setFontWeight(QFont::Bold);
224 nickFormat.setFontUnderline(true);
225 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700226
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700227 QTextCursor cursor(textEdit->textCursor());
228 cursor.movePosition(QTextCursor::End);
229 QTextTableFormat tableFormat;
230 tableFormat.setBorder(0);
231 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
232 QString action;
233 if (msg.type() == SyncDemo::ChatMessage::JOIN)
234 {
235 action = "enters room";
236 }
237 else
238 {
239 action = "leaves room";
240 }
241
242 QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action);
243 QTextTableCell fromCell = table->cellAt(0, 0);
244 fromCell.setFormat(nickFormat);
245 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700246
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700247 time_t timestamp = msg.timestamp();
248 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700249 }
250
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700251 QScrollBar *bar = textEdit->verticalScrollBar();
252 bar->setValue(bar->maximum());
253}
254
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700255void
256ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
257{
258 QTextCharFormat timeFormat;
259 timeFormat.setForeground(Qt::gray);
260 timeFormat.setFontUnderline(true);
261 timeFormat.setUnderlineColor(Qt::gray);
262 QTextTableCell timeCell = table->cellAt(0, 1);
263 timeCell.setFormat(timeFormat);
264 timeCell.firstCursorPosition().insertText(formatTime(timestamp));
265}
266
267QString
268ChatDialog::formatTime(time_t timestamp)
269{
270 struct tm *tm_time = localtime(&timestamp);
271 int hour = tm_time->tm_hour;
272 QString amOrPM;
273 if (hour > 12)
274 {
275 hour -= 12;
276 amOrPM = "PM";
277 }
278 else
279 {
280 amOrPM = "AM";
281 if (hour == 0)
282 {
283 hour = 12;
284 }
285 }
286
287 char textTime[12];
288 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
289 return QString(textTime);
290}
291
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700292void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700293ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
294{
295 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700296#ifdef __DEBUG
297 std::cout << "<<< Tree update signal emitted" << std::endl;
298#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700299}
300
301void
302ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700303{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700304#ifdef __DEBUG
305 std::cout << "<<< processing Tree Update" << std::endl;
306#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700307 if (v.empty())
308 {
309 return;
310 }
311
312 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700313 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700314 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700315 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
316 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700317
318 int n = v.size();
319 int totalMissingPackets = 0;
320 for (int i = 0; i < n; i++)
321 {
322 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
323 }
324
Zhenkai Zhubb198112012-09-27 11:31:42 -0700325 for (int i = 0; i < n; i++)
326 {
327 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700328 {
329 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
330 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700331 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700332#ifdef __DEBUG
333 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
334#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700335 }
336 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700337 else
338 {
339 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
340 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700341 }
342
343 // adjust the view
344 fitView();
345
346}
347
348void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700349ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
350{
Zhenkai Zhubb198112012-09-27 11:31:42 -0700351 emit dataReceived(name.c_str(), buf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700352#ifdef __DEBUG
353 std::cout <<"<<< " << name << " fetched" << std::endl;
354#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700355}
356
357void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700358ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
359{
360 emit dataReceived(name.c_str(), buf, len, false);
361}
362
363void
364ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700365{
366 SyncDemo::ChatMessage msg;
367 if (!msg.ParseFromArray(buf, len))
368 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700369 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700370 }
371
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700372 // display msg received from network
373 // we have to do so; this function is called by ccnd thread
374 // so if we call appendMsg directly
375 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
376 // the "cannonical" way to is use signal-slot
Zhenkai Zhubb198112012-09-27 11:31:42 -0700377 if (show)
378 {
379 appendMessage(msg);
380 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700381
382 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700383 std::string stdStrName = name.toStdString();
384 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
385 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
386#ifdef __DEBUG
387 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
388#endif
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700389 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
390 {
391 processRemove(prefix.c_str());
392 }
393 else
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700394 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700395 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700396 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
397 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700398 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700399}
400
401void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700402ChatDialog::processRemoveWrapper(std::string prefix)
403{
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700404#ifdef __DEBUG
405 std::cout << "Sync REMOVE signal received for prefix: " << prefix << std::endl;
406#endif
407 //emit removeReceived(prefix.c_str());
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700408}
409
410void
411ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700412{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700413#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700414 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700415#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700416 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700417 if (removed)
418 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700419 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700420 m_scene->plot(m_sock->getRootDigest().c_str());
421 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700422}
423
424void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700425ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700426 msg.set_from(m_user.getNick().toStdString());
427 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700428 msg.set_data(text.toStdString());
429 time_t seconds = time(NULL);
430 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700431 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700432}
433
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700434void
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700435ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700436{
437 msg.set_from(m_user.getNick().toStdString());
438 msg.set_to(m_user.getChatroom().toStdString());
439 time_t seconds = time(NULL);
440 msg.set_timestamp(seconds);
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700441 msg.set_type(type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700442}
443
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700444static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
445
446QString
447ChatDialog::getRandomString()
448{
449 std::string randStr;
450 boost::random::random_device rng;
451 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
452 for (int i = 0; i < 10; i ++)
453 {
454 randStr += chars[index_dist(rng)];
455 }
456 return randStr.c_str();
457}
458
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700459bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700460ChatDialog::readSettings()
461{
462 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700463 QString nick = s.value("nick", "").toString();
464 QString chatroom = s.value("chatroom", "").toString();
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700465 QString originPrefix = s.value("originPrefix", "").toString();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700466 m_minimaniho = s.value("minimaniho", false).toBool();
467 if (nick == "" || chatroom == "" || originPrefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700468 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700469 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700470 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700471 else {
472 m_user.setNick(nick);
473 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700474 m_user.setOriginPrefix(originPrefix);
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700475 m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700476 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700477 }
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700478
479// QTimer::singleShot(500, this, SLOT(buttonPressed()));
480 // return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700481}
482
483void
484ChatDialog::writeSettings()
485{
486 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700487 s.setValue("nick", m_user.getNick());
488 s.setValue("chatroom", m_user.getChatroom());
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700489 s.setValue("originPrefix", m_user.getOriginPrefix());
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700490 s.setValue("minimaniho", m_minimaniho);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700491}
492
493void
494ChatDialog::updateLabels()
495{
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700496 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
497 infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700498 infoLabel->setText(settingDisp);
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700499 //QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
500 //prefixLabel->setText(prefixDisp);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700501}
502
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700503void
504ChatDialog::returnPressed()
505{
506 QString text = lineEdit->text();
507 if (text.isEmpty())
508 return;
509
Zhenkai Zhub6338822012-05-31 13:27:24 -0700510 lineEdit->clear();
511
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700512 SyncDemo::ChatMessage msg;
513 formChatMessage(text, msg);
514
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700515 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700516
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700517 sendMsg(msg);
518
519 fitView();
520}
521
522void
523ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
524{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700525 // send msg
526 size_t size = msg.ByteSize();
527 char *buf = new char[size];
528 msg.SerializeToArray(buf, size);
529 if (!msg.IsInitialized())
530 {
531 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
532 abort();
533 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700534 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700535
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700536 delete buf;
537
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700538 m_lastMsgTime = time(NULL);
539
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700540 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700541 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700542 std::vector<Sync::MissingDataInfo> v;
543 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700544 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700545 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700546 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
547 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
548 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700549}
550
551void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700552ChatDialog::sendJoin()
553{
554 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700555 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700556 sendMsg(msg);
557 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
558}
559
560void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700561ChatDialog::sendHello()
562{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700563 time_t now = time(NULL);
564 int elapsed = now - m_lastMsgTime;
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700565 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700566 {
567 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700568 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700569 sendMsg(msg);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700570 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700571 }
572 else
573 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700574 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700575 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700576}
577
578void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700579ChatDialog::buttonPressed()
580{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700581 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700582 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700583 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700584 QTimer::singleShot(100, this, SLOT(checkSetting()));
585}
586
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700587void ChatDialog::treeButtonPressed()
588{
589 if (treeViewer->isVisible())
590 {
591 treeViewer->hide();
592 treeButton->setText("Show Sync Tree");
593 }
594 else
595 {
596 treeViewer->show();
597 treeButton->setText("Hide Sync Tree");
598 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700599
600 fitView();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700601}
602
Zhenkai Zhue837f792012-06-05 20:47:54 -0700603void
604ChatDialog::checkSetting()
605{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700606 if (m_user.getOriginPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
Zhenkai Zhue837f792012-06-05 20:47:54 -0700607 {
608 buttonPressed();
609 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700610}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700611
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700612void
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700613ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700614{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700615 QString randString = getRandomString();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700616 bool needWrite = false;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700617 bool needFresh = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700618 if (!nick.isEmpty() && nick != m_user.getNick()) {
619 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700620 needWrite = true;
621 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700622 if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) {
623 m_user.setOriginPrefix(originPrefix);
624 m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700625 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700626 needFresh = true;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700627 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700628 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
629 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700630 m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700631 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700632 needFresh = true;
633 }
634
635 if (needFresh)
636 {
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700637
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700638 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700639 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700640 m_scene->clearAll();
641 m_scene->plot("Empty");
642 }
Zhenkai Zhucc4c2c02012-09-27 21:24:37 -0700643
644 textEdit->clear();
645
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700646 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700647 if (m_sock != NULL)
648 {
649 delete m_sock;
650 m_sock = NULL;
651 }
652 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
653 syncPrefix += "/";
654 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700655 try
656 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700657 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700658 sendJoin();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700659 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700660 }
661 catch (Sync::CcnxOperationException ex)
662 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700663 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 -0700664 std::exit(1);
665 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700666
667 fitView();
668
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700669 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700670
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700671 if (needWrite) {
672 writeSettings();
673 updateLabels();
674 }
675}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700676
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700677void
678ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
679{
680 switch (reason)
681 {
682 case QSystemTrayIcon::Trigger:
683 case QSystemTrayIcon::DoubleClick:
684 break;
685 case QSystemTrayIcon::MiddleClick:
686 // showMessage();
687 break;
688 default:;
689 }
690}
691
692void
693ChatDialog::showMessage(QString from, QString data)
694{
695 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
696 if (!isActiveWindow())
697 {
698 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
699 trayIcon->setIcon(QIcon(":/images/note.png"));
700 }
701}
702
703void
704ChatDialog::messageClicked()
705{
706 this->showMaximized();
707}
708
709void
710ChatDialog::createActions()
711{
712 minimizeAction = new QAction(tr("Mi&nimize"), this);
713 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
714
715 maximizeAction = new QAction(tr("Ma&ximize"), this);
716 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
717
718 restoreAction = new QAction(tr("&Restore"), this);
719 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
720
721 quitAction = new QAction(tr("Quit"), this);
722 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
723}
724
725void
726ChatDialog::createTrayIcon()
727{
728 trayIconMenu = new QMenu(this);
729 trayIconMenu->addAction(minimizeAction);
730 trayIconMenu->addAction(maximizeAction);
731 trayIconMenu->addAction(restoreAction);
732 trayIconMenu->addSeparator();
733 trayIconMenu->addAction(quitAction);
734
735 trayIcon = new QSystemTrayIcon(this);
736 trayIcon->setContextMenu(trayIconMenu);
737
738 QIcon icon(":/images/icon_small.png");
739 trayIcon->setIcon(icon);
740 setWindowIcon(icon);
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700741 trayIcon->setToolTip("Chronos System Tray Icon");
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700742 trayIcon->setVisible(true);
743}
744
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700745void
746ChatDialog::resizeEvent(QResizeEvent *e)
747{
748 fitView();
749}
750
751void
752ChatDialog::showEvent(QShowEvent *e)
753{
754 fitView();
755}
756
757void
758ChatDialog::fitView()
759{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700760 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700761 QRectF rect = m_scene->itemsBoundingRect();
762 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700763 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700764}