Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten util/IncognitoBrowser.{h,c}pp #4280

Merged
merged 1 commit into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 66 additions & 57 deletions src/util/IncognitoBrowser.cpp
Original file line number Diff line number Diff line change
@@ -1,81 +1,90 @@
#include "IncognitoBrowser.hpp"
#include "util/IncognitoBrowser.hpp"

#include <QProcess>
#include <QRegularExpression>
#include <QSettings>
#include <QVariant>

namespace chatterino {
namespace {

using namespace chatterino;

#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
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)
{
replacers.emplace_back(
QRegularExpression("(" + switch_.first + "\\.exe\"?).*",
QRegularExpression::CaseInsensitiveOption),
"\\1 " + switch_.second);
}
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)
// try to find matching regex and apply it
for (const auto &replacement : replacers)
{
if (replacement.first.match(command).hasMatch())
{
if (replacement.first.match(command).hasMatch())
{
command.replace(replacement.first, replacement.second);
return command;
}
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();

// couldn't match any browser -> unknown browser
// 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();
}

QString getCommand(const QString &link)
// inject switch to enable private browsing
command = injectPrivateSwitch(command);
if (command.isNull())
{
// 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;
return QString();
}

// link
command += " " + link;

return command;
}
#endif

} // namespace

namespace chatterino {

bool supportsIncognitoLinks()
{
#ifdef Q_OS_WIN
Expand Down
2 changes: 1 addition & 1 deletion src/util/IncognitoBrowser.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <QtGlobal>
#include <QString>

namespace chatterino {

Expand Down