OSDN Git Service

Moved all IPC functions into the MUtilities libraries.
[lamexp/LameXP.git] / src / Main.cpp
index 50da3f6..d7ed39c 100644 (file)
@@ -38,6 +38,9 @@
 #include <MUtils/OSSupport.h>
 #include <MUtils/Version.h>
 #include <MUtils/CPUFeatures.h>
+#include <MUtils/Terminal.h>
+#include <MUtils/Startup.h>
+#include <MUtils/IPCChannel.h>
 
 //Qt includes
 #include <QApplication>
 // Main function
 ///////////////////////////////////////////////////////////////////////////////
 
-static 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;
 
-       //Get CLI arguments
-       const QStringList &arguments = lamexp_arguments();
-
-       //Init console
-       lamexp_init_console(arguments);
-
        //Print version info
        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(MUtils::Version::build_date().year(), MUtils::OS::current_date().year()));
-       qDebug("Built on %s at %s with %s for Win-%s.\n", MUTILS_UTF8(MUtils::Version::build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::build_time().toString(Qt::ISODate)), MUtils::Version::compiler_version(), MUtils::Version::compiler_arch());
+       qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(MUtils::Version::app_build_date().year(), MUtils::OS::current_date().year()));
+       qDebug("Built on %s at %s with %s for Win-%s.\n", MUTILS_UTF8(MUtils::Version::app_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::app_build_time().toString(Qt::ISODate)), MUtils::Version::compiler_version(), MUtils::Version::compiler_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("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
 
+       //Print library version
+       qDebug("This application is powerd by MUtils library v%u.%02u (%s, %s).\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate)));
+
        //Print warning, if this is a "debug" build
        if(MUTILS_DEBUG)
        {
@@ -80,16 +80,19 @@ static int lamexp_main(int argc, char* argv[])
                qWarning("---------------------------------------------------------\n");
        }
        
+       //Get CLI arguments
+       const QStringList &arguments = MUtils::OS::arguments();
+
        //Enumerate CLI arguments
        qDebug("Command-Line Arguments:");
        for(int i = 0; i < arguments.count(); i++)
        {
                qDebug("argv[%d]=%s", i, MUTILS_UTF8(arguments.at(i)));
        }
-       qDebug("");
+       qDebug(" ");
 
        //Detect CPU capabilities
-       const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(lamexp_arguments());
+       const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(MUtils::OS::arguments());
        qDebug("   CPU vendor id  :  %s (Intel=%s)", cpuFeatures.vendor, MUTILS_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);
@@ -97,29 +100,38 @@ static int lamexp_main(int argc, char* argv[])
        qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
 
        //Initialize Qt
-       if(!lamexp_init_qt(argc, argv))
+       if(!MUtils::Startup::init_qt(argc, argv, QLatin1String("LameXP - Audio Encoder Front-End")))
        {
-               return -1;
+               lamexp_finalization();
+               return EXIT_FAILURE;
        }
 
+       //Initialize application
+       qApp->setWindowIcon(lamexp_app_icon());
+       qApp->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build())); 
+
+       //Add the default translations
+       lamexp_translation_init();
+
        //Check for expiration
        if(lamexp_version_demo())
        {
                const QDate currentDate = MUtils::OS::current_date();
-               if(currentDate.addDays(1) < MUtils::Version::build_date())
+               if(currentDate.addDays(1) < MUtils::Version::app_build_date())
                {
-                       qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), MUtils::Version::build_date().toString(Qt::ISODate).toLatin1().constData());
+                       qFatal("System's date (%s) is before LameXP build date (%s). Huh?", currentDate.toString(Qt::ISODate).toLatin1().constData(), MUtils::Version::app_build_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());
        }
 
