main.cpp 4.26 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Claudio Valerio's avatar
Claudio Valerio committed
15 16 17 18 19 20 21 22 23
#include <QtGui>
#include <QTextCodec>

#include "frameworks/UBPlatformUtils.h"
#include "frameworks/UBFileSystemUtils.h"

#include "UBApplication.h"
#include "UBSettings.h"

24 25
/* Uncomment this for memory leaks detection */
/*
26 27 28 29 30 31 32
#if defined(WIN32) && defined(_DEBUG)
     #define _CRTDBG_MAP_ALLOC
     #include <stdlib.h>
     #include <crtdbg.h>
     #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
     #define new DEBUG_NEW
#endif
33
*/
34

Claudio Valerio's avatar
Claudio Valerio committed
35
void ub_message_output(QtMsgType type, const char *msg) {
36 37 38
    // We must temporarily remove the handler to avoid the infinite recursion of
    // ub_message_output -> qt_message_output -> ub_message_output -> qt_message_output ...
    QtMsgHandler previousHandler = qInstallMsgHandler(0);
Claudio Valerio's avatar
Claudio Valerio committed
39 40

#if defined(QT_NO_DEBUG)
41 42 43 44 45
    // Suppress qDebug output in release builds
    if (type != QtDebugMsg)
    {
        qt_message_output(type, msg);
    }
Claudio Valerio's avatar
Claudio Valerio committed
46 47

#else
48 49
    // Default output in debug builds
    qt_message_output(type, msg);
Claudio Valerio's avatar
Claudio Valerio committed
50 51
#endif

52
    if (UBApplication::app() && UBApplication::app()->isVerbose()) {
53
        QString logFileNamePath = UBSettings::userDataDirectory() + "/log/uniboard.log";
54
        QFile logFile(logFileNamePath);
Claudio Valerio's avatar
Claudio Valerio committed
55

56 57
        if (logFile.exists() && logFile.size() > 10000000)
            logFile.remove();
Claudio Valerio's avatar
Claudio Valerio committed
58

59 60 61 62 63 64 65
        if (logFile.open(QIODevice::Append | QIODevice::Text)) {
            QTextStream out(&logFile);
            out << QDateTime::currentDateTime().toString(Qt::ISODate)
                << "      " << msg << "\n";
            logFile.close();
        }
    }
Claudio Valerio's avatar
Claudio Valerio committed
66

67
    qInstallMsgHandler(previousHandler);
Claudio Valerio's avatar
Claudio Valerio committed
68 69
}

70 71
int main(int argc, char *argv[]) 
{
72

73 74 75
    // Uncomment next section to have memory leaks information
    // tracing in VC++ debug mode under Windows
    /*
76 77 78
#if defined(_MSC_VER) && defined(_DEBUG)
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
79
*/
80

81
    Q_INIT_RESOURCE(sankore);
Claudio Valerio's avatar
Claudio Valerio committed
82

83
    qInstallMsgHandler(ub_message_output);
Claudio Valerio's avatar
Claudio Valerio committed
84 85

#if defined(Q_WS_X11)
86 87
    qDebug() << "Setting GraphicsSystem to raster";
    QApplication::setGraphicsSystem("raster");
Claudio Valerio's avatar
Claudio Valerio committed
88 89
#endif

90 91 92 93 94 95 96
    UBApplication app("Sankore", argc, argv);

    //BUGFIX:
    //when importing a sankore file that contains a non standard character
    //the codecForLocale or the codecForCString is used to convert the file path
    //into a const char*. This is why in french windows setup the codec name shouldn't be
    //set to UTF-8. For example, setting UTF-8, will convert "Hati" into "Ha-ti.
97

98
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
99 100
    //QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
Claudio Valerio's avatar
Claudio Valerio committed
101

102 103
    QStringList args = app.arguments();

104
    QString dumpPath = UBSettings::userDataDirectory() + "/log";
105 106 107
    QDir logDir(dumpPath);
    if (!logDir.exists())
        logDir.mkdir(dumpPath);
Claudio Valerio's avatar
Claudio Valerio committed
108

109
    QString fileToOpen;
Claudio Valerio's avatar
Claudio Valerio committed
110

111 112 113 114
    if (args.size() > 1) {
        // On Windows/Linux first argument is the file that has been double clicked.
        // On Mac OSX we use FileOpen QEvent to manage opening file in current instance. So we will never
        // have file to open as a parameter on OSX.
Claudio Valerio's avatar
Claudio Valerio committed
115

116
        QFile f(args[1]);
Claudio Valerio's avatar
Claudio Valerio committed
117

118 119
        if (f.exists()) {
            fileToOpen += args[1];
Claudio Valerio's avatar
Claudio Valerio committed
120

121 122 123 124 125 126
            if (app.sendMessage(UBSettings::appPingMessage, 20000)) {
                app.sendMessage(fileToOpen, 1000000);
                return 0;
            }
        }
    }
Claudio Valerio's avatar
Claudio Valerio committed
127

128
    app.initialize(false);
Claudio Valerio's avatar
Claudio Valerio committed
129

130 131
    QObject::connect(&app, SIGNAL(messageReceived(const QString&)), &app,
                     SLOT(handleOpenMessage(const QString&)));
Claudio Valerio's avatar
Claudio Valerio committed
132

133
    int result = app.exec(fileToOpen);
Claudio Valerio's avatar
Claudio Valerio committed
134

135
    app.cleanup();
Claudio Valerio's avatar
Claudio Valerio committed
136

137
    qDebug() << "application is quitting";
138

Claudio Valerio's avatar
Claudio Valerio committed
139

140

141
    return result;
Claudio Valerio's avatar
Claudio Valerio committed
142 143

}