main.cpp 5.6 KB
Newer Older
1
/*
2
 *  Kaidan - A user-friendly XMPP client for every device!
3
 *
Linus Jahn's avatar
Linus Jahn committed
4 5
 *  Copyright (C) 2016-2018 Kaidan developers and contributors
 *  (see the LICENSE file for a full list of copyright authors)
6 7 8 9 10 11
 *
 *  Kaidan 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.
 *
Linus Jahn's avatar
Linus Jahn committed
12 13 14 15 16 17 18 19 20 21
 *  In addition, as a special exception, the author of Kaidan gives
 *  permission to link the code of its release with the OpenSSL
 *  project's "OpenSSL" library (or with modified versions of it that
 *  use the same license as the "OpenSSL" library), and distribute the
 *  linked executables. You must obey the GNU General Public License in
 *  all respects for all of the code used other than "OpenSSL". If you
 *  modify this file, you may extend this exception to your version of
 *  the file, but you are not obligated to do so.  If you do not wish to
 *  do so, delete this exception statement from your version.
 *
22 23 24 25 26 27
 *  Kaidan 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
Linus Jahn's avatar
Linus Jahn committed
28
 *  along with Kaidan.  If not, see <http://www.gnu.org/licenses/>.
29 30
 */

31
// Qt
32 33 34
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
Georg's avatar
Georg committed
35
#include <QGuiApplication>
36
#include <QLocale>
37
#include <qqml.h>
Georg's avatar
Georg committed
38
#include <QQmlApplicationEngine>
39
#include <QQmlContext>
40
#include <QTranslator>
41
#include <QLibraryInfo>
42
// Kaidan
43
#include "Kaidan.h"
44 45 46 47 48
#include "RosterModel.h"
#include "MessageModel.h"
#include "AvatarFileStorage.h"
#include "Globals.h"
#include "Enums.h"
Ilya Bizyaev's avatar
Ilya Bizyaev committed
49 50 51 52 53 54 55 56 57
#include "StatusBar.h"

#ifdef QMAKE_BUILD
#include "./3rdparty/kirigami/src/kirigamiplugin.h"
#endif

#ifdef Q_OS_ANDROID
#include <QtAndroid>
#endif
Georg's avatar
Georg committed
58

59
enum CommandLineParseResult {
60 61 62 63 64 65 66 67 68 69 70 71 72
	CommandLineOk,
	CommandLineError,
	CommandLineVersionRequested,
	CommandLineHelpRequested
};

CommandLineParseResult parseCommandLine(QCommandLineParser &parser, QString *errorMessage)
{
	// add all possible arguments
	QCommandLineOption helpOption = parser.addHelpOption();
	QCommandLineOption versionOption = parser.addVersionOption();

	// parse arguments
73
	if (!parser.parse(QGuiApplication::arguments())) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87
		*errorMessage = parser.errorText();
		return CommandLineError;
	}

	// check for special cases
	if (parser.isSet(versionOption))
		return CommandLineVersionRequested;

	if (parser.isSet(helpOption))
		return CommandLineHelpRequested;
	// if nothing special happened, return OK
	return CommandLineOk;
}

88 89
int main(int argc, char *argv[])
{
90 91 92 93
	//
	// App
	//

94
	// create a qt app
95
	QGuiApplication app(argc, argv);
96 97

	// name, display name, description
98 99 100
	QGuiApplication::setApplicationName(APPLICATION_NAME);
	QGuiApplication::setApplicationDisplayName(APPLICATION_DISPLAY_NAME);
	QGuiApplication::setApplicationVersion(VERSION_STRING);
Georg's avatar
Georg committed
101

102 103 104
	// attributes
	QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

105 106 107 108 109
	// register qMetaTypes
	qRegisterMetaType<RosterModel*>("RosterModel*");
	qRegisterMetaType<MessageModel*>("MessageModel*");
	qRegisterMetaType<AvatarFileStorage*>("AvatarFileStorage*");
	qRegisterMetaType<ContactMap>("ContactMap");
110
	qRegisterMetaType<Qt::ApplicationState>("Qt::ApplicationState");
111 112 113
	qmlRegisterUncreatableMetaObject(Enums::staticMetaObject, APPLICATION_ID,
	                                 1, 0, "Kaidan", "Access to enums & flags only");

114 115 116
	// Qt-Translator
	QTranslator qtTranslator;
	qtTranslator.load("qt_" + QLocale::system().name(),
117
	                  QLibraryInfo::location(QLibraryInfo::TranslationsPath));
118 119 120 121
	app.installTranslator(&qtTranslator);

	// Kaidan-Translator
	QTranslator kaidanTranslator;
122 123
	// load the systems locale or none from resources
	kaidanTranslator.load(QLocale::system().name(), ":/i18n");
124 125 126
	app.installTranslator(&kaidanTranslator);


127 128 129 130
	//
	// Kaidan back-end
	//

131
	Kaidan kaidan(&app);
132

133 134 135 136
	//
	// Command line arguments
	//

137 138 139
	// create parser and add a description
	QCommandLineParser parser;
	parser.setApplicationDescription(QString(APPLICATION_DISPLAY_NAME) +
140
	                                 " - " + QString(APPLICATION_DESCRIPTION));
141 142 143

	// parse the arguments
	QString commandLineErrorMessage;
144 145 146 147 148 149 150 151 152 153 154 155
	switch (parseCommandLine(parser, &commandLineErrorMessage)) {
	case CommandLineError:
		qWarning() << commandLineErrorMessage;
		return 1;
	case CommandLineVersionRequested:
		parser.showVersion();
		return 0;
	case CommandLineHelpRequested:
		parser.showHelp();
		return 0;
	case CommandLineOk:
		break;
156 157
	}

158

159 160 161
	//
	// QML-GUI
	//
162

163 164
	QQmlApplicationEngine engine;

165 166
#ifndef SAILFISH_OS
	// QtQuickControls2 Style
167 168 169 170
	if (qgetenv("QT_QUICK_CONTROLS_STYLE").isEmpty()) {
		qDebug() << "QT_QUICK_CONTROLS_STYLE not set, setting to Material";
		qputenv("QT_QUICK_CONTROLS_STYLE", "Material");
	}
171
#endif
172

173 174 175 176
#ifdef UBUNTU_TOUCH
       qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "true");
#endif

177
	// QML type bindings
Ilya Bizyaev's avatar
Ilya Bizyaev committed
178 179 180
#ifdef QMAKE_BUILD
	KirigamiPlugin::getInstance().registerTypes();
#endif
181 182 183
	qmlRegisterType<StatusBar>("StatusBar", 0, 1, "StatusBar");
	qmlRegisterUncreatableMetaObject(Enums::staticMetaObject, APPLICATION_ID,
		1, 0, "Enums", "Can't create object; only enums defined!");
Georg's avatar
Georg committed
184

185
	engine.rootContext()->setContextProperty("kaidan", &kaidan);
186
	engine.load(QUrl("qrc:/qml/main.qml"));
Ilya Bizyaev's avatar
Ilya Bizyaev committed
187 188
	if(engine.rootObjects().isEmpty())
		return -1;
Georg's avatar
Georg committed
189

Ilya Bizyaev's avatar
Ilya Bizyaev committed
190 191 192 193
#ifdef Q_OS_ANDROID
	//TODO: use QQmlApplicationEngine::objectCreated to ensure QML was actually loaded?
	QtAndroid::hideSplashScreen();
#endif
194

195
	// enter qt main loop
196
	return app.exec();
Georg's avatar
Georg committed
197
}