Major redesign of the AudioFileModel class: Split data into separate AudioFileModel_M...
[LameXP.git] / src / Main.cpp
blobf7cf174acfc2a0a573f26b7dff832132f12179c8
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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 //Windows includes
43 #define NOMINMAX
44 #define WIN32_LEAN_AND_MEAN
45 #include <Windows.h>
47 //Forward declaration
48 LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo);
50 ///////////////////////////////////////////////////////////////////////////////
51 // Main function
52 ///////////////////////////////////////////////////////////////////////////////
54 static int lamexp_main(int argc, char* argv[])
56 int iResult = -1;
57 int iShutdown = shutdownFlag_None;
58 bool bAccepted = true;
60 //Increase "main" thread priority
61 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
63 //Get CLI arguments
64 const QStringList &arguments = lamexp_arguments();
66 //Init console
67 lamexp_init_console(arguments);
69 //Print version info
70 qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
71 qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(lamexp_version_date().year(), lamexp_current_date_safe().year()));
72 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());
74 //print license info
75 qDebug("This program is free software: you can redistribute it and/or modify");
76 qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
77 qDebug("Note that this program is distributed with ABSOLUTELY NO WARRANTY.\n");
79 //Print warning, if this is a "debug" build
80 if(LAMEXP_DEBUG)
82 qWarning("---------------------------------------------------------");
83 qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
84 qWarning("---------------------------------------------------------\n");
87 //Enumerate CLI arguments
88 qDebug("Command-Line Arguments:");
89 for(int i = 0; i < arguments.count(); i++)
91 qDebug("argv[%d]=%s", i, arguments.at(i).toUtf8().constData());
93 qDebug("");
95 //Detect CPU capabilities
96 lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features(arguments);
97 qDebug(" CPU vendor id : %s (Intel: %s)", cpuFeatures.vendor, LAMEXP_BOOL2STR(cpuFeatures.intel));
98 qDebug("CPU brand string : %s", cpuFeatures.brand);
99 qDebug(" CPU signature : Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
100 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));
101 qDebug(" Number of CPU's : %d\n", cpuFeatures.count);
103 //Initialize Qt
104 if(!lamexp_init_qt(argc, argv))
106 return -1;
109 //Check for expiration
110 if(lamexp_version_demo())
112 const QDate currentDate = lamexp_current_date_safe();
113 if(currentDate.addDays(1) < lamexp_version_date())
115 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());
117 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(lamexp_version_expires().toString(Qt::ISODate)).toLatin1().constData());
120 //Check for multiple instances of LameXP
121 if((iResult = lamexp_init_ipc()) != 0)
123 qDebug("LameXP is already running, connecting to running instance...");
124 if(iResult == 1)
126 MessageProducerThread *messageProducerThread = new MessageProducerThread();
127 messageProducerThread->start();
128 if(!messageProducerThread->wait(30000))
130 messageProducerThread->terminate();
131 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);
132 messageBox.exec();
133 messageProducerThread->wait();
134 LAMEXP_DELETE(messageProducerThread);
135 return -1;
137 LAMEXP_DELETE(messageProducerThread);
139 return 0;
142 //Kill application?
143 for(int i = 0; i < argc; i++)
145 if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
147 return 0;
151 //Self-test
152 if(LAMEXP_DEBUG)
154 InitializationThread::selfTest();
157 //Taskbar init
158 WinSevenTaskbar::init();
160 //Create models
161 FileListModel *fileListModel = new FileListModel();
162 AudioFileModel *metaInfo = new AudioFileModel();
163 SettingsModel *settingsModel = new SettingsModel();
165 //Show splash screen
166 InitializationThread *poInitializationThread = new InitializationThread(&cpuFeatures);
167 SplashScreen::showSplash(poInitializationThread);
168 settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
169 LAMEXP_DELETE(poInitializationThread);
171 //Validate settings
172 settingsModel->validate();
174 //Create main window
175 MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
177 //Main application loop
178 while(bAccepted && (iShutdown <= shutdownFlag_None))
180 //Show main window
181 poMainWindow->show();
182 iResult = QApplication::instance()->exec();
183 bAccepted = poMainWindow->isAccepted();
185 //Sync settings
186 settingsModel->syncNow();
188 //Show processing dialog
189 if(bAccepted && (fileListModel->rowCount() > 0))
191 ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
192 processingDialog->exec();
193 iShutdown = processingDialog->getShutdownFlag();
194 LAMEXP_DELETE(processingDialog);
198 //Free models
199 LAMEXP_DELETE(poMainWindow);
200 LAMEXP_DELETE(fileListModel);
201 LAMEXP_DELETE(metaInfo);
202 LAMEXP_DELETE(settingsModel);
204 //Taskbar un-init
205 WinSevenTaskbar::uninit();
207 //Final clean-up
208 qDebug("Shutting down, please wait...\n");
210 //Shotdown computer
211 if(iShutdown > shutdownFlag_None)
213 if(!lamexp_shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate)))
215 QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
219 //Terminate
220 return iResult;
223 ///////////////////////////////////////////////////////////////////////////////
224 // Applicaton entry point
225 ///////////////////////////////////////////////////////////////////////////////
227 static int _main(int argc, char* argv[])
229 if(LAMEXP_DEBUG)
231 int iResult = -1;
232 qInstallMsgHandler(lamexp_message_handler);
233 iResult = lamexp_main(argc, argv);
234 lamexp_finalization();
235 return iResult;
237 else
239 int iResult = -1;
242 qInstallMsgHandler(lamexp_message_handler);
243 iResult = lamexp_main(argc, argv);
244 lamexp_finalization();
246 catch(char *error)
248 fflush(stdout);
249 fflush(stderr);
250 fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
251 lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
253 catch(int error)
255 fflush(stdout);
256 fflush(stderr);
257 fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
258 lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
260 catch(...)
262 fflush(stdout);
263 fflush(stderr);
264 fprintf(stderr, "\nGURU MEDITATION !!!\n");
265 lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
267 return iResult;
271 int main(int argc, char* argv[])
273 if(LAMEXP_DEBUG)
275 int exit_code = -1;
276 LAMEXP_MEMORY_CHECK(_main, exit_code, argc, argv);
277 return exit_code;
279 else
281 __try
283 SetUnhandledExceptionFilter(lamexp_exception_handler);
284 _set_invalid_parameter_handler(lamexp_invalid_param_handler);
285 return _main(argc, argv);
287 __except(1)
289 fflush(stdout);
290 fflush(stderr);
291 fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
292 lamexp_fatal_exit(L"Unhandeled structured exception error, application will exit!");