Debian Codegraph @

src/models/joblistmodel.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2012 Kevin Funk <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "joblistmodel.h"
22
23 #include "hostinfo.h"
24 #include "monitor.h"
25
26 #include <QDateTime>
27 #include <QDebug>
28 #include <QTimer>
29 #include <QFileInfo>
30 #include <QDir>
31 #include <QCoreApplication>
32
33 #include <algorithm>
34
35 static QString formatByteSize(unsigned int value)
36 {
37 static const QStringList units = {
38 QStringLiteral("B"),
39 QStringLiteral("KiB"),
40 QStringLiteral("MiB")
41 };
42
43 int unit = 0;
44 while (unit < units.size() && value > 1024) {
45 ++unit;
46 value /= 1024;
47 }
48 return QCoreApplication::tr("%1 %2").arg(QLocale::system().toString(value), units[unit]);
49 }
50
51 /**
52 * Remove some of the parts of a file path
53 *
54 * @param numberOfFilePathParts
55 * If 0 => Return the file name, excluding the path
56 * If 1 => Return '.../ancestorDir/file.ext' if possible
57 * If 2 => ...
58 */
59 static QString trimFilePath(const QString &filePath, int numberOfFilePathParts)
60 {
61 const QChar separator = QDir::separator();
62 if (numberOfFilePathParts == 0) {
63 return filePath.mid(filePath.lastIndexOf(separator) + 1);
64 }
65
66 int counter = numberOfFilePathParts;
67 int index = 0;
68 do {
69 index = filePath.lastIndexOf(separator, index - 1);
70 } while (counter-- && index > 0);
71
72 if (index > 0) {
73 return QLatin1String("...") + filePath.mid(index);
74 }
75
76 return filePath;
77 }
78
79 JobListModel::JobListModel(QObject *parent)
80 : QAbstractListModel(parent)
81 , m_expireTimer(new QTimer(this))
82 {
83 connect(m_expireTimer, SIGNAL(timeout()),
84 this, SLOT(slotExpireFinishedJobs()));
85 }
86
87 Monitor *JobListModel::monitor() const
88 {
89 return m_monitor;
90 }
91
92 void JobListModel::setMonitor(Monitor *monitor)
93 {
94 if (m_monitor == monitor) {
95 return;
96 }
97
98 if (m_monitor) {
99 disconnect(m_monitor.data(), SIGNAL(jobUpdated(Job)), this, SLOT(updateJob(Job)));
100 }
101 m_monitor = monitor;
102 if (m_monitor) {
103 connect(m_monitor.data(), SIGNAL(jobUpdated(Job)), this, SLOT(updateJob(Job)));
104 }
105 }
106
107 void JobListModel::setHostId(unsigned int hostId)
108 {
109 if (m_hostId == hostId) {
110 return;
111 }
112
113 m_hostId = hostId;
114 clear();
115 }
116
117 void JobListModel::updateJob(const Job &job)
118 {
119 const int index = m_jobs.indexOf(job);
120 if (index != -1) {
121 m_jobs[index] = job;
122 emit dataChanged(indexForJob(job, 0), indexForJob(job, _JobColumnCount - 1));
123 } else {
124 if (m_hostId && m_jobType == RemoteJobs && job.server != m_hostId)
125 return;
126 if (m_hostId && m_jobType == LocalJobs && job.client != m_hostId)
127 return;
128 beginInsertRows(QModelIndex(), m_jobs.size(), m_jobs.size());
129 m_jobs << job;
130 endInsertRows();
131 }
132
133 const bool finished = (job.state == Job::Finished || job.state == Job::Failed);
134 if (finished) {
135 expireItem(job);
136 }
137 }
138
139 void JobListModel::clear()
140 {
141 beginResetModel();
142 m_jobs.clear();
143 m_finishedJobs.clear();
144 endResetModel();
145 }
146
147 int JobListModel::columnCount(const QModelIndex &parent) const
148 {
149 Q_UNUSED(parent);
150 return _JobColumnCount;
151 }
152
153 Job JobListModel::jobForIndex(const QModelIndex &index) const
154 {
155 return m_jobs.value(index.row());
156 }
157
158 QModelIndex JobListModel::indexForJob(const Job &job, int column)
159 {
160 const int i = m_jobs.indexOf(job);
161 return index(i, column);
162 }
163
164 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const
165 {
166 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
167 switch (section) {
168 case JobColumnID:
169 return tr("ID");
170 case JobColumnFilename:
171 return tr("File name");
172 case JobColumnClient:
173 return tr("Client");
174 case JobColumnServer:
175 return tr("Server");
176 case JobColumnState:
177 return tr("State");
178 case JobColumnReal:
179 return tr("Real");
180 case JobColumnUser:
181 return tr("User");
182 case JobColumnFaults:
183 return tr("Faults");
184 case JobColumnSizeIn:
185 return tr("Size In");
186 case JobColumnSizeOut:
187 return tr("Size Out");
188 }
189 }
190 return QAbstractListModel::headerData(section, orientation, role);
191 }
192
193 QVariant JobListModel::data(const QModelIndex &index, int role) const
194 {
195 if (!index.isValid()) {
196 return QVariant();
197 }
198
199 const Job job = jobForIndex(index);
200 const int column = index.column();
201 Q_ASSERT(m_monitor);
202 const HostInfoManager *manager = m_monitor->hostInfoManager();
203 if (role == Qt::DisplayRole) {
204 switch (column) {
205 case JobColumnID:
206 return job.id;
207 case JobColumnFilename:
208 return trimFilePath(job.fileName, m_numberOfFilePathParts);
209 case JobColumnClient:
210 return manager->nameForHost(job.client);
211 case JobColumnServer:
212 return manager->nameForHost(job.server);
213 case JobColumnState:
214 return job.stateAsString();
215 case JobColumnReal:
216 return job.real_msec;
217 case JobColumnUser:
218 return job.user_msec;
219 case JobColumnFaults:
220 return job.pfaults;
221 case JobColumnSizeIn:
222 return formatByteSize(job.in_uncompressed);
223 case JobColumnSizeOut:
224 return formatByteSize(job.out_uncompressed);
225 default:
226 break;
227 }
228 } else if (role == Qt::TextAlignmentRole) {
229 switch (column) {
230 case JobColumnID:
231 return Qt::AlignRight;
232 case JobColumnReal:
233 return Qt::AlignRight;
234 case JobColumnUser:
235 return Qt::AlignRight;
236 case JobColumnFaults:
237 return Qt::AlignRight;
238 case JobColumnSizeIn:
239 return Qt::AlignRight;
240 case JobColumnSizeOut:
241 return Qt::AlignRight;
242 default:
243 break;
244 }
245 }
246 return QVariant();
247 }
248
249 QModelIndex JobListModel::parent(const QModelIndex &child) const
250 {
251 Q_UNUSED(child);
252 return QModelIndex();
253 }
254
255 int JobListModel::rowCount(const QModelIndex &parent) const
256 {
257 Q_UNUSED(parent);
258 return m_jobs.size();
259 }
260
261 void JobListModel::slotExpireFinishedJobs()
262 {
263 const uint currentTime = QDateTime::currentDateTime().toSecsSinceEpoch();
264
265 // this list is sorted by the age of the finished jobs, the oldest is the first
266 // so we've to find the first job which isn't old enough to expire
267 FinishedJobs::iterator it = m_finishedJobs.begin();
268 for (const FinishedJobs::iterator itEnd = m_finishedJobs.end(); it != itEnd; ++it) {
269 if (currentTime - (*it).time < ( uint )m_expireDuration) {
270 break;
271 }
272
273 unsigned int jobId = (*it).jobId;
274 removeItemById(jobId);
275 }
276
277 m_finishedJobs.erase(m_finishedJobs.begin(), it);
278
279 if (m_finishedJobs.empty()) {
280 m_expireTimer->stop();
281 }
282 }
283
284 void JobListModel::removeItem(const Job &job)
285 {
286 removeItemById(job.id);
287 }
288
289 void JobListModel::removeItemById(unsigned int jobId)
290 {
291 QVector<Job>::iterator it = std::find_if(m_jobs.begin(), m_jobs.end(),
292 [jobId](const Job &job) {
293 return job.id == jobId;
294 });
295 int index = std::distance(m_jobs.begin(), it);
296 beginRemoveRows(QModelIndex(), index, index);
297 m_jobs.erase(it);
298 endRemoveRows();
299 }
300
301 void JobListModel::expireItem(const Job &job)
302 {
303 if (m_expireDuration == 0) {
304 removeItem(job);
305 return;
306 }
307
308 const uint currentTime = QDateTime::currentDateTime().toSecsSinceEpoch();
309 m_finishedJobs.push_back(FinishedJob(currentTime, job.id));
310
311 if (!m_expireTimer->isActive()) {
312 m_expireTimer->start(1000);
313 }
314 }
315
316 JobListSortFilterProxyModel::JobListSortFilterProxyModel(QObject *parent)
317 : QSortFilterProxyModel(parent)
318 {
319 }
320
321 bool JobListSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
322 {
323 if ((left.column() != JobListModel::JobColumnSizeIn && left.column() != JobListModel::JobColumnSizeOut)
324 || (right.column() != JobListModel::JobColumnSizeIn && right.column() != JobListModel::JobColumnSizeOut)) {
325 return QSortFilterProxyModel::lessThan(left, right);
326 }
327 // Sort file sizes correctly, the view shows them already formatted in a way that wouldn't be correctly numerically
328 // compared.
329 const JobListModel *model = static_cast<JobListModel *>(sourceModel());
330 Job jobLeft = model->jobForIndex(left);
331 Job jobRight = model->jobForIndex(right);
332 unsigned int leftValue = left.column() == JobListModel::JobColumnSizeIn ? jobLeft.in_uncompressed : jobLeft.out_uncompressed;
333 unsigned int rightValue = right.column() == JobListModel::JobColumnSizeIn ? jobRight.in_uncompressed : jobRight.out_uncompressed;
334 return leftValue < rightValue;
335 }