Debian Codegraph @

src/mainwindow.cc

raw
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 "utils.h"
34
35 #include <QDebug>
36 #include <QLabel>
37 #include <QMenuBar>
38 #include <QStatusBar>
39 #include <QMessageBox>
40 #include <QApplication>
41 #include <QSettings>
42 #include <QMenu>
43
44 #include <algorithm>
45
46 namespace {
47
48 struct PlatformStat
49 {
50 PlatformStat()
51 : jobs(0)
52 , maxJobs(0) {}
53
54 unsigned int jobs;
55 unsigned int maxJobs;
56 };
57
58 }
59
60 MainWindow::MainWindow(QWidget *parent)
61 : QMainWindow(parent)
62 , m_view(nullptr)
63 {
64 QIcon appIcon = QIcon();
65 appIcon.addFile(QStringLiteral(":/images/128-apps-icemon.png"), QSize(128, 128));
66 appIcon.addFile(QStringLiteral(":/images/48-apps-icemon.png"), QSize(48, 48));
67 appIcon.addFile(QStringLiteral(":/images/32-apps-icemon.png"), QSize(32, 32));
68 appIcon.addFile(QStringLiteral(":/images/22-apps-icemon.png"), QSize(22, 22));
69 appIcon.addFile(QStringLiteral(":/images/16-apps-icemon.png"), QSize(16, 16));
70 setWindowIcon(appIcon);
71 setWindowTitle(QApplication::translate("appName", Icemon::Version::appName));
72
73 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
74 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
75 QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
76
77 m_schedStatusWidget = new QLabel;
78 statusBar()->addPermanentWidget(m_schedStatusWidget);
79
80 m_jobStatsWidget = new QLabel;
81 m_jobStatsWidget->setVisible(false);
82 statusBar()->addPermanentWidget(m_jobStatsWidget);
83
84 QAction *action = fileMenu->addAction(tr("&Quit"), this, SLOT(close()), tr("Ctrl+Q"));
85 action->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
86 action->setMenuRole(QAction::QuitRole);
87
88 m_viewMode = new QActionGroup(this);
89 action = m_viewMode->addAction(tr("&List View"));
90 action->setCheckable(true);
91 action->setData(QStringLiteral("list"));
92 action = m_viewMode->addAction(tr("&Star View"));
93 action->setCheckable(true);
94 action->setData(QStringLiteral("star"));
95 action = m_viewMode->addAction(tr("&Gantt View"));
96 action->setCheckable(true);
97 action->setData(QStringLiteral("gantt"));
98 action = m_viewMode->addAction(tr("Summary &View"));
99 action->setCheckable(true);
100 action->setData(QStringLiteral("summary"));
101 action = m_viewMode->addAction(tr("&Flow View"));
102 action->setCheckable(true);
103 action->setData(QStringLiteral("flow"));
104 action = m_viewMode->addAction(tr("&Detailed Host View"));
105 action->setCheckable(true);
106 action->setData(QStringLiteral("detailedhost"));
107 connect(m_viewMode, SIGNAL(triggered(QAction *)), this, SLOT(handleViewModeActionTriggered(QAction *)));
108 viewMenu->addActions(m_viewMode->actions());
109
110 viewMenu->addSeparator();
111
112 action = viewMenu->addAction(tr("Pause"));
113 action->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
114 action->setCheckable(true);
115 connect(action, SIGNAL(triggered()), this, SLOT(pauseView()));
116 m_pauseViewAction = action;
117
118 viewMenu->addSeparator();
119
120 action = viewMenu->addAction(tr("Configure View..."));
121 action->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
122 connect(action, SIGNAL(triggered()), this, SLOT(configureView()));
123 m_configureViewAction = action;
124
125 action = helpMenu->addAction(tr("About Qt..."));
126 connect(action, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
127 action->setMenuRole(QAction::AboutQtRole);
128
129 action = helpMenu->addAction(tr("About..."));
130 action->setIcon(appIcon);
131 connect(action, SIGNAL(triggered()), this, SLOT(about()));
132 action->setMenuRole(QAction::AboutRole);
133
134 m_hostInfoManager = new HostInfoManager;
135 setMonitor(new IcecreamMonitor(m_hostInfoManager, this));
136
137 resize(600, 400);
138 readSettings();
139 }
140
141 MainWindow::~MainWindow()
142 {
143 delete m_hostInfoManager;
144 }
145
146 void MainWindow::closeEvent(QCloseEvent *e)
147 {
148 writeSettings();
149
150 QMainWindow::closeEvent(e);
151 }
152
153 void MainWindow::readSettings()
154 {
155 QSettings settings;
156 restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray());
157 restoreState(settings.value(QStringLiteral("windowState")).toByteArray());
158 QString viewId = settings.value(QStringLiteral("currentView")).toString();
159
160 auto view = StatusViewFactory::create(viewId, this);
161 setView(view);
162 }
163
164 void MainWindow::writeSettings()
165 {
166 QSettings settings;
167 settings.setValue(QStringLiteral("geometry"), saveGeometry());
168 settings.setValue(QStringLiteral("windowState"), saveState());
169 settings.setValue(QStringLiteral("currentView"), (m_view ? m_view->id() : QString()));
170 settings.sync();
171 }
172
173 Monitor *MainWindow::monitor() const
174 {
175 return m_monitor;
176 }
177
178 void MainWindow::setMonitor(Monitor *monitor)
179 {
180 if (m_monitor == monitor) {
181 return;
182 }
183
184 if (m_monitor) {
185 disconnect(m_monitor, SIGNAL(schedulerStateChanged(Monitor::SchedulerState)),
186 this, SLOT(updateSchedulerState(Monitor::SchedulerState)));
187 disconnect(m_monitor, SIGNAL(jobUpdated(const Job &)), this, SLOT(updateJob(Job)));
188 disconnect(m_monitor->hostInfoManager(), SIGNAL(hostMapChanged()), this, SLOT(updateJobStats()));
189 }
190
191 m_monitor = monitor;
192
193 if (m_monitor) {
194 connect(m_monitor, SIGNAL(schedulerStateChanged(Monitor::SchedulerState)),
195 this, SLOT(updateSchedulerState(Monitor::SchedulerState)));
196 connect(m_monitor, SIGNAL(jobUpdated(const Job &)), this, SLOT(updateJob(Job)));
197 connect(m_monitor->hostInfoManager(), SIGNAL(hostMapChanged()), this, SLOT(updateJobStats()));
198 }
199
200 if (m_view) {
201 m_view->setMonitor(m_monitor);
202 }
203 updateSchedulerState(m_monitor ? m_monitor->schedulerState() : Monitor::Offline);
204 }
205
206 StatusView *MainWindow::view() const
207 {
208 return m_view;
209 }
210
211 void MainWindow::setView(StatusView *view)
212 {
213 if (m_view == view) {
214 return;
215 }
216
217 delete m_view;
218
219 m_view = view;
220
221 if (m_view) {
222 m_configureViewAction->setEnabled(m_view->isConfigurable());
223 m_pauseViewAction->setEnabled(m_view->isPausable());
224 m_view->setMonitor(m_monitor);
225
226 setCentralWidget(m_view->widget());
227 }
228
229 // update action-group
230 const QString viewId = (m_view ? m_view->id() : QString());
231 foreach(QAction * action, m_viewMode->actions()) {
232 if (action->data().toString() == viewId) {
233 action->setChecked(true);
234 break;
235 }
236 }
237 }
238
239 void MainWindow::pauseView()
240 {
241 m_view->togglePause();
242 }
243
244 void MainWindow::configureView()
245 {
246 m_view->configureView();
247 }
248
249 void MainWindow::about()
250 {
251 QString about = tr("<p><strong>%1</strong><br/>"
252 "Version: %2</p>"
253 "<p><strong>%3</strong></p>"
254 "<p>Maintainer:<br/>"
255 "Kevin Funk &lt;[email protected]&gt;</p>"
256 "<p>Contributors:<br/>"
257 "Daniel Molkentin &lt;[email protected]&gt;"
258 "<p>Based on Icemon for KDE written by:<br/>"
259 "Frerich Raabe &lt;[email protected]&gt;<br/>"
260 "Stephan Kulow &lt;[email protected]&gt;<br/>"
261 "Cornelius Schumacher &lt;[email protected]&gt;</p>"
262 "Homepage: <a href=\"%4\">%4</a><br/><br/>"
263 "Licensed under the GPLv2.<br/>").arg(
264 QLatin1String(Icemon::Version::appName),
265 QLatin1String(Icemon::Version::version),
266 QLatin1String(Icemon::Version::description),
267 QLatin1String(Icemon::Version::homePage));
268
269 QMessageBox::about(this, tr("About %1")
270 .arg(QLatin1String(Icemon::Version::appShortName)), about);
271 }
272
273 void MainWindow::updateSchedulerState(Monitor::SchedulerState state)
274 {
275 if (state == Monitor::Online) {
276 QString statusText = m_hostInfoManager->schedulerName();
277
278 if (!m_hostInfoManager->networkName().isEmpty()) {
279 statusText.append(QStringLiteral(" @ ")).append(m_hostInfoManager->networkName());
280 }
281
282 m_schedStatusWidget->setText(statusText.isEmpty() ? tr("Scheduler is online.") : statusText);
283 } else
284 {
285 m_schedStatusWidget->setText(tr("Scheduler is offline."));
286 }
287
288 m_activeJobs.clear();
289 updateJobStats();
290 }
291
292 void MainWindow::updateJob(const Job &job)
293 {
294 if (job.isActive()) {
295 m_activeJobs[job.id] = job;
296 updateJobStats();
297 } else if (job.isDone()) {
298 m_activeJobs.remove(job.id);
299 updateJobStats();
300 }
301 }
302
303 void MainWindow::updateJobStats()
304 {
305 if (!m_monitor->schedulerState()) {
306 m_jobStatsWidget->clear();
307 m_jobStatsWidget->setVisible(false);
308 return;
309 }
310
311 QMap<QString, PlatformStat> perPlatformStats;
312 const HostInfoManager::HostMap hostMap = m_monitor->hostInfoManager()->hostMap();
313 for (auto i = hostMap.begin(); i != hostMap.end(); ++i) {
314 if (!i.value()->isOffline() && !i.value()->noRemote()) {
315 perPlatformStats[i.value()->platform()].maxJobs += i.value()->maxJobs();
316 }
317 }
318 for (JobList::const_iterator i = m_activeJobs.constBegin(); i != m_activeJobs.constEnd(); ++i) {
319 const HostInfo *server = hostMap[i.value().server != 0 ? i.value().server : i.value().client];
320 if (server && !server->isOffline() && !server->noRemote()) {
321 ++perPlatformStats[server->platform()].jobs;
322 }
323 }
324
325 // Turn into something we can sort differently
326 QVector<QPair<QString, PlatformStat>> statistics;
327 for (auto it = perPlatformStats.constBegin(); it != perPlatformStats.constEnd(); ++it) {
328 statistics << qMakePair(it.key(), it.value());
329 }
330
331 // Sort, move the platform with the highest max jobs count to the front
332 std::sort(statistics.begin(), statistics.end(), [](const QPair<QString, PlatformStat>& a,
333 const QPair<QString, PlatformStat>& b) {
334 return a.second.maxJobs > b.second.maxJobs;
335 });
336
337 // Compose the text
338 QString text;
339 foreach (auto pair, statistics) {
340 const QString& platform = pair.first;
341 const PlatformStat& stat = pair.second;
342 if (stat.maxJobs == 0) {
343 continue;
344 }
345
346 if (!text.isEmpty()) {
347 text.append(QStringLiteral(" - "));
348 }
349
350 text.append(QStringLiteral("<strong>%2/%3</strong> on %1").arg(platform).arg(stat.jobs).arg(stat.maxJobs));
351 }
352
353 m_jobStatsWidget->setText(tr("| Active jobs: %1").arg(text));
354 m_jobStatsWidget->setVisible(true);
355 }
356
357 void MainWindow::setCurrentNet(const QByteArray &netname)
358 {
359 m_monitor->setCurrentNetname(netname);
360 }
361
362 void MainWindow::setCurrentSched(const QByteArray &schedname)
363 {
364 m_monitor->setCurrentSchedname(schedname);
365 }
366
367 void MainWindow::setCurrentPort(uint schedport)
368 {
369 m_monitor->setCurrentSchedport(schedport);
370 }
371
372 void MainWindow::handleViewModeActionTriggered(QAction *action)
373 {
374 const QString viewId = action->data().toString();
375 Q_ASSERT(!viewId.isEmpty());
376 setView(StatusViewFactory::create(viewId, this));
377 }
378
379 // It's nasty that we have to hard-code the implementations of Monitor
380 // But we can't just add a setMonitor() method because we require the host info manager
381 void MainWindow::setTestModeEnabled(bool testMode)
382 {
383 if (testMode) {
384 setMonitor(new FakeMonitor(m_hostInfoManager, this));
385 } else {
386 setMonitor(new IcecreamMonitor(m_hostInfoManager, this));
387 }
388 }