Updated Ukrainian translation.
[LameXP.git] / src / Main.cpp
blobb3accc63966fc597b4fdc7b27eb91ec1f774714d
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>
41 #include <QInputDialog>
43 ///////////////////////////////////////////////////////////////////////////////
44 // Main function
45 ///////////////////////////////////////////////////////////////////////////////
47 static int lamexp_main(int argc, char* argv[])
49 int iResult = -1;
50 int iShutdown = shutdownFlag_None;
51 bool bAccepted = true;
53 //Get CLI arguments
54 const QStringList &arguments = lamexp_arguments();
56 //Init console
57 lamexp_init_console(arguments);
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());
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");
69 //Print warning, if this is a "debug" build
70 if(LAMEXP_DEBUG)
72 qWarning("---------------------------------------------------------");
73 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
74 qWarning("---------------------------------------------------------\n");
77 //Enumerate CLI arguments
78 qDebug("Command-Line Arguments:");
79 for(int i = 0; i < arguments.count(); i++)
81 qDebug("argv[%d]=%s", i, arguments.at(i).toUtf8().constData());
83 qDebug("");
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);
93 //Initialize Qt
94 if(!lamexp_init_qt(argc, argv))
96 return -1;
99 //Check for expiration
100 if(lamexp_version_demo())
102 if(QDate::currentDate().addDays(1) < lamexp_version_date())
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());
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());
109 //Check for multiple instances of LameXP
110 if((iResult = lamexp_init_ipc()) != 0)
112 qDebug("LameXP is already running, connecting to running instance...");
113 if(iResult == 1)
115 MessageProducerThread *messageProducerThread = new MessageProducerThread();
116 messageProducerThread->start();
117 if(!messageProducerThread->wait(30000))
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;
126 LAMEXP_DELETE(messageProducerThread);
128 return 0;
131 //Kill application?
132 for(int i = 0; i < argc; i++)
134 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
136 return 0;
140 //Self-test
141 if(LAMEXP_DEBUG)
143 InitializationThread::selfTest();
146 //Taskbar init
147 WinSevenTaskbar::init();
149 //Create models
150 FileListModel *fileListModel = new FileListModel();
151 AudioFileModel *metaInfo = new AudioFileModel();
152 SettingsModel *settingsModel = new SettingsModel();
154 //Show splash screen
155 InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
156 SplashScreen::showSplash(poInitializationThread);
157 settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
158 LAMEXP_DELETE(poInitializationThread);
160 //Validate settings
161 settingsModel->validate();
163 //Create main window
164 MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
166 //Main application loop
167 while(bAccepted && (iShutdown <= shutdownFlag_None))
169 //Show main window
170 poMainWindow->show();
171 iResult = QApplication::instance()->exec();
172 bAccepted = poMainWindow->isAccepted();
174 //Show processing dialog
175 if(bAccepted && (fileListModel->rowCount() > 0))
177 ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
178 processingDialog->exec();
179 iShutdown = processingDialog->getShutdownFlag();
180 LAMEXP_DELETE(processingDialog);
184 //Free models
185 LAMEXP_DELETE(poMainWindow);
186 LAMEXP_DELETE(fileListModel);
187 LAMEXP_DELETE(metaInfo);
188 LAMEXP_DELETE(settingsModel);
190 //Taskbar un-init
191 WinSevenTaskbar::uninit();
193 //Final clean-up
194 qDebug("Shutting down, please wait...\n");
196 //Shotdown computer
197 if(iShutdown > shutdownFlag_None)
199 if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
201 QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
205 //Terminate
206 return iResult;
209 ///////////////////////////////////////////////////////////////////////////////
210 // Applicaton entry point
211 ///////////////////////////////////////////////////////////////////////////////
213 static int _main(int argc, char* argv[])
215 if(LAMEXP_DEBUG)
217 int iResult = -1;
218 qInstallMsgHandler(lamexp_message_handler);
219 iResult = lamexp_main(argc, argv);
220 lamexp_finalization();
221 return iResult;
223 else
225 int iResult = -1;
228 qInstallMsgHandler(lamexp_message_handler);
229 iResult = lamexp_main(argc, argv);
230 lamexp_finalization();
232 catch(char *error)
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);
240 catch(int error)
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);
248 catch(...)
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);
256 return iResult;
260 int main(int argc, char* argv[])
262 if(LAMEXP_DEBUG)
264 int exit_code = -1;
265 LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
266 return exit_code;
268 else
270 __try
272 SetUnhandledExceptionFilter(lamexp_exception_handler);
273 _set_invalid_parameter_handler(lamexp_invalid_param_handler);
274 return _main(argc, argv);
276 __except(1)
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);