OSDN Git Service

Some improvements and simplifications to error handling functions.
[lamexp/LameXP.git] / src / Main.cpp
index b27e730..3305b57 100644 (file)
@@ -1,11 +1,12 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2014 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
 // the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// (at your option) any later version, but always including the *additional*
+// restrictions defined in the "License.txt" file.
 //
 // This program is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -30,6 +31,7 @@
 #include "Model_FileList.h"
 #include "Model_AudioFile.h"
 #include "Encoder_Abstract.h"
+#include "WinSevenTaskbar.h"
 
 //Qt includes
 #include <QApplication>
 #include <QDate>
 #include <QMutex>
 #include <QDir>
-#include <QInputDialog>
 
 ///////////////////////////////////////////////////////////////////////////////
 // Main function
 ///////////////////////////////////////////////////////////////////////////////
 
-int lamexp_main(int argc, char* argv[])
+static int lamexp_main(int argc, char* argv[])
 {
        int iResult = -1;
+       int iShutdown = shutdownFlag_None;
        bool bAccepted = true;
-       bool bShutdown = false;
-       
+
+       //Get CLI arguments
+       const QStringList &arguments = lamexp_arguments();
+
        //Init console
-       lamexp_init_console(argc, argv);
+       lamexp_init_console(arguments);
 
        //Print version info
-       qDebug("LameXP - Audio Encoder Front-End");
-       qDebug("Version %d.%02d %s, Build %d [%s], compiled with %s", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_compiler());
-       qDebug("Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>\n", max(lamexp_version_date().year(),QDate::currentDate().year()));
+       qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
+       qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(lamexp_version_date().year(), lamexp_current_date_safe().year()));
+       qDebug("Built on %s at %s with %s for Win-%s.\n", lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_time(), lamexp_version_compiler(), lamexp_version_arch());
        
        //print license info
        qDebug("This program is free software: you can redistribute it and/or modify");
        qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
-       qDebug("This program comes with ABSOLUTELY NO WARRANTY.\n");
+       qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
 
        //Print warning, if this is a "debug" build
        if(LAMEXP_DEBUG)
@@ -70,14 +74,22 @@ int lamexp_main(int argc, char* argv[])
                qWarning("---------------------------------------------------------\n"); 
        }
        
+       //Enumerate CLI arguments
+       qDebug("Command-Line Arguments:");
+       for(int i = 0; i < arguments.count(); i++)
+       {
+               qDebug("argv[%d]=%s", i, QUTF8(arguments.at(i)));
+       }
+       qDebug("");
+
        //Detect CPU capabilities
