| 1 |
/* |
| 2 |
This file is part of Icecream. |
| 3 |
|
| 4 |
Copyright (c) 2003 Frerich Raabe <[email protected]> |
| 5 |
Copyright (c) 2003,2004 Stephan Kulow <[email protected]> |
| 6 |
Copyright (c) 2003,2004 Cornelius Schumacher <[email protected]> |
| 7 |
Copyright (c) 2014 Kevin Funk <[email protected]> |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License along |
| 20 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 21 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mainwindow.h" |
| 25 |
|
| 26 |
#include "hostinfo.h" |
| 27 |
#include "version.h" |
| 28 |
#include "fakemonitor.h" |
| 29 |
#include "icecreammonitor.h" |
| 30 |
#include "statusview.h" |
| 31 |
#include "statusviewfactory.h" |
| 32 |
|
| 33 |
#include <QCloseEvent> |
| 34 |
#include <QDebug> |
| 35 |
#include <QLabel> |
| 36 |
#include <QMenuBar> |
| 37 |
#include <QStatusBar> |
| 38 |
#include <QMessageBox> |
| 39 |
#include <QApplication> |
| 40 |
#include <QSettings> |
| 41 |
#include <QMenu> |
| 42 |
#include <QActionGroup> |
| 43 |
|
| 44 |
#include <algorithm> |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
struct PlatformStat |
| 49 |
{ |
| 50 |
unsigned int jobs{0}; |
| 51 |
unsigned int maxJobs{0}; |
| 52 |
}; |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
MainWindow::MainWindow(QWidget *parent) |
| 57 |
: QMainWindow(parent) |
| 58 |
{ |
| 59 |
QIcon appIcon = QIcon(); |
| 60 |
appIcon.addFile(QStringLiteral(":/images/128-apps-icemon.png"), QSize(128, 128)); |
| 61 |
appIcon.addFile(QStringLiteral(":/images/48-apps-icemon.png"), QSize(48, 48)); |
| 62 |
appIcon.addFile(QStringLiteral(":/images/32-apps-icemon.png"), QSize(32, 32)); |
| 63 |
appIcon.addFile(QStringLiteral(":/images/22-apps-icemon.png"), QSize(22, 22)); |
| 64 |
appIcon.addFile(QStringLiteral(":/images/16-apps-icemon.png"), QSize(16, 16)); |
| 65 |
setWindowIcon(appIcon); |
| 66 |
setWindowTitle(QApplication::translate("appName", Icemon::Version::appName)); |
| 67 |
|
| 68 |
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
| 69 |
QMenu *viewMenu = menuBar()->addMenu(tr("&View")); |
| 70 |
QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); |
| 71 |
|
| 72 |
m_schedStatusWidget = new QLabel; |
| 73 |
statusBar()->addPermanentWidget(m_schedStatusWidget); |
| 74 |
|
| 75 |
m_jobStatsWidget = new QLabel; |
| 76 |
m_jobStatsWidget->setVisible(false); |
| 77 |
statusBar()->addPermanentWidget(m_jobStatsWidget); |
| 78 |
|
| 79 |
QAction *action = nullptr; |
| 80 |
|
| 81 |
if (QSystemTrayIcon::isSystemTrayAvailable()) |
| 82 |
{ |
| 83 |
action = fileMenu->addAction(tr("Show in System Tray")); |
| 84 |
action->setIcon(QIcon::fromTheme(QStringLiteral("systemtray"))); |
| 85 |
action->setCheckable(true); |
| 86 |
connect(action, &QAction::triggered, this, &MainWindow::updateSystemTrayVisible); |
| 87 |
m_showInSystemTrayAction = action; |
| 88 |
|
| 89 |
auto *systrayMenu = new QMenu(this); |
| 90 |
QAction *quitAction = systrayMenu->addAction(tr("&Quit"), this, SLOT(quit()), tr("Ctrl+Q")); |
| 91 |
quitAction->setIcon(QIcon::fromTheme(QStringLiteral("application-exit"))); |
| 92 |
quitAction->setMenuRole(QAction::QuitRole); |
| 93 |
|
| 94 |
m_systemTrayIcon = new QSystemTrayIcon(this); |
| 95 |
m_systemTrayIcon->setIcon(appIcon); |
| 96 |
m_systemTrayIcon->setToolTip(windowTitle()); |
| 97 |
m_systemTrayIcon->setContextMenu(systrayMenu); |
| 98 |
connect(m_systemTrayIcon, &QSystemTrayIcon::activated, this, &MainWindow::systemTrayIconActivated); |
| 99 |
|
| 100 |
fileMenu->addSeparator(); |
| 101 |
} |
| 102 |
|
| 103 |
action = fileMenu->addAction(tr("&Quit"), this, &QWidget::close, tr("Ctrl+Q")); |
| 104 |
action->setIcon(QIcon::fromTheme(QStringLiteral("application-exit"))); |
| 105 |
action->setMenuRole(QAction::QuitRole); |
| 106 |
|
| 107 |
m_viewMode = new QActionGroup(this); |
| 108 |
action = m_viewMode->addAction(tr("&List View")); |
| 109 |
action->setCheckable(true); |
| 110 |
action->setData(QStringLiteral("list")); |
| 111 |
action = m_viewMode->addAction(tr("&Star View")); |
| 112 |
action->setCheckable(true); |
| 113 |
action->setData(QStringLiteral("star")); |
| 114 |
action = m_viewMode->addAction(tr("&Gantt View")); |
| 115 |
action->setCheckable(true); |
| 116 |
action->setData(QStringLiteral("gantt")); |
| 117 |
action = m_viewMode->addAction(tr("Summary &View")); |
| 118 |
action->setCheckable(true); |
| 119 |
action->setData(QStringLiteral("summary")); |
| 120 |
action = m_viewMode->addAction(tr("&Flow View")); |
| 121 |
action->setCheckable(true); |
| 122 |
action->setData(QStringLiteral("flow")); |
| 123 |
action = m_viewMode->addAction(tr("&Detailed Host View")); |
| 124 |
action->setCheckable(true); |
| 125 |
action->setData(QStringLiteral("detailedhost")); |
| 126 |
connect(m_viewMode, &QActionGroup::triggered, this, &MainWindow::handleViewModeActionTriggered); |
| 127 |
viewMenu->addActions(m_viewMode->actions()); |
| 128 |
|
| 129 |
viewMenu->addSeparator(); |
| 130 |
|
| 131 |
action = viewMenu->addAction(tr("Pause")); |
| 132 |
action->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause"))); |
| 133 |
action->setCheckable(true); |
| 134 |
connect(action, &QAction::triggered, this, &MainWindow::pauseView); |
| 135 |
m_pauseViewAction = action; |
| 136 |
|
| 137 |
viewMenu->addSeparator(); |
| 138 |
|
| 139 |
action = viewMenu->addAction(tr("Configure View...")); |
| 140 |
action->setIcon(QIcon::fromTheme(QStringLiteral("configure"))); |
| 141 |
connect(action, &QAction::triggered, this, &MainWindow::configureView); |
| 142 |
m_configureViewAction = action; |
| 143 |
|
| 144 |
action = helpMenu->addAction(tr("About Qt...")); |
| 145 |
connect(action, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
| 146 |
action->setMenuRole(QAction::AboutQtRole); |
| 147 |
|
| 148 |
action = helpMenu->addAction(tr("About...")); |
| 149 |
action->setIcon(appIcon); |
| 150 |
connect(action, &QAction::triggered, this, &MainWindow::about); |
| 151 |
action->setMenuRole(QAction::AboutRole); |
| 152 |
|
| 153 |
m_hostInfoManager = new HostInfoManager; |
| 154 |
setMonitor(new IcecreamMonitor(m_hostInfoManager, this)); |
| 155 |
|
| 156 |
resize(600, 400); |
| 157 |
readSettings(); |
| 158 |
} |
| 159 |
|
| 160 |
MainWindow::~MainWindow() |
| 161 |
{ |
| 162 |
delete m_hostInfoManager; |
| 163 |
} |
| 164 |
|
| 165 |
void MainWindow::closeEvent(QCloseEvent *e) |
| 166 |
{ |
| 167 |
if (m_systemTrayIcon && m_systemTrayIcon->isVisible()) |
| 168 |
{ |
| 169 |
QSettings settings; |
| 170 |
const bool shownBefore = settings.value(QStringLiteral("programWillKeepRunningWarningShown")).toBool(); |
| 171 |
if (!shownBefore) { |
| 172 |
QMessageBox::information(this, tr("Systray"), |
| 173 |
tr("The program will keep running in the " |
| 174 |
"system tray. To terminate the program, " |
| 175 |
"choose <b>Quit</b> in the context menu " |
| 176 |
"of the system tray entry.")); |
| 177 |
settings.setValue(QStringLiteral("programWillKeepRunningWarningShown"), true); |
| 178 |
settings.sync(); |
| 179 |
} |
| 180 |
|
| 181 |
hide(); |
| 182 |
e->ignore(); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
writeSettings(); |
| 187 |
|
| 188 |
QMainWindow::closeEvent(e); |
| 189 |
} |
| 190 |
|
| 191 |
void MainWindow::readSettings() |
| 192 |
{ |
| 193 |
QSettings settings; |
| 194 |
restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray()); |
| 195 |
restoreState(settings.value(QStringLiteral("windowState")).toByteArray()); |
| 196 |
bool showSystemTray = settings.value(QStringLiteral("showSystemTray")).toBool(); |
| 197 |
QString viewId = settings.value(QStringLiteral("currentView")).toString(); |
| 198 |
|
| 199 |
auto view = StatusViewFactory::create(viewId, this); |
| 200 |
setView(view); |
| 201 |
|
| 202 |
if (m_systemTrayIcon) |
| 203 |
{ |
| 204 |
m_showInSystemTrayAction->setChecked(showSystemTray); |
| 205 |
updateSystemTrayVisible(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void MainWindow::writeSettings() |
| 210 |
{ |
| 211 |
QSettings settings; |
| 212 |
settings.setValue(QStringLiteral("geometry"), saveGeometry()); |
| 213 |
settings.setValue(QStringLiteral("windowState"), saveState()); |
| 214 |
if (m_systemTrayIcon) |
| 215 |
{ |
| 216 |
settings.setValue(QStringLiteral("showSystemTray"), m_systemTrayIcon->isVisible()); |
| 217 |
} |
| 218 |
settings.setValue(QStringLiteral("currentView"), (m_view ? m_view->id() : QString())); |
| 219 |
settings.sync(); |
| 220 |
} |
| 221 |
|
| 222 |
Monitor *MainWindow::monitor() const |
| 223 |
{ |
| 224 |
return m_monitor; |
| 225 |
} |
| 226 |
|
| 227 |
void MainWindow::setMonitor(Monitor *monitor) |
| 228 |
{ |
| 229 |
if (m_monitor == monitor) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if (m_monitor) { |
| 234 |
disconnect(m_monitor.data(), &Monitor::schedulerStateChanged, |
| 235 |
this, &MainWindow::updateSchedulerState); |
| 236 |
disconnect(m_monitor.data(), &Monitor::jobUpdated, this, &MainWindow::updateJob); |
| 237 |
disconnect(m_monitor->hostInfoManager(), &HostInfoManager::hostMapChanged, this, &MainWindow::updateJobStats); |
| 238 |
} |
| 239 |
|
| 240 |
m_monitor = monitor; |
| 241 |
|
| 242 |
if (m_monitor) { |
| 243 |
connect(m_monitor.data(), &Monitor::schedulerStateChanged, |
| 244 |
this, &MainWindow::updateSchedulerState); |
| 245 |
connect(m_monitor.data(), &Monitor::jobUpdated, this, &MainWindow::updateJob); |
| 246 |
connect(m_monitor->hostInfoManager(), &HostInfoManager::hostMapChanged, this, &MainWindow::updateJobStats); |
| 247 |
} |
| 248 |
|
| 249 |
if (m_view) { |
| 250 |
m_view->setMonitor(m_monitor); |
| 251 |
} |
| 252 |
updateSchedulerState(m_monitor ? m_monitor->schedulerState() : Monitor::Offline); |
| 253 |
} |
| 254 |
|
| 255 |
StatusView *MainWindow::view() const |
| 256 |
{ |
| 257 |
return m_view; |
| 258 |
} |
| 259 |
|
| 260 |
void MainWindow::setView(StatusView *view) |
| 261 |
{ |
| 262 |
if (m_view == view) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
delete m_view; |
| 267 |
|
| 268 |
m_view = view; |
| 269 |
|
| 270 |
if (m_view) { |
| 271 |
m_configureViewAction->setEnabled(m_view->isConfigurable()); |
| 272 |
m_pauseViewAction->setEnabled(m_view->isPausable()); |
| 273 |
m_view->setMonitor(m_monitor); |
| 274 |
|
| 275 |
setCentralWidget(m_view->widget()); |
| 276 |
} |
| 277 |
|
| 278 |
// update action-group |
| 279 |
const QString viewId = (m_view ? m_view->id() : QString()); |
| 280 |
foreach(QAction * action, m_viewMode->actions()) { |
| 281 |
if (action->data().toString() == viewId) { |
| 282 |
action->setChecked(true); |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void MainWindow::pauseView() |
| 289 |
{ |
| 290 |
m_view->togglePause(); |
| 291 |
} |
| 292 |
|
| 293 |
void MainWindow::configureView() |
| 294 |
{ |
| 295 |
m_view->configureView(); |
| 296 |
} |
| 297 |
|
| 298 |
void MainWindow::updateSystemTrayVisible() |
| 299 |
{ |
| 300 |
if (!m_systemTrayIcon) |
| 301 |
{ |
| 302 |
return; |
| 303 |
} |
| 304 |
if (m_showInSystemTrayAction->isChecked()) { |
| 305 |
m_systemTrayIcon->show(); |
| 306 |
} else { |
| 307 |
m_systemTrayIcon->hide(); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
void MainWindow::quit() |
| 312 |
{ |
| 313 |
writeSettings(); |
| 314 |
qApp->quit(); |
| 315 |
} |
| 316 |
|
| 317 |
void MainWindow::systemTrayIconActivated(QSystemTrayIcon::ActivationReason reason) |
| 318 |
{ |
| 319 |
switch (reason) { |
| 320 |
case QSystemTrayIcon::Trigger: |
| 321 |
if (isVisible()) { |
| 322 |
if (isMinimized()) { |
| 323 |
showMaximized(); |
| 324 |
} else { |
| 325 |
hide(); |
| 326 |
} |
| 327 |
} else { |
| 328 |
show(); |
| 329 |
} |
| 330 |
break; |
| 331 |
|
| 332 |
case QSystemTrayIcon::DoubleClick: |
| 333 |
case QSystemTrayIcon::MiddleClick: |
| 334 |
case QSystemTrayIcon::Unknown: |
| 335 |
default: |
| 336 |
; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void MainWindow::about() |
| 341 |
{ |
| 342 |
QString about = tr("<p><strong>%1</strong><br/>" |
| 343 |
"Version: %2</p>" |
| 344 |
"<p><strong>%3</strong></p>" |
| 345 |
"<p>Maintainer:<br/>" |
| 346 |
"Kevin Funk <[email protected]></p>" |
| 347 |
"<p>Contributors:<br/>" |
| 348 |
"Daniel Molkentin <[email protected]>" |
| 349 |
"<p>Based on Icemon for KDE written by:<br/>" |
| 350 |
"Frerich Raabe <[email protected]><br/>" |
| 351 |
"Stephan Kulow <[email protected]><br/>" |
| 352 |
"Cornelius Schumacher <[email protected]></p>" |
| 353 |
"Homepage: <a href=\"%4\">%4</a><br/><br/>" |
| 354 |
"Licensed under the GPLv2.<br/>").arg( |
| 355 |
QLatin1String(Icemon::Version::appName), |
| 356 |
QLatin1String(Icemon::Version::version), |
| 357 |
QLatin1String(Icemon::Version::description), |
| 358 |
QLatin1String(Icemon::Version::homePage)); |
| 359 |
|
| 360 |
QMessageBox::about(this, tr("About %1") |
| 361 |
.arg(QLatin1String(Icemon::Version::appShortName)), about); |
| 362 |
} |
| 363 |
|
| 364 |
void MainWindow::updateSchedulerState(Monitor::SchedulerState state) |
| 365 |
{ |
| 366 |
if (state == Monitor::Online) { |
| 367 |
QString statusText = m_hostInfoManager->schedulerName(); |
| 368 |
|
| 369 |
if (!m_hostInfoManager->networkName().isEmpty()) { |
| 370 |
statusText.append(QStringLiteral(" @ ")).append(m_hostInfoManager->networkName()); |
| 371 |
} |
| 372 |
|
| 373 |
m_schedStatusWidget->setText(statusText.isEmpty() ? tr("Scheduler is online.") : statusText); |
| 374 |
} else |
| 375 |
{ |
| 376 |
m_schedStatusWidget->setText(tr("Scheduler is offline.")); |
| 377 |
} |
| 378 |
|
| 379 |
m_activeJobs.clear(); |
| 380 |
updateJobStats(); |
| 381 |
} |
| 382 |
|
| 383 |
void MainWindow::updateJob(const Job &job) |
| 384 |
{ |
| 385 |
if (job.isActive()) { |
| 386 |
m_activeJobs[job.id] = job; |
| 387 |
updateJobStats(); |
| 388 |
} else if (job.isDone()) { |
| 389 |
m_activeJobs.remove(job.id); |
| 390 |
updateJobStats(); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
void MainWindow::updateJobStats() |
| 395 |
{ |
| 396 |
if (!m_monitor->schedulerState()) { |
| 397 |
m_jobStatsWidget->clear(); |
| 398 |
m_jobStatsWidget->setVisible(false); |
| 399 |
if (m_systemTrayIcon) |
| 400 |
{ |
| 401 |
m_systemTrayIcon->setToolTip(tr("Scheduler is offline.")); |
| 402 |
} |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
QMap<QString, PlatformStat> perPlatformStats; |
| 407 |
const HostInfoManager::HostMap hostMap = m_monitor->hostInfoManager()->hostMap(); |
| 408 |
for (auto i = hostMap.begin(); i != hostMap.end(); ++i) { |
| 409 |
if (!i.value()->isOffline() && !i.value()->noRemote()) { |
| 410 |
perPlatformStats[i.value()->platform()].maxJobs += i.value()->maxJobs(); |
| 411 |
} |
| 412 |
} |
| 413 |
for (JobList::const_iterator i = m_activeJobs.constBegin(); i != m_activeJobs.constEnd(); ++i) { |
| 414 |
const HostInfo *server = hostMap[i.value().server != 0 ? i.value().server : i.value().client]; |
| 415 |
if (server && !server->isOffline() && !server->noRemote()) { |
| 416 |
++perPlatformStats[server->platform()].jobs; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
// Turn into something we can sort differently |
| 421 |
QVector<QPair<QString, PlatformStat>> statistics; |
| 422 |
for (auto it = perPlatformStats.constBegin(); it != perPlatformStats.constEnd(); ++it) { |
| 423 |
statistics << qMakePair(it.key(), it.value()); |
| 424 |
} |
| 425 |
|
| 426 |
// Sort, move the platform with the highest max jobs count to the front |
| 427 |
std::sort(statistics.begin(), statistics.end(), [](const QPair<QString, PlatformStat>& a, |
| 428 |
const QPair<QString, PlatformStat>& b) { |
| 429 |
return a.second.maxJobs > b.second.maxJobs; |
| 430 |
}); |
| 431 |
|
| 432 |
// Compose the text |
| 433 |
QString text; |
| 434 |
QString textNoHTML; |
| 435 |
foreach (auto pair, statistics) { |
| 436 |
const QString& platform = pair.first; |
| 437 |
const PlatformStat& stat = pair.second; |
| 438 |
if (stat.maxJobs == 0) { |
| 439 |
continue; |
| 440 |
} |
| 441 |
|
| 442 |
if (!text.isEmpty()) { |
| 443 |
text.append(QStringLiteral(" - ")); |
| 444 |
textNoHTML.append(QStringLiteral(" - ")); |
| 445 |
} |
| 446 |
|
| 447 |
text.append(QStringLiteral("<strong>%2/%3</strong> on %1").arg(platform).arg(stat.jobs).arg(stat.maxJobs)); |
| 448 |
textNoHTML.append(QStringLiteral("%2/%3 on %1").arg(platform).arg(stat.jobs).arg(stat.maxJobs)); |
| 449 |
} |
| 450 |
|
| 451 |
m_jobStatsWidget->setText(tr("| Active jobs: %1").arg(text)); |
| 452 |
m_jobStatsWidget->setVisible(true); |
| 453 |
if (m_systemTrayIcon) |
| 454 |
{ |
| 455 |
m_systemTrayIcon->setToolTip(tr("Active jobs: %1").arg(textNoHTML)); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
void MainWindow::setCurrentNet(const QByteArray &netname) |
| 460 |
{ |
| 461 |
m_monitor->setCurrentNetname(netname); |
| 462 |
} |
| 463 |
|
| 464 |
void MainWindow::setCurrentSched(const QByteArray &schedname) |
| 465 |
{ |
| 466 |
m_monitor->setCurrentSchedname(schedname); |
| 467 |
} |
| 468 |
|
| 469 |
void MainWindow::setCurrentPort(uint schedport) |
| 470 |
{ |
| 471 |
m_monitor->setCurrentSchedport(schedport); |
| 472 |
} |
| 473 |
|
| 474 |
void MainWindow::handleViewModeActionTriggered(QAction *action) |
| 475 |
{ |
| 476 |
const QString viewId = action->data().toString(); |
| 477 |
Q_ASSERT(!viewId.isEmpty()); |
| 478 |
setView(StatusViewFactory::create(viewId, this)); |
| 479 |
} |
| 480 |
|
| 481 |
// It's nasty that we have to hard-code the implementations of Monitor |
| 482 |
// But we can't just add a setMonitor() method because we require the host info manager |
| 483 |
void MainWindow::setTestModeEnabled(bool testMode) |
| 484 |
{ |
| 485 |
if (testMode) { |
| 486 |
setMonitor(new FakeMonitor(m_hostInfoManager, this)); |
| 487 |
} else { |
| 488 |
setMonitor(new IcecreamMonitor(m_hostInfoManager, this)); |
| 489 |
} |
| 490 |
} |