OSDN Git Service

Bump NVEncC binaries to version 5.06.
[x264-launcher/x264-launcher.git] / src / model_jobList.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2020 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 //Internal
23 #include "global.h"
24 #include "model_jobList.h"
25 #include "thread_encode.h"
26 #include "encoder_factory.h"
27 #include "model_options.h"
28 #include "model_preferences.h"
29 #include "resource.h"
30
31 //MUtils
32 #include <MUtils/Sound.h>
33
34 //Qt
35 #include <QIcon>
36 #include <QFileInfo>
37 #include <QSettings>
38
39 static const char *KEY_ENTRY_COUNT = "entry_count";
40 static const char *KEY_SOURCE_FILE = "source_file";
41 static const char *KEY_OUTPUT_FILE = "output_file";
42 static const char *KEY_ENC_OPTIONS = "enc_options";
43
44 static const char *JOB_TEMPLATE = "job_%08x";
45
46 #define VALID_INDEX(INDEX) ((INDEX).isValid() && ((INDEX).row() >= 0) && ((INDEX).row() < m_jobs.count()))
47
48 JobListModel::JobListModel(PreferencesModel *preferences)
49 {
50         m_preferences = preferences;
51 }
52
53 JobListModel::~JobListModel(void)
54 {
55         while(!m_jobs.isEmpty())
56         {
57                 QUuid id = m_jobs.takeFirst();
58                 EncodeThread *thread = m_threads.value(id, NULL);
59                 LogFileModel *logFile = m_logFile.value(id, NULL);
60                 MUTILS_DELETE(thread);
61                 MUTILS_DELETE(logFile);
62         }
63 }
64
65 ///////////////////////////////////////////////////////////////////////////////
66 // Model interface
67 ///////////////////////////////////////////////////////////////////////////////
68
69 int JobListModel::columnCount(const QModelIndex &parent) const
70 {
71         return 4;
72 }
73
74 int JobListModel::rowCount(const QModelIndex &parent) const
75 {
76         return m_jobs.count();
77 }
78
79 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const 
80 {
81         if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
82         {
83                 switch(section)
84                 {
85                 case 0:
86                         return QVariant::fromValue<QString>(tr("Job"));
87                         break;
88                 case 1:
89                         return QVariant::fromValue<QString>(tr("Status"));
90                         break;
91                 case 2:
92                         return QVariant::fromValue<QString>(tr("Progress"));
93                         break;
94                 case 3:
95                         return QVariant::fromValue<QString>(tr("Details"));
96                         break;
97                 default:
98                         return QVariant();
99                         break;
100                 }
101         }
102
103         return QVariant();
104 }
105
106 QModelIndex JobListModel::index(int row, int column, const QModelIndex &parent) const
107 {
108         return createIndex(row, column, NULL);
109 }
110
111 QModelIndex JobListModel::parent(const QModelIndex &index) const
112 {
113         return QModelIndex();
114 }
115
116 QVariant JobListModel::data(const QModelIndex &index, int role) const
117 {
118         if(role == Qt::DisplayRole)
119         {
120                 if(index.row() >= 0 && index.row() < m_jobs.count())
121                 {
122                         switch(index.column())
123                         {
124                         case 0:
125                                 return m_name.value(m_jobs.at(index.row()));
126                                 break;
127                         case 1:
128                                 switch(m_status.value(m_jobs.at(index.row())))
129                                 {
130                                 case JobStatus_Enqueued:
131                                         return QVariant::fromValue<QString>(tr("Enqueued."));
132                                         break;
133                                 case JobStatus_Starting:
134                                         return QVariant::fromValue<QString>(tr("Starting..."));
135                                         break;
136                                 case JobStatus_Indexing:
137                                         return QVariant::fromValue<QString>(tr("Indexing..."));
138                                         break;
139                                 case JobStatus_Running:
140                                         return QVariant::fromValue<QString>(tr("Running..."));
141                                         break;
142                                 case JobStatus_Running_Pass1:
143                                         return QVariant::fromValue<QString>(tr("Running... (Pass 1)"));
144                                         break;
145                                 case JobStatus_Running_Pass2:
146                                         return QVariant::fromValue<QString>(tr("Running... (Pass 2)"));
147                                         break;
148                                 case JobStatus_Completed:
149                                         return QVariant::fromValue<QString>(tr("Completed."));
150                                         break;
151                                 case JobStatus_Failed:
152                                         return QVariant::fromValue<QString>(tr("Failed!"));
153                                         break;
154                                 case JobStatus_Pausing:
155                                         return QVariant::fromValue<QString>(tr("Pausing..."));
156                                         break;
157                                 case JobStatus_Paused:
158                                         return QVariant::fromValue<QString>(tr("Paused."));
159                                         break;
160                                 case JobStatus_Resuming:
161                                         return QVariant::fromValue<QString>(tr("Resuming..."));
162                                         break;
163                                 case JobStatus_Aborting:
164                                         return QVariant::fromValue<QString>(tr("Aborting..."));
165                                         break;
166                                 case JobStatus_Aborted:
167                                         return QVariant::fromValue<QString>(tr("Aborted!"));
168                                         break;
169                                 default:
170                                         return QVariant::fromValue<QString>(tr("(Unknown)"));
171                                         break;
172                                 }
173                                 break;
174                         case 2:
175                                 return QString().sprintf("%d%%", m_progress.value(m_jobs.at(index.row())));
176                                 break;
177                         case 3:
178                                 return m_details.value(m_jobs.at(index.row()));
179                                 break;
180                         default:
181                                 return QVariant();
182                                 break;
183                         }
184                 }
185         }
186         else if(role == Qt::DecorationRole)
187         {
188                 if(index.row() >= 0 && index.row() < m_jobs.count() && index.column() == 0)
189                 {
190                         switch(m_status.value(m_jobs.at(index.row())))
191                         {
192                         case JobStatus_Enqueued:
193                                 return QIcon(":/buttons/hourglass.png");
194                                 break;
195                         case JobStatus_Starting:
196                                 return QIcon(":/buttons/lightning.png");
197                                 break;
198                         case JobStatus_Indexing:
199                                 return QIcon(":/buttons/find.png");
200                                 break;
201                         case JobStatus_Running:
202                         case JobStatus_Running_Pass1:
203                         case JobStatus_Running_Pass2:
204                                 return QIcon(":/buttons/play.png");
205                                 break;
206                         case JobStatus_Completed:
207                                 return QIcon(":/buttons/accept.png");
208                                 break;
209                         case JobStatus_Failed:
210                                 return QIcon(":/buttons/exclamation.png");
211                                 break;
212                         case JobStatus_Pausing:
213                                 return QIcon(":/buttons/clock_pause.png");
214                                 break;
215                         case JobStatus_Paused:
216                                 return QIcon(":/buttons/suspended.png");
217                                 break;
218                         case JobStatus_Resuming:
219                                 return QIcon(":/buttons/clock_play.png");
220                                 break;
221                         case JobStatus_Aborting:
222                                 return QIcon(":/buttons/clock_stop.png");
223                                 break;
224                         case JobStatus_Aborted:
225                                 return QIcon(":/buttons/error.png");
226                                 break;
227                         default:
228                                 return QVariant();
229                                 break;
230                         }
231                 }
232         }
233
234         return QVariant();
235 }
236
237 ///////////////////////////////////////////////////////////////////////////////
238 // Public interface
239 ///////////////////////////////////////////////////////////////////////////////
240
241 QModelIndex JobListModel::insertJob(EncodeThread *thread)
242 {
243         const QUuid id = thread->getId();
244         if(m_jobs.contains(id))
245         {
246                 return QModelIndex();
247         }
248         
249         const AbstractEncoderInfo &encoderInfo = EncoderFactory::getEncoderInfo(thread->options()->encType());
250         const QStringList encoderNameParts = encoderInfo.getName().simplified().split(' ', QString::SkipEmptyParts);
251         QString config = encoderNameParts.isEmpty() ? encoderInfo.getName() : encoderNameParts.first();
252         switch(encoderInfo.rcModeToType(thread->options()->rcMode()))
253         {
254         case AbstractEncoderInfo::RC_TYPE_QUANTIZER:
255                 config.append(QString(", %1@%2").arg(encoderInfo.rcModeToString(thread->options()->rcMode()), QString::number(qRound(thread->options()->quantizer()))));
256                 break;
257         case AbstractEncoderInfo::RC_TYPE_RATE_KBPS:
258         case AbstractEncoderInfo::RC_TYPE_MULTIPASS:
259                 config.append(QString(", %1@%2").arg(encoderInfo.rcModeToString(thread->options()->rcMode()), QString::number(thread->options()->bitrate())));
260                 break;
261         }
262
263         int n = 2;
264         QString jobName = QString("%1 [%2]").arg(QFileInfo(thread->sourceFileName()).completeBaseName().simplified(), config);
265         forever
266         {
267                 bool unique = true;
268                 for(int i = 0; i < m_jobs.count(); i++)
269                 {
270                         if(m_name.value(m_jobs.at(i)).compare(jobName, Qt::CaseInsensitive) == 0)
271                         {
272                                 unique = false;
273                                 break;
274                         }
275                 }
276                 if(!unique)
277                 {
278                         jobName = QString("%1 %2 [%3]").arg(QFileInfo(thread->sourceFileName()).completeBaseName().simplified(), QString::number(n++), config);
279                         continue;
280                 }
281                 break;
282         }
283         
284         LogFileModel *logFile = new LogFileModel(thread->sourceFileName(), thread->outputFileName(), config);
285         
286         beginInsertRows(QModelIndex(), m_jobs.count(), m_jobs.count());
287         m_jobs.append(id);
288         m_name.insert(id, jobName);
289         m_status.insert(id, JobStatus_Enqueued);
290         m_progress.insert(id, 0);
291         m_threads.insert(id, thread);
292         m_logFile.insert(id, logFile);
293         m_details.insert(id, tr("Not started yet."));
294         endInsertRows();
295
296         connect(thread, SIGNAL(statusChanged(QUuid, JobStatus)), this, SLOT(updateStatus(QUuid, JobStatus)), Qt::QueuedConnection);
297         connect(thread, SIGNAL(progressChanged(QUuid, unsigned int)), this, SLOT(updateProgress(QUuid, unsigned int)), Qt::QueuedConnection);
298         connect(thread, SIGNAL(messageLogged(QUuid, qint64, QString)), logFile, SLOT(addLogMessage(QUuid, qint64,  QString)), Qt::QueuedConnection);
299         connect(thread, SIGNAL(detailsChanged(QUuid, QString)), this, SLOT(updateDetails(QUuid, QString)), Qt::QueuedConnection);
300         
301         return createIndex(m_jobs.count() - 1, 0, NULL);
302 }
303
304 bool JobListModel::startJob(const QModelIndex &index)
305 {
306         if(VALID_INDEX(index))
307         {
308                 QUuid id = m_jobs.at(index.row());
309                 if(m_status.value(id) == JobStatus_Enqueued)
310                 {
311                         updateStatus(id, JobStatus_Starting);
312                         updateDetails(id, tr("Starting up, please wait..."));
313                         m_threads.value(id)->start();
314                         return true;
315                 }
316         }
317
318         return false;
319 }
320
321 bool JobListModel::pauseJob(const QModelIndex &index)
322 {
323         if(VALID_INDEX(index))
324         {
325                 QUuid id = m_jobs.at(index.row());
326                 JobStatus status = m_status.value(id);
327                 if((status == JobStatus_Indexing) || (status == JobStatus_Running) ||
328                         (status == JobStatus_Running_Pass1) || (status == JobStatus_Running_Pass2))
329                 {
330                         updateStatus(id, JobStatus_Pausing);
331                         m_threads.value(id)->pauseJob();
332                         return true;
333                 }
334         }
335
336         return false;
337 }
338
339 bool JobListModel::resumeJob(const QModelIndex &index)
340 {
341         if(VALID_INDEX(index))
342         {
343                 QUuid id = m_jobs.at(index.row());
344                 JobStatus status = m_status.value(id);
345                 if(status == JobStatus_Paused)
346                 {
347                         updateStatus(id, JobStatus_Resuming);
348                         m_threads.value(id)->resumeJob();
349                         return true;
350                 }
351         }
352
353         return false;
354 }
355
356 bool JobListModel::abortJob(const QModelIndex &index)
357 {
358         if(VALID_INDEX(index))
359         {
360                 QUuid id = m_jobs.at(index.row());
361                 if(m_status.value(id) == JobStatus_Indexing || m_status.value(id) == JobStatus_Running ||
362                         m_status.value(id) == JobStatus_Running_Pass1 || JobStatus_Running_Pass2)
363                 {
364                         updateStatus(id, JobStatus_Aborting);
365                         m_threads.value(id)->abortJob();
366                         return true;
367                 }
368         }
369
370         return false;
371 }
372
373 bool JobListModel::deleteJob(const QModelIndex &index)
374 {
375         if(VALID_INDEX(index))
376         {
377                 QUuid id = m_jobs.at(index.row());
378                 if(m_status.value(id) == JobStatus_Completed || m_status.value(id) == JobStatus_Failed ||
379                         m_status.value(id) == JobStatus_Aborted || m_status.value(id) == JobStatus_Enqueued)
380                 {
381                         int idx = index.row();
382                         QUuid id = m_jobs.at(idx);
383                         EncodeThread *thread = m_threads.value(id, NULL);
384                         LogFileModel *logFile = m_logFile.value(id, NULL);
385                         if((thread == NULL) || (!thread->isRunning()))
386                         {
387                                 
388                                 beginRemoveRows(QModelIndex(), idx, idx);
389                                 m_jobs.removeAt(index.row());
390                                 m_name.remove(id);
391                                 m_threads.remove(id);
392                                 m_status.remove(id);
393                                 m_progress.remove(id);
394                                 m_logFile.remove(id);
395                                 m_details.remove(id);
396                                 endRemoveRows();
397                                 MUTILS_DELETE(thread);
398                                 MUTILS_DELETE(logFile);
399                                 return true;
400                         }
401                 }
402         }
403
404         return false;
405 }
406
407 bool JobListModel::moveJob(const QModelIndex &index, const int &direction)
408 {
409         if(VALID_INDEX(index))
410         {
411                 if((direction == MOVE_UP) && (index.row() > 0))
412                 {
413                         beginMoveRows(QModelIndex(), index.row(), index.row(), QModelIndex(), index.row() - 1);
414                         m_jobs.swap(index.row(), index.row() - 1);
415                         endMoveRows();
416                         return true;
417                 }
418                 if((direction == MOVE_DOWN) && (index.row() < m_jobs.size() - 1))
419                 {
420                         beginMoveRows(QModelIndex(), index.row(), index.row(), QModelIndex(), index.row() + 2);
421                         m_jobs.swap(index.row(), index.row() + 1);
422                         endMoveRows();
423                         return true;
424                 }
425         }
426
427         return false;
428 }
429
430 LogFileModel *JobListModel::getLogFile(const QModelIndex &index)
431 {
432         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
433         {
434                 return m_logFile.value(m_jobs.at(index.row()));
435         }
436
437         return NULL;
438 }
439
440 const QString &JobListModel::getJobSourceFile(const QModelIndex &index)
441 {
442         static QString nullStr;
443         
444         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
445         {
446                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
447                 return (thread != NULL) ? thread->sourceFileName() : nullStr;
448         }
449
450         return nullStr;
451 }
452
453 const QString &JobListModel::getJobOutputFile(const QModelIndex &index)
454 {
455         static QString nullStr;
456         
457         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
458         {
459                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
460                 return (thread != NULL) ? thread->outputFileName() : nullStr;
461         }
462
463         return nullStr;
464 }
465
466 JobStatus JobListModel::getJobStatus(const QModelIndex &index)
467 {
468         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
469         {
470                 return m_status.value(m_jobs.at(index.row()));
471         }
472
473         return static_cast<JobStatus>(-1);
474 }
475
476 unsigned int JobListModel::getJobProgress(const QModelIndex &index)
477 {
478         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
479         {
480                 return m_progress.value(m_jobs.at(index.row()));
481         }
482
483         return 0;
484 }
485
486 const OptionsModel *JobListModel::getJobOptions(const QModelIndex &index)
487 {
488         static QString nullStr;
489         
490         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
491         {
492                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
493                 return (thread != NULL) ? thread->options() : NULL;
494         }
495
496         return NULL;
497 }
498
499 QModelIndex JobListModel::getJobIndexById(const QUuid &id)
500 {
501         if(m_jobs.contains(id))
502         {
503                 return createIndex(m_jobs.indexOf(id), 0);
504         }
505
506         return QModelIndex();
507 }
508
509 ///////////////////////////////////////////////////////////////////////////////
510 // Slots
511 ///////////////////////////////////////////////////////////////////////////////
512
513 void JobListModel::updateStatus(const QUuid &jobId, JobStatus newStatus)
514 {
515         int index = -1;
516         
517         if((index = m_jobs.indexOf(jobId)) >= 0)
518         {
519                 m_status.insert(jobId, newStatus);
520                 emit dataChanged(createIndex(index, 0), createIndex(index, 1));
521
522                 if(m_preferences->getEnableSounds())
523                 {
524                         switch(newStatus)
525                         {
526                         case JobStatus_Completed:
527                                 MUtils::Sound::play_sound("tada", true);
528                                 break;
529                         case JobStatus_Aborted:
530                                 MUtils::Sound::play_sound("shattering", true);
531                                 break;
532                         case JobStatus_Failed:
533                                 MUtils::Sound::play_sound("failure", true);
534                                 break;
535                         }
536                 }
537         }
538 }
539
540 void JobListModel::updateProgress(const QUuid &jobId, unsigned int newProgress)
541 {
542         int index = -1;
543
544         if((index = m_jobs.indexOf(jobId)) >= 0)
545         {
546                 m_progress.insert(jobId, qBound(0U, newProgress, 100U));
547                 emit dataChanged(createIndex(index, 2), createIndex(index, 2));
548         }
549 }
550
551 void JobListModel::updateDetails(const QUuid &jobId, const QString &details)
552 {
553         int index = -1;
554
555         if((index = m_jobs.indexOf(jobId)) >= 0)
556         {
557                 m_details.insert(jobId, details);
558                 emit dataChanged(createIndex(index, 3), createIndex(index, 3));
559         }
560 }
561
562 size_t JobListModel::saveQueuedJobs(void)
563 {
564         const QString appDir = x264_data_path();
565         QSettings settings(QString("%1/queue.ini").arg(appDir), QSettings::IniFormat);
566         
567         settings.clear();
568         settings.setValue(KEY_ENTRY_COUNT, 0);
569         size_t jobCounter = 0;
570
571         for(QList<QUuid>::ConstIterator iter = m_jobs.constBegin(); iter != m_jobs.constEnd(); iter++)
572         {
573                 if(m_status.value(*iter) == JobStatus_Enqueued)
574                 {
575                         if(const EncodeThread *thread = m_threads.value(*iter))
576                         {
577                                 settings.beginGroup(QString().sprintf(JOB_TEMPLATE, jobCounter++));
578                                 settings.setValue(KEY_SOURCE_FILE, thread->sourceFileName());
579                                 settings.setValue(KEY_OUTPUT_FILE, thread->outputFileName());
580
581                                 settings.beginGroup(KEY_ENC_OPTIONS);
582                                 OptionsModel::saveOptions(thread->options(), settings);
583
584                                 settings.endGroup();
585                                 settings.endGroup();
586
587                                 settings.setValue(KEY_ENTRY_COUNT, jobCounter);
588                         }
589                 }
590         }
591
592         settings.sync();
593         return jobCounter;
594 }
595
596 size_t JobListModel::loadQueuedJobs(const SysinfoModel *sysinfo)
597 {
598         const QString appDir = x264_data_path();
599         QSettings settings(QString("%1/queue.ini").arg(appDir), QSettings::IniFormat);
600
601         bool ok = false;
602         const size_t jobCounter = settings.value(KEY_ENTRY_COUNT, 0).toUInt(&ok);
603
604         if((!ok) || (jobCounter < 1))
605         {
606                 return 0;
607         }
608
609         const QStringList groups = settings.childGroups();
610         for(size_t i = 0; i < jobCounter; i++)
611         {
612                 if(!groups.contains(QString().sprintf(JOB_TEMPLATE, i)))
613                 {
614                         return 0;
615                 }
616         }
617
618         size_t jobsCreated = 0;
619         for(size_t i = 0; i < jobCounter; i++)
620         {
621                 settings.beginGroup(QString().sprintf(JOB_TEMPLATE, i));
622                 const QString sourceFileName = settings.value(KEY_SOURCE_FILE, QString()).toString().trimmed();
623                 const QString outputFileName = settings.value(KEY_OUTPUT_FILE, QString()).toString().trimmed();
624
625                 if(sourceFileName.isEmpty() || outputFileName.isEmpty())
626                 {
627                         settings.endGroup();
628                         continue;
629                 }
630
631                 settings.beginGroup(KEY_ENC_OPTIONS);
632                 OptionsModel options(sysinfo);
633                 const bool okay = OptionsModel::loadOptions(&options, settings);
634
635                 settings.endGroup();
636                 settings.endGroup();
637
638                 if(okay)
639                 {
640                         EncodeThread *thread = new EncodeThread(sourceFileName, outputFileName, &options, sysinfo, m_preferences);
641                         insertJob(thread);
642                         jobsCreated++;
643                 }
644         }
645
646         return jobsCreated;
647 }
648
649 void JobListModel::clearQueuedJobs(void)
650 {
651         const QString appDir = x264_data_path();
652         QSettings settings(QString("%1/queue.ini").arg(appDir), QSettings::IniFormat);
653         settings.clear();
654         settings.setValue(KEY_ENTRY_COUNT, 0);
655         settings.sync();
656 }