blob: 464ac913001c7139d963f1cc07296200fe3318fd [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 Zhu6c4fc112012-10-07 17:07:43 -070011#include <boost/lexical_cast.hpp>
Zhenkai Zhu6bda0bd2012-10-06 23:50:39 -070012#include <stdio.h>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070013
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -070014#define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/chronos"
Zhenkai Zhu6bda0bd2012-10-06 23:50:39 -070015#define LOCAL_PREFIX_QUERY "/local/ndn/prefix"
16#define DEFAULT_LOCAL_PREFIX "/private/local"
Zhenkai Zhu756666a2012-10-07 00:24:35 -070017#define CCN_EXEC "/usr/local/bin/ccnpeek"
Zhenkai Zhu82a62752012-06-04 17:11:04 -070018
Zhenkai Zhuc61dbc22012-10-08 11:36:37 -070019static const int HELLO_INTERVAL = FRESHNESS * 3 / 4; // seconds
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070020
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070021ChatDialog::ChatDialog(QWidget *parent)
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -070022 : QDialog(parent), m_sock(NULL), m_lastMsgTime(0), m_historyInitialized(false), m_joined(false)
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070023{
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070024 // have to register this, otherwise
25 // the signal-slot system won't recognize this type
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070026 qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
27 qRegisterMetaType<size_t>("size_t");
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070028 setupUi(this);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070029 m_session = time(NULL);
Zhenkai Zhu716fe852012-10-08 18:27:55 -070030 m_scene = new DigestTreeScene(this);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070031
32 readSettings();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070033
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070034 updateLabels();
35
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070036 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070037
Zhenkai Zhu82a62752012-06-04 17:11:04 -070038 treeViewer->setScene(m_scene);
39 m_scene->plot("Empty");
40 QRectF rect = m_scene->itemsBoundingRect();
41 m_scene->setSceneRect(rect);
42
Zhenkai Zhu9036e032012-09-27 20:59:33 -070043 listView->setStyleSheet("QListView { alternate-background-color: white; background: #F0F0F0; color: darkGreen; font: bold large; }");
Zhenkai Zhuf55f4382012-09-28 10:58:54 -070044 listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
45 listView->setDragDropMode(QAbstractItemView::NoDragDrop);
46 listView->setSelectionMode(QAbstractItemView::NoSelection);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070047 m_rosterModel = new QStringListModel(this);
48 listView->setModel(m_rosterModel);
49
Zhenkai Zhu4b953d92012-10-08 13:09:40 -070050 refreshButton->setIcon(QIcon(QPixmap(":images/refresh.png")));
51
Zhenkai Zhu86df7412012-09-27 16:30:20 -070052 createActions();
53 createTrayIcon();
54 m_timer = new QTimer(this);
55 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
56 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhub60b7e12012-09-28 11:34:21 -070057 connect(treeButton, SIGNAL(pressed()), this, SLOT(treeButtonPressed()));
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -070058 connect(refreshButton, SIGNAL(pressed()), this, SLOT(updateLocalPrefix()));
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070059 connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), this, SLOT(processData(QString, const char *, size_t, bool, bool)));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070060 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070061 connect(m_timer, SIGNAL(timeout()), this, SLOT(replot()));
62 connect(m_scene, SIGNAL(replot()), this, SLOT(replot()));
63 connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(showNormal()));
64 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070065 connect(m_scene, SIGNAL(rosterChanged(QStringList)), this, SLOT(updateRosterList(QStringList)));
Zhenkai Zhu86df7412012-09-27 16:30:20 -070066
Zhenkai Zhu82a62752012-06-04 17:11:04 -070067 // create sync socket
68 if(!m_user.getChatroom().isEmpty()) {
69 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
70 syncPrefix += "/";
71 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070072 try
73 {
Alexander Afanasyev288edd82012-10-04 17:07:56 -070074 m_sock = new Sync::SyncAppSocket(syncPrefix,
75 bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
76 bind(&ChatDialog::processRemoveWrapper, this, _1));
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070077 Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
78 handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -070079 //QTimer::singleShot(100, this, SLOT(getLocalPrefix()));
80 if (!getLocalPrefix())
81 {
82 // if getLocalPrefix indicates no prefix change
83 // this sock is going to be used
84 QTimer::singleShot(600, this, SLOT(sendJoin()));
85 m_timer->start(FRESHNESS * 1000);
86 disableTreeDisplay();
87 QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
88 }
89 else
90 {
91 // this socket is going to be destroyed anyway
92 // why bother doing the following steps
Alexander Afanasyevebe118d2012-10-08 08:56:34 -070093
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -070094 // the same steps would be performed for another socket
95 // in settingUpdated
96 }
Zhenkai Zhua4fb1242012-06-05 20:26:05 -070097 }
98 catch (Sync::CcnxOperationException ex)
99 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700100 QMessageBox::critical(this, tr("Chronos"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700101 std::exit(1);
102 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700103 }
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700104
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700105}
106
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700107ChatDialog::~ChatDialog()
108{
109 if (m_sock != NULL)
110 {
Zhenkai Zhucf024442012-10-05 10:33:08 -0700111 sendLeave();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700112 delete m_sock;
113 m_sock = NULL;
114 }
115}
116
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700117void
Zhenkai Zhucf024442012-10-05 10:33:08 -0700118ChatDialog::sendLeave()
119{
120 SyncDemo::ChatMessage msg;
121 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
122 sendMsg(msg);
123 usleep(500000);
124 m_sock->remove(m_user.getPrefix().toStdString());
125 usleep(5000);
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700126 m_joined = false;
Zhenkai Zhucf024442012-10-05 10:33:08 -0700127#ifdef __DEBUG
128 std::cout << "Sync REMOVE signal sent" << std::endl;
129#endif
130}
131
132void
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700133ChatDialog::replot()
134{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700135 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700136 m_scene->plot(m_sock->getRootDigest().c_str());
Zhenkai Zhuc9e4e3c2012-10-02 11:47:31 -0700137 fitView();
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700138}
139
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700140void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700141ChatDialog::updateRosterList(QStringList staleUserList)
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700142{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700143 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700144 QStringList rosterList = m_scene->getRosterList();
145 m_rosterModel->setStringList(rosterList);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700146 QString user;
147 QStringListIterator it(staleUserList);
148 while(it.hasNext())
149 {
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700150 std::string nick = it.next().toStdString();
151 if (nick.empty())
152 continue;
153
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700154 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700155 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
Zhenkai Zhu32cbdce2012-10-02 11:09:38 -0700156 msg.set_from(nick);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700157 appendMessage(msg);
158 }
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700159}
160
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700161void
162ChatDialog::setVisible(bool visible)
163{
164 minimizeAction->setEnabled(visible);
165 maximizeAction->setEnabled(!isMaximized());
166 restoreAction->setEnabled(isMaximized() || !visible);
167 QDialog::setVisible(visible);
168}
169
170void
171ChatDialog::closeEvent(QCloseEvent *e)
172{
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700173 if (trayIcon->isVisible() && !m_minimaniho)
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700174 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700175 QMessageBox::information(this, tr("Chronos"),
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700176 tr("The program will keep running in the "
177 "system tray. To terminate the program"
178 "choose <b>Quit</b> in the context memu"
179 "of the system tray entry."));
180 hide();
181 e->ignore();
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700182 m_minimaniho = true;
183 writeSettings();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700184 }
185}
186
187void
188ChatDialog::changeEvent(QEvent *e)
189{
190 switch(e->type())
191 {
192 case QEvent::ActivationChange:
193 if (isActiveWindow())
194 {
195 trayIcon->setIcon(QIcon(":/images/icon_small.png"));
196 }
197 break;
198 default:
199 break;
200 }
201}
202
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700203void
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700204ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700205{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700206 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700207
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700208 if (msg.type() == SyncDemo::ChatMessage::CHAT)
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700209 {
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700210
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700211 if (!msg.has_data())
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700212 {
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700213 return;
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700214 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700215
216 if (msg.from().empty() || msg.data().empty())
217 {
218 return;
219 }
220
221 if (!msg.has_timestamp())
222 {
223 return;
224 }
225
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700226 if (m_history.size() == MAX_HISTORY_ENTRY)
227 {
228 m_history.dequeue();
229 }
230
231 m_history.enqueue(msg);
232
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700233 QTextCharFormat nickFormat;
234 nickFormat.setForeground(Qt::darkGreen);
235 nickFormat.setFontWeight(QFont::Bold);
236 nickFormat.setFontUnderline(true);
237 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700238
239 QTextCursor cursor(textEdit->textCursor());
240 cursor.movePosition(QTextCursor::End);
241 QTextTableFormat tableFormat;
242 tableFormat.setBorder(0);
243 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
244 QString from = QString("%1 ").arg(msg.from().c_str());
245 QTextTableCell fromCell = table->cellAt(0, 0);
246 fromCell.setFormat(nickFormat);
247 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700248
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700249 time_t timestamp = msg.timestamp();
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700250 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700251
252 QTextCursor nextCursor(textEdit->textCursor());
253 nextCursor.movePosition(QTextCursor::End);
254 table = nextCursor.insertTable(1, 1, tableFormat);
Zhenkai Zhu767eac72012-10-08 11:20:57 -0700255 table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str()));
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700256 if (!isHistory)
257 {
Zhenkai Zhu767eac72012-10-08 11:20:57 -0700258 showMessage(from, QString::fromUtf8(msg.data().c_str()));
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700259 }
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700260 }
261
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700262 if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
263 {
264 QTextCharFormat nickFormat;
265 nickFormat.setForeground(Qt::gray);
266 nickFormat.setFontWeight(QFont::Bold);
267 nickFormat.setFontUnderline(true);
268 nickFormat.setUnderlineColor(Qt::gray);
Zhenkai Zhub98e6022012-09-27 13:48:23 -0700269
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700270 QTextCursor cursor(textEdit->textCursor());
271 cursor.movePosition(QTextCursor::End);
272 QTextTableFormat tableFormat;
273 tableFormat.setBorder(0);
274 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
275 QString action;
276 if (msg.type() == SyncDemo::ChatMessage::JOIN)
277 {
278 action = "enters room";
279 }
280 else
281 {
282 action = "leaves room";
283 }
284
285 QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action);
286 QTextTableCell fromCell = table->cellAt(0, 0);
287 fromCell.setFormat(nickFormat);
288 fromCell.firstCursorPosition().insertText(from);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700289
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700290 time_t timestamp = msg.timestamp();
291 printTimeInCell(table, timestamp);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700292 }
293
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700294 QScrollBar *bar = textEdit->verticalScrollBar();
295 bar->setValue(bar->maximum());
296}
297
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -0700298void
299ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
300{
301 QTextCharFormat timeFormat;
302 timeFormat.setForeground(Qt::gray);
303 timeFormat.setFontUnderline(true);
304 timeFormat.setUnderlineColor(Qt::gray);
305 QTextTableCell timeCell = table->cellAt(0, 1);
306 timeCell.setFormat(timeFormat);
307 timeCell.firstCursorPosition().insertText(formatTime(timestamp));
308}
309
310QString
311ChatDialog::formatTime(time_t timestamp)
312{
313 struct tm *tm_time = localtime(&timestamp);
314 int hour = tm_time->tm_hour;
315 QString amOrPM;
316 if (hour > 12)
317 {
318 hour -= 12;
319 amOrPM = "PM";
320 }
321 else
322 {
323 amOrPM = "AM";
324 if (hour == 0)
325 {
326 hour = 12;
327 }
328 }
329
330 char textTime[12];
331 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
332 return QString(textTime);
333}
334
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700335void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700336ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
337{
338 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700339#ifdef __DEBUG
340 std::cout << "<<< Tree update signal emitted" << std::endl;
341#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700342}
343
344void
345ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700346{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700347#ifdef __DEBUG
348 std::cout << "<<< processing Tree Update" << std::endl;
349#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700350 if (v.empty())
351 {
352 return;
353 }
354
355 // reflect the changes on digest tree
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700356 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700357 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700358 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
359 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700360
361 int n = v.size();
362 int totalMissingPackets = 0;
363 for (int i = 0; i < n; i++)
364 {
365 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
366 }
367
Zhenkai Zhubb198112012-09-27 11:31:42 -0700368 for (int i = 0; i < n; i++)
369 {
370 if (totalMissingPackets < 4)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700371 {
372 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
373 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700374 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700375#ifdef __DEBUG
376 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
377#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700378 }
379 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700380 else
381 {
382 m_sock->fetchRaw(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1, _2, _3), 2);
383 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700384 }
385
386 // adjust the view
387 fitView();
388
389}
390
391void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700392ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
393{
Zhenkai Zhuae29f5a2012-10-05 17:28:04 -0700394 char *tempBuf = new char[len];
395 memcpy(tempBuf, buf, len);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700396 emit dataReceived(name.c_str(), tempBuf, len, true, false);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700397#ifdef __DEBUG
398 std::cout <<"<<< " << name << " fetched" << std::endl;
399#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700400}
401
402void
Zhenkai Zhubb198112012-09-27 11:31:42 -0700403ChatDialog::processDataNoShowWrapper(std::string name, const char *buf, size_t len)
404{
Zhenkai Zhuae29f5a2012-10-05 17:28:04 -0700405 char *tempBuf = new char[len];
406 memcpy(tempBuf, buf, len);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700407 emit dataReceived(name.c_str(), tempBuf, len, false, false);
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700408
409 if (!m_historyInitialized)
410 {
411 fetchHistory(name);
412 m_historyInitialized = true;
413 }
414}
415
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700416void
417ChatDialog::processDataHistoryWrapper(std::string name, const char *buf, size_t len)
418{
419 char *tempBuf = new char[len];
420 memcpy(tempBuf, buf, len);
421 emit dataReceived(name.c_str(), tempBuf, len, true, true);
422}
423
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700424void
425ChatDialog::fetchHistory(std::string name)
426{
427 std::string nameWithoutSeq = name.substr(0, name.find_last_of('/'));
428 std::string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/'));
429 prefix += "/history";
430 Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
431 QString randomString = getRandomString();
432 for (int i = 0; i < MAX_HISTORY_ENTRY; i++)
433 {
434 QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700435 handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3));
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700436 }
437}
438
439void
440ChatDialog::respondHistoryRequest(std::string interest)
441{
442 std::string seqStr = interest.substr(interest.find_last_of('/') + 1);
443 int seq = boost::lexical_cast<int>(seqStr);
444 if (seq >= 0 && seq < m_history.size())
445 {
446 Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
447 SyncDemo::ChatMessage msg = m_history.at(seq);
448 size_t size = msg.ByteSize();
449 char *buf = new char[size];
450 msg.SerializeToArray(buf, size);
451 handle->publishRawData(interest, buf, size, 1);
452 delete buf;
453 }
Zhenkai Zhubb198112012-09-27 11:31:42 -0700454}
455
456void
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700457ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700458{
459 SyncDemo::ChatMessage msg;
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700460 bool corrupted = false;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700461 if (!msg.ParseFromArray(buf, len))
462 {
Zhenkai Zhubb198112012-09-27 11:31:42 -0700463 std::cerr << "Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?" << std::endl;
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700464 // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
465 msg.set_from("inconnu");
466 msg.set_type(SyncDemo::ChatMessage::OTHER);
467 corrupted = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700468 }
469
Zhenkai Zhuae29f5a2012-10-05 17:28:04 -0700470 delete [] buf;
471 buf = NULL;
472
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700473 // display msg received from network
474 // we have to do so; this function is called by ccnd thread
475 // so if we call appendMsg directly
476 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
477 // the "cannonical" way to is use signal-slot
Zhenkai Zhuef8d0632012-10-05 10:24:55 -0700478 if (show && !corrupted)
Zhenkai Zhubb198112012-09-27 11:31:42 -0700479 {
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700480 appendMessage(msg, isHistory);
Zhenkai Zhubb198112012-09-27 11:31:42 -0700481 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700482
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700483 if (!isHistory)
484 {
485 // update the tree view
486 std::string stdStrName = name.toStdString();
487 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
488 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700489#ifdef __DEBUG
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700490 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700491#endif
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700492 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
493 {
494 processRemove(prefix.c_str());
495 }
496 else
497 {
498 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
499 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
500 }
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700501 }
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700502 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700503}
504
505void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700506ChatDialog::processRemoveWrapper(std::string prefix)
507{
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700508#ifdef __DEBUG
509 std::cout << "Sync REMOVE signal received for prefix: " << prefix << std::endl;
510#endif
511 //emit removeReceived(prefix.c_str());
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700512}
513
514void
515ChatDialog::processRemove(QString prefix)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700516{
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700517#ifdef __DEBUG
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700518 std::cout << "<<< remove node for prefix" << prefix.toStdString() << std::endl;
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700519#endif
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700520 bool removed = m_scene->removeNode(prefix);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700521 if (removed)
522 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700523 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu591e8c32012-09-26 11:57:50 -0700524 m_scene->plot(m_sock->getRootDigest().c_str());
525 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700526}
527
528void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700529ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700530 msg.set_from(m_user.getNick().toStdString());
531 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu767eac72012-10-08 11:20:57 -0700532 msg.set_data(text.toUtf8().constData());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700533 time_t seconds = time(NULL);
534 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700535 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700536}
537
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700538void
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700539ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700540{
541 msg.set_from(m_user.getNick().toStdString());
542 msg.set_to(m_user.getChatroom().toStdString());
543 time_t seconds = time(NULL);
544 msg.set_timestamp(seconds);
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700545 msg.set_type(type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700546}
547
Zhenkai Zhu59245aa2012-09-26 16:07:04 -0700548static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
549
550QString
551ChatDialog::getRandomString()
552{
553 std::string randStr;
554 boost::random::random_device rng;
555 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
556 for (int i = 0; i < 10; i ++)
557 {
558 randStr += chars[index_dist(rng)];
559 }
560 return randStr.c_str();
561}
562
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700563bool
Zhenkai Zhu6bda0bd2012-10-06 23:50:39 -0700564ChatDialog::getLocalPrefix()
565{
Zhenkai Zhu86581412012-10-08 16:58:39 -0700566// /*
567// * this method tries to use ccncat
568// * however, it does not work in Mac OS X app bundle
569// * it works well in command line though
570// */
571
572// std::string cmd = CCN_EXEC;
573// cmd += " -c -v ";
574// cmd += LOCAL_PREFIX_QUERY;
575// QString localPrefix;
576// #define MAX_PREFIX_LEN 100
577// FILE *fp = popen(cmd.c_str(), "r");
578// if (fp != NULL)
579// {
580// char prefix[MAX_PREFIX_LEN];
581// if (fgets(prefix, MAX_PREFIX_LEN, fp) != NULL)
582// {
583// localPrefix = prefix;
584// localPrefix.remove('\n');
585// }
586// else
587// {
588// localPrefix = DEFAULT_LOCAL_PREFIX;
589// }
590// pclose(fp);
591// }
592// else
593// {
594// localPrefix = DEFAULT_LOCAL_PREFIX;
595// }
596// return localPrefix;
Alexander Afanasyevebe118d2012-10-08 08:56:34 -0700597 std::cerr << "trying to get local prefix" << std::endl;
598
Zhenkai Zhu86581412012-10-08 16:58:39 -0700599 if (m_sock != NULL)
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700600 {
601 QString originPrefix = QString::fromStdString (m_sock->getLocalPrefix()).trimmed ();
602 std::cerr << "got: " << originPrefix.toStdString () << std::endl;
Zhenkai Zhuba707342012-10-08 16:20:15 -0700603
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700604 if (originPrefix != "" && m_user.getOriginPrefix () != originPrefix)
Zhenkai Zhu86581412012-10-08 16:58:39 -0700605 {
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700606 emit settingUpdated(m_user.getNick (), m_user.getChatroom (), originPrefix);
607 // prefix updated
608 return true;
Zhenkai Zhu86581412012-10-08 16:58:39 -0700609 }
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700610 }
611
612 // prefix not changed
613 return false;
614}
615
616void
617ChatDialog::updateLocalPrefix()
618{
619 getLocalPrefix();
Zhenkai Zhu6bda0bd2012-10-06 23:50:39 -0700620}
621
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700622bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700623ChatDialog::readSettings()
624{
625 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700626 QString nick = s.value("nick", "").toString();
627 QString chatroom = s.value("chatroom", "").toString();
Zhenkai Zhu86581412012-10-08 16:58:39 -0700628 // QString originPrefix = s.value("originPrefix", "").toString();
Alexander Afanasyev5ddc68b2012-10-08 00:21:40 -0700629
Zhenkai Zhu86581412012-10-08 16:58:39 -0700630 // Sync::CcnxWrapperPtr wrapper = Sync::CcnxWrapper::Create ();
631 // QString originPrefix = QString::fromStdString (wrapper->getLocalPrefix());
632 // Sync::CcnxWrapper::Destroy ();
Alexander Afanasyev5ddc68b2012-10-08 00:21:40 -0700633
Zhenkai Zhu86581412012-10-08 16:58:39 -0700634 QString originPrefix = DEFAULT_LOCAL_PREFIX;
Alexander Afanasyev5ddc68b2012-10-08 00:21:40 -0700635
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700636 m_minimaniho = s.value("minimaniho", false).toBool();
637 if (nick == "" || chatroom == "" || originPrefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700638 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700639 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700640 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700641 else {
642 m_user.setNick(nick);
643 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700644 m_user.setOriginPrefix(originPrefix);
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700645 m_user.setPrefix(originPrefix + "/" + chatroom + "/" + getRandomString());
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700646 m_scene->setCurrentPrefix(originPrefix + "/" + chatroom + "/" + getRandomString());
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700647 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700648 }
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700649
650// QTimer::singleShot(500, this, SLOT(buttonPressed()));
651 // return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700652}
653
654void
655ChatDialog::writeSettings()
656{
657 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700658 s.setValue("nick", m_user.getNick());
659 s.setValue("chatroom", m_user.getChatroom());
Zhenkai Zhu6bda0bd2012-10-06 23:50:39 -0700660 //s.setValue("originPrefix", m_user.getOriginPrefix());
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700661 s.setValue("minimaniho", m_minimaniho);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700662}
663
664void
665ChatDialog::updateLabels()
666{
Zhenkai Zhu76ff02b2012-09-27 21:11:03 -0700667 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
668 infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700669 infoLabel->setText(settingDisp);
Zhenkai Zhu6bda0bd2012-10-06 23:50:39 -0700670 QString prefixDisp;
671 if (m_user.getPrefix().startsWith(DEFAULT_LOCAL_PREFIX))
672 {
673 prefixDisp = QString("<Warning: Auto config prefix failed.>\n <Prefix = %1>").arg(m_user.getPrefix());
674 prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}");
675 }
676 else
677 {
678 prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix());
679 prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}");
680 }
681 prefixLabel->setText(prefixDisp);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700682}
683
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700684void
685ChatDialog::returnPressed()
686{
687 QString text = lineEdit->text();
688 if (text.isEmpty())
689 return;
690
Zhenkai Zhub6338822012-05-31 13:27:24 -0700691 lineEdit->clear();
692
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700693 SyncDemo::ChatMessage msg;
694 formChatMessage(text, msg);
695
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700696 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700697
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700698 sendMsg(msg);
699
700 fitView();
701}
702
703void
704ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
705{
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700706 // send msg
707 size_t size = msg.ByteSize();
708 char *buf = new char[size];
709 msg.SerializeToArray(buf, size);
710 if (!msg.IsInitialized())
711 {
712 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
713 abort();
714 }
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700715 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700716
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700717 delete buf;
718
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700719 m_lastMsgTime = time(NULL);
720
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700721 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700722 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700723 std::vector<Sync::MissingDataInfo> v;
724 v.push_back(mdi);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700725 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700726 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700727 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
728 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
729 }
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700730}
731
732void
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700733ChatDialog::sendJoin()
734{
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700735 m_joined = true;
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700736 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700737 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700738 sendMsg(msg);
Zhenkai Zhuc61dbc22012-10-08 11:36:37 -0700739 boost::random::random_device rng;
740 boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
741 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700742 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
743}
744
745void
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700746ChatDialog::sendHello()
747{
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700748 time_t now = time(NULL);
749 int elapsed = now - m_lastMsgTime;
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700750 if (elapsed >= m_randomizedInterval / 1000)
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700751 {
752 SyncDemo::ChatMessage msg;
Zhenkai Zhub5b78462012-09-28 14:10:37 -0700753 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700754 sendMsg(msg);
Zhenkai Zhuc61dbc22012-10-08 11:36:37 -0700755 boost::random::random_device rng;
756 boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
757 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700758 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700759 }
760 else
761 {
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700762 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700763 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700764}
765
766void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700767ChatDialog::buttonPressed()
768{
Alexander Afanasyev5ddc68b2012-10-08 00:21:40 -0700769 Sync::SyncLogic &logic = m_sock->getLogic ();
770 logic.printState ();
771
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700772 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getOriginPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700773 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700774 dialog.exec();
Zhenkai Zhue837f792012-06-05 20:47:54 -0700775 QTimer::singleShot(100, this, SLOT(checkSetting()));
776}
777
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700778void ChatDialog::treeButtonPressed()
779{
780 if (treeViewer->isVisible())
781 {
782 treeViewer->hide();
783 treeButton->setText("Show Sync Tree");
784 }
785 else
786 {
787 treeViewer->show();
788 treeButton->setText("Hide Sync Tree");
789 }
Zhenkai Zhu25e33e52012-09-28 13:00:07 -0700790
791 fitView();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -0700792}
793
Zhenkai Zhu0e7a9b22012-10-05 17:55:17 -0700794void ChatDialog::enableTreeDisplay()
795{
796 treeButton->setEnabled(true);
797 treeViewer->show();
798 fitView();
799}
800
801void ChatDialog::disableTreeDisplay()
802{
803 treeButton->setEnabled(false);
804 treeViewer->hide();
805 fitView();
806}
807
Zhenkai Zhue837f792012-06-05 20:47:54 -0700808void
809ChatDialog::checkSetting()
810{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700811 if (m_user.getOriginPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty())
Zhenkai Zhue837f792012-06-05 20:47:54 -0700812 {
813 buttonPressed();
814 }
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700815}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700816
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700817void
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700818ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700819{
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700820 QString randString = getRandomString();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700821 bool needWrite = false;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700822 bool needFresh = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700823 if (!nick.isEmpty() && nick != m_user.getNick()) {
824 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700825 needWrite = true;
826 }
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700827 QString oldPrefix = m_user.getPrefix();
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700828 if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) {
829 m_user.setOriginPrefix(originPrefix);
830 m_user.setPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString);
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700831 m_scene->setCurrentPrefix(originPrefix + "/" + m_user.getChatroom() + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700832 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700833 needFresh = true;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700834 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700835 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
836 m_user.setChatroom(chatroom);
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700837 m_user.setPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString);
Zhenkai Zhu716fe852012-10-08 18:27:55 -0700838 m_scene->setCurrentPrefix(m_user.getOriginPrefix() + "/" + chatroom + "/" + randString);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700839 needWrite = true;
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700840 needFresh = true;
841 }
842
843 if (needFresh)
844 {
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700845
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700846 {
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700847 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu44b43752012-06-05 21:18:37 -0700848 m_scene->clearAll();
849 m_scene->plot("Empty");
850 }
Zhenkai Zhucc4c2c02012-09-27 21:24:37 -0700851
852 textEdit->clear();
853
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700854 if (m_sock != NULL)
855 {
Zhenkai Zhu77fc8f22012-10-08 15:12:56 -0700856 // keep the new prefix
857 QString newPrefix = m_user.getPrefix();
858 // send leave for the old
859 m_user.setPrefix(oldPrefix);
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700860 // there is no point to send leave if we haven't joined yet
861 if (m_joined)
862 {
863 sendLeave();
864 }
Zhenkai Zhu77fc8f22012-10-08 15:12:56 -0700865 // resume new prefix
866 m_user.setPrefix(newPrefix);
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700867 Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
868 handle->clearInterestFilter(oldPrefix.toStdString());
869 m_history.clear();
870 m_historyInitialized = false;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700871 delete m_sock;
872 m_sock = NULL;
873 }
874 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
875 syncPrefix += "/";
876 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700877 try
878 {
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700879 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700880 Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
881 handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700882 QTimer::singleShot(600, this, SLOT(sendJoin()));
Zhenkai Zhu78799ea2012-10-08 11:51:56 -0700883 m_timer->start(FRESHNESS * 1000);
Zhenkai Zhu0e7a9b22012-10-05 17:55:17 -0700884 disableTreeDisplay();
885 QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700886 }
887 catch (Sync::CcnxOperationException ex)
888 {
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700889 QMessageBox::critical(this, tr("Chronos"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700890 std::exit(1);
891 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700892
893 fitView();
894
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700895 }
Zhenkai Zhue95c64a2012-09-27 21:46:44 -0700896
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700897 if (needWrite) {
898 writeSettings();
899 updateLabels();
900 }
901}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700902
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700903void
904ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
905{
906 switch (reason)
907 {
908 case QSystemTrayIcon::Trigger:
909 case QSystemTrayIcon::DoubleClick:
910 break;
911 case QSystemTrayIcon::MiddleClick:
912 // showMessage();
913 break;
914 default:;
915 }
916}
917
918void
919ChatDialog::showMessage(QString from, QString data)
920{
921 // std::cout <<"Showing Message: " << from.toStdString() << ": " << data.toStdString() << std::endl;
922 if (!isActiveWindow())
923 {
924 trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
925 trayIcon->setIcon(QIcon(":/images/note.png"));
926 }
927}
928
929void
930ChatDialog::messageClicked()
931{
932 this->showMaximized();
933}
934
935void
936ChatDialog::createActions()
937{
938 minimizeAction = new QAction(tr("Mi&nimize"), this);
939 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
940
941 maximizeAction = new QAction(tr("Ma&ximize"), this);
942 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
943
944 restoreAction = new QAction(tr("&Restore"), this);
945 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
946
947 quitAction = new QAction(tr("Quit"), this);
948 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
949}
950
951void
952ChatDialog::createTrayIcon()
953{
954 trayIconMenu = new QMenu(this);
955 trayIconMenu->addAction(minimizeAction);
956 trayIconMenu->addAction(maximizeAction);
957 trayIconMenu->addAction(restoreAction);
958 trayIconMenu->addSeparator();
959 trayIconMenu->addAction(quitAction);
960
961 trayIcon = new QSystemTrayIcon(this);
962 trayIcon->setContextMenu(trayIconMenu);
963
964 QIcon icon(":/images/icon_small.png");
965 trayIcon->setIcon(icon);
966 setWindowIcon(icon);
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -0700967 trayIcon->setToolTip("Chronos System Tray Icon");
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700968 trayIcon->setVisible(true);
969}
970
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700971void
972ChatDialog::resizeEvent(QResizeEvent *e)
973{
974 fitView();
975}
976
977void
978ChatDialog::showEvent(QShowEvent *e)
979{
980 fitView();
981}
982
983void
984ChatDialog::fitView()
985{
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700986 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700987 QRectF rect = m_scene->itemsBoundingRect();
988 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700989 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700990}