File: main.cpp

package info (click to toggle)
dianara 1.3.6-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 4,896 kB
  • sloc: cpp: 22,181; xml: 34; makefile: 3
file content (316 lines) | stat: -rw-r--r-- 10,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 *   This file is part of Dianara
 *   Copyright 2012-2016  JanKusanagi JRR <jancoding@gmx.com>
 *
 *   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 2 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, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .
 */

#include <QApplication>
#include <QTranslator>
#include <QLibraryInfo> // Include needed in some cases
#include <iostream>

#include "mainwindow.h"





#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
void customMessageHandlerQt4(QtMsgType type, const char *msg)
{
    // do nothing
    Q_UNUSED(type)
    Q_UNUSED(msg)  // FIXME, memory leak?

    return;
}
#else
void customMessageHandlerQt5(QtMsgType type,
                             const QMessageLogContext &context,
                             const QString &msg)
{
    Q_UNUSED(type)
    Q_UNUSED(context)
    Q_UNUSED(msg)

    // Do nothing

    return;
}
#endif



int main(int argc, char *argv[])
{
    QApplication dianaraApp(argc, argv);
    dianaraApp.setApplicationName("Dianara");
    dianaraApp.setApplicationVersion("1.3.6");
    dianaraApp.setOrganizationName("JanCoding");
    dianaraApp.setOrganizationDomain("jancoding.wordpress.com");

    std::cout << QString("Dianara v%1 - JanKusanagi 2012-2016\n"
                         "https://jancoding.wordpress.com/dianara\n\n")
                        .arg(dianaraApp.applicationVersion()).toStdString();

    std::cout << QString("- Built with Qt v%1").arg(QT_VERSION_STR)
                                               .toStdString();

/*
 * REPRODUCIBLEBUILD is defined via .pro file when SOURCE_DATE_EPOCH is
 * defined in the build environment. This is used to avoid hardcoding
 * timestamps and this way make the builds reproducible.
 *
 */
#ifndef REPRODUCIBLEBUILD
    std::cout << QString(" on %1, %2")
                 .arg(__DATE__)
                 .arg(__TIME__).toStdString();
#endif
    std::cout << "\n";

    std::cout << QString("- Running with Qt v%1\n\n").arg(qVersion())
                                                     .toStdString();
    std::cout.flush();


    // To make the mswin version of Dianara distributable as a standalone package
#ifdef Q_OS_WIN
    dianaraApp.addLibraryPath("./plugins/");
#endif


    QStringList cmdLine = qApp->arguments();
    cmdLine.removeFirst(); // Remove the program executable name/path

    bool debugMode = false;
    bool nextParameterIsConfig = false;
    bool ignoreSslErrors = false;
    bool nohttps = false;

    // Parse command line parameters
    if (!cmdLine.isEmpty())
    {
        foreach (QString argument, cmdLine)
        {
            // Help
            if (argument.startsWith("--help", Qt::CaseInsensitive)
             || argument.startsWith("-h", Qt::CaseInsensitive))
            {
                std::cout << "\nHelp:\n";
                std::cout << "    " << argv[0] << " [options]\n\n";
                std::cout << "Options:\n";
                std::cout << "    -c  --config [name]       Use a different "
                             "configuration file and data folder\n";
                std::cout << "    -d  --debug               Show debug messages "
                             "in the terminal\n";
                std::cout << "        --linkcolor=[color]   Set color for "
                             "links, such as 'green' or '#00B500'\n"
                             "\n";
                std::cout << "        --nohttps             Use this if your "
                             "server does not support HTTPS\n";
                std::cout << "        --ignoresslerrors     Ignore SSL errors "
                             "(dangerous, use with care!)\n"
                             "\n";
                std::cout << "    -v  --version             Show version "
                             "information and exit\n";
                std::cout << "    -h  --help                Show this help and "
                             "exit\n";
                std::cout << "\n";

                return 0; // Exit to shell
            }


            // Version info
            if (argument.startsWith("--version", Qt::CaseInsensitive)
             || argument.startsWith("-v", Qt::CaseInsensitive))
            {
                // Version information already shown
                std::cout << "\n"
                             "You can get the latest stable version from "
                             "http://dianara.nongnu.org and\n"
                             "the latest development version from "
                             "https://gitlab.com/dianara/dianara-dev"
                             "\n\n";

                return 0;
            }


            // Use different config file, by setting a different applicationName
            if (argument.startsWith("--config", Qt::CaseInsensitive)
             || argument.startsWith("-c", Qt::CaseInsensitive))
            {
                nextParameterIsConfig = true;
                cmdLine.removeAll(argument);
            }
            else if (nextParameterIsConfig)
            {
                QString configName = "Dianara_" + argument.toLower();
                dianaraApp.setApplicationName(configName);
                nextParameterIsConfig = false;

                std::cout << "Using alternate config file: "
                          << configName.toStdString() << "\n";
            }

            // Debug mode
            if (argument.startsWith("--debug", Qt::CaseInsensitive)
             || argument.startsWith("-d", Qt::CaseInsensitive))
            {
                debugMode = true;
                cmdLine.removeAll(argument);

                std::cout << "Debug messages enabled\n";
            }


            // Option to set link color (useful in GTK environments)
            if (argument.startsWith("--linkcolor=", Qt::CaseInsensitive))
            {
                QString linkColorName = argument.split("=").last();
                QColor linkColor = QColor(linkColorName);
                if (linkColor.isValid())
                {
                    QPalette newPalette = dianaraApp.palette();
                    newPalette.setColor(QPalette::Link, linkColor);
                    dianaraApp.setPalette(newPalette);

                    std::cout << "Forcing link color to: "
                              << linkColorName.toStdString() << "\n";
                }
                else
                {
                    std::cout << "Invalid link color specified!\n";
                }
            }


            // Option to use non-https servers
            if (argument.startsWith("--nohttps", Qt::CaseInsensitive))
            {
                nohttps = true;
                cmdLine.removeAll(argument);

                std::cout << "*** Using No-HTTPS mode\n";
            }


            // Option to ignore SSL errors
            if (argument.startsWith("--ignoresslerrors", Qt::CaseInsensitive))
            {
                ignoreSslErrors = true;
                cmdLine.removeAll(argument);

                std::cout << "*************************************\n"
                             "*** WARNING: Ignoring SSL errors. ***\n"
                             "***          This is insecure!    ***\n"
                             "*************************************\n";
            }

        } // End foreach
    }


    // Register custom message handler, to hide debug messages unless specified
    if (!debugMode)
    {
    #ifdef Q_OS_WIN
        FreeConsole();
    #endif

        std::cout << "To see debug messages while running, use --debug\n";

    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
        qInstallMsgHandler(customMessageHandlerQt4);
    #else
        qInstallMessageHandler(customMessageHandlerQt5);
    #endif
    }


    // Load translation files
    // Get language from LANG environment variable or system's locale
    QString languageString = qgetenv("LANG");
    if (languageString.isEmpty())
    {
        languageString = QLocale::system().name();
    }

    QString languageFile;
    bool languageLoaded;


    QTranslator translatorQt;
    languageFile = QString("qt_%1").arg(languageString);
    std::cout << "\n"
              << "Using Qt translation "
              << QLibraryInfo::location(QLibraryInfo::TranslationsPath).toStdString()
              << "/" << languageFile.toStdString() << "... ";
    languageLoaded = translatorQt.load(languageFile,
                                       QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    if (languageLoaded)
    {
        std::cout << "OK";
        dianaraApp.installTranslator(&translatorQt);
    }
    else
    {
        std::cout << "Unavailable";
    }


    QTranslator translatorDianara;
    languageFile = QString(":/translations/dianara_%1").arg(languageString);
    std::cout << "\n"
              << "Using program translation "
              << languageFile.toStdString() << "... ";

    languageLoaded = translatorDianara.load(languageFile);
    if (languageLoaded)
    {
        std::cout << "OK";
        dianaraApp.installTranslator(&translatorDianara);
    }
    else
    {
        std::cout << "Unavailable";
    }


    std::cout << "\n\n";
    std::cout.flush();


    MainWindow dianaraWindow;

    if (ignoreSslErrors)
    {
        dianaraWindow.enableIgnoringSslErrors();
    }

    if (nohttps)
    {
        dianaraWindow.enableNoHttpsMode();
    }

    dianaraWindow.toggleMainWindow(true);  // show(), firstTime=true


    return dianaraApp.exec();
}