mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Flatten util/IncognitoBrowser.{h,c}pp (#4280)
This commit is contained in:
parent
1043f9f803
commit
e48945e370
2 changed files with 66 additions and 57 deletions
|
@ -1,81 +1,90 @@
|
||||||
#include "IncognitoBrowser.hpp"
|
#include "util/IncognitoBrowser.hpp"
|
||||||
|
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
namespace chatterino {
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
using namespace chatterino;
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QString injectPrivateSwitch(QString command)
|
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>>{
|
||||||
|
{"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"},
|
||||||
|
};
|
||||||
|
|
||||||
|
// transform into regex and replacement string
|
||||||
|
std::vector<std::pair<QRegularExpression, QString>> replacers;
|
||||||
|
for (const auto &switch_ : switches)
|
||||||
{
|
{
|
||||||
// list of command line switches to turn on private browsing in browsers
|
replacers.emplace_back(
|
||||||
static auto switches = std::vector<std::pair<QString, QString>>{
|
QRegularExpression("(" + switch_.first + "\\.exe\"?).*",
|
||||||
{"firefox", "-private-window"}, {"librewolf", "-private-window"},
|
QRegularExpression::CaseInsensitiveOption),
|
||||||
{"waterfox", "-private-window"}, {"icecat", "-private-window"},
|
"\\1 " + switch_.second);
|
||||||
{"chrome", "-incognito"}, {"vivaldi", "-incognito"},
|
}
|
||||||
{"opera", "-newprivatetab"}, {"opera\\\\launcher", "--private"},
|
|
||||||
{"iexplore", "-private"}, {"msedge", "-inprivate"},
|
|
||||||
};
|
|
||||||
|
|
||||||
// transform into regex and replacement string
|
// try to find matching regex and apply it
|
||||||
std::vector<std::pair<QRegularExpression, QString>> replacers;
|
for (const auto &replacement : replacers)
|
||||||
for (const auto &switch_ : switches)
|
{
|
||||||
|
if (replacement.first.match(command).hasMatch())
|
||||||
{
|
{
|
||||||
replacers.emplace_back(
|
command.replace(replacement.first, replacement.second);
|
||||||
QRegularExpression("(" + switch_.first + "\\.exe\"?).*",
|
return command;
|
||||||
QRegularExpression::CaseInsensitiveOption),
|
|
||||||
"\\1 " + switch_.second);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// try to find matching regex and apply it
|
// couldn't match any browser -> unknown browser
|
||||||
for (const auto &replacement : replacers)
|
return QString();
|
||||||
{
|
}
|
||||||
if (replacement.first.match(command).hasMatch())
|
|
||||||
{
|
|
||||||
command.replace(replacement.first, replacement.second);
|
|
||||||
return command;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// couldn't match any browser -> unknown browser
|
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();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString getCommand(const QString &link)
|
// inject switch to enable private browsing
|
||||||
|
command = injectPrivateSwitch(command);
|
||||||
|
if (command.isNull())
|
||||||
{
|
{
|
||||||
// get default browser prog id
|
return QString();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// link
|
||||||
|
command += " " + link;
|
||||||
|
|
||||||
|
return command;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace chatterino {
|
||||||
|
|
||||||
bool supportsIncognitoLinks()
|
bool supportsIncognitoLinks()
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QtGlobal>
|
#include <QString>
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue