blob: db4b321846ac9cddf7e333d8ab1488c7ceda8f2b [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
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -08008#include "config.h"
9
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070010#include "tray-menu.h"
11#include "ui_traymenu.h" //generated from traymenu.ui
12#include "quit-dialog.h"
13#include "fib-input-dialog.h"
Ilya Moiseenko656d52d2013-10-25 14:19:10 -070014#include "network-manager.h"
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070015
16#include <QMenu>
17#include <QCloseEvent>
18#include <QDesktopServices>
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070019#include <QUrl>
20#include <QCheckBox>
21#include <QLabel>
22#include <QHBoxLayout>
23#include <QLineEdit>
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070024#include <QDir>
Ilya Moiseenko69abb972013-11-04 16:02:20 -080025#include <QScrollBar>
26
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070027
28TrayMenu::TrayMenu(QWidget *parent) :
29 QMainWindow(parent),
30 ui(new Ui::TrayMenu)
31{
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070032 networkManager = new NetworkManager();
33
34 QCoreApplication::setOrganizationName("UCLA");
35 QCoreApplication::setOrganizationDomain("named-data.net");
36 QCoreApplication::setApplicationName("NDNx Control Center");
37
38 persistentSettings = new QSettings();
39
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070040 ui->setupUi(this);
41
42 createTrayIcon();
43 setIcon(true);
44 createToolbar();
45 createTableView();
46
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070047 loadSettings();
Ilya Moiseenko656d52d2013-10-25 14:19:10 -070048
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070049 connect(ui->openTrafficMapButton, SIGNAL(pressed()), this, SLOT(openTrafficMap()));
50 connect(ui->openRoutingStatusButton, SIGNAL(pressed()), this, SLOT(openRoutingStatus()));
51 connect(ui->addFibButton, SIGNAL(pressed()), this, SLOT(showFibInputDialog()));
52 connect(ui->deleteFibButton, SIGNAL(released()), this, SLOT(deleteFibEntry()));
53 connect(ui->softwareUpdateCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeSoftwareUpdate()));
54 connect(ui->hubDiscoveryCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeHubDiscovery()));
55 connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart()));
56 connect(ui->shutdownCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeShutdownExit()));
57 connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070058 connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070059
60 statusUpdateThread = new QThread();
61 statusUpdateThread->start();
62
63 daemonStatusTimer = new QTimer(statusUpdateThread);
64 connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -080065 daemonStatusTimer->start(2000);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070066
67 trayIcon->show();
Ilya Moiseenko69abb972013-11-04 16:02:20 -080068 model = NULL;
69 dialog = new FibInputDialog(this);
70
71 urlManager = new QNetworkAccessManager(this);
72 connect(urlManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(runXmlProc(QNetworkReply*)));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070073}
74
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070075void TrayMenu::loadSettings()
76{
77 QVariant value;
78
79 value = persistentSettings->value(ALLOW_SOFTWARE_UPDATES);
80 if(!value.isNull())
81 {
82 allowAutomaticUpdates = value.toBool();
83 ui->softwareUpdateCheckbox->setChecked(allowAutomaticUpdates);
84 }
85 else
86 {
87 ui->softwareUpdateCheckbox->setChecked(true);
88 changeSoftwareUpdate();
89 }
90
91 value = persistentSettings->value(ENABLE_HUB_DISCOVERY);
92 if(!value.isNull())
93 {
94 enableHubDiscovery = value.toBool();
95 ui->hubDiscoveryCheckbox->setChecked(enableHubDiscovery);
96 }
97 else
98 {
99 ui->hubDiscoveryCheckbox->setChecked(true);
100 changeHubDiscovery();
101 }
102
103 value = persistentSettings->value(ENABLE_START_ON_LOGIN);
104 if(!value.isNull())
105 {
106 enableStartOnLogin = value.toBool();
107 ui->loginStartCheckbox->setChecked(enableStartOnLogin);
108 }
109 else
110 {
111 ui->loginStartCheckbox->setChecked(false);
112 changeLoginStart();
113 }
114
115 value = persistentSettings->value(SHUTDOWN_ON_EXIT);
116 if(!value.isNull())
117 {
118 shutdownOnExit = value.toBool();
119 ui->shutdownCheckbox->setChecked(shutdownOnExit);
120 }
121 else
122 {
123 ui->shutdownCheckbox->setChecked(false);
124 changeShutdownExit();
125 }
126}
127
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700128void TrayMenu::changeSoftwareUpdate()
129{
130 if(ui->softwareUpdateCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700131 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700132 allowAutomaticUpdates = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700133 persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, true);
134 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700135 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700136 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700137 allowAutomaticUpdates = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700138 persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, false);
139 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700140}
141
142void TrayMenu::changeHubDiscovery()
143{
144 if(ui->hubDiscoveryCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700145 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700146 enableHubDiscovery = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700147 persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true);
148 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700149 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700150 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700151 enableHubDiscovery = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700152 persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false);
153 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700154}
155
156void TrayMenu::changeLoginStart()
157{
158 if(ui->loginStartCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700159 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700160 enableStartOnLogin = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700161 persistentSettings->setValue(ENABLE_START_ON_LOGIN, true);
162 makeAutostartDirectory();
163 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700164 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700165 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700166 enableStartOnLogin = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700167 persistentSettings->setValue(ENABLE_START_ON_LOGIN, false);
168
169 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800170 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700171 QStringList arguments;
172 arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE;
173 process->start("rm", arguments);
174 }
175}
176
177void TrayMenu::makeAutostartDirectory()
178{
179 QProcess *process = new QProcess();
180 connect(process, SIGNAL(finished(int)), this, SLOT(copyFile()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800181 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700182 QStringList arguments;
183 arguments << QDir::homePath() + AUTOSTART_DIRECTORY;
184 process->start("mkdir", arguments);
185}
186
187void TrayMenu::copyFile()
188{
189 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800190 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700191 QStringList arguments;
Alexander Afanasyeveb111572013-11-08 22:59:43 -0800192 arguments << QString(RESOURCES_DIR) + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700193 process->start("cp",arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700194}
195
196void TrayMenu::changeShutdownExit()
197{
198 if(ui->shutdownCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700199 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700200 shutdownOnExit = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700201 persistentSettings->setValue(SHUTDOWN_ON_EXIT, true);
202 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700203 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700204 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700205 shutdownOnExit = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700206 persistentSettings->setValue(SHUTDOWN_ON_EXIT, false);
207 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700208}
209
210void TrayMenu::showFibInputDialog()
211{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800212 dialog->clear();
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700213 dialog->exec();
214}
215
216void TrayMenu::openTrafficMap()
217{
218 QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode));
219}
220
221void TrayMenu::openRoutingStatus()
222{
223 QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode));
224}
225
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700226void TrayMenu::openCertificationPage()
227{
228 QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode));
229}
230
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700231void TrayMenu::createTrayIcon()
232{
233 trayIconMenu = new QMenu(this);
234
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800235 statusIndicator = new QAction("Inactive", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700236 trayIconMenu->addAction(statusIndicator);
237
238 trayIconMenu->addSeparator();
239
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700240 displayStatus = new QAction(" Sent / Recv ", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700241 trayIconMenu->addAction(displayStatus);
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700242 interestSentRecv = new QAction("Interests 0 / 0", this);
243 trayIconMenu->addAction(interestSentRecv);
244 dataSentRecv = new QAction("Data 0 / 0", this);
245 trayIconMenu->addAction(dataSentRecv);
246
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700247 trayIconMenu->addSeparator();
248
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800249 open = new QAction("Preferences...", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700250 connect(open, SIGNAL(triggered()), this, SLOT(show()));
251 trayIconMenu->addAction(open);
252
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800253 close = new QAction("Quit...", this);
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800254 // connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit()));
255 connect(close, SIGNAL(triggered()), qApp, SLOT(quit()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700256 trayIconMenu->addAction(close);
257
258 trayIcon = new QSystemTrayIcon(this);
259 trayIcon->setContextMenu(trayIconMenu);
260
261 connect(
262 trayIcon,
263 SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
264 this,
265 SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))
266 );
267}
268
269void TrayMenu::createToolbar()
270{
271 toolBar = new QToolBar(this);
272 toolBar->setFloatable(false);
273 toolBar->setMovable(false);
274 toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
275 toolBar->setAllowedAreas(Qt::TopToolBarArea);
276 toolBar->setOrientation(Qt::Horizontal);
277 toolBar->setIconSize(QSize(32,32));
278 toolBar->resize(this->width(), 64);
279
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800280 openGeneralSettings = new QAction("General", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700281 openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png"));
282 connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked()));
283
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800284 openForwardingSettings = new QAction("Forwarding", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700285 openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png"));
286 connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked()));
287
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800288 openSecuritySettings = new QAction("Security", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700289 openSecuritySettings->setIcon(QIcon(":/resource/Resources/emblem-system.png"));
290 connect(openSecuritySettings,SIGNAL(triggered()),this, SLOT(securitySettingsClicked()));
291
292 toolBar->addAction(openGeneralSettings);
293 toolBar->addAction(openForwardingSettings);
294 toolBar->addAction(openSecuritySettings);
295}
296
297void TrayMenu::generalSettingsClicked()
298{
299 ui->generalSettingsWidget->show();
300 ui->forwardingSettingsWidget->hide();
301 ui->securitySettingsWidget->hide();
302}
303
304void TrayMenu::forwardingSettingsClicked()
305{
306 ui->generalSettingsWidget->hide();
307 ui->forwardingSettingsWidget->show();
308 ui->securitySettingsWidget->hide();
309}
310
311void TrayMenu::securitySettingsClicked()
312{
313 ui->generalSettingsWidget->hide();
314 ui->forwardingSettingsWidget->hide();
315 ui->securitySettingsWidget->show();
316}
317
318void TrayMenu::displayPopup()
319{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800320 trayIcon->showMessage("NDNx Status", statusXml);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700321}
322
323void TrayMenu::addFibEntry()
324{
325 QString name = dialog->getPrefixName();
326 QString tunnelType = dialog->getTunnelType();
327 QString endpoint = dialog->getEndpoint();
328
329 QStringList arguments;
330 arguments << "add" << name << tunnelType << endpoint;
331
332 dialog->hide();
333
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700334 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800335 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700336 process->start(NDND_FIB_COMMAND, arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700337}
338
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800339// void TrayMenu::confirmQuit()
340// {
341// if(shutdownOnExit)
342// terminateDaemonAndClose();
343// else
344// {
345// QuitDialog dialog(this);
346// dialog.exec();
347// }
348// }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700349
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800350// void TrayMenu::terminateDaemonAndClose()
351// {
352// QProcess *process = new QProcess(this);
353// process->start(NDND_STOP_COMMAND);
354// connect(process,SIGNAL(finished(int)), qApp, SLOT(quit()));
355// connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
356// }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700357
358void TrayMenu::closeEvent(QCloseEvent *event)
359{
360 if (trayIcon->isVisible())
361 {
362 hide();
363 event->ignore();
364 }
365}
366
367void TrayMenu::showEvent(QShowEvent * event)
368{
369 ui->generalSettingsWidget->show();
370 ui->forwardingSettingsWidget->hide();
371 ui->securitySettingsWidget->hide();
372}
373
374void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
375{
376 if(reason == QSystemTrayIcon::Trigger)
377 this->show();
378}
379
380void TrayMenu::setIcon(bool isConnected)
381{
382 if(isConnected)
383 trayIcon->setIcon(QIcon(":/resource/Resources/icon-connected-white.png"));
384 else
385 trayIcon->setIcon(QIcon(":/resource/Resources/icon-disconnected-white.png"));
386}
387
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700388void TrayMenu::daemonStatusUpdate()
389{
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -0800390 urlManager->get(QNetworkRequest(QUrl("http://localhost:6363/?f=xml")));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800391}
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700392
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800393void TrayMenu::runXmlProc(QNetworkReply *reply)
394{
395 QByteArray buffer = reply->readAll();
396 applyStatusXslt = new QProcess();
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700397
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800398 QStringList arguments;
Alexander Afanasyeveb111572013-11-08 22:59:43 -0800399 arguments << QString(RESOURCES_DIR) + "/" + STATUS_XSLT_FILE << "-";
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800400 connect(applyStatusXslt,SIGNAL(finished(int)), this, SLOT(parseStatusXml()));
401 //connect(applyStatusXslt,SIGNAL(finished(int)), applyStatusXslt, SLOT(deleteLater()));
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -0800402 applyStatusXslt->start(XSLTPROC,arguments);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800403 applyStatusXslt->write(buffer);
404 applyStatusXslt->closeWriteChannel();
405
406 applyFibXslt = new QProcess();
407 QStringList args;
Alexander Afanasyeveb111572013-11-08 22:59:43 -0800408 args << QString(RESOURCES_DIR) + "/" + FIB_XSLT_FILE << "-";
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800409 connect(applyFibXslt,SIGNAL(finished(int)), this, SLOT(parseFibXml()));
410 //connect(applyFibXslt,SIGNAL(finished(int)), applyFibXslt, SLOT(deleteLater()));
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -0800411 applyFibXslt->start(XSLTPROC,args);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800412 applyFibXslt->write(buffer);
413 applyFibXslt->closeWriteChannel();
414}
415
416void TrayMenu::parseStatusXml()
417{
418 QByteArray stdout = applyStatusXslt->readAllStandardOutput();
419 QByteArray stderr = applyStatusXslt->readAllStandardError();
420 applyStatusXslt->deleteLater();
421
422 statusXml = QString(stdout);
423
424 if((statusXml == "") || (stderr.length()>0)) // there was an error during Query evaluation
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700425 {
426 daemonStarted = false;
427 setIcon(false);
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800428 statusIndicator->setText("Inactive");
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700429
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800430 // QProcess *process = new QProcess();
431 // if(enableHubDiscovery)
432 // connect(process, SIGNAL(finished(int)), networkManager, SLOT(autoconfigDaemon()));
433 //
434 // connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
435 // process->start(NDND_START_COMMAND);
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700436 }
437 else
438 {
439 daemonStarted = true;
440 setIcon(true);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800441 statusIndicator->setText("Active");
442
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700443 QString interestIn = statusXml.mid(statusXml.indexOf("<in>")+4, statusXml.indexOf("</in>") - (statusXml.indexOf("<in>")+4));
444 QString interestOut = statusXml.mid(statusXml.indexOf("<out>")+5, statusXml.indexOf("</out>") - (statusXml.indexOf("<out>")+5));
445 QString dataIn = statusXml.mid(statusXml.lastIndexOf("<in>")+4, statusXml.lastIndexOf("</in>") - (statusXml.lastIndexOf("<in>")+4));
446 QString dataOut = statusXml.mid(statusXml.lastIndexOf("<out>")+5, statusXml.lastIndexOf("</out>") - (statusXml.lastIndexOf("<out>")+5));
447
448 int i = 0;
449 int k = 0;
450 if((dataOut.length() - interestOut.length()) > 0)
451 {
452 i = dataOut.length() - interestOut.length();
453 i*=2; //because space takes less space than a letter
454 }
455
456 if((interestOut.length() - dataOut.length()) > 0)
457 {
458 k = interestOut.length() - dataOut.length();
459 k*=2; //because space takes less space than a letter
460 }
461
462 QString interestStats = QString("%1%2%3%4").arg("Interests",-16,' ').arg(interestOut,6+i,' ').arg(" / ",3).arg(interestIn,-6,' ');
463 QString dataStats = QString("%1%2%3%4").arg("Data",-20,' ').arg(dataOut,6+k,' ').arg(" / ",3).arg(dataIn,-6,' ');
464
465 //Now I try to align header "Sent / Recv" centrally with the upper line
466 QString padding;
467 for(int j = 0; j < interestStats.indexOf(interestOut); j++)
468 {
469 if(interestStats.at(j)==' ')
470 padding +=" ";
471 else
472 padding += " "; //because space takes less space than a letter
473 }
474
475 QString header;
476 int m = 0;
477 if(interestOut.length() - QString("Sent").length() > 0)
478 {
479 m = interestOut.length() - QString("Sent").length();
480 m *=3;
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700481 header = QString("%1%2").arg(padding).arg(" Sent / Recv",QString(" Sent / Recv").length() + m,' ');
482 }
483 else if(interestOut.length() - QString("Sent").length() < 0)
484 {
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700485 padding.truncate(padding.length()-(QString("Sent").length() - interestOut.length()));
486 header = padding + "Sent / Recv";
487 }
488 else
489 {
490 header = padding + " Sent / Recv";
491 }
492
493 interestSentRecv->setText(interestStats);
494 dataSentRecv->setText(dataStats);
495 displayStatus->setText(header);
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700496 }
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800497}
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700498
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800499void TrayMenu::parseFibXml()
500{
501 QByteArray stdout = applyFibXslt->readAllStandardOutput();
502 QByteArray stderr = applyFibXslt->readAllStandardError();
503 applyFibXslt->deleteLater();
504
505 fibContentsXml = QString(stdout);
506
507 if((stdout == "") || (stderr.length()>0))
508 return;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700509
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700510 if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1))
511 {
512 networkManager->autoconfigDaemon();
513 }
514
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700515 QDomDocument xmldoc;
516 QDomElement root;
517
518 xmldoc.setContent(fibContentsXml);
519 root=xmldoc.documentElement();
520
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800521 if(model != NULL)
522 {
523 scrollPosition = ui->tableView->verticalScrollBar()->value();
524 model->clear();
525 delete model;
526 }
527
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700528 model = new QStandardItemModel(root.childNodes().count(),3);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800529 model->setHorizontalHeaderItem(0, new QStandardItem("NDN prefix"));
530 model->setHorizontalHeaderItem(1, new QStandardItem("Face ID"));
531 model->setHorizontalHeaderItem(2, new QStandardItem("Endpoint"));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700532
533 int row = 0;
534 QDomNode fibEntry=root.firstChild();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800535
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700536 while (!fibEntry.isNull())
537 {
538 QDomNodeList properties = fibEntry.childNodes();
539
540 QDomNode faceID = properties.at(0);
541 QDomNode ip = properties.at(1);
542 QDomNode prefix = properties.at(2);
543
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700544 model->setItem(row, 0, new QStandardItem(prefix.toElement().text()));
545 model->setItem(row, 1, new QStandardItem(faceID.toElement().text()));
546 model->setItem(row, 2, new QStandardItem(ip.toElement().text()));
547
548 fibEntry = fibEntry.nextSibling();
549 row++;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800550 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700551
552 ui->tableView->setModel(model);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800553 //ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
554 ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
555 ui->tableView->setColumnWidth(0, 260);
556 ui->tableView->setColumnWidth(1, 57);
557 ui->tableView->setColumnWidth(2, 150);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700558
559 if(selectedRow >= 0)
560 ui->tableView->selectRow(selectedRow);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800561
562 if(scrollPosition >= 0)
563 ui->tableView->verticalScrollBar()->setValue(scrollPosition);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700564}
565
566void TrayMenu::createTableView()
567{
568 ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
569 ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
570 ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
571
572 selectedRow = -1;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800573 scrollPosition = -1;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700574}
575
576void TrayMenu::selectTableRow()
577{
578 selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row();
579}
580
581void TrayMenu::deleteFibEntry()
582{
583 if(selectedRow < 0)
584 return;
585
586 QStandardItem *prefix = model->item(selectedRow,0);
587 if(prefix == NULL)
588 return;
589
590 QStandardItem *faceID = model->item(selectedRow,1);
591 if(faceID == NULL)
592 return;
593
594 QStringList arguments;
595 arguments << "del" << prefix->text() << "face" << faceID->text();
596
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700597 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800598 connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700599 process->start(NDND_FIB_COMMAND, arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700600}
601
602TrayMenu::~TrayMenu()
603{
604 daemonStatusTimer->stop();
605 statusUpdateThread->exit();
606 delete ui;
607 delete trayIcon;
608 delete trayIconMenu;
609 delete open;
610 delete close;
611 delete openGeneralSettings;
612 delete openForwardingSettings;
613 delete openSecuritySettings;
614 delete toolBar;
615 delete statusUpdateThread;
616 delete daemonStatusTimer;
617 delete dialog;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800618 delete networkManager;
619 delete persistentSettings;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700620}
Alexander Afanasyeva822b572013-11-04 12:36:34 -0800621
622#if WAF
623#include "tray-menu.moc"
624#include "tray-menu.cpp.moc"
625#endif