Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 8 | #include "config.h" |
| 9 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 10 | #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 Moiseenko | 656d52d | 2013-10-25 14:19:10 -0700 | [diff] [blame] | 14 | #include "network-manager.h" |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 15 | |
| 16 | #include <QMenu> |
| 17 | #include <QCloseEvent> |
| 18 | #include <QDesktopServices> |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 19 | #include <QUrl> |
| 20 | #include <QCheckBox> |
| 21 | #include <QLabel> |
| 22 | #include <QHBoxLayout> |
| 23 | #include <QLineEdit> |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 24 | #include <QDir> |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 25 | #include <QScrollBar> |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 26 | #include <QHBoxLayout> |
| 27 | #include <QVBoxLayout> |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 28 | |
| 29 | TrayMenu::TrayMenu(QWidget *parent) : |
| 30 | QMainWindow(parent), |
| 31 | ui(new Ui::TrayMenu) |
| 32 | { |
Ilya Moiseenko | 5a4e424 | 2013-11-10 12:54:50 -0800 | [diff] [blame] | 33 | processLookup = new QProcess(this); |
| 34 | QStringList arguments; |
| 35 | arguments << "-al"; |
| 36 | connect(processLookup,SIGNAL(finished(int)), this, SLOT(parseProcessLookup())); |
| 37 | processLookup->start("ps",arguments); |
| 38 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 39 | networkManager = new NetworkManager(); |
| 40 | |
| 41 | QCoreApplication::setOrganizationName("UCLA"); |
| 42 | QCoreApplication::setOrganizationDomain("named-data.net"); |
| 43 | QCoreApplication::setApplicationName("NDNx Control Center"); |
| 44 | |
| 45 | persistentSettings = new QSettings(); |
| 46 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 47 | ui->setupUi(this); |
| 48 | |
| 49 | createTrayIcon(); |
| 50 | setIcon(true); |
| 51 | createToolbar(); |
| 52 | createTableView(); |
| 53 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 54 | loadSettings(); |
Ilya Moiseenko | 656d52d | 2013-10-25 14:19:10 -0700 | [diff] [blame] | 55 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 56 | connect(ui->openTrafficMapButton, SIGNAL(pressed()), this, SLOT(openTrafficMap())); |
| 57 | connect(ui->openRoutingStatusButton, SIGNAL(pressed()), this, SLOT(openRoutingStatus())); |
| 58 | connect(ui->addFibButton, SIGNAL(pressed()), this, SLOT(showFibInputDialog())); |
| 59 | connect(ui->deleteFibButton, SIGNAL(released()), this, SLOT(deleteFibEntry())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 60 | connect(ui->hubDiscoveryCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeHubDiscovery())); |
| 61 | connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 62 | connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 63 | connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 64 | |
| 65 | statusUpdateThread = new QThread(); |
| 66 | statusUpdateThread->start(); |
| 67 | |
| 68 | daemonStatusTimer = new QTimer(statusUpdateThread); |
| 69 | connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate())); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 70 | daemonStatusTimer->start(2000); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 71 | |
| 72 | trayIcon->show(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 73 | model = NULL; |
| 74 | dialog = new FibInputDialog(this); |
| 75 | |
| 76 | urlManager = new QNetworkAccessManager(this); |
| 77 | connect(urlManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(runXmlProc(QNetworkReply*))); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 80 | void TrayMenu::loadSettings() |
| 81 | { |
| 82 | QVariant value; |
| 83 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 84 | value = persistentSettings->value(ENABLE_HUB_DISCOVERY); |
| 85 | if(!value.isNull()) |
| 86 | { |
| 87 | enableHubDiscovery = value.toBool(); |
| 88 | ui->hubDiscoveryCheckbox->setChecked(enableHubDiscovery); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | ui->hubDiscoveryCheckbox->setChecked(true); |
| 93 | changeHubDiscovery(); |
| 94 | } |
| 95 | |
| 96 | value = persistentSettings->value(ENABLE_START_ON_LOGIN); |
| 97 | if(!value.isNull()) |
| 98 | { |
| 99 | enableStartOnLogin = value.toBool(); |
| 100 | ui->loginStartCheckbox->setChecked(enableStartOnLogin); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | ui->loginStartCheckbox->setChecked(false); |
| 105 | changeLoginStart(); |
| 106 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void TrayMenu::changeHubDiscovery() |
| 110 | { |
| 111 | if(ui->hubDiscoveryCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 112 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 113 | enableHubDiscovery = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 114 | persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true); |
| 115 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 116 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 117 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 118 | enableHubDiscovery = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 119 | persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false); |
| 120 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void TrayMenu::changeLoginStart() |
| 124 | { |
| 125 | if(ui->loginStartCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 126 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 127 | enableStartOnLogin = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 128 | persistentSettings->setValue(ENABLE_START_ON_LOGIN, true); |
| 129 | makeAutostartDirectory(); |
| 130 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 131 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 132 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 133 | enableStartOnLogin = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 134 | persistentSettings->setValue(ENABLE_START_ON_LOGIN, false); |
| 135 | |
| 136 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 137 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 138 | QStringList arguments; |
| 139 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE; |
| 140 | process->start("rm", arguments); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void TrayMenu::makeAutostartDirectory() |
| 145 | { |
| 146 | QProcess *process = new QProcess(); |
| 147 | connect(process, SIGNAL(finished(int)), this, SLOT(copyFile())); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 148 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 149 | QStringList arguments; |
| 150 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY; |
| 151 | process->start("mkdir", arguments); |
| 152 | } |
| 153 | |
| 154 | void TrayMenu::copyFile() |
| 155 | { |
| 156 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 157 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 158 | QStringList arguments; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 159 | arguments << QString(RESOURCES_DIR) + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 160 | process->start("cp",arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 163 | void TrayMenu::showFibInputDialog() |
| 164 | { |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 165 | dialog->clear(); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 166 | dialog->exec(); |
| 167 | } |
| 168 | |
| 169 | void TrayMenu::openTrafficMap() |
| 170 | { |
| 171 | QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode)); |
| 172 | } |
| 173 | |
| 174 | void TrayMenu::openRoutingStatus() |
| 175 | { |
| 176 | QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode)); |
| 177 | } |
| 178 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 179 | void TrayMenu::openCertificationPage() |
| 180 | { |
| 181 | QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode)); |
| 182 | } |
| 183 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 184 | void TrayMenu::createTrayIcon() |
| 185 | { |
| 186 | trayIconMenu = new QMenu(this); |
| 187 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 188 | statusIndicator = new QAction("Inactive", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 189 | trayIconMenu->addAction(statusIndicator); |
| 190 | |
| 191 | trayIconMenu->addSeparator(); |
| 192 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 193 | displayStatus = new QAction(" Sent / Recv ", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 194 | trayIconMenu->addAction(displayStatus); |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 195 | interestSentRecv = new QAction("Interests 0 / 0", this); |
| 196 | trayIconMenu->addAction(interestSentRecv); |
| 197 | dataSentRecv = new QAction("Data 0 / 0", this); |
| 198 | trayIconMenu->addAction(dataSentRecv); |
| 199 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 200 | trayIconMenu->addSeparator(); |
| 201 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 202 | open = new QAction("Preferences...", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 203 | connect(open, SIGNAL(triggered()), this, SLOT(show())); |
| 204 | trayIconMenu->addAction(open); |
| 205 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 206 | close = new QAction("Quit", this); |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame] | 207 | // connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit())); |
| 208 | connect(close, SIGNAL(triggered()), qApp, SLOT(quit())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 209 | trayIconMenu->addAction(close); |
| 210 | |
| 211 | trayIcon = new QSystemTrayIcon(this); |
| 212 | trayIcon->setContextMenu(trayIconMenu); |
| 213 | |
| 214 | connect( |
| 215 | trayIcon, |
| 216 | SIGNAL(activated(QSystemTrayIcon::ActivationReason)), |
| 217 | this, |
| 218 | SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | void TrayMenu::createToolbar() |
| 223 | { |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 224 | ui->toolBar->setFloatable(false); |
| 225 | ui->toolBar->setMovable(false); |
| 226 | ui->toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); |
| 227 | ui->toolBar->setAllowedAreas(Qt::TopToolBarArea); |
| 228 | ui->toolBar->setOrientation(Qt::Horizontal); |
| 229 | ui->toolBar->setIconSize(QSize(32,32)); |
| 230 | ui->toolBar->resize(this->width(), 64); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 231 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 232 | openGeneralSettings = new QAction("General", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 233 | openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png")); |
| 234 | connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked())); |
| 235 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 236 | openForwardingSettings = new QAction("Forwarding", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 237 | openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png")); |
| 238 | connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked())); |
| 239 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 240 | openSecuritySettings = new QAction("Security", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 241 | openSecuritySettings->setIcon(QIcon(":/resource/Resources/emblem-system.png")); |
| 242 | connect(openSecuritySettings,SIGNAL(triggered()),this, SLOT(securitySettingsClicked())); |
| 243 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 244 | ui->toolBar->addAction(openGeneralSettings); |
| 245 | ui->toolBar->addAction(openForwardingSettings); |
| 246 | ui->toolBar->addAction(openSecuritySettings); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void TrayMenu::generalSettingsClicked() |
| 250 | { |
| 251 | ui->generalSettingsWidget->show(); |
| 252 | ui->forwardingSettingsWidget->hide(); |
| 253 | ui->securitySettingsWidget->hide(); |
| 254 | } |
| 255 | |
| 256 | void TrayMenu::forwardingSettingsClicked() |
| 257 | { |
| 258 | ui->generalSettingsWidget->hide(); |
| 259 | ui->forwardingSettingsWidget->show(); |
| 260 | ui->securitySettingsWidget->hide(); |
| 261 | } |
| 262 | |
| 263 | void TrayMenu::securitySettingsClicked() |
| 264 | { |
| 265 | ui->generalSettingsWidget->hide(); |
| 266 | ui->forwardingSettingsWidget->hide(); |
| 267 | ui->securitySettingsWidget->show(); |
| 268 | } |
| 269 | |
| 270 | void TrayMenu::displayPopup() |
| 271 | { |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 272 | trayIcon->showMessage("NDNx Status", statusXml); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void TrayMenu::addFibEntry() |
| 276 | { |
| 277 | QString name = dialog->getPrefixName(); |
| 278 | QString tunnelType = dialog->getTunnelType(); |
| 279 | QString endpoint = dialog->getEndpoint(); |
| 280 | |
| 281 | QStringList arguments; |
| 282 | arguments << "add" << name << tunnelType << endpoint; |
| 283 | |
| 284 | dialog->hide(); |
| 285 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 286 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 287 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 288 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 291 | void TrayMenu::closeEvent(QCloseEvent *event) |
| 292 | { |
| 293 | if (trayIcon->isVisible()) |
| 294 | { |
| 295 | hide(); |
| 296 | event->ignore(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void TrayMenu::showEvent(QShowEvent * event) |
| 301 | { |
| 302 | ui->generalSettingsWidget->show(); |
| 303 | ui->forwardingSettingsWidget->hide(); |
| 304 | ui->securitySettingsWidget->hide(); |
| 305 | } |
| 306 | |
| 307 | void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
| 308 | { |
| 309 | if(reason == QSystemTrayIcon::Trigger) |
| 310 | this->show(); |
| 311 | } |
| 312 | |
| 313 | void TrayMenu::setIcon(bool isConnected) |
| 314 | { |
| 315 | if(isConnected) |
| 316 | trayIcon->setIcon(QIcon(":/resource/Resources/icon-connected-white.png")); |
| 317 | else |
| 318 | trayIcon->setIcon(QIcon(":/resource/Resources/icon-disconnected-white.png")); |
| 319 | } |
| 320 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 321 | void TrayMenu::daemonStatusUpdate() |
| 322 | { |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 323 | urlManager->get(QNetworkRequest(QUrl("http://localhost:6363/?f=xml"))); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 324 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 325 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 326 | void TrayMenu::runXmlProc(QNetworkReply *reply) |
| 327 | { |
| 328 | QByteArray buffer = reply->readAll(); |
| 329 | applyStatusXslt = new QProcess(); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 330 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 331 | QStringList arguments; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 332 | arguments << QString(RESOURCES_DIR) + "/" + STATUS_XSLT_FILE << "-"; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 333 | connect(applyStatusXslt,SIGNAL(finished(int)), this, SLOT(parseStatusXml())); |
| 334 | //connect(applyStatusXslt,SIGNAL(finished(int)), applyStatusXslt, SLOT(deleteLater())); |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 335 | applyStatusXslt->start(XSLTPROC,arguments); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 336 | applyStatusXslt->write(buffer); |
| 337 | applyStatusXslt->closeWriteChannel(); |
| 338 | |
| 339 | applyFibXslt = new QProcess(); |
| 340 | QStringList args; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 341 | args << QString(RESOURCES_DIR) + "/" + FIB_XSLT_FILE << "-"; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 342 | connect(applyFibXslt,SIGNAL(finished(int)), this, SLOT(parseFibXml())); |
| 343 | //connect(applyFibXslt,SIGNAL(finished(int)), applyFibXslt, SLOT(deleteLater())); |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 344 | applyFibXslt->start(XSLTPROC,args); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 345 | applyFibXslt->write(buffer); |
| 346 | applyFibXslt->closeWriteChannel(); |
| 347 | } |
| 348 | |
| 349 | void TrayMenu::parseStatusXml() |
| 350 | { |
| 351 | QByteArray stdout = applyStatusXslt->readAllStandardOutput(); |
| 352 | QByteArray stderr = applyStatusXslt->readAllStandardError(); |
| 353 | applyStatusXslt->deleteLater(); |
| 354 | |
| 355 | statusXml = QString(stdout); |
| 356 | |
| 357 | if((statusXml == "") || (stderr.length()>0)) // there was an error during Query evaluation |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 358 | { |
| 359 | daemonStarted = false; |
| 360 | setIcon(false); |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame] | 361 | statusIndicator->setText("Inactive"); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 362 | |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame] | 363 | // QProcess *process = new QProcess(); |
| 364 | // if(enableHubDiscovery) |
| 365 | // connect(process, SIGNAL(finished(int)), networkManager, SLOT(autoconfigDaemon())); |
| 366 | // |
| 367 | // connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater())); |
| 368 | // process->start(NDND_START_COMMAND); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 369 | } |
| 370 | else |
| 371 | { |
| 372 | daemonStarted = true; |
| 373 | setIcon(true); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 374 | statusIndicator->setText("Active"); |
| 375 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 376 | QString interestIn = statusXml.mid(statusXml.indexOf("<in>")+4, statusXml.indexOf("</in>") - (statusXml.indexOf("<in>")+4)); |
| 377 | QString interestOut = statusXml.mid(statusXml.indexOf("<out>")+5, statusXml.indexOf("</out>") - (statusXml.indexOf("<out>")+5)); |
| 378 | QString dataIn = statusXml.mid(statusXml.lastIndexOf("<in>")+4, statusXml.lastIndexOf("</in>") - (statusXml.lastIndexOf("<in>")+4)); |
| 379 | QString dataOut = statusXml.mid(statusXml.lastIndexOf("<out>")+5, statusXml.lastIndexOf("</out>") - (statusXml.lastIndexOf("<out>")+5)); |
| 380 | |
| 381 | int i = 0; |
| 382 | int k = 0; |
| 383 | if((dataOut.length() - interestOut.length()) > 0) |
| 384 | { |
| 385 | i = dataOut.length() - interestOut.length(); |
| 386 | i*=2; //because space takes less space than a letter |
| 387 | } |
| 388 | |
| 389 | if((interestOut.length() - dataOut.length()) > 0) |
| 390 | { |
| 391 | k = interestOut.length() - dataOut.length(); |
| 392 | k*=2; //because space takes less space than a letter |
| 393 | } |
| 394 | |
| 395 | QString interestStats = QString("%1%2%3%4").arg("Interests",-16,' ').arg(interestOut,6+i,' ').arg(" / ",3).arg(interestIn,-6,' '); |
| 396 | QString dataStats = QString("%1%2%3%4").arg("Data",-20,' ').arg(dataOut,6+k,' ').arg(" / ",3).arg(dataIn,-6,' '); |
| 397 | |
| 398 | //Now I try to align header "Sent / Recv" centrally with the upper line |
| 399 | QString padding; |
| 400 | for(int j = 0; j < interestStats.indexOf(interestOut); j++) |
| 401 | { |
| 402 | if(interestStats.at(j)==' ') |
| 403 | padding +=" "; |
| 404 | else |
| 405 | padding += " "; //because space takes less space than a letter |
| 406 | } |
| 407 | |
| 408 | QString header; |
| 409 | int m = 0; |
| 410 | if(interestOut.length() - QString("Sent").length() > 0) |
| 411 | { |
| 412 | m = interestOut.length() - QString("Sent").length(); |
| 413 | m *=3; |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 414 | header = QString("%1%2").arg(padding).arg(" Sent / Recv",QString(" Sent / Recv").length() + m,' '); |
| 415 | } |
| 416 | else if(interestOut.length() - QString("Sent").length() < 0) |
| 417 | { |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 418 | padding.truncate(padding.length()-(QString("Sent").length() - interestOut.length())); |
| 419 | header = padding + "Sent / Recv"; |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | header = padding + " Sent / Recv"; |
| 424 | } |
| 425 | |
| 426 | interestSentRecv->setText(interestStats); |
| 427 | dataSentRecv->setText(dataStats); |
| 428 | displayStatus->setText(header); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 429 | } |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 430 | } |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 431 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 432 | void TrayMenu::parseFibXml() |
| 433 | { |
| 434 | QByteArray stdout = applyFibXslt->readAllStandardOutput(); |
| 435 | QByteArray stderr = applyFibXslt->readAllStandardError(); |
| 436 | applyFibXslt->deleteLater(); |
| 437 | |
| 438 | fibContentsXml = QString(stdout); |
| 439 | |
| 440 | if((stdout == "") || (stderr.length()>0)) |
| 441 | return; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 442 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 443 | if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1)) |
| 444 | { |
| 445 | networkManager->autoconfigDaemon(); |
| 446 | } |
| 447 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 448 | QDomDocument xmldoc; |
| 449 | QDomElement root; |
| 450 | |
| 451 | xmldoc.setContent(fibContentsXml); |
| 452 | root=xmldoc.documentElement(); |
| 453 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 454 | if(model != NULL) |
| 455 | { |
| 456 | scrollPosition = ui->tableView->verticalScrollBar()->value(); |
| 457 | model->clear(); |
| 458 | delete model; |
| 459 | } |
| 460 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 461 | model = new QStandardItemModel(root.childNodes().count(),3); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 462 | model->setHorizontalHeaderItem(0, new QStandardItem("NDN prefix")); |
| 463 | model->setHorizontalHeaderItem(1, new QStandardItem("Face ID")); |
| 464 | model->setHorizontalHeaderItem(2, new QStandardItem("Endpoint")); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 465 | |
| 466 | int row = 0; |
| 467 | QDomNode fibEntry=root.firstChild(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 468 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 469 | while (!fibEntry.isNull()) |
| 470 | { |
| 471 | QDomNodeList properties = fibEntry.childNodes(); |
| 472 | |
| 473 | QDomNode faceID = properties.at(0); |
| 474 | QDomNode ip = properties.at(1); |
| 475 | QDomNode prefix = properties.at(2); |
| 476 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 477 | model->setItem(row, 0, new QStandardItem(prefix.toElement().text())); |
| 478 | model->setItem(row, 1, new QStandardItem(faceID.toElement().text())); |
| 479 | model->setItem(row, 2, new QStandardItem(ip.toElement().text())); |
| 480 | |
| 481 | fibEntry = fibEntry.nextSibling(); |
| 482 | row++; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 483 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 484 | |
| 485 | ui->tableView->setModel(model); |
| 486 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 487 | ui->tableView->setColumnWidth(0, ui->tableView->size().width() / 2 ); |
| 488 | ui->tableView->setColumnWidth(1, ui->tableView->size().width() / 6 ); |
| 489 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 490 | if(selectedRow >= 0) |
| 491 | ui->tableView->selectRow(selectedRow); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 492 | |
| 493 | if(scrollPosition >= 0) |
| 494 | ui->tableView->verticalScrollBar()->setValue(scrollPosition); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void TrayMenu::createTableView() |
| 498 | { |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 499 | ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed); |
| 500 | ui->tableView->horizontalHeader()->setStretchLastSection(true); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 501 | ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 502 | ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 503 | |
| 504 | selectedRow = -1; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 505 | scrollPosition = -1; |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 506 | |
| 507 | QHBoxLayout *hlayout = new QHBoxLayout(); |
| 508 | hlayout->addWidget(ui->addFibButton,0, Qt::AlignLeft); |
| 509 | hlayout->addWidget(ui->deleteFibButton, 20, Qt::AlignLeft); |
| 510 | |
| 511 | QVBoxLayout *vlayout = new QVBoxLayout(); |
| 512 | vlayout->addWidget(ui->label); |
| 513 | vlayout->addWidget(ui->tableView); |
| 514 | vlayout->addLayout(hlayout); |
| 515 | |
| 516 | ui->forwardingSettingsWidget->setLayout(vlayout); |
| 517 | |
| 518 | QVBoxLayout *expanding = new QVBoxLayout(); |
| 519 | expanding->addWidget(ui->forwardingSettingsWidget); |
| 520 | ui->centralWidget->setLayout(expanding); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void TrayMenu::selectTableRow() |
| 524 | { |
| 525 | selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row(); |
| 526 | } |
| 527 | |
| 528 | void TrayMenu::deleteFibEntry() |
| 529 | { |
| 530 | if(selectedRow < 0) |
| 531 | return; |
| 532 | |
| 533 | QStandardItem *prefix = model->item(selectedRow,0); |
| 534 | if(prefix == NULL) |
| 535 | return; |
| 536 | |
| 537 | QStandardItem *faceID = model->item(selectedRow,1); |
| 538 | if(faceID == NULL) |
| 539 | return; |
| 540 | |
| 541 | QStringList arguments; |
| 542 | arguments << "del" << prefix->text() << "face" << faceID->text(); |
| 543 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 544 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 545 | connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 546 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 549 | void TrayMenu::resizeEvent(QResizeEvent * event) |
| 550 | { |
| 551 | ui->tableView->setColumnWidth(0, ui->tableView->size().width() / 2); |
| 552 | ui->tableView->setColumnWidth(1, ui->tableView->size().width() / 6); |
| 553 | } |
| 554 | |
| 555 | |
Ilya Moiseenko | 5a4e424 | 2013-11-10 12:54:50 -0800 | [diff] [blame] | 556 | void TrayMenu::parseProcessLookup() |
| 557 | { |
| 558 | QByteArray stdout = processLookup->readAllStandardOutput(); |
| 559 | processLookup->deleteLater(); |
| 560 | |
| 561 | QString s = QString(stdout); |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 562 | |
Ilya Moiseenko | 5a4e424 | 2013-11-10 12:54:50 -0800 | [diff] [blame] | 563 | if(s == "") |
| 564 | return; |
| 565 | else if(s.indexOf("ndnx-control") != s.lastIndexOf("ndnx-control")) |
| 566 | qApp->quit(); |
| 567 | } |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 568 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 569 | TrayMenu::~TrayMenu() |
| 570 | { |
| 571 | daemonStatusTimer->stop(); |
Alexander Afanasyev | 5cce3ba | 2013-11-08 23:35:24 -0800 | [diff] [blame] | 572 | statusUpdateThread->exit(0); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 573 | delete ui; |
| 574 | delete trayIcon; |
| 575 | delete trayIconMenu; |
| 576 | delete open; |
| 577 | delete close; |
| 578 | delete openGeneralSettings; |
| 579 | delete openForwardingSettings; |
| 580 | delete openSecuritySettings; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 581 | delete statusUpdateThread; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 582 | delete dialog; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 583 | delete networkManager; |
| 584 | delete persistentSettings; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 585 | } |
Alexander Afanasyev | a822b57 | 2013-11-04 12:36:34 -0800 | [diff] [blame] | 586 | |
| 587 | #if WAF |
| 588 | #include "tray-menu.moc" |
| 589 | #include "tray-menu.cpp.moc" |
| 590 | #endif |