blob: e118969dcf07890f55830f159042cd3a4f83df71 [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 HELLO_INTERVAL = 90; // seconds
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070015
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070016ChatDialog::ChatDialog(QWidget *parent)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -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 Zhu6082ede2012-09-27 17:28:46 -070042 m_rosterModel = new QStringListModel(this);
43 listView->setModel(m_rosterModel);
44
Zhenkai Zhu86df7412012-09-27 16:30:20 -070045 createActions();
46 createTrayIcon();
47 m_timer = new QTimer(this);
48 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
49 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
50 connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool)), this, SLOT(processData(QString, const char *, size_t, bool)));
51 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
52 connect(this, SIGNAL(removeReceived(QString)), this, SLOT(processRemove(QString)));
53 connect(m_timer, SIGNAL(timeout()), this, SLOT(replot()));
54 connect(m_scene, SIGNAL(replot()), this, SLOT(replot()));
55 connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal()));
56 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070057 connect(m_scene, SIGNAL(rosterChanged()), this, SLOT(updateRosterList()));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070058
Zhenkai Zhu82a62752012-06-04 17:11:04 -070059 // create sync socket
60 if(!m_user.getChatroom().isEmpty()) {
61 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
62 syncPrefix += "/";
63 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070064 try
65 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070066 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
67 sendHello();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070068 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070069 }
70 catch (Sync::CcnxOperationException ex)
71 {
72 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
73 std::exit(1);
74 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -070075 }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070076
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070077//testDraw();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070078}
79
Zhenkai Zhu82a62752012-06-04 17:11:04 -070080ChatDialog::~ChatDialog()
81{
82 if (m_sock != NULL)
83 {
Zhenkai Zhu591e8c32012-09-26 11:57:50 -070084 m_sock->remove(m_user.getPrefix().toStdString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -070085 delete m_sock;
86 m_sock = NULL;
87 }
88}
89
Zhenkai Zhu86df7412012-09-27 16:30:20 -070090void
91ChatDialog::replot()
92{
Zhenkai Zhu9036e032012-09-27 20:59:33 -070093 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu86df7412012-09-27 16:30:20 -070094 m_scene->plot(m_sock->getRootDigest().c_str());
95}
96
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070097void
98ChatDialog::updateRosterList()
99{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700100 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700101 QStringList rosterList = m_scene->getRosterList();
102 m_rosterModel->setStringList(rosterList);
103}
104
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700105void
106ChatDialog::setVisible(bool visible)
107{
108 minimizeAction->setEnabled(visible);
109 maximizeAction->setEnabled(!isMaximized());
110 restoreAction->setEnabled(isMaximized() || !visible);
111 QDialog::setVisible(visible);
112}
113
114void
115ChatDialog::closeEvent(QCloseEvent *e)
116{
117 if (trayIcon->isVisible())
118 {
119 QMessageBox::information(this, tr("Sync-Demo"),
120 tr("The program will keep running in the "
121 "system tray. To terminate the program"
122 "choose <b>Quit</b> in the context memu"
123 "of the system tray entry."));
124 hide();
125 e->ignore();
126 }
127}
128
129void
130ChatDialog::changeEvent(QEvent *e)
131{
132 switch(e->type())
133 {
134 case QEvent::ActivationChange:
135 if (isActiveWindow())
136 {
137 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
138 }
139 break;
140 default:
141 break;
142 }
143}
144
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700145void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700146ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700147{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700148 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700149
150 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700151 return;
Zhenkai Zhub6338822012-05-31 13:27:24 -0700152 }
153
154 if (!msg.has_data()) {
155 return;
156 }
157
158 if (msg.from().empty() || msg.data().empty()) {
159 return;
160 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700161
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700162 if (!msg.has_timestamp())
163 {
164 return;
165 }
166
167 QTextCharFormat nickFormat;
168 nickFormat.setForeground(Qt::darkGreen);
169 nickFormat.setFontWeight(QFont::Bold);
170 nickFormat.setFontUnderline(true);
171 nickFormat.setUnderlineColor(Qt::gray);
172 QTextCharFormat timeFormat;
173 timeFormat.setForeground(Qt::gray);
174 timeFormat.setFontUnderline(true);
175 timeFormat.setUnderlineColor(Qt::gray);
176
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700177 QTextCursor cursor(textEdit->textCursor());
178 cursor.movePosition(QTextCursor::End);
179 QTextTableFormat tableFormat;
180 tableFormat.setBorder(0);
181 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700182 QString from = QString("%1 ").arg(msg.from().c_str());
183 QTextTableCell fromCell = table->cellAt(0, 0);
184 fromCell.setFormat(nickFormat);
185 fromCell.firstCursorPosition().insertText(from);
186 QTextTableCell timeCell = table->cellAt(0, 1);
187 timeCell.setFormat(timeFormat);
188 time_t timestamp = msg.timestamp();
189 struct tm *tm_time = localtime(&timestamp);
190 int hour = tm_time->tm_hour;
191 QString amOrPM;
192 if (hour > 12)
193 {
194 hour -= 12;
195 amOrPM = "PM";
196 }
197 else
198 {
199 amOrPM = "AM";
200 if (hour == 0)
201 {
202 hour = 12;
203 }
204 }
205
206 char textTime[12];
207 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
208 timeCell.firstCursorPosition().insertText(textTime);
209
210
211 QTextCursor nextCursor(textEdit->textCursor());
212 table = nextCursor.insertTable(1, 1, tableFormat);
213 table->cellAt(0, 0).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700214 QScrollBar *bar = textEdit->verticalScrollBar();
215 bar->setValue(bar->maximum());
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700216 showMessage(from, msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700217}
218
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700219void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700220ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
221{
222 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700223#ifdef __DEBUG
224 std::cout << "<<< Tree update signal emitted" << std::endl;
225#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700226}
227
228void
229ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700230{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700231#ifdef __DEBUG
232 std::cout << "<<< processing Tree Update" << std::endl;
233#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700234 if (v.empty())
235 {
236 return;
237 }
238
239 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700240 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700241 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700242 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
243 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700244
245 int n = v.size();
246 int totalMissingPackets = 0;
247 for (int i = 0; i < n; i++)
248 {
249 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
250 }
251
Zhenkai Zhubb198112012-09-27 11:31:42 -0700252 for (int i = 0; i < n; i++)
253 {
254 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700255 {
256 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
257 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700258 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700259#ifdef __DEBUG
260 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
261#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700262 }
263 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700264 else
265 {
266 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
267 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700268 }
269
270 // adjust the view
271 fitView();
272
273}
274
275void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700276ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
277{
Zhenkai Zhubb198112012-09-27 11:31:42 -0700278 emit dataReceived(name.c_str(), buf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700279#ifdef __DEBUG
280 std::cout <<"<<< " << name << " fetched" << std::endl;
281#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700282}
283
284void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700285ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
286{
287 emit dataReceived(name.c_str(), buf, len, false);
288}
289
290void
291ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700292{
293 SyncDemo::ChatMessage msg;
294 if (!msg.ParseFromArray(buf, len))
295 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700296 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700297 }
298
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700299 // display msg received from network
300 // we have to do so; this function is called by ccnd thread
301 // so if we call appendMsg directly
302 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
303 // the "cannonical" way to is use signal-slot
Zhenkai Zhubb198112012-09-27 11:31:42 -0700304 if (show)
305 {
306 appendMessage(msg);
307 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700308
309 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700310 std::string stdStrName = name.toStdString();
311 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
312 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
313#ifdef __DEBUG
314 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
315#endif
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700316 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700317 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700318 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
319 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700320 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700321}
322
323void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700324ChatDialog::processRemoveWrapper(std::string prefix)
325{
326 emit removeReceived(prefix.c_str());
327}
328
329void
330ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700331{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700332#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700333 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700334#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700335 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700336 if (removed)
337 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700338 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700339 m_scene->plot(m_sock->getRootDigest().c_str());
340 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700341}
342
343void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700344ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700345 msg.set_from(m_user.getNick().toStdString());
346 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700347 msg.set_data(text.toStdString());
348 time_t seconds = time(NULL);
349 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700350 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700351}
352
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700353void
354ChatDialog::formHelloMessage(SyncDemo::ChatMessage &msg)
355{
356 msg.set_from(m_user.getNick().toStdString());
357 msg.set_to(m_user.getChatroom().toStdString());
358 time_t seconds = time(NULL);
359 msg.set_timestamp(seconds);
360 msg.set_type(SyncDemo::ChatMessage::HELLO);
361}
362
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700363static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
364
365QString
366ChatDialog::getRandomString()
367{
368 std::string randStr;
369 boost::random::random_device rng;
370 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
371 for (int i = 0; i < 10; i ++)
372 {
373 randStr += chars[index_dist(rng)];
374 }
375 return randStr.c_str();
376}
377
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700378bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700379ChatDialog::readSettings()
380{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700381#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700382 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700383 QString nick = s.value("nick", "").toString();
384 QString chatroom = s.value("chatroom", "").toString();
385 QString prefix = s.value("prefix", "").toString();
386 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700387 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700388 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700389 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700390 else {
391 m_user.setNick(nick);
392 m_user.setChatroom(chatroom);
393 m_user.setPrefix(prefix);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700394 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700395 }
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700396#else
397 QTimer::singleShot(500, this, SLOT(buttonPressed()));
398 return false;
399#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700400}
401
402void
403ChatDialog::writeSettings()
404{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700405#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700406 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700407 s.setValue("nick", m_user.getNick());
408 s.setValue("chatroom", m_user.getChatroom());
409 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700410#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700411}
412
413void
414ChatDialog::updateLabels()
415{
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700416 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
417 infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700418 infoLabel->setText(settingDisp);
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700419 //QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
420 //prefixLabel->setText(prefixDisp);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700421}
422
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700423void
424ChatDialog::returnPressed()
425{
426 QString text = lineEdit->text();
427 if (text.isEmpty())
428 return;
429
Zhenkai Zhub6338822012-05-31 13:27:24 -0700430 lineEdit->clear();
431
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700432 SyncDemo::ChatMessage msg;
433 formChatMessage(text, msg);
434
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700435 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700436
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700437 sendMsg(msg);
438
439 fitView();
440}
441
442void
443ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
444{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700445 // send msg
446 size_t size = msg.ByteSize();
447 char *buf = new char[size];
448 msg.SerializeToArray(buf, size);
449 if (!msg.IsInitialized())
450 {
451 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
452 abort();
453 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700454 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700455
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700456 delete buf;
457
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700458 m_lastMsgTime = time(NULL);
459
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700460 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700461 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700462 std::vector<Sync::MissingDataInfo> v;
463 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700464 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700465 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700466 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
467 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
468 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700469}
470
471void
472ChatDialog::sendHello()
473{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700474 time_t now = time(NULL);
475 int elapsed = now - m_lastMsgTime;
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700476 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700477 {
478 SyncDemo::ChatMessage msg;
479 formHelloMessage(msg);
480 sendMsg(msg);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700481 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700482 }
483 else
484 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700485 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700486 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700487}
488
489void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700490ChatDialog::buttonPressed()
491{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700492 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700493 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700494 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700495 QTimer::singleShot(100, this, SLOT(checkSetting()));
496}
497
498void
499ChatDialog::checkSetting()
500{
501 if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
502 {
503 buttonPressed();
504 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700505}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700506
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700507void
508ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
509{
510 bool needWrite = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700511 if (!nick.isEmpty() && nick != m_user.getNick()) {
512 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700513 needWrite = true;
514 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700515 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700516 m_user.setPrefix(prefix + "/" + getRandomString());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700517 needWrite = true;
518 // TODO: set the previous prefix as left?
519 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700520 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
521 m_user.setChatroom(chatroom);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700522 needWrite = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700523
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700524 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700525 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700526 m_scene->clearAll();
527 m_scene->plot("Empty");
528 }
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700529 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700530 if (m_sock != NULL)
531 {
532 delete m_sock;
533 m_sock = NULL;
534 }
535 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
536 syncPrefix += "/";
537 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700538 try
539 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700540 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
541 sendHello();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700542 m_timer->start(FRESHNESS * 2000);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700543 }
544 catch (Sync::CcnxOperationException ex)
545 {
546 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
547 std::exit(1);
548 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700549
550 fitView();
551
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700552 }
553 if (needWrite) {
554 writeSettings();
555 updateLabels();
556 }
557}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700558
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700559void
560ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
561{
562 switch (reason)
563 {
564 case QSystemTrayIcon::Trigger:
565 case QSystemTrayIcon::DoubleClick:
566 break;
567 case QSystemTrayIcon::MiddleClick:
568 // showMessage();
569 break;
570 default:;
571 }
572}
573
574void
575ChatDialog::showMessage(QString from, QString data)
576{
577 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
578 if (!isActiveWindow())
579 {
580 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
581 trayIcon->setIcon(QIcon(":/images/note.png"));
582 }
583}
584
585void
586ChatDialog::messageClicked()
587{
588 this->showMaximized();
589}
590
591void
592ChatDialog::createActions()
593{
594 minimizeAction = new QAction(tr("Mi&nimize"), this);
595 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
596
597 maximizeAction = new QAction(tr("Ma&ximize"), this);
598 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
599
600 restoreAction = new QAction(tr("&Restore"), this);
601 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
602
603 quitAction = new QAction(tr("Quit"), this);
604 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
605}
606
607void
608ChatDialog::createTrayIcon()
609{
610 trayIconMenu = new QMenu(this);
611 trayIconMenu->addAction(minimizeAction);
612 trayIconMenu->addAction(maximizeAction);
613 trayIconMenu->addAction(restoreAction);
614 trayIconMenu->addSeparator();
615 trayIconMenu->addAction(quitAction);
616
617 trayIcon = new QSystemTrayIcon(this);
618 trayIcon->setContextMenu(trayIconMenu);
619
620 QIcon icon(":/images/icon_small.png");
621 trayIcon->setIcon(icon);
622 setWindowIcon(icon);
623 trayIcon->setToolTip("Sync-Demo System Tray Icon");
624 trayIcon->setVisible(true);
625}
626
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700627void
628ChatDialog::resizeEvent(QResizeEvent *e)
629{
630 fitView();
631}
632
633void
634ChatDialog::showEvent(QShowEvent *e)
635{
636 fitView();
637}
638
639void
640ChatDialog::fitView()
641{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700642 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700643 QRectF rect = m_scene->itemsBoundingRect();
644 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700645 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700646}