OSDN Git Service

Happy new year 2014!
[x264-launcher/x264-launcher.git] / src / win_main.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 "win_main.h"
23 #include "uic_win_main.h"
24
25 #include "global.h"
26 #include "model_status.h"
27 #include "model_jobList.h"
28 #include "model_options.h"
29 #include "model_preferences.h"
30 #include "model_recently.h"
31 #include "thread_avisynth.h"
32 #include "thread_vapoursynth.h"
33 #include "thread_encode.h"
34 #include "taskbar7.h"
35 #include "win_addJob.h"
36 #include "win_preferences.h"
37 #include "win_updater.h"
38 #include "resource.h"
39
40 #include <QDate>
41 #include <QTimer>
42 #include <QCloseEvent>
43 #include <QMessageBox>
44 #include <QDesktopServices>
45 #include <QUrl>
46 #include <QDir>
47 #include <QLibrary>
48 #include <QProcess>
49 #include <QProgressDialog>
50 #include <QScrollBar>
51 #include <QTextStream>
52 #include <QSettings>
53 #include <QFileDialog>
54
55 #include <ctime>
56
57 const char *home_url = "http://muldersoft.com/";
58 const char *update_url = "https://github.com/lordmulder/Simple-x264-Launcher/releases/latest";
59 const char *tpl_last = "<LAST_USED>";
60
61 #define SET_FONT_BOLD(WIDGET,BOLD) do { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); } while(0)
62 #define SET_TEXT_COLOR(WIDGET,COLOR) do { QPalette _palette = WIDGET->palette(); _palette.setColor(QPalette::WindowText, (COLOR)); _palette.setColor(QPalette::Text, (COLOR)); WIDGET->setPalette(_palette); } while(0)
63 #define LINK(URL) "<a href=\"" URL "\">" URL "</a>"
64 #define INIT_ERROR_EXIT() do { m_initialized = true; close(); qApp->exit(-1); return; } while(0)
65
66 //static int exceptionFilter(_EXCEPTION_RECORD *dst, _EXCEPTION_POINTERS *src) { memcpy(dst, src->ExceptionRecord, sizeof(_EXCEPTION_RECORD)); return EXCEPTION_EXECUTE_HANDLER; }
67
68 ///////////////////////////////////////////////////////////////////////////////
69 // Constructor & Destructor
70 ///////////////////////////////////////////////////////////////////////////////
71
72 /*
73  * Constructor
74  */
75 MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures)
76 :
77         m_cpuFeatures(cpuFeatures),
78         m_appDir(QApplication::applicationDirPath()),
79         m_options(NULL),
80         m_jobList(NULL),
81         m_droppedFiles(NULL),
82         m_preferences(NULL),
83         m_recentlyUsed(NULL),
84         m_skipVersionTest(false),
85         m_abortOnTimeout(true),
86         m_firstShow(true),
87         m_initialized(false),
88         ui(new Ui::MainWindow())
89 {
90         //Init the dialog, from the .ui file
91         ui->setupUi(this);
92         setWindowFlags(windowFlags() & (~Qt::WindowMaximizeButtonHint));
93
94         //Register meta types
95         qRegisterMetaType<QUuid>("QUuid");
96         qRegisterMetaType<QUuid>("DWORD");
97         qRegisterMetaType<JobStatus>("JobStatus");
98
99         //Load preferences
100         m_preferences = new PreferencesModel();
101         PreferencesModel::loadPreferences(m_preferences);
102
103         //Load recently used
104         m_recentlyUsed = new RecentlyUsed();
105         RecentlyUsed::loadRecentlyUsed(m_recentlyUsed);
106
107         //Create options object
108         m_options = new OptionsModel();
109         OptionsModel::loadTemplate(m_options, QString::fromLatin1(tpl_last));
110
111         //Create IPC thread object
112         //m_ipcThread = new IPCThread();
113         //connect(m_ipcThread, SIGNAL(instanceCreated(unsigned int)), this, SLOT(instanceCreated(unsigned int)), Qt::QueuedConnection);
114
115         //Freeze minimum size
116         setMinimumSize(size());
117         ui->splitter->setSizes(QList<int>() << 16 << 196);
118
119         //Update title
120         ui->labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));
121         ui->labelBuildDate->installEventFilter(this);
122         setWindowTitle(QString("%1 (%2 Mode)").arg(windowTitle(), m_cpuFeatures->x64 ? "64-Bit" : "32-Bit"));
123         if(X264_DEBUG)
124         {
125                 setWindowTitle(QString("%1 | !!! DEBUG VERSION !!!").arg(windowTitle()));
126                 setStyleSheet("QMenuBar, QMainWindow { background-color: yellow }");
127         }
128         else if(x264_is_prerelease())
129         {
130                 setWindowTitle(QString("%1 | PRE-RELEASE VERSION").arg(windowTitle()));
131         }
132         
133         //Create model
134         m_jobList = new JobListModel(m_preferences);
135         connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
136         ui->jobsView->setModel(m_jobList);
137         
138         //Setup view
139         ui->jobsView->horizontalHeader()->setSectionHidden(3, true);
140         ui->jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
141         ui->jobsView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
142         ui->jobsView->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
143         ui->jobsView->horizontalHeader()->resizeSection(1, 150);
144         ui->jobsView->horizontalHeader()->resizeSection(2, 90);
145         ui->jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
146         connect(ui->jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
147
148         //Create context menu
149         QAction *actionClipboard = new QAction(QIcon(":/buttons/page_paste.png"), tr("Copy to Clipboard"), ui->logView);
150         actionClipboard->setEnabled(false);
151         ui->logView->addAction(actionClipboard);
152         connect(actionClipboard, SIGNAL(triggered(bool)), this, SLOT(copyLogToClipboard(bool)));
153         ui->jobsView->addActions(ui->menuJob->actions());
154
155         //Enable buttons
156         connect(ui->buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
157         connect(ui->buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
158         connect(ui->buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
159         connect(ui->buttonPauseJob, SIGNAL(toggled(bool)), this, SLOT(pauseButtonPressed(bool)));
160         connect(ui->actionJob_Delete, SIGNAL(triggered()), this, SLOT(deleteButtonPressed()));
161         connect(ui->actionJob_Restart, SIGNAL(triggered()), this, SLOT(restartButtonPressed()));
162         connect(ui->actionJob_Browse, SIGNAL(triggered()), this, SLOT(browseButtonPressed()));
163
164         //Enable menu
165         connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openActionTriggered()));
166         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
167         connect(ui->actionWebMulder, SIGNAL(triggered()), this, SLOT(showWebLink()));
168         connect(ui->actionWebX264, SIGNAL(triggered()), this, SLOT(showWebLink()));
169         connect(ui->actionWebKomisar, SIGNAL(triggered()), this, SLOT(showWebLink()));
170         connect(ui->actionWebVideoLAN, SIGNAL(triggered()), this, SLOT(showWebLink()));
171         connect(ui->actionWebJEEB, SIGNAL(triggered()), this, SLOT(showWebLink()));
172         connect(ui->actionWebAvisynth32, SIGNAL(triggered()), this, SLOT(showWebLink()));
173         connect(ui->actionWebAvisynth64, SIGNAL(triggered()), this, SLOT(showWebLink()));
174         connect(ui->actionWebVapourSynth, SIGNAL(triggered()), this, SLOT(showWebLink()));
175         connect(ui->actionWebVapourSynthDocs, SIGNAL(triggered()), this, SLOT(showWebLink()));
176         connect(ui->actionWebWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
177         connect(ui->actionWebBluRay, SIGNAL(triggered()), this, SLOT(showWebLink()));
178         connect(ui->actionWebAvsWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
179         connect(ui->actionWebSecret, SIGNAL(triggered()), this, SLOT(showWebLink()));
180         connect(ui->actionWebSupport, SIGNAL(triggered()), this, SLOT(showWebLink()));
181         connect(ui->actionPreferences, SIGNAL(triggered()), this, SLOT(showPreferences()));
182         connect(ui->actionCheckForUpdates, SIGNAL(triggered()), this, SLOT(checkUpdates()));
183
184         //Create floating label
185         m_label = new QLabel(ui->jobsView->viewport());
186         m_label->setText(tr("No job created yet. Please click the 'Add New Job' button!"));
187         m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
188         SET_TEXT_COLOR(m_label, Qt::darkGray);
189         SET_FONT_BOLD(m_label, true);
190         m_label->setVisible(true);
191         m_label->setContextMenuPolicy(Qt::ActionsContextMenu);
192         m_label->addActions(ui->jobsView->actions());
193         connect(ui->splitter, SIGNAL(splitterMoved(int, int)), this, SLOT(updateLabelPos()));
194         updateLabelPos();
195 }
196
197 /*
198  * Destructor
199  */
200 MainWindow::~MainWindow(void)
201 {
202         OptionsModel::saveTemplate(m_options, QString::fromLatin1(tpl_last));
203         
204         X264_DELETE(m_jobList);
205         X264_DELETE(m_options);
206         X264_DELETE(m_droppedFiles);
207         X264_DELETE(m_label);
208
209         while(!m_toolsList.isEmpty())
210         {
211                 QFile *temp = m_toolsList.takeFirst();
212                 X264_DELETE(temp);
213         }
214
215         /*
216         if(m_ipcThread->isRunning())
217         {
218                 m_ipcThread->setAbort();
219                 if(!m_ipcThread->wait(5000))
220                 {
221                         m_ipcThread->terminate();
222                         m_ipcThread->wait();
223                 }
224         }
225         */
226
227         //X264_DELETE(m_ipcThread);
228         X264_DELETE(m_preferences);
229         X264_DELETE(m_recentlyUsed);
230         VapourSynthCheckThread::unload();
231         AvisynthCheckThread::unload();
232
233         delete ui;
234 }
235
236 ///////////////////////////////////////////////////////////////////////////////
237 // Slots
238 ///////////////////////////////////////////////////////////////////////////////
239
240 /*
241  * The "add" button was clicked
242  */
243 void MainWindow::addButtonPressed()
244 {
245         qDebug("MainWindow::addButtonPressed");
246         bool runImmediately = (countRunningJobs() < (m_preferences->autoRunNextJob() ? m_preferences->maxRunningJobCount() : 1));
247         QString sourceFileName, outputFileName;
248         
249         if(createJob(sourceFileName, outputFileName, m_options, runImmediately))
250         {
251                 appendJob(sourceFileName, outputFileName, m_options, runImmediately);
252         }
253 }
254
255 /*
256  * The "open" action was triggered
257  */
258 void MainWindow::openActionTriggered()
259 {
260         QStringList fileList = QFileDialog::getOpenFileNames(this, tr("Open Source File(s)"), m_recentlyUsed->sourceDirectory(), AddJobDialog::getInputFilterLst(), NULL, QFileDialog::DontUseNativeDialog);
261         if(!fileList.empty())
262         {
263                 m_recentlyUsed->setSourceDirectory(QFileInfo(fileList.last()).absolutePath());
264                 if(fileList.count() > 1)
265                 {
266                         createJobMultiple(fileList);
267                 }
268                 else
269                 {
270                         bool runImmediately = (countRunningJobs() < (m_preferences->autoRunNextJob() ? m_preferences->maxRunningJobCount() : 1));
271                         QString sourceFileName(fileList.first()), outputFileName;
272                         if(createJob(sourceFileName, outputFileName, m_options, runImmediately))
273                         {
274                                 appendJob(sourceFileName, outputFileName, m_options, runImmediately);
275                         }
276                 }
277         }
278 }
279
280 /*
281  * The "start" button was clicked
282  */
283 void MainWindow::startButtonPressed(void)
284 {
285         m_jobList->startJob(ui->jobsView->currentIndex());
286 }
287
288 /*
289  * The "abort" button was clicked
290  */
291 void MainWindow::abortButtonPressed(void)
292 {
293         m_jobList->abortJob(ui->jobsView->currentIndex());
294 }
295
296 /*
297  * The "delete" button was clicked
298  */
299 void MainWindow::deleteButtonPressed(void)
300 {
301         m_jobList->deleteJob(ui->jobsView->currentIndex());
302         m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
303 }
304
305 /*
306  * The "browse" button was clicked
307  */
308 void MainWindow::browseButtonPressed(void)
309 {
310         QString outputFile = m_jobList->getJobOutputFile(ui->jobsView->currentIndex());
311         if((!outputFile.isEmpty()) && QFileInfo(outputFile).exists() && QFileInfo(outputFile).isFile())
312         {
313                 QProcess::startDetached(QString::fromLatin1("explorer.exe"), QStringList() << QString::fromLatin1("/select,") << QDir::toNativeSeparators(outputFile), QFileInfo(outputFile).path());
314         }
315         else
316         {
317                 QMessageBox::warning(this, tr("Not Found"), tr("Sorry, the output file could not be found!"));
318         }
319 }
320
321 /*
322  * The "pause" button was clicked
323  */
324 void MainWindow::pauseButtonPressed(bool checked)
325 {
326         if(checked)
327         {
328                 m_jobList->pauseJob(ui->jobsView->currentIndex());
329         }
330         else
331         {
332                 m_jobList->resumeJob(ui->jobsView->currentIndex());
333         }
334 }
335
336 /*
337  * The "restart" button was clicked
338  */
339 void MainWindow::restartButtonPressed(void)
340 {
341         const QModelIndex index = ui->jobsView->currentIndex();
342         const OptionsModel *options = m_jobList->getJobOptions(index);
343         QString sourceFileName = m_jobList->getJobSourceFile(index);
344         QString outputFileName = m_jobList->getJobOutputFile(index);
345
346         if((options) && (!sourceFileName.isEmpty()) && (!outputFileName.isEmpty()))
347         {
348                 bool runImmediately = (countRunningJobs() < (m_preferences->autoRunNextJob() ? m_preferences->maxRunningJobCount() : 1)); //bool runImmediately = true;
349                 OptionsModel *tempOptions = new OptionsModel(*options);
350                 if(createJob(sourceFileName, outputFileName, tempOptions, runImmediately, true))
351                 {
352                         appendJob(sourceFileName, outputFileName, tempOptions, runImmediately);
353                 }
354                 X264_DELETE(tempOptions);
355         }
356 }
357
358 /*
359  * Job item selected by user
360  */
361 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
362 {
363         qDebug("Job selected: %d", current.row());
364         
365         if(ui->logView->model())
366         {
367                 disconnect(ui->logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
368         }
369         
370         if(current.isValid())
371         {
372                 ui->logView->setModel(m_jobList->getLogFile(current));
373                 connect(ui->logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
374                 ui->logView->actions().first()->setEnabled(true);
375                 QTimer::singleShot(0, ui->logView, SLOT(scrollToBottom()));
376
377                 ui->progressBar->setValue(m_jobList->getJobProgress(current));
378                 ui->editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
379                 updateButtons(m_jobList->getJobStatus(current));
380                 updateTaskbar(m_jobList->getJobStatus(current), m_jobList->data(m_jobList->index(current.row(), 0, QModelIndex()), Qt::DecorationRole).value<QIcon>());
381         }
382         else
383         {
384                 ui->logView->setModel(NULL);
385                 ui->logView->actions().first()->setEnabled(false);
386                 ui->progressBar->setValue(0);
387                 ui->editDetails->clear();
388                 updateButtons(JobStatus_Undefined);
389                 updateTaskbar(JobStatus_Undefined, QIcon());
390         }
391
392         ui->progressBar->repaint();
393 }
394
395 /*
396  * Handle update of job info (status, progress, details, etc)
397  */
398 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
399 {
400         int selected = ui->jobsView->currentIndex().row();
401         
402         if(topLeft.column() <= 1 && bottomRight.column() >= 1) /*STATUS*/
403         {
404                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
405                 {
406                         JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
407                         if(i == selected)
408                         {
409                                 qDebug("Current job changed status!");
410                                 updateButtons(status);
411                                 updateTaskbar(status, m_jobList->data(m_jobList->index(i, 0, QModelIndex()), Qt::DecorationRole).value<QIcon>());
412                         }
413                         if((status == JobStatus_Completed) || (status == JobStatus_Failed))
414                         {
415                                 if(m_preferences->autoRunNextJob()) QTimer::singleShot(0, this, SLOT(launchNextJob()));
416                                 if(m_preferences->shutdownComputer()) QTimer::singleShot(0, this, SLOT(shutdownComputer()));
417                                 if(m_preferences->saveLogFiles()) saveLogFile(m_jobList->index(i, 1, QModelIndex()));
418                         }
419                 }
420         }
421         if(topLeft.column() <= 2 && bottomRight.column() >= 2) /*PROGRESS*/
422         {
423                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
424                 {
425                         if(i == selected)
426                         {
427                                 ui->progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
428                                 WinSevenTaskbar::setTaskbarProgress(this, ui->progressBar->value(), ui->progressBar->maximum());
429                                 break;
430                         }
431                 }
432         }
433         if(topLeft.column() <= 3 && bottomRight.column() >= 3) /*DETAILS*/
434         {
435                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
436                 {
437                         if(i == selected)
438                         {
439                                 ui->editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
440                                 break;
441                         }
442                 }
443         }
444 }
445
446 /*
447  * Handle new log file content
448  */
449 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
450 {
451         QTimer::singleShot(0, ui->logView, SLOT(scrollToBottom()));
452 }
453
454 /*
455  * About screen
456  */
457 void MainWindow::showAbout(void)
458 {
459         QString text;
460
461         text += QString().sprintf("<nobr><tt>Simple x264 Launcher v%u.%02u.%u - use 64-Bit x264 with 32-Bit Avisynth<br>", x264_version_major(), x264_version_minor(), x264_version_build());
462         text += QString().sprintf("Copyright (c) 2004-%04d LoRd_MuldeR &lt;mulder2@gmx.de&gt;. Some rights reserved.<br>", qMax(x264_version_date().year(),QDate::currentDate().year()));
463         text += QString().sprintf("Built on %s at %s with %s for Win-%s.<br><br>", x264_version_date().toString(Qt::ISODate).toLatin1().constData(), x264_version_time(), x264_version_compiler(), x264_version_arch());
464         text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
465         text += QString().sprintf("it under the terms of the GNU General Public License &lt;http://www.gnu.org/&gt;.<br>");
466         text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
467         text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", home_url, home_url);
468
469         QMessageBox aboutBox(this);
470         aboutBox.setIconPixmap(QIcon(":/images/movie.png").pixmap(64,64));
471         aboutBox.setWindowTitle(tr("About..."));
472         aboutBox.setText(text.replace("-", "&minus;"));
473         aboutBox.addButton(tr("About x264"), QMessageBox::NoRole);
474         aboutBox.addButton(tr("About AVS"), QMessageBox::NoRole);
475         aboutBox.addButton(tr("About VPY"), QMessageBox::NoRole);
476         aboutBox.addButton(tr("About Qt"), QMessageBox::NoRole);
477         aboutBox.setEscapeButton(aboutBox.addButton(tr("Close"), QMessageBox::NoRole));
478                 
479         forever
480         {
481                 x264_beep(x264_beep_info);
482                 switch(aboutBox.exec())
483                 {
484                 case 0:
485                         {
486                                 QString text2;
487                                 text2 += tr("<nobr><tt>x264 - the best H.264/AVC encoder. Copyright (c) 2003-2013 x264 project.<br>");
488                                 text2 += tr("Free software library for encoding video streams into the H.264/MPEG-4 AVC format.<br>");
489                                 text2 += tr("Released under the terms of the GNU General Public License.<br><br>");
490                                 text2 += tr("Please visit <a href=\"%1\">%1</a> for obtaining a commercial x264 license.<br>").arg("http://x264licensing.com/");
491                                 text2 += tr("Read the <a href=\"%1\">user's manual</a> to get started and use the <a href=\"%2\">support forum</a> for help!<br></tt></nobr>").arg("http://mewiki.project357.com/wiki/X264_Settings", "http://forum.doom9.org/forumdisplay.php?f=77");
492
493                                 QMessageBox x264Box(this);
494                                 x264Box.setIconPixmap(QIcon(":/images/x264.png").pixmap(48,48));
495                                 x264Box.setWindowTitle(tr("About x264"));
496                                 x264Box.setText(text2.replace("-", "&minus;"));
497                                 x264Box.setEscapeButton(x264Box.addButton(tr("Close"), QMessageBox::NoRole));
498                                 x264_beep(x264_beep_info);
499                                 x264Box.exec();
500                         }
501                         break;
502                 case 1:
503                         {
504                                 QString text2;
505                                 text2 += tr("<nobr><tt>Avisynth - powerful video processing scripting language.<br>");
506                                 text2 += tr("Copyright (c) 2000 Ben Rudiak-Gould and all subsequent developers.<br>");
507                                 text2 += tr("Released under the terms of the GNU General Public License.<br><br>");
508                                 text2 += tr("Please visit the web-site <a href=\"%1\">%1</a> for more information.<br>").arg("http://avisynth.org/");
509                                 text2 += tr("Read the <a href=\"%1\">guide</a> to get started and use the <a href=\"%2\">support forum</a> for help!<br></tt></nobr>").arg("http://avisynth.nl/index.php/First_script", "http://forum.doom9.org/forumdisplay.php?f=33");
510
511                                 QMessageBox x264Box(this);
512                                 x264Box.setIconPixmap(QIcon(":/images/avisynth.png").pixmap(48,67));
513                                 x264Box.setWindowTitle(tr("About Avisynth"));
514                                 x264Box.setText(text2.replace("-", "&minus;"));
515                                 x264Box.setEscapeButton(x264Box.addButton(tr("Close"), QMessageBox::NoRole));
516                                 x264_beep(x264_beep_info);
517                                 x264Box.exec();
518                         }
519                         break;
520                 case 2:
521                         {
522                                 QString text2;
523                                 text2 += tr("<nobr><tt>VapourSynth - application for video manipulation based on Python.<br>");
524                                 text2 += tr("Copyright (c) 2012 Fredrik Mellbin.<br>");
525                                 text2 += tr("Released under the terms of the GNU Lesser General Public.<br><br>");
526                                 text2 += tr("Please visit the web-site <a href=\"%1\">%1</a> for more information.<br>").arg("http://www.vapoursynth.com/");
527                                 text2 += tr("Read the <a href=\"%1\">documentation</a> to get started and use the <a href=\"%2\">support forum</a> for help!<br></tt></nobr>").arg("http://www.vapoursynth.com/doc/", "http://forum.doom9.org/showthread.php?t=165771");
528
529                                 QMessageBox x264Box(this);
530                                 x264Box.setIconPixmap(QIcon(":/images/python.png").pixmap(48,48));
531                                 x264Box.setWindowTitle(tr("About VapourSynth"));
532                                 x264Box.setText(text2.replace("-", "&minus;"));
533                                 x264Box.setEscapeButton(x264Box.addButton(tr("Close"), QMessageBox::NoRole));
534                                 x264_beep(x264_beep_info);
535                                 x264Box.exec();
536                         }
537                         break;
538                 case 3:
539                         QMessageBox::aboutQt(this);
540                         break;
541                 default:
542                         return;
543                 }
544         }
545 }
546
547 /*
548  * Open web-link
549  */
550 void MainWindow::showWebLink(void)
551 {
552         if(QObject::sender() == ui->actionWebMulder)          QDesktopServices::openUrl(QUrl(home_url));
553         if(QObject::sender() == ui->actionWebX264)            QDesktopServices::openUrl(QUrl("http://www.x264.com/"));
554         if(QObject::sender() == ui->actionWebKomisar)         QDesktopServices::openUrl(QUrl("http://komisar.gin.by/"));
555         if(QObject::sender() == ui->actionWebVideoLAN)        QDesktopServices::openUrl(QUrl("http://download.videolan.org/pub/x264/binaries/"));
556         if(QObject::sender() == ui->actionWebJEEB)            QDesktopServices::openUrl(QUrl("http://x264.fushizen.eu/"));
557         if(QObject::sender() == ui->actionWebAvisynth32)      QDesktopServices::openUrl(QUrl("http://sourceforge.net/projects/avisynth2/files/AviSynth%202.5/"));
558         if(QObject::sender() == ui->actionWebAvisynth64)      QDesktopServices::openUrl(QUrl("http://code.google.com/p/avisynth64/downloads/list"));
559         if(QObject::sender() == ui->actionWebVapourSynth)     QDesktopServices::openUrl(QUrl("http://www.vapoursynth.com/"));
560         if(QObject::sender() == ui->actionWebVapourSynthDocs) QDesktopServices::openUrl(QUrl("http://www.vapoursynth.com/doc/"));
561         if(QObject::sender() == ui->actionWebWiki)            QDesktopServices::openUrl(QUrl("http://mewiki.project357.com/wiki/X264_Settings"));
562         if(QObject::sender() == ui->actionWebBluRay)          QDesktopServices::openUrl(QUrl("http://www.x264bluray.com/"));
563         if(QObject::sender() == ui->actionWebAvsWiki)         QDesktopServices::openUrl(QUrl("http://avisynth.nl/index.php/Main_Page#Usage"));
564         if(QObject::sender() == ui->actionWebSupport)         QDesktopServices::openUrl(QUrl("http://forum.doom9.org/showthread.php?t=144140"));
565         if(QObject::sender() == ui->actionWebSecret)          QDesktopServices::openUrl(QUrl("http://www.youtube.com/watch_popup?v=AXIeHY-OYNI"));
566 }
567
568 /*
569  * Pereferences dialog
570  */
571 void MainWindow::showPreferences(void)
572 {
573         PreferencesDialog *preferences = new PreferencesDialog(this, m_preferences, m_cpuFeatures->x64);
574         preferences->exec();
575         X264_DELETE(preferences);
576 }
577
578 /*
579  * Launch next job, after running job has finished
580  */
581 void MainWindow::launchNextJob(void)
582 {
583         qDebug("launchNextJob(void)");
584         
585         const int rows = m_jobList->rowCount(QModelIndex());
586
587         if(countRunningJobs() >= m_preferences->maxRunningJobCount())
588         {
589                 qDebug("Still have too many jobs running, won't launch next one yet!");
590                 return;
591         }
592
593         int startIdx= ui->jobsView->currentIndex().isValid() ? qBound(0, ui->jobsView->currentIndex().row(), rows-1) : 0;
594
595         for(int i = 0; i < rows; i++)
596         {
597                 int currentIdx = (i + startIdx) % rows;
598                 JobStatus status = m_jobList->getJobStatus(m_jobList->index(currentIdx, 0, QModelIndex()));
599                 if(status == JobStatus_Enqueued)
600                 {
601                         if(m_jobList->startJob(m_jobList->index(currentIdx, 0, QModelIndex())))
602                         {
603                                 ui->jobsView->selectRow(currentIdx);
604                                 return;
605                         }
606                 }
607         }
608                 
609         qWarning("No enqueued jobs left!");
610 }
611
612 /*
613  * Save log to text file
614  */
615 void MainWindow::saveLogFile(const QModelIndex &index)
616 {
617         if(index.isValid())
618         {
619                 if(LogFileModel *log = m_jobList->getLogFile(index))
620                 {
621                         QDir(QString("%1/logs").arg(x264_data_path())).mkpath(".");
622                         QString logFilePath = QString("%1/logs/LOG.%2.%3.txt").arg(x264_data_path(), QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString(Qt::ISODate).replace(':', "-"));
623                         QFile outFile(logFilePath);
624                         if(outFile.open(QIODevice::WriteOnly))
625                         {
626                                 QTextStream outStream(&outFile);
627                                 outStream.setCodec("UTF-8");
628                                 outStream.setGenerateByteOrderMark(true);
629                                 
630                                 const int rows = log->rowCount(QModelIndex());
631                                 for(int i = 0; i < rows; i++)
632                                 {
633                                         outStream << log->data(log->index(i, 0, QModelIndex()), Qt::DisplayRole).toString() << QLatin1String("\r\n");
634                                 }
635                                 outFile.close();
636                         }
637                         else
638                         {
639                                 qWarning("Failed to open log file for writing:\n%s", logFilePath.toUtf8().constData());
640                         }
641                 }
642         }
643 }
644
645 /*
646  * Shut down the computer (with countdown)
647  */
648 void MainWindow::shutdownComputer(void)
649 {
650         qDebug("shutdownComputer(void)");
651         
652         if(countPendingJobs() > 0)
653         {
654                 qDebug("Still have pending jobs, won't shutdown yet!");
655                 return;
656         }
657         
658         const int iTimeout = 30;
659         const Qt::WindowFlags flags = Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint;
660         const QString text = QString("%1%2%1").arg(QString().fill(' ', 18), tr("Warning: Computer will shutdown in %1 seconds..."));
661         
662         qWarning("Initiating shutdown sequence!");
663         
664         QProgressDialog progressDialog(text.arg(iTimeout), tr("Cancel Shutdown"), 0, iTimeout + 1, this, flags);
665         QPushButton *cancelButton = new QPushButton(tr("Cancel Shutdown"), &progressDialog);
666         cancelButton->setIcon(QIcon(":/buttons/power_on.png"));
667         progressDialog.setModal(true);
668         progressDialog.setAutoClose(false);
669         progressDialog.setAutoReset(false);
670         progressDialog.setWindowIcon(QIcon(":/buttons/power_off.png"));
671         progressDialog.setWindowTitle(windowTitle());
672         progressDialog.setCancelButton(cancelButton);
673         progressDialog.show();
674         
675         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
676         QApplication::setOverrideCursor(Qt::WaitCursor);
677         x264_play_sound(IDR_WAVE1, false);
678         QApplication::restoreOverrideCursor();
679         
680         QTimer timer;
681         timer.setInterval(1000);
682         timer.start();
683
684         QEventLoop eventLoop(this);
685         connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
686         connect(&progressDialog, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
687
688         for(int i = 1; i <= iTimeout; i++)
689         {
690                 eventLoop.exec();
691                 if(progressDialog.wasCanceled())
692                 {
693                         progressDialog.close();
694                         return;
695                 }
696                 progressDialog.setValue(i+1);
697                 progressDialog.setLabelText(text.arg(iTimeout-i));
698                 if(iTimeout-i == 3) progressDialog.setCancelButton(NULL);
699                 QApplication::processEvents();
700                 x264_play_sound(((i < iTimeout) ? IDR_WAVE2 : IDR_WAVE3), false);
701         }
702         
703         qWarning("Shutting down !!!");
704
705         if(x264_shutdown_computer("Simple x264 Launcher: All jobs completed, shutting down!", 10, true))
706         {
707                 qApp->closeAllWindows();
708         }
709 }
710
711 /*
712  * Main initialization function (called only once!)
713  */
714 void MainWindow::init(void)
715 {
716         static const char *binFiles = "x86/x264_8bit_x86.exe:x64/x264_8bit_x64.exe:x86/x264_10bit_x86.exe:x64/x264_10bit_x64.exe:x86/avs2yuv_x86.exe:x64/avs2yuv_x64.exe";
717         QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
718
719         updateLabelPos();
720
721         //Check for a running instance
722         /*
723         bool firstInstance = false;
724         if(m_ipcThread->initialize(&firstInstance))
725         {
726                 m_ipcThread->start();
727                 if(!firstInstance)
728                 {
729                         if(!m_ipcThread->wait(5000))
730                         {
731                                 QMessageBox::warning(this, tr("Not Responding"), tr("<nobr>Another instance of this application is already running, but did not respond in time.<br>If the problem persists, please kill the running instance from the task manager!</nobr>"), tr("Quit"));
732                                 m_ipcThread->terminate();
733                                 m_ipcThread->wait();
734                         }
735                         INIT_ERROR_EXIT();
736                 }
737         }
738         */
739
740         //Check all binaries
741         while(!binaries.isEmpty())
742         {
743                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
744                 QString current = binaries.takeFirst();
745                 QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
746                 if(file->open(QIODevice::ReadOnly))
747                 {
748                         if(!x264_is_executable(file->fileName()))
749                         {
750                                 QMessageBox::critical(this, tr("Invalid File!"), tr("<nobr>At least on required tool is not a valid Win32 or Win64 binary:<br><tt style=\"whitespace:nowrap\">%1</tt><br><br>Please re-install the program in order to fix the problem!</nobr>").arg(QDir::toNativeSeparators(QString("%1/toolset/%2").arg(m_appDir, current))).replace("-", "&minus;"));
751                                 qFatal(QString("Binary is invalid: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
752                                 INIT_ERROR_EXIT();
753                         }
754                         m_toolsList << file;
755                 }
756                 else
757                 {
758                         X264_DELETE(file);
759                         QMessageBox::critical(this, tr("File Not Found!"), tr("<nobr>At least on required tool could not be found:<br><tt style=\"whitespace:nowrap\">%1</tt><br><br>Please re-install the program in order to fix the problem!</nobr>").arg(QDir::toNativeSeparators(QString("%1/toolset/%2").arg(m_appDir, current))).replace("-", "&minus;"));
760                         qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
761                         INIT_ERROR_EXIT();
762                 }
763         }
764
765         //Check for portable mode
766         if(x264_portable())
767         {
768                 bool ok = false;
769                 static const char *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
770                 QFile writeTest(QString("%1/%2").arg(x264_data_path(), QUuid::createUuid().toString()));
771                 if(writeTest.open(QIODevice::WriteOnly))
772                 {
773                         ok = (writeTest.write(data) == strlen(data));
774                         writeTest.remove();
775                 }
776                 if(!ok)
777                 {
778                         int val = QMessageBox::warning(this, tr("Write Test Failed"), tr("<nobr>The application was launched in portable mode, but the program path is <b>not</b> writable!</nobr>"), tr("Quit"), tr("Ignore"));
779                         if(val != 1) INIT_ERROR_EXIT();
780                 }
781         }
782
783         //Pre-release popup
784         if(x264_is_prerelease())
785         {
786                 qsrand(time(NULL)); int rnd = qrand() % 3;
787                 int val = QMessageBox::information(this, tr("Pre-Release Version"), tr("Note: This is a pre-release version. Please do NOT use for production!<br>Click the button #%1 in order to continue...<br><br>(There will be no such message box in the final version of this application)").arg(QString::number(rnd + 1)), tr("(1)"), tr("(2)"), tr("(3)"), qrand() % 3);
788                 if(rnd != val) INIT_ERROR_EXIT();
789         }
790
791         //Make sure this CPU can run x264 (requires MMX + MMXEXT/iSSE to run x264 with ASM enabled, additionally requires SSE1 for most x264 builds)
792         if(!(m_cpuFeatures->mmx && m_cpuFeatures->mmx2))
793         {
794                 QMessageBox::critical(this, tr("Unsupported CPU"), tr("<nobr>Sorry, but this machine is <b>not</b> physically capable of running x264 (with assembly).<br>Please get a CPU that supports at least the MMX and MMXEXT instruction sets!</nobr>"), tr("Quit"));
795                 qFatal("System does not support MMX and MMXEXT, x264 will not work !!!");
796                 INIT_ERROR_EXIT();
797         }
798         else if(!(m_cpuFeatures->mmx && m_cpuFeatures->sse))
799         {
800                 qWarning("WARNING: System does not support SSE1, most x264 builds will not work !!!\n");
801                 int val = QMessageBox::warning(this, tr("Unsupported CPU"), tr("<nobr>It appears that this machine does <b>not</b> support the SSE1 instruction set.<br>Thus most builds of x264 will <b>not</b> run on this computer at all.<br><br>Please get a CPU that supports the MMX and SSE1 instruction sets!</nobr>"), tr("Quit"), tr("Ignore"));
802                 if(val != 1) INIT_ERROR_EXIT();
803         }
804
805         //Skip version check (not recommended!)
806         if(qApp->arguments().contains("--skip-x264-version-check", Qt::CaseInsensitive))
807         {
808                 qWarning("x264 version check disabled, you have been warned!\n");
809                 m_skipVersionTest = true;
810         }
811         
812         //Don't abort encoding process on timeout (not recommended!)
813         if(qApp->arguments().contains("--no-deadlock-detection", Qt::CaseInsensitive))
814         {
815                 qWarning("Deadlock detection disabled, you have been warned!\n");
816                 m_abortOnTimeout = false;
817         }
818
819         //Check for Avisynth support
820         if(!qApp->arguments().contains("--skip-avisynth-check", Qt::CaseInsensitive))
821         {
822                 qDebug("[Check for Avisynth support]");
823                 volatile double avisynthVersion = 0.0;
824                 const int result = AvisynthCheckThread::detect(&avisynthVersion);
825                 if(result < 0)
826                 {
827                         QString text = tr("A critical error was encountered while checking your Avisynth version.").append("<br>");
828                         text += tr("This is most likely caused by an erroneous Avisynth Plugin, please try to clean your Plugins folder!").append("<br>");
829                         text += tr("We suggest to move all .dll and .avsi files out of your Avisynth Plugins folder and try again.");
830                         int val = QMessageBox::critical(this, tr("Avisynth Error"), QString("<nobr>%1</nobr>").arg(text).replace("-", "&minus;"), tr("Quit"), tr("Ignore"));
831                         if(val != 1) INIT_ERROR_EXIT();
832                 }
833                 if((!result) || (avisynthVersion < 2.5))
834                 {
835                         if(!m_preferences->disableWarnings())
836                         {
837                                 QString text = tr("It appears that Avisynth is <b>not</b> currently installed on your computer.<br>Therefore Avisynth (.avs) input will <b>not</b> be working at all!").append("<br><br>");
838                                 text += tr("Please download and install Avisynth:").append("<br>").append(LINK("http://sourceforge.net/projects/avisynth2/files/AviSynth%202.5/"));
839                                 int val = QMessageBox::warning(this, tr("Avisynth Missing"), QString("<nobr>%1</nobr>").arg(text).replace("-", "&minus;"), tr("Quit"), tr("Ignore"));
840                                 if(val != 1) INIT_ERROR_EXIT();
841                         }
842                 }
843                 qDebug(" ");
844         }
845
846         //Check for VapourSynth support
847         if(!qApp->arguments().contains("--skip-vapoursynth-check", Qt::CaseInsensitive))
848         {
849                 qDebug("[Check for VapourSynth support]");
850                 volatile double avisynthVersion = 0.0;
851                 const int result = VapourSynthCheckThread::detect(m_vapoursynthPath);
852                 if(result < 0)
853                 {
854                         QString text = tr("A critical error was encountered while checking your VapourSynth installation.").append("<br>");
855                         text += tr("This is most likely caused by an erroneous VapourSynth Plugin, please try to clean your Filters folder!").append("<br>");
856                         text += tr("We suggest to move all .dll files out of your VapourSynth Filters folder and try again.");
857                         int val = QMessageBox::critical(this, tr("VapourSynth Error"), QString("<nobr>%1</nobr>").arg(text).replace("-", "&minus;"), tr("Quit"), tr("Ignore"));
858                         if(val != 1) INIT_ERROR_EXIT();
859                 }
860                 if((!result) || (m_vapoursynthPath.isEmpty()))
861                 {
862                         if(!m_preferences->disableWarnings())
863                         {
864                                 QString text = tr("It appears that VapourSynth is <b>not</b> currently installed on your computer.<br>Therefore VapourSynth (.vpy) input will <b>not</b> be working at all!").append("<br><br>");
865                                 text += tr("Please download and install VapourSynth for Windows (R19 or later):").append("<br>").append(LINK("http://www.vapoursynth.com/")).append("<br><br>");
866                                 text += tr("Note that Python 3.3 (x86) is a prerequisite for installing VapourSynth:").append("<br>").append(LINK("http://www.python.org/getit/")).append("<br>");
867                                 int val = QMessageBox::warning(this, tr("VapourSynth Missing"), QString("<nobr>%1</nobr>").arg(text).replace("-", "&minus;"), tr("Quit"), tr("Ignore"));
868                                 if(val != 1) INIT_ERROR_EXIT();
869                         }
870                 }
871                 qDebug(" ");
872         }
873
874         //Update initialized flag (must do this before update check!)
875         m_initialized = true;
876
877         //Enable drag&drop support for this window, required for Qt v4.8.4+
878         setAcceptDrops(true);
879
880         //Check for expiration
881         if(x264_version_date().addMonths(6) < x264_current_date_safe())
882         {
883                 QString text;
884                 text += QString("<nobr><tt>%1</tt></nobr><br><br>").arg(tr("Your version of Simple x264 Launcher is more than 6 months old!").replace("-", "&minus;"));
885                 text += QString("<nobr><tt>%1<br><a href=\"%2\">%2</a><br><br>").arg(tr("You can download the most recent version from the official web-site now:").replace("-", "&minus;"), QString::fromLatin1(update_url));
886                 text += QString("<nobr><tt>%1</tt></nobr><br>").arg(tr("Alternatively, click 'Check for Updates' to run the auto-update utility.").replace("-", "&minus;"));
887                 QMessageBox msgBox(this);
888                 msgBox.setIconPixmap(QIcon(":/images/update.png").pixmap(56,56));
889                 msgBox.setWindowTitle(tr("Update Notification"));
890                 msgBox.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
891                 msgBox.setText(text);
892                 QPushButton *btn1 = msgBox.addButton(tr("Check for Updates"), QMessageBox::AcceptRole);
893                 QPushButton *btn2 = msgBox.addButton(tr("Discard"), QMessageBox::NoRole);
894                 QPushButton *btn3 = msgBox.addButton(btn2->text(), QMessageBox::RejectRole);
895                 btn2->setEnabled(false);
896                 btn3->setVisible(false);
897                 QTimer::singleShot(7500, btn2, SLOT(hide()));
898                 QTimer::singleShot(7500, btn3, SLOT(show()));
899                 if(msgBox.exec() == 0)
900                 {
901                         QTimer::singleShot(0, this, SLOT(checkUpdates()));
902                         return;
903                 }
904         }
905         else if((!m_preferences->noUpdateReminder()) && (m_recentlyUsed->lastUpdateCheck() + 14 < x264_current_date_safe().toJulianDay()))
906         {
907                 if(QMessageBox::warning(this, tr("Update Notification"), QString("<nobr>%1</nobr>").arg(tr("Your last update check was more than 14 days ago. Check for updates now?")), tr("Check for Updates"), tr("Discard")) == 0)
908                 {
909                         QTimer::singleShot(0, this, SLOT(checkUpdates()));
910                         return;
911                 }
912         }
913
914         //Add files from command-line
915         parseCommandLineArgs();
916 }
917
918 /*
919  * Update the label position
920  */
921 void MainWindow::updateLabelPos(void)
922 {
923         const QWidget *const viewPort = ui->jobsView->viewport();
924         m_label->setGeometry(0, 0, viewPort->width(), viewPort->height());
925 }
926
927 /*
928  * Copy the complete log to the clipboard
929  */
930 void MainWindow::copyLogToClipboard(bool checked)
931 {
932         qDebug("copyLogToClipboard");
933         
934         if(LogFileModel *log = dynamic_cast<LogFileModel*>(ui->logView->model()))
935         {
936                 log->copyToClipboard();
937                 x264_beep(x264_beep_info);
938         }
939 }
940
941 /*
942  * Process the dropped files
943  */
944 void MainWindow::handleDroppedFiles(void)
945 {
946         qDebug("MainWindow::handleDroppedFiles");
947         if(m_droppedFiles)
948         {
949                 QStringList droppedFiles(*m_droppedFiles);
950                 m_droppedFiles->clear();
951                 createJobMultiple(droppedFiles);
952         }
953         qDebug("Leave from MainWindow::handleDroppedFiles!");
954 }
955
956 void MainWindow::instanceCreated(unsigned int pid)
957 {
958         qDebug("Notification from other instance (PID=0x%X) received!", pid);
959         
960         x264_blink_window(this, 5, 125);
961         x264_bring_to_front(this);
962         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
963         x264_blink_window(this, 5, 125);
964 }
965
966 void MainWindow::checkUpdates(void)
967 {
968         if(!m_initialized)
969         {
970                 return;
971         }
972
973         if(countRunningJobs() > 0)
974         {
975                 QMessageBox::warning(this, tr("Jobs Are Running"), tr("Sorry, can not update while there still are running jobs!"));
976                 return;
977         }
978
979         UpdaterDialog *updater = new UpdaterDialog(this, QString("%1/toolset").arg(m_appDir));
980         const int ret = updater->exec();
981
982         if(updater->getSuccess())
983         {
984                 m_recentlyUsed->setLastUpdateCheck(x264_current_date_safe().toJulianDay());
985                 RecentlyUsed::saveRecentlyUsed(m_recentlyUsed);
986         }
987
988         if(ret == UpdaterDialog::READY_TO_INSTALL_UPDATE)
989         {
990                 qWarning("Exitting program to install update...");
991                 close();
992                 QApplication::quit();
993         }
994
995         X264_DELETE(updater);
996 }
997
998 ///////////////////////////////////////////////////////////////////////////////
999 // Event functions
1000 ///////////////////////////////////////////////////////////////////////////////
1001
1002 /*
1003  * Window shown event
1004  */
1005 void MainWindow::showEvent(QShowEvent *e)
1006 {
1007         QMainWindow::showEvent(e);
1008
1009         if(m_firstShow)
1010         {
1011                 m_firstShow = false;
1012                 QTimer::singleShot(0, this, SLOT(init()));
1013         }
1014 }
1015
1016 /*
1017  * Window close event
1018  */
1019 void MainWindow::closeEvent(QCloseEvent *e)
1020 {
1021         if(!m_initialized)
1022         {
1023                 e->ignore();
1024                 return;
1025         }
1026
1027         if(countRunningJobs() > 0)
1028         {
1029                 e->ignore();
1030                 QMessageBox::warning(this, tr("Jobs Are Running"), tr("Sorry, can not exit while there still are running jobs!"));
1031                 return;
1032         }
1033         
1034         if(countPendingJobs() > 0)
1035         {
1036                 int ret = QMessageBox::question(this, tr("Jobs Are Pending"), tr("Do you really want to quit and discard the pending jobs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
1037                 if(ret != QMessageBox::Yes)
1038                 {
1039                         e->ignore();
1040                         return;
1041                 }
1042         }
1043
1044         while(m_jobList->rowCount(QModelIndex()) > 0)
1045         {
1046                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
1047                 if(!m_jobList->deleteJob(m_jobList->index(0, 0, QModelIndex())))
1048                 {
1049                         e->ignore();
1050                         QMessageBox::warning(this, tr("Failed To Exit"), tr("Sorry, at least one job could not be deleted!"));
1051                         return;
1052                 }
1053         }
1054
1055         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
1056         QMainWindow::closeEvent(e);
1057 }
1058
1059 /*
1060  * Window resize event
1061  */
1062 void MainWindow::resizeEvent(QResizeEvent *e)
1063 {
1064         QMainWindow::resizeEvent(e);
1065         updateLabelPos();
1066 }
1067
1068 /*
1069  * Event filter
1070  */
1071 bool MainWindow::eventFilter(QObject *o, QEvent *e)
1072 {
1073         if((o == ui->labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
1074         {
1075                 QTimer::singleShot(0, this, SLOT(showAbout()));
1076                 return true;
1077         }
1078         return false;
1079 }
1080
1081 /*
1082  * Win32 message filter
1083  */
1084 bool MainWindow::winEvent(MSG *message, long *result)
1085 {
1086         return WinSevenTaskbar::handleWinEvent(message, result);
1087 }
1088
1089 /*
1090  * File dragged over window
1091  */
1092 void MainWindow::dragEnterEvent(QDragEnterEvent *event)
1093 {
1094         bool accept[2] = {false, false};
1095
1096         foreach(const QString &fmt, event->mimeData()->formats())
1097         {
1098                 accept[0] = accept[0] || fmt.contains("text/uri-list", Qt::CaseInsensitive);
1099                 accept[1] = accept[1] || fmt.contains("FileNameW", Qt::CaseInsensitive);
1100         }
1101
1102         if(accept[0] && accept[1])
1103         {
1104                 event->acceptProposedAction();
1105         }
1106 }
1107
1108 /*
1109  * File dropped onto window
1110  */
1111 void MainWindow::dropEvent(QDropEvent *event)
1112 {
1113         QStringList droppedFiles;
1114         QList<QUrl> urls = event->mimeData()->urls();
1115
1116         while(!urls.isEmpty())
1117         {
1118                 QUrl currentUrl = urls.takeFirst();
1119                 QFileInfo file(currentUrl.toLocalFile());
1120                 if(file.exists() && file.isFile())
1121                 {
1122                         qDebug("MainWindow::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
1123                         droppedFiles << file.canonicalFilePath();
1124                 }
1125         }
1126         
1127         if(droppedFiles.count() > 0)
1128         {
1129                 if(!m_droppedFiles)
1130                 {
1131                         m_droppedFiles = new QStringList();
1132                 }
1133                 m_droppedFiles->append(droppedFiles);
1134                 m_droppedFiles->sort();
1135                 QTimer::singleShot(0, this, SLOT(handleDroppedFiles()));
1136         }
1137 }
1138
1139 ///////////////////////////////////////////////////////////////////////////////
1140 // Private functions
1141 ///////////////////////////////////////////////////////////////////////////////
1142
1143 /*
1144  * Creates a new job
1145  */
1146 bool MainWindow::createJob(QString &sourceFileName, QString &outputFileName, OptionsModel *options, bool &runImmediately, const bool restart, int fileNo, int fileTotal, bool *applyToAll)
1147 {
1148         bool okay = false;
1149         AddJobDialog *addDialog = new AddJobDialog(this, options, m_recentlyUsed, m_cpuFeatures->x64, m_preferences->use10BitEncoding(), m_preferences->saveToSourcePath());
1150
1151         addDialog->setRunImmediately(runImmediately);
1152         if(!sourceFileName.isEmpty()) addDialog->setSourceFile(sourceFileName);
1153         if(!outputFileName.isEmpty()) addDialog->setOutputFile(outputFileName);
1154         if(restart) addDialog->setWindowTitle(tr("Restart Job"));
1155
1156         const bool multiFile = (fileNo >= 0) && (fileTotal > 1);
1157         if(multiFile)
1158         {
1159                 addDialog->setSourceEditable(false);
1160                 addDialog->setWindowTitle(addDialog->windowTitle().append(tr(" (File %1 of %2)").arg(QString::number(fileNo+1), QString::number(fileTotal))));
1161                 addDialog->setApplyToAllVisible(applyToAll);
1162         }
1163
1164         if(addDialog->exec() == QDialog::Accepted)
1165         {
1166                 sourceFileName = addDialog->sourceFile();
1167                 outputFileName = addDialog->outputFile();
1168                 runImmediately = addDialog->runImmediately();
1169                 if(applyToAll)
1170                 {
1171                         *applyToAll = addDialog->applyToAll();
1172                 }
1173                 okay = true;
1174         }
1175
1176         X264_DELETE(addDialog);
1177         return okay;
1178 }
1179
1180 /*
1181  * Creates a new job from *multiple* files
1182  */
1183 bool MainWindow::createJobMultiple(const QStringList &filePathIn)
1184 {
1185         QStringList::ConstIterator iter;
1186         bool applyToAll = false, runImmediately = false;
1187         int counter = 0;
1188
1189         //Add files individually
1190         for(iter = filePathIn.constBegin(); (iter != filePathIn.constEnd()) && (!applyToAll); iter++)
1191         {
1192                 runImmediately = (countRunningJobs() < (m_preferences->autoRunNextJob() ? m_preferences->maxRunningJobCount() : 1));
1193                 QString sourceFileName(*iter), outputFileName;
1194                 if(createJob(sourceFileName, outputFileName, m_options, runImmediately, false, counter++, filePathIn.count(), &applyToAll))
1195                 {
1196                         if(appendJob(sourceFileName, outputFileName, m_options, runImmediately))
1197                         {
1198                                 continue;
1199                         }
1200                 }
1201                 return false;
1202         }
1203
1204         //Add remaining files
1205         while(applyToAll && (iter != filePathIn.constEnd()))
1206         {
1207                 const bool runImmediatelyTmp = runImmediately && (countRunningJobs() < (m_preferences->autoRunNextJob() ? m_preferences->maxRunningJobCount() : 1));
1208                 const QString sourceFileName = *iter;
1209                 const QString outputFileName = AddJobDialog::generateOutputFileName(sourceFileName, m_recentlyUsed->outputDirectory(), m_recentlyUsed->filterIndex(), m_preferences->saveToSourcePath());
1210                 if(!appendJob(sourceFileName, outputFileName, m_options, runImmediatelyTmp))
1211                 {
1212                         return false;
1213                 }
1214                 iter++;
1215         }
1216
1217         return true;
1218 }
1219
1220
1221 /*
1222  * Append a new job
1223  */
1224 bool MainWindow::appendJob(const QString &sourceFileName, const QString &outputFileName, OptionsModel *options, const bool runImmediately)
1225 {
1226         bool okay = false;
1227         
1228         EncodeThread *thrd = new EncodeThread
1229         (
1230                 sourceFileName,
1231                 outputFileName,
1232                 options,
1233                 QString("%1/toolset").arg(m_appDir),
1234                 m_vapoursynthPath,
1235                 m_cpuFeatures->x64,
1236                 m_preferences->use10BitEncoding(),
1237                 m_cpuFeatures->x64 && m_preferences->useAvisyth64Bit(),
1238                 m_skipVersionTest,
1239                 m_preferences->processPriority(),
1240                 m_abortOnTimeout
1241         );
1242
1243         QModelIndex newIndex = m_jobList->insertJob(thrd);
1244
1245         if(newIndex.isValid())
1246         {
1247                 if(runImmediately)
1248                 {
1249                         ui->jobsView->selectRow(newIndex.row());
1250                         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
1251                         m_jobList->startJob(newIndex);
1252                 }
1253
1254                 okay = true;
1255         }
1256
1257         m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
1258         return okay;
1259 }
1260
1261 /*
1262  * Jobs that are not completed (or failed, or aborted) yet
1263  */
1264 unsigned int MainWindow::countPendingJobs(void)
1265 {
1266         unsigned int count = 0;
1267         const int rows = m_jobList->rowCount(QModelIndex());
1268
1269         for(int i = 0; i < rows; i++)
1270         {
1271                 JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
1272                 if(status != JobStatus_Completed && status != JobStatus_Aborted && status != JobStatus_Failed)
1273                 {
1274                         count++;
1275                 }
1276         }
1277
1278         return count;
1279 }
1280
1281 /*
1282  * Jobs that are still active, i.e. not terminated or enqueued
1283  */
1284 unsigned int MainWindow::countRunningJobs(void)
1285 {
1286         unsigned int count = 0;
1287         const int rows = m_jobList->rowCount(QModelIndex());
1288
1289         for(int i = 0; i < rows; i++)
1290         {
1291                 JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
1292                 if(status != JobStatus_Completed && status != JobStatus_Aborted && status != JobStatus_Failed && status != JobStatus_Enqueued)
1293                 {
1294                         count++;
1295                 }
1296         }
1297
1298         return count;
1299 }
1300
1301 /*
1302  * Update all buttons with respect to current job status
1303  */
1304 void MainWindow::updateButtons(JobStatus status)
1305 {
1306         qDebug("MainWindow::updateButtons(void)");
1307
1308         ui->buttonStartJob->setEnabled(status == JobStatus_Enqueued);
1309         ui->buttonAbortJob->setEnabled(status == JobStatus_Indexing || status == JobStatus_Running || status == JobStatus_Running_Pass1 || status == JobStatus_Running_Pass2 || status == JobStatus_Paused);
1310         ui->buttonPauseJob->setEnabled(status == JobStatus_Indexing || status == JobStatus_Running || status == JobStatus_Paused || status == JobStatus_Running_Pass1 || status == JobStatus_Running_Pass2);
1311         ui->buttonPauseJob->setChecked(status == JobStatus_Paused || status == JobStatus_Pausing);
1312
1313         ui->actionJob_Delete->setEnabled(status == JobStatus_Completed || status == JobStatus_Aborted || status == JobStatus_Failed || status == JobStatus_Enqueued);
1314         ui->actionJob_Restart->setEnabled(status == JobStatus_Completed || status == JobStatus_Aborted || status == JobStatus_Failed || status == JobStatus_Enqueued);
1315         ui->actionJob_Browse->setEnabled(status == JobStatus_Completed);
1316
1317         ui->actionJob_Start->setEnabled(ui->buttonStartJob->isEnabled());
1318         ui->actionJob_Abort->setEnabled(ui->buttonAbortJob->isEnabled());
1319         ui->actionJob_Pause->setEnabled(ui->buttonPauseJob->isEnabled());
1320         ui->actionJob_Pause->setChecked(ui->buttonPauseJob->isChecked());
1321
1322         ui->editDetails->setEnabled(status != JobStatus_Paused);
1323 }
1324
1325 /*
1326  * Update the taskbar with current job status
1327  */
1328 void MainWindow::updateTaskbar(JobStatus status, const QIcon &icon)
1329 {
1330         qDebug("MainWindow::updateTaskbar(void)");
1331
1332         switch(status)
1333         {
1334         case JobStatus_Undefined:
1335                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarNoState);
1336                 break;
1337         case JobStatus_Aborting:
1338         case JobStatus_Starting:
1339         case JobStatus_Pausing:
1340         case JobStatus_Resuming:
1341                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
1342                 break;
1343         case JobStatus_Aborted:
1344         case JobStatus_Failed:
1345                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarErrorState);
1346                 break;
1347         case JobStatus_Paused:
1348                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarPausedState);
1349                 break;
1350         default:
1351                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarNormalState);
1352                 break;
1353         }
1354
1355         switch(status)
1356         {
1357         case JobStatus_Aborting:
1358         case JobStatus_Starting:
1359         case JobStatus_Pausing:
1360         case JobStatus_Resuming:
1361                 break;
1362         default:
1363                 WinSevenTaskbar::setTaskbarProgress(this, ui->progressBar->value(), ui->progressBar->maximum());
1364                 break;
1365         }
1366
1367         WinSevenTaskbar::setOverlayIcon(this, icon.isNull() ? NULL : &icon);
1368 }
1369
1370 /*
1371  * Parse command-line arguments
1372  */
1373 void MainWindow::parseCommandLineArgs(void)
1374 {
1375         QStringList files, args = x264_arguments();
1376         while(!args.isEmpty())
1377         {
1378                 QString current = args.takeFirst();
1379                 if((current.compare("--add", Qt::CaseInsensitive) == 0) || (current.compare("--add-file", Qt::CaseInsensitive) == 0))
1380                 {
1381                         if(!args.isEmpty())
1382                         {
1383                                 current = args.takeFirst();
1384                                 if(QFileInfo(current).exists() && QFileInfo(current).isFile())
1385                                 {
1386                                         files << QFileInfo(current).canonicalFilePath();
1387                                 }
1388                                 else
1389                                 {
1390                                         qWarning("File '%s' not found!", current.toUtf8().constData());
1391                                 }
1392                         }
1393                         else
1394                         {
1395                                 qWarning("Argument for '--add-file' is missing!");
1396                         }
1397                 }
1398                 else if(current.compare("--add-job", Qt::CaseInsensitive) == 0)
1399                 {
1400                         if(args.size() >= 3)
1401                         {
1402                                 const QString fileSrc = args.takeFirst();
1403                                 const QString fileOut = args.takeFirst();
1404                                 const QString templId = args.takeFirst();
1405                                 if(QFileInfo(fileSrc).exists() && QFileInfo(fileSrc).isFile())
1406                                 {
1407                                         OptionsModel options;
1408                                         if(!(templId.isEmpty() || (templId.compare("-", Qt::CaseInsensitive) == 0)))
1409                                         {
1410                                                 if(!OptionsModel::loadTemplate(&options, templId.trimmed()))
1411                                                 {
1412                                                         qWarning("Template '%s' could not be found -> using defaults!", templId.trimmed().toUtf8().constData());
1413                                                 }
1414                                         }
1415                                         appendJob(fileSrc, fileOut, &options, true);
1416                                 }
1417                                 else
1418                                 {
1419                                         qWarning("Source file '%s' not found!", fileSrc.toUtf8().constData());
1420                                 }
1421                         }
1422                         else
1423                         {
1424                                 qWarning("Argument(s) for '--add-job' are missing!");
1425                                 args.clear();
1426                         }
1427                 }
1428                 else
1429                 {
1430                         qWarning("Unknown argument: %s", current.toUtf8().constData());
1431                 }
1432         }
1433
1434         if(files.count() > 0)
1435         {
1436                 createJobMultiple(files);
1437         }
1438 }