/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "browser.h" #include "configuration.h" #include "session/sessiondialog.h" #include "session_json.hpp" #include "settings.h" #include #include #include #include #include int main(int argc, char **argv) { // change log pattern spdlog::set_pattern("[%^%l%$] [%P:%t] %v"); #ifdef QT_DEBUG spdlog::set_level(spdlog::level::debug); // Set global log level to debug #endif QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("url", "URLs to open"); // generic options QCommandLineOption cmd_config({ "c", "config" }, "Set the configuration file.", "path"); parser.addOption(cmd_config); QCommandLineOption cmd_session({ "s", "session" }, "Open the specified session.", "path"); parser.addOption(cmd_session); QCommandLineOption cmd_pick_session("pick-session", "Show all available sessions and select which one to open."); parser.addOption(cmd_pick_session); // Qt options QCommandLineOption cmd_renderer("renderer", "Select the OpenGL renderer used by the application. Available options are: desktop, gles, software", "value"); parser.addOption(cmd_renderer); // SingleApplication options QCommandLineOption cmd_no_remote("no-remote", "Start a new instance that won't accept or send remote commands."); parser.addOption(cmd_no_remote); { // handle command line options that need to be set before the QApplication object is created QStringList args; for(int i = 0; i < argc; ++i) args.append(argv[i]); parser.parse(args); // the OpenGL implementation needs to be manually set to software if it's not properly configured if(parser.isSet(cmd_renderer)) { const auto value = parser.value(cmd_renderer); if(value == QLatin1String("desktop")) { QApplication::setAttribute(Qt::AA_UseDesktopOpenGL, true); } else if(value == QLatin1String("gles")) { QApplication::setAttribute(Qt::AA_UseOpenGLES, true); } else if(value == QLatin1String("software")) { QApplication::setAttribute(Qt::AA_UseSoftwareOpenGL, true); } else { spdlog::error("Unknown cmd_renderer flag: {}", qUtf8Printable(value)); } } } // set this, otherwise the webview becomes black when using a stylesheet QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); init_conf(parser.value(cmd_config).toStdString()); Browser app(argc, argv); parser.process(app); const auto profile = []() { Configuration conf; return conf.value("profile.default").value(); }(); QStringList urls = parser.positionalArguments(); if(urls.isEmpty()) { urls.append(QString()); } // if app is primary, create new sessions from received messages if(app.isPrimary() && !parser.isSet(cmd_no_remote)) { QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, const QByteArray &message) { Q_UNUSED(instanceId); JsonSession session(message); app.open(session.get()); }); } { const auto session = [&]() { if(parser.isSet(cmd_session)) { QFile sessionJson(parser.value(cmd_session)); if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { return JsonSession(sessionJson.readAll()); } } if(parser.isSet(cmd_pick_session)) { SessionDialog dlg; if(const auto pick = dlg.pickSession()) { return JsonSession(pick.value()); } } return JsonSession(profile, urls); }(); if(app.isPrimary() || parser.isSet(cmd_no_remote)) { app.open(session.get()); } else { // app is secondary and not standalone return app.sendMessage(session.serialize()) ? EXIT_SUCCESS : EXIT_FAILURE; } } return QApplication::exec(); }