blob: 2449ed653df15f368754adf36673743b9939ce58 [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 Zhu3e26bb42012-09-27 11:04:09 -070014static const int FRESHNESS = 60;
15static const int HELLO_INTERVAL = 59;
16
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 Zhu7e9b06d2012-06-02 00:44:42 -070048
49 readSettings();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070050
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070051 updateLabels();
52
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070053 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070054 m_scene = new DigestTreeScene(this);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070055
Zhenkai Zhu82a62752012-06-04 17:11:04 -070056 treeViewer->setScene(m_scene);
57 m_scene->plot("Empty");
58 QRectF rect = m_scene->itemsBoundingRect();
59 m_scene->setSceneRect(rect);
60
61 // create sync socket
62 if(!m_user.getChatroom().isEmpty()) {
63 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
64 syncPrefix += "/";
65 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070066 try
67 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070068 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
69 sendHello();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070070 }
71 catch (Sync::CcnxOperationException ex)
72 {
73 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
74 std::exit(1);
75 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -070076 }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070077
78 createActions();
79 createTrayIcon();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070080 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu85845d22012-06-01 23:10:43 -070081 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhubb198112012-09-27 11:31:42 -070082 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 -070083 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070084 connect(this, SIGNAL(removeReceived(QString)), this, SLOT(processRemove(QString)));
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070085 connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal()));
86 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
87
88//testDraw();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070089}
90
Zhenkai Zhu82a62752012-06-04 17:11:04 -070091ChatDialog::~ChatDialog()
92{
93 if (m_sock != NULL)
94 {
Zhenkai Zhu591e8c32012-09-26 11:57:50 -070095 m_sock->remove(m_user.getPrefix().toStdString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -070096 delete m_sock;
97 m_sock = NULL;
98 }
99}
100
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700101void
102ChatDialog::setVisible(bool visible)
103{
104 minimizeAction->setEnabled(visible);
105 maximizeAction->setEnabled(!isMaximized());
106 restoreAction->setEnabled(isMaximized() || !visible);
107 QDialog::setVisible(visible);
108}
109
110void
111ChatDialog::closeEvent(QCloseEvent *e)
112{
113 if (trayIcon->isVisible())
114 {
115 QMessageBox::information(this, tr("Sync-Demo"),
116 tr("The program will keep running in the "
117 "system tray. To terminate the program"
118 "choose <b>Quit</b> in the context memu"
119 "of the system tray entry."));
120 hide();
121 e->ignore();
122 }
123}
124
125void
126ChatDialog::changeEvent(QEvent *e)
127{
128 switch(e->type())
129 {
130 case QEvent::ActivationChange:
131 if (isActiveWindow())
132 {
133 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
134 }
135 break;
136 default:
137 break;
138 }
139}
140
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700141void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700142ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700143{
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700144 boost::mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700145
146 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700147 return;
Zhenkai Zhub6338822012-05-31 13:27:24 -0700148 }
149
150 if (!msg.has_data()) {
151 return;
152 }
153
154 if (msg.from().empty() || msg.data().empty()) {
155 return;
156 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700157
158 QTextCursor cursor(textEdit->textCursor());
159 cursor.movePosition(QTextCursor::End);
160 QTextTableFormat tableFormat;
161 tableFormat.setBorder(0);
162 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700163 QString from = QString("<%1>: ").arg(msg.from().c_str());
164 table->cellAt(0, 0).firstCursorPosition().insertText(from);
165 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700166 QScrollBar *bar = textEdit->verticalScrollBar();
167 bar->setValue(bar->maximum());
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700168 showMessage(from, msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700169}
170
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700171void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700172ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
173{
174 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700175#ifdef __DEBUG
176 std::cout << "<<< Tree update signal emitted" << std::endl;
177#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700178}
179
180void
181ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700182{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700183#ifdef __DEBUG
184 std::cout << "<<< processing Tree Update" << std::endl;
185#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700186 if (v.empty())
187 {
188 return;
189 }
190
191 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700192 {
193 boost::mutex::scoped_lock lock(m_sceneMutex);
194 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
195 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700196
197 int n = v.size();
198 int totalMissingPackets = 0;
199 for (int i = 0; i < n; i++)
200 {
201 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
202 }
203
Zhenkai Zhubb198112012-09-27 11:31:42 -0700204 for (int i = 0; i < n; i++)
205 {
206 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700207 {
208 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
209 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700210 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700211#ifdef __DEBUG
212 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
213#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700214 }
215 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700216 else
217 {
218 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
219 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700220 }
221
222 // adjust the view
223 fitView();
224
225}
226
227void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700228ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
229{
Zhenkai Zhubb198112012-09-27 11:31:42 -0700230 emit dataReceived(name.c_str(), buf, len, true);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700231#ifdef __DEBUG
232 std::cout <<"<<< " << name << " fetched" << std::endl;
233#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700234}
235
236void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700237ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
238{
239 emit dataReceived(name.c_str(), buf, len, false);
240}
241
242void
243ChatDialog::processData(QString name, const char *buf, size_t len, bool show)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700244{
245 SyncDemo::ChatMessage msg;
246 if (!msg.ParseFromArray(buf, len))
247 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700248 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700249 }
250
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700251 // display msg received from network
252 // we have to do so; this function is called by ccnd thread
253 // so if we call appendMsg directly
254 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
255 // the "cannonical" way to is use signal-slot
Zhenkai Zhubb198112012-09-27 11:31:42 -0700256 if (show)
257 {
258 appendMessage(msg);
259 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700260
261 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700262 std::string stdStrName = name.toStdString();
263 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
264 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
265#ifdef __DEBUG
266 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
267#endif
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700268 {
269 boost::mutex::scoped_lock lock(m_sceneMutex);
270 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
271 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700272 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700273}
274
275void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700276ChatDialog::processRemoveWrapper(std::string prefix)
277{
278 emit removeReceived(prefix.c_str());
279}
280
281void
282ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700283{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700284#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700285 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700286#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700287 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700288 if (removed)
289 {
290 m_scene->plot(m_sock->getRootDigest().c_str());
291 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700292}
293
294void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700295ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700296 msg.set_from(m_user.getNick().toStdString());
297 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700298 msg.set_data(text.toStdString());
299 time_t seconds = time(NULL);
300 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700301 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700302}
303
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700304void
305ChatDialog::formHelloMessage(SyncDemo::ChatMessage &msg)
306{
307 msg.set_from(m_user.getNick().toStdString());
308 msg.set_to(m_user.getChatroom().toStdString());
309 time_t seconds = time(NULL);
310 msg.set_timestamp(seconds);
311 msg.set_type(SyncDemo::ChatMessage::HELLO);
312}
313
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700314static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
315
316QString
317ChatDialog::getRandomString()
318{
319 std::string randStr;
320 boost::random::random_device rng;
321 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
322 for (int i = 0; i < 10; i ++)
323 {
324 randStr += chars[index_dist(rng)];
325 }
326 return randStr.c_str();
327}
328
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700329bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700330ChatDialog::readSettings()
331{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700332#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700333 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700334 QString nick = s.value("nick", "").toString();
335 QString chatroom = s.value("chatroom", "").toString();
336 QString prefix = s.value("prefix", "").toString();
337 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700338 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700339 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700340 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700341 else {
342 m_user.setNick(nick);
343 m_user.setChatroom(chatroom);
344 m_user.setPrefix(prefix);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700345 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700346 }
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700347#else
348 QTimer::singleShot(500, this, SLOT(buttonPressed()));
349 return false;
350#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700351}
352
353void
354ChatDialog::writeSettings()
355{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700356#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700357 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700358 s.setValue("nick", m_user.getNick());
359 s.setValue("chatroom", m_user.getChatroom());
360 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700361#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700362}
363
364void
365ChatDialog::updateLabels()
366{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700367 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700368 infoLabel->setText(settingDisp);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700369 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700370 prefixLabel->setText(prefixDisp);
371}
372
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700373void
374ChatDialog::returnPressed()
375{
376 QString text = lineEdit->text();
377 if (text.isEmpty())
378 return;
379
Zhenkai Zhub6338822012-05-31 13:27:24 -0700380 lineEdit->clear();
381
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700382 SyncDemo::ChatMessage msg;
383 formChatMessage(text, msg);
384
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700385 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700386
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700387 sendMsg(msg);
388
389 fitView();
390}
391
392void
393ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
394{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700395 // send msg
396 size_t size = msg.ByteSize();
397 char *buf = new char[size];
398 msg.SerializeToArray(buf, size);
399 if (!msg.IsInitialized())
400 {
401 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
402 abort();
403 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700404 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700405
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700406 delete buf;
407
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700408 m_lastMsgTime = time(NULL);
409
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700410 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700411 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700412 std::vector<Sync::MissingDataInfo> v;
413 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700414 {
415 boost::mutex::scoped_lock lock(m_sceneMutex);
416 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
417 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
418 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700419}
420
421void
422ChatDialog::sendHello()
423{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700424 time_t now = time(NULL);
425 int elapsed = now - m_lastMsgTime;
426 if (elapsed >= HELLO_INTERVAL)
427 {
428 SyncDemo::ChatMessage msg;
429 formHelloMessage(msg);
430 sendMsg(msg);
431 QTimer::singleShot(HELLO_INTERVAL * 1000, this, SLOT(sendHello()));
432 }
433 else
434 {
435 QTimer::singleShot((HELLO_INTERVAL - elapsed) * 1000, this, SLOT(sendHello()));
436 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700437}
438
439void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700440ChatDialog::buttonPressed()
441{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700442 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700443 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700444 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700445 QTimer::singleShot(100, this, SLOT(checkSetting()));
446}
447
448void
449ChatDialog::checkSetting()
450{
451 if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
452 {
453 buttonPressed();
454 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700455}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700456
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700457void
458ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
459{
460 bool needWrite = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700461 if (!nick.isEmpty() && nick != m_user.getNick()) {
462 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700463 needWrite = true;
464 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700465 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700466 m_user.setPrefix(prefix + "/" + getRandomString());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700467 needWrite = true;
468 // TODO: set the previous prefix as left?
469 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700470 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
471 m_user.setChatroom(chatroom);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700472 needWrite = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700473
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700474 {
475 boost::mutex::scoped_lock lock(m_sceneMutex);
476 m_scene->clearAll();
477 m_scene->plot("Empty");
478 }
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700479 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700480 if (m_sock != NULL)
481 {
482 delete m_sock;
483 m_sock = NULL;
484 }
485 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
486 syncPrefix += "/";
487 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700488 try
489 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700490 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
491 sendHello();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700492 }
493 catch (Sync::CcnxOperationException ex)
494 {
495 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
496 std::exit(1);
497 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700498
499 fitView();
500
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700501 }
502 if (needWrite) {
503 writeSettings();
504 updateLabels();
505 }
506}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700507
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700508void
509ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
510{
511 switch (reason)
512 {
513 case QSystemTrayIcon::Trigger:
514 case QSystemTrayIcon::DoubleClick:
515 break;
516 case QSystemTrayIcon::MiddleClick:
517 // showMessage();
518 break;
519 default:;
520 }
521}
522
523void
524ChatDialog::showMessage(QString from, QString data)
525{
526 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
527 if (!isActiveWindow())
528 {
529 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
530 trayIcon->setIcon(QIcon(":/images/note.png"));
531 }
532}
533
534void
535ChatDialog::messageClicked()
536{
537 this->showMaximized();
538}
539
540void
541ChatDialog::createActions()
542{
543 minimizeAction = new QAction(tr("Mi&nimize"), this);
544 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
545
546 maximizeAction = new QAction(tr("Ma&ximize"), this);
547 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
548
549 restoreAction = new QAction(tr("&Restore"), this);
550 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
551
552 quitAction = new QAction(tr("Quit"), this);
553 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
554}
555
556void
557ChatDialog::createTrayIcon()
558{
559 trayIconMenu = new QMenu(this);
560 trayIconMenu->addAction(minimizeAction);
561 trayIconMenu->addAction(maximizeAction);
562 trayIconMenu->addAction(restoreAction);
563 trayIconMenu->addSeparator();
564 trayIconMenu->addAction(quitAction);
565
566 trayIcon = new QSystemTrayIcon(this);
567 trayIcon->setContextMenu(trayIconMenu);
568
569 QIcon icon(":/images/icon_small.png");
570 trayIcon->setIcon(icon);
571 setWindowIcon(icon);
572 trayIcon->setToolTip("Sync-Demo System Tray Icon");
573 trayIcon->setVisible(true);
574}
575
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700576void
577ChatDialog::resizeEvent(QResizeEvent *e)
578{
579 fitView();
580}
581
582void
583ChatDialog::showEvent(QShowEvent *e)
584{
585 fitView();
586}
587
588void
589ChatDialog::fitView()
590{
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700591 boost::mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700592 QRectF rect = m_scene->itemsBoundingRect();
593 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700594 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700595}