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> |
| 17 | #include <QMessageBox> |
| 18 | #include <QUrl> |
| 19 | #include <QCheckBox> |
| 20 | #include <QLabel> |
| 21 | #include <QHBoxLayout> |
| 22 | #include <QLineEdit> |
| 23 | #include <QProcess> |
| 24 | #include <QXmlStreamReader> |
| 25 | #include <QXmlQuery> |
| 26 | #include <QDebug> |
| 27 | #include <QtXml> |
| 28 | #include <QStandardItemModel> |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 29 | #include <QDir> |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame^] | 30 | #include <QWidgetAction> |
| 31 | #include <QTextStream> |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 32 | |
| 33 | TrayMenu::TrayMenu(QWidget *parent) : |
| 34 | QMainWindow(parent), |
| 35 | ui(new Ui::TrayMenu) |
| 36 | { |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 37 | networkManager = new NetworkManager(); |
| 38 | |
| 39 | QCoreApplication::setOrganizationName("UCLA"); |
| 40 | QCoreApplication::setOrganizationDomain("named-data.net"); |
| 41 | QCoreApplication::setApplicationName("NDNx Control Center"); |
| 42 | |
| 43 | persistentSettings = new QSettings(); |
| 44 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 45 | ui->setupUi(this); |
| 46 | |
| 47 | createTrayIcon(); |
| 48 | setIcon(true); |
| 49 | createToolbar(); |
| 50 | createTableView(); |
| 51 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 52 | loadSettings(); |
Ilya Moiseenko | 656d52d | 2013-10-25 14:19:10 -0700 | [diff] [blame] | 53 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 54 | connect(ui->openTrafficMapButton, SIGNAL(pressed()), this, SLOT(openTrafficMap())); |
| 55 | connect(ui->openRoutingStatusButton, SIGNAL(pressed()), this, SLOT(openRoutingStatus())); |
| 56 | connect(ui->addFibButton, SIGNAL(pressed()), this, SLOT(showFibInputDialog())); |
| 57 | connect(ui->deleteFibButton, SIGNAL(released()), this, SLOT(deleteFibEntry())); |
| 58 | connect(ui->softwareUpdateCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeSoftwareUpdate())); |
| 59 | connect(ui->hubDiscoveryCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeHubDiscovery())); |
| 60 | connect(ui->loginStartCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeLoginStart())); |
| 61 | connect(ui->shutdownCheckbox, SIGNAL(stateChanged(int)), this, SLOT(changeShutdownExit())); |
| 62 | connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectTableRow())); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 63 | connect(ui->openNdnCertificationButton, SIGNAL(released()), this, SLOT(openCertificationPage())); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 64 | |
| 65 | statusUpdateThread = new QThread(); |
| 66 | statusUpdateThread->start(); |
| 67 | |
| 68 | daemonStatusTimer = new QTimer(statusUpdateThread); |
| 69 | connect(daemonStatusTimer, SIGNAL(timeout()), this, SLOT(daemonStatusUpdate())); |
| 70 | daemonStatusTimer->start(1000); |
| 71 | |
| 72 | trayIcon->show(); |
| 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(); |
| 170 | QStringList arguments; |
| 171 | arguments << QDir::homePath() + AUTOSTART_DIRECTORY + SHORTCUT_FILE; |
| 172 | process->start("rm", arguments); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void TrayMenu::makeAutostartDirectory() |
| 177 | { |
| 178 | QProcess *process = new QProcess(); |
| 179 | connect(process, SIGNAL(finished(int)), this, SLOT(copyFile())); |
| 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(); |
| 188 | QStringList arguments; |
| 189 | arguments << QApplication::applicationDirPath() + "/" + SHORTCUT_FILE << QDir::homePath() + AUTOSTART_DIRECTORY; |
| 190 | process->start("cp",arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void TrayMenu::changeShutdownExit() |
| 194 | { |
| 195 | if(ui->shutdownCheckbox->isChecked()) |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 196 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 197 | shutdownOnExit = true; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 198 | persistentSettings->setValue(SHUTDOWN_ON_EXIT, true); |
| 199 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 200 | else |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 201 | { |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 202 | shutdownOnExit = false; |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 203 | persistentSettings->setValue(SHUTDOWN_ON_EXIT, false); |
| 204 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void TrayMenu::showFibInputDialog() |
| 208 | { |
| 209 | dialog = new FibInputDialog(this); |
| 210 | dialog->exec(); |
| 211 | } |
| 212 | |
| 213 | void TrayMenu::openTrafficMap() |
| 214 | { |
| 215 | QDesktopServices::openUrl(QUrl("http://ndnmap.arl.wustl.edu/", QUrl::TolerantMode)); |
| 216 | } |
| 217 | |
| 218 | void TrayMenu::openRoutingStatus() |
| 219 | { |
| 220 | QDesktopServices::openUrl(QUrl("http://netlab.cs.memphis.edu/script/htm/status.htm", QUrl::TolerantMode)); |
| 221 | } |
| 222 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 223 | void TrayMenu::openCertificationPage() |
| 224 | { |
| 225 | QDesktopServices::openUrl(QUrl("http://ndncert.named-data.net", QUrl::TolerantMode)); |
| 226 | } |
| 227 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 228 | void TrayMenu::createTrayIcon() |
| 229 | { |
| 230 | trayIconMenu = new QMenu(this); |
| 231 | |
| 232 | statusIndicator = new QAction(tr("Inactive"), this); |
| 233 | trayIconMenu->addAction(statusIndicator); |
| 234 | |
| 235 | trayIconMenu->addSeparator(); |
| 236 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame^] | 237 | displayStatus = new QAction(" Sent / Recv ", this); |
| 238 | //connect(displayStatus, SIGNAL(triggered()), this, SLOT(displayPopup())); |
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 | |
| 246 | trayIconMenu->addSeparator(); |
| 247 | |
| 248 | open = new QAction(tr("Preferences..."), this); |
| 249 | connect(open, SIGNAL(triggered()), this, SLOT(show())); |
| 250 | trayIconMenu->addAction(open); |
| 251 | |
| 252 | close = new QAction(tr("Quit..."), this); |
| 253 | connect(close, SIGNAL(triggered()), this, SLOT(confirmQuit())); |
| 254 | trayIconMenu->addAction(close); |
| 255 | |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame^] | 256 | |
| 257 | |
| 258 | |
| 259 | /*QWidgetAction * wa = new QWidgetAction(this); |
| 260 | wa->setDefaultWidget(new QPushButton("Default")); |
| 261 | |
| 262 | trayIconMenu->setDefaultAction(wa); |
| 263 | */ |
| 264 | //trayIconMenu->addAction(); |
| 265 | /*QVBoxLayout *layout = new QVBoxLayout(wa->defaultWidget()); |
| 266 | |
| 267 | QLineEdit *edit = new QLineEdit("", wa->defaultWidget()); |
| 268 | layout->addWidget(edit); |
| 269 | |
| 270 | trayIconMenu->addAction(wa);*/ |
| 271 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 272 | trayIcon = new QSystemTrayIcon(this); |
| 273 | trayIcon->setContextMenu(trayIconMenu); |
| 274 | |
| 275 | connect( |
| 276 | trayIcon, |
| 277 | SIGNAL(activated(QSystemTrayIcon::ActivationReason)), |
| 278 | this, |
| 279 | SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)) |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | void TrayMenu::createToolbar() |
| 284 | { |
| 285 | toolBar = new QToolBar(this); |
| 286 | toolBar->setFloatable(false); |
| 287 | toolBar->setMovable(false); |
| 288 | toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); |
| 289 | toolBar->setAllowedAreas(Qt::TopToolBarArea); |
| 290 | toolBar->setOrientation(Qt::Horizontal); |
| 291 | toolBar->setIconSize(QSize(32,32)); |
| 292 | toolBar->resize(this->width(), 64); |
| 293 | |
| 294 | openGeneralSettings = new QAction(tr("General"), this); |
| 295 | openGeneralSettings->setIcon(QIcon(":/resource/Resources/preferences-desktop.png")); |
| 296 | connect(openGeneralSettings,SIGNAL(triggered()),this, SLOT(generalSettingsClicked())); |
| 297 | |
| 298 | openForwardingSettings = new QAction(tr("Forwarding"), this); |
| 299 | openForwardingSettings->setIcon(QIcon(":/resource/Resources/emblem-web.png")); |
| 300 | connect(openForwardingSettings,SIGNAL(triggered()),this, SLOT(forwardingSettingsClicked())); |
| 301 | |
| 302 | openSecuritySettings = new QAction(tr("Security"), this); |
| 303 | openSecuritySettings->setIcon(QIcon(":/resource/Resources/emblem-system.png")); |
| 304 | connect(openSecuritySettings,SIGNAL(triggered()),this, SLOT(securitySettingsClicked())); |
| 305 | |
| 306 | toolBar->addAction(openGeneralSettings); |
| 307 | toolBar->addAction(openForwardingSettings); |
| 308 | toolBar->addAction(openSecuritySettings); |
| 309 | } |
| 310 | |
| 311 | void TrayMenu::generalSettingsClicked() |
| 312 | { |
| 313 | ui->generalSettingsWidget->show(); |
| 314 | ui->forwardingSettingsWidget->hide(); |
| 315 | ui->securitySettingsWidget->hide(); |
| 316 | } |
| 317 | |
| 318 | void TrayMenu::forwardingSettingsClicked() |
| 319 | { |
| 320 | ui->generalSettingsWidget->hide(); |
| 321 | ui->forwardingSettingsWidget->show(); |
| 322 | ui->securitySettingsWidget->hide(); |
| 323 | } |
| 324 | |
| 325 | void TrayMenu::securitySettingsClicked() |
| 326 | { |
| 327 | ui->generalSettingsWidget->hide(); |
| 328 | ui->forwardingSettingsWidget->hide(); |
| 329 | ui->securitySettingsWidget->show(); |
| 330 | } |
| 331 | |
| 332 | void TrayMenu::displayPopup() |
| 333 | { |
| 334 | trayIcon->showMessage(tr("NDNx Status"), statusXml); |
| 335 | } |
| 336 | |
| 337 | void TrayMenu::addFibEntry() |
| 338 | { |
| 339 | QString name = dialog->getPrefixName(); |
| 340 | QString tunnelType = dialog->getTunnelType(); |
| 341 | QString endpoint = dialog->getEndpoint(); |
| 342 | |
| 343 | QStringList arguments; |
| 344 | arguments << "add" << name << tunnelType << endpoint; |
| 345 | |
| 346 | dialog->hide(); |
| 347 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 348 | QProcess *process = new QProcess(); |
| 349 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void TrayMenu::confirmQuit() |
| 353 | { |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 354 | if(shutdownOnExit) |
| 355 | terminateDaemonAndClose(); |
| 356 | else |
| 357 | { |
| 358 | QuitDialog dialog(this); |
| 359 | dialog.exec(); |
| 360 | } |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void TrayMenu::terminateDaemonAndClose() |
| 364 | { |
| 365 | QProcess *process = new QProcess(this); |
| 366 | process->start(NDND_STOP_COMMAND); |
| 367 | connect(process,SIGNAL(finished(int)), qApp, SLOT(quit())); |
| 368 | } |
| 369 | |
| 370 | void TrayMenu::closeEvent(QCloseEvent *event) |
| 371 | { |
| 372 | if (trayIcon->isVisible()) |
| 373 | { |
| 374 | hide(); |
| 375 | event->ignore(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void TrayMenu::showEvent(QShowEvent * event) |
| 380 | { |
| 381 | ui->generalSettingsWidget->show(); |
| 382 | ui->forwardingSettingsWidget->hide(); |
| 383 | ui->securitySettingsWidget->hide(); |
| 384 | } |
| 385 | |
| 386 | void TrayMenu::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
| 387 | { |
| 388 | if(reason == QSystemTrayIcon::Trigger) |
| 389 | this->show(); |
| 390 | } |
| 391 | |
| 392 | void TrayMenu::setIcon(bool isConnected) |
| 393 | { |
| 394 | if(isConnected) |
| 395 | trayIcon->setIcon(QIcon(":/resource/Resources/icon-connected-white.png")); |
| 396 | else |
| 397 | trayIcon->setIcon(QIcon(":/resource/Resources/icon-disconnected-white.png")); |
| 398 | } |
| 399 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 400 | void TrayMenu::daemonStatusUpdate() |
| 401 | { |
| 402 | QXmlQuery query(QXmlQuery::XSLT20); |
| 403 | query.setFocus(QUrl("http://localhost:9695/?f=xml")); |
| 404 | |
| 405 | query.setQuery(QUrl("qrc:/resource/Resources/status.xslt")); // TODO: I suspect it's being read from HDD each time |
| 406 | query.evaluateTo(&statusXml); |
| 407 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 408 | if(statusXml == "") // there was an error during Query evaluation |
| 409 | { |
| 410 | daemonStarted = false; |
| 411 | setIcon(false); |
| 412 | statusIndicator->setText(tr("Starting...")); |
| 413 | |
| 414 | QProcess *process = new QProcess(); |
| 415 | if(enableHubDiscovery) |
| 416 | connect(process, SIGNAL(finished(int)), networkManager, SLOT(autoconfigDaemon())); |
| 417 | process->start(NDND_START_COMMAND); |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | daemonStarted = true; |
| 422 | setIcon(true); |
| 423 | statusIndicator->setText(tr("Active")); |
Ilya Moiseenko | 3adef43 | 2013-11-01 03:42:40 -0700 | [diff] [blame^] | 424 | //qDebug() << statusXml; |
| 425 | QString interestIn = statusXml.mid(statusXml.indexOf("<in>")+4, statusXml.indexOf("</in>") - (statusXml.indexOf("<in>")+4)); |
| 426 | QString interestOut = statusXml.mid(statusXml.indexOf("<out>")+5, statusXml.indexOf("</out>") - (statusXml.indexOf("<out>")+5)); |
| 427 | QString dataIn = statusXml.mid(statusXml.lastIndexOf("<in>")+4, statusXml.lastIndexOf("</in>") - (statusXml.lastIndexOf("<in>")+4)); |
| 428 | QString dataOut = statusXml.mid(statusXml.lastIndexOf("<out>")+5, statusXml.lastIndexOf("</out>") - (statusXml.lastIndexOf("<out>")+5)); |
| 429 | |
| 430 | int i = 0; |
| 431 | int k = 0; |
| 432 | if((dataOut.length() - interestOut.length()) > 0) |
| 433 | { |
| 434 | i = dataOut.length() - interestOut.length(); |
| 435 | i*=2; //because space takes less space than a letter |
| 436 | } |
| 437 | |
| 438 | if((interestOut.length() - dataOut.length()) > 0) |
| 439 | { |
| 440 | k = interestOut.length() - dataOut.length(); |
| 441 | k*=2; //because space takes less space than a letter |
| 442 | } |
| 443 | |
| 444 | QString interestStats = QString("%1%2%3%4").arg("Interests",-16,' ').arg(interestOut,6+i,' ').arg(" / ",3).arg(interestIn,-6,' '); |
| 445 | QString dataStats = QString("%1%2%3%4").arg("Data",-20,' ').arg(dataOut,6+k,' ').arg(" / ",3).arg(dataIn,-6,' '); |
| 446 | |
| 447 | //Now I try to align header "Sent / Recv" centrally with the upper line |
| 448 | QString padding; |
| 449 | for(int j = 0; j < interestStats.indexOf(interestOut); j++) |
| 450 | { |
| 451 | if(interestStats.at(j)==' ') |
| 452 | padding +=" "; |
| 453 | else |
| 454 | padding += " "; //because space takes less space than a letter |
| 455 | } |
| 456 | |
| 457 | QString header; |
| 458 | int m = 0; |
| 459 | if(interestOut.length() - QString("Sent").length() > 0) |
| 460 | { |
| 461 | m = interestOut.length() - QString("Sent").length(); |
| 462 | m *=3; |
| 463 | //qDebug() << "m=" << m; |
| 464 | header = QString("%1%2").arg(padding).arg(" Sent / Recv",QString(" Sent / Recv").length() + m,' '); |
| 465 | } |
| 466 | else if(interestOut.length() - QString("Sent").length() < 0) |
| 467 | { |
| 468 | //qDebug() << "truncating"; |
| 469 | padding.truncate(padding.length()-(QString("Sent").length() - interestOut.length())); |
| 470 | header = padding + "Sent / Recv"; |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | header = padding + " Sent / Recv"; |
| 475 | } |
| 476 | |
| 477 | interestSentRecv->setText(interestStats); |
| 478 | dataSentRecv->setText(dataStats); |
| 479 | displayStatus->setText(header); |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 482 | query.setQuery(QUrl("qrc:/resource/Resources/status-to-fib.xslt")); // TODO: I suspect it's being read from HDD each time |
| 483 | query.evaluateTo(&fibContentsXml); |
| 484 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 485 | if ((enableHubDiscovery) && (fibContentsXml.indexOf("ndn:/autoconf-route",0,Qt::CaseInsensitive) == -1)) |
| 486 | { |
| 487 | networkManager->autoconfigDaemon(); |
| 488 | } |
| 489 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 490 | QDomDocument xmldoc; |
| 491 | QDomElement root; |
| 492 | |
| 493 | xmldoc.setContent(fibContentsXml); |
| 494 | root=xmldoc.documentElement(); |
| 495 | |
| 496 | model = new QStandardItemModel(root.childNodes().count(),3); |
| 497 | model->setHorizontalHeaderItem(0, new QStandardItem(tr("NDN prefix"))); |
| 498 | model->setHorizontalHeaderItem(1, new QStandardItem(tr("Face ID"))); |
| 499 | model->setHorizontalHeaderItem(2, new QStandardItem(tr("Endpoint"))); |
| 500 | |
| 501 | int row = 0; |
| 502 | QDomNode fibEntry=root.firstChild(); |
| 503 | while (!fibEntry.isNull()) |
| 504 | { |
| 505 | QDomNodeList properties = fibEntry.childNodes(); |
| 506 | |
| 507 | QDomNode faceID = properties.at(0); |
| 508 | QDomNode ip = properties.at(1); |
| 509 | QDomNode prefix = properties.at(2); |
| 510 | |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 511 | model->setItem(row, 0, new QStandardItem(prefix.toElement().text())); |
| 512 | model->setItem(row, 1, new QStandardItem(faceID.toElement().text())); |
| 513 | model->setItem(row, 2, new QStandardItem(ip.toElement().text())); |
| 514 | |
| 515 | fibEntry = fibEntry.nextSibling(); |
| 516 | row++; |
| 517 | } |
| 518 | |
| 519 | ui->tableView->setModel(model); |
| 520 | |
| 521 | if(selectedRow >= 0) |
| 522 | ui->tableView->selectRow(selectedRow); |
| 523 | } |
| 524 | |
| 525 | void TrayMenu::createTableView() |
| 526 | { |
| 527 | ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); |
| 528 | ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 529 | ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 530 | |
| 531 | selectedRow = -1; |
| 532 | } |
| 533 | |
| 534 | void TrayMenu::selectTableRow() |
| 535 | { |
| 536 | selectedRow = ui->tableView->selectionModel()->selection().indexes().at(0).row(); |
| 537 | } |
| 538 | |
| 539 | void TrayMenu::deleteFibEntry() |
| 540 | { |
| 541 | if(selectedRow < 0) |
| 542 | return; |
| 543 | |
| 544 | QStandardItem *prefix = model->item(selectedRow,0); |
| 545 | if(prefix == NULL) |
| 546 | return; |
| 547 | |
| 548 | QStandardItem *faceID = model->item(selectedRow,1); |
| 549 | if(faceID == NULL) |
| 550 | return; |
| 551 | |
| 552 | QStringList arguments; |
| 553 | arguments << "del" << prefix->text() << "face" << faceID->text(); |
| 554 | |
Ilya Moiseenko | 393cbdc | 2013-10-29 00:45:40 -0700 | [diff] [blame] | 555 | QProcess *process = new QProcess(); |
| 556 | process->start(NDND_FIB_COMMAND, arguments); |
Ilya Moiseenko | 18c8a13 | 2013-10-24 01:52:52 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | TrayMenu::~TrayMenu() |
| 560 | { |
| 561 | daemonStatusTimer->stop(); |
| 562 | statusUpdateThread->exit(); |
| 563 | delete ui; |
| 564 | delete trayIcon; |
| 565 | delete trayIconMenu; |
| 566 | delete open; |
| 567 | delete close; |
| 568 | delete openGeneralSettings; |
| 569 | delete openForwardingSettings; |
| 570 | delete openSecuritySettings; |
| 571 | delete toolBar; |
| 572 | delete statusUpdateThread; |
| 573 | delete daemonStatusTimer; |
| 574 | delete dialog; |
| 575 | } |