2020-09-01 14:31:31 -04:00
|
|
|
/*!
|
2020-08-03 13:27:42 -04:00
|
|
|
* Copyright (C) 2015-2020 by Savoir-faire Linux
|
|
|
|
* Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>
|
|
|
|
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
|
|
|
|
* Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
|
2020-08-28 12:04:45 -04:00
|
|
|
* Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
|
2020-08-03 13:27:42 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mainapplication.h"
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
#include "appsettingsmanager.h"
|
2020-08-03 13:27:42 -04:00
|
|
|
#include "globalinstances.h"
|
|
|
|
#include "globalsystemtray.h"
|
2020-09-01 14:31:31 -04:00
|
|
|
#include "qmlregister.h"
|
2020-08-03 13:27:42 -04:00
|
|
|
#include "qrimageprovider.h"
|
2020-09-01 14:31:31 -04:00
|
|
|
#include "pixbufmanipulator.h"
|
2020-08-03 13:27:42 -04:00
|
|
|
#include "tintedbuttonimageprovider.h"
|
|
|
|
|
2020-08-31 15:57:10 -04:00
|
|
|
#include <QAction>
|
2020-08-03 13:27:42 -04:00
|
|
|
#include <QFontDatabase>
|
2020-08-31 15:57:10 -04:00
|
|
|
#include <QMenu>
|
2020-08-03 13:27:42 -04:00
|
|
|
#include <QQmlContext>
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined _MSC_VER && !COMPILE_ONLY
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#endif
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
static void
|
|
|
|
consoleDebug()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
AllocConsole();
|
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
FILE* fpstdout = stdout;
|
|
|
|
freopen_s(&fpstdout, "CONOUT$", "w", stdout);
|
|
|
|
FILE* fpstderr = stderr;
|
|
|
|
freopen_s(&fpstderr, "CONOUT$", "w", stderr);
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
COORD coordInfo;
|
|
|
|
coordInfo.X = 130;
|
|
|
|
coordInfo.Y = 9000;
|
|
|
|
|
|
|
|
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
|
|
|
|
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
static void
|
|
|
|
vsConsoleDebug()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/*
|
|
|
|
* Print debug to output window if using VS.
|
|
|
|
*/
|
|
|
|
QObject::connect(&LRCInstance::behaviorController(),
|
|
|
|
&lrc::api::BehaviorController::debugMessageReceived,
|
2020-08-18 17:41:05 -04:00
|
|
|
[](const QString& message) {
|
2020-08-03 13:27:42 -04:00
|
|
|
OutputDebugStringA((message + "\n").toStdString().c_str());
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
static QString
|
|
|
|
getDebugFilePath()
|
|
|
|
{
|
|
|
|
QDir logPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
|
|
|
|
logPath.cdUp();
|
|
|
|
return QString(logPath.absolutePath() + "/jami/jami.log");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fileDebug(QFile* debugFile)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
QObject::connect(&LRCInstance::behaviorController(),
|
|
|
|
&lrc::api::BehaviorController::debugMessageReceived,
|
2020-08-18 17:41:05 -04:00
|
|
|
[debugFile](const QString& message) {
|
2020-08-03 13:27:42 -04:00
|
|
|
if (debugFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
|
|
|
|
auto msg = (message + "\n").toStdString().c_str();
|
|
|
|
debugFile->write(msg, qstrlen(msg));
|
|
|
|
debugFile->close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
MainApplication::MainApplication(int& argc, char** argv)
|
|
|
|
: QApplication(argc, argv)
|
|
|
|
, engine_(new QQmlApplicationEngine())
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-09-01 14:31:31 -04:00
|
|
|
QObject::connect(this, &QApplication::aboutToQuit, [this] { cleanup(); });
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
void
|
|
|
|
MainApplication::init()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-09-01 14:31:31 -04:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
if (!getenv("QT_QPA_PLATFORMTHEME"))
|
|
|
|
setenv("QT_QPA_PLATFORMTHEME", "gtk3", true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (auto string : QCoreApplication::arguments()) {
|
|
|
|
if (string == "-d" || string == "--debug") {
|
|
|
|
consoleDebug();
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
Utils::removeOldVersions();
|
|
|
|
loadTranslations();
|
|
|
|
setApplicationFont();
|
|
|
|
|
|
|
|
#if defined _MSC_VER && !COMPILE_ONLY
|
|
|
|
gnutls_global_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GlobalInstances::setPixmapManipulator(std::make_unique<PixbufManipulator>());
|
|
|
|
initLrc();
|
|
|
|
|
|
|
|
bool startMinimized {false};
|
|
|
|
parseArguments(startMinimized);
|
|
|
|
|
|
|
|
initSettings();
|
|
|
|
initSystray();
|
|
|
|
initQmlEngine();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainApplication::loadTranslations()
|
|
|
|
{
|
|
|
|
auto appDir = qApp->applicationDirPath() + "/";
|
|
|
|
const auto locale_name = QLocale::system().name();
|
|
|
|
const auto locale_lang = locale_name.split('_')[0];
|
|
|
|
|
2020-08-18 17:41:05 -04:00
|
|
|
QTranslator* qtTranslator_lang = new QTranslator(this);
|
|
|
|
QTranslator* qtTranslator_name = new QTranslator(this);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (locale_name != locale_lang) {
|
|
|
|
if (qtTranslator_lang->load("qt_" + locale_lang,
|
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
|
|
|
|
installTranslator(qtTranslator_lang);
|
|
|
|
}
|
|
|
|
qtTranslator_name->load("qt_" + locale_name,
|
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
installTranslator(qtTranslator_name);
|
|
|
|
|
2020-08-18 17:41:05 -04:00
|
|
|
QTranslator* lrcTranslator_lang = new QTranslator(this);
|
|
|
|
QTranslator* lrcTranslator_name = new QTranslator(this);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (locale_name != locale_lang) {
|
|
|
|
if (lrcTranslator_lang->load(appDir + "share/libringclient/translations/lrc_" + locale_lang))
|
|
|
|
installTranslator(lrcTranslator_lang);
|
|
|
|
}
|
|
|
|
if (lrcTranslator_name->load(appDir + "share/libringclient/translations/lrc_" + locale_name))
|
|
|
|
installTranslator(lrcTranslator_name);
|
|
|
|
|
2020-08-18 17:41:05 -04:00
|
|
|
QTranslator* mainTranslator_lang = new QTranslator(this);
|
|
|
|
QTranslator* mainTranslator_name = new QTranslator(this);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (locale_name != locale_lang) {
|
|
|
|
if (mainTranslator_lang->load(appDir + "share/ring/translations/ring_client_windows_"
|
|
|
|
+ locale_lang))
|
|
|
|
installTranslator(mainTranslator_lang);
|
|
|
|
}
|
|
|
|
if (mainTranslator_name->load(appDir + "share/ring/translations/ring_client_windows_"
|
|
|
|
+ locale_name))
|
|
|
|
installTranslator(mainTranslator_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainApplication::initLrc()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Init mainwindow and finish splash when mainwindow shows up.
|
|
|
|
*/
|
|
|
|
std::atomic_bool isMigrating(false);
|
|
|
|
LRCInstance::init(
|
|
|
|
[this, &isMigrating] {
|
|
|
|
/*
|
|
|
|
* TODO: splash screen for account migration.
|
|
|
|
*/
|
|
|
|
isMigrating = true;
|
|
|
|
while (isMigrating) {
|
|
|
|
this->processEvents();
|
|
|
|
}
|
|
|
|
},
|
2020-09-01 14:31:31 -04:00
|
|
|
[&isMigrating] {
|
2020-08-03 13:27:42 -04:00
|
|
|
while (!isMigrating) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
}
|
|
|
|
isMigrating = false;
|
|
|
|
});
|
|
|
|
LRCInstance::subscribeToDebugReceived();
|
|
|
|
LRCInstance::getAPI().holdConferences = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-01 14:31:31 -04:00
|
|
|
MainApplication::parseArguments(bool& startMinimized)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
QString uri = "";
|
|
|
|
|
|
|
|
for (auto string : QCoreApplication::arguments()) {
|
|
|
|
if (string.startsWith("jami:")) {
|
|
|
|
uri = string;
|
|
|
|
} else {
|
|
|
|
if (string == "-m" || string == "--minimized") {
|
|
|
|
startMinimized = true;
|
|
|
|
}
|
2020-09-01 14:31:31 -04:00
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
debugFile_.reset(new QFile(getDebugFilePath()));
|
2020-08-03 13:27:42 -04:00
|
|
|
auto dbgFile = string == "-f" || string == "--file";
|
|
|
|
auto dbgConsole = string == "-c" || string == "--vsconsole";
|
|
|
|
if (dbgFile || dbgConsole) {
|
|
|
|
if (dbgFile) {
|
|
|
|
debugFile_->open(QIODevice::WriteOnly | QIODevice::Truncate);
|
|
|
|
debugFile_->close();
|
|
|
|
fileDebug(debugFile_.get());
|
|
|
|
}
|
|
|
|
if (dbgConsole) {
|
|
|
|
vsConsoleDebug();
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 14:31:31 -04:00
|
|
|
#endif
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainApplication::setApplicationFont()
|
|
|
|
{
|
|
|
|
QFont font;
|
|
|
|
font.setFamily("Segoe UI");
|
|
|
|
setFont(font);
|
|
|
|
QFontDatabase::addApplicationFont(":/images/FontAwesome.otf");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-01 14:31:31 -04:00
|
|
|
MainApplication::initQmlEngine()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-09-01 14:31:31 -04:00
|
|
|
registerTypes();
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
engine_->addImageProvider(QLatin1String("qrImage"), new QrImageProvider());
|
|
|
|
engine_->addImageProvider(QLatin1String("tintedPixmap"), new TintedButtonImageProvider());
|
|
|
|
|
|
|
|
engine_->load(QUrl(QStringLiteral("qrc:/src/MainApplicationWindow.qml")));
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
void
|
|
|
|
MainApplication::initSettings()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-09-01 14:31:31 -04:00
|
|
|
AppSettingsManager::instance().initValues();
|
|
|
|
auto downloadPath = AppSettingsManager::instance().getValue(Settings::Key::DownloadPath);
|
|
|
|
LRCInstance::dataTransferModel().downloadDirectory = downloadPath.toString() + "/";
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
void
|
|
|
|
MainApplication::initSystray()
|
|
|
|
{
|
2020-08-31 15:57:10 -04:00
|
|
|
GlobalSystemTray& sysIcon = GlobalSystemTray::instance();
|
|
|
|
sysIcon.setIcon(QIcon(":images/jami.png"));
|
|
|
|
|
|
|
|
QMenu* systrayMenu = new QMenu();
|
|
|
|
|
|
|
|
QAction* exitAction = new QAction(tr("Exit"), this);
|
|
|
|
connect(exitAction, &QAction::triggered,
|
2020-09-01 14:31:31 -04:00
|
|
|
[this]{
|
|
|
|
engine_->quit();
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
connect(&sysIcon, &QSystemTrayIcon::activated,
|
|
|
|
[](QSystemTrayIcon::ActivationReason reason) {
|
|
|
|
if (reason != QSystemTrayIcon::ActivationReason::Context)
|
|
|
|
emit LRCInstance::instance().restoreAppRequested();
|
2020-08-31 15:57:10 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
systrayMenu->addAction(exitAction);
|
|
|
|
sysIcon.setContextMenu(systrayMenu);
|
|
|
|
sysIcon.show();
|
2020-09-01 14:31:31 -04:00
|
|
|
}
|
2020-08-31 15:57:10 -04:00
|
|
|
|
2020-09-01 14:31:31 -04:00
|
|
|
void
|
|
|
|
MainApplication::cleanup()
|
|
|
|
{
|
|
|
|
GlobalSystemTray::instance().hide();
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
FreeConsole();
|
|
|
|
#endif
|
|
|
|
QApplication::exit(0);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|