OSDN Git Service

Properly remember the last selected output file filter index.
[x264-launcher/x264-launcher.git] / src / win_addJob.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 "win_addJob.h"
23
24 #include "global.h"
25 #include "model_options.h"
26 #include "win_help.h"
27 #include "win_editor.h"
28
29 #include <QDate>
30 #include <QTimer>
31 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QFileDialog>
34 #include <QDesktopServices>
35 #include <QValidator>
36 #include <QDir>
37 #include <QInputDialog>
38 #include <QSettings>
39 #include <QUrl>
40 #include <QAction>
41 #include <QClipboard>
42
43 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
44
45 #define REMOVE_USAFED_ITEM \
46 { \
47         for(int i = 0; i < cbxTemplate->count(); i++) \
48         { \
49                 OptionsModel* temp = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>()); \
50                 if(temp == NULL) \
51                 { \
52                         cbxTemplate->blockSignals(true); \
53                         cbxTemplate->removeItem(i); \
54                         cbxTemplate->blockSignals(false); \
55                         break; \
56                 } \
57         } \
58 }
59
60 #define ADD_CONTEXTMENU_ACTION(WIDGET, ICON, TEXT, SLOTNAME) \
61 { \
62         QAction *_action = new QAction((ICON), (TEXT), this); \
63         _action->setData(QVariant::fromValue<void*>(WIDGET)); \
64         WIDGET->addAction(_action); \
65         connect(_action, SIGNAL(triggered(bool)), this, SLOT(SLOTNAME())); \
66 }
67
68 #define ADD_CONTEXTMENU_SEPARATOR(WIDGET) \
69 { \
70         QAction *_action = new QAction(this); \
71         _action->setSeparator(true); \
72         WIDGET->addAction(_action); \
73
74
75 ///////////////////////////////////////////////////////////////////////////////
76 // Validator
77 ///////////////////////////////////////////////////////////////////////////////
78
79 class StringValidator : public QValidator
80 {
81 public:
82         StringValidator(QLabel *notifier, QLabel *icon)
83         :
84                 m_notifier(notifier), m_icon(icon)
85         {
86                 m_notifier->hide();
87                 m_icon->hide();
88         }
89         
90         virtual State validate(QString &input, int &pos) const = 0;
91
92         virtual void fixup(QString &input) const
93         {
94                 input = input.simplified();
95         }
96
97 protected:
98         QLabel *const m_notifier, *const m_icon;
99
100         bool checkParam(const QString &input, const QString &param, const bool doubleMinus) const
101         {
102                 static const char c[20] = {' ', '*', '?', '<', '>', '/', '\\', '"', '\'', '!', '+', '#', '&', '%', '=', ',', ';', '.', 'ยด', '`'};
103                 const QString prefix = doubleMinus ? QLatin1String("--") : QLatin1String("-");
104                 
105                 bool flag = false;
106                 if(param.length() > 1)
107                 {
108                         flag = flag || input.endsWith(QString("%1%2").arg(prefix, param), Qt::CaseInsensitive);
109                         for(size_t i = 0; i < sizeof(c); i++)
110                         {
111                                 flag = flag || input.contains(QString("%1%2%3").arg(prefix, param, QChar::fromLatin1(c[i])), Qt::CaseInsensitive);
112                         }
113                 }
114                 else
115                 {
116                         flag = flag || input.startsWith(QString("-%1").arg(param));
117                         for(size_t i = 0; i < sizeof(c); i++)
118                         {
119                                 flag = flag || input.contains(QString("%1-%2").arg(QChar::fromLatin1(c[i]), param), Qt::CaseSensitive);
120                         }
121                 }
122                 if(flag)
123                 {
124                         if(m_notifier)
125                         {
126                                 m_notifier->setText(tr("Invalid parameter: %1").arg((param.length() > 1) ? QString("%1%2").arg(prefix, param) : QString("-%1").arg(param)));
127                                 if(m_notifier->isHidden()) m_notifier->show();
128                                 if(m_icon) { if(m_icon->isHidden()) m_icon->show(); }
129                         }
130                 }
131                 else
132                 {
133                         if(m_notifier)
134                         {
135                                 if(m_notifier->isVisible()) m_notifier->hide();
136                                 if(m_icon) { if(m_icon->isVisible()) m_icon->hide(); }
137                         }
138                 }
139                 return flag;
140         }
141 };
142
143 class StringValidatorX264 : public StringValidator
144 {
145 public:
146         StringValidatorX264(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
147
148         virtual State validate(QString &input, int &pos) const
149         {
150                 static const char* p[] = {"B", "o", "h", "p", "q", "fps", "frames", "preset", "tune", "profile",
151                         "stdin", "crf", "bitrate", "qp", "pass", "stats", "output", "help","quiet", NULL};
152
153                 bool invalid = false;
154
155                 for(size_t i = 0; p[i] && (!invalid); i++)
156                 {
157                         invalid = invalid || checkParam(input, QString::fromLatin1(p[i]), true);
158                 }
159
160                 return invalid ? QValidator::Intermediate : QValidator::Acceptable;
161         }
162 };
163
164 class StringValidatorAvs2YUV : public StringValidator
165 {
166 public:
167         StringValidatorAvs2YUV(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
168
169         virtual State validate(QString &input, int &pos) const
170         {
171                 static const char* p[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL};
172
173                 bool invalid = false;
174
175                 for(size_t i = 0; p[i] && (!invalid); i++)
176                 {
177                         invalid = invalid || checkParam(input, QString::fromLatin1(p[i]), false);
178                 }
179
180                 return invalid ? QValidator::Intermediate : QValidator::Acceptable;
181         }
182 };
183
184 ///////////////////////////////////////////////////////////////////////////////
185 // Constructor & Destructor
186 ///////////////////////////////////////////////////////////////////////////////
187
188 AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *options, bool x64supported)
189 :
190         QDialog(parent),
191         m_defaults(new OptionsModel()),
192         m_options(options),
193         m_x64supported(x64supported),
194         m_initialDir_src(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
195         m_initialDir_out(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
196         m_lastFilterIndex(0)
197 {
198         //Init the dialog, from the .ui file
199         setupUi(this);
200         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
201         
202         //Fix dialog size
203         buttonSaveTemplate->setMaximumHeight(20);
204         buttonDeleteTemplate->setMaximumHeight(20);
205         resize(width(), minimumHeight());
206         setMinimumSize(size());
207         setMaximumHeight(height());
208
209         //Setup file type filter
210         m_types.clear();
211         m_types << tr("Matroska Files (*.mkv)");
212         m_types << tr("MPEG-4 Part 14 Container (*.mp4)");
213         m_types << tr("H.264 Elementary Stream (*.264)");
214
215         //Monitor RC mode combobox
216         connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(modeIndexChanged(int)));
217
218         //Activate buttons
219         connect(buttonBrowseSource, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
220         connect(buttonBrowseOutput, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
221         connect(buttonSaveTemplate, SIGNAL(clicked()), this, SLOT(saveTemplateButtonClicked()));
222         connect(buttonDeleteTemplate, SIGNAL(clicked()), this, SLOT(deleteTemplateButtonClicked()));
223
224         //Setup validator
225         editCustomX264Params->installEventFilter(this);
226         editCustomX264Params->setValidator(new StringValidatorX264(labelNotificationX264, iconNotificationX264));
227         editCustomX264Params->clear();
228         editCustomAvs2YUVParams->installEventFilter(this);
229         editCustomAvs2YUVParams->setValidator(new StringValidatorAvs2YUV(labelNotificationAvs2YUV, iconNotificationAvs2YUV));
230         editCustomAvs2YUVParams->clear();
231
232         //Install event filter
233         labelHelpScreenX264->installEventFilter(this);
234         labelHelpScreenAvs2YUV->installEventFilter(this);
235
236         //Monitor for options changes
237         connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
238         connect(spinQuantizer, SIGNAL(valueChanged(double)), this, SLOT(configurationChanged()));
239         connect(spinBitrate, SIGNAL(valueChanged(int)), this, SLOT(configurationChanged()));
240         connect(cbxPreset, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
241         connect(cbxTuning, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
242         connect(cbxProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
243         connect(editCustomX264Params, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
244         connect(editCustomAvs2YUVParams, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
245
246         //Create context menus
247         ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
248         ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
249         ADD_CONTEXTMENU_SEPARATOR(editCustomX264Params);
250         ADD_CONTEXTMENU_SEPARATOR(editCustomAvs2YUVParams);
251         ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
252         ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
253         ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
254         ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
255
256         //Setup template selector
257         loadTemplateList();
258         connect(cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
259
260         //Load directories
261         const QString appDir = x264_data_path();
262         QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
263         m_initialDir_src = settings.value("path/directory_openFrom", m_initialDir_src).toString();
264         m_initialDir_out = settings.value("path/directory_saveTo", m_initialDir_out).toString();
265         m_lastFilterIndex = settings.value("path/filterIndex", m_lastFilterIndex).toInt();
266 }
267
268 AddJobDialog::~AddJobDialog(void)
269 {
270         //Free templates
271         for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
272         {
273                 if(cbxTemplate->itemText(i).startsWith("<") || cbxTemplate->itemText(i).endsWith(">"))
274                 {
275                         continue;
276                 }
277                 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
278                 cbxTemplate->setItemData(i, QVariant::fromValue<void*>(NULL));
279                 X264_DELETE(item);
280         }
281         
282         X264_DELETE(m_defaults);
283 }
284
285 ///////////////////////////////////////////////////////////////////////////////
286 // Events
287 ///////////////////////////////////////////////////////////////////////////////
288
289 void AddJobDialog::showEvent(QShowEvent *event)
290 {
291         QDialog::showEvent(event);
292         templateSelected();
293
294         if(!editSource->text().isEmpty()) m_initialDir_src = QFileInfo(QDir::fromNativeSeparators(editSource->text())).path();
295         if(!editOutput->text().isEmpty()) m_initialDir_out = QFileInfo(QDir::fromNativeSeparators(editOutput->text())).path();
296
297         if((!editSource->text().isEmpty()) && editOutput->text().isEmpty())
298         {
299                 generateOutputFileName(QDir::fromNativeSeparators(editSource->text()));
300                 buttonAccept->setFocus();
301         }
302
303         labelNotificationX264->hide();
304         iconNotificationX264->hide();
305         labelNotificationAvs2YUV->hide();
306         iconNotificationAvs2YUV->hide();
307 }
308
309 bool AddJobDialog::eventFilter(QObject *o, QEvent *e)
310 {
311         if((o == labelHelpScreenX264) && (e->type() == QEvent::MouseButtonPress))
312         {
313                 HelpDialog *helpScreen = new HelpDialog(this, false, m_x64supported);
314                 helpScreen->exec();
315                 X264_DELETE(helpScreen);
316         }
317         else if((o == labelHelpScreenAvs2YUV) && (e->type() == QEvent::MouseButtonPress))
318         {
319                 HelpDialog *helpScreen = new HelpDialog(this, true, m_x64supported);
320                 helpScreen->exec();
321                 X264_DELETE(helpScreen);
322         }
323         else if((o == editCustomX264Params) && (e->type() == QEvent::FocusOut))
324         {
325                 editCustomX264Params->setText(editCustomX264Params->text().simplified());
326         }
327         else if((o == editCustomAvs2YUVParams) && (e->type() == QEvent::FocusOut))
328         {
329                 editCustomAvs2YUVParams->setText(editCustomAvs2YUVParams->text().simplified());
330         }
331         return false;
332 }
333
334 void AddJobDialog::dragEnterEvent(QDragEnterEvent *event)
335 {
336         QStringList formats = event->mimeData()->formats();
337         
338         if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
339         {
340                 event->acceptProposedAction();
341         }
342 }
343
344 void AddJobDialog::dropEvent(QDropEvent *event)
345 {
346         QString droppedFile;
347         QList<QUrl> urls = event->mimeData()->urls();
348
349         if(urls.count() > 1)
350         {
351                 QDragEnterEvent dragEvent(event->pos(), event->proposedAction(), event->mimeData(), Qt::NoButton, Qt::NoModifier);
352                 if(qApp->notify(parent(), &dragEvent))
353                 {
354                         qApp->notify(parent(), event);
355                         reject(); return;
356                 }
357         }
358
359         while((!urls.isEmpty()) && droppedFile.isEmpty())
360         {
361                 QUrl currentUrl = urls.takeFirst();
362                 QFileInfo file(currentUrl.toLocalFile());
363                 if(file.exists() && file.isFile())
364                 {
365                         qDebug("AddJobDialog::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
366                         droppedFile = file.canonicalFilePath();
367                 }
368         }
369         
370         if(!droppedFile.isEmpty())
371         {
372                 editSource->setText(QDir::toNativeSeparators(droppedFile));
373                 generateOutputFileName(droppedFile);
374         }
375 }
376
377 ///////////////////////////////////////////////////////////////////////////////
378 // Slots
379 ///////////////////////////////////////////////////////////////////////////////
380
381 void AddJobDialog::modeIndexChanged(int index)
382 {
383         spinQuantizer->setEnabled(index == 0 || index == 1);
384         spinBitrate->setEnabled(index == 2 || index == 3);
385 }
386
387 void AddJobDialog::accept(void)
388 {
389         if(editSource->text().trimmed().isEmpty())
390         {
391                 QMessageBox::warning(this, tr("Not Found!"), tr("Please select a valid source file first!"));
392                 return;
393         }
394         
395         if(editOutput->text().trimmed().isEmpty())
396         {
397                 QMessageBox::warning(this, tr("Not Selected!"), tr("<nobr>Please select a valid output file first!</nobr>"));
398                 return;
399         }
400
401         QFileInfo sourceFile = QFileInfo(editSource->text());
402         if(!(sourceFile.exists() && sourceFile.isFile()))
403         {
404                 QMessageBox::warning(this, tr("Not Found!"), tr("<nobr>The selected source file could not be found!</nobr>"));
405                 return;
406         }
407
408         QFileInfo outputDir = QFileInfo(QFileInfo(editOutput->text()).path());
409         if(!(outputDir.exists() && outputDir.isDir() && outputDir.isWritable()))
410         {
411                 QMessageBox::warning(this, tr("Not Writable!"), tr("<nobr>Output directory does not exist or is not writable!</nobr>"));
412                 return;
413         }
414
415         QFileInfo outputFile = QFileInfo(editOutput->text());
416         if(outputFile.exists() && outputFile.isFile())
417         {
418                 int ret = QMessageBox::question(this, tr("Already Exists!"), tr("<nobr>Output file already exists! Overwrite?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
419                 if(ret != QMessageBox::Yes) return;
420         }
421         if(outputFile.exists() && (!outputFile.isFile()))
422         {
423                 QMessageBox::warning(this, tr("Not a File!"), tr("<nobr>Selected output file does not appear to be a valid file!</nobr>"));
424                 return;
425         }
426         if(!editCustomX264Params->hasAcceptableInput())
427         {
428                 int ret = QMessageBox::warning(this, tr("Invalid Params"), tr("<nobr>Your custom parameters are invalid and will be discarded!</nobr>"), QMessageBox::Ignore | QMessageBox::Cancel, QMessageBox::Cancel);
429                 if(ret != QMessageBox::Ignore) return;
430         }
431
432         //Save directories
433         const QString appDir = x264_data_path();
434         QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
435         if(settings.isWritable())
436         {
437                 settings.setValue("path/directory_saveTo", m_initialDir_out);
438                 settings.setValue("path/directory_openFrom", m_initialDir_src);
439                 settings.setValue("path/filterIndex", m_lastFilterIndex);
440                 settings.sync();
441         }
442
443         saveOptions(m_options);
444         QDialog::accept();
445 }
446
447 void AddJobDialog::browseButtonClicked(void)
448 {
449         if(QObject::sender() == buttonBrowseSource)
450         {
451                 QString initDir = VALID_DIR(m_initialDir_src) ? m_initialDir_src : QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
452                 if(!editSource->text().isEmpty()) initDir = QString("%1/%2").arg(initDir, QFileInfo(QDir::fromNativeSeparators(editSource->text())).fileName());
453
454                 QString filePath = QFileDialog::getOpenFileName(this, tr("Open Source File"), initDir, makeFileFilter(), NULL, QFileDialog::DontUseNativeDialog);
455                 if(!(filePath.isNull() || filePath.isEmpty()))
456                 {
457                         editSource->setText(QDir::toNativeSeparators(filePath));
458                         generateOutputFileName(filePath);
459                         m_initialDir_src = QFileInfo(filePath).path();
460                 }
461         }
462         else if(QObject::sender() == buttonBrowseOutput)
463         {
464                 QString initDir = VALID_DIR(m_initialDir_out) ? m_initialDir_out : QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
465                 if(!editOutput->text().isEmpty()) initDir = QString("%1/%2").arg(initDir, QFileInfo(QDir::fromNativeSeparators(editOutput->text())).completeBaseName());
466                 int filterIdx = getFilterIndex(QFileInfo(QDir::fromNativeSeparators(editOutput->text())).suffix());
467                 QString selectedType = m_types.at((filterIdx >= 0) ? filterIdx : m_lastFilterIndex);
468
469                 QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Output File"), initDir, m_types.join(";;"), &selectedType, QFileDialog::DontUseNativeDialog | QFileDialog::DontConfirmOverwrite);
470
471                 if(!(filePath.isNull() || filePath.isEmpty()))
472                 {
473                         if(getFilterIndex(QFileInfo(filePath).suffix()) < 0)
474                         {
475                                 filterIdx = m_types.indexOf(selectedType);
476                                 if(filterIdx >= 0)
477                                 {
478                                         filePath = QString("%1.%2").arg(filePath, getFilterExt(filterIdx));
479                                 }
480                         }
481                         editOutput->setText(QDir::toNativeSeparators(filePath));
482                         m_lastFilterIndex = getFilterIndex(QFileInfo(filePath).suffix());
483                         m_initialDir_out = QFileInfo(filePath).path();
484                 }
485         }
486 }
487
488 void AddJobDialog::configurationChanged(void)
489 {
490         OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
491         if(options)
492         {
493                 cbxTemplate->blockSignals(true);
494                 cbxTemplate->insertItem(0, tr("<Unsaved Configuration>"), QVariant::fromValue<void*>(NULL));
495                 cbxTemplate->setCurrentIndex(0);
496                 cbxTemplate->blockSignals(false);
497         }
498 }
499
500 void AddJobDialog::templateSelected(void)
501 {
502         OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
503         if(options)
504         {
505                 qDebug("Loading options!");
506                 REMOVE_USAFED_ITEM;
507                 restoreOptions(options);
508         }
509
510         modeIndexChanged(cbxRateControlMode->currentIndex());
511 }
512
513 void AddJobDialog::saveTemplateButtonClicked(void)
514 {
515         qDebug("Saving template");
516         QString name = tr("New Template");
517         int n = 2;
518
519         while(OptionsModel::templateExists(name))
520         {
521                 name = tr("New Template (%1)").arg(QString::number(n++));
522         }
523
524         OptionsModel *options = new OptionsModel();
525         saveOptions(options);
526
527         if(options->equals(m_defaults))
528         {
529                 QMessageBox::warning (this, tr("Oups"), tr("<nobr>It makes no sense to save the default settings!</nobr>"));
530                 cbxTemplate->blockSignals(true);
531                 cbxTemplate->setCurrentIndex(0);
532                 cbxTemplate->blockSignals(false);
533                 REMOVE_USAFED_ITEM;
534                 X264_DELETE(options);
535                 return;
536         }
537
538         for(int i = 0; i < cbxTemplate->count(); i++)
539         {
540                 const QString tempName = cbxTemplate->itemText(i);
541                 if(tempName.contains('<') || tempName.contains('>'))
542                 {
543                         continue;
544                 }
545                 OptionsModel* test = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
546                 if(test != NULL)
547                 {
548                         if(options->equals(test))
549                         {
550                                 QMessageBox::warning (this, tr("Oups"), tr("<nobr>There already is a template for the current settings!</nobr>"));
551                                 cbxTemplate->blockSignals(true);
552                                 cbxTemplate->setCurrentIndex(i);
553                                 cbxTemplate->blockSignals(false);
554                                 REMOVE_USAFED_ITEM;
555                                 X264_DELETE(options);
556                                 return;
557                         }
558                 }
559         }
560
561         forever
562         {
563                 bool ok = false;
564                 name = QInputDialog::getText(this, tr("Save Template"), tr("Please enter the name of the template:").leftJustified(144, ' '), QLineEdit::Normal, name, &ok).simplified();
565                 if(!ok)
566                 {
567                         X264_DELETE(options);
568                         return;
569                 }
570                 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/') || name.contains('"'))
571                 {
572                         QMessageBox::warning (this, tr("Invalid Name"), tr("<nobr>Sorry, the name you have entered is invalid!</nobr>"));
573                         while(name.contains('<')) name.remove('<');
574                         while(name.contains('>')) name.remove('>');
575                         while(name.contains('\\')) name.remove('\\');
576                         while(name.contains('/')) name.remove('/');
577                         while(name.contains('"')) name.remove('"');
578                         name = name.simplified();
579                         continue;
580                 }
581                 if(OptionsModel::templateExists(name))
582                 {
583                         int ret = QMessageBox::warning (this, tr("Already Exists"), tr("<nobr>A template of that name already exists! Overwrite?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
584                         if(ret != QMessageBox::Yes)
585                         {
586                                 continue;
587                         }
588                 }
589                 break;
590         }
591         
592         if(!OptionsModel::saveTemplate(options, name))
593         {
594                 QMessageBox::critical(this, tr("Save Failed"), tr("Sorry, the template could not be saved!"));
595                 X264_DELETE(options);
596                 return;
597         }
598         
599         int index = cbxTemplate->model()->rowCount();
600         cbxTemplate->blockSignals(true);
601         for(int i = 0; i < cbxTemplate->count(); i++)
602         {
603                 if(cbxTemplate->itemText(i).compare(name, Qt::CaseInsensitive) == 0)
604                 {
605                         index = -1; //Do not append new template
606                         OptionsModel *oldItem = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
607                         cbxTemplate->setItemData(i, QVariant::fromValue<void*>(options));
608                         cbxTemplate->setCurrentIndex(i);
609                         X264_DELETE(oldItem);
610                 }
611         }
612         if(index >= 0)
613         {
614                 cbxTemplate->insertItem(index, name, QVariant::fromValue<void*>(options));
615                 cbxTemplate->setCurrentIndex(index);
616         }
617         cbxTemplate->blockSignals(false);
618
619         REMOVE_USAFED_ITEM;
620 }
621
622 void AddJobDialog::deleteTemplateButtonClicked(void)
623 {
624         const int index = cbxTemplate->currentIndex();
625         QString name = cbxTemplate->itemText(index);
626
627         if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/'))
628         {
629                 QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
630                 return;
631         }
632
633         int ret = QMessageBox::question (this, tr("Delete Template"), tr("<nobr>Do you really want to delete the selected template?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
634         if(ret != QMessageBox::Yes)
635         {
636                 return;
637         }
638
639
640         OptionsModel::deleteTemplate(name);
641         OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(index).value<void*>());
642         cbxTemplate->removeItem(index);
643         X264_DELETE(item);
644 }
645
646 void AddJobDialog::editorActionTriggered(void)
647 {
648
649         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
650         {
651                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
652                 
653                 EditorDialog *editor = new EditorDialog(this);
654                 editor->setEditText(lineEdit->text());
655
656                 if(editor->exec() == QDialog::Accepted)
657                 {
658                         lineEdit->setText(editor->getEditText());
659                 }
660
661                 X264_DELETE(editor);
662         }
663 }
664
665 void AddJobDialog::copyActionTriggered(void)
666 {
667         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
668         {
669                 QClipboard *clipboard = QApplication::clipboard();
670                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
671                 QString text = lineEdit->hasSelectedText() ? lineEdit->selectedText() : lineEdit->text();
672                 clipboard->setText(text);
673         }
674 }
675
676 void AddJobDialog::pasteActionTriggered(void)
677 {
678         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
679         {
680                 QClipboard *clipboard = QApplication::clipboard();
681                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
682                 QString text = clipboard->text();
683                 if(!text.isEmpty()) lineEdit->setText(text);
684         }
685 }
686
687 ///////////////////////////////////////////////////////////////////////////////
688 // Public functions
689 ///////////////////////////////////////////////////////////////////////////////
690
691 QString AddJobDialog::sourceFile(void)
692 {
693         return QDir::fromNativeSeparators(editSource->text());
694 }
695
696 QString AddJobDialog::outputFile(void)
697 {
698         return QDir::fromNativeSeparators(editOutput->text());
699 }
700
701 ///////////////////////////////////////////////////////////////////////////////
702 // Private functions
703 ///////////////////////////////////////////////////////////////////////////////
704
705 void AddJobDialog::loadTemplateList(void)
706 {
707         cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
708         cbxTemplate->setCurrentIndex(0);
709
710         QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates();
711         QStringList templateNames = templates.keys();
712         templateNames.sort();
713
714         while(!templateNames.isEmpty())
715         {
716                 QString current = templateNames.takeFirst();
717                 cbxTemplate->addItem(current, QVariant::fromValue<void*>(templates.value(current)));
718
719                 if(templates.value(current)->equals(m_options))
720                 {
721                         cbxTemplate->setCurrentIndex(cbxTemplate->count() - 1);
722                 }
723         }
724
725         if((cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
726         {
727                 cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
728                 cbxTemplate->setCurrentIndex(1);
729         }
730 }
731
732 void AddJobDialog::updateComboBox(QComboBox *cbox, const QString &text)
733 {
734         for(int i = 0; i < cbox->model()->rowCount(); i++)
735         {
736                 if(cbox->model()->data(cbox->model()->index(i, 0, QModelIndex())).toString().compare(text, Qt::CaseInsensitive) == 0)
737                 {
738                         cbox->setCurrentIndex(i);
739                         break;
740                 }
741         }
742 }
743
744 void AddJobDialog::restoreOptions(OptionsModel *options)
745 {
746         cbxRateControlMode->blockSignals(true);
747         spinQuantizer->blockSignals(true);
748         spinBitrate->blockSignals(true);
749         cbxPreset->blockSignals(true);
750         cbxTuning->blockSignals(true);
751         cbxProfile->blockSignals(true);
752         editCustomX264Params->blockSignals(true);
753         editCustomAvs2YUVParams->blockSignals(true);
754
755         cbxRateControlMode->setCurrentIndex(options->rcMode());
756         spinQuantizer->setValue(options->quantizer());
757         spinBitrate->setValue(options->bitrate());
758         updateComboBox(cbxPreset, options->preset());
759         updateComboBox(cbxTuning, options->tune());
760         updateComboBox(cbxProfile, options->profile());
761         editCustomX264Params->setText(options->customX264());
762         editCustomAvs2YUVParams->setText(options->customAvs2YUV());
763
764         cbxRateControlMode->blockSignals(false);
765         spinQuantizer->blockSignals(false);
766         spinBitrate->blockSignals(false);
767         cbxPreset->blockSignals(false);
768         cbxTuning->blockSignals(false);
769         cbxProfile->blockSignals(false);
770         editCustomX264Params->blockSignals(false);
771         editCustomAvs2YUVParams->blockSignals(false);
772 }
773
774 void AddJobDialog::saveOptions(OptionsModel *options)
775 {
776         options->setRCMode(static_cast<OptionsModel::RCMode>(cbxRateControlMode->currentIndex()));
777         options->setQuantizer(spinQuantizer->value());
778         options->setBitrate(spinBitrate->value());
779         options->setPreset(cbxPreset->model()->data(cbxPreset->model()->index(cbxPreset->currentIndex(), 0)).toString());
780         options->setTune(cbxTuning->model()->data(cbxTuning->model()->index(cbxTuning->currentIndex(), 0)).toString());
781         options->setProfile(cbxProfile->model()->data(cbxProfile->model()->index(cbxProfile->currentIndex(), 0)).toString());
782         options->setCustomX264(editCustomX264Params->hasAcceptableInput() ? editCustomX264Params->text().simplified() : QString());
783         options->setCustomAvs2YUV(editCustomAvs2YUVParams->hasAcceptableInput() ? editCustomAvs2YUVParams->text().simplified() : QString());
784 }
785
786 QString AddJobDialog::makeFileFilter(void)
787 {
788         static const struct
789         {
790                 const char *name;
791                 const char *fext;
792         }
793         s_filters[] =
794         {
795                 {"Avisynth Scripts", "avs"},
796                 {"Matroska Files", "mkv"},
797                 {"MPEG-4 Part 14 Container", "mp4"},
798                 {"Audio Video Interleaved", "avi"},
799                 {"Flash Video", "flv"},
800                 {"YUV4MPEG2 Stream", "y4m"},
801                 {"Uncompresses YUV Data", "yuv"},
802                 {NULL, NULL}
803         };
804
805         QString filters("All supported files (");
806
807         for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
808         {
809                 filters += QString((index > 0) ? " *.%1" : "*.%1").arg(QString::fromLatin1(s_filters[index].fext));
810         }
811
812         filters += QString(");;");
813
814         for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
815         {
816                 filters += QString("%1 (*.%2);;").arg(QString::fromLatin1(s_filters[index].name), QString::fromLatin1(s_filters[index].fext));
817         }
818                 
819         filters += QString("All files (*.*)");
820         return filters;
821 }
822
823 void AddJobDialog::generateOutputFileName(const QString &filePath)
824 {
825         QString name = QFileInfo(filePath).completeBaseName();
826         QString path = VALID_DIR(m_initialDir_out) ? m_initialDir_out : QFileInfo(filePath).path();
827         QString fext = getFilterExt(m_lastFilterIndex);
828                         
829         QString outPath = QString("%1/%2.%3").arg(path, name, fext);
830
831         if(QFileInfo(outPath).exists())
832         {
833                 int i = 2;
834                 while(QFileInfo(outPath).exists())
835                 {
836                         outPath = QString("%1/%2 (%3).%4").arg(path, name, QString::number(i++), fext);
837                 }
838         }
839
840         editOutput->setText(QDir::toNativeSeparators(outPath));
841 }
842
843 int AddJobDialog::getFilterIndex(const QString &fileExt)
844 {
845         if(!fileExt.isEmpty())
846         {
847                 QRegExp ext("\\(\\*\\.(.+)\\)");
848                 for(int i = 0; i < m_types.count(); i++)
849                 {
850                         if(ext.lastIndexIn(m_types.at(i)) >= 0)
851                         {
852                                 if(fileExt.compare(ext.cap(1), Qt::CaseInsensitive) == 0)
853                                 {
854                                         return i;
855                                 }
856                         }
857                 }
858         }
859
860         return -1;
861 }
862
863 QString AddJobDialog::getFilterExt(int filterIdx)
864 {
865         int index = qBound(0, filterIdx, m_types.count()-1);
866
867         QRegExp ext("\\(\\*\\.(.+)\\)");
868         if(ext.lastIndexIn(m_types.at(index)) >= 0)
869         {
870                 return ext.cap(1).toLower();
871         }
872
873         return QString::fromLatin1("mkv");
874 }