1 /***************************************************************************
2 								  main.cpp  -  main application
3 									  -------------------
4 	 begin                : sob maj 7 2005
5 	 copyright            : (C) 2005 Michal Rudolf <mrudolf@kdewebdev.org>
6  ***************************************************************************/
7 
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 
17 #include <QApplication>
18 #include <QLocale>
19 #include <QTranslator>
20 #include "mainwindow.h"
21 #include "settings.h"
22 #include "style.h"
23 #include "logstream.h"
24 
25 // Necessary includes and defines for memory leak detection:
26 #ifdef _MSC_VER
27 #define _CRTDBG_MAP_ALLOC
28 #include <stdlib.h>
29 #include <crtdbg.h>
30 #endif // _MSC_VER
31 
32 #ifdef _MSC_VER
33 #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
34 #define new DEBUG_NEW
35 #endif // _MSC_VER
36 
37 
38 #if defined(_MSC_VER)
39 
40 // Code to display the memory leak report
41 // We use a custom report hook to filter out Qt's own memory leaks
42 // Credit to Andreas Schmidts - http://www.schmidt-web-berlin.de/winfig/blog/?p=154
43 
44 _CRT_REPORT_HOOK prevHook;
45 
customReportHook(int,char * message,int *)46 int customReportHook(int /* reportType */, char* message, int* /* returnValue */) {
47   // This function is called several times for each memory leak.
48   // Each time a part of the error message is supplied.
49   // This holds number of subsequent detail messages after
50   // a leak was reported
51   const int numFollowupDebugMsgParts = 2;
52   static bool ignoreMessage = false;
53   static int debugMsgPartsCount = 0;
54 
55   // check if the memory leak reporting starts
56   if ((strncmp(message,"Detected memory leaks!\n", 10) == 0)
57     || ignoreMessage)
58   {
59     // check if the memory leak reporting ends
60     if (strncmp(message,"Object dump complete.\n", 10) == 0)
61     {
62       _CrtSetReportHook(prevHook);
63       ignoreMessage = false;
64     } else
65       ignoreMessage = true;
66 
67     // something from our own code?
68     if(strstr(message, ".cpp") == NULL)
69     {
70       if(debugMsgPartsCount++ < numFollowupDebugMsgParts)
71         // give it back to _CrtDbgReport() to be printed to the console
72         return FALSE;
73       else
74         return TRUE;  // ignore it
75     } else
76     {
77       debugMsgPartsCount = 0;
78       // give it back to _CrtDbgReport() to be printed to the console
79       return FALSE;
80     }
81   } else
82     // give it back to _CrtDbgReport() to be printed to the console
83     return FALSE;
84 }
85 
86 #endif
87 
main(int argc,char ** argv)88 int main(int argc, char** argv)
89 {
90 #if defined(_MSC_VER)
91 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
92 prevHook = _CrtSetReportHook(customReportHook);
93 // _CrtSetBreakAlloc(157); // Use this line to break at the nth memory allocation
94 #endif
95 
96     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);     // HiDPI support
97     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);  // Windows Surface Book
98 
99     QApplication app(argc, argv);
100 
101     QDir dir(QApplication::applicationDirPath());
102 
103 #ifdef QT_DEBUG
104     QApplication::addLibraryPath(dir.absolutePath());
105 #else
106     QStringList l;
107     l<<dir.absolutePath();
108     QApplication::setLibraryPaths(l);
109 #endif
110 
111 #ifdef Q_OS_WIN
112     dir.cd("platforms");
113     QApplication::addLibraryPath(dir.absolutePath());
114 #endif
115 
116 #ifdef Q_OS_MAC
117     dir.cdUp();
118     dir.cd("plugins");
119     QApplication::addLibraryPath(dir.absolutePath());
120     dir.cdUp();
121     dir.cd("Frameworks");
122     QApplication::addLibraryPath(dir.absolutePath());
123 #endif
124 
125     app.setWindowIcon(QIcon(":/images/chessx.png"));
126 
127     app.setApplicationName("chessx");
128     app.setOrganizationName("chessx");
129     app.setOrganizationDomain("chessx.sourceforge.net");
130 
131     QString portableIni = Settings::portableIniPath();
132     if (QFile::exists(portableIni))
133     {
134         AppSettings = new Settings(portableIni);
135     }
136     if (!AppSettings)
137     {
138         AppSettings = new Settings;
139     }
140 
141     bool showIcons = AppSettings->getValue("/MainWindow/ShowMenuIcons").toBool();
142     if (!showIcons) app.setAttribute(Qt::AA_DontShowIconsInMenus);  // Icons are *no longer shown* in menus
143 
144     // style our application with custom dark style
145     Style* chessxStyle = new Style;
146     app.setStyle(chessxStyle);
147     QPalette palette = app.palette();
148     chessxStyle->modifyPalette(palette);
149     app.setPalette(palette);
150 
151 #ifdef Q_OS_MAC
152     signal(SIGPIPE, SIG_IGN);
153 #endif
154 
155     QString shortSystemLang = QString("chessx_%1.qm").arg(QLocale::system().name().left(2));
156     QString fullSystemLang = QString("chessx_%1.qm").arg(QLocale::system().name().left(5));
157 
158     // Language may have two forms: "pt_BR" or "pl"
159     QString lang = QString("chessx_%1.qm").arg(AppSettings->getValue("/General/language").toString());
160 
161     QDir().mkpath(AppSettings->dataPath() + "/lang/");
162 
163     QTranslator translator;
164     if (lang != "chessx_en.qm")
165     {
166         QString filePath = AppSettings->dataPath() + QDir::separator() + "lang" + QDir::separator();
167 
168         if(translator.load(QString(":/i18n/") + lang) ||
169                 translator.load(filePath + lang) ||
170                 translator.load(QString(":/i18n/") + fullSystemLang) ||
171                 translator.load(filePath + fullSystemLang) ||
172                 translator.load(QString(":/i18n/") + shortSystemLang) ||
173                 translator.load(filePath + shortSystemLang))
174         {
175             app.installTranslator(&translator);
176         }
177     }
178 
179     MainWindow* mainWindow = new MainWindow;
180 
181     mainWindow->show();
182 
183     // Destroy main window and close application
184     app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
185 
186     LogStream logStream;
187     int result = app.exec();
188 
189     delete AppSettings;
190 
191 #if defined(_MSC_VER)
192 // Once the app has finished running and has been deleted,
193 // we run this command to view the memory leaks:
194 _CrtDumpMemoryLeaks();
195 #endif
196 
197 return result;
198 }
199 
200