OSDN Git Service

Extinguished some remaining uses of argv[] or QApplication::arguments().
[lamexp/LameXP.git] / src / Dialog_Processing.cpp
index cfdc7ed..d73b199 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 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
@@ -107,7 +107,8 @@ ProcessingDialog::ProcessingDialog(FileListModel *fileListModel, AudioFileModel
        m_shutdownFlag(shutdownFlag_None),
        m_diskObserver(NULL),
        m_cpuObserver(NULL),
-       m_ramObserver(NULL)
+       m_ramObserver(NULL),
+       m_firstShow(true)
 {
        //Init the dialog, from the .ui file
        setupUi(this);
@@ -185,6 +186,7 @@ ProcessingDialog::ProcessingDialog(FileListModel *fileListModel, AudioFileModel
        m_succeededJobs.clear();
        m_failedJobs.clear();
        m_userAborted = false;
+       m_forcedAbort = false;
        m_timerStart = 0I64;
 }
 
@@ -255,33 +257,33 @@ ProcessingDialog::~ProcessingDialog(void)
 
 void ProcessingDialog::showEvent(QShowEvent *event)
 {
-       static const char *NA = " N/A";
+       QDialog::showEvent(event);
 
-       setCloseButtonEnabled(false);
-       button_closeDialog->setEnabled(false);
-       button_AbortProcess->setEnabled(false);
-       m_systemTray->setVisible(true);
-       
-       if(!SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS))
+       if(m_firstShow)
        {
-               SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
-       }
+               static const char *NA = " N/A";
+       
+               setCloseButtonEnabled(false);
+               button_closeDialog->setEnabled(false);
+               button_AbortProcess->setEnabled(false);
+               m_systemTray->setVisible(true);
+       
+               if(!SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS))
+               {
+                       SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
+               }
 
-       label_cpu->setText(NA);
-       label_disk->setText(NA);
-       label_ram->setText(NA);
+               label_cpu->setText(NA);
+               label_disk->setText(NA);
+               label_ram->setText(NA);
 
-       QTimer::singleShot(1000, this, SLOT(initEncoding()));
+               QTimer::singleShot(1000, this, SLOT(initEncoding()));
+               m_firstShow = false;
+       }
 }
 
 void ProcessingDialog::closeEvent(QCloseEvent *event)
 {
-       if(lamexp_session_ending() && !m_userAborted)
-       {
-               qWarning("Computer is shutting down, LameXP will abort and exit!");
-               abortEncoding();
-       }
-       
        if(!button_closeDialog->isEnabled())
        {
                event->ignore();
@@ -321,6 +323,31 @@ bool ProcessingDialog::eventFilter(QObject *obj, QEvent *event)
        return false;
 }
 
+bool ProcessingDialog::event(QEvent *e)
+{
+       switch(e->type())
+       {
+       case lamexp_event_queryendsession:
+               qWarning("System is shutting down, preparing to abort...");
+               if(!m_userAborted) abortEncoding(true);
+               return true;
+       case lamexp_event_endsession:
+               qWarning("System is shutting down, encoding will be aborted now...");
+               if(isVisible())
+               {
+                       while(!close())
+                       {
+                               if(!m_userAborted) abortEncoding(true);
+                               QApplication::processEvents(QEventLoop::WaitForMoreEvents & QEventLoop::ExcludeUserInputEvents);
+                       }
+               }
+               m_pendingJobs.clear();
+               return true;
+       default:
+               return QDialog::event(e);
+       }
+}
+
 bool ProcessingDialog::winEvent(MSG *message, long *result)
 {
        return WinSevenTaskbar::handleWinEvent(message, result);
@@ -332,12 +359,15 @@ bool ProcessingDialog::winEvent(MSG *message, long *result)
 
 void ProcessingDialog::initEncoding(void)
 {
+       qDebug("Initializing encoding process...");
+       
        m_runningThreads = 0;
        m_currentFile = 0;
        m_allJobs.clear();
        m_succeededJobs.clear();
        m_failedJobs.clear();
        m_userAborted = false;
+       m_forcedAbort = false;
        m_playList.clear();
        
        CHANGE_BACKGROUND_COLOR(frame_header, QColor(Qt::white));
@@ -377,7 +407,7 @@ void ProcessingDialog::initEncoding(void)
        unsigned int maximumInstances = qBound(0U, m_settings->maximumInstances(), MAX_INSTANCES);
        if(maximumInstances < 1)
        {
-               lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
+               lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features(lamexp_arguments());
                maximumInstances = cores2instances(qBound(1, cpuFeatures.count, 64));
        }
 
@@ -399,11 +429,11 @@ void ProcessingDialog::initEncoding(void)
        }
 }
 
-void ProcessingDialog::abortEncoding(void)
+void ProcessingDialog::abortEncoding(bool force)
 {
        m_userAborted = true;
+       if(force) m_forcedAbort = true;
        button_AbortProcess->setEnabled(false);
-       
        SET_PROGRESS_TEXT(tr("Aborted! Waiting for running jobs to terminate..."));
 
        for(int i = 0; i < m_threadList.count(); i++)
@@ -429,7 +459,7 @@ void ProcessingDialog::doneEncoding(void)
                m_threadList.takeAt(index)->deleteLater();
        }
 
-       if(!m_pendingJobs.isEmpty() && !m_userAborted && !lamexp_session_ending())
+       if(!m_pendingJobs.isEmpty() && !m_userAborted)
        {
                startNextJob();
                qDebug("Running jobs: %u", m_runningThreads);
@@ -445,14 +475,14 @@ void ProcessingDialog::doneEncoding(void)
        QApplication::setOverrideCursor(Qt::WaitCursor);
        qDebug("Running jobs: %u", m_runningThreads);
 
-       if(!m_userAborted && m_settings->createPlaylist() && !m_settings->outputToSourceDir() && !lamexp_session_ending())
+       if(!m_userAborted && m_settings->createPlaylist() && !m_settings->outputToSourceDir())
        {
                SET_PROGRESS_TEXT(tr("Creating the playlist file, please wait..."));
                QApplication::processEvents();
                writePlayList();
        }
        
-       if(m_userAborted || lamexp_session_ending())
+       if(m_userAborted)
        {
                CHANGE_BACKGROUND_COLOR(frame_header, QColor("#FFF3BA"));
                WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarErrorState);
@@ -461,7 +491,7 @@ void ProcessingDialog::doneEncoding(void)
                m_systemTray->showMessage(tr("LameXP - Aborted"), tr("Process was aborted by the user."), QSystemTrayIcon::Warning);
                m_systemTray->setIcon(QIcon(":/icons/cd_delete.png"));
                QApplication::processEvents();
-               if(m_settings->soundsEnabled() && !lamexp_session_ending())
+               if(m_settings->soundsEnabled() && !m_forcedAbort)
                {
                        PlaySound(MAKEINTRESOURCE(IDR_WAVE_ABORTED), GetModuleHandle(NULL), SND_RESOURCE | SND_SYNC);
                }
@@ -515,11 +545,7 @@ void ProcessingDialog::doneEncoding(void)
 
        QApplication::restoreOverrideCursor();
 
-       if(lamexp_session_ending())
-       {
-               accept();
-       }
-       else if(!m_userAborted && checkBox_shutdownComputer->isChecked())
+       if(!m_userAborted && checkBox_shutdownComputer->isChecked())
        {
                if(shutdownComputer())
                {
@@ -945,7 +971,7 @@ void ProcessingDialog::systemTrayActivated(QSystemTrayIcon::ActivationReason rea
 {
        if(reason == QSystemTrayIcon::DoubleClick)
        {
-               SetForegroundWindow(this->winId());
+               SetForegroundWindow(reinterpret_cast<HWND>(this->winId()));
        }
 }