OSDN Git Service

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