OSDN Git Service

Added option to delete jobs from the list + also added option to browse the output...
[x264-launcher/x264-launcher.git] / src / model_jobList.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "model_jobList.h"
23 #include "global.h"
24 #include "thread_encode.h"
25
26 #include <QIcon>
27 #include <QFileInfo>
28
29 JobListModel::JobListModel(void)
30 {
31 }
32
33 JobListModel::~JobListModel(void)
34 {
35         while(!m_jobs.isEmpty())
36         {
37                 QUuid id = m_jobs.takeFirst();
38                 EncodeThread *thread = m_threads.value(id, NULL);
39                 LogFileModel *logFile = m_logFile.value(id, NULL);
40                 X264_DELETE(thread);
41                 X264_DELETE(logFile);
42         }
43 }
44
45 ///////////////////////////////////////////////////////////////////////////////
46 // Model interface
47 ///////////////////////////////////////////////////////////////////////////////
48
49 int JobListModel::columnCount(const QModelIndex &parent) const
50 {
51         return 4;
52 }
53
54 int JobListModel::rowCount(const QModelIndex &parent) const
55 {
56         return m_jobs.count();
57 }
58
59 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const 
60 {
61         if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
62         {
63                 switch(section)
64                 {
65                 case 0:
66                         return QVariant::fromValue<QString>(tr("Job"));
67                         break;
68                 case 1:
69                         return QVariant::fromValue<QString>(tr("Status"));
70                         break;
71                 case 2:
72                         return QVariant::fromValue<QString>(tr("Progress"));
73                         break;
74                 case 3:
75                         return QVariant::fromValue<QString>(tr("Details"));
76                         break;
77                 default:
78                         return QVariant();
79                         break;
80                 }
81         }
82
83         return QVariant();
84 }
85
86 QModelIndex JobListModel::index(int row, int column, const QModelIndex &parent) const
87 {
88         return createIndex(row, column, NULL);
89 }
90
91 QModelIndex JobListModel::parent(const QModelIndex &index) const
92 {
93         return QModelIndex();
94 }
95
96 QVariant JobListModel::data(const QModelIndex &index, int role) const
97 {
98         if(role == Qt::DisplayRole)
99         {
100                 if(index.row() >= 0 && index.row() < m_jobs.count())
101                 {
102                         switch(index.column())
103                         {
104                         case 0:
105                                 return m_name.value(m_jobs.at(index.row()));
106                                 break;
107                         case 1:
108                                 switch(m_status.value(m_jobs.at(index.row())))
109                                 {
110                                 case EncodeThread::JobStatus_Enqueued:
111                                         return QVariant::fromValue<QString>(tr("Enqueued."));
112                                         break;
113                                 case EncodeThread::JobStatus_Starting:
114                                         return QVariant::fromValue<QString>(tr("Starting..."));
115                                         break;
116                                 case EncodeThread::JobStatus_Indexing:
117                                         return QVariant::fromValue<QString>(tr("Indexing..."));
118                                         break;
119                                 case EncodeThread::JobStatus_Running:
120                                         return QVariant::fromValue<QString>(tr("Running..."));
121                                         break;
122                                 case EncodeThread::JobStatus_Running_Pass1:
123                                         return QVariant::fromValue<QString>(tr("Running... (Pass 1)"));
124                                         break;
125                                 case EncodeThread::JobStatus_Running_Pass2:
126                                         return QVariant::fromValue<QString>(tr("Running... (Pass 2)"));
127                                         break;
128                                 case EncodeThread::JobStatus_Completed:
129                                         return QVariant::fromValue<QString>(tr("Completed."));
130                                         break;
131                                 case EncodeThread::JobStatus_Failed:
132                                         return QVariant::fromValue<QString>(tr("Failed!"));
133                                         break;
134                                 case EncodeThread::JobStatus_Pausing:
135                                         return QVariant::fromValue<QString>(tr("Pausing..."));
136                                         break;
137                                 case EncodeThread::JobStatus_Paused:
138                                         return QVariant::fromValue<QString>(tr("Paused."));
139                                         break;
140                                 case EncodeThread::JobStatus_Resuming:
141                                         return QVariant::fromValue<QString>(tr("Resuming..."));
142                                         break;
143                                 case EncodeThread::JobStatus_Aborting:
144                                         return QVariant::fromValue<QString>(tr("Aborting..."));
145                                         break;
146                                 case EncodeThread::JobStatus_Aborted:
147                                         return QVariant::fromValue<QString>(tr("Aborted!"));
148                                         break;
149                                 default:
150                                         return QVariant::fromValue<QString>(tr("(Unknown)"));
151                                         break;
152                                 }
153                                 break;
154                         case 2:
155                                 return QString().sprintf("%d%%", m_progress.value(m_jobs.at(index.row())));
156                                 break;
157                         case 3:
158                                 return m_details.value(m_jobs.at(index.row()));
159                                 break;
160                         default:
161                                 return QVariant();
162                                 break;
163                         }
164                 }
165         }
166         else if(role == Qt::DecorationRole)
167         {
168                 if(index.row() >= 0 && index.row() < m_jobs.count() && index.column() == 0)
169                 {
170                         switch(m_status.value(m_jobs.at(index.row())))
171                         {
172                         case EncodeThread::JobStatus_Enqueued:
173                                 return QIcon(":/buttons/hourglass.png");
174                                 break;
175                         case EncodeThread::JobStatus_Starting:
176                                 return QIcon(":/buttons/lightning.png");
177                                 break;
178                         case EncodeThread::JobStatus_Indexing:
179                                 return QIcon(":/buttons/find.png");
180                                 break;
181                         case EncodeThread::JobStatus_Running:
182                         case EncodeThread::JobStatus_Running_Pass1:
183                         case EncodeThread::JobStatus_Running_Pass2:
184                                 return QIcon(":/buttons/play.png");
185                                 break;
186                         case EncodeThread::JobStatus_Completed:
187                                 return QIcon(":/buttons/accept.png");
188                                 break;
189                         case EncodeThread::JobStatus_Failed:
190                                 return QIcon(":/buttons/exclamation.png");
191                                 break;
192                         case EncodeThread::JobStatus_Pausing:
193                                 return QIcon(":/buttons/clock_pause.png");
194                                 break;
195                         case EncodeThread::JobStatus_Paused:
196                                 return QIcon(":/buttons/suspended.png");
197                                 break;
198                         case EncodeThread::JobStatus_Resuming:
199                                 return QIcon(":/buttons/clock_play.png");
200                                 break;
201                         case EncodeThread::JobStatus_Aborting:
202                                 return QIcon(":/buttons/clock_stop.png");
203                                 break;
204                         case EncodeThread::JobStatus_Aborted:
205                                 return QIcon(":/buttons/error.png");
206                                 break;
207                         default:
208                                 return QVariant();
209                                 break;
210                         }
211                 }
212         }
213
214         return QVariant();
215 }
216
217 ///////////////////////////////////////////////////////////////////////////////
218 // Public interface
219 ///////////////////////////////////////////////////////////////////////////////
220
221 QModelIndex JobListModel::insertJob(EncodeThread *thread)
222 {
223         QUuid id = thread->getId();
224         LogFileModel *logFile = NULL;
225
226         if(m_jobs.contains(id))
227         {
228                 return QModelIndex();
229         }
230                 
231         int n = 2;
232         QString jobName = QFileInfo(thread->sourceFileName()).completeBaseName();
233
234         forever
235         {
236                 bool unique = true;
237                 for(int i = 0; i < m_jobs.count(); i++)
238                 {
239                         if(m_name.value(m_jobs.at(i)).compare(jobName, Qt::CaseInsensitive) == 0)
240                         {
241                                 unique = false;
242                                 break;
243                         }
244                 }
245                 if(!unique)
246                 {
247                         jobName = QString("%1 (%2)").arg(QFileInfo(thread->sourceFileName()).completeBaseName(), QString::number(n++));
248                         continue;
249                 }
250                 break;
251         }
252
253         beginInsertRows(QModelIndex(), m_jobs.count(), m_jobs.count());
254         m_jobs.append(id);
255         m_name.insert(id, jobName);
256         m_status.insert(id, EncodeThread::JobStatus_Enqueued);
257         m_progress.insert(id, 0);
258         m_threads.insert(id, thread);
259         m_logFile.insert(id, (logFile = new LogFileModel));
260         m_details.insert(id, tr("Not started yet."));
261         endInsertRows();
262
263         connect(thread, SIGNAL(statusChanged(QUuid, EncodeThread::JobStatus)), this, SLOT(updateStatus(QUuid, EncodeThread::JobStatus)), Qt::QueuedConnection);
264         connect(thread, SIGNAL(progressChanged(QUuid, unsigned int)), this, SLOT(updateProgress(QUuid, unsigned int)), Qt::QueuedConnection);
265         connect(thread, SIGNAL(messageLogged(QUuid, QString)), logFile, SLOT(addLogMessage(QUuid, QString)), Qt::QueuedConnection);
266         connect(thread, SIGNAL(detailsChanged(QUuid, QString)), this, SLOT(updateDetails(QUuid, QString)), Qt::QueuedConnection);
267         
268         return createIndex(m_jobs.count() - 1, 0, NULL);
269 }
270
271 bool JobListModel::startJob(const QModelIndex &index)
272 {
273         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
274         {
275                 QUuid id = m_jobs.at(index.row());
276                 if(m_status.value(id) == EncodeThread::JobStatus_Enqueued)
277                 {
278                         updateStatus(id, EncodeThread::JobStatus_Starting);
279                         updateDetails(id, tr("Starting up, please wait..."));
280                         m_threads.value(id)->start();
281                         return true;
282                 }
283         }
284
285         return false;
286 }
287
288 bool JobListModel::pauseJob(const QModelIndex &index)
289 {
290         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
291         {
292                 QUuid id = m_jobs.at(index.row());
293                 EncodeThread::JobStatus status = m_status.value(id);
294                 if((status == EncodeThread::JobStatus_Indexing) || (status == EncodeThread::JobStatus_Running) ||
295                         (status == EncodeThread::JobStatus_Running_Pass1) || (status == EncodeThread::JobStatus_Running_Pass2))
296                 {
297                         updateStatus(id, EncodeThread::JobStatus_Pausing);
298                         m_threads.value(id)->pauseJob();
299                         return true;
300                 }
301         }
302
303         return false;
304 }
305
306 bool JobListModel::resumeJob(const QModelIndex &index)
307 {
308         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
309         {
310                 QUuid id = m_jobs.at(index.row());
311                 EncodeThread::JobStatus status = m_status.value(id);
312                 if(status == EncodeThread::JobStatus_Paused)
313                 {
314                         updateStatus(id, EncodeThread::JobStatus_Resuming);
315                         m_threads.value(id)->resumeJob();
316                         return true;
317                 }
318         }
319
320         return false;
321 }
322
323 bool JobListModel::abortJob(const QModelIndex &index)
324 {
325         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
326         {
327                 QUuid id = m_jobs.at(index.row());
328                 if(m_status.value(id) == EncodeThread::JobStatus_Indexing || m_status.value(id) == EncodeThread::JobStatus_Running ||
329                         m_status.value(id) == EncodeThread::JobStatus_Running_Pass1 || EncodeThread::JobStatus_Running_Pass2)
330                 {
331                         updateStatus(id, EncodeThread::JobStatus_Aborting);
332                         m_threads.value(id)->abortJob();
333                         return true;
334                 }
335         }
336
337         return false;
338 }
339
340 bool JobListModel::deleteJob(const QModelIndex &index)
341 {
342         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
343         {
344                 QUuid id = m_jobs.at(index.row());
345                 if(m_status.value(id) == EncodeThread::JobStatus_Completed || m_status.value(id) == EncodeThread::JobStatus_Failed ||
346                         m_status.value(id) == EncodeThread::JobStatus_Aborted || m_status.value(id) == EncodeThread::JobStatus_Enqueued)
347                 {
348                         int idx = index.row();
349                         QUuid id = m_jobs.at(idx);
350                         EncodeThread *thread = m_threads.value(id, NULL);
351                         LogFileModel *logFile = m_logFile.value(id, NULL);
352                         if((thread == NULL) || (!thread->isRunning()))
353                         {
354                                 
355                                 beginRemoveRows(QModelIndex(), idx, idx);
356                                 m_jobs.removeAt(index.row());
357                                 m_name.remove(id);
358                                 m_threads.remove(id);
359                                 m_status.remove(id);
360                                 m_progress.remove(id);
361                                 m_logFile.remove(id);
362                                 m_details.remove(id);
363                                 endRemoveRows();
364                                 X264_DELETE(thread);
365                                 X264_DELETE(logFile);
366                                 return true;
367                         }
368                 }
369         }
370
371         return false;
372 }
373
374 LogFileModel *JobListModel::getLogFile(const QModelIndex &index)
375 {
376         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
377         {
378                 return m_logFile.value(m_jobs.at(index.row()));
379         }
380
381         return NULL;
382 }
383
384 const QString &JobListModel::getJobOutputFile(const QModelIndex &index)
385 {
386         static QString nullStr;
387         
388         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
389         {
390                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
391                 return (thread != NULL) ? thread->outputFileName() : nullStr;
392         }
393
394         return nullStr;
395 }
396
397 EncodeThread::JobStatus JobListModel::getJobStatus(const QModelIndex &index)
398 {
399         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
400         {
401                 return m_status.value(m_jobs.at(index.row()));
402         }
403
404         return static_cast<EncodeThread::JobStatus>(-1);
405 }
406
407 unsigned int JobListModel::getJobProgress(const QModelIndex &index)
408 {
409         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
410         {
411                 return m_progress.value(m_jobs.at(index.row()));
412         }
413
414         return 0;
415 }
416
417 QModelIndex JobListModel::getJobIndexById(const QUuid &id)
418 {
419         if(m_jobs.contains(id))
420         {
421                 return createIndex(m_jobs.indexOf(id), 0);
422         }
423
424         return QModelIndex();
425 }
426
427 ///////////////////////////////////////////////////////////////////////////////
428 // Slots
429 ///////////////////////////////////////////////////////////////////////////////
430
431 void JobListModel::updateStatus(const QUuid &jobId, EncodeThread::JobStatus newStatus)
432 {
433         int index = -1;
434         
435         if((index = m_jobs.indexOf(jobId)) >= 0)
436         {
437                 m_status.insert(jobId, newStatus);
438                 emit dataChanged(createIndex(index, 0), createIndex(index, 1));
439         }
440 }
441
442 void JobListModel::updateProgress(const QUuid &jobId, unsigned int newProgress)
443 {
444         int index = -1;
445
446         if((index = m_jobs.indexOf(jobId)) >= 0)
447         {
448                 m_progress.insert(jobId, qBound(0U, newProgress, 100U));
449                 emit dataChanged(createIndex(index, 2), createIndex(index, 2));
450         }
451 }
452
453 void JobListModel::updateDetails(const QUuid &jobId, const QString &details)
454 {
455         int index = -1;
456
457         if((index = m_jobs.indexOf(jobId)) >= 0)
458         {
459                 m_details.insert(jobId, details);
460                 emit dataChanged(createIndex(index, 3), createIndex(index, 3));
461         }
462 }