main.cpp 4.11 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25
 */
26 27


Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35 36 37 38
#include <QtGui>
#include <QTextCodec>

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

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

39 40
/* Uncomment this for memory leaks detection */
/*
41 42 43 44 45 46 47
#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
48
*/
49

50
void ub_message_output(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
51 52
    // We must temporarily remove the handler to avoid the infinite recursion of
    // ub_message_output -> qt_message_output -> ub_message_output -> qt_message_output ...
53
    QtMessageHandler previousHandler = qInstallMessageHandler(0);
Claudio Valerio's avatar
Claudio Valerio committed
54 55

#if defined(QT_NO_DEBUG)
56 57 58
    // Suppress qDebug output in release builds
    if (type != QtDebugMsg)
    {
59
        qt_message_output(type, context, msg);
60
    }
Claudio Valerio's avatar
Claudio Valerio committed
61 62

#else
63
    // Default output in debug builds
64
    qt_message_output(type, context, msg);
Claudio Valerio's avatar
Claudio Valerio committed
65 66
#endif

67
    if (UBApplication::app() && UBApplication::app()->isVerbose()) {
Claudio Valerio's avatar
Claudio Valerio committed
68
        QString logFileNamePath = UBSettings::userDataDirectory() + "/log/"+ qApp->applicationName() + ".log";
69
        QFile logFile(logFileNamePath);
Claudio Valerio's avatar
Claudio Valerio committed
70

71 72
        if (logFile.exists() && logFile.size() > 10000000)
            logFile.remove();
Claudio Valerio's avatar
Claudio Valerio committed
73

74 75 76 77 78 79 80
        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
81

82
    qInstallMessageHandler(previousHandler);
Claudio Valerio's avatar
Claudio Valerio committed
83 84
}

85
int main(int argc, char *argv[])
86
{
87

88 89
    // Uncomment next section to have memory leaks information
    // tracing in VC++ debug mode under Windows
90
/*
91 92 93
#if defined(_MSC_VER) && defined(_DEBUG)
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
94
*/
95

96 97 98 99
    // QT_NO_GLIB=1 is set by default on Linux, and prevents media playback
    if (qEnvironmentVariableIsSet("QT_NO_GLIB"))
        qunsetenv("QT_NO_GLIB");

100
    Q_INIT_RESOURCE(OpenBoard);
Claudio Valerio's avatar
Claudio Valerio committed
101

102
    qInstallMessageHandler(ub_message_output);
Claudio Valerio's avatar
Claudio Valerio committed
103

104
    UBApplication app("OpenBoard", argc, argv);
105 106 107

    QStringList args = app.arguments();

108
    QString dumpPath = UBSettings::userDataDirectory() + "/log";
109 110 111
    QDir logDir(dumpPath);
    if (!logDir.exists())
        logDir.mkdir(dumpPath);
Claudio Valerio's avatar
Claudio Valerio committed
112

113
    QString fileToOpen;
Claudio Valerio's avatar
Claudio Valerio committed
114

115 116 117 118
    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
119

120
        QFile f(args[1]);
Claudio Valerio's avatar
Claudio Valerio committed
121

122 123
        if (f.exists()) {
            fileToOpen += args[1];
Claudio Valerio's avatar
Claudio Valerio committed
124

125 126 127 128 129 130
            if (app.sendMessage(UBSettings::appPingMessage, 20000)) {
                app.sendMessage(fileToOpen, 1000000);
                return 0;
            }
        }
    }
Claudio Valerio's avatar
Claudio Valerio committed
131

132
    qDebug() << "file name argument" << fileToOpen;
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

}