OSDN Git Service

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