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 <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QStringList>
|
|
|
|
|
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
|
|
|
|
|
|
|
parser.addOptions({
|
|
|
|
{{"v", "version"}, "Displays version information."},
|
|
|
|
crashRecoveryOption,
|
2020-10-19 07:08:02 +02:00
|
|
|
parentWindowOption,
|
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()))
|
|
|
|
{
|
|
|
|
qDebug() << "Warning: Unhandled options:"
|
|
|
|
<< parser.unknownOptionNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet("help"))
|
|
|
|
{
|
|
|
|
qDebug().noquote() << parser.helpText();
|
|
|
|
::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
|
|
|
{
|
2020-09-26 16:03:51 +02:00
|
|
|
QJsonArray channelArray;
|
|
|
|
QStringList channelArgList = parser.value("c").split(";");
|
|
|
|
for (QString channelArg : channelArgList)
|
2019-09-22 15:30:04 +02:00
|
|
|
{
|
2020-09-26 16:03:51 +02:00
|
|
|
// Twitch is default platform
|
|
|
|
QString platform = "t";
|
|
|
|
QString channelName = channelArg;
|
|
|
|
|
|
|
|
const QRegExp regExp("(.):(.*)");
|
|
|
|
if (regExp.indexIn(channelArg) != -1)
|
|
|
|
{
|
|
|
|
platform = regExp.cap(1);
|
|
|
|
channelName = regExp.cap(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Twitch (default)
|
|
|
|
if (platform == "t")
|
|
|
|
{
|
|
|
|
// TODO: try not to parse JSON
|
|
|
|
QString channelObjectString =
|
|
|
|
"{\"splits2\": { \"data\": { \"name\": \"" + channelName +
|
|
|
|
"\", \"type\": \"twitch\" }, \"type\": \"split\" }}";
|
|
|
|
channelArray.push_back(
|
|
|
|
QJsonDocument::fromJson(channelObjectString.toUtf8())
|
|
|
|
.object());
|
|
|
|
}
|
2019-09-22 15:30:04 +02:00
|
|
|
}
|
2020-09-26 16:03:51 +02:00
|
|
|
if (channelArray.size() > 0)
|
2019-10-07 18:57:33 +02:00
|
|
|
{
|
2020-09-26 16:03:51 +02:00
|
|
|
this->dontSaveSettings = true;
|
|
|
|
this->channelsToJoin = channelArray;
|
2019-10-07 18:57:33 +02:00
|
|
|
}
|
2019-09-22 15:30:04 +02:00
|
|
|
}
|
2020-09-26 16:03:51 +02:00
|
|
|
|
|
|
|
this->printVersion = parser.isSet("v");
|
|
|
|
this->crashRecovery = parser.isSet("crash-recovery");
|
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
|