close Warning: Failed to sync with repository "ogBrowser-Git": (1366, "Incorrect string value: '\\xF0\\x9F\\x93\\xA6 I...' for column 'message' at row 1"); repository information may be out of date. Look in the Trac log for more information including mitigation strategies.

source: ogBrowser-Git/src/main.cpp

fix_uploadmain
Last change on this file was 98db196, checked in by Vadim Troshchinskiy Shmelev <vtroshchinskiy@…>, 4 months ago

Use DBus to command browser to quit or change URL

  • Property mode set to 100644
File size: 4.5 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
74    QCommandLineOption helpOption  = parser.addHelpOption();
75
76    OGCommandLineOptions &options = OGCommandLineOptions::getInstance();
77
78
79    parser.process(a);
80    QStringList positional = parser.positionalArguments();
81
82    if (parser.isSet(helpOption)) {
83        return 0;
84    }
85
86    auto connection = QDBusConnection::sessionBus();
87    if (!connection.isConnected()) {
88        qWarning() << "Can't connect to the D-Bus session bus!";
89    } else {
90        if (parser.isSet(stopRunning)) {
91            QDBusInterface iface(SERVICE_NAME, "/");
92            if(iface.isValid()) {
93                auto reply = iface.call("quit");
94                return 0;
95            } else {
96                qCritical() << "Can't connect to running browser";
97                return 1;
98            }
99
100            return 0;
101        }
102
103
104        if (!connection.registerService(SERVICE_NAME)) {
105            qCritical() << "Can't register service" << SERVICE_NAME << ", browser already running";
106
107            if (positional.length() > 0) {
108                qInfo() << "Setting URL on running browser";
109                QDBusInterface iface(SERVICE_NAME, "/");
110                if (iface.isValid()) {
111                    auto reply = iface.call("setURL", positional[0]);
112                    exit(0);
113                }
114            }
115            exit(1);
116        }
117
118        qDebug() << "DBus service" << SERVICE_NAME << "registered";
119
120        connection.registerObject("/", &dbus_interface, QDBusConnection::ExportAllSlots);
121        qDebug() << "DBus object registered";
122    }
123
124    if (parser.isSet(openInNewTabOption)) {
125        options.setNewTabBehavior(OGCommandLineOptions::NewTabBehavior::NewTab);
126    }
127
128    if (parser.isSet(replacePageOption)) {
129        options.setNewTabBehavior(OGCommandLineOptions::NewTabBehavior::ReplacePage);
130    }
131
132    if (parser.isSet(ignoreNewPageOption)) {
133        options.setNewTabBehavior(OGCommandLineOptions::NewTabBehavior::Ignore);
134    }
135
136    if (parser.isSet(ignoreSslErrors)) {
137        options.setIgnoreSslErrors(true);
138    }
139
140
141    if ( positional.length() > 0) {
142        options.setUrl( positional[0 ] );
143    }
144
145    MainWindow w;
146    dbus_interface.setMainWindow(&w);
147    w.show();
148    return a.exec();
149}
Note: See TracBrowser for help on using the repository browser.