OSDN Git Service

Implemented asynchronous handling of dropped files, so the source application gets...
[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
31 #include <QDate>
32 #include <QTimer>
33 #include <QCloseEvent>
34 #include <QMessageBox>
35 #include <QDesktopServices>
36 #include <QUrl>
37 #include <QDir>
38 #include <QLibrary>
39 #include <QProcess>
40 #include <QProgressDialog>
41
42 #include <Mmsystem.h>
43
44 const char *home_url = "http://mulder.brhack.net/";
45 const char *update_url = "http://code.google.com/p/mulder/downloads/list";
46 const char *tpl_last = "<LAST_USED>";
47
48 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
49 #define SET_TEXT_COLOR(WIDGET,COLOR) { QPalette _palette = WIDGET->palette(); _palette.setColor(QPalette::WindowText, (COLOR)); _palette.setColor(QPalette::Text, (COLOR)); WIDGET->setPalette(_palette); }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 // Constructor & Destructor
53 ///////////////////////////////////////////////////////////////////////////////
54
55 /*
56  * Constructor
57  */
58 MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures)
59 :
60         m_cpuFeatures(cpuFeatures),
61         m_appDir(QApplication::applicationDirPath()),
62         m_options(NULL),
63         m_jobList(NULL),
64         m_droppedFiles(NULL),
65         m_firstShow(true)
66 {
67         //Init the dialog, from the .ui file
68         setupUi(this);
69         setWindowFlags(windowFlags() & (~Qt::WindowMaximizeButtonHint));
70
71         //Register meta types
72         qRegisterMetaType<QUuid>("QUuid");
73         qRegisterMetaType<EncodeThread::JobStatus>("EncodeThread::JobStatus");
74
75         //Load preferences
76         PreferencesDialog::initPreferences(&m_preferences);
77         PreferencesDialog::loadPreferences(&m_preferences);
78
79         //Create options object
80         m_options = new OptionsModel();
81         OptionsModel::loadTemplate(m_options, QString::fromLatin1(tpl_last));
82
83         //Freeze minimum size
84         setMinimumSize(size());
85         splitter->setSizes(QList<int>() << 16 << 196);
86
87         //Update title
88         labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));
89         labelBuildDate->installEventFilter(this);
90         setWindowTitle(QString("%1 (%2 Mode)").arg(windowTitle(), m_cpuFeatures->x64 ? "64-Bit" : "32-Bit"));
91         if(X264_DEBUG)
92         {
93                 setWindowTitle(QString("%1 | !!! DEBUG VERSION !!!").arg(windowTitle()));
94                 setStyleSheet("QMenuBar, QMainWindow { background-color: yellow }");
95         }
96         else if(x264_is_prerelease())
97         {
98                 setWindowTitle(QString("%1 | PRE-RELEASE VERSION").arg(windowTitle()));
99         }
100         
101         //Create model
102         m_jobList = new JobListModel();
103         connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
104         jobsView->setModel(m_jobList);
105         
106         //Setup view
107         jobsView->horizontalHeader()->setSectionHidden(3, true);
108         jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
109         jobsView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
110         jobsView->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
111         jobsView->horizontalHeader()->resizeSection(1, 150);
112         jobsView->horizontalHeader()->resizeSection(2, 90);
113         jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
114         connect(jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
115
116         //Create context menu
117         QAction *actionClipboard = new QAction(QIcon(":/buttons/page_paste.png"), tr("Copy to Clipboard"), logView);
118         actionClipboard->setEnabled(false);
119         logView->addAction(actionClipboard);
120         connect(actionClipboard, SIGNAL(triggered(bool)), this, SLOT(copyLogToClipboard(bool)));
121         jobsView->addActions(menuJob->actions());
122
123         //Enable buttons
124         connect(buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
125         connect(buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
126         connect(buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
127         connect(buttonPauseJob, SIGNAL(toggled(bool)), this, SLOT(pauseButtonPressed(bool)));
128         connect(actionJob_Delete, SIGNAL(triggered()), this, SLOT(deleteButtonPressed()));
129         connect(actionJob_Browse, SIGNAL(triggered()), this, SLOT(browseButtonPressed()));
130
131         //Enable menu
132         connect(actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
133         connect(actionWebMulder, SIGNAL(triggered()), this, SLOT(showWebLink()));
134         connect(actionWebX264, SIGNAL(triggered()), this, SLOT(showWebLink()));
135         connect(actionWebKomisar, SIGNAL(triggered()), this, SLOT(showWebLink()));
136         connect(actionWebJarod, SIGNAL(triggered()), this, SLOT(showWebLink()));
137         connect(actionWebJEEB, SIGNAL(triggered()), this, SLOT(showWebLink()));
138         connect(actionWebAvisynth32, SIGNAL(triggered()), this, SLOT(showWebLink()));
139         connect(actionWebAvisynth64, SIGNAL(triggered()), this, SLOT(showWebLink()));
140         connect(actionWebWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
141         connect(actionWebBluRay, SIGNAL(triggered()), this, SLOT(showWebLink()));
142         connect(actionWebSecret, SIGNAL(triggered()), this, SLOT(showWebLink()));
143         connect(actionPreferences, SIGNAL(triggered()), this, SLOT(showPreferences()));
144
145         //Create floating label
146         m_label = new QLabel(jobsView->viewport());
147         m_label->setText(tr("No job created yet. Please click the 'Add New Job' button!"));
148         m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
149         SET_TEXT_COLOR(m_label, Qt::darkGray);
150         SET_FONT_BOLD(m_label, true);
151         m_label->setVisible(true);
152         m_label->setContextMenuPolicy(Qt::ActionsContextMenu);
153         m_label->addActions(jobsView->actions());
154         connect(splitter, SIGNAL(splitterMoved(int, int)), this, SLOT(updateLabelPos()));
155         updateLabelPos();
156 }
157
158 /*
159  * Destructor
160  */
161 MainWindow::~MainWindow(void)
162 {
163         OptionsModel::saveTemplate(m_options, QString::fromLatin1(tpl_last));
164         
165         X264_DELETE(m_jobList);
166         X264_DELETE(m_options);
167         X264_DELETE(m_droppedFiles);
168         X264_DELETE(m_label);
169
170         while(!m_toolsList.isEmpty())
171         {
172                 QFile *temp = m_toolsList.takeFirst();
173                 X264_DELETE(temp);
174         }
175 }
176
177 ///////////////////////////////////////////////////////////////////////////////
178 // Slots
179 ///////////////////////////////////////////////////////////////////////////////
180
181 /*
182  * The "add" button was clicked
183  */
184 void MainWindow::addButtonPressed(const QString &filePath, int fileNo, int fileTotal, bool *ok)
185 {
186         qDebug("MainWindow::addButtonPressed");
187         
188         if(ok) *ok = false;
189         
190         AddJobDialog *addDialog = new AddJobDialog(this, m_options, m_cpuFeatures->x64);
191         addDialog->setRunImmediately(countRunningJobs() < (m_preferences.autoRunNextJob ? m_preferences.maxRunningJobCount : 1));
192         if((fileNo >= 0) && (fileTotal > 1)) addDialog->setWindowTitle(addDialog->windowTitle().append(tr(" (File %1 of %2)").arg(QString::number(fileNo+1), QString::number(fileTotal))));
193         if(!filePath.isEmpty()) addDialog->setSourceFile(filePath);
194
195         int result = addDialog->exec();
196         if(result == QDialog::Accepted)
197         {
198                 EncodeThread *thrd = new EncodeThread
199                 (
200                         addDialog->sourceFile(),
201                         addDialog->outputFile(),
202                         m_options,
203                         QString("%1/toolset").arg(m_appDir),
204                         m_cpuFeatures->x64,
205                         m_cpuFeatures->x64 && m_preferences.useAvisyth64Bit
206                 );
207
208                 QModelIndex newIndex = m_jobList->insertJob(thrd);
209
210                 if(newIndex.isValid())
211                 {
212                         if(addDialog->runImmediately())
213                         {
214                                 jobsView->selectRow(newIndex.row());
215                                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
216                                 m_jobList->startJob(newIndex);
217                         }
218
219                         if(ok) *ok = true;
220                 }
221         }
222
223         m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
224         X264_DELETE(addDialog);
225 }
226
227 /*
228  * The "start" button was clicked
229  */
230 void MainWindow::startButtonPressed(void)
231 {
232         m_jobList->startJob(jobsView->currentIndex());
233 }
234
235 /*
236  * The "abort" button was clicked
237  */
238 void MainWindow::abortButtonPressed(void)
239 {
240         m_jobList->abortJob(jobsView->currentIndex());
241 }
242
243 /*
244  * The "delete" button was clicked
245  */
246 void MainWindow::deleteButtonPressed(void)
247 {
248         m_jobList->deleteJob(jobsView->currentIndex());
249         m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
250 }
251
252 /*
253  * The "browse" button was clicked
254  */
255 void MainWindow::browseButtonPressed(void)
256 {
257         QString outputFile = m_jobList->getJobOutputFile(jobsView->currentIndex());
258         if((!outputFile.isEmpty()) && QFileInfo(outputFile).exists() && QFileInfo(outputFile).isFile())
259         {
260                 QProcess::startDetached(QString::fromLatin1("explorer.exe"), QStringList() << QString::fromLatin1("/select,") << QDir::toNativeSeparators(outputFile), QFileInfo(outputFile).path());
261         }
262         else
263         {
264                 QMessageBox::warning(this, tr("Not Found"), tr("Sorry, the output file could not be found!"));
265         }
266 }
267
268 /*
269  * The "pause" button was clicked
270  */
271 void MainWindow::pauseButtonPressed(bool checked)
272 {
273         if(checked)
274         {
275                 m_jobList->pauseJob(jobsView->currentIndex());
276         }
277         else
278         {
279                 m_jobList->resumeJob(jobsView->currentIndex());
280         }
281 }
282
283 /*
284  * Job item selected by user
285  */
286 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
287 {
288         qDebug("Job selected: %d", current.row());
289         
290         if(logView->model())
291         {
292                 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
293         }
294         
295         if(current.isValid())
296         {
297                 logView->setModel(m_jobList->getLogFile(current));
298                 connect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
299                 logView->actions().first()->setEnabled(true);
300                 QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
301
302                 progressBar->setValue(m_jobList->getJobProgress(current));
303                 editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
304                 updateButtons(m_jobList->getJobStatus(current));
305                 updateTaskbar(m_jobList->getJobStatus(current), m_jobList->data(m_jobList->index(current.row(), 0, QModelIndex()), Qt::DecorationRole).value<QIcon>());
306         }
307         else
308         {
309                 logView->setModel(NULL);
310                 logView->actions().first()->setEnabled(false);
311                 progressBar->setValue(0);
312                 editDetails->clear();
313                 updateButtons(EncodeThread::JobStatus_Undefined);
314                 updateTaskbar(EncodeThread::JobStatus_Undefined, QIcon());
315         }
316
317         progressBar->repaint();
318 }
319
320 /*
321  * Handle update of job info (status, progress, details, etc)
322  */
323 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
324 {
325         int selected = jobsView->currentIndex().row();
326         
327         if(topLeft.column() <= 1 && bottomRight.column() >= 1) /*STATUS*/
328         {
329                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
330                 {
331                         EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
332                         if(i == selected)
333                         {
334                                 qDebug("Current job changed status!");
335                                 updateButtons(status);
336                                 updateTaskbar(status, m_jobList->data(m_jobList->index(i, 0, QModelIndex()), Qt::DecorationRole).value<QIcon>());
337                         }
338                         if((status == EncodeThread::JobStatus_Completed) || (status == EncodeThread::JobStatus_Failed))
339                         {
340                                 if(m_preferences.autoRunNextJob) QTimer::singleShot(0, this, SLOT(launchNextJob()));
341                                 if(m_preferences.shutdownComputer) QTimer::singleShot(0, this, SLOT(shutdownComputer()));
342                         }
343                 }
344         }
345         if(topLeft.column() <= 2 && bottomRight.column() >= 2) /*PROGRESS*/
346         {
347                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
348                 {
349                         if(i == selected)
350                         {
351                                 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
352                                 WinSevenTaskbar::setTaskbarProgress(this, progressBar->value(), progressBar->maximum());
353                                 break;
354                         }
355                 }
356         }
357         if(topLeft.column() <= 3 && bottomRight.column() >= 3) /*DETAILS*/
358         {
359                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
360                 {
361                         if(i == selected)
362                         {
363                                 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
364                                 break;
365                         }
366                 }
367         }
368 }
369
370 /*
371  * Handle new log file content
372  */
373 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
374 {
375         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
376 }
377
378 /*
379  * About screen
380  */
381 void MainWindow::showAbout(void)
382 {
383         QString text;
384
385         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_patch());
386         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()));
387         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());
388         text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
389         text += QString().sprintf("it under the terms of the GNU General Public License &lt;http://www.gnu.org/&gt;.<br>");
390         text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
391         text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", home_url, home_url);
392
393         QMessageBox aboutBox(this);
394         aboutBox.setIconPixmap(QIcon(":/images/movie.png").pixmap(64,64));
395         aboutBox.setWindowTitle(tr("About..."));
396         aboutBox.setText(text.replace("-", "&minus;"));
397         aboutBox.addButton(tr("About x264"), QMessageBox::NoRole);
398         aboutBox.addButton(tr("About Qt"), QMessageBox::NoRole);
399         aboutBox.setEscapeButton(aboutBox.addButton(tr("Close"), QMessageBox::NoRole));
400                 
401         forever
402         {
403                 MessageBeep(MB_ICONINFORMATION);
404                 switch(aboutBox.exec())
405                 {
406                 case 0:
407                         {
408                                 QString text2;
409                                 text2 += tr("<nobr><tt>x264 - the best H.264/AVC encoder. Copyright (c) 2003-2012 x264 project.<br>");
410                                 text2 += tr("Free software library for encoding video streams into the H.264/MPEG-4 AVC format.<br>");
411                                 text2 += tr("Released under the terms of the GNU General Public License.<br><br>");
412                                 text2 += tr("Please visit <a href=\"%1\">%1</a> for obtaining a <u>commercial</u> x264 license!<br></tt></nobr>").arg("http://x264licensing.com/");
413
414                                 QMessageBox x264Box(this);
415                                 x264Box.setIconPixmap(QIcon(":/images/x264.png").pixmap(48,48));
416                                 x264Box.setWindowTitle(tr("About x264"));
417                                 x264Box.setText(text2.replace("-", "&minus;"));
418                                 x264Box.setEscapeButton(x264Box.addButton(tr("Close"), QMessageBox::NoRole));
419                                 MessageBeep(MB_ICONINFORMATION);
420                                 x264Box.exec();
421                         }
422                         break;
423                 case 1:
424                         QMessageBox::aboutQt(this);
425                         break;
426                 default:
427                         return;
428                 }
429         }
430 }
431
432 /*
433  * Open web-link
434  */
435 void MainWindow::showWebLink(void)
436 {
437         if(QObject::sender() == actionWebMulder)     QDesktopServices::openUrl(QUrl(home_url));
438         if(QObject::sender() == actionWebX264)       QDesktopServices::openUrl(QUrl("http://www.x264.com/"));
439         if(QObject::sender() == actionWebKomisar)    QDesktopServices::openUrl(QUrl("http://komisar.gin.by/"));
440         if(QObject::sender() == actionWebJarod)      QDesktopServices::openUrl(QUrl("http://www.x264.nl/"));
441         if(QObject::sender() == actionWebJEEB)       QDesktopServices::openUrl(QUrl("http://x264.fushizen.eu/"));
442         if(QObject::sender() == actionWebAvisynth32) QDesktopServices::openUrl(QUrl("http://sourceforge.net/projects/avisynth2/files/AviSynth%202.5/"));
443         if(QObject::sender() == actionWebAvisynth64) QDesktopServices::openUrl(QUrl("http://code.google.com/p/avisynth64/downloads/list"));
444         if(QObject::sender() == actionWebWiki)       QDesktopServices::openUrl(QUrl("http://mewiki.project357.com/wiki/X264_Settings"));
445         if(QObject::sender() == actionWebBluRay)     QDesktopServices::openUrl(QUrl("http://www.x264bluray.com/"));
446         if(QObject::sender() == actionWebSecret)     QDesktopServices::openUrl(QUrl("http://www.youtube.com/watch_popup?v=AXIeHY-OYNI"));
447 }
448
449 /*
450  * Pereferences dialog
451  */
452 void MainWindow::showPreferences(void)
453 {
454         PreferencesDialog *preferences = new PreferencesDialog(this, &m_preferences, m_cpuFeatures->x64);
455         preferences->exec();
456         X264_DELETE(preferences);
457 }
458
459 /*
460  * Launch next job, after running job has finished
461  */
462 void MainWindow::launchNextJob(void)
463 {
464         qDebug("launchNextJob(void)");
465
466         
467         const int rows = m_jobList->rowCount(QModelIndex());
468
469         if(countRunningJobs() >= m_preferences.maxRunningJobCount)
470         {
471                 qDebug("Still have too many jobs running, won't launch next one yet!");
472                 return;
473         }
474
475         int startIdx= jobsView->currentIndex().isValid() ? qBound(0, jobsView->currentIndex().row(), rows-1) : 0;
476
477         for(int i = 0; i < rows; i++)
478         {
479                 int currentIdx = (i + startIdx) % rows;
480                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(currentIdx, 0, QModelIndex()));
481                 if(status == EncodeThread::JobStatus_Enqueued)
482                 {
483                         if(m_jobList->startJob(m_jobList->index(currentIdx, 0, QModelIndex())))
484                         {
485                                 jobsView->selectRow(currentIdx);
486                                 return;
487                         }
488                 }
489         }
490                 
491         qWarning("No enqueued jobs left!");
492 }
493
494 /*
495  * Shut down the computer (with countdown)
496  */
497 void MainWindow::shutdownComputer(void)
498 {
499         qDebug("shutdownComputer(void)");
500         
501         if(countPendingJobs() > 0)
502         {
503                 qDebug("Still have pending jobs, won't shutdown yet!");
504                 return;
505         }
506         
507         const int iTimeout = 30;
508         const Qt::WindowFlags flags = Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint;
509         const QString text = QString("%1%2%1").arg(QString().fill(' ', 18), tr("Warning: Computer will shutdown in %1 seconds..."));
510         
511         qWarning("Initiating shutdown sequence!");
512         
513         QProgressDialog progressDialog(text.arg(iTimeout), tr("Cancel Shutdown"), 0, iTimeout + 1, this, flags);
514         QPushButton *cancelButton = new QPushButton(tr("Cancel Shutdown"), &progressDialog);
515         cancelButton->setIcon(QIcon(":/buttons/power_on.png"));
516         progressDialog.setModal(true);
517         progressDialog.setAutoClose(false);
518         progressDialog.setAutoReset(false);
519         progressDialog.setWindowIcon(QIcon(":/buttons/power_off.png"));
520         progressDialog.setWindowTitle(windowTitle());
521         progressDialog.setCancelButton(cancelButton);
522         progressDialog.show();
523         
524         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
525         QApplication::setOverrideCursor(Qt::WaitCursor);
526         PlaySound(MAKEINTRESOURCE(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE | SND_SYNC);
527         QApplication::restoreOverrideCursor();
528         
529         QTimer timer;
530         timer.setInterval(1000);
531         timer.start();
532
533         QEventLoop eventLoop(this);
534         connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
535         connect(&progressDialog, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
536
537         for(int i = 1; i <= iTimeout; i++)
538         {
539                 eventLoop.exec();
540                 if(progressDialog.wasCanceled())
541                 {
542                         progressDialog.close();
543                         return;
544                 }
545                 progressDialog.setValue(i+1);
546                 progressDialog.setLabelText(text.arg(iTimeout-i));
547                 if(iTimeout-i == 3) progressDialog.setCancelButton(NULL);
548                 QApplication::processEvents();
549                 PlaySound(MAKEINTRESOURCE((i < iTimeout) ? IDR_WAVE2 : IDR_WAVE3), GetModuleHandle(NULL), SND_RESOURCE | SND_SYNC);
550         }
551         
552         qWarning("Shutting down !!!");
553
554         if(x264_shutdown_computer("Simple x264 Launcher: All jobs completed, shutting down!", 10, true))
555         {
556                 qApp->closeAllWindows();
557         }
558 }
559
560 /*
561  * Main initialization function (called only once!)
562  */
563 void MainWindow::init(void)
564 {
565         static const char *binFiles = "x264.exe:x264_x64.exe:avs2yuv.exe:avs2yuv_x64.exe";
566         QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
567
568         updateLabelPos();
569
570         //Check all binaries
571         while(!binaries.isEmpty())
572         {
573                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
574                 QString current = binaries.takeFirst();
575                 QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
576                 if(file->open(QIODevice::ReadOnly))
577                 {
578                         bool binaryTypeOkay = false;
579                         DWORD binaryType;
580                         if(GetBinaryType(QWCHAR(file->fileName()), &binaryType))
581                         {
582                                 binaryTypeOkay = (binaryType == SCS_32BIT_BINARY || binaryType == SCS_64BIT_BINARY);
583                         }
584                         if(!binaryTypeOkay)
585                         {
586                                 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;"));
587                                 qFatal(QString("Binary is invalid: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
588                                 close(); qApp->exit(-1); return;
589                         }
590                         m_toolsList << file;
591                 }
592                 else
593                 {
594                         X264_DELETE(file);
595                         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;"));
596                         qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
597                         close(); qApp->exit(-1); return;
598                 }
599         }
600
601         //Check for portable mode
602         if(x264_portable())
603         {
604                 bool ok = false;
605                 static const char *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
606                 QFile writeTest(QString("%1/%2").arg(x264_data_path(), QUuid::createUuid().toString()));
607                 if(writeTest.open(QIODevice::WriteOnly))
608                 {
609                         ok = (writeTest.write(data) == strlen(data));
610                         writeTest.remove();
611                 }
612                 if(!ok)
613                 {
614                         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"));
615                         if(val != 1) { close(); qApp->exit(-1); return; }
616                 }
617         }
618
619         //Pre-release popup
620         if(x264_is_prerelease())
621         {
622                 qsrand(time(NULL)); int rnd = qrand() % 3;
623                 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);
624                 if(rnd != val) { close(); qApp->exit(-1); return; }
625         }
626
627         //Make sure this CPU can run x264 (requires MMX + MMXEXT/iSSE to run x264 with ASM enabled, additionally requires SSE1 for most x264 builds)
628         if(!(m_cpuFeatures->mmx && m_cpuFeatures->mmx2))
629         {
630                 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"));
631                 qFatal("System does not support MMX and MMXEXT, x264 will not work !!!");
632                 close(); qApp->exit(-1); return;
633         }
634         else if(!(m_cpuFeatures->mmx && m_cpuFeatures->sse))
635         {
636                 qWarning("WARNING: System does not support SSE1, most x264 builds will not work !!!\n");
637                 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"));
638                 if(val != 1) { close(); qApp->exit(-1); return; }
639         }
640
641         //Check for Avisynth support
642         bool avsAvailable = false;
643         QLibrary *avsLib = new QLibrary("avisynth.dll");
644         if(avsLib->load())
645         {
646                 avsAvailable = (avsLib->resolve("avs_create_script_environment") != NULL);
647         }
648         if(!avsAvailable)
649         {
650                 avsLib->unload(); X264_DELETE(avsLib);
651                 int val = QMessageBox::warning(this, tr("Avisynth Missing"), tr("<nobr>It appears that Avisynth is <b>not</b> currently installed on your computer.<br>Thus 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"));
652                 if(val != 1) { close(); qApp->exit(-1); return; }
653         }
654
655         //Check for expiration
656         if(x264_version_date().addMonths(6) < QDate::currentDate())
657         {
658                 QMessageBox msgBox(this);
659                 msgBox.setIconPixmap(QIcon(":/images/update.png").pixmap(56,56));
660                 msgBox.setWindowTitle(tr("Update Notification"));
661                 msgBox.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
662                 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));
663                 QPushButton *btn1 = msgBox.addButton(tr("Discard"), QMessageBox::NoRole);
664                 QPushButton *btn2 = msgBox.addButton(tr("Discard"), QMessageBox::AcceptRole);
665                 btn1->setEnabled(false);
666                 btn2->setVisible(false);
667                 QTimer::singleShot(5000, btn1, SLOT(hide()));
668                 QTimer::singleShot(5000, btn2, SLOT(show()));
669                 msgBox.exec();
670         }
671
672         //Add files from command-line
673         bool bAddFile = false;
674         QStringList files, args = qApp->arguments();
675         while(!args.isEmpty())
676         {
677                 QString current = args.takeFirst();
678                 if(!bAddFile)
679                 {
680                         bAddFile = (current.compare("--add", Qt::CaseInsensitive) == 0);
681                         continue;
682                 }
683                 if((!current.startsWith("--")) && QFileInfo(current).exists() && QFileInfo(current).isFile())
684                 {
685                         files << QFileInfo(current).canonicalFilePath();
686                 }
687         }
688         if(int totalFiles = files.count())
689         {
690                 bool ok = true; int n = 0;
691                 while((!files.isEmpty()) && ok)
692                 {
693                         QString currentFile = files.takeFirst();
694                         qDebug("Adding file: %s", currentFile.toUtf8().constData());
695                         addButtonPressed(currentFile, n++, totalFiles, &ok);
696                 }
697         }
698 }
699
700 /*
701  * Update the label position
702  */
703 void MainWindow::updateLabelPos(void)
704 {
705         const QWidget *const viewPort = jobsView->viewport();
706         m_label->setGeometry(0, 0, viewPort->width(), viewPort->height());
707 }
708
709 /*
710  * Copy the complete log to the clipboard
711  */
712 void MainWindow::copyLogToClipboard(bool checked)
713 {
714         qDebug("copyLogToClipboard");
715         
716         if(LogFileModel *log = dynamic_cast<LogFileModel*>(logView->model()))
717         {
718                 log->copyToClipboard();
719                 MessageBeep(MB_ICONINFORMATION);
720         }
721 }
722
723 /*
724  * Process the dropped files
725  */
726 void MainWindow::handleDroppedFiles(void)
727 {
728         qDebug("MainWindow::handleDroppedFiles");
729         if(m_droppedFiles)
730         {
731                 QStringList droppedFiles(*m_droppedFiles);
732                 m_droppedFiles->clear();
733                 int totalFiles = droppedFiles.count();
734                 bool ok = true; int n = 0;
735                 while((!droppedFiles.isEmpty()) && ok)
736                 {
737                         QString currentFile = droppedFiles.takeFirst();
738                         qDebug("Adding file: %s", currentFile.toUtf8().constData());
739                         addButtonPressed(currentFile, n++, totalFiles, &ok);
740                 }
741         }
742         qDebug("Leave from MainWindow::handleDroppedFiles!");
743 }
744
745 ///////////////////////////////////////////////////////////////////////////////
746 // Event functions
747 ///////////////////////////////////////////////////////////////////////////////
748
749 /*
750  * Window shown event
751  */
752 void MainWindow::showEvent(QShowEvent *e)
753 {
754         QMainWindow::showEvent(e);
755
756         if(m_firstShow)
757         {
758                 m_firstShow = false;
759                 QTimer::singleShot(0, this, SLOT(init()));
760         }
761 }
762
763 /*
764  * Window close event
765  */
766 void MainWindow::closeEvent(QCloseEvent *e)
767 {
768         if(countRunningJobs() > 0)
769         {
770                 e->ignore();
771                 QMessageBox::warning(this, tr("Jobs Are Running"), tr("Sorry, can not exit while there still are running jobs!"));
772                 return;
773         }
774         
775         if(countPendingJobs() > 0)
776         {
777                 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);
778                 if(ret != QMessageBox::Yes)
779                 {
780                         e->ignore();
781                         return;
782                 }
783         }
784
785         while(m_jobList->rowCount(QModelIndex()) > 0)
786         {
787                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
788                 if(!m_jobList->deleteJob(m_jobList->index(0, 0, QModelIndex())))
789                 {
790                         e->ignore();
791                         QMessageBox::warning(this, tr("Failed To Exit"), tr("Sorry, at least one job could not be deleted!"));
792                         return;
793                 }
794         }
795
796         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
797         QMainWindow::closeEvent(e);
798 }
799
800 /*
801  * Window resize event
802  */
803 void MainWindow::resizeEvent(QResizeEvent *e)
804 {
805         QMainWindow::resizeEvent(e);
806         updateLabelPos();
807 }
808
809 /*
810  * Event filter
811  */
812 bool MainWindow::eventFilter(QObject *o, QEvent *e)
813 {
814         if((o == labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
815         {
816                 QTimer::singleShot(0, this, SLOT(showAbout()));
817                 return true;
818         }
819         return false;
820 }
821
822 /*
823  * Win32 message filter
824  */
825 bool MainWindow::winEvent(MSG *message, long *result)
826 {
827         return WinSevenTaskbar::handleWinEvent(message, result);
828 }
829
830 /*
831  * File dragged over window
832  */
833 void MainWindow::dragEnterEvent(QDragEnterEvent *event)
834 {
835         QStringList formats = event->mimeData()->formats();
836         
837         if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
838         {
839                 event->acceptProposedAction();
840         }
841 }
842
843 /*
844  * File dropped onto window
845  */
846 void MainWindow::dropEvent(QDropEvent *event)
847 {
848         QStringList droppedFiles;
849         QList<QUrl> urls = event->mimeData()->urls();
850
851         while(!urls.isEmpty())
852         {
853                 QUrl currentUrl = urls.takeFirst();
854                 QFileInfo file(currentUrl.toLocalFile());
855                 if(file.exists() && file.isFile())
856                 {
857                         qDebug("MainWindow::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
858                         droppedFiles << file.canonicalFilePath();
859                 }
860         }
861         
862         if(droppedFiles.count() > 0)
863         {
864                 if(!m_droppedFiles)
865                 {
866                         m_droppedFiles = new QStringList();
867                 }
868                 m_droppedFiles->append(droppedFiles);
869                 m_droppedFiles->sort();
870                 QTimer::singleShot(0, this, SLOT(handleDroppedFiles()));
871         }
872 }
873
874 ///////////////////////////////////////////////////////////////////////////////
875 // Private functions
876 ///////////////////////////////////////////////////////////////////////////////
877
878 /*
879  * Jobs that are not completed (or failed, or aborted) yet
880  */
881 unsigned int MainWindow::countPendingJobs(void)
882 {
883         unsigned int count = 0;
884         const int rows = m_jobList->rowCount(QModelIndex());
885
886         for(int i = 0; i < rows; i++)
887         {
888                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
889                 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed)
890                 {
891                         count++;
892                 }
893         }
894
895         return count;
896 }
897
898 /*
899  * Jobs that are still active, i.e. not terminated or enqueued
900  */
901 unsigned int MainWindow::countRunningJobs(void)
902 {
903         unsigned int count = 0;
904         const int rows = m_jobList->rowCount(QModelIndex());
905
906         for(int i = 0; i < rows; i++)
907         {
908                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
909                 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed && status != EncodeThread::JobStatus_Enqueued)
910                 {
911                         count++;
912                 }
913         }
914
915         return count;
916 }
917
918 /*
919  * Update all buttons with respect to current job status
920  */
921 void MainWindow::updateButtons(EncodeThread::JobStatus status)
922 {
923         qDebug("MainWindow::updateButtons(void)");
924
925         buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
926         buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 || status == EncodeThread::JobStatus_Paused);
927         buttonPauseJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2);
928         buttonPauseJob->setChecked(status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Pausing);
929
930         actionJob_Delete->setEnabled(status == EncodeThread::JobStatus_Completed || status == EncodeThread::JobStatus_Aborted || status == EncodeThread::JobStatus_Failed || status == EncodeThread::JobStatus_Enqueued);
931         actionJob_Browse->setEnabled(status == EncodeThread::JobStatus_Completed);
932
933         actionJob_Start->setEnabled(buttonStartJob->isEnabled());
934         actionJob_Abort->setEnabled(buttonAbortJob->isEnabled());
935         actionJob_Pause->setEnabled(buttonPauseJob->isEnabled());
936         actionJob_Pause->setChecked(buttonPauseJob->isChecked());
937
938         editDetails->setEnabled(status != EncodeThread::JobStatus_Paused);
939 }
940
941 /*
942  * Update the taskbar with current job status
943  */
944 void MainWindow::updateTaskbar(EncodeThread::JobStatus status, const QIcon &icon)
945 {
946         qDebug("MainWindow::updateTaskbar(void)");
947
948         switch(status)
949         {
950         case EncodeThread::JobStatus_Undefined:
951                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarNoState);
952                 break;
953         case EncodeThread::JobStatus_Aborting:
954         case EncodeThread::JobStatus_Starting:
955         case EncodeThread::JobStatus_Pausing:
956         case EncodeThread::JobStatus_Resuming:
957                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
958                 break;
959         case EncodeThread::JobStatus_Aborted:
960         case EncodeThread::JobStatus_Failed:
961                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarErrorState);
962                 break;
963         case EncodeThread::JobStatus_Paused:
964                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarPausedState);
965                 break;
966         default:
967                 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarNormalState);
968                 break;
969         }
970
971         switch(status)
972         {
973         case EncodeThread::JobStatus_Aborting:
974         case EncodeThread::JobStatus_Starting:
975         case EncodeThread::JobStatus_Pausing:
976         case EncodeThread::JobStatus_Resuming:
977                 break;
978         default:
979                 WinSevenTaskbar::setTaskbarProgress(this, progressBar->value(), progressBar->maximum());
980                 break;
981         }
982
983         WinSevenTaskbar::setOverlayIcon(this, icon.isNull() ? NULL : &icon);
984 }