-       lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
-       qDebug("   CPU vendor id  :  %s (Intel: %d)", cpuFeatures.vendor, (cpuFeatures.intel ? 1 : 0));
+       lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features(arguments);
+       qDebug("   CPU vendor id  :  %s (Intel: %s)", cpuFeatures.vendor, LAMEXP_BOOL2STR(cpuFeatures.intel));
        qDebug("CPU brand string  :  %s", cpuFeatures.brand);
        qDebug("   CPU signature  :  Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
-       qDebug("CPU capabilities  :  MMX: %s, SSE: %s, SSE2: %s, SSE3: %s, SSSE3: %s, x64: %s", LAMEXP_BOOL(cpuFeatures.mmx), LAMEXP_BOOL(cpuFeatures.sse), LAMEXP_BOOL(cpuFeatures.sse2), LAMEXP_BOOL(cpuFeatures.sse3), LAMEXP_BOOL(cpuFeatures.ssse3), LAMEXP_BOOL(cpuFeatures.x64));
+       qDebug("CPU capabilities  :  MMX: %s, SSE: %s, SSE2: %s, SSE3: %s, SSSE3: %s, x64: %s", LAMEXP_BOOL2STR(cpuFeatures.mmx), LAMEXP_BOOL2STR(cpuFeatures.sse), LAMEXP_BOOL2STR(cpuFeatures.sse2), LAMEXP_BOOL2STR(cpuFeatures.sse3), LAMEXP_BOOL2STR(cpuFeatures.ssse3), LAMEXP_BOOL2STR(cpuFeatures.x64));
        qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
-       
+
        //Initialize Qt
        if(!lamexp_init_qt(argc, argv))
        {
@@ -87,6 +99,11 @@ int lamexp_main(int argc, char* argv[])
        //Check for expiration
        if(lamexp_version_demo())
        {
+               const QDate currentDate = lamexp_current_date_safe();
+               if(currentDate.addDays(1) < lamexp_version_date())
+               {
+                       qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData());
+               }
                qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
        }
 
@@ -115,20 +132,30 @@ int lamexp_main(int argc, char* argv[])
        //Kill application?
        for(int i = 0; i < argc; i++)
        {
-               if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
+               if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
                {
                        return 0;
                }
        }
        
+       //Self-test
+       if(LAMEXP_DEBUG)
+       {
+               InitializationThread::selfTest();
+       }
+
+       //Taskbar init
+       WinSevenTaskbar::init();
+
        //Create models
        FileListModel *fileListModel = new FileListModel();
-       AudioFileModel *metaInfo = new AudioFileModel();
+       AudioFileModel_MetaInfo *metaInfo = new AudioFileModel_MetaInfo();
        SettingsModel *settingsModel = new SettingsModel();
-       
+
        //Show splash screen
        InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
        SplashScreen::showSplash(poInitializationThread);
+       settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
        LAMEXP_DELETE(poInitializationThread);
 
        //Validate settings
@@ -138,19 +165,22 @@ int lamexp_main(int argc, char* argv[])
        MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
        
        //Main application loop
-       while(bAccepted && !bShutdown)
+       while(bAccepted && (iShutdown <= shutdownFlag_None))
        {
                //Show main window
                poMainWindow->show();
                iResult = QApplication::instance()->exec();
                bAccepted = poMainWindow->isAccepted();
 
+               //Sync settings
+               settingsModel->syncNow();
+
                //Show processing dialog
-               if(bAccepted && fileListModel->rowCount() > 0)
+               if(bAccepted && (fileListModel->rowCount() > 0))
                {
                        ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
                        processingDialog->exec();
-                       bShutdown = processingDialog->getShutdownFlag();
+                       iShutdown = processingDialog->getShutdownFlag();
                        LAMEXP_DELETE(processingDialog);
                }
        }
@@ -161,13 +191,16 @@ int lamexp_main(int argc, char* argv[])
        LAMEXP_DELETE(metaInfo);
        LAMEXP_DELETE(settingsModel);
 
+       //Taskbar un-init
+       WinSevenTaskbar::uninit();
+
        //Final clean-up
        qDebug("Shutting down, please wait...\n");
 
        //Shotdown computer
-       if(bShutdown)
+       if(iShutdown > shutdownFlag_None)
        {
-               if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12))
+               if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
                {
                        QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
                }
@@ -181,58 +214,58 @@ int lamexp_main(int argc, char* argv[])
 // Applicaton entry point
 ///////////////////////////////////////////////////////////////////////////////
 
-int main(int argc, char* argv[])
+static int _main(int argc, char* argv[])
 {
        if(LAMEXP_DEBUG)
        {
                int iResult = -1;
                qInstallMsgHandler(lamexp_message_handler);
-               LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
+               iResult = lamexp_main(argc, argv);
                lamexp_finalization();
                return iResult;
        }
        else
        {
+               int iResult = -1;
                try
                {
-                       int iResult = -1;
                        qInstallMsgHandler(lamexp_message_handler);
                        iResult = lamexp_main(argc, argv);
                        lamexp_finalization();
-                       return iResult;
                }
-               catch(char *error)
+               catch(const std::exception &error)
                {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION: %s\n", error);
-                       FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
-               }
-               catch(int error)
-               {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION: Error code 0x%X\n", error);
-                       FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
+                       lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
                }
                catch(...)
                {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n");
-                       FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
+                       lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
                }
+               return iResult;
        }
 }
 
-extern "C"
+int main(int argc, char* argv[])
 {
-       void __declspec(dllexport) __stdcall Test(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
+       if(LAMEXP_DEBUG)
+       {
+               int exit_code = -1;
+               LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
+               return exit_code;
+       }
+       else
        {
-               OutputDebugStringA(lpszCmdLine);
-               MessageBoxA(0, lpszCmdLine, "LameXP is here!", MB_ICONINFORMATION);
+               __try
+               {
+                       lamexp_init_error_handlers();
+                       return _main(argc, argv);
+               }
+               __except(1)
+               {
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
+                       lamexp_fatal_exit("Unhandeled structured exception error, application will exit!");
+               }
        }
 }