-       //Check for multiple instances of LameXP
-       if((iResult = lamexp_init_ipc()) != 0)
+       //Initialize IPC
+       MUtils::IPCChannel *ipcChannel = new MUtils::IPCChannel("LameXP", "MultiInstanceHandling");
+       if((iResult = ipcChannel->initialize()) != MUtils::IPC_RET_SUCCESS_MASTER)
        {
-               qDebug("LameXP is already running, connecting to running instance...");
-               if(iResult == 1)
+               if(iResult == MUtils::IPC_RET_SUCCESS_SLAVE)
                {
-                       MessageProducerThread *messageProducerThread = new MessageProducerThread();
+                       qDebug("LameXP is already running, connecting to running instance...");
+                       MessageProducerThread *messageProducerThread = new MessageProducerThread(ipcChannel);
                        messageProducerThread->start();
                        if(!messageProducerThread->wait(30000))
                        {
@@ -128,11 +140,19 @@ static int lamexp_main(int argc, char* argv[])
                                messageBox.exec();
                                messageProducerThread->wait();
                                MUTILS_DELETE(messageProducerThread);
-                               return -1;
+                               MUTILS_DELETE(ipcChannel);
+                               lamexp_finalization();
+                               return EXIT_FAILURE;
                        }
                        MUTILS_DELETE(messageProducerThread);
                }
-               return 0;
+               else
+               {
+                       qFatal("The IPC initialization has failed!");
+               }
+               MUTILS_DELETE(ipcChannel);
+               lamexp_finalization();
+               return EXIT_SUCCESS;
        }
 
        //Kill application?
@@ -140,7 +160,9 @@ static int lamexp_main(int argc, char* argv[])
        {
                if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
                {
-                       return 0;
+                       MUTILS_DELETE(ipcChannel);
+                       lamexp_finalization();
+                       return EXIT_SUCCESS;
                }
        }
        
@@ -168,14 +190,14 @@ static int lamexp_main(int argc, char* argv[])
        settingsModel->validate();
 
        //Create main window
-       MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
+       MainWindow *poMainWindow = new MainWindow(ipcChannel, fileListModel, metaInfo, settingsModel);
        
        //Main application loop
        while(bAccepted && (iShutdown <= shutdownFlag_None))
        {
                //Show main window
                poMainWindow->show();
-               iResult = QApplication::instance()->exec();
+               iResult = qApp->exec();
                bAccepted = poMainWindow->isAccepted();
 
                //Sync settings
@@ -199,6 +221,7 @@ static int lamexp_main(int argc, char* argv[])
 
        //Taskbar un-init
        WinSevenTaskbar::uninit();
+       MUTILS_DELETE(ipcChannel);
 
        //Final clean-up
        qDebug("Shutting down, please wait...\n");
@@ -206,13 +229,14 @@ static int lamexp_main(int argc, char* argv[])
        //Shotdown computer
        if(iShutdown > shutdownFlag_None)
        {
-               if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
+               if(!MUtils::OS::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);
                }
        }
 
        //Terminate
+       lamexp_finalization();
        return iResult;
 }
 
@@ -220,58 +244,7 @@ static int lamexp_main(int argc, char* argv[])
 // Applicaton entry point
 ///////////////////////////////////////////////////////////////////////////////
 
-static int _main(int argc, char* argv[])
-{
-       if(MUTILS_DEBUG)
-       {
-               int iResult = -1;
-               qInstallMsgHandler(lamexp_message_handler);
-               iResult = lamexp_main(argc, argv);
-               lamexp_finalization();
-               return iResult;
-       }
-       else
-       {
-               int iResult = -1;
-               try
-               {
-                       qInstallMsgHandler(lamexp_message_handler);
-                       iResult = lamexp_main(argc, argv);
-                       lamexp_finalization();
-               }
-               catch(const std::exception &error)
-               {
-                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
-                       MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
-               }
-               catch(...)
-               {
-                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
-                       MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
-               }
-               return iResult;
-       }
-}
-
 int main(int argc, char* argv[])
 {
-       if(MUTILS_DEBUG)
-       {
-               int exit_code = -1;
-               LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
-               return exit_code;
-       }
-       else
-       {
-               __try
-               {
-                       lamexp_init_error_handlers();
-                       return _main(argc, argv);
-               }
-               __except(1)
-               {
-                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
-                       MUtils::OS::fatal_exit(L"Unhandeled structured exception error, application will exit!");
-               }
-       }
+       return MUtils::Startup::startup(argc, argv, lamexp_main, lamexp_version_demo());
 }