blob: b6c405edcf06127d739d878fe20cedb4b9b2279a [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>
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -080026#include <QHBoxLayout>
27#include <QVBoxLayout>
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070028
29TrayMenu::TrayMenu(QWidget *parent) :
30 QMainWindow(parent),
31 ui(new Ui::TrayMenu)
32{
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070033 networkManager = new NetworkManager();
34
35 QCoreApplication::setOrganizationName("UCLA");
36 QCoreApplication::setOrganizationDomain("named-data.net");
37 QCoreApplication::setApplicationName("NDNx Control Center");
38
39 persistentSettings = new QSettings();
40
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070041 ui->setupUi(this);
42
43 createTrayIcon();
44 setIcon(true);
45 createToolbar();
46 createTableView();
47
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070048 loadSettings();
Ilya Moiseenko656d52d2013-10-25 14:19:10 -070049
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070050 connect(ui->openTrafficMapButton, SIGNAL(pressed()), this, SLOT(openTrafficMap()));
51 connect(ui->openRoutingStatusButton, SIGNAL(pressed()), this, SLOT(openRoutingStatus()));
52 connect(ui->addFibButton, SIGNAL(pressed()), this, SLOT(showFibInputDialog()));
53 connect(ui->deleteFibButton, SIGNAL(released()), this, SLOT(deleteFibEntry()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070054 connect(ui->hubDiscoveryCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeHubDiscovery()));
55 connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070056 connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070057 connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070058
59 statusUpdateThread = new QThread();
60 statusUpdateThread->start();
61
62 daemonStatusTimer = new QTimer(statusUpdateThread);
63 connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -080064 daemonStatusTimer->start(2000);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070065
66 trayIcon->show();
Ilya Moiseenko69abb972013-11-04 16:02:20 -080067 model = NULL;
68 dialog = new FibInputDialog(this);
69
70 urlManager = new QNetworkAccessManager(this);
71 connect(urlManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(runXmlProc(QNetworkReply*)));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -070072}
73
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070074void TrayMenu::loadSettings()
75{
76 QVariant value;
77
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -070078 value = persistentSettings->value(ENABLE_HUB_DISCOVERY);
79 if(!value.isNull())
80 {
81 enableHubDiscovery = value.toBool();
82 ui->hubDiscoveryCheckbox->setChecked(enableHubDiscovery);
83 }
84 else
85 {
86 ui->hubDiscoveryCheckbox->setChecked(true);
87 changeHubDiscovery();
88 }
89
90 value = persistentSettings->value(ENABLE_START_ON_LOGIN);
91 if(!value.isNull())
92 {
93 enableStartOnLogin = value.toBool();
94 ui->loginStartCheckbox->setChecked(enableStartOnLogin);
95 }
96 else
97 {
98 ui->loginStartCheckbox->setChecked(false);
99 changeLoginStart();
100 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700101}
102
103void TrayMenu::changeHubDiscovery()
104{
105 if(ui->hubDiscoveryCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700106 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700107 enableHubDiscovery = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700108 persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true);
109 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700110 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700111 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700112 enableHubDiscovery = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700113 persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false);
114 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700115}
116
117void TrayMenu::changeLoginStart()
118{
119 if(ui->loginStartCheckbox->isChecked())
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700120 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700121 enableStartOnLogin = true;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700122 persistentSettings->setValue(ENABLE_START_ON_LOGIN, true);
123 makeAutostartDirectory();
124 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700125 else
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700126 {
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700127 enableStartOnLogin = false;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700128 persistentSettings->setValue(ENABLE_START_ON_LOGIN, false);
129
130 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800131 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700132 QStringList arguments;
133 arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE;
134 process->start("rm", arguments);
135 }
136}
137
138void TrayMenu::makeAutostartDirectory()
139{
140 QProcess *process = new QProcess();
141 connect(process, SIGNAL(finished(int)), this, SLOT(copyFile()));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800142 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700143 QStringList arguments;
144 arguments << QDir::homePath() + AUTOSTART_DIRECTORY;
145 process->start("mkdir", arguments);
146}
147
148void TrayMenu::copyFile()
149{
150 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800151 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700152 QStringList arguments;
Alexander Afanasyeveb111572013-11-08 22:59:43 -0800153 arguments << QString(RESOURCES_DIR) + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY;
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700154 process->start("cp",arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700155}
156
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700157void TrayMenu::showFibInputDialog()
158{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800159 dialog->clear();
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700160 dialog->exec();
161}
162
163void TrayMenu::openTrafficMap()
164{
165 QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode));
166}
167
168void TrayMenu::openRoutingStatus()
169{
170 QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode));
171}
172
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700173void TrayMenu::openCertificationPage()
174{
175 QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode));
176}
177
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700178void TrayMenu::createTrayIcon()
179{
180 trayIconMenu = new QMenu(this);
181
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800182 statusIndicator = new QAction("Inactive", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700183 trayIconMenu->addAction(statusIndicator);
184
185 trayIconMenu->addSeparator();
186
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700187 displayStatus = new QAction(" Sent / Recv ", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700188 trayIconMenu->addAction(displayStatus);
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700189 interestSentRecv = new QAction("Interests 0 / 0", this);
190 trayIconMenu->addAction(interestSentRecv);
191 dataSentRecv = new QAction("Data 0 / 0", this);
192 trayIconMenu->addAction(dataSentRecv);
193
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700194 trayIconMenu->addSeparator();
195
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800196 open = new QAction("Preferences...", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700197 connect(open, SIGNAL(triggered()), this, SLOT(show()));
198 trayIconMenu->addAction(open);
199
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800200 close = new QAction("Quit", this);
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800201 // connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit()));
202 connect(close, SIGNAL(triggered()), qApp, SLOT(quit()));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700203 trayIconMenu->addAction(close);
204
205 trayIcon = new QSystemTrayIcon(this);
206 trayIcon->setContextMenu(trayIconMenu);
207
208 connect(
209 trayIcon,
210 SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
211 this,
212 SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))
213 );
214}
215
216void TrayMenu::createToolbar()
217{
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800218 ui->toolBar->setFloatable(false);
219 ui->toolBar->setMovable(false);
220 ui->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
221 ui->toolBar->setAllowedAreas(Qt::TopToolBarArea);
222 ui->toolBar->setOrientation(Qt::Horizontal);
223 ui->toolBar->setIconSize(QSize(32,32));
224 ui->toolBar->resize(this->width(), 64);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700225
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800226 openGeneralSettings = new QAction("General", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700227 openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png"));
228 connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked()));
229
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800230 openForwardingSettings = new QAction("Forwarding", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700231 openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png"));
232 connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked()));
233
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800234 openSecuritySettings = new QAction("Security", this);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700235 openSecuritySettings->setIcon(QIcon(":/resource/Resources/emblem-system.png"));
236 connect(openSecuritySettings,SIGNAL(triggered()),this, SLOT(securitySettingsClicked()));
237
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800238 ui->toolBar->addAction(openGeneralSettings);
239 ui->toolBar->addAction(openForwardingSettings);
240 ui->toolBar->addAction(openSecuritySettings);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700241}
242
243void TrayMenu::generalSettingsClicked()
244{
245 ui->generalSettingsWidget->show();
246 ui->forwardingSettingsWidget->hide();
247 ui->securitySettingsWidget->hide();
248}
249
250void TrayMenu::forwardingSettingsClicked()
251{
252 ui->generalSettingsWidget->hide();
253 ui->forwardingSettingsWidget->show();
254 ui->securitySettingsWidget->hide();
255}
256
257void TrayMenu::securitySettingsClicked()
258{
259 ui->generalSettingsWidget->hide();
260 ui->forwardingSettingsWidget->hide();
261 ui->securitySettingsWidget->show();
262}
263
264void TrayMenu::displayPopup()
265{
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800266 trayIcon->showMessage("NDNx Status", statusXml);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700267}
268
269void TrayMenu::addFibEntry()
270{
271 QString name = dialog->getPrefixName();
272 QString tunnelType = dialog->getTunnelType();
273 QString endpoint = dialog->getEndpoint();
274
275 QStringList arguments;
276 arguments << "add" << name << tunnelType << endpoint;
277
278 dialog->hide();
279
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700280 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800281 connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700282 process->start(NDND_FIB_COMMAND, arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700283}
284
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700285void TrayMenu::closeEvent(QCloseEvent *event)
286{
287 if (trayIcon->isVisible())
288 {
289 hide();
290 event->ignore();
291 }
292}
293
294void TrayMenu::showEvent(QShowEvent * event)
295{
296 ui->generalSettingsWidget->show();
297 ui->forwardingSettingsWidget->hide();
298 ui->securitySettingsWidget->hide();
299}
300
301void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
302{
303 if(reason == QSystemTrayIcon::Trigger)
304 this->show();
305}
306
307void TrayMenu::setIcon(bool isConnected)
308{
309 if(isConnected)
310 trayIcon->setIcon(QIcon(":/resource/Resources/icon-connected-white.png"));
311 else
312 trayIcon->setIcon(QIcon(":/resource/Resources/icon-disconnected-white.png"));
313}
314
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700315void TrayMenu::daemonStatusUpdate()
316{
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -0800317 urlManager->get(QNetworkRequest(QUrl("http://localhost:6363/?f=xml")));
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800318}
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700319
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800320void TrayMenu::runXmlProc(QNetworkReply *reply)
321{
322 QByteArray buffer = reply->readAll();
323 applyStatusXslt = new QProcess();
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700324
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800325 QStringList arguments;
Alexander Afanasyeveb111572013-11-08 22:59:43 -0800326 arguments << QString(RESOURCES_DIR) + "/" + STATUS_XSLT_FILE << "-";
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800327 connect(applyStatusXslt,SIGNAL(finished(int)), this, SLOT(parseStatusXml()));
328 //connect(applyStatusXslt,SIGNAL(finished(int)), applyStatusXslt, SLOT(deleteLater()));
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -0800329 applyStatusXslt->start(XSLTPROC,arguments);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800330 applyStatusXslt->write(buffer);
331 applyStatusXslt->closeWriteChannel();
332
333 applyFibXslt = new QProcess();
334 QStringList args;
Alexander Afanasyeveb111572013-11-08 22:59:43 -0800335 args << QString(RESOURCES_DIR) + "/" + FIB_XSLT_FILE << "-";
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800336 connect(applyFibXslt,SIGNAL(finished(int)), this, SLOT(parseFibXml()));
337 //connect(applyFibXslt,SIGNAL(finished(int)), applyFibXslt, SLOT(deleteLater()));
Alexander Afanasyev669ea1a2013-11-08 22:25:23 -0800338 applyFibXslt->start(XSLTPROC,args);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800339 applyFibXslt->write(buffer);
340 applyFibXslt->closeWriteChannel();
341}
342
343void TrayMenu::parseStatusXml()
344{
345 QByteArray stdout = applyStatusXslt->readAllStandardOutput();
346 QByteArray stderr = applyStatusXslt->readAllStandardError();
347 applyStatusXslt->deleteLater();
348
349 statusXml = QString(stdout);
350
351 if((statusXml == "") || (stderr.length()>0)) // there was an error during Query evaluation
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700352 {
353 daemonStarted = false;
354 setIcon(false);
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800355 statusIndicator->setText("Inactive");
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700356
Alexander Afanasyev86240d02013-11-08 23:32:53 -0800357 // QProcess *process = new QProcess();
358 // if(enableHubDiscovery)
359 // connect(process, SIGNAL(finished(int)), networkManager, SLOT(autoconfigDaemon()));
360 //
361 // connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
362 // process->start(NDND_START_COMMAND);
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700363 }
364 else
365 {
366 daemonStarted = true;
367 setIcon(true);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800368 statusIndicator->setText("Active");
369
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700370 QString interestIn = statusXml.mid(statusXml.indexOf("<in>")+4, statusXml.indexOf("</in>") - (statusXml.indexOf("<in>")+4));
371 QString interestOut = statusXml.mid(statusXml.indexOf("<out>")+5, statusXml.indexOf("</out>") - (statusXml.indexOf("<out>")+5));
372 QString dataIn = statusXml.mid(statusXml.lastIndexOf("<in>")+4, statusXml.lastIndexOf("</in>") - (statusXml.lastIndexOf("<in>")+4));
373 QString dataOut = statusXml.mid(statusXml.lastIndexOf("<out>")+5, statusXml.lastIndexOf("</out>") - (statusXml.lastIndexOf("<out>")+5));
374
375 int i = 0;
376 int k = 0;
377 if((dataOut.length() - interestOut.length()) > 0)
378 {
379 i = dataOut.length() - interestOut.length();
380 i*=2; //because space takes less space than a letter
381 }
382
383 if((interestOut.length() - dataOut.length()) > 0)
384 {
385 k = interestOut.length() - dataOut.length();
386 k*=2; //because space takes less space than a letter
387 }
388
389 QString interestStats = QString("%1%2%3%4").arg("Interests",-16,' ').arg(interestOut,6+i,' ').arg(" / ",3).arg(interestIn,-6,' ');
390 QString dataStats = QString("%1%2%3%4").arg("Data",-20,' ').arg(dataOut,6+k,' ').arg(" / ",3).arg(dataIn,-6,' ');
391
392 //Now I try to align header "Sent / Recv" centrally with the upper line
393 QString padding;
394 for(int j = 0; j < interestStats.indexOf(interestOut); j++)
395 {
396 if(interestStats.at(j)==' ')
397 padding +=" ";
398 else
399 padding += " "; //because space takes less space than a letter
400 }
401
402 QString header;
403 int m = 0;
404 if(interestOut.length() - QString("Sent").length() > 0)
405 {
406 m = interestOut.length() - QString("Sent").length();
407 m *=3;
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700408 header = QString("%1%2").arg(padding).arg(" Sent / Recv",QString(" Sent / Recv").length() + m,' ');
409 }
410 else if(interestOut.length() - QString("Sent").length() < 0)
411 {
Ilya Moiseenko3adef432013-11-01 03:42:40 -0700412 padding.truncate(padding.length()-(QString("Sent").length() - interestOut.length()));
413 header = padding + "Sent / Recv";
414 }
415 else
416 {
417 header = padding + " Sent / Recv";
418 }
419
420 interestSentRecv->setText(interestStats);
421 dataSentRecv->setText(dataStats);
422 displayStatus->setText(header);
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700423 }
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800424}
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700425
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800426void TrayMenu::parseFibXml()
427{
428 QByteArray stdout = applyFibXslt->readAllStandardOutput();
429 QByteArray stderr = applyFibXslt->readAllStandardError();
430 applyFibXslt->deleteLater();
431
432 fibContentsXml = QString(stdout);
433
434 if((stdout == "") || (stderr.length()>0))
435 return;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700436
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700437 if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1))
438 {
439 networkManager->autoconfigDaemon();
440 }
441
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700442 QDomDocument xmldoc;
443 QDomElement root;
444
445 xmldoc.setContent(fibContentsXml);
446 root=xmldoc.documentElement();
447
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800448 if(model != NULL)
449 {
450 scrollPosition = ui->tableView->verticalScrollBar()->value();
451 model->clear();
452 delete model;
453 }
454
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700455 model = new QStandardItemModel(root.childNodes().count(),3);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800456 model->setHorizontalHeaderItem(0, new QStandardItem("NDN prefix"));
457 model->setHorizontalHeaderItem(1, new QStandardItem("Face ID"));
458 model->setHorizontalHeaderItem(2, new QStandardItem("Endpoint"));
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700459
460 int row = 0;
461 QDomNode fibEntry=root.firstChild();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800462
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700463 while (!fibEntry.isNull())
464 {
465 QDomNodeList properties = fibEntry.childNodes();
466
467 QDomNode faceID = properties.at(0);
468 QDomNode ip = properties.at(1);
469 QDomNode prefix = properties.at(2);
470
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700471 model->setItem(row, 0, new QStandardItem(prefix.toElement().text()));
472 model->setItem(row, 1, new QStandardItem(faceID.toElement().text()));
473 model->setItem(row, 2, new QStandardItem(ip.toElement().text()));
474
475 fibEntry = fibEntry.nextSibling();
476 row++;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800477 }
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700478
479 ui->tableView->setModel(model);
480
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800481 ui->tableView->setColumnWidth(0, ui->tableView->size().width() / 2 );
482 ui->tableView->setColumnWidth(1, ui->tableView->size().width() / 6 );
483
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700484 if(selectedRow >= 0)
485 ui->tableView->selectRow(selectedRow);
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800486
487 if(scrollPosition >= 0)
488 ui->tableView->verticalScrollBar()->setValue(scrollPosition);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700489}
490
491void TrayMenu::createTableView()
492{
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800493 ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
494 ui->tableView->horizontalHeader()->setStretchLastSection(true);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700495 ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
496 ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
497
498 selectedRow = -1;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800499 scrollPosition = -1;
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800500
501 QHBoxLayout *hlayout = new QHBoxLayout();
502 hlayout->addWidget(ui->addFibButton,0, Qt::AlignLeft);
503 hlayout->addWidget(ui->deleteFibButton, 20, Qt::AlignLeft);
504
505 QVBoxLayout *vlayout = new QVBoxLayout();
506 vlayout->addWidget(ui->label);
507 vlayout->addWidget(ui->tableView);
508 vlayout->addLayout(hlayout);
509
510 ui->forwardingSettingsWidget->setLayout(vlayout);
511
512 QVBoxLayout *expanding = new QVBoxLayout();
513 expanding->addWidget(ui->forwardingSettingsWidget);
514 ui->centralWidget->setLayout(expanding);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700515}
516
517void TrayMenu::selectTableRow()
518{
519 selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row();
520}
521
522void TrayMenu::deleteFibEntry()
523{
524 if(selectedRow < 0)
525 return;
526
527 QStandardItem *prefix = model->item(selectedRow,0);
528 if(prefix == NULL)
529 return;
530
531 QStandardItem *faceID = model->item(selectedRow,1);
532 if(faceID == NULL)
533 return;
534
535 QStringList arguments;
536 arguments << "del" << prefix->text() << "face" << faceID->text();
537
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700538 QProcess *process = new QProcess();
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800539 connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater()));
Ilya Moiseenko393cbdc2013-10-29 00:45:40 -0700540 process->start(NDND_FIB_COMMAND, arguments);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700541}
542
Ilya Moiseenkoa439f3d2013-11-09 18:41:46 -0800543void TrayMenu::resizeEvent(QResizeEvent * event)
544{
545 ui->tableView->setColumnWidth(0, ui->tableView->size().width() / 2);
546 ui->tableView->setColumnWidth(1, ui->tableView->size().width() / 6);
547}
548
549
550
551
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700552TrayMenu::~TrayMenu()
553{
554 daemonStatusTimer->stop();
Alexander Afanasyev5cce3ba2013-11-08 23:35:24 -0800555 statusUpdateThread->exit(0);
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700556 delete ui;
557 delete trayIcon;
558 delete trayIconMenu;
559 delete open;
560 delete close;
561 delete openGeneralSettings;
562 delete openForwardingSettings;
563 delete openSecuritySettings;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700564 delete statusUpdateThread;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700565 delete dialog;
Ilya Moiseenko69abb972013-11-04 16:02:20 -0800566 delete networkManager;
567 delete persistentSettings;
Ilya Moiseenko18c8a132013-10-24 01:52:52 -0700568}
Alexander Afanasyeva822b572013-11-04 12:36:34 -0800569
570#if WAF
571#include "tray-menu.moc"
572#include "tray-menu.cpp.moc"
573#endif