OSDN Git Service

90ab4eab52bce7dfe4b2fe8cdd4c369bfb1a0fbf
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 //LameXP includes
24 #include "Global.h"
25 #include "Dialog_SplashScreen.h"
26 #include "Dialog_MainWindow.h"
27 #include "Dialog_Processing.h"
28 #include "Thread_Initialization.h"
29 #include "Thread_MessageProducer.h"
30 #include "Model_Settings.h"
31 #include "Model_FileList.h"
32 #include "Model_AudioFile.h"
33 #include "Encoder_Abstract.h"
34 #include "WinSevenTaskbar.h"
35
36 //MUitls
37 #include <MUtils/Global.h>
38 #include <MUtils/OSSupport.h>
39 #include <MUtils/Version.h>
40 #include <MUtils/CPUFeatures.h>
41 #include <MUtils/Terminal.h>
42 #include <MUtils/Startup.h>
43
44 //Qt includes
45 #include <QApplication>
46 #include <QMessageBox>
47 #include <QDate>
48 #include <QMutex>
49 #include <QDir>
50
51 ///////////////////////////////////////////////////////////////////////////////
52 // Main function
53 ///////////////////////////////////////////////////////////////////////////////
54
55 static int lamexp_main(int &argc, char **argv)
56 {
57         int iResult = -1;
58         int iShutdown = shutdownFlag_None;
59         bool bAccepted = true;
60
61         //Print version info
62         qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
63         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()));
64         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());
65         
66         //print license info
67         qDebug("This program is free software: you can redistribute it and/or modify");
68         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
69         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
70
71         //Print library version
72         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)));
73
74         //Print warning, if this is a "debug" build
75         if(MUTILS_DEBUG)
76         {
77                 qWarning("---------------------------------------------------------");
78                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
79                 qWarning("---------------------------------------------------------\n");
80         }
81         
82         //Get CLI arguments
83         const QStringList &arguments = MUtils::OS::arguments();
84
85         //Enumerate CLI arguments
86         qDebug("Command-Line Arguments:");
87         for(int i = 0; i < arguments.count(); i++)
88         {
89                 qDebug("argv[%d]=%s", i, MUTILS_UTF8(arguments.at(i)));
90         }
91         qDebug(" ");
92
93         //Detect CPU capabilities
94         const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(MUtils::OS::arguments());
95         qDebug("   CPU vendor id  :  %s (Intel=%s)", cpuFeatures.vendor, MUTILS_BOOL2STR(cpuFeatures.intel));
96         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
97         qDebug("   CPU signature  :  Family=%d Model=%d Stepping=%d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
98         qDebug("CPU capabilities  :  MMX=%s SSE=%s SSE2=%s SSE3=%s SSSE3=%s SSE4=%s SSE4.2=%s x64=%s", MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_MMX), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE2), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSSE3), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE4), MUTILS_BOOL2STR(cpuFeatures.features & MUtils::CPUFetaures::FLAG_SSE42), MUTILS_BOOL2STR(cpuFeatures.x64));
99         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
100
101         //Initialize Qt
102         if(!MUtils::Startup::init_qt(argc, argv, QLatin1String("LameXP - Audio Encoder Front-End")))
103         {
104                 lamexp_finalization();
105                 return -1;
106         }
107
108         //Initialize application
109         qApp->setWindowIcon(lamexp_app_icon());
110         qApp->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build())); 
111
112         //Add the default translations
113         lamexp_translation_init();
114
115         //Check for expiration
116         if(lamexp_version_demo())
117         {
118                 const QDate currentDate = MUtils::OS::current_date();
119                 if(currentDate.addDays(1) < MUtils::Version::app_build_date())
120                 {
121                         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());
122                 }
123                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
124         }
125
126         //Check for multiple instances of LameXP
127         if((iResult = lamexp_init_ipc()) != 0)
128         {
129                 qDebug("LameXP is already running, connecting to running instance...");
130                 if(iResult == 1)
131                 {
132                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
133                         messageProducerThread->start();
134                         if(!messageProducerThread->wait(30000))
135                         {
136                                 messageProducerThread->terminate();
137                                 QMessageBox messageBox(QMessageBox::Critical, "LameXP", "LameXP is already running, but the running instance doesn't respond!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
138                                 messageBox.exec();
139                                 messageProducerThread->wait();
140                                 MUTILS_DELETE(messageProducerThread);
141                                 lamexp_finalization();
142                                 return -1;
143                         }
144                         MUTILS_DELETE(messageProducerThread);
145                 }
146                 lamexp_finalization();
147                 return 0;
148         }
149
150         //Kill application?
151         for(int i = 0; i < argc; i++)
152         {
153                 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
154                 {
155                         lamexp_finalization();
156                         return 0;
157                 }
158         }
159         
160         //Self-test
161         if(MUTILS_DEBUG)
162         {
163                 InitializationThread::selfTest();
164         }
165
166         //Taskbar init
167         WinSevenTaskbar::init();
168
169         //Create models
170         FileListModel *fileListModel = new FileListModel();
171         AudioFileModel_MetaInfo *metaInfo = new AudioFileModel_MetaInfo();
172         SettingsModel *settingsModel = new SettingsModel();
173
174         //Show splash screen
175         InitializationThread *poInitializationThread = new InitializationThread(cpuFeatures);
176         SplashScreen::showSplash(poInitializationThread);
177         settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
178         MUTILS_DELETE(poInitializationThread);
179
180         //Validate settings
181         settingsModel->validate();
182
183         //Create main window
184         MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
185         
186         //Main application loop
187         while(bAccepted && (iShutdown <= shutdownFlag_None))
188         {
189                 //Show main window
190                 poMainWindow->show();
191                 iResult = qApp->exec();
192                 bAccepted = poMainWindow->isAccepted();
193
194                 //Sync settings
195                 settingsModel->syncNow();
196
197                 //Show processing dialog
198                 if(bAccepted && (fileListModel->rowCount() > 0))
199                 {
200                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
201                         processingDialog->exec();
202                         iShutdown = processingDialog->getShutdownFlag();
203                         MUTILS_DELETE(processingDialog);
204                 }
205         }
206         
207         //Free models
208         MUTILS_DELETE(poMainWindow);
209         MUTILS_DELETE(fileListModel);
210         MUTILS_DELETE(metaInfo);
211         MUTILS_DELETE(settingsModel);
212
213         //Taskbar un-init
214         WinSevenTaskbar::uninit();
215
216         //Final clean-up
217         qDebug("Shutting down, please wait...\n");
218
219         //Shotdown computer
220         if(iShutdown > shutdownFlag_None)
221         {
222                 if(!MUtils::OS::shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
223                 {
224                         QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
225                 }
226         }
227
228         //Terminate
229         lamexp_finalization();
230         return iResult;
231 }
232
233 ///////////////////////////////////////////////////////////////////////////////
234 // Applicaton entry point
235 ///////////////////////////////////////////////////////////////////////////////
236
237 int main(int argc, char* argv[])
238 {
239         return MUtils::Startup::startup(argc, argv, lamexp_main);
240 }