blob: 19ea99a880fec2c86a36684644c49bbe67750649 [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 Zhu64f9ede2012-06-05 11:32:00 -070082 connect(this, SIGNAL(dataReceived(QString, const char *, size_t)), this, SLOT(processData(QString, const char *, size_t)));
83 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
204 if (totalMissingPackets < 10) {
205 for (int i = 0; i < n; i++)
206 {
207 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
208 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700209 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700210#ifdef __DEBUG
211 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
212#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700213 }
214 }
215 }
216 else
217 {
218 // too bad; too many missing packets
219 // we may just join a new chatroom
220 // or some network patition just healed
221 // we don't try to fetch any data in this case (for now)
222 }
223
224 // adjust the view
225 fitView();
226
227}
228
229void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700230ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
231{
232 emit dataReceived(name.c_str(), buf, len);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700233#ifdef __DEBUG
234 std::cout <<"<<< " << name << " fetched" << std::endl;
235#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700236}
237
238void
239ChatDialog::processData(QString name, const char *buf, size_t len)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700240{
241 SyncDemo::ChatMessage msg;
242 if (!msg.ParseFromArray(buf, len))
243 {
244 std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
245 abort();
246 }
247
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700248 // display msg received from network
249 // we have to do so; this function is called by ccnd thread
250 // so if we call appendMsg directly
251 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
252 // the "cannonical" way to is use signal-slot
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700253 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700254
255 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700256 std::string stdStrName = name.toStdString();
257 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
258 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
259#ifdef __DEBUG
260 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
261#endif
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700262 {
263 boost::mutex::scoped_lock lock(m_sceneMutex);
264 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
265 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700266 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700267}
268
269void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700270ChatDialog::processRemoveWrapper(std::string prefix)
271{
272 emit removeReceived(prefix.c_str());
273}
274
275void
276ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700277{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700278#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700279 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700280#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700281 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700282 if (removed)
283 {
284 m_scene->plot(m_sock->getRootDigest().c_str());
285 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700286}
287
288void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700289ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700290 msg.set_from(m_user.getNick().toStdString());
291 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700292 msg.set_data(text.toStdString());
293 time_t seconds = time(NULL);
294 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700295 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700296}
297
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700298void
299ChatDialog::formHelloMessage(SyncDemo::ChatMessage &msg)
300{
301 msg.set_from(m_user.getNick().toStdString());
302 msg.set_to(m_user.getChatroom().toStdString());
303 time_t seconds = time(NULL);
304 msg.set_timestamp(seconds);
305 msg.set_type(SyncDemo::ChatMessage::HELLO);
306}
307
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700308static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
309
310QString
311ChatDialog::getRandomString()
312{
313 std::string randStr;
314 boost::random::random_device rng;
315 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
316 for (int i = 0; i < 10; i ++)
317 {
318 randStr += chars[index_dist(rng)];
319 }
320 return randStr.c_str();
321}
322
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700323bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700324ChatDialog::readSettings()
325{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700326#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700327 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700328 QString nick = s.value("nick", "").toString();
329 QString chatroom = s.value("chatroom", "").toString();
330 QString prefix = s.value("prefix", "").toString();
331 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700332 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700333 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700334 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700335 else {
336 m_user.setNick(nick);
337 m_user.setChatroom(chatroom);
338 m_user.setPrefix(prefix);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700339 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700340 }
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700341#else
342 QTimer::singleShot(500, this, SLOT(buttonPressed()));
343 return false;
344#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700345}
346
347void
348ChatDialog::writeSettings()
349{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700350#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700351 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700352 s.setValue("nick", m_user.getNick());
353 s.setValue("chatroom", m_user.getChatroom());
354 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700355#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700356}
357
358void
359ChatDialog::updateLabels()
360{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700361 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700362 infoLabel->setText(settingDisp);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700363 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700364 prefixLabel->setText(prefixDisp);
365}
366
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700367void
368ChatDialog::returnPressed()
369{
370 QString text = lineEdit->text();
371 if (text.isEmpty())
372 return;
373
Zhenkai Zhub6338822012-05-31 13:27:24 -0700374 lineEdit->clear();
375
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700376 SyncDemo::ChatMessage msg;
377 formChatMessage(text, msg);
378
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700379 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700380
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700381 sendMsg(msg);
382
383 fitView();
384}
385
386void
387ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
388{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700389 // send msg
390 size_t size = msg.ByteSize();
391 char *buf = new char[size];
392 msg.SerializeToArray(buf, size);
393 if (!msg.IsInitialized())
394 {
395 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
396 abort();
397 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700398 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700399
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700400 delete buf;
401
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700402 m_lastMsgTime = time(NULL);
403
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700404 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700405 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700406 std::vector<Sync::MissingDataInfo> v;
407 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700408 {
409 boost::mutex::scoped_lock lock(m_sceneMutex);
410 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
411 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
412 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700413}
414
415void
416ChatDialog::sendHello()
417{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700418 time_t now = time(NULL);
419 int elapsed = now - m_lastMsgTime;
420 if (elapsed >= HELLO_INTERVAL)
421 {
422 SyncDemo::ChatMessage msg;
423 formHelloMessage(msg);
424 sendMsg(msg);
425 QTimer::singleShot(HELLO_INTERVAL * 1000, this, SLOT(sendHello()));
426 }
427 else
428 {
429 QTimer::singleShot((HELLO_INTERVAL - elapsed) * 1000, this, SLOT(sendHello()));
430 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700431}
432
433void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700434ChatDialog::buttonPressed()
435{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700436 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700437 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700438 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700439 QTimer::singleShot(100, this, SLOT(checkSetting()));
440}
441
442void
443ChatDialog::checkSetting()
444{
445 if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
446 {
447 buttonPressed();
448 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700449}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700450
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700451void
452ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
453{
454 bool needWrite = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700455 if (!nick.isEmpty() && nick != m_user.getNick()) {
456 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700457 needWrite = true;
458 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700459 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700460 m_user.setPrefix(prefix + "/" + getRandomString());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700461 needWrite = true;
462 // TODO: set the previous prefix as left?
463 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700464 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
465 m_user.setChatroom(chatroom);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700466 needWrite = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700467
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700468 {
469 boost::mutex::scoped_lock lock(m_sceneMutex);
470 m_scene->clearAll();
471 m_scene->plot("Empty");
472 }
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700473 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700474 if (m_sock != NULL)
475 {
476 delete m_sock;
477 m_sock = NULL;
478 }
479 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
480 syncPrefix += "/";
481 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700482 try
483 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700484 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
485 sendHello();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700486 }
487 catch (Sync::CcnxOperationException ex)
488 {
489 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
490 std::exit(1);
491 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700492
493 fitView();
494
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700495 }
496 if (needWrite) {
497 writeSettings();
498 updateLabels();
499 }
500}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700501
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700502void
503ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
504{
505 switch (reason)
506 {
507 case QSystemTrayIcon::Trigger:
508 case QSystemTrayIcon::DoubleClick:
509 break;
510 case QSystemTrayIcon::MiddleClick:
511 // showMessage();
512 break;
513 default:;
514 }
515}
516
517void
518ChatDialog::showMessage(QString from, QString data)
519{
520 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
521 if (!isActiveWindow())
522 {
523 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
524 trayIcon->setIcon(QIcon(":/images/note.png"));
525 }
526}
527
528void
529ChatDialog::messageClicked()
530{
531 this->showMaximized();
532}
533
534void
535ChatDialog::createActions()
536{
537 minimizeAction = new QAction(tr("Mi&nimize"), this);
538 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
539
540 maximizeAction = new QAction(tr("Ma&ximize"), this);
541 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
542
543 restoreAction = new QAction(tr("&Restore"), this);
544 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
545
546 quitAction = new QAction(tr("Quit"), this);
547 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
548}
549
550void
551ChatDialog::createTrayIcon()
552{
553 trayIconMenu = new QMenu(this);
554 trayIconMenu->addAction(minimizeAction);
555 trayIconMenu->addAction(maximizeAction);
556 trayIconMenu->addAction(restoreAction);
557 trayIconMenu->addSeparator();
558 trayIconMenu->addAction(quitAction);
559
560 trayIcon = new QSystemTrayIcon(this);
561 trayIcon->setContextMenu(trayIconMenu);
562
563 QIcon icon(":/images/icon_small.png");
564 trayIcon->setIcon(icon);
565 setWindowIcon(icon);
566 trayIcon->setToolTip("Sync-Demo System Tray Icon");
567 trayIcon->setVisible(true);
568}
569
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700570void
571ChatDialog::resizeEvent(QResizeEvent *e)
572{
573 fitView();
574}
575
576void
577ChatDialog::showEvent(QShowEvent *e)
578{
579 fitView();
580}
581
582void
583ChatDialog::fitView()
584{
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700585 boost::mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700586 QRectF rect = m_scene->itemsBoundingRect();
587 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700588 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700589}