OSDN Git Service

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