mirror-chatterino2/src/util/IncognitoBrowser.cpp

100 lines
3 KiB
C++
Raw Normal View History

#include "IncognitoBrowser.hpp"
2018-10-16 14:13:19 +02:00
#include <QProcess>
#include <QRegularExpression>
2018-10-16 14:13:19 +02:00
#include <QSettings>
#include <QVariant>
#include "debug/Log.hpp"
namespace chatterino {
namespace {
#ifdef Q_OS_WIN
QString injectPrivateSwitch(QString command)
2018-10-16 14:13:19 +02:00
{
// list of command line switches to turn on private browsing in browsers
static auto switches = std::vector<std::pair<QString, QString>>{
{"firefox", "-private-window"}, {"chrome", "-incognito"},
{"vivaldi", "-incognito"}, {"opera", "-newprivatetab"},
{"opera\\\\launcher", "--private"}, {"iexplore", "-private"},
2018-10-16 14:13:19 +02:00
};
// transform into regex and replacement string
std::vector<std::pair<QRegularExpression, QString>> replacers;
2018-10-21 13:43:02 +02:00
for (const auto &switch_ : switches)
{
replacers.emplace_back(
QRegularExpression("(" + switch_.first + "\\.exe\"?).*",
QRegularExpression::CaseInsensitiveOption),
"\\1 " + switch_.second);
}
// try to find matching regex and apply it
2018-10-21 13:43:02 +02:00
for (const auto &replacement : replacers)
{
if (replacement.first.match(command).hasMatch())
{
command.replace(replacement.first, replacement.second);
return command;
2018-10-16 14:13:19 +02:00
}
}
// couldn't match any browser -> unknown browser
2018-10-16 14:13:19 +02:00
return QString();
}
QString getCommand(const QString &link)
{
// get default browser prog id
auto browserId = QSettings("HKEY_CURRENT_"
2018-10-16 14:13:19 +02:00
"USER\\Software\\Microsoft\\Windows\\Shell\\"
"Associations\\UrlAssociatio"
"ns\\http\\UserChoice",
QSettings::NativeFormat)
.value("Progid")
.toString();
// get default browser start command
auto command = QSettings("HKEY_CLASSES_ROOT\\" + browserId +
"\\shell\\open\\command",
QSettings::NativeFormat)
.value("Default")
.toString();
2018-10-21 13:43:02 +02:00
if (command.isNull())
return QString();
2018-10-16 14:13:19 +02:00
log(command);
2018-10-16 14:13:19 +02:00
// inject switch to enable private browsing
command = injectPrivateSwitch(command);
2018-10-21 13:43:02 +02:00
if (command.isNull())
return QString();
// link
command += " " + link;
return command;
2018-10-16 14:13:19 +02:00
}
#endif
} // namespace
bool supportsIncognitoLinks()
{
#ifdef Q_OS_WIN
return !getCommand("").isNull();
#else
return false;
#endif
2018-10-16 14:13:19 +02:00
}
void openLinkIncognito(const QString &link)
{
#ifdef Q_OS_WIN
auto command = getCommand(link);
2018-10-16 14:13:19 +02:00
QProcess::startDetached(command);
2018-10-16 14:13:19 +02:00
#endif
}
} // namespace chatterino