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