OSDN Git Service

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