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 #include <functional>
35
36 static QString formatByteSize(unsigned int value)
37 {
38 static const QStringList units = {
39 QStringLiteral("B"),
40 QStringLiteral("KiB"),
41 QStringLiteral("MiB")
42 };
43
44 int unit = 0;
45 while (unit < units.size() && value > 1024) {
46 ++unit;
47 value /= 1024;
48 }
49 return QCoreApplication::tr("%1 %2").arg(QLocale::system().toString(value), units[unit]);
50 }
51
52 /**
53 * Remove some of the parts of a file path
54 *
55 * @param numberOfFilePathParts
56 * If 0 => Return the file name, excluding the path
57 * If 1 => Return '.../ancestorDir/file.ext' if possible
58 * If 2 => ...
59 */
60 static QString trimFilePath(const QString &filePath, int numberOfFilePathParts)
61 {
62 const QChar separator = QDir::separator();
63 if (numberOfFilePathParts == 0) {
64 return filePath.mid(filePath.lastIndexOf(separator) + 1);
65 }
66
67 int counter = numberOfFilePathParts;
68 int index = 0;
69 do {
70 index = filePath.lastIndexOf(separator, index - 1);
71 } while (counter-- && index > 0);
72
73 if (index > 0) {
74 return QLatin1String("...") + filePath.mid(index);
75 }
76
77 return filePath;
78 }
79
80 JobListModel::JobListModel(QObject *parent)
81 : QAbstractListModel(parent)
82 , m_numberOfFilePathParts(2)
83 , m_expireDuration(-1)
84 , m_expireTimer(new QTimer(this))
85 , m_jobType(AllJobs)
86 , m_hostId(0)
87 {
88 connect(m_expireTimer, SIGNAL(timeout()),
89 this, SLOT(slotExpireFinishedJobs()));
90 }
91
92 Monitor *JobListModel::monitor() const
93 {
94 return m_monitor;
95 }
96
97 void JobListModel::setMonitor(Monitor *monitor)
98 {
99 if (m_monitor == monitor) {
100 return;
101 }
102
103 if (m_monitor) {
104 disconnect(m_monitor.data(), SIGNAL(jobUpdated(Job)), this, SLOT(updateJob(Job)));
105 }
106 m_monitor = monitor;
107 if (m_monitor) {
108 connect(m_monitor.data(), SIGNAL(jobUpdated(Job)), this, SLOT(updateJob(Job)));
109 }
110 }
111
112 void JobListModel::setHostId(unsigned int hostId)
113 {
114 if (m_hostId == hostId) {
115 return;
116 }
117
118 m_hostId = hostId;
119 clear();
120 }
121
122 void JobListModel::updateJob(const Job &job)
123 {
124 const int index = m_jobs.indexOf(job);
125 if (index != -1) {
126 m_jobs[index] = job;
127 emit dataChanged(indexForJob(job, 0), indexForJob(job, _JobColumnCount - 1));
128 } else {
129 if (m_hostId && m_jobType == RemoteJobs && job.server != m_hostId)
130 return;
131 if (m_hostId && m_jobType == LocalJobs && job.client != m_hostId)
132 return;
133 beginInsertRows(QModelIndex(), m_jobs.size(), m_jobs.size());
134 m_jobs << job;
135 endInsertRows();
136 }
137
138 const bool finished = (job.state == Job::Finished || job.state == Job::Failed);
139 if (finished) {
140 expireItem(job);
141 }
142 }
143
144 void JobListModel::clear()
145 {
146 beginResetModel();
147 m_jobs.clear();
148 m_finishedJobs.clear();
149 endResetModel();
150 }
151
152 int JobListModel::columnCount(const QModelIndex &parent) const
153 {
154 Q_UNUSED(parent);
155 return _JobColumnCount;
156 }
157
158 Job JobListModel::jobForIndex(const QModelIndex &index) const
159 {
160 return m_jobs.value(index.row());
161 }
162
163 QModelIndex JobListModel::indexForJob(const Job &job, int column)
164 {
165 const int i = m_jobs.indexOf(job);
166 return index(i, column);
167 }
168
169 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const
170 {
171 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
172 switch (section) {
173 case JobColumnID:
174 return tr("ID");
175 case JobColumnFilename:
176 return tr("File name");
177 case JobColumnClient:
178 return tr("Client");
179 case JobColumnServer:
180 return tr("Server");
181 case JobColumnState:
182 return tr("State");
183 case JobColumnReal:
184 return tr("Real");
185 case JobColumnUser:
186 return tr("User");
187 case JobColumnFaults:
188 return tr("Faults");
189 case JobColumnSizeIn:
190 return tr("Size In");
191 case JobColumnSizeOut:
192 return tr("Size Out");
193 }
194 }
195 return QAbstractListModel::headerData(section, orientation, role);
196 }
197
198 QVariant JobListModel::data(const QModelIndex &index, int role) const
199 {
200 if (!index.isValid()) {
201 return QVariant();
202 }
203
204 const Job job = jobForIndex(index);
205 const int column = index.column();
206 Q_ASSERT(m_monitor);
207 const HostInfoManager *manager = m_monitor->hostInfoManager();
208 if (role == Qt::DisplayRole) {
209 switch (column) {
210 case JobColumnID:
211 return job.id;
212 case JobColumnFilename:
213 return trimFilePath(job.fileName, m_numberOfFilePathParts);
214 case JobColumnClient:
215 return manager->nameForHost(job.client);
216 case JobColumnServer:
217 return manager->nameForHost(job.server);
218 case JobColumnState:
219 return job.stateAsString();
220 case JobColumnReal:
221 return job.real_msec;
222 case JobColumnUser:
223 return job.user_msec;
224 case JobColumnFaults:
225 return job.pfaults;
226 case JobColumnSizeIn:
227 return formatByteSize(job.in_uncompressed);
228 case JobColumnSizeOut:
229 return formatByteSize(job.out_uncompressed);
230 default:
231 break;
232 }
233 } else if (role == Qt::TextAlignmentRole) {
234 switch (column) {
235 case JobColumnID:
236 return Qt::AlignRight;
237 case JobColumnReal:
238 return Qt::AlignRight;
239 case JobColumnUser:
240 return Qt::AlignRight;
241 case JobColumnFaults:
242 return Qt::AlignRight;
243 case JobColumnSizeIn:
244 return Qt::AlignRight;
245 case JobColumnSizeOut:
246 return Qt::AlignRight;
247 default:
248 break;
249 }
250 }
251 return QVariant();
252 }
253
254 QModelIndex JobListModel::parent(const QModelIndex &child) const
255 {
256 Q_UNUSED(child);
257 return QModelIndex();
258 }
259
260 int JobListModel::rowCount(const QModelIndex &parent) const
261 {
262 Q_UNUSED(parent);
263 return m_jobs.size();
264 }
265
266 struct find_jobid
267 : public std::unary_function<Job, bool>
268 {
269 public:
270 explicit find_jobid(unsigned int jobId)
271 : m_jobId(jobId) {}
272
273 bool operator()(const Job &job) const
274 {
275 return job.id == m_jobId;
276 }
277
278 private:
279 unsigned int m_jobId;
280 };
281
282 void JobListModel::slotExpireFinishedJobs()
283 {
284 const uint currentTime = QDateTime::currentDateTime().toTime_t();
285
286 // this list is sorted by the age of the finished jobs, the oldest is the first
287 // so we've to find the first job which isn't old enough to expire
288 FinishedJobs::iterator it = m_finishedJobs.begin();
289 for (const FinishedJobs::iterator itEnd = m_finishedJobs.end(); it != itEnd; ++it) {
290 if (currentTime - (*it).time < ( uint )m_expireDuration) {
291 break;
292 }
293
294 unsigned int jobId = (*it).jobId;
295 removeItemById(jobId);
296 }
297
298 m_finishedJobs.erase(m_finishedJobs.begin(), it);
299
300 if (m_finishedJobs.empty()) {
301 m_expireTimer->stop();
302 }
303 }
304
305 void JobListModel::removeItem(const Job &job)
306 {
307 removeItemById(job.id);
308 }
309
310 void JobListModel::removeItemById(unsigned int jobId)
311 {
312 QVector<Job>::iterator it = std::find_if(m_jobs.begin(), m_jobs.end(), find_jobid(jobId));
313 int index = std::distance(m_jobs.begin(), it);
314 beginRemoveRows(QModelIndex(), index, index);
315 m_jobs.erase(it);
316 endRemoveRows();
317 }
318
319 void JobListModel::expireItem(const Job &job)
320 {
321 if (m_expireDuration == 0) {
322 removeItem(job);
323 return;
324 }
325
326 const uint currentTime = QDateTime::currentDateTime().toTime_t();
327 m_finishedJobs.push_back(FinishedJob(currentTime, job.id));
328
329 if (!m_expireTimer->isActive()) {
330 m_expireTimer->start(1000);
331 }
332 }
333
334 JobListSortFilterProxyModel::JobListSortFilterProxyModel(QObject *parent)
335 : QSortFilterProxyModel(parent)
336 {
337 }
338
339 bool JobListSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
340 {
341 if ((left.column() != JobListModel::JobColumnSizeIn && left.column() != JobListModel::JobColumnSizeOut)
342 || (right.column() != JobListModel::JobColumnSizeIn && right.column() != JobListModel::JobColumnSizeOut)) {
343 return QSortFilterProxyModel::lessThan(left, right);
344 }
345 // Sort file sizes correctly, the view shows them already formatted in a way that wouldn't be correctly numerically
346 // compared.
347 const JobListModel *model = static_cast<JobListModel *>(sourceModel());
348 Job jobLeft = model->jobForIndex(left);
349 Job jobRight = model->jobForIndex(right);
350 unsigned int leftValue = left.column() == JobListModel::JobColumnSizeIn ? jobLeft.in_uncompressed : jobLeft.out_uncompressed;
351 unsigned int rightValue = right.column() == JobListModel::JobColumnSizeIn ? jobRight.in_uncompressed : jobRight.out_uncompressed;
352 return leftValue < rightValue;
353 }