2019-09-08 22:27:57 +02:00
|
|
|
#include "IncognitoBrowser.hpp"
|
|
|
|
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace {
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QString injectPrivateSwitch(QString command)
|
|
|
|
{
|
|
|
|
// list of command line switches to turn on private browsing in browsers
|
|
|
|
static auto switches = std::vector<std::pair<QString, QString>>{
|
2022-03-06 13:36:20 +01:00
|
|
|
{"firefox", "-private-window"}, {"librewolf", "-private-window"},
|
|
|
|
{"waterfox", "-private-window"}, {"icecat", "-private-window"},
|
|
|
|
{"chrome", "-incognito"}, {"vivaldi", "-incognito"},
|
|
|
|
{"opera", "-newprivatetab"}, {"opera\\\\launcher", "--private"},
|
|
|
|
{"iexplore", "-private"}, {"msedge", "-inprivate"},
|
2019-09-08 22:27:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// transform into regex and replacement string
|
|
|
|
std::vector<std::pair<QRegularExpression, QString>> replacers;
|
|
|
|
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
|
|
|
|
for (const auto &replacement : replacers)
|
|
|
|
{
|
|
|
|
if (replacement.first.match(command).hasMatch())
|
|
|
|
{
|
|
|
|
command.replace(replacement.first, replacement.second);
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// couldn't match any browser -> unknown browser
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getCommand(const QString &link)
|
|
|
|
{
|
|
|
|
// get default browser prog id
|
|
|
|
auto browserId = QSettings("HKEY_CURRENT_"
|
|
|
|
"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();
|
|
|
|
if (command.isNull())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
// inject switch to enable private browsing
|
|
|
|
command = injectPrivateSwitch(command);
|
|
|
|
if (command.isNull())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
// link
|
|
|
|
command += " " + link;
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool supportsIncognitoLinks()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
return !getCommand("").isNull();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-06-27 13:40:44 +02:00
|
|
|
bool openLinkIncognito(const QString &link)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
auto command = getCommand(link);
|
|
|
|
|
2022-11-10 20:11:40 +01:00
|
|
|
// TODO: split command into program path and incognito argument
|
|
|
|
return QProcess::startDetached(command, {});
|
2021-06-27 13:40:44 +02:00
|
|
|
#else
|
|
|
|
return false;
|
2019-09-08 22:27:57 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|