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> |
| 26 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 27 | |
| 28 | TrayMenu::TrayMenu(QWidget *parent) : |
| 29 | QMainWindow(parent), |
| 30 | ui(new Ui::TrayMenu) |
| 31 | { |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 32 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 40 | ui->setupUi(this); |
| 41 | |
| 42 | createTrayIcon(); |
| 43 | setIcon(true); |
| 44 | createToolbar(); |
| 45 | createTableView(); |
| 46 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 47 | loadSettings(); |
Ilya Moiseenko | 656d52d | 2013-10-25 14:19:10 -0700 | [diff] [blame] | 48 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 49 | 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 58 | connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 59 | |
| 60 | statusUpdateThread = new QThread(); |
| 61 | statusUpdateThread->start(); |
| 62 | |
| 63 | daemonStatusTimer = new QTimer(statusUpdateThread); |
| 64 | connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate())); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 65 | daemonStatusTimer->start(2000); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 66 | |
| 67 | trayIcon->show(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 68 | model = NULL; |
| 69 | dialog = new FibInputDialog(this); |
| 70 | |
| 71 | urlManager = new QNetworkAccessManager(this); |
| 72 | connect(urlManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(runXmlProc(QNetworkReply*))); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 75 | void 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 128 | void TrayMenu::changeSoftwareUpdate() |
| 129 | { |
| 130 | if(ui->softwareUpdateCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 131 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 132 | allowAutomaticUpdates = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 133 | persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, true); |
| 134 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 135 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 136 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 137 | allowAutomaticUpdates = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 138 | persistentSettings->setValue(ALLOW_SOFTWARE_UPDATES, false); |
| 139 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void TrayMenu::changeHubDiscovery() |
| 143 | { |
| 144 | if(ui->hubDiscoveryCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 145 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 146 | enableHubDiscovery = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 147 | persistentSettings->setValue(ENABLE_HUB_DISCOVERY, true); |
| 148 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 149 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 150 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 151 | enableHubDiscovery = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 152 | persistentSettings->setValue(ENABLE_HUB_DISCOVERY, false); |
| 153 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void TrayMenu::changeLoginStart() |
| 157 | { |
| 158 | if(ui->loginStartCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 159 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 160 | enableStartOnLogin = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 161 | persistentSettings->setValue(ENABLE_START_ON_LOGIN, true); |
| 162 | makeAutostartDirectory(); |
| 163 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 164 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 165 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 166 | enableStartOnLogin = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 167 | persistentSettings->setValue(ENABLE_START_ON_LOGIN, false); |
| 168 | |
| 169 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 170 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 171 | QStringList arguments; |
| 172 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE; |
| 173 | process->start("rm", arguments); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void TrayMenu::makeAutostartDirectory() |
| 178 | { |
| 179 | QProcess *process = new QProcess(); |
| 180 | connect(process, SIGNAL(finished(int)), this, SLOT(copyFile())); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 181 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 182 | QStringList arguments; |
| 183 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY; |
| 184 | process->start("mkdir", arguments); |
| 185 | } |
| 186 | |
| 187 | void TrayMenu::copyFile() |
| 188 | { |
| 189 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 190 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 191 | QStringList arguments; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 192 | arguments << QString(RESOURCES_DIR) + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 193 | process->start("cp",arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void TrayMenu::changeShutdownExit() |
| 197 | { |
| 198 | if(ui->shutdownCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 199 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 200 | shutdownOnExit = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 201 | persistentSettings->setValue(SHUTDOWN_ON_EXIT, true); |
| 202 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 203 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 204 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 205 | shutdownOnExit = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 206 | persistentSettings->setValue(SHUTDOWN_ON_EXIT, false); |
| 207 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void TrayMenu::showFibInputDialog() |
| 211 | { |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 212 | dialog->clear(); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 213 | dialog->exec(); |
| 214 | } |
| 215 | |
| 216 | void TrayMenu::openTrafficMap() |
| 217 | { |
| 218 | QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode)); |
| 219 | } |
| 220 | |
| 221 | void TrayMenu::openRoutingStatus() |
| 222 | { |
| 223 | QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode)); |
| 224 | } |
| 225 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 226 | void TrayMenu::openCertificationPage() |
| 227 | { |
| 228 | QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode)); |
| 229 | } |
| 230 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 231 | void TrayMenu::createTrayIcon() |
| 232 | { |
| 233 | trayIconMenu = new QMenu(this); |
| 234 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 235 | statusIndicator = new QAction("Inactive", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 236 | trayIconMenu->addAction(statusIndicator); |
| 237 | |
| 238 | trayIconMenu->addSeparator(); |
| 239 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 240 | displayStatus = new QAction(" Sent / Recv ", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 241 | trayIconMenu->addAction(displayStatus); |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 242 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 247 | trayIconMenu->addSeparator(); |
| 248 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 249 | open = new QAction("Preferences...", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 250 | connect(open, SIGNAL(triggered()), this, SLOT(show())); |
| 251 | trayIconMenu->addAction(open); |
| 252 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 253 | close = new QAction("Quit...", this); |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame^] | 254 | // connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit())); |
| 255 | connect(close, SIGNAL(triggered()), qApp, SLOT(quit())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 256 | 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 | |
| 269 | void 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 Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 280 | openGeneralSettings = new QAction("General", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 281 | openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png")); |
| 282 | connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked())); |
| 283 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 284 | openForwardingSettings = new QAction("Forwarding", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 285 | openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png")); |
| 286 | connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked())); |
| 287 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 288 | openSecuritySettings = new QAction("Security", this); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 289 | 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 | |
| 297 | void TrayMenu::generalSettingsClicked() |
| 298 | { |
| 299 | ui->generalSettingsWidget->show(); |
| 300 | ui->forwardingSettingsWidget->hide(); |
| 301 | ui->securitySettingsWidget->hide(); |
| 302 | } |
| 303 | |
| 304 | void TrayMenu::forwardingSettingsClicked() |
| 305 | { |
| 306 | ui->generalSettingsWidget->hide(); |
| 307 | ui->forwardingSettingsWidget->show(); |
| 308 | ui->securitySettingsWidget->hide(); |
| 309 | } |
| 310 | |
| 311 | void TrayMenu::securitySettingsClicked() |
| 312 | { |
| 313 | ui->generalSettingsWidget->hide(); |
| 314 | ui->forwardingSettingsWidget->hide(); |
| 315 | ui->securitySettingsWidget->show(); |
| 316 | } |
| 317 | |
| 318 | void TrayMenu::displayPopup() |
| 319 | { |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 320 | trayIcon->showMessage("NDNx Status", statusXml); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 334 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 335 | connect(process,SIGNAL(finished(int)),process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 336 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame^] | 339 | // void TrayMenu::confirmQuit() |
| 340 | // { |
| 341 | // if(shutdownOnExit) |
| 342 | // terminateDaemonAndClose(); |
| 343 | // else |
| 344 | // { |
| 345 | // QuitDialog dialog(this); |
| 346 | // dialog.exec(); |
| 347 | // } |
| 348 | // } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 349 | |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame^] | 350 | // 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 357 | |
| 358 | void TrayMenu::closeEvent(QCloseEvent *event) |
| 359 | { |
| 360 | if (trayIcon->isVisible()) |
| 361 | { |
| 362 | hide(); |
| 363 | event->ignore(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void TrayMenu::showEvent(QShowEvent * event) |
| 368 | { |
| 369 | ui->generalSettingsWidget->show(); |
| 370 | ui->forwardingSettingsWidget->hide(); |
| 371 | ui->securitySettingsWidget->hide(); |
| 372 | } |
| 373 | |
| 374 | void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
| 375 | { |
| 376 | if(reason == QSystemTrayIcon::Trigger) |
| 377 | this->show(); |
| 378 | } |
| 379 | |
| 380 | void 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 388 | void TrayMenu::daemonStatusUpdate() |
| 389 | { |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 390 | urlManager->get(QNetworkRequest(QUrl("http://localhost:6363/?f=xml"))); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 391 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 392 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 393 | void TrayMenu::runXmlProc(QNetworkReply *reply) |
| 394 | { |
| 395 | QByteArray buffer = reply->readAll(); |
| 396 | applyStatusXslt = new QProcess(); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 397 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 398 | QStringList arguments; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 399 | arguments << QString(RESOURCES_DIR) + "/" + STATUS_XSLT_FILE << "-"; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 400 | connect(applyStatusXslt,SIGNAL(finished(int)), this, SLOT(parseStatusXml())); |
| 401 | //connect(applyStatusXslt,SIGNAL(finished(int)), applyStatusXslt, SLOT(deleteLater())); |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 402 | applyStatusXslt->start(XSLTPROC,arguments); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 403 | applyStatusXslt->write(buffer); |
| 404 | applyStatusXslt->closeWriteChannel(); |
| 405 | |
| 406 | applyFibXslt = new QProcess(); |
| 407 | QStringList args; |
Alexander Afanasyev | eb11157 | 2013-11-08 22:59:43 -0800 | [diff] [blame] | 408 | args << QString(RESOURCES_DIR) + "/" + FIB_XSLT_FILE << "-"; |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 409 | connect(applyFibXslt,SIGNAL(finished(int)), this, SLOT(parseFibXml())); |
| 410 | //connect(applyFibXslt,SIGNAL(finished(int)), applyFibXslt, SLOT(deleteLater())); |
Alexander Afanasyev | 669ea1a | 2013-11-08 22:25:23 -0800 | [diff] [blame] | 411 | applyFibXslt->start(XSLTPROC,args); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 412 | applyFibXslt->write(buffer); |
| 413 | applyFibXslt->closeWriteChannel(); |
| 414 | } |
| 415 | |
| 416 | void 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 425 | { |
| 426 | daemonStarted = false; |
| 427 | setIcon(false); |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame^] | 428 | statusIndicator->setText("Inactive"); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 429 | |
Alexander Afanasyev | 86240d0 | 2013-11-08 23:32:53 -0800 | [diff] [blame^] | 430 | // 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 436 | } |
| 437 | else |
| 438 | { |
| 439 | daemonStarted = true; |
| 440 | setIcon(true); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 441 | statusIndicator->setText("Active"); |
| 442 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 443 | 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 Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 481 | 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 Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame] | 485 | 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 496 | } |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 497 | } |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 498 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 499 | void 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 509 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 510 | if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1)) |
| 511 | { |
| 512 | networkManager->autoconfigDaemon(); |
| 513 | } |
| 514 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 515 | QDomDocument xmldoc; |
| 516 | QDomElement root; |
| 517 | |
| 518 | xmldoc.setContent(fibContentsXml); |
| 519 | root=xmldoc.documentElement(); |
| 520 | |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 521 | if(model != NULL) |
| 522 | { |
| 523 | scrollPosition = ui->tableView->verticalScrollBar()->value(); |
| 524 | model->clear(); |
| 525 | delete model; |
| 526 | } |
| 527 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 528 | model = new QStandardItemModel(root.childNodes().count(),3); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 529 | model->setHorizontalHeaderItem(0, new QStandardItem("NDN prefix")); |
| 530 | model->setHorizontalHeaderItem(1, new QStandardItem("Face ID")); |
| 531 | model->setHorizontalHeaderItem(2, new QStandardItem("Endpoint")); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 532 | |
| 533 | int row = 0; |
| 534 | QDomNode fibEntry=root.firstChild(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 535 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 536 | 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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 544 | 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 Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 550 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 551 | |
| 552 | ui->tableView->setModel(model); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 553 | //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 Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 558 | |
| 559 | if(selectedRow >= 0) |
| 560 | ui->tableView->selectRow(selectedRow); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 561 | |
| 562 | if(scrollPosition >= 0) |
| 563 | ui->tableView->verticalScrollBar()->setValue(scrollPosition); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void 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 Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 573 | scrollPosition = -1; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void TrayMenu::selectTableRow() |
| 577 | { |
| 578 | selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row(); |
| 579 | } |
| 580 | |
| 581 | void 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 Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 597 | QProcess *process = new QProcess(); |
Ilya Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 598 | connect(process,SIGNAL(finished(int)), process, SLOT(deleteLater())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 599 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | TrayMenu::~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 Moiseenko | 69abb97 | 2013-11-04 16:02:20 -0800 | [diff] [blame] | 618 | delete networkManager; |
| 619 | delete persistentSettings; |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 620 | } |
Alexander Afanasyev | a822b57 | 2013-11-04 12:36:34 -0800 | [diff] [blame] | 621 | |
| 622 | #if WAF |
| 623 | #include "tray-menu.moc" |
| 624 | #include "tray-menu.cpp.moc" |
| 625 | #endif |