OSDN Git Service

Refactored the Main() function into several sub-functions + some improvements of...
[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 #include <MUtils/IPCChannel.h>
44
45 //Qt includes
46 #include <QApplication>
47 #include <QMessageBox>
48 #include <QDate>
49 #include <QMutex>
50 #include <QDir>
51
52 ///////////////////////////////////////////////////////////////////////////////
53 // Helper functions
54 ///////////////////////////////////////////////////////////////////////////////
55
56 static void lamexp_print_logo(void)
57 {
58         //Print version info
59         qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
60         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()));
61         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());
62         
63         //print license info
64         qDebug("This program is free software: you can redistribute it and/or modify");
65         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
66         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
67
68         //Print library version
69         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)));
70         
71         //Print warning, if this is a "debug" build
72         if(MUTILS_DEBUG)
73         {
74                 qWarning("---------------------------------------------------------");
75                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
76                 qWarning("---------------------------------------------------------\n");
77         }
78 }
79
80 static int lamexp_initialize_ipc(MUtils::IPCChannel *const ipcChannel)
81 {
82         int iResult = 0;
83
84         if((iResult = ipcChannel->initialize()) != MUtils::IPCChannel::RET_SUCCESS_MASTER)
85         {
86                 if(iResult == MUtils::IPCChannel::RET_SUCCESS_SLAVE)
87                 {
88                         qDebug("LameXP is already running, connecting to running instance...");
89                         QScopedPointer<MessageProducerThread> messageProducerThread(new MessageProducerThread(ipcChannel));
90                         messageProducerThread->start();
91                         if(!messageProducerThread->wait(30000))
92                         {
93                                 qWarning("MessageProducer thread has encountered timeout -> going to kill!");
94                                 messageProducerThread->terminate();
95                                 messageProducerThread->wait();
96                                 MUtils::OS::system_message_err(L"LameXP", L"LameXP is already running, but the running instance doesn't respond!");
97                                 return -1;
98                         }
99                         return 0;
100                 }
101                 else
102                 {
103                         qFatal("The IPC initialization has failed!");
104                         return -1;
105                 }
106         }
107
108         return 1;
109 }
110
111 static void lamexp_show_splash(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, SettingsModel *const settingsModel)
112 {
113         QScopedPointer<InitializationThread> poInitializationThread(new InitializationThread(cpuFeatures));
114         SplashScreen::showSplash(poInitializationThread.data());
115         settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
116 }
117
118 static int lamexp_main_loop_helper(MUtils::IPCChannel *const ipcChannel, FileListModel *const fileListModel, AudioFileModel_MetaInfo *const metaInfo, SettingsModel *const settingsModel, int &iShutdown)
119 {
120         int iResult = -1;
121         bool bAccepted = true;
122
123         //Create main window
124         QScopedPointer<MainWindow> poMainWindow(new MainWindow(ipcChannel, fileListModel, metaInfo, settingsModel));
125
126         //Main application loop
127         while(bAccepted && (iShutdown <= SHUTDOWN_FLAG_NONE))
128         {
129                 //Show main window
130                 poMainWindow->show();
131                 iResult = qApp->exec();
132                 bAccepted = poMainWindow->isAccepted();
133
134                 //Sync settings
135                 settingsModel->syncNow();
136
137                 //Show processing dialog
138                 if(bAccepted && (fileListModel->rowCount() > 0))
139                 {
140                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
141                         processingDialog->exec();
142                         iShutdown = processingDialog->getShutdownFlag();
143                         MUTILS_DELETE(processingDialog);
144                 }
145         }
146
147         return iResult;
148 }
149
150 static int lamexp_main_loop(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtils::IPCChannel *const ipcChannel, int &iShutdown)
151 {
152         //Create models
153         QScopedPointer<FileListModel>           fileListModel(new FileListModel()          );
154         QScopedPointer<AudioFileModel_MetaInfo> metaInfo     (new AudioFileModel_MetaInfo());
155         QScopedPointer<SettingsModel>           settingsModel(new SettingsModel()          );
156
157         //Show splash screen
158         lamexp_show_splash(cpuFeatures, settingsModel.data());
159
160         //Validate settings
161         settingsModel->validate();
162
163         //Main processing loop
164         return lamexp_main_loop_helper(ipcChannel, fileListModel.data(), metaInfo.data(), settingsModel.data(), iShutdown);
165 }
166
167 ///////////////////////////////////////////////////////////////////////////////
168 // Main function
169 ///////////////////////////////////////////////////////////////////////////////
170
171 static int lamexp_main(int &argc, char **argv)
172 {
173         int iResult = -1;
174         int iShutdown = SHUTDOWN_FLAG_NONE;
175         
176         //Print logo
177         lamexp_print_logo();
178
179         //Get CLI arguments
180         const QStringList &arguments = MUtils::OS::arguments();
181
182         //Enumerate CLI arguments
183         qDebug("Command-Line Arguments:");
184         for(int i = 0; i < arguments.count(); i++)
185         {
186                 qDebug("argv[%d]=%s", i, MUTILS_UTF8(arguments.at(i)));
187         }
188         qDebug(" ");
189
190         //Detect CPU capabilities
191         const MUtils::CPUFetaures::cpu_info_t cpuFeatures = MUtils::CPUFetaures::detect(MUtils::OS::arguments());
192         qDebug("   CPU vendor id  :  %s (Intel=%s)", cpuFeatures.vendor, MUTILS_BOOL2STR(cpuFeatures.intel));
193         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
194         qDebug("   CPU signature  :  Family=%d Model=%d Stepping=%d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
195         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));
196         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
197
198         //Initialize Qt
199         if(!MUtils::Startup::init_qt(argc, argv, QLatin1String("LameXP - Audio Encoder Front-End")))
200         {
201                 lamexp_finalization();
202                 return EXIT_FAILURE;
203         }
204
205         //Initialize application
206         qApp->setWindowIcon(lamexp_app_icon());
207         qApp->setApplicationVersion(QString().sprintf("%d.%02d.%04d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build())); 
208
209         //Add the default translations
210         lamexp_translation_init();
211
212         //Check for expiration
213         if(lamexp_version_demo())
214         {
215                 const QDate currentDate = MUtils::OS::current_date();
216                 if(currentDate.addDays(1) < MUtils::Version::app_build_date())
217                 {
218                         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());
219                 }
220                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
221         }
222
223         //Initialize IPC
224         QScopedPointer<MUtils::IPCChannel> ipcChannel(new MUtils::IPCChannel("lamexp-v4", lamexp_version_build(), "instance"));
225         if((iResult = lamexp_initialize_ipc(ipcChannel.data())) < 1)
226         {
227                 return (iResult == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
228         }
229
230         //Kill application?
231         for(int i = 0; i < argc; i++)
232         {
233                 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
234                 {
235                         lamexp_finalization();
236                         return EXIT_SUCCESS;
237                 }
238         }
239         
240         //Self-test
241         if(MUTILS_DEBUG)
242         {
243                 InitializationThread::selfTest();
244         }
245
246         //Taskbar init
247         WinSevenTaskbar::init();
248
249         //Main application loop
250         iResult = lamexp_main_loop(cpuFeatures, ipcChannel.data(), iShutdown);
251
252         //Taskbar un-init
253         WinSevenTaskbar::uninit();
254
255         //Final clean-up
256         qDebug("Shutting down, please wait...\n");
257
258         //Shotdown computer
259         if(iShutdown > SHUTDOWN_FLAG_NONE)
260         {
261                 if(!MUtils::OS::shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == SHUTDOWN_FLAG_HIBERNATE)))
262                 {
263                         QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
264                 }
265         }
266
267         //Terminate
268         lamexp_finalization();
269         return iResult;
270 }
271
272 ///////////////////////////////////////////////////////////////////////////////
273 // Applicaton entry point
274 ///////////////////////////////////////////////////////////////////////////////
275
276 int main(int argc, char* argv[])
277 {
278         return MUtils::Startup::startup(argc, argv, lamexp_main, lamexp_version_demo());
279 }