OSDN Git Service

Some improvements and simplifications to error handling functions.
[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 //Qt includes
37 #include <QApplication>
38 #include <QMessageBox>
39 #include <QDate>
40 #include <QMutex>
41 #include <QDir>
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(), lamexp_current_date_safe().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, QUTF8(arguments.at(i)));
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_BOOL2STR(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_BOOL2STR(cpuFeatures.mmx), LAMEXP_BOOL2STR(cpuFeatures.sse), LAMEXP_BOOL2STR(cpuFeatures.sse2), LAMEXP_BOOL2STR(cpuFeatures.sse3), LAMEXP_BOOL2STR(cpuFeatures.ssse3), LAMEXP_BOOL2STR(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                 const QDate currentDate = lamexp_current_date_safe();
103                 if(currentDate.addDays(1) < lamexp_version_date())
104                 {
105                         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());
106                 }
107                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
108         }
109
110         //Check for multiple instances of LameXP
111         if((iResult = lamexp_init_ipc()) != 0)
112         {
113                 qDebug("LameXP is already running, connecting to running instance...");
114                 if(iResult == 1)
115                 {
116                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
117                         messageProducerThread->start();
118                         if(!messageProducerThread->wait(30000))
119                         {
120                                 messageProducerThread->terminate();
121                                 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);
122                                 messageBox.exec();
123                                 messageProducerThread->wait();
124                                 LAMEXP_DELETE(messageProducerThread);
125                                 return -1;
126                         }
127                         LAMEXP_DELETE(messageProducerThread);
128                 }
129                 return 0;
130         }
131
132         //Kill application?
133         for(int i = 0; i < argc; i++)
134         {
135                 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
136                 {
137                         return 0;
138                 }
139         }
140         
141         //Self-test
142         if(LAMEXP_DEBUG)
143         {
144                 InitializationThread::selfTest();
145         }
146
147         //Taskbar init
148         WinSevenTaskbar::init();
149
150         //Create models
151         FileListModel *fileListModel = new FileListModel();
152         AudioFileModel_MetaInfo *metaInfo = new AudioFileModel_MetaInfo();
153         SettingsModel *settingsModel = new SettingsModel();
154
155         //Show splash screen
156         InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
157         SplashScreen::showSplash(poInitializationThread);
158         settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
159         LAMEXP_DELETE(poInitializationThread);
160
161         //Validate settings
162         settingsModel->validate();
163
164         //Create main window
165         MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
166         
167         //Main application loop
168         while(bAccepted && (iShutdown <= shutdownFlag_None))
169         {
170                 //Show main window
171                 poMainWindow->show();
172                 iResult = QApplication::instance()->exec();
173                 bAccepted = poMainWindow->isAccepted();
174
175                 //Sync settings
176                 settingsModel->syncNow();
177
178                 //Show processing dialog
179                 if(bAccepted && (fileListModel->rowCount() > 0))
180                 {
181                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
182                         processingDialog->exec();
183                         iShutdown = processingDialog->getShutdownFlag();
184                         LAMEXP_DELETE(processingDialog);
185                 }
186         }
187         
188         //Free models
189         LAMEXP_DELETE(poMainWindow);
190         LAMEXP_DELETE(fileListModel);
191         LAMEXP_DELETE(metaInfo);
192         LAMEXP_DELETE(settingsModel);
193
194         //Taskbar un-init
195         WinSevenTaskbar::uninit();
196
197         //Final clean-up
198         qDebug("Shutting down, please wait...\n");
199
200         //Shotdown computer
201         if(iShutdown > shutdownFlag_None)
202         {
203                 if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
204                 {
205                         QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
206                 }
207         }
208
209         //Terminate
210         return iResult;
211 }
212
213 ///////////////////////////////////////////////////////////////////////////////
214 // Applicaton entry point
215 ///////////////////////////////////////////////////////////////////////////////
216
217 static int _main(int argc, char* argv[])
218 {
219         if(LAMEXP_DEBUG)
220         {
221                 int iResult = -1;
222                 qInstallMsgHandler(lamexp_message_handler);
223                 iResult = lamexp_main(argc, argv);
224                 lamexp_finalization();
225                 return iResult;
226         }
227         else
228         {
229                 int iResult = -1;
230                 try
231                 {
232                         qInstallMsgHandler(lamexp_message_handler);
233                         iResult = lamexp_main(argc, argv);
234                         lamexp_finalization();
235                 }
236                 catch(const std::exception &error)
237                 {
238                         PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
239                         lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
240                 }
241                 catch(...)
242                 {
243                         PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
244                         lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
245                 }
246                 return iResult;
247         }
248 }
249
250 int main(int argc, char* argv[])
251 {
252         if(LAMEXP_DEBUG)
253         {
254                 int exit_code = -1;
255                 LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
256                 return exit_code;
257         }
258         else
259         {
260                 __try
261                 {
262                         lamexp_init_error_handlers();
263                         return _main(argc, argv);
264                 }
265                 __except(1)
266                 {
267                         PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
268                         lamexp_fatal_exit("Unhandeled structured exception error, application will exit!");
269                 }
270         }
271 }