OSDN Git Service

Do not close the application in shutdownComputer(), if hibernation was requested.
[x264-launcher/x264-launcher.git] / src / win_main.cpp
index eaee34b..2495b8b 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Simple x264 Launcher
-// Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -71,7 +71,7 @@
 #include <QFileDialog>
 #include <QSystemTrayIcon>
 #include <QMovie>
-
+#include <QTextDocument>
 #include <ctime>
 
 //Constants
@@ -109,6 +109,7 @@ MainWindow::MainWindow(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtil
        m_pendingFiles(new QStringList()),
        m_preferences(NULL),
        m_recentlyUsed(NULL),
+       m_postOperation(POST_OP_DONOTHING),
        m_initialized(false),
        ui(new Ui::MainWindow())
 {
@@ -140,6 +141,9 @@ MainWindow::MainWindow(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtil
        m_options.reset(new OptionsModel(m_sysinfo.data()));
        OptionsModel::loadTemplate(m_options.data(), QString::fromLatin1(tpl_last));
 
+       //DPI scaling
+       MUtils::GUI::scale_widget(this);
+
        //Freeze minimum size
        setMinimumSize(size());
        ui->splitter->setSizes(QList<int>() << 16 << 196);
@@ -217,21 +221,29 @@ MainWindow::MainWindow(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtil
        connect(ui->actionOpen,             SIGNAL(triggered()), this, SLOT(openActionTriggered()));
        connect(ui->actionCleanup_Finished, SIGNAL(triggered()), this, SLOT(cleanupActionTriggered()));
        connect(ui->actionCleanup_Enqueued, SIGNAL(triggered()), this, SLOT(cleanupActionTriggered()));
+       connect(ui->actionPostOp_DoNothing, SIGNAL(triggered()), this, SLOT(postOpActionTriggered()));
+       connect(ui->actionPostOp_PowerDown, SIGNAL(triggered()), this, SLOT(postOpActionTriggered()));
+       connect(ui->actionPostOp_Hibernate, SIGNAL(triggered()), this, SLOT(postOpActionTriggered()));
        connect(ui->actionAbout,            SIGNAL(triggered()), this, SLOT(showAbout()));
        connect(ui->actionPreferences,      SIGNAL(triggered()), this, SLOT(showPreferences()));
        connect(ui->actionCheckForUpdates,  SIGNAL(triggered()), this, SLOT(checkUpdates()));
        ui->actionCleanup_Finished->setData(QVariant(bool(0)));
        ui->actionCleanup_Enqueued->setData(QVariant(bool(1)));
+       ui->actionPostOp_DoNothing->setData(QVariant(POST_OP_DONOTHING));
+       ui->actionPostOp_PowerDown->setData(QVariant(POST_OP_POWERDOWN));
+       ui->actionPostOp_Hibernate->setData(QVariant(POST_OP_HIBERNATE));
+       ui->actionPostOp_Hibernate->setEnabled(MUtils::OS::is_hibernation_supported());
 
        //Setup web-links
        SETUP_WEBLINK(ui->actionWebMulder,          home_url);
        SETUP_WEBLINK(ui->actionWebX264,            "http://www.videolan.org/developers/x264.html");
        SETUP_WEBLINK(ui->actionWebX265,            "http://www.videolan.org/developers/x265.html");
+       SETUP_WEBLINK(ui->actionWebX264LigH,        "http://www.mediafire.com/?bxvu1vvld31k1");
+       SETUP_WEBLINK(ui->actionWebX264VideoLAN,    "http://artifacts.videolan.org/x264/");
        SETUP_WEBLINK(ui->actionWebX264Komisar,     "http://komisar.gin.by/");
-       SETUP_WEBLINK(ui->actionWebX264VideoLAN,    "http://download.videolan.org/pub/x264/binaries/");
+       SETUP_WEBLINK(ui->actionWebX265LigH,        "http://www.mediafire.com/?6lfp2jlygogwa");
        SETUP_WEBLINK(ui->actionWebX264FreeCodecs,  "http://www.free-codecs.com/x264_video_codec_download.htm");
        SETUP_WEBLINK(ui->actionWebX265Fllear,      "http://x265.ru/en/builds/");
-       SETUP_WEBLINK(ui->actionWebX265LigH,        "https://www.mediafire.com/?6lfp2jlygogwa");
        SETUP_WEBLINK(ui->actionWebX265Snowfag,     "http://builds.x265.eu/");
        SETUP_WEBLINK(ui->actionWebX265FreeCodecs,  "http://www.free-codecs.com/x265_hevc_encoder_download.htm");
        SETUP_WEBLINK(ui->actionWebAvisynth32,      "https://sourceforge.net/projects/avisynth2/files/AviSynth%202.6/");
@@ -413,6 +425,31 @@ void MainWindow::cleanupActionTriggered(void)
 }
 
 /*
+* The "clean-up" action was invoked
+*/
+void MainWindow::postOpActionTriggered(void)
+{
+       ENSURE_APP_IS_READY();
+
+       QAction *const sender = dynamic_cast<QAction*>(QObject::sender());
+       if (sender)
+       {
+               const QVariant data = sender->data();
+               if (data.isValid() && (data.type() == QVariant::Int))
+               {
+                       const postOp_t mode = (postOp_t)data.toInt();
+                       if ((mode >= POST_OP_DONOTHING) && (mode <= POST_OP_HIBERNATE))
+                       {
+                               m_postOperation = mode;
+                               ui->actionPostOp_PowerDown->setChecked(mode == POST_OP_POWERDOWN);
+                               ui->actionPostOp_Hibernate->setChecked(mode == POST_OP_HIBERNATE);
+                               ui->actionPostOp_DoNothing->setChecked(mode == POST_OP_DONOTHING);
+                       }
+               }
+       }
+}
+
+/*
  * The "start" button was clicked
  */
 void MainWindow::startButtonPressed(void)
@@ -720,8 +757,9 @@ void MainWindow::launchNextJob(void)
                
        qWarning("No enqueued jobs left to be started!");
 
-       if(m_preferences->getShutdownComputer())
+       if(m_postOperation)
        {
+               qDebug("Post operation has been scheduled! (m_postOperation: %d)", m_postOperation);
                QTimer::singleShot(0, this, SLOT(shutdownComputer()));
        }
 }
@@ -733,20 +771,31 @@ void MainWindow::saveLogFile(const QModelIndex &index)
 {
        if(index.isValid())
        {
-               if(LogFileModel *log = m_jobList->getLogFile(index))
+               const LogFileModel *const logData = m_jobList->getLogFile(index);
+               const QString &outputFilePath = m_jobList->getJobOutputFile(index);
+               if(logData && (!outputFilePath.isEmpty()))
                {
-                       const QString outputDir = QString("%1/logs").arg(x264_data_path());
-                       const QString timeStamp = QDateTime::currentDateTime().toString("yyyy-MM-dd.HH-mm-ss");
-                       QDir(outputDir).mkpath(".");
-                       const QString logFilePath = MUtils::make_unique_file(outputDir, QString("LOG.%1").arg(timeStamp), QLatin1String("txt"));
-                       if(logFilePath.isEmpty())
+                       const QFileInfo outputFileInfo(outputFilePath);
+                       if (outputFileInfo.absoluteDir().exists())
                        {
-                               qWarning("Failed to generate log file name. Giving up!");
-                               return;
+                               const QString outputDir = outputFileInfo.absolutePath(), outputName = outputFileInfo.fileName();
+                               const QString logFilePath = MUtils::make_unique_file(outputDir, outputName, QLatin1String("log"), true);
+                               if (!logFilePath.isEmpty())
+                               {
+                                       qDebug("Saving log file to: \"%s\"", MUTILS_UTF8(logFilePath));
+                                       if (!logData->saveToLocalFile(logFilePath))
+                                       {
+                                               qWarning("Failed to open log file for writing:\n%s", logFilePath.toUtf8().constData());
+                                       }
+                               }
+                               else
+                               {
+                                       qWarning("Failed to generate log file name. Giving up!");
+                               }
                        }
-                       if(!log->saveToLocalFile(logFilePath))
+                       else
                        {
-                               qWarning("Failed to open log file for writing:\n%s", logFilePath.toUtf8().constData());
+                               qWarning("Output directory does not seem to exist. Giving up!");
                        }
                }
        }
@@ -758,16 +807,23 @@ void MainWindow::saveLogFile(const QModelIndex &index)
 void MainWindow::shutdownComputer(void)
 {
        ENSURE_APP_IS_READY();
+       qDebug("shutdownComputer (m_postOperation: %d)", m_postOperation);
 
        if(countPendingJobs() > 0)
        {
-               qDebug("Still have pending jobs, won't shutdown yet!");
+               qWarning("Still have pending jobs, won't shutdown yet!");
                return;
        }
 
+       if ((m_postOperation != POST_OP_POWERDOWN) && (m_postOperation != POST_OP_HIBERNATE))
+       {
+               qWarning("No post-operation has been schedule!");
+       }
+
        const int iTimeout = 30;
        const Qt::WindowFlags flags = Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint;
-       const QString text = QString("%1%2%1").arg(QString().fill(' ', 18), tr("Warning: Computer will shutdown in %1 seconds..."));
+       const bool hibernate = (m_postOperation == POST_OP_HIBERNATE);
+       const QString text = QString("%1%2%1").arg(QString().fill(' ', 18), hibernate ? tr("Warning: Computer will hibernate in %1 seconds...") : tr("Warning: Computer will shutdown in %1 seconds..."));
        
        qWarning("Initiating shutdown sequence!");
        
@@ -812,9 +868,12 @@ void MainWindow::shutdownComputer(void)
        
        qWarning("Shutting down !!!");
 
-       if(MUtils::OS::shutdown_computer("Simple x264 Launcher: All jobs completed, shutting down!", 10, true, false))
+       if(MUtils::OS::shutdown_computer("Simple x264 Launcher: All jobs completed, shutting down!", 10, true, hibernate))
        {
-               qApp->closeAllWindows();
+               if (!hibernate)
+               {
+                       qApp->closeAllWindows();
+               }
        }
 
 }
@@ -839,10 +898,11 @@ void MainWindow::init(void)
        //---------------------------------------
                
        qDebug("[Validating binaries]");
-       if(!BinariesCheckThread::check(m_sysinfo.data()))
+       QString failedPath;
+       if(!BinariesCheckThread::check(m_sysinfo.data(), &failedPath))
        {
-               QMessageBox::critical(this, tr("Invalid File!"), tr("<nobr>At least one tool is missing or is not a valid Win32/Win64 binary.<br>Please re-install the program in order to fix the problem!</nobr>").replace("-", "&minus;"));
-               qFatal("At least one tool is missing or is not a valid Win32/Win64 binary!");
+               QMessageBox::critical(this, tr("Invalid File!"), tr("<nobr>At least one tool is missing or is not a valid Win32/Win64 binary:</nobr><br><tt>%1</tt><br><br><nobr>Please re-install the program in order to fix the problem!</nobr>").replace("-", "&minus;").arg(Qt::escape(QDir::toNativeSeparators(failedPath))));
+               qFatal("At least one tool is missing or is not a valid Win32/Win64 binary. Program will exit now!");
        }
        qDebug(" ");
        
@@ -1342,18 +1402,22 @@ void MainWindow::closeEvent(QCloseEvent *e)
        //Save pending jobs for next time, if desired by user
        if(countPendingJobs() > 0)
        {
-               int ret = QMessageBox::question(this, tr("Jobs Are Pending"), tr("You still have pending jobs. How do you want to proceed?"), tr("Save Pending Jobs"), tr("Discard"));
-               if(ret == 0)
+               if (!m_preferences->getSaveQueueNoConfirm())
                {
-                       m_jobList->saveQueuedJobs();
+                       const int ret = QMessageBox::question(this, tr("Jobs Are Pending"), tr("<nobr>You still have some pending jobs in your queue. How do you want to proceed?</nobr>"), tr("Save Jobs"), tr("Always Save Jobs"), tr("Discard Jobs"));
+                       if ((ret >= 0) && (ret <= 1))
+                       {
+                               if (ret > 0)
+                               {
+                                       m_preferences->setSaveQueueNoConfirm(true);
+                                       PreferencesModel::savePreferences(m_preferences.data());
+                               }
+                               m_jobList->saveQueuedJobs();
+                       }
                }
                else
                {
-                       if(QMessageBox::warning(this, tr("Jobs Are Pending"), tr("Do you really want to discard all pending jobs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes)
-                       {
-                               e->ignore();
-                               return;
-                       }
+                       m_jobList->saveQueuedJobs();
                }
        }