OSDN Git Service

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