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 | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 33 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 41 | ui->setupUi(this); |
| 42 | |
| 43 | createTrayIcon(); |
| 44 | setIcon(true); |
| 45 | createToolbar(); |
| 46 | createTableView(); |
| 47 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 48 | loadSettings(); |
Ilya Moiseenko | 656d52d | 2013-10-25 14:19:10 -0700 | [diff] [blame] | 49 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 50 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 54 | connect(ui->hubDiscoveryCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeHubDiscovery())); |
| 55 | connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 56 | connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 57 | connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 58 | |
| 59 | statusUpdateThread = new QThread(); |
| 60 | statusUpdateThread->start(); |
| 61 | |
| 62 | daemonStatusTimer = new QTimer(statusUpdateThread); |
| 63 | connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate())); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 64 | daemonStatusTimer->start(2000); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 65 | |
| 66 | trayIcon->show(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 67 | model = NULL; |
| 68 | dialog = new FibInputDialog(this); |
| 69 | |
| 70 | urlManager = new QNetworkAccessManager(this); |
| 71 | connect(urlManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(runXmlProc(QNetworkReply*))); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 74 | void TrayMenu::loadSettings() |
| 75 | { |
| 76 | QVariant value; |
| 77 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 78 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void TrayMenu::changeHubDiscovery() |
| 104 | { |
| 105 | if(ui->hubDiscoveryCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 106 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 107 | enableHubDiscovery = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 108 | persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true); |
| 109 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 110 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 111 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 112 | enableHubDiscovery = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 113 | persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false); |
| 114 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void TrayMenu::changeLoginStart() |
| 118 | { |
| 119 | if(ui->loginStartCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 120 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 121 | enableStartOnLogin = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 122 | persistentSettings->setValue(ENABLE_START_ON_LOGIN, true); |
| 123 | makeAutostartDirectory(); |
| 124 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 125 | else |
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 = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 128 | persistentSettings->setValue(ENABLE_START_ON_LOGIN, false); |
| 129 | |
| 130 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 131 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 132 | QStringList arguments; |
| 133 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE; |
| 134 | process->start("rm", arguments); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void TrayMenu::makeAutostartDirectory() |
| 139 | { |
| 140 | QProcess *process = new QProcess(); |
| 141 | connect(process, SIGNAL(finished(int)), this, SLOT(copyFile())); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 142 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 143 | QStringList arguments; |
| 144 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY; |
| 145 | process->start("mkdir", arguments); |
| 146 | } |
| 147 | |
| 148 | void TrayMenu::copyFile() |
| 149 | { |
| 150 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 151 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 152 | QStringList arguments; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 153 | arguments << QString(RESOURCES_DIR) + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 154 | process->start("cp",arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 157 | void TrayMenu::showFibInputDialog() |
| 158 | { |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 159 | dialog->clear(); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 160 | dialog->exec(); |
| 161 | } |
| 162 | |
| 163 | void TrayMenu::openTrafficMap() |
| 164 | { |
| 165 | QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode)); |
| 166 | } |
| 167 | |
| 168 | void TrayMenu::openRoutingStatus() |
| 169 | { |
| 170 | QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode)); |
| 171 | } |
| 172 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 173 | void TrayMenu::openCertificationPage() |
| 174 | { |
| 175 | QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode)); |
| 176 | } |
| 177 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 178 | void TrayMenu::createTrayIcon() |
| 179 | { |
| 180 | trayIconMenu = new QMenu(this); |
| 181 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 182 | statusIndicator = new QAction("Inactive", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 183 | trayIconMenu->addAction(statusIndicator); |
| 184 | |
| 185 | trayIconMenu->addSeparator(); |
| 186 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 187 | displayStatus = new QAction(" Sent / Recv ", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 188 | trayIconMenu->addAction(displayStatus); |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 189 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 194 | trayIconMenu->addSeparator(); |
| 195 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 196 | open = new QAction("Preferences...", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 197 | connect(open, SIGNAL(triggered()), this, SLOT(show())); |
| 198 | trayIconMenu->addAction(open); |
| 199 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 200 | close = new QAction("Quit", this); |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame] | 201 | // connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit())); |
| 202 | connect(close, SIGNAL(triggered()), qApp, SLOT(quit())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 203 | 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 | |
| 216 | void TrayMenu::createToolbar() |
| 217 | { |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 218 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 225 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 226 | openGeneralSettings = new QAction("General", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 227 | openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png")); |
| 228 | connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked())); |
| 229 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 230 | openForwardingSettings = new QAction("Forwarding", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 231 | openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png")); |
| 232 | connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked())); |
| 233 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 234 | openSecuritySettings = new QAction("Security", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 235 | openSecuritySettings->setIcon(QIcon(":/resource/Resources/emblem-system.png")); |
| 236 | connect(openSecuritySettings,SIGNAL(triggered()),this, SLOT(securitySettingsClicked())); |
| 237 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 238 | ui->toolBar->addAction(openGeneralSettings); |
| 239 | ui->toolBar->addAction(openForwardingSettings); |
| 240 | ui->toolBar->addAction(openSecuritySettings); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void TrayMenu::generalSettingsClicked() |
| 244 | { |
| 245 | ui->generalSettingsWidget->show(); |
| 246 | ui->forwardingSettingsWidget->hide(); |
| 247 | ui->securitySettingsWidget->hide(); |
| 248 | } |
| 249 | |
| 250 | void TrayMenu::forwardingSettingsClicked() |
| 251 | { |
| 252 | ui->generalSettingsWidget->hide(); |
| 253 | ui->forwardingSettingsWidget->show(); |
| 254 | ui->securitySettingsWidget->hide(); |
| 255 | } |
| 256 | |
| 257 | void TrayMenu::securitySettingsClicked() |
| 258 | { |
| 259 | ui->generalSettingsWidget->hide(); |
| 260 | ui->forwardingSettingsWidget->hide(); |
| 261 | ui->securitySettingsWidget->show(); |
| 262 | } |
| 263 | |
| 264 | void TrayMenu::displayPopup() |
| 265 | { |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 266 | trayIcon->showMessage("NDNx Status", statusXml); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 280 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 281 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 282 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 285 | void TrayMenu::closeEvent(QCloseEvent *event) |
| 286 | { |
| 287 | if (trayIcon->isVisible()) |
| 288 | { |
| 289 | hide(); |
| 290 | event->ignore(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void TrayMenu::showEvent(QShowEvent * event) |
| 295 | { |
| 296 | ui->generalSettingsWidget->show(); |
| 297 | ui->forwardingSettingsWidget->hide(); |
| 298 | ui->securitySettingsWidget->hide(); |
| 299 | } |
| 300 | |
| 301 | void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
| 302 | { |
| 303 | if(reason == QSystemTrayIcon::Trigger) |
| 304 | this->show(); |
| 305 | } |
| 306 | |
| 307 | void 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 315 | void TrayMenu::daemonStatusUpdate() |
| 316 | { |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 317 | urlManager->get(QNetworkRequest(QUrl("http://localhost:6363/?f=xml"))); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 318 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 319 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 320 | void TrayMenu::runXmlProc(QNetworkReply *reply) |
| 321 | { |
| 322 | QByteArray buffer = reply->readAll(); |
| 323 | applyStatusXslt = new QProcess(); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 324 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 325 | QStringList arguments; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 326 | arguments << QString(RESOURCES_DIR) + "/" + STATUS_XSLT_FILE << "-"; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 327 | connect(applyStatusXslt,SIGNAL(finished(int)), this, SLOT(parseStatusXml())); |
| 328 | //connect(applyStatusXslt,SIGNAL(finished(int)), applyStatusXslt, SLOT(deleteLater())); |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 329 | applyStatusXslt->start(XSLTPROC,arguments); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 330 | applyStatusXslt->write(buffer); |
| 331 | applyStatusXslt->closeWriteChannel(); |
| 332 | |
| 333 | applyFibXslt = new QProcess(); |
| 334 | QStringList args; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 335 | args << QString(RESOURCES_DIR) + "/" + FIB_XSLT_FILE << "-"; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 336 | connect(applyFibXslt,SIGNAL(finished(int)), this, SLOT(parseFibXml())); |
| 337 | //connect(applyFibXslt,SIGNAL(finished(int)), applyFibXslt, SLOT(deleteLater())); |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 338 | applyFibXslt->start(XSLTPROC,args); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 339 | applyFibXslt->write(buffer); |
| 340 | applyFibXslt->closeWriteChannel(); |
| 341 | } |
| 342 | |
| 343 | void 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 352 | { |
| 353 | daemonStarted = false; |
| 354 | setIcon(false); |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame] | 355 | statusIndicator->setText("Inactive"); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame] | 357 | // 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
| 366 | daemonStarted = true; |
| 367 | setIcon(true); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 368 | statusIndicator->setText("Active"); |
| 369 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 370 | 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 Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 408 | 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 Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 412 | 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 423 | } |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 424 | } |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 425 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 426 | void 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 436 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 437 | if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1)) |
| 438 | { |
| 439 | networkManager->autoconfigDaemon(); |
| 440 | } |
| 441 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 442 | QDomDocument xmldoc; |
| 443 | QDomElement root; |
| 444 | |
| 445 | xmldoc.setContent(fibContentsXml); |
| 446 | root=xmldoc.documentElement(); |
| 447 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 448 | if(model != NULL) |
| 449 | { |
| 450 | scrollPosition = ui->tableView->verticalScrollBar()->value(); |
| 451 | model->clear(); |
| 452 | delete model; |
| 453 | } |
| 454 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 455 | model = new QStandardItemModel(root.childNodes().count(),3); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 456 | model->setHorizontalHeaderItem(0, new QStandardItem("NDN prefix")); |
| 457 | model->setHorizontalHeaderItem(1, new QStandardItem("Face ID")); |
| 458 | model->setHorizontalHeaderItem(2, new QStandardItem("Endpoint")); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 459 | |
| 460 | int row = 0; |
| 461 | QDomNode fibEntry=root.firstChild(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 462 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 463 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 471 | 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 Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 477 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 478 | |
| 479 | ui->tableView->setModel(model); |
| 480 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 481 | ui->tableView->setColumnWidth(0, ui->tableView->size().width() / 2 ); |
| 482 | ui->tableView->setColumnWidth(1, ui->tableView->size().width() / 6 ); |
| 483 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 484 | if(selectedRow >= 0) |
| 485 | ui->tableView->selectRow(selectedRow); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 486 | |
| 487 | if(scrollPosition >= 0) |
| 488 | ui->tableView->verticalScrollBar()->setValue(scrollPosition); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void TrayMenu::createTableView() |
| 492 | { |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 493 | ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed); |
| 494 | ui->tableView->horizontalHeader()->setStretchLastSection(true); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 495 | ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 496 | ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 497 | |
| 498 | selectedRow = -1; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 499 | scrollPosition = -1; |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 500 | |
| 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void TrayMenu::selectTableRow() |
| 518 | { |
| 519 | selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row(); |
| 520 | } |
| 521 | |
| 522 | void 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 538 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 539 | connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 540 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Ilya Moiseenko | a439f3d | 2013-11-09 18:41:46 -0800 | [diff] [blame] | 543 | void 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 552 | TrayMenu::~TrayMenu() |
| 553 | { |
| 554 | daemonStatusTimer->stop(); |
Alexander Afanasyev | 5cce3ba | 2013-11-08 23:35:24 -0800 | [diff] [blame] | 555 | statusUpdateThread->exit(0); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 556 | delete ui; |
| 557 | delete trayIcon; |
| 558 | delete trayIconMenu; |
| 559 | delete open; |
| 560 | delete close; |
| 561 | delete openGeneralSettings; |
| 562 | delete openForwardingSettings; |
| 563 | delete openSecuritySettings; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 564 | delete statusUpdateThread; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 565 | delete dialog; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 566 | delete networkManager; |
| 567 | delete persistentSettings; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 568 | } |
Alexander Afanasyev | a822b57 | 2013-11-04 12:36:34 -0800 | [diff] [blame] | 569 | |
| 570 | #if WAF |
| 571 | #include "tray-menu.moc" |
| 572 | #include "tray-menu.cpp.moc" |
| 573 | #endif |