2018-10-16 16:07:59 +02:00
|
|
|
#include "IncognitoBrowser.hpp"
|
2018-10-16 14:13:19 +02:00
|
|
|
|
|
|
|
#include <QProcess>
|
2018-10-16 16:07:59 +02:00
|
|
|
#include <QRegularExpression>
|
2018-10-16 14:13:19 +02:00
|
|
|
#include <QSettings>
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
|
|
#include "debug/Log.hpp"
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace {
|
2018-10-16 16:07:59 +02:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QString injectPrivateSwitch(QString command)
|
2018-10-16 14:13:19 +02:00
|
|
|
{
|
2018-10-16 16:07:59 +02:00
|
|
|
// list of command line switches to turn on private browsing in browsers
|
|
|
|
static auto switches = std::vector<std::pair<QString, QString>>{
|
2018-10-16 16:25:48 +02:00
|
|
|
{"firefox", "-private-window"}, {"chrome", "-incognito"},
|
|
|
|
{"vivaldi", "-incognito"}, {"opera", "-newprivatetab"},
|
|
|
|
{"opera\\\\launcher", "--private"}, {"iexplore", "-private"},
|
2018-10-16 14:13:19 +02:00
|
|
|
};
|
|
|
|
|
2018-10-16 16:07:59 +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)
|
|
|
|
{
|
2018-10-16 16:07:59 +02:00
|
|
|
replacers.emplace_back(
|
2018-10-16 16:25:48 +02:00
|
|
|
QRegularExpression("(" + switch_.first + "\\.exe\"?).*",
|
|
|
|
QRegularExpression::CaseInsensitiveOption),
|
2018-10-16 16:07:59 +02:00
|
|
|
"\\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())
|
|
|
|
{
|
2018-10-16 16:07:59 +02:00
|
|
|
command.replace(replacement.first, replacement.second);
|
|
|
|
return command;
|
2018-10-16 14:13:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
// couldn't match any browser -> unknown browser
|
2018-10-16 14:13:19 +02:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
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();
|
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
// 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
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
log(command);
|
2018-10-16 14:13:19 +02:00
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
// inject switch to enable private browsing
|
|
|
|
command = injectPrivateSwitch(command);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (command.isNull())
|
|
|
|
return QString();
|
2018-10-16 16:07:59 +02:00
|
|
|
|
|
|
|
// link
|
|
|
|
command += " " + link;
|
|
|
|
|
|
|
|
return command;
|
2018-10-16 14:13:19 +02:00
|
|
|
}
|
2018-10-16 16:07:59 +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
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
void openLinkIncognito(const QString &link)
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
auto command = getCommand(link);
|
2018-10-16 14:13:19 +02:00
|
|
|
|
2018-10-16 16:07:59 +02:00
|
|
|
QProcess::startDetached(command);
|
2018-10-16 14:13:19 +02:00
|
|
|
#endif
|
2018-10-16 16:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|