OSDN Git Service

Added option to delete jobs from the list + also added option to browse the output...
[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 "global.h"
25 #include "model_jobList.h"
26 #include "model_options.h"
27 #include "win_addJob.h"
28
29 #include <QDate>
30 #include <QTimer>
31 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QDesktopServices>
34 #include <QUrl>
35 #include <QDir>
36 #include <QLibrary>
37 #include <QProcess>
38
39 //#include <Shellapi.h>
40
41 const char *home_url = "http://mulder.brhack.net/";
42
43 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
44 #define SET_TEXT_COLOR(WIDGET,COLOR) { QPalette _palette = WIDGET->palette(); _palette.setColor(QPalette::WindowText, (COLOR)); _palette.setColor(QPalette::Text, (COLOR)); WIDGET->setPalette(_palette); }
45
46 ///////////////////////////////////////////////////////////////////////////////
47 // Constructor & Destructor
48 ///////////////////////////////////////////////////////////////////////////////
49
50 MainWindow::MainWindow(bool x64supported)
51 :
52         m_x64supported(x64supported),
53         m_appDir(QApplication::applicationDirPath()),
54         m_firstShow(true)
55 {
56         //Init the dialog, from the .ui file
57         setupUi(this);
58         setWindowFlags(windowFlags() & (~Qt::WindowMaximizeButtonHint));
59
60         //Register meta types
61         qRegisterMetaType<QUuid>("QUuid");
62         qRegisterMetaType<EncodeThread::JobStatus>("EncodeThread::JobStatus");
63
64         //Freeze minimum size
65         setMinimumSize(size());
66         splitter->setSizes(QList<int>() << 16 << 196);
67
68         //Update title
69         labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));
70         labelBuildDate->installEventFilter(this);
71         setWindowTitle(QString("%1 (%2 Mode)").arg(windowTitle(), m_x64supported ? "64-Bit" : "32-Bit"));
72         if(x264_is_prerelease()) setWindowTitle(QString("%1 | PRE-RELEASE VERSION").arg(windowTitle()));
73
74
75         //Create model
76         m_jobList = new JobListModel();
77         connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
78         jobsView->setModel(m_jobList);
79         
80         //Setup view
81         jobsView->horizontalHeader()->setSectionHidden(3, true);
82         jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
83         jobsView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
84         jobsView->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
85         jobsView->horizontalHeader()->resizeSection(1, 150);
86         jobsView->horizontalHeader()->resizeSection(2, 90);
87         jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
88         connect(jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
89
90         //Create context menu
91         QAction *actionClipboard = new QAction(QIcon(":/buttons/page_paste.png"), tr("Copy to Clipboard"), logView);
92         actionClipboard->setEnabled(false);
93         logView->addAction(actionClipboard);
94         connect(actionClipboard, SIGNAL(triggered(bool)), this, SLOT(copyLogToClipboard(bool)));
95         jobsView->addActions(menuJob->actions());
96
97         //Enable buttons
98         connect(buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
99         connect(buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
100         connect(buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
101         connect(buttonPauseJob, SIGNAL(toggled(bool)), this, SLOT(pauseButtonPressed(bool)));
102         connect(actionJob_Delete, SIGNAL(triggered()), this, SLOT(deleteButtonPressed()));
103         connect(actionJob_Browse, SIGNAL(triggered()), this, SLOT(browseButtonPressed()));
104
105         //Enable menu
106         connect(actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
107         connect(actionWebMulder, SIGNAL(triggered()), this, SLOT(showWebLink()));
108         connect(actionWebX264, SIGNAL(triggered()), this, SLOT(showWebLink()));
109         connect(actionWebKomisar, SIGNAL(triggered()), this, SLOT(showWebLink()));
110         connect(actionWebJarod, SIGNAL(triggered()), this, SLOT(showWebLink()));
111         connect(actionWebWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
112         connect(actionWebBluRay, SIGNAL(triggered()), this, SLOT(showWebLink()));
113
114         //Create floating label
115         m_label = new QLabel(jobsView);
116         m_label->setText(tr("No job created yet. Please click the 'Add New Job' button!"));
117         m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
118         SET_TEXT_COLOR(m_label, Qt::darkGray);
119         SET_FONT_BOLD(m_label, true);
120         m_label->setVisible(true);
121         m_label->setContextMenuPolicy(Qt::ActionsContextMenu);
122         m_label->addActions(jobsView->actions());
123         connect(splitter, SIGNAL(splitterMoved(int, int)), this, SLOT(updateLabel()));
124         updateLabel();
125
126         //Create options object
127         m_options = new OptionsModel();
128 }
129
130 MainWindow::~MainWindow(void)
131 {
132         X264_DELETE(m_jobList);
133         X264_DELETE(m_options);
134         X264_DELETE(m_label);
135
136         while(!m_toolsList.isEmpty())
137         {
138                 QFile *temp = m_toolsList.takeFirst();
139                 X264_DELETE(temp);
140         }
141 }
142
143 ///////////////////////////////////////////////////////////////////////////////
144 // Slots
145 ///////////////////////////////////////////////////////////////////////////////
146
147 void MainWindow::addButtonPressed(const QString &filePath, bool *ok)
148 {
149         if(ok) *ok = false;
150         
151         AddJobDialog *addDialog = new AddJobDialog(this, m_options);
152         addDialog->setRunImmediately(!havePendingJobs());
153         if(!filePath.isEmpty()) addDialog->setSourceFile(filePath);
154         int result = addDialog->exec();
155         
156         if(result == QDialog::Accepted)
157         {
158                 
159                 EncodeThread *thrd = new EncodeThread
160                 (
161                         addDialog->sourceFile(),
162                         addDialog->outputFile(),
163                         m_options,
164                         QString("%1/toolset").arg(m_appDir),
165                         m_x64supported
166                 );
167
168                 QModelIndex newIndex = m_jobList->insertJob(thrd);
169
170                 if(newIndex.isValid())
171                 {
172                         if(addDialog->runImmediately())
173                         {
174                                 jobsView->selectRow(newIndex.row());
175                                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
176                                 m_jobList->startJob(newIndex);
177                         }
178
179                         if(ok) *ok = true;
180                 }
181         }
182
183         m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
184         X264_DELETE(addDialog);
185 }
186
187 void MainWindow::startButtonPressed(void)
188 {
189         m_jobList->startJob(jobsView->currentIndex());
190 }
191
192 void MainWindow::abortButtonPressed(void)
193 {
194         m_jobList->abortJob(jobsView->currentIndex());
195 }
196
197 void MainWindow::deleteButtonPressed(void)
198 {
199         m_jobList->deleteJob(jobsView->currentIndex());
200         m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
201 }
202
203 void MainWindow::browseButtonPressed(void)
204 {
205         QString outputFile = m_jobList->getJobOutputFile(jobsView->currentIndex());
206         if((!outputFile.isEmpty()) && QFileInfo(outputFile).exists() && QFileInfo(outputFile).isFile())
207         {
208                 QProcess::startDetached(QString::fromLatin1("explorer.exe"), QStringList() << QString::fromLatin1("/select,") << QDir::toNativeSeparators(outputFile), QFileInfo(outputFile).path());
209         }
210         else
211         {
212                 QMessageBox::warning(this, tr("Not Found"), tr("Sorry, the output file could not be found!"));
213         }
214 }
215
216 void MainWindow::pauseButtonPressed(bool checked)
217 {
218         if(checked)
219         {
220                 m_jobList->pauseJob(jobsView->currentIndex());
221         }
222         else
223         {
224                 m_jobList->resumeJob(jobsView->currentIndex());
225         }
226 }
227
228 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
229 {
230         qDebug("Job selected: %d", current.row());
231         
232         if(logView->model())
233         {
234                 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
235         }
236         
237         if(current.isValid())
238         {
239                 logView->setModel(m_jobList->getLogFile(current));
240                 connect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
241                 logView->actions().first()->setEnabled(true);
242                 QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
243         
244                 progressBar->setValue(m_jobList->getJobProgress(current));
245                 editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
246                 updateButtons(m_jobList->getJobStatus(current));
247         }
248         else
249         {
250                 logView->setModel(NULL);
251                 logView->actions().first()->setEnabled(false);
252                 progressBar->setValue(0);
253                 editDetails->clear();
254                 updateButtons(EncodeThread::JobStatus_Undefined);
255         }
256
257         progressBar->repaint();
258 }
259
260 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
261 {
262         int selected = jobsView->currentIndex().row();
263         
264         if(topLeft.column() <= 1 && bottomRight.column() >= 1)
265         {
266                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
267                 {
268                         EncodeThread::JobStatus status =  m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
269                         if(i == selected)
270                         {
271                                 qDebug("Current job changed status!");
272                                 updateButtons(status);
273                         }
274                         if(status == EncodeThread::JobStatus_Completed)
275                         {
276                                 QTimer::singleShot(0, this, SLOT(launchNextJob()));
277                         }
278                 }
279         }
280         else if(topLeft.column() <= 2 && bottomRight.column() >= 2)
281         {
282                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
283                 {
284                         if(i == selected)
285                         {
286                                 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
287                                 break;
288                         }
289                 }
290         }
291         else if(topLeft.column() <= 3 && bottomRight.column() >= 3)
292         {
293                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
294                 {
295                         if(i == selected)
296                         {
297                                 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
298                                 break;
299                         }
300                 }
301         }
302 }
303
304 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
305 {
306         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
307 }
308
309 void MainWindow::showAbout(void)
310 {
311         QString text;
312
313         text += QString().sprintf("<nobr><tt>Simple x264 Launcher v%u.%02u - use 64-Bit x264 with 32-Bit Avisynth<br>", x264_version_major(), x264_version_minor());
314         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()));
315         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());
316         text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
317         text += QString().sprintf("it under the terms of the GNU General Public License &lt;http://www.gnu.org/&gt;.<br>");
318         text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
319         text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", home_url, home_url);
320
321         forever
322         {
323                 int ret = QMessageBox::information(this, tr("About..."), text.replace("-", "&minus;"), tr("About x264"), tr("About Qt"), tr("Close"));
324
325                 switch(ret)
326                 {
327                 case 0:
328                         {
329                                 QString text2;
330                                 text2 += tr("<nobr><tt>x264 - the best H.264/AVC encoder. Copyright (c) 2003-2012 x264 project.<br>");
331                                 text2 += tr("Free software library for encoding video streams into the H.264/MPEG-4 AVC format.<br>");
332                                 text2 += tr("Released under the terms of the GNU General Public License.<br><br>");
333                                 text2 += tr("Please visit <a href=\"%1\">%1</a> for obtaining a <u>commercial</u> x264 license!<br></tt></nobr>").arg("http://x264licensing.com/");
334                                 QMessageBox::information(this, tr("About x264"), text2.replace("-", "&minus;"), tr("Close"));
335                         }
336                         break;
337                 case 1:
338                         QMessageBox::aboutQt(this);
339                         break;
340                 default:
341                         return;
342                 }
343         }
344 }
345
346 void MainWindow::showWebLink(void)
347 {
348         if(QObject::sender() == actionWebMulder) QDesktopServices::openUrl(QUrl(home_url));
349         if(QObject::sender() == actionWebX264) QDesktopServices::openUrl(QUrl("http://www.x264.com/"));
350         if(QObject::sender() == actionWebKomisar) QDesktopServices::openUrl(QUrl("http://komisar.gin.by/"));
351         if(QObject::sender() == actionWebJarod) QDesktopServices::openUrl(QUrl("http://www.x264.nl/"));
352         if(QObject::sender() == actionWebWiki) QDesktopServices::openUrl(QUrl("http://mewiki.project357.com/wiki/X264_Settings"));
353         if(QObject::sender() == actionWebBluRay) QDesktopServices::openUrl(QUrl("http://www.x264bluray.com/"));
354 }
355
356 void MainWindow::launchNextJob(void)
357 {
358         const int rows = m_jobList->rowCount(QModelIndex());
359
360         for(int i = 0; i < rows; i++)
361         {
362                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
363                 if(status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2)
364                 {
365                         qWarning("Still have a job running, won't launch next yet!");
366                         return;
367                 }
368         }
369
370         for(int i = 0; i < rows; i++)
371         {
372                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
373                 if(status == EncodeThread::JobStatus_Enqueued)
374                 {
375                         m_jobList->startJob(m_jobList->index(i, 0, QModelIndex()));
376                         return;
377                 }
378         }
379
380         qWarning("No enqueued jobs left!");
381 }
382
383 void MainWindow::init(void)
384 {
385         static const char *binFiles = "x264.exe:x264_x64.exe:avs2yuv.exe";
386         QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
387
388         updateLabel();
389
390         //Check all binaries
391         while(!binaries.isEmpty())
392         {
393                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
394                 QString current = binaries.takeFirst();
395                 QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
396                 if(file->open(QIODevice::ReadOnly))
397                 {
398                         bool binaryTypeOkay = false;
399                         DWORD binaryType;
400                         if(GetBinaryType(QWCHAR(file->fileName()), &binaryType))
401                         {
402                                 binaryTypeOkay = (binaryType == SCS_32BIT_BINARY || binaryType == SCS_64BIT_BINARY);
403                         }
404                         if(!binaryTypeOkay)
405                         {
406                                 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;"));
407                                 qFatal(QString("Binary is invalid: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
408                                 close(); qApp->exit(-1); return;
409                         }
410                         m_toolsList << file;
411                 }
412                 else
413                 {
414                         X264_DELETE(file);
415                         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;"));
416                         qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
417                         close(); qApp->exit(-1); return;
418                 }
419         }
420
421         //Pre-release popup
422         if(x264_is_prerelease())
423         {
424                 qsrand(time(NULL)); int rnd = qrand() % 3;
425                 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);
426                 if(rnd != val) { close(); qApp->exit(-1); return; }
427         }
428
429         //Check for Avisynth support
430         bool avsAvailable = false;
431         QLibrary *avsLib = new QLibrary("avisynth.dll");
432         if(avsLib->load())
433         {
434                 avsAvailable = (avsLib->resolve("avs_create_script_environment") != NULL);
435         }
436         if(!avsAvailable)
437         {
438                 avsLib->unload(); X264_DELETE(avsLib);
439                 int val = QMessageBox::warning(this, tr("Avisynth Missing"), tr("<nobr>It appears that Avisynth is not currently installed on your computer.<br>Thus Avisynth input will not 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"));
440                 if(val != 1) { close(); qApp->exit(-1); return; }
441         }
442
443         //Check for expiration
444         if(x264_version_date().addMonths(6) < QDate::currentDate())
445         {
446                 QMessageBox msgBox(this);
447                 msgBox.setIcon(QMessageBox::Information);
448                 msgBox.setWindowTitle(tr("Update Notification"));
449                 msgBox.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
450                 msgBox.setText(tr("<nobr><tt>Oups, this version of 'Simple x264 Launcher' is more than 6 months old.<br><br>Please check the official web-site at <a href=\"%1\">%1</a> for updates!<br></tt></nobr>").replace("-", "&minus;").arg(home_url));
451                 QPushButton *btn1 = msgBox.addButton(tr("Discard"), QMessageBox::NoRole);
452                 QPushButton *btn2 = msgBox.addButton(tr("Discard"), QMessageBox::AcceptRole);
453                 btn1->setEnabled(false);
454                 btn2->setVisible(false);
455                 QTimer::singleShot(5000, btn1, SLOT(hide()));
456                 QTimer::singleShot(5000, btn2, SLOT(show()));
457                 msgBox.exec();
458         }
459 }
460
461 void MainWindow::updateLabel(void)
462 {
463         m_label->setGeometry(0, 0, jobsView->width(), jobsView->height());
464 }
465
466 void MainWindow::copyLogToClipboard(bool checked)
467 {
468         qDebug("copyLogToClipboard");
469         
470         if(LogFileModel *log = dynamic_cast<LogFileModel*>(logView->model()))
471         {
472                 log->copyToClipboard();
473                 MessageBeep(MB_ICONINFORMATION);
474         }
475 }
476
477 ///////////////////////////////////////////////////////////////////////////////
478 // Event functions
479 ///////////////////////////////////////////////////////////////////////////////
480
481 void MainWindow::showEvent(QShowEvent *e)
482 {
483         QMainWindow::showEvent(e);
484
485         if(m_firstShow)
486         {
487                 m_firstShow = false;
488                 QTimer::singleShot(0, this, SLOT(init()));
489         }
490 }
491
492 void MainWindow::closeEvent(QCloseEvent *e)
493 {
494         if(haveRunningJobs())
495         {
496                 e->ignore();
497                 QMessageBox::warning(this, tr("Jobs Are Running"), tr("Sorry, can not exit while there still are running jobs!"));
498                 return;
499         }
500         
501         if(havePendingJobs())
502         {
503                 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);
504                 if(ret != QMessageBox::Yes)
505                 {
506                         e->ignore();
507                         return;
508                 }
509         }
510
511         while(m_jobList->rowCount(QModelIndex()) > 0)
512         {
513                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
514                 if(!m_jobList->deleteJob(m_jobList->index(0, 0, QModelIndex())))
515                 {
516                         e->ignore();
517                         QMessageBox::warning(this, tr("Failed To Exit"), tr("Sorry, at least one job could not be deleted!"));
518                         return;
519                 }
520         }
521
522         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
523         QMainWindow::closeEvent(e);
524 }
525
526 void MainWindow::resizeEvent(QResizeEvent *e)
527 {
528         QMainWindow::resizeEvent(e);
529         updateLabel();
530 }
531
532 bool MainWindow::eventFilter(QObject *o, QEvent *e)
533 {
534         if((o == labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
535         {
536                 QTimer::singleShot(0, this, SLOT(showAbout()));
537                 return true;
538         }
539         return false;
540 }
541
542 /*
543  * File dragged over window
544  */
545 void MainWindow::dragEnterEvent(QDragEnterEvent *event)
546 {
547         QStringList formats = event->mimeData()->formats();
548         
549         if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
550         {
551                 event->acceptProposedAction();
552         }
553 }
554
555 /*
556  * File dropped onto window
557  */
558 void MainWindow::dropEvent(QDropEvent *event)
559 {
560         QStringList droppedFiles;
561         QList<QUrl> urls = event->mimeData()->urls();
562
563         while(!urls.isEmpty())
564         {
565                 QUrl currentUrl = urls.takeFirst();
566                 QFileInfo file(currentUrl.toLocalFile());
567                 if(file.exists() && file.isFile())
568                 {
569                         qDebug("Dropped File: %s", file.canonicalFilePath().toUtf8().constData());
570                         droppedFiles << file.canonicalFilePath();
571                 }
572         }
573         
574         droppedFiles.sort();
575         
576         bool ok = true;
577         while((!droppedFiles.isEmpty()) && ok)
578         {
579                 QString currentFile = droppedFiles.takeFirst();
580                 qDebug("Adding file: %s", currentFile.toUtf8().constData());
581                 addButtonPressed(currentFile, &ok);
582         }
583 }
584
585 ///////////////////////////////////////////////////////////////////////////////
586 // Private functions
587 ///////////////////////////////////////////////////////////////////////////////
588
589 /*Jobs that are not completed (or failed, or aborted) yet*/
590 bool MainWindow::havePendingJobs(void)
591 {
592         const int rows = m_jobList->rowCount(QModelIndex());
593
594         for(int i = 0; i < rows; i++)
595         {
596                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
597                 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed)
598                 {
599                         return true;
600                 }
601         }
602
603         return false;
604 }
605
606 /*Jobs that are still active, i.e. not terminated or enqueued*/
607 bool MainWindow::haveRunningJobs(void)
608 {
609         const int rows = m_jobList->rowCount(QModelIndex());
610
611         for(int i = 0; i < rows; i++)
612         {
613                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
614                 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed && status != EncodeThread::JobStatus_Enqueued)
615                 {
616                         return true;
617                 }
618         }
619
620         return false;
621 }
622
623 void MainWindow::updateButtons(EncodeThread::JobStatus status)
624 {
625         qDebug("MainWindow::updateButtons(void)");
626
627         buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
628         buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 || status == EncodeThread::JobStatus_Paused);
629         buttonPauseJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2);
630         buttonPauseJob->setChecked(status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Pausing);
631
632         actionJob_Delete->setEnabled(status == EncodeThread::JobStatus_Completed || status == EncodeThread::JobStatus_Aborted || status == EncodeThread::JobStatus_Failed || status == EncodeThread::JobStatus_Enqueued);
633         actionJob_Browse->setEnabled(status == EncodeThread::JobStatus_Completed);
634
635         actionJob_Start->setEnabled(buttonStartJob->isEnabled());
636         actionJob_Abort->setEnabled(buttonAbortJob->isEnabled());
637         actionJob_Pause->setEnabled(buttonPauseJob->isEnabled());
638         actionJob_Pause->setChecked(buttonPauseJob->isChecked());
639
640         editDetails->setEnabled(status != EncodeThread::JobStatus_Paused);
641 }