blob: 81237e2b98d7dd5ec32a81e251b5ea6845da7fba [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 Zhu82a62752012-06-04 17:11:04 -070012#define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo"
13
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -070014static const int FRESHNESS = 120; // seconds
15static const int HELLO_INTERVAL = 90; // seconds
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070016
Zhenkai Zhu21d75f92012-06-04 21:23:34 -070017void
18ChatDialog::testDraw()
19{
20 std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"};
21 std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"};
22 std::vector<Sync::MissingDataInfo> v;
23 for (int i = 0; i < 5; i++)
24 {
25 Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )};
26 v.push_back(mdi);
27 }
28
29 m_scene->processUpdate(v, "12341234@!#%!@");
30
31 for (int i = 0; i < 5; i++)
32 {
33 m_scene-> msgReceived(prefix[i].c_str(), nick[i].c_str());
34 }
35
36 fitView();
37}
38
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070039ChatDialog::ChatDialog(QWidget *parent)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070040 : QDialog(parent), m_sock(NULL), m_lastMsgTime(0)
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070041{
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070042 // have to register this, otherwise
43 // the signal-slot system won't recognize this type
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070044 qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
45 qRegisterMetaType<size_t>("size_t");
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070046 setupUi(this);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070047 m_session = time(NULL);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -070048 boost::random::random_device rng;
49 boost::random::uniform_int_distribution<> uniform(1, 29000);
50 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070051
52 readSettings();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070053
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070054 updateLabels();
55
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070056 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070057 m_scene = new DigestTreeScene(this);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070058
Zhenkai Zhu82a62752012-06-04 17:11:04 -070059 treeViewer->setScene(m_scene);
60 m_scene->plot("Empty");
61 QRectF rect = m_scene->itemsBoundingRect();
62 m_scene->setSceneRect(rect);
63
64 // create sync socket
65 if(!m_user.getChatroom().isEmpty()) {
66 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
67 syncPrefix += "/";
68 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070069 try
70 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070071 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
72 sendHello();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070073 }
74 catch (Sync::CcnxOperationException ex)
75 {
76 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
77 std::exit(1);
78 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -070079 }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070080
81 createActions();
82 createTrayIcon();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070083 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu85845d22012-06-01 23:10:43 -070084 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhubb198112012-09-27 11:31:42 -070085 connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool)), this, SLOT(processData(QString, const char *, size_t, bool)));
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070086 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070087 connect(this, SIGNAL(removeReceived(QString)), this, SLOT(processRemove(QString)));
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070088 connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal()));
89 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
90
91//testDraw();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070092}
93
Zhenkai Zhu82a62752012-06-04 17:11:04 -070094ChatDialog::~ChatDialog()
95{
96 if (m_sock != NULL)
97 {
Zhenkai Zhu591e8c32012-09-26 11:57:50 -070098 m_sock->remove(m_user.getPrefix().toStdString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -070099 delete m_sock;
100 m_sock = NULL;
101 }
102}
103
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700104void
105ChatDialog::setVisible(bool visible)
106{
107 minimizeAction->setEnabled(visible);
108 maximizeAction->setEnabled(!isMaximized());
109 restoreAction->setEnabled(isMaximized() || !visible);
110 QDialog::setVisible(visible);
111}
112
113void
114ChatDialog::closeEvent(QCloseEvent *e)
115{
116 if (trayIcon->isVisible())
117 {
118 QMessageBox::information(this, tr("Sync-Demo"),
119 tr("The program will keep running in the "
120 "system tray. To terminate the program"
121 "choose <b>Quit</b> in the context memu"
122 "of the system tray entry."));
123 hide();
124 e->ignore();
125 }
126}
127
128void
129ChatDialog::changeEvent(QEvent *e)
130{
131 switch(e->type())
132 {
133 case QEvent::ActivationChange:
134 if (isActiveWindow())
135 {
136 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
137 }
138 break;
139 default:
140 break;
141 }
142}
143
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700144void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700145ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700146{
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700147 boost::mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700148
149 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700150 return;
Zhenkai Zhub6338822012-05-31 13:27:24 -0700151 }
152
153 if (!msg.has_data()) {
154 return;
155 }
156
157 if (msg.from().empty() || msg.data().empty()) {
158 return;
159 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700160
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700161 if (!msg.has_timestamp())
162 {
163 return;
164 }
165
166 QTextCharFormat nickFormat;
167 nickFormat.setForeground(Qt::darkGreen);
168 nickFormat.setFontWeight(QFont::Bold);
169 nickFormat.setFontUnderline(true);
170 nickFormat.setUnderlineColor(Qt::gray);
171 QTextCharFormat timeFormat;
172 timeFormat.setForeground(Qt::gray);
173 timeFormat.setFontUnderline(true);
174 timeFormat.setUnderlineColor(Qt::gray);
175
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700176 QTextCursor cursor(textEdit->textCursor());
177 cursor.movePosition(QTextCursor::End);
178 QTextTableFormat tableFormat;
179 tableFormat.setBorder(0);
180 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700181 QString from = QString("%1 ").arg(msg.from().c_str());
182 QTextTableCell fromCell = table->cellAt(0, 0);
183 fromCell.setFormat(nickFormat);
184 fromCell.firstCursorPosition().insertText(from);
185 QTextTableCell timeCell = table->cellAt(0, 1);
186 timeCell.setFormat(timeFormat);
187 time_t timestamp = msg.timestamp();
188 struct tm *tm_time = localtime(&timestamp);
189 int hour = tm_time->tm_hour;
190 QString amOrPM;
191 if (hour > 12)
192 {
193 hour -= 12;
194 amOrPM = "PM";
195 }
196 else
197 {
198 amOrPM = "AM";
199 if (hour == 0)
200 {
201 hour = 12;
202 }
203 }
204
205 char textTime[12];
206 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
207 timeCell.firstCursorPosition().insertText(textTime);
208
209
210 QTextCursor nextCursor(textEdit->textCursor());
211 table = nextCursor.insertTable(1, 1, tableFormat);
212 table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700213 QScrollBar *bar = textEdit->verticalScrollBar();
214 bar->setValue(bar->maximum());
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700215 showMessage(from, msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700216}
217
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700218void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700219ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
220{
221 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700222#ifdef __DEBUG
223 std::cout << "<<< Tree update signal emitted" << std::endl;
224#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700225}
226
227void
228ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700229{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700230#ifdef __DEBUG
231 std::cout << "<<< processing Tree Update" << std::endl;
232#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700233 if (v.empty())
234 {
235 return;
236 }
237
238 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700239 {
240 boost::mutex::scoped_lock lock(m_sceneMutex);
241 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
242 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700243
244 int n = v.size();
245 int totalMissingPackets = 0;
246 for (int i = 0; i < n; i++)
247 {
248 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
249 }
250
Zhenkai Zhubb198112012-09-27 11:31:42 -0700251 for (int i = 0; i < n; i++)
252 {
253 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700254 {
255 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
256 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700257 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700258#ifdef __DEBUG
259 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
260#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700261 }
262 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700263 else
264 {
265 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
266 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700267 }
268
269 // adjust the view
270 fitView();
271
272}
273
274void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700275ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
276{
Zhenkai Zhubb198112012-09-27 11:31:42 -0700277 emit dataReceived(name.c_str(), buf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700278#ifdef __DEBUG
279 std::cout <<"<<< " << name << " fetched" << std::endl;
280#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700281}
282
283void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700284ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
285{
286 emit dataReceived(name.c_str(), buf, len, false);
287}
288
289void
290ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700291{
292 SyncDemo::ChatMessage msg;
293 if (!msg.ParseFromArray(buf, len))
294 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700295 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700296 }
297
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700298 // display msg received from network
299 // we have to do so; this function is called by ccnd thread
300 // so if we call appendMsg directly
301 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
302 // the "cannonical" way to is use signal-slot
Zhenkai Zhubb198112012-09-27 11:31:42 -0700303 if (show)
304 {
305 appendMessage(msg);
306 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700307
308 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700309 std::string stdStrName = name.toStdString();
310 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
311 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
312#ifdef __DEBUG
313 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
314#endif
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700315 {
316 boost::mutex::scoped_lock lock(m_sceneMutex);
317 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
318 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700319 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700320}
321
322void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700323ChatDialog::processRemoveWrapper(std::string prefix)
324{
325 emit removeReceived(prefix.c_str());
326}
327
328void
329ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700330{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700331#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700332 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700333#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700334 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700335 if (removed)
336 {
337 m_scene->plot(m_sock->getRootDigest().c_str());
338 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700339}
340
341void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700342ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700343 msg.set_from(m_user.getNick().toStdString());
344 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700345 msg.set_data(text.toStdString());
346 time_t seconds = time(NULL);
347 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700348 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700349}
350
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700351void
352ChatDialog::formHelloMessage(SyncDemo::ChatMessage &msg)
353{
354 msg.set_from(m_user.getNick().toStdString());
355 msg.set_to(m_user.getChatroom().toStdString());
356 time_t seconds = time(NULL);
357 msg.set_timestamp(seconds);
358 msg.set_type(SyncDemo::ChatMessage::HELLO);
359}
360
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700361static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
362
363QString
364ChatDialog::getRandomString()
365{
366 std::string randStr;
367 boost::random::random_device rng;
368 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
369 for (int i = 0; i < 10; i ++)
370 {
371 randStr += chars[index_dist(rng)];
372 }
373 return randStr.c_str();
374}
375
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700376bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700377ChatDialog::readSettings()
378{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700379#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700380 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700381 QString nick = s.value("nick", "").toString();
382 QString chatroom = s.value("chatroom", "").toString();
383 QString prefix = s.value("prefix", "").toString();
384 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700385 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700386 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700387 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700388 else {
389 m_user.setNick(nick);
390 m_user.setChatroom(chatroom);
391 m_user.setPrefix(prefix);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700392 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700393 }
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700394#else
395 QTimer::singleShot(500, this, SLOT(buttonPressed()));
396 return false;
397#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700398}
399
400void
401ChatDialog::writeSettings()
402{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700403#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700404 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700405 s.setValue("nick", m_user.getNick());
406 s.setValue("chatroom", m_user.getChatroom());
407 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700408#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700409}
410
411void
412ChatDialog::updateLabels()
413{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700414 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700415 infoLabel->setText(settingDisp);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700416 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700417 prefixLabel->setText(prefixDisp);
418}
419
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700420void
421ChatDialog::returnPressed()
422{
423 QString text = lineEdit->text();
424 if (text.isEmpty())
425 return;
426
Zhenkai Zhub6338822012-05-31 13:27:24 -0700427 lineEdit->clear();
428
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700429 SyncDemo::ChatMessage msg;
430 formChatMessage(text, msg);
431
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700432 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700433
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700434 sendMsg(msg);
435
436 fitView();
437}
438
439void
440ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
441{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700442 // send msg
443 size_t size = msg.ByteSize();
444 char *buf = new char[size];
445 msg.SerializeToArray(buf, size);
446 if (!msg.IsInitialized())
447 {
448 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
449 abort();
450 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700451 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700452
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700453 delete buf;
454
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700455 m_lastMsgTime = time(NULL);
456
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700457 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700458 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700459 std::vector<Sync::MissingDataInfo> v;
460 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700461 {
462 boost::mutex::scoped_lock lock(m_sceneMutex);
463 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
464 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
465 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700466}
467
468void
469ChatDialog::sendHello()
470{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700471 time_t now = time(NULL);
472 int elapsed = now - m_lastMsgTime;
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700473 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700474 {
475 SyncDemo::ChatMessage msg;
476 formHelloMessage(msg);
477 sendMsg(msg);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700478 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700479 }
480 else
481 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700482 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700483 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700484}
485
486void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700487ChatDialog::buttonPressed()
488{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700489 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700490 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700491 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700492 QTimer::singleShot(100, this, SLOT(checkSetting()));
493}
494
495void
496ChatDialog::checkSetting()
497{
498 if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
499 {
500 buttonPressed();
501 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700502}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700503
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700504void
505ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
506{
507 bool needWrite = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700508 if (!nick.isEmpty() && nick != m_user.getNick()) {
509 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700510 needWrite = true;
511 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700512 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700513 m_user.setPrefix(prefix + "/" + getRandomString());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700514 needWrite = true;
515 // TODO: set the previous prefix as left?
516 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700517 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
518 m_user.setChatroom(chatroom);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700519 needWrite = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700520
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700521 {
522 boost::mutex::scoped_lock lock(m_sceneMutex);
523 m_scene->clearAll();
524 m_scene->plot("Empty");
525 }
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700526 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700527 if (m_sock != NULL)
528 {
529 delete m_sock;
530 m_sock = NULL;
531 }
532 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
533 syncPrefix += "/";
534 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700535 try
536 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700537 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
538 sendHello();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700539 }
540 catch (Sync::CcnxOperationException ex)
541 {
542 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
543 std::exit(1);
544 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700545
546 fitView();
547
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700548 }
549 if (needWrite) {
550 writeSettings();
551 updateLabels();
552 }
553}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700554
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700555void
556ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
557{
558 switch (reason)
559 {
560 case QSystemTrayIcon::Trigger:
561 case QSystemTrayIcon::DoubleClick:
562 break;
563 case QSystemTrayIcon::MiddleClick:
564 // showMessage();
565 break;
566 default:;
567 }
568}
569
570void
571ChatDialog::showMessage(QString from, QString data)
572{
573 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
574 if (!isActiveWindow())
575 {
576 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
577 trayIcon->setIcon(QIcon(":/images/note.png"));
578 }
579}
580
581void
582ChatDialog::messageClicked()
583{
584 this->showMaximized();
585}
586
587void
588ChatDialog::createActions()
589{
590 minimizeAction = new QAction(tr("Mi&nimize"), this);
591 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
592
593 maximizeAction = new QAction(tr("Ma&ximize"), this);
594 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
595
596 restoreAction = new QAction(tr("&Restore"), this);
597 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
598
599 quitAction = new QAction(tr("Quit"), this);
600 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
601}
602
603void
604ChatDialog::createTrayIcon()
605{
606 trayIconMenu = new QMenu(this);
607 trayIconMenu->addAction(minimizeAction);
608 trayIconMenu->addAction(maximizeAction);
609 trayIconMenu->addAction(restoreAction);
610 trayIconMenu->addSeparator();
611 trayIconMenu->addAction(quitAction);
612
613 trayIcon = new QSystemTrayIcon(this);
614 trayIcon->setContextMenu(trayIconMenu);
615
616 QIcon icon(":/images/icon_small.png");
617 trayIcon->setIcon(icon);
618 setWindowIcon(icon);
619 trayIcon->setToolTip("Sync-Demo System Tray Icon");
620 trayIcon->setVisible(true);
621}
622
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700623void
624ChatDialog::resizeEvent(QResizeEvent *e)
625{
626 fitView();
627}
628
629void
630ChatDialog::showEvent(QShowEvent *e)
631{
632 fitView();
633}
634
635void
636ChatDialog::fitView()
637{
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700638 boost::mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700639 QRectF rect = m_scene->itemsBoundingRect();
640 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700641 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700642}