OSDN Git Service

Actually use selected encoder in progress dialog + forward output dir.
[lamexp/LameXP.git] / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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
34 //Qt includes
35 #include <QApplication>
36 #include <QMessageBox>
37 #include <QDate>
38 #include <QMutex>
39 #include <QDir>
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // Main function
43 ///////////////////////////////////////////////////////////////////////////////
44
45 int lamexp_main(int argc, char* argv[])
46 {
47         int iResult = -1;
48         bool bAccepted = true;
49         
50         //Init console
51         lamexp_init_console(argc, argv);
52         
53         //Print version info
54         qDebug("LameXP - Audio Encoder Front-End");
55         qDebug("Version %d.%02d %s, Build %d [%s], MSVC compiler v%02d.%02d", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build(), lamexp_version_date().toString(Qt::ISODate).toLatin1().constData(), _MSC_VER / 100, _MSC_VER % 100);
56         qDebug("Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>\n", max(lamexp_version_date().year(),QDate::currentDate().year()));
57         
58         //print license info
59         qDebug("This program is free software: you can redistribute it and/or modify");
60         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
61         qDebug("This program comes with ABSOLUTELY NO WARRANTY.\n");
62
63         //Print warning, if this is a "debug" build
64         LAMEXP_CHECK_DEBUG_BUILD;
65         
66         //Detect CPU capabilities
67         lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
68         qDebug("CPU brand string:  %s", cpuFeatures.brand);
69         qDebug("CPU signature:     Family: %d, Model: %d, Stepping: %d", cpuFeatures.family, cpuFeatures.model, cpuFeatures.stepping);
70         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));
71         qDebug("CPU no. of cores:  %d\n", cpuFeatures.count);
72         
73         //Initialize Qt
74         lamexp_init_qt(argc, argv);
75         
76         //Check for expiration
77         if(lamexp_version_demo())
78         {
79                 QDate expireDate = lamexp_version_date().addDays(14);
80                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(expireDate.toString(Qt::ISODate)).toLatin1().constData());
81                 if(QDate::currentDate() >= expireDate)
82                 {
83                         qWarning("Binary has expired !!!");
84                         QMessageBox::warning(NULL, "LameXP - Expired", QString("This demo (pre-release) version of LameXP has expired at %1.\nLameXP is free software and release versions won't expire.").arg(expireDate.toString()), "Exit Program");
85                         return 0;
86                 }
87         }
88
89         //Check for multiple instances of LameXP
90         if((iResult = lamexp_init_ipc()) != 0)
91         {
92                 qDebug("LameXP is already running, connecting to running instance...");
93                 if(iResult == 1)
94                 {
95                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
96                         messageProducerThread->start();
97                         if(!messageProducerThread->wait(30000))
98                         {
99                                 messageProducerThread->terminate();
100                                 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);
101                                 messageBox.exec();
102                                 messageProducerThread->wait();
103                                 LAMEXP_DELETE(messageProducerThread);
104                                 return -1;
105                         }
106                         LAMEXP_DELETE(messageProducerThread);
107                 }
108                 return 0;
109         }
110
111         //Kill application?
112         for(int i = 0; i < argc; i++)
113         {
114                 if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
115                 {
116                         return 0;
117                 }
118         }
119         
120         //Create models
121         FileListModel *fileListModel = new FileListModel();
122         AudioFileModel *metaInfo = new AudioFileModel();
123         SettingsModel *settingsModel = new SettingsModel();
124         settingsModel->validate();
125         
126         //Show splash screen
127         InitializationThread *poInitializationThread = new InitializationThread();
128         SplashScreen::showSplash(poInitializationThread);
129         LAMEXP_DELETE(poInitializationThread);
130
131         //Show main window
132         while(bAccepted)
133         {
134                 MainWindow *poMainWindow = new MainWindow(fileListModel, metaInfo, settingsModel);
135                 poMainWindow->show();
136                 iResult = QApplication::instance()->exec();
137                 bAccepted = poMainWindow->isAccepted();
138                 LAMEXP_DELETE(poMainWindow);
139
140                 //Show processing dialog
141                 if(bAccepted && fileListModel->rowCount() > 0)
142                 {
143                         ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, settingsModel);
144                         processingDialog->exec();
145                         LAMEXP_DELETE(processingDialog);
146                 }
147         }
148         
149         //Free models
150         LAMEXP_DELETE(fileListModel);
151         LAMEXP_DELETE(metaInfo);
152         LAMEXP_DELETE(settingsModel);
153         
154         //Final clean-up
155         qDebug("Shutting down, please wait...\n");
156
157         //Terminate
158         return iResult;
159 }
160
161 ///////////////////////////////////////////////////////////////////////////////
162 // Applicaton entry point
163 ///////////////////////////////////////////////////////////////////////////////
164
165 int main(int argc, char* argv[])
166 {
167         try
168         {
169                 int iResult;
170                 qInstallMsgHandler(lamexp_message_handler);
171                 LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
172                 lamexp_finalization();
173                 return iResult;
174         }
175         catch(char *error)
176         {
177                 fflush(stdout);
178                 fflush(stderr);
179                 fprintf(stderr, "\nEXCEPTION ERROR: %s\n", error);
180                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
181                 TerminateProcess(GetCurrentProcess(), -1);
182         }
183         catch(int error)
184         {
185                 fflush(stdout);
186                 fflush(stderr);
187                 fprintf(stderr, "\nEXCEPTION ERROR: Error code 0x%X\n", error);
188                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
189                 TerminateProcess(GetCurrentProcess(), -1);
190         }
191         catch(...)
192         {
193                 fflush(stdout);
194                 fflush(stderr);
195                 fprintf(stderr, "\nEXCEPTION ERROR !!!\n");
196                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
197                 TerminateProcess(GetCurrentProcess(), -1);
198         }
199 }