OSDN Git Service

Implemented support for adding directories via Drag&Drop and CLI
[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 "Thread_Initialization.h"
27 #include "Thread_MessageProducer.h"
28 #include "Model_Settings.h"
29
30 //Qt includes
31 #include <QApplication>
32 #include <QMessageBox>
33 #include <QDate>
34
35 ///////////////////////////////////////////////////////////////////////////////
36 // Main function
37 ///////////////////////////////////////////////////////////////////////////////
38
39 int lamexp_main(int argc, char* argv[])
40 {
41         int iResult = -1;
42         
43         //Init console
44         lamexp_init_console(argc, argv);
45         
46         //Print version info
47         qDebug("LameXP - Audio Encoder Front-End");
48         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);
49         qDebug("Copyright (C) 2004-%04d LoRd_MuldeR <MuldeR2@GMX.de>\n", max(lamexp_version_date().year(),QDate::currentDate().year()));
50         
51         //print license info
52         qDebug("This program is free software: you can redistribute it and/or modify");
53         qDebug("it under the terms of the GNU General Public License <http://www.gnu.org/>.");
54         qDebug("This program comes with ABSOLUTELY NO WARRANTY.\n");
55
56         //Print warning, if this is a "debug" build
57         LAMEXP_CHECK_DEBUG_BUILD;
58         
59         //Initialize Qt
60         lamexp_init_qt(argc, argv);
61         
62         //Check for expiration
63         if(lamexp_version_demo())
64         {
65                 QDate expireDate = lamexp_version_date().addDays(14);
66                 qWarning(QString("Note: This demo (pre-release) version of LameXP will expire at %1.\n").arg(expireDate.toString(Qt::ISODate)).toLatin1().constData());
67                 if(QDate::currentDate() >= expireDate)
68                 {
69                         qWarning("Binary has expired !!!");
70                         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");
71                         return 0;
72                 }
73         }
74
75         //Check for multiple instances of LameXP
76         if((iResult = lamexp_init_ipc()) != 0)
77         {
78                 qDebug("LameXP is already running, connecting to running instance...");
79                 if(iResult == 1)
80                 {
81                         MessageProducerThread *messageProducerThread = new MessageProducerThread();
82                         messageProducerThread->start();
83                         if(!messageProducerThread->wait(30000))
84                         {
85                                 messageProducerThread->terminate();
86                                 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);
87                                 messageBox.exec();
88                                 messageProducerThread->wait();
89                                 LAMEXP_DELETE(messageProducerThread);
90                                 return -1;
91                         }
92                         LAMEXP_DELETE(messageProducerThread);
93                 }
94                 return 0;
95         }
96
97         //Kill application?
98         for(int i = 0; i < argc; i++)
99         {
100                 if(!_stricmp("--kill", argv[i]) || !_stricmp("--force-kill", argv[i]))
101                 {
102                         return 0;
103                 }
104         }
105         
106         //Show splash screen
107         InitializationThread *poInitializationThread = new InitializationThread();
108         SplashScreen::showSplash(poInitializationThread);
109         LAMEXP_DELETE(poInitializationThread);
110
111         //Show main window
112         MainWindow *poMainWindow = new MainWindow();
113         poMainWindow->show();
114         iResult = QApplication::instance()->exec();
115         LAMEXP_DELETE(poMainWindow);
116         
117         //Final clean-up
118         qDebug("Shutting down, please wait...\n");
119
120         //Terminate
121         return iResult;
122 }
123
124 ///////////////////////////////////////////////////////////////////////////////
125 // Message Handler
126 ///////////////////////////////////////////////////////////////////////////////
127
128 static void lamexp_message_handler(QtMsgType type, const char *msg)
129 {
130         static HANDLE hConsole = NULL;
131         
132         if(!hConsole)
133         {
134                 hConsole = CreateFile(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
135         }
136
137         CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
138         GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
139
140         switch(type)
141         {
142         case QtCriticalMsg:
143         case QtFatalMsg:
144                 fflush(stdout);
145                 fflush(stderr);
146                 SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
147                 fprintf(stderr, "\nCRITICAL ERROR !!!\n%s\n\n", msg);
148                 MessageBoxA(NULL, msg, "LameXP - CRITICAL ERROR", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
149                 break;
150         case QtWarningMsg:
151                 SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
152                 fprintf(stderr, "%s\n", msg);
153                 fflush(stderr);
154                 break;
155         default:
156                 SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
157                 fprintf(stderr, "%s\n", msg);
158                 fflush(stderr);
159                 break;
160         }
161
162         SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
163
164         if(type == QtCriticalMsg || type == QtFatalMsg)
165         {
166                 FatalAppExit(0, L"The application has encountered a critical error and will exit now!");
167                 TerminateProcess(GetCurrentProcess(), -1);
168         }
169  }
170
171
172 ///////////////////////////////////////////////////////////////////////////////
173 // Applicaton entry point
174 ///////////////////////////////////////////////////////////////////////////////
175
176 int main(int argc, char* argv[])
177 {
178         try
179         {
180                 int iResult;
181                 qInstallMsgHandler(lamexp_message_handler);
182                 LAMEXP_MEMORY_CHECK(iResult = lamexp_main(argc, argv));
183                 lamexp_finalization();
184                 return iResult;
185         }
186         catch(char *error)
187         {
188                 fflush(stdout);
189                 fflush(stderr);
190                 fprintf(stderr, "\nEXCEPTION ERROR: %s\n", error);
191                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
192                 TerminateProcess(GetCurrentProcess(), -1);
193         }
194         catch(int error)
195         {
196                 fflush(stdout);
197                 fflush(stderr);
198                 fprintf(stderr, "\nEXCEPTION ERROR: Error code 0x%X\n", error);
199                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
200                 TerminateProcess(GetCurrentProcess(), -1);
201         }
202         catch(...)
203         {
204                 fflush(stdout);
205                 fflush(stderr);
206                 fprintf(stderr, "\nEXCEPTION ERROR !!!\n");
207                 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
208                 TerminateProcess(GetCurrentProcess(), -1);
209         }
210 }