blob: f2d11c63cccdef320ef5c532464be28b61397168 [file] [log] [blame]
Ilya Moiseenko18c8a132013-10-24 01:52:52 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * @copyright See LICENCE for copyright and license information.
4 *
5 * @author Ilya Moiseenko <iliamo@ucla.edu>
6 */
7
8#include "tray-menu.h"
9#include "ui_traymenu.h" //generated from traymenu.ui
10#include "quit-dialog.h"
11#include "fib-input-dialog.h"
Ilya Moiseenko656d52d2013-10-25 14:19:10 -070012#include "network-manager.h"
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070013
14#include <QMenu>
15#include <QCloseEvent>
16#include <QDesktopServices>
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070017#include <QUrl>
18#include <QCheckBox>
19#include <QLabel>
20#include <QHBoxLayout>
21#include <QLineEdit>
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070022#include <QDir>
Ilya Moiseenko69abb972013-11-04 16:02:20 -080023#include <QScrollBar>
24
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070025
26TrayMenu::TrayMenu(QWidget *parent) :
27 QMainWindow(parent),
28 ui(new Ui::TrayMenu)
29{
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070030 networkManager = new NetworkManager();
31
32 QCoreApplication::setOrganizationName("UCLA");
33 QCoreApplication::setOrganizationDomain("named-data.net");
34 QCoreApplication::setApplicationName("NDNx Control Center");
35
36 persistentSettings = new QSettings();
37
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070038 ui->setupUi(this);
39
40 createTrayIcon();
41 setIcon(true);
42 createToolbar();
43 createTableView();
44
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070045 loadSettings();
Ilya Moiseenko656d52d2013-10-25 14:19:10 -070046
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070047 connect(ui->openTrafficMapButton, SIGNAL(pressed()), this, SLOT(openTrafficMap()));
48 connect(ui->openRoutingStatusButton, SIGNAL(pressed()), this, SLOT(openRoutingStatus()));
49 connect(ui->addFibButton, SIGNAL(pressed()), this, SLOT(showFibInputDialog()));
50 connect(ui->deleteFibButton, SIGNAL(released()), this, SLOT(deleteFibEntry()));
51 connect(ui->softwareUpdateCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeSoftwareUpdate()));
52 connect(ui->hubDiscoveryCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeHubDiscovery()));
53 connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart()));
54 connect(ui->shutdownCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeShutdownExit()));
55 connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070056 connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070057
58 statusUpdateThread = new QThread();
59 statusUpdateThread->start();
60
61 daemonStatusTimer = new QTimer(statusUpdateThread);
62 connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -080063 daemonStatusTimer->start(2000);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070064
65 trayIcon->show();
Ilya Moiseenko69abb972013-11-04 16:02:20 -080066 model = NULL;
67 dialog = new FibInputDialog(this);
68
69 urlManager = new QNetworkAccessManager(this);
70 connect(urlManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(runXmlProc(QNetworkReply*)));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070071}
72
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070073void TrayMenu::loadSettings()
74{
75 QVariant value;
76
77 value = persistentSettings->value(ALLOW_SOFTWARE_UPDATES);
78 if(!value.isNull())
79 {
80 allowAutomaticUpdates = value.toBool();
81 ui->softwareUpdateCheckbox->setChecked(allowAutomaticUpdates);
82 }
83 else
84 {
85 ui->softwareUpdateCheckbox->setChecked(true);
86 changeSoftwareUpdate();
87 }
88
89 value = persistentSettings->value(ENABLE_HUB_DISCOVERY);
90 if(!value.isNull())
91 {
92 enableHubDiscovery = value.toBool();
93 ui->hubDiscoveryCheckbox->setChecked(enableHubDiscovery);
94 }
95 else
96 {
97 ui->hubDiscoveryCheckbox->setChecked(true);
98 changeHubDiscovery();
99 }
100
101 value = persistentSettings->value(ENABLE_START_ON_LOGIN);
102 if(!value.isNull())
103 {
104 enableStartOnLogin = value.toBool();
105 ui->loginStartCheckbox->setChecked(enableStartOnLogin);
106 }
107 else
108 {
109 ui->loginStartCheckbox->setChecked(false);
110 changeLoginStart();
111 }
112
113 value = persistentSettings->value(SHUTDOWN_ON_EXIT);
114 if(!value.isNull())
115 {
116 shutdownOnExit = value.toBool();
117 ui->shutdownCheckbox->setChecked(shutdownOnExit);
118 }
119 else
120 {
121 ui->shutdownCheckbox->setChecked(false);
122 changeShutdownExit();
123 }
124}
125
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700126void TrayMenu::changeSoftwareUpdate()
127{
128 if(ui->softwareUpdateCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700129 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700130 allowAutomaticUpdates = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700131 persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, true);
132 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700133 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700134 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700135 allowAutomaticUpdates = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700136 persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, false);
137 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700138}
139
140void TrayMenu::changeHubDiscovery()
141{
142 if(ui->hubDiscoveryCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700143 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700144 enableHubDiscovery = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700145 persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true);
146 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700147 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700148 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700149 enableHubDiscovery = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700150 persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false);
151 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700152}
153
154void TrayMenu::changeLoginStart()
155{
156 if(ui->loginStartCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700157 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700158 enableStartOnLogin = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700159 persistentSettings->setValue(ENABLE_START_ON_LOGIN, true);
160 makeAutostartDirectory();
161 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700162 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700163 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700164 enableStartOnLogin = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700165 persistentSettings->setValue(ENABLE_START_ON_LOGIN, false);
166
167 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800168 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700169 QStringList arguments;
170 arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE;
171 process->start("rm", arguments);
172 }
173}
174
175void TrayMenu::makeAutostartDirectory()
176{
177 QProcess *process = new QProcess();
178 connect(process, SIGNAL(finished(int)), this, SLOT(copyFile()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800179 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700180 QStringList arguments;
181 arguments << QDir::homePath() + AUTOSTART_DIRECTORY;
182 process->start("mkdir", arguments);
183}
184
185void TrayMenu::copyFile()
186{
187 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800188 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700189 QStringList arguments;
190 arguments << QApplication::applicationDirPath() + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY;
191 process->start("cp",arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700192}
193
194void TrayMenu::changeShutdownExit()
195{
196 if(ui->shutdownCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700197 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700198 shutdownOnExit = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700199 persistentSettings->setValue(SHUTDOWN_ON_EXIT, true);
200 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700201 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700202 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700203 shutdownOnExit = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700204 persistentSettings->setValue(SHUTDOWN_ON_EXIT, false);
205 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700206}
207
208void TrayMenu::showFibInputDialog()
209{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800210 dialog->clear();
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700211 dialog->exec();
212}
213
214void TrayMenu::openTrafficMap()
215{
216 QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode));
217}
218
219void TrayMenu::openRoutingStatus()
220{
221 QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode));
222}
223
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700224void TrayMenu::openCertificationPage()
225{
226 QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode));
227}
228
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700229void TrayMenu::createTrayIcon()
230{
231 trayIconMenu = new QMenu(this);
232
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800233 statusIndicator = new QAction("Inactive", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700234 trayIconMenu->addAction(statusIndicator);
235
236 trayIconMenu->addSeparator();
237
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700238 displayStatus = new QAction(" Sent / Recv ", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700239 trayIconMenu->addAction(displayStatus);
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700240 interestSentRecv = new QAction("Interests 0 / 0", this);
241 trayIconMenu->addAction(interestSentRecv);
242 dataSentRecv = new QAction("Data 0 / 0", this);
243 trayIconMenu->addAction(dataSentRecv);
244
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700245 trayIconMenu->addSeparator();
246
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800247 open = new QAction("Preferences...", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700248 connect(open, SIGNAL(triggered()), this, SLOT(show()));
249 trayIconMenu->addAction(open);
250
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800251 close = new QAction("Quit...", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700252 connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit()));
253 trayIconMenu->addAction(close);
254
255 trayIcon = new QSystemTrayIcon(this);
256 trayIcon->setContextMenu(trayIconMenu);
257
258 connect(
259 trayIcon,
260 SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
261 this,
262 SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))
263 );
264}
265
266void TrayMenu::createToolbar()
267{
268 toolBar = new QToolBar(this);
269 toolBar->setFloatable(false);
270 toolBar->setMovable(false);
271 toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
272 toolBar->setAllowedAreas(Qt::TopToolBarArea);
273 toolBar->setOrientation(Qt::Horizontal);
274 toolBar->setIconSize(QSize(32,32));
275 toolBar->resize(this->width(), 64);
276
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800277 openGeneralSettings = new QAction("General", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700278 openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png"));
279 connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked()));
280
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800281 openForwardingSettings = new QAction("Forwarding", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700282 openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png"));
283 connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked()));
284
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800285 openSecuritySettings = new QAction("Security", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700286 openSecuritySettings->setIcon(QIcon(":/resource/Resources/emblem-system.png"));
287 connect(openSecuritySettings,SIGNAL(triggered()),this, SLOT(securitySettingsClicked()));
288
289 toolBar->addAction(openGeneralSettings);
290 toolBar->addAction(openForwardingSettings);
291 toolBar->addAction(openSecuritySettings);
292}
293
294void TrayMenu::generalSettingsClicked()
295{
296 ui->generalSettingsWidget->show();
297 ui->forwardingSettingsWidget->hide();
298 ui->securitySettingsWidget->hide();
299}
300
301void TrayMenu::forwardingSettingsClicked()
302{
303 ui->generalSettingsWidget->hide();
304 ui->forwardingSettingsWidget->show();
305 ui->securitySettingsWidget->hide();
306}
307
308void TrayMenu::securitySettingsClicked()
309{
310 ui->generalSettingsWidget->hide();
311 ui->forwardingSettingsWidget->hide();
312 ui->securitySettingsWidget->show();
313}
314
315void TrayMenu::displayPopup()
316{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800317 trayIcon->showMessage("NDNx Status", statusXml);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700318}
319
320void TrayMenu::addFibEntry()
321{
322 QString name = dialog->getPrefixName();
323 QString tunnelType = dialog->getTunnelType();
324 QString endpoint = dialog->getEndpoint();
325
326 QStringList arguments;
327 arguments << "add" << name << tunnelType << endpoint;
328
329 dialog->hide();
330
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700331 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800332 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700333 process->start(NDND_FIB_COMMAND, arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700334}
335
336void TrayMenu::confirmQuit()
337{
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700338 if(shutdownOnExit)
339 terminateDaemonAndClose();
340 else
341 {
342 QuitDialog dialog(this);
343 dialog.exec();
344 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700345}
346
347void TrayMenu::terminateDaemonAndClose()
348{
349 QProcess *process = new QProcess(this);
350 process->start(NDND_STOP_COMMAND);
351 connect(process,SIGNAL(finished(int)), qApp, SLOT(quit()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800352 connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700353}
354
355void TrayMenu::closeEvent(QCloseEvent *event)
356{
357 if (trayIcon->isVisible())
358 {
359 hide();
360 event->ignore();
361 }
362}
363
364void TrayMenu::showEvent(QShowEvent * event)
365{
366 ui->generalSettingsWidget->show();
367 ui->forwardingSettingsWidget->hide();
368 ui->securitySettingsWidget->hide();
369}
370
371void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
372{
373 if(reason == QSystemTrayIcon::Trigger)
374 this->show();
375}
376
377void TrayMenu::setIcon(bool isConnected)
378{
379 if(isConnected)
380 trayIcon->setIcon(QIcon(":/resource/Resources/icon-connected-white.png"));
381 else
382 trayIcon->setIcon(QIcon(":/resource/Resources/icon-disconnected-white.png"));
383}
384
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700385void TrayMenu::daemonStatusUpdate()
386{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800387 urlManager->get(QNetworkRequest(QUrl("http://localhost:9695/?f=xml")));
388}
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700389
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800390void TrayMenu::runXmlProc(QNetworkReply *reply)
391{
392 QByteArray buffer = reply->readAll();
393 applyStatusXslt = new QProcess();
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700394
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800395 QStringList arguments;
396 arguments << QApplication::applicationDirPath() + "/" + STATUS_XSLT_FILE << "-";
397 connect(applyStatusXslt,SIGNAL(finished(int)), this, SLOT(parseStatusXml()));
398 //connect(applyStatusXslt,SIGNAL(finished(int)), applyStatusXslt, SLOT(deleteLater()));
399 applyStatusXslt->start(XSLT_PROC,arguments);
400 applyStatusXslt->write(buffer);
401 applyStatusXslt->closeWriteChannel();
402
403 applyFibXslt = new QProcess();
404 QStringList args;
405 args << QApplication::applicationDirPath() + "/" + FIB_XSLT_FILE << "-";
406 connect(applyFibXslt,SIGNAL(finished(int)), this, SLOT(parseFibXml()));
407 //connect(applyFibXslt,SIGNAL(finished(int)), applyFibXslt, SLOT(deleteLater()));
408 applyFibXslt->start(XSLT_PROC,args);
409 applyFibXslt->write(buffer);
410 applyFibXslt->closeWriteChannel();
411}
412
413void TrayMenu::parseStatusXml()
414{
415 QByteArray stdout = applyStatusXslt->readAllStandardOutput();
416 QByteArray stderr = applyStatusXslt->readAllStandardError();
417 applyStatusXslt->deleteLater();
418
419 statusXml = QString(stdout);
420
421 if((statusXml == "") || (stderr.length()>0)) // there was an error during Query evaluation
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700422 {
423 daemonStarted = false;
424 setIcon(false);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800425 statusIndicator->setText("Starting...");
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700426
427 QProcess *process = new QProcess();
428 if(enableHubDiscovery)
429 connect(process, SIGNAL(finished(int)), networkManager, SLOT(autoconfigDaemon()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800430
431 connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700432 process->start(NDND_START_COMMAND);
433 }
434 else
435 {
436 daemonStarted = true;
437 setIcon(true);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800438 statusIndicator->setText("Active");
439
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700440 QString interestIn = statusXml.mid(statusXml.indexOf("<in>")+4, statusXml.indexOf("</in>") - (statusXml.indexOf("<in>")+4));
441 QString interestOut = statusXml.mid(statusXml.indexOf("<out>")+5, statusXml.indexOf("</out>") - (statusXml.indexOf("<out>")+5));
442 QString dataIn = statusXml.mid(statusXml.lastIndexOf("<in>")+4, statusXml.lastIndexOf("</in>") - (statusXml.lastIndexOf("<in>")+4));
443 QString dataOut = statusXml.mid(statusXml.lastIndexOf("<out>")+5, statusXml.lastIndexOf("</out>") - (statusXml.lastIndexOf("<out>")+5));
444
445 int i = 0;
446 int k = 0;
447 if((dataOut.length() - interestOut.length()) > 0)
448 {
449 i = dataOut.length() - interestOut.length();
450 i*=2; //because space takes less space than a letter
451 }
452
453 if((interestOut.length() - dataOut.length()) > 0)
454 {
455 k = interestOut.length() - dataOut.length();
456 k*=2; //because space takes less space than a letter
457 }
458
459 QString interestStats = QString("%1%2%3%4").arg("Interests",-16,' ').arg(interestOut,6+i,' ').arg(" / ",3).arg(interestIn,-6,' ');
460 QString dataStats = QString("%1%2%3%4").arg("Data",-20,' ').arg(dataOut,6+k,' ').arg(" / ",3).arg(dataIn,-6,' ');
461
462 //Now I try to align header "Sent / Recv" centrally with the upper line
463 QString padding;
464 for(int j = 0; j < interestStats.indexOf(interestOut); j++)
465 {
466 if(interestStats.at(j)==' ')
467 padding +=" ";
468 else
469 padding += " "; //because space takes less space than a letter
470 }
471
472 QString header;
473 int m = 0;
474 if(interestOut.length() - QString("Sent").length() > 0)
475 {
476 m = interestOut.length() - QString("Sent").length();
477 m *=3;
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700478 header = QString("%1%2").arg(padding).arg(" Sent / Recv",QString(" Sent / Recv").length() + m,' ');
479 }
480 else if(interestOut.length() - QString("Sent").length() < 0)
481 {
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700482 padding.truncate(padding.length()-(QString("Sent").length() - interestOut.length()));
483 header = padding + "Sent / Recv";
484 }
485 else
486 {
487 header = padding + " Sent / Recv";
488 }
489
490 interestSentRecv->setText(interestStats);
491 dataSentRecv->setText(dataStats);
492 displayStatus->setText(header);
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700493 }
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800494}
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700495
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800496void TrayMenu::parseFibXml()
497{
498 QByteArray stdout = applyFibXslt->readAllStandardOutput();
499 QByteArray stderr = applyFibXslt->readAllStandardError();
500 applyFibXslt->deleteLater();
501
502 fibContentsXml = QString(stdout);
503
504 if((stdout == "") || (stderr.length()>0))
505 return;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700506
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700507 if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1))
508 {
509 networkManager->autoconfigDaemon();
510 }
511
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700512 QDomDocument xmldoc;
513 QDomElement root;
514
515 xmldoc.setContent(fibContentsXml);
516 root=xmldoc.documentElement();
517
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800518 if(model != NULL)
519 {
520 scrollPosition = ui->tableView->verticalScrollBar()->value();
521 model->clear();
522 delete model;
523 }
524
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700525 model = new QStandardItemModel(root.childNodes().count(),3);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800526 model->setHorizontalHeaderItem(0, new QStandardItem("NDN prefix"));
527 model->setHorizontalHeaderItem(1, new QStandardItem("Face ID"));
528 model->setHorizontalHeaderItem(2, new QStandardItem("Endpoint"));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700529
530 int row = 0;
531 QDomNode fibEntry=root.firstChild();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800532
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700533 while (!fibEntry.isNull())
534 {
535 QDomNodeList properties = fibEntry.childNodes();
536
537 QDomNode faceID = properties.at(0);
538 QDomNode ip = properties.at(1);
539 QDomNode prefix = properties.at(2);
540
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700541 model->setItem(row, 0, new QStandardItem(prefix.toElement().text()));
542 model->setItem(row, 1, new QStandardItem(faceID.toElement().text()));
543 model->setItem(row, 2, new QStandardItem(ip.toElement().text()));
544
545 fibEntry = fibEntry.nextSibling();
546 row++;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800547 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700548
549 ui->tableView->setModel(model);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800550 //ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
551 ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
552 ui->tableView->setColumnWidth(0, 260);
553 ui->tableView->setColumnWidth(1, 57);
554 ui->tableView->setColumnWidth(2, 150);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700555
556 if(selectedRow >= 0)
557 ui->tableView->selectRow(selectedRow);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800558
559 if(scrollPosition >= 0)
560 ui->tableView->verticalScrollBar()->setValue(scrollPosition);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700561}
562
563void TrayMenu::createTableView()
564{
565 ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
566 ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
567 ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
568
569 selectedRow = -1;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800570 scrollPosition = -1;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700571}
572
573void TrayMenu::selectTableRow()
574{
575 selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row();
576}
577
578void TrayMenu::deleteFibEntry()
579{
580 if(selectedRow < 0)
581 return;
582
583 QStandardItem *prefix = model->item(selectedRow,0);
584 if(prefix == NULL)
585 return;
586
587 QStandardItem *faceID = model->item(selectedRow,1);
588 if(faceID == NULL)
589 return;
590
591 QStringList arguments;
592 arguments << "del" << prefix->text() << "face" << faceID->text();
593
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700594 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800595 connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700596 process->start(NDND_FIB_COMMAND, arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700597}
598
599TrayMenu::~TrayMenu()
600{
601 daemonStatusTimer->stop();
602 statusUpdateThread->exit();
603 delete ui;
604 delete trayIcon;
605 delete trayIconMenu;
606 delete open;
607 delete close;
608 delete openGeneralSettings;
609 delete openForwardingSettings;
610 delete openSecuritySettings;
611 delete toolBar;
612 delete statusUpdateThread;
613 delete daemonStatusTimer;
614 delete dialog;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800615 delete networkManager;
616 delete persistentSettings;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700617}
Alexander Afanasyeva822b572013-11-04 12:36:34 -0800618
619#if WAF
620#include "tray-menu.moc"
621#include "tray-menu.cpp.moc"
622#endif