Updated Ukrainian translation.
[LameXP.git] / src / Main.cpp
blobdc9e65ea5cf46e4aa18b3fb6b4d295d0363b3da0
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
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"
35 //Qt includes
36 #include <QApplication>
37 #include <QMessageBox>
38 #include <QDate>
39 #include <QMutex>
40 #include <QDir>
42 ///////////////////////////////////////////////////////////////////////////////
43 // Main function
44 ///////////////////////////////////////////////////////////////////////////////
46 static int lamexp_main(int argc, char* argv[])
48 int iResult = -1;
49 int iShutdown = shutdownFlag_None;
50 bool bAccepted = true;
52 //Get CLI arguments
53 const QStringList &arguments = lamexp_arguments();
55 //Init console
56 lamexp_init_console(arguments);
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(lamexp_version_date().year(),QDate::currentDate().year()));
61 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 //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");
68 //Print warning, if this is a "debug" build
69 if(LAMEXP_DEBUG)
71 qWarning("---------------------------------------------------------");
72 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
73 qWarning("---------------------------------------------------------\n");
76 //Enumerate CLI arguments
77 qDebug("Command-Line Arguments:");
78 for(int i = 0; i < arguments.count(); i++)
80 qDebug("argv[%d]=%s", i, arguments.at(i).toUtf8().constData());
82 qDebug("");
84 //Detect CPU capabilities
85 lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features(arguments);
86 qDebug(" CPU vendor id : %s (Intel: %s)", cpuFeatures.vendor, LAMEXP_BOOL2STR(cpuFeatures.intel));
87 qDebug("CPU brand string : %s", cpuFeatures.brand);
88 qDebug(" CPU signature : Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
89 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));
90 qDebug(" Number of CPU's : %d\n", cpuFeatures.count);
92 //Initialize Qt
93 if(!lamexp_init_qt(argc, argv))
95 return -1;
98 //Check for expiration
99 if(lamexp_version_demo())
101 if(QDate::currentDate().addDays(1) < lamexp_version_date())
103 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 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 //Check for multiple instances of LameXP
109 if((iResult = lamexp_init_ipc()) != 0)
111 qDebug("LameXP is already running, connecting to running instance...");
112 if(iResult == 1)
114 MessageProducerThread *messageProducerThread = new MessageProducerThread();
115 messageProducerThread->start();
116 if(!messageProducerThread->wait(30000))
118 messageProducerThread->terminate();
119 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);
120 messageBox.exec();
121 messageProducerThread->wait();
122 LAMEXP_DELETE(messageProducerThread);
123 return -1;
125 LAMEXP_DELETE(messageProducerThread);
127 return 0;
130 //Kill application?
131 for(int i = 0; i < argc; i++)
133 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
135 return 0;
139 //Self-test
140 if(LAMEXP_DEBUG)
142 InitializationThread::selfTest();
145 //Taskbar init
146 WinSevenTaskbar::init();
148 //Create models
149 FileListModel *fileListModel = new FileListModel();
150 AudioFileModel *metaInfo = new AudioFileModel();
151 SettingsModel *settingsModel = new SettingsModel();
153 //Show splash screen
154 InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
155 SplashScreen::showSplash(poInitializationThread);
156 settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
157 LAMEXP_DELETE(poInitializationThread);
159 //Validate settings
160 settingsModel->validate();
162 //Create main window
163 MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
165 //Main application loop
166 while(bAccepted && (iShutdown <= shutdownFlag_None))
168 //Show main window
169 poMainWindow->show();
170 iResult = QApplication::instance()->exec();
171 bAccepted = poMainWindow->isAccepted();
173 //Show processing dialog
174 if(bAccepted && (fileListModel->rowCount() > 0))
176 ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
177 processingDialog->exec();
178 iShutdown = processingDialog->getShutdownFlag();
179 LAMEXP_DELETE(processingDialog);
183 //Free models
184 LAMEXP_DELETE(poMainWindow);
185 LAMEXP_DELETE(fileListModel);
186 LAMEXP_DELETE(metaInfo);
187 LAMEXP_DELETE(settingsModel);
189 //Taskbar un-init
190 WinSevenTaskbar::uninit();
192 //Final clean-up
193 qDebug("Shutting down, please wait...\n");
195 //Shotdown computer
196 if(iShutdown > shutdownFlag_None)
198 if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
200 QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
204 //Terminate
205 return iResult;
208 ///////////////////////////////////////////////////////////////////////////////
209 // Applicaton entry point
210 ///////////////////////////////////////////////////////////////////////////////
212 static int _main(int argc, char* argv[])
214 if(LAMEXP_DEBUG)
216 int iResult = -1;
217 qInstallMsgHandler(lamexp_message_handler);
218 iResult = lamexp_main(argc, argv);
219 lamexp_finalization();
220 return iResult;
222 else
224 int iResult = -1;
227 qInstallMsgHandler(lamexp_message_handler);
228 iResult = lamexp_main(argc, argv);
229 lamexp_finalization();
231 catch(char *error)
233 fflush(stdout);
234 fflush(stderr);
235 fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
236 lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
238 catch(int error)
240 fflush(stdout);
241 fflush(stderr);
242 fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
243 lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
245 catch(...)
247 fflush(stdout);
248 fflush(stderr);
249 fprintf(stderr, "\nGURU MEDITATION !!!\n");
250 lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
252 return iResult;
256 int main(int argc, char* argv[])
258 if(LAMEXP_DEBUG)
260 int exit_code = -1;
261 LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
262 return exit_code;
264 else
266 __try
268 SetUnhandledExceptionFilter(lamexp_exception_handler);
269 _set_invalid_parameter_handler(lamexp_invalid_param_handler);
270 return _main(argc, argv);
272 __except(1)
274 fflush(stdout);
275 fflush(stderr);
276 fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
277 lamexp_fatal_exit(L"Unhandeled structured exception error, application will exit!");