2019-09-22 15:30:04 +02:00
|
|
|
#include "Args.hpp"
|
|
|
|
|
2020-09-26 16:03:51 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QStringList>
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2021-01-23 16:26:42 +01:00
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
#include "singletons/WindowManager.hpp"
|
2021-03-13 12:14:40 +01:00
|
|
|
#include "util/AttachToConsole.hpp"
|
2021-01-23 16:26:42 +01:00
|
|
|
#include "util/CombinePath.hpp"
|
|
|
|
#include "widgets/Window.hpp"
|
2020-09-26 16:03:51 +02:00
|
|
|
|
2019-09-22 15:30:04 +02:00
|
|
|
namespace chatterino {
|
|
|
|
|
2020-09-26 16:03:51 +02:00
|
|
|
Args::Args(const QApplication &app)
|
2019-09-22 15:30:04 +02:00
|
|
|
{
|
2020-09-26 16:03:51 +02:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription("Chatterino 2 Client for Twitch Chat");
|
|
|
|
parser.addHelpOption();
|
|
|
|
|
|
|
|
// Used internally by app to restart after unexpected crashes
|
|
|
|
QCommandLineOption crashRecoveryOption("crash-recovery");
|
2020-10-19 07:08:02 +02:00
|
|
|
crashRecoveryOption.setFlags(QCommandLineOption::HiddenFromHelp);
|
|
|
|
|
|
|
|
// Added to ignore the parent-window option passed during native messaging
|
|
|
|
QCommandLineOption parentWindowOption("parent-window");
|
|
|
|
parentWindowOption.setFlags(QCommandLineOption::HiddenFromHelp);
|
2020-09-26 16:03:51 +02:00
|
|
|
|
2021-03-13 12:14:40 +01:00
|
|
|
// Verbose
|
|
|
|
QCommandLineOption verboseOption({{"v", "verbose"},
|
|
|
|
"Attaches to the Console on windows, "
|
|
|
|
"allowing you to see debug output."});
|
|
|
|
crashRecoveryOption.setFlags(QCommandLineOption::HiddenFromHelp);
|
|
|
|
|
2020-09-26 16:03:51 +02:00
|
|
|
parser.addOptions({
|
2021-03-13 11:44:44 +01:00
|
|
|
{{"V", "version"}, "Displays version information."},
|
2020-09-26 16:03:51 +02:00
|
|
|
crashRecoveryOption,
|
2020-10-19 07:08:02 +02:00
|
|
|
parentWindowOption,
|
2021-03-13 12:14:40 +01:00
|
|
|
verboseOption,
|
2020-09-26 16:03:51 +02:00
|
|
|
});
|
|
|
|
parser.addOption(QCommandLineOption(
|
|
|
|
{"c", "channels"},
|
|
|
|
"Joins only supplied channels on startup. Use letters with colons to "
|
|
|
|
"specify platform. Only twitch channels are supported at the moment.\n"
|
|
|
|
"If platform isn't specified, default is Twitch.",
|
|
|
|
"t:channel1;t:channel2;..."));
|
2020-10-18 22:56:54 +02:00
|
|
|
|
|
|
|
if (!parser.parse(app.arguments()))
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCWarning(chatterinoArgs)
|
|
|
|
<< "Unhandled options:" << parser.unknownOptionNames();
|
2020-10-18 22:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet("help"))
|
|
|
|
{
|
2021-03-13 12:14:40 +01:00
|
|
|
attachToConsole();
|
2021-01-23 16:26:42 +01:00
|
|
|
qInfo().noquote() << parser.helpText();
|
2020-10-18 22:56:54 +02:00
|
|
|
::exit(EXIT_SUCCESS);
|
|
|
|
}
|
2020-09-26 16:03:51 +02:00
|
|
|
|
|
|
|
const QStringList args = parser.positionalArguments();
|
|
|
|
this->shouldRunBrowserExtensionHost =
|
|
|
|
(args.size() > 0 && (args[0].startsWith("chrome-extension://") ||
|
|
|
|
args[0].endsWith(".json")));
|
|
|
|
|
|
|
|
if (parser.isSet("c"))
|
2019-09-22 15:30:04 +02:00
|
|
|
{
|
2021-01-23 16:26:42 +01:00
|
|
|
this->applyCustomChannelLayout(parser.value("c"));
|
|
|
|
}
|
|
|
|
|
2021-03-13 12:14:40 +01:00
|
|
|
this->verbose = parser.isSet(verboseOption);
|
|
|
|
|
|
|
|
this->printVersion = parser.isSet("V");
|
2021-01-23 16:26:42 +01:00
|
|
|
this->crashRecovery = parser.isSet("crash-recovery");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Args::applyCustomChannelLayout(const QString &argValue)
|
|
|
|
{
|
|
|
|
WindowLayout layout;
|
|
|
|
WindowDescriptor window;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is only one window that is loaded from the --channels
|
|
|
|
* argument so that is what we use as the main window.
|
|
|
|
*/
|
|
|
|
window.type_ = WindowType::Main;
|
|
|
|
|
|
|
|
// Load main window layout from config file so we can use the same geometry
|
|
|
|
const QRect configMainLayout = [] {
|
|
|
|
const QString windowLayoutFile =
|
|
|
|
combinePath(getPaths()->settingsDirectory,
|
|
|
|
WindowManager::WINDOW_LAYOUT_FILENAME);
|
|
|
|
|
|
|
|
const WindowLayout configLayout =
|
|
|
|
WindowLayout::loadFromFile(windowLayoutFile);
|
|
|
|
|
|
|
|
for (const WindowDescriptor &window : configLayout.windows_)
|
|
|
|
{
|
|
|
|
if (window.type_ != WindowType::Main)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return window.geometry_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QRect(-1, -1, -1, -1);
|
|
|
|
}();
|
|
|
|
|
|
|
|
window.geometry_ = std::move(configMainLayout);
|
|
|
|
|
|
|
|
QStringList channelArgList = argValue.split(";");
|
|
|
|
for (const QString &channelArg : channelArgList)
|
|
|
|
{
|
|
|
|
if (channelArg.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Twitch is default platform
|
|
|
|
QString platform = "t";
|
|
|
|
QString channelName = channelArg;
|
|
|
|
|
|
|
|
const QRegExp regExp("(.):(.*)");
|
|
|
|
if (regExp.indexIn(channelArg) != -1)
|
2019-09-22 15:30:04 +02:00
|
|
|
{
|
2021-01-23 16:26:42 +01:00
|
|
|
platform = regExp.cap(1);
|
|
|
|
channelName = regExp.cap(2);
|
2019-09-22 15:30:04 +02:00
|
|
|
}
|
2021-01-23 16:26:42 +01:00
|
|
|
|
|
|
|
// Twitch (default)
|
|
|
|
if (platform == "t")
|
2019-10-07 18:57:33 +02:00
|
|
|
{
|
2021-01-23 16:26:42 +01:00
|
|
|
TabDescriptor tab;
|
|
|
|
|
|
|
|
// Set first tab as selected
|
|
|
|
tab.selected_ = window.tabs_.empty();
|
|
|
|
tab.rootNode_ = SplitNodeDescriptor{"twitch", channelName};
|
|
|
|
|
|
|
|
window.tabs_.emplace_back(std::move(tab));
|
2019-10-07 18:57:33 +02:00
|
|
|
}
|
2019-09-22 15:30:04 +02:00
|
|
|
}
|
2020-09-26 16:03:51 +02:00
|
|
|
|
2021-01-23 16:26:42 +01:00
|
|
|
// Only respect --channels if we could actually parse any channels
|
|
|
|
if (!window.tabs_.empty())
|
|
|
|
{
|
|
|
|
this->dontSaveSettings = true;
|
|
|
|
|
|
|
|
layout.windows_.emplace_back(std::move(window));
|
|
|
|
this->customChannelLayout = std::move(layout);
|
|
|
|
}
|
2019-09-22 15:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Args *instance = nullptr;
|
|
|
|
|
2020-09-26 16:03:51 +02:00
|
|
|
void initArgs(const QApplication &app)
|
2019-09-22 15:30:04 +02:00
|
|
|
{
|
2020-09-26 16:03:51 +02:00
|
|
|
instance = new Args(app);
|
2019-09-22 15:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Args &getArgs()
|
|
|
|
{
|
|
|
|
assert(instance);
|
|
|
|
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|