OSDN Git Service

Extinguished some remaining uses of argv[] or QApplication::arguments().
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 //LameXP includes
23 #include "Global.h"
24 #include "Dialog_SplashScreen.h"
25 #include "Dialog_MainWindow.h"
26 #include "Dialog_Processing.h"
27 #include "Thread_Initialization.h"
28 #include "Thread_MessageProducer.h"
29 #include "Model_Settings.h"
30 #include "Model_FileList.h"
31 #include "Model_AudioFile.h"
32 #include "Encoder_Abstract.h"
33 #include "WinSevenTaskbar.h"
34
35 //Qt includes
36 #include <QApplication>
37 #include <QMessageBox>
38 #include <QDate>
39 #include <QMutex>
40 #include <QDir>
41 #include <QInputDialog>
42
43 ///////////////////////////////////////////////////////////////////////////////
44 // Main function
45 ///////////////////////////////////////////////////////////////////////////////
46
47 static int lamexp_main(int argc, char* argv[])
48 {
49         int iResult = -1;
50         int iShutdown = shutdownFlag_None;
51         bool bAccepted = true;
52
53         //Get CLI arguments
54         const QStringList &arguments = lamexp_arguments();
55
56         //Init console
57         lamexp_init_console(arguments);
58
59         //Print version info
60         qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
61         qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(lamexp_version_date().year(),QDate::currentDate().year()));
62         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());
63         
64         //print license info
65         qDebug("This program is free software: you can redistribute it and/or modify");
66         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
67         qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
68
69         //Print warning, if this is a "debug" build
70         if(LAMEXP_DEBUG)
71         {
72                 qWarning("---------------------------------------------------------");
73                 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
74                 qWarning("---------------------------------------------------------\n"); 
75         }
76         
77         //Enumerate CLI arguments
78         qDebug("Command-Line Arguments:");
79         for(int i = 0; i < arguments.count(); i++)
80         {
81                 qDebug("argv[%d]=%s", i, arguments.at(i).toUtf8().constData());
82         }
83         qDebug("");
84
85         //Detect CPU capabilities
86         lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features(arguments);
87         qDebug("   CPU vendor id  :  %s (Intel: %s)", cpuFeatures.vendor, LAMEXP_BOOL(cpuFeatures.intel));
88         qDebug("CPU brand string  :  %s", cpuFeatures.brand);
89         qDebug("   CPU signature  :  Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
90         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));
91         qDebug(" Number of CPU's  :  %d\n", cpuFeatures.count);
92         
93         //Initialize Qt
94         if(!lamexp_init_qt(argc, argv))
95         {
96                 return -1;
97         }
98
99         //Check for expiration
100         if(lamexp_version_demo())
101         {
102                 if(QDate::currentDate().addDays(1) < lamexp_version_date())
103                 {
104                         qFatal("System's date (%s) is before LameXP build date (%s). Huh?", QDate::currentDate().toString(Qt::ISODate).toLatin1().constData(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData());
105                 }
106                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
107         }
108
109         //Check for multiple instances of LameXP
110         if((iResult = lamexp_init_ipc()) != 0)
111         {
112                 qDebug("LameXP is already running, connecting to running instance...");
113                 if(iResult == 1)
114                 {
115                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
116                         messageProducerThread->start();
117                         if(!messageProducerThread->wait(30000))
118                         {
119                                 messageProducerThread->terminate();
120                                 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);
121                                 messageBox.exec();
122                                 messageProducerThread->wait();
123                                 LAMEXP_DELETE(messageProducerThread);
124                                 return -1;
125                         }
126                         LAMEXP_DELETE(messageProducerThread);
127                 }
128                 return 0;
129         }
130
131         //Kill application?
132         for(int i = 0; i < argc; i++)
133         {
134                 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
135                 {
136                         return 0;
137                 }
138         }
139         
140         //Self-test
141         if(LAMEXP_DEBUG)
142         {
143                 InitializationThread::selfTest();
144         }
145
146         //Taskbar init
147         WinSevenTaskbar::init();
148
149         //Create models
150         FileListModel *fileListModel = new FileListModel();
151         AudioFileModel *metaInfo = new AudioFileModel();
152         SettingsModel *settingsModel = new SettingsModel();
153         
154         //Show splash screen
155         InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
156         SplashScreen::showSplash(poInitializationThread);
157         settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
158         LAMEXP_DELETE(poInitializationThread);
159
160         //Validate settings
161         settingsModel->validate();
162
163         //Create main window
164         MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
165         
166         //Main application loop
167         while(bAccepted && (iShutdown <= shutdownFlag_None))
168         {
169                 //Show main window
170                 poMainWindow->show();
171                 iResult = QApplication::instance()->exec();
172                 bAccepted = poMainWindow->isAccepted();
173
174                 //Show processing dialog
175                 if(bAccepted && (fileListModel->rowCount() > 0))
176                 {
177                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
178                         processingDialog->exec();
179                         iShutdown = processingDialog->getShutdownFlag();
180                         LAMEXP_DELETE(processingDialog);
181                 }
182         }
183         
184         //Free models
185         LAMEXP_DELETE(poMainWindow);
186         LAMEXP_DELETE(fileListModel);
187         LAMEXP_DELETE(metaInfo);
188         LAMEXP_DELETE(settingsModel);
189
190         //Taskbar un-init
191         WinSevenTaskbar::uninit();
192
193         //Final clean-up
194         qDebug("Shutting down, please wait...\n");
195
196         //Shotdown computer
197         if(iShutdown > shutdownFlag_None)
198         {
199                 if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
200                 {
201                         QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
202                 }
203         }
204
205         //Terminate
206         return iResult;
207 }
208
209 ///////////////////////////////////////////////////////////////////////////////
210 // Applicaton entry point
211 ///////////////////////////////////////////////////////////////////////////////
212
213 static int _main(int argc, char* argv[])
214 {
215         if(LAMEXP_DEBUG)
216         {
217                 int iResult = -1;
218                 qInstallMsgHandler(lamexp_message_handler);
219                 iResult = lamexp_main(argc, argv);
220                 lamexp_finalization();
221                 return iResult;
222         }
223         else
224         {
225                 int iResult = -1;
226                 try
227                 {
228                         qInstallMsgHandler(lamexp_message_handler);
229                         iResult = lamexp_main(argc, argv);
230                         lamexp_finalization();
231                 }
232                 catch(char *error)
233                 {
234                         fflush(stdout);
235                         fflush(stderr);
236                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
237                         FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
238                         TerminateProcess(GetCurrentProcess(), -1);
239                 }
240                 catch(int error)
241                 {
242                         fflush(stdout);
243                         fflush(stderr);
244                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
245                         FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
246                         TerminateProcess(GetCurrentProcess(), -1);
247                 }
248                 catch(...)
249                 {
250                         fflush(stdout);
251                         fflush(stderr);
252                         fprintf(stderr, "\nGURU MEDITATION !!!\n");
253                         FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
254                         TerminateProcess(GetCurrentProcess(), -1);
255                 }
256                 return iResult;
257         }
258 }
259
260 int main(int argc, char* argv[])
261 {
262         if(LAMEXP_DEBUG)
263         {
264                 int exit_code = -1;
265                 LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
266                 return exit_code;
267         }
268         else
269         {
270                 __try
271                 {
272                         SetUnhandledExceptionFilter(lamexp_exception_handler);
273                         _set_invalid_parameter_handler(lamexp_invalid_param_handler);
274                         return _main(argc, argv);
275                 }
276                 __except(1)
277                 {
278                         fflush(stdout);
279                         fflush(stderr);
280                         fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
281                         FatalAppExit(0, L"Unhandeled structured exception error, application will exit!");
282                         TerminateProcess(GetCurrentProcess(), -1);
283                 }
284         }
285 }