source: ogBrowser-Git/src/main.cpp

shutdown-environment
Last change on this file was dcd2e6a, checked in by Vadim Troshchinskiy Shmelev <vtroshchinskiy@…>, 8 weeks ago

Shutdown desktop environment on exit, initial implementation

  • Property mode set to 100644
File size: 4.9 KB
Line 
1#include <QApplication>
2#include <QTranslator>
3#include <QDebug>
4#include <QCommandLineParser>
5#include <QDBusConnection>
6#include <QDBusInterface>
7#include <QDBusError>
8
9#include "mainwindow.h"
10#include "ogcommandline.h"
11#include "desktopparser.h"
12#include "dbusinterface.h"
13
14
15const QString SERVICE_NAME{"es.opengnsys.OGBrowser.browser"};
16
17int main(int argc, char *argv[])
18{
19    QApplication a(argc, argv);
20    QCoreApplication::setApplicationName("OGBrowser");
21    QCoreApplication::setOrganizationName("OpenGnsys");
22    QCoreApplication::setOrganizationDomain("opengnsys.es");
23
24    QTranslator translator;
25    QStringList translationDirs{QCoreApplication::applicationDirPath(), "", "."};
26
27    DBusInterface dbus_interface = DBusInterface();
28
29
30    bool translationsOk = false;
31
32    for(const QString &dir : translationDirs) {
33        qDebug() << "Trying to find translations in" << dir;
34        if (translator.load(QLocale(), "OGBrowser_", "", dir)) {
35            qDebug() << "Translations loaded";
36            QCoreApplication::installTranslator(&translator);
37            translationsOk = true;
38            break;
39        }
40    }
41
42    if (!translationsOk) {
43        qWarning() << "Failed to load translations. Tried looking in:" << translationDirs;
44    }
45
46
47    auto &desktopParser = DesktopParser::getInstance();
48    desktopParser.loadSchemes();
49
50
51    QCommandLineParser parser;
52    QCommandLineOption openInNewTabOption("openInNewTab", "Create a new tab when opening a new page");
53    parser.addOption(openInNewTabOption);
54
55    QCommandLineOption replacePageOption("replacePage", "When trying to open a new page/tab, replace the current page instead");
56    parser.addOption(replacePageOption);
57
58    QCommandLineOption ignoreNewPageOption("ignoreNewPage", "Ignore attempts to open documents in a new page/tab");
59    parser.addOption(ignoreNewPageOption);
60
61    QCommandLineOption disableWebSecurityOption("disable-web-security", "Disable web security and allow custom URL schemes");
62    parser.addOption(disableWebSecurityOption);
63
64    QCommandLineOption noSandboxOption("no-sandbox", "Disable Chromium sandbox to allow running as root");
65    parser.addOption(noSandboxOption);
66
67    QCommandLineOption ignoreSslErrors("ignore-ssl-errors", "Ignore SSL certificate errors");
68    parser.addOption(ignoreSslErrors);
69
70    QCommandLineOption stopRunning("stop-running", "Stop any running instances");
71    parser.addOption(stopRunning);
72
73    QCommandLineOption terminateEnvironmentOnShutdown("terminate-environment-on-shutdown", "Terminate the desktop environment if the browser is shut down (i3 and sway supported)");
74    parser.addOption(terminateEnvironmentOnShutdown);
75
76
77    QCommandLineOption helpOption  = parser.addHelpOption();
78
79    OGCommandLineOptions &options = OGCommandLineOptions::getInstance();
80
81
82    parser.process(a);
83    QStringList positional = parser.positionalArguments();
84
85    if (parser.isSet(helpOption)) {
86        return 0;
87    }
88
89    auto connection = QDBusConnection::sessionBus();
90    if (!connection.isConnected()) {
91        qWarning() << "Can't connect to the D-Bus session bus!";
92    } else {
93        if (parser.isSet(stopRunning)) {
94            QDBusInterface iface(SERVICE_NAME, "/");
95            if(iface.isValid()) {
96                auto reply = iface.call("quit");
97                return 0;
98            } else {
99                qCritical() << "Can't connect to running browser";
100                return 1;
101            }
102
103            return 0;
104        }
105
106
107        if (!connection.registerService(SERVICE_NAME)) {
108            qCritical() << "Can't register service" << SERVICE_NAME << ", browser already running";
109
110            if (positional.length() > 0) {
111                qInfo() << "Setting URL on running browser";
112                QDBusInterface iface(SERVICE_NAME, "/");
113                if (iface.isValid()) {
114                    auto reply = iface.call("setURL", positional[0]);
115                    exit(0);
116                }
117            }
118            exit(1);
119        }
120
121        qDebug() << "DBus service" << SERVICE_NAME << "registered";
122
123        connection.registerObject("/", &dbus_interface, QDBusConnection::ExportAllSlots);
124        qDebug() << "DBus object registered";
125    }
126
127    if (parser.isSet(openInNewTabOption)) {
128        options.setNewTabBehavior(OGCommandLineOptions::NewTabBehavior::NewTab);
129    }
130
131    if (parser.isSet(replacePageOption)) {
132        options.setNewTabBehavior(OGCommandLineOptions::NewTabBehavior::ReplacePage);
133    }
134
135    if (parser.isSet(ignoreNewPageOption)) {
136        options.setNewTabBehavior(OGCommandLineOptions::NewTabBehavior::Ignore);
137    }
138
139    if (parser.isSet(ignoreSslErrors)) {
140        options.setIgnoreSslErrors(true);
141    }
142
143    if (parser.isSet(terminateEnvironmentOnShutdown)) {
144        options.setTerminateEnvironmentOnShutdown(true);
145    }
146
147    if ( positional.length() > 0) {
148        options.setUrl( positional[0 ] );
149    }
150
151    MainWindow w;
152    dbus_interface.setMainWindow(&w);
153    w.show();
154    return a.exec();
155}
Note: See TracBrowser for help on using the repository browser.