aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: b85c3fcd7c78624eb52063379ad261642fc39f30 (plain)
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
/*
 * This file is part of Remote Support Desktop
 * https://gitlab.das-netzwerkteam.de/RemoteWebApp/rwa.support.desktopapp
 * Copyright 2020, 2021 Daniel Teichmann <daniel.teichmann@das-netzwerkteam.de>
 * Copyright 2020, 2021 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * 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 St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QQuickItem>
#include <QObject>
#include <QTranslator>
#include <QDebug>
#include <QQmlContext>
#include <QQuickStyle>
#include <signal.h>

#include "RWADBusAdaptor.cpp"
#include "session.h"
#include "scenes/add_server_wizard/add_server_wizard.h"
#include "RWAHostModel.h"
#include "RWAHost.h"

#define BUILD_TIME __DATE__ " "  __TIME__

int main(int argc, char *argv[]) {
    qDebug() << "This app was built on: " << BUILD_TIME;

    // We don't want users to have multiple instances of this app running
    QString tmpDirPath = QDir::tempPath() + "/rwa.support.desktopapp";
    QString tmpFilePath = tmpDirPath + "/prevent-multiple-instances.lock";
    QDir tmpDir(tmpDirPath);
    if (!tmpDir.exists()) {
        // Ensure that the path exists
        tmpDir.mkpath(".");
    }
    QLockFile lockFile(tmpFilePath);
    qDebug().noquote() << QString("Checking for a lockfile at: '%0'").arg(tmpFilePath);

    if(!lockFile.tryLock(100)){
        qDebug().noquote() << "You already have this app running.\n"
                           << "Only one instance is allowed.\n"
                           << "Closing application now with an error.";

        return 1;
    }

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    QQuickStyle::setStyle("Material");

    QTranslator translator;
    qDebug().noquote() << QString("Locales: Loading locale: qrc:/locales/bin/%0").arg(QLocale::system().name());
    if(translator.load(":/locales/bin/" + QLocale::system().name())) {
        app.installTranslator(&translator);
        qDebug().noquote() << "Locales: Loaded: " + QLocale::system().name() + " locale!";
    } else {
        qWarning() << "Locales: Unable to load translation!";
    }

    QQmlApplicationEngine engine(&app);

    QScopedPointer<MainQMLAdaptor> main_gui (new MainQMLAdaptor(&app, &engine));

    // Make mainqmladaptor available to QML
    engine.rootContext()->setContextProperty("mainqmladaptor", main_gui.data());

    QScopedPointer<DBusAPI> _dbus_api (new DBusAPI());
    QObject::connect(_dbus_api.data(),
                     SIGNAL(serviceGetWebAppHostsResponse(QJsonDocument*)),
                     main_gui.data(),
                     SLOT(get_web_app_hosts_response(QJsonDocument*)));
    _dbus_api.data()->get_web_app_hosts_request();

    engine.load(QUrl(QStringLiteral("qrc:/src/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    QScopedPointer<Session> session (new Session(&app, main_gui.data()));

    QObject::connect(main_gui.data(),
                     SIGNAL(minimizeWindow()),
                     engine.rootObjects().takeFirst(),
                     SLOT(minimizeWindow()));

    QObject::connect(main_gui.data(),
                     SIGNAL(showWindow()),
                     engine.rootObjects().takeFirst(),
                     SLOT(showWindow()));

    QObject::connect(engine.rootObjects().takeFirst()->findChild<QObject*>("sidebar_drawer"),
                     SIGNAL(rwaHostSelected(QString)),
                     main_gui.data(),
                     SLOT(onRwaHostSelected(QString)));

    // Make add_server_wizard available to QML
    QScopedPointer<Add_Server_wizard> wizard (new Add_Server_wizard(&app, main_gui.data()));
    engine.rootContext()->setContextProperty("add_server_wizard", wizard.data());

    return app.exec();
}