From d14a4df9e39b7af4009ffe5ff887b40cf3cc6bc8 Mon Sep 17 00:00:00 2001 From: fourtf Date: Tue, 16 Oct 2018 16:07:59 +0200 Subject: [PATCH] opening links in private browsing mode (windows) tested on firefox, chrome and internet explorer --- src/util/IncognitoBrowser.cpp | 107 +++++++++++++++----------- src/util/IncognitoBrowser.hpp | 12 +-- src/widgets/helper/ChannelView.cpp | 16 ++-- src/widgets/splits/SplitContainer.cpp | 5 +- 4 files changed, 76 insertions(+), 64 deletions(-) diff --git a/src/util/IncognitoBrowser.cpp b/src/util/IncognitoBrowser.cpp index 341478c74..81cb2ffda 100644 --- a/src/util/IncognitoBrowser.cpp +++ b/src/util/IncognitoBrowser.cpp @@ -1,40 +1,50 @@ -#include "incognitobrowser.hpp" +#include "IncognitoBrowser.hpp" #include +#include #include #include #include "debug/Log.hpp" -#ifdef Q_OS_WIN - namespace chatterino { namespace { - QString getAppPath(const QString &executableName) +#ifdef Q_OS_WIN + QString injectPrivateSwitch(QString command) { - auto paths = QStringList{ - // clang-format off - "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + executableName, - "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + executableName - // clang-format on + // list of command line switches to turn on private browsing in browsers + static auto switches = std::vector>{ + {"firefox", "-private-window"}, {"chrome", "-incognito"}, + {"vivaldi", "--private"}, {"opera", "-newprivatetab"}, + {"iexplore", "-private"}, }; - for (const auto &path : paths) { - auto val = QSettings(path, QSettings::NativeFormat) - .value("Default") - .toString(); - if (!val.isNull()) { - return val; + // transform into regex and replacement string + std::vector> replacers; + for (const auto &switch_ : switches) { + replacers.emplace_back( + QRegularExpression("(" + switch_.first + "\\.exe\"?).*"), + "\\1 " + switch_.second); + + log("(" + switch_.first + "\\.exe\"?).*"); + } + + // 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(); } -} // namespace -void openLinkIncognito(const QString &link) -{ - auto browserChoice = QSettings("HKEY_CURRENT_" + 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", @@ -42,33 +52,44 @@ void openLinkIncognito(const QString &link) .value("Progid") .toString(); - if (!browserChoice.isNull()) { - if (browserChoice == "FirefoxURL") { - // Firefox - auto path = getAppPath("firefox.exe"); + // 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(); - QProcess::startDetached(path, {"-private-window", link}); - } else if (browserChoice == "ChromeHTML") { - // Chrome - auto path = getAppPath("chrome.exe"); + log(command); - QProcess::startDetached(path, {"-incognito", link}); - } - // Possible implementation for MS Edge. - // Doesn't quite work yet. - /* - else if (browserChoice == "AppXq0fevzme2pys62n3e0fbqa7peapykr8v") { - // Edge - QProcess::startDetached("C:\\Windows\\System32\\cmd.exe", - {"/c", "start", - "shell:AppsFolder\Microsoft.MicrosoftEdge_" - "8wekyb3d8bbwe!MicrosoftEdge", - "-private", link}); - } - */ + // 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 +} + +void openLinkIncognito(const QString &link) +{ +#ifdef Q_OS_WIN + auto command = getCommand(link); + + QProcess::startDetached(command); +#endif } } // namespace chatterino - -#endif diff --git a/src/util/IncognitoBrowser.hpp b/src/util/IncognitoBrowser.hpp index bfc489507..1461d2b34 100644 --- a/src/util/IncognitoBrowser.hpp +++ b/src/util/IncognitoBrowser.hpp @@ -2,13 +2,9 @@ #include -#ifdef Q_OS_WIN - -// only supported on windows right now -# define INCOGNITO_LINKS_SUPPORTED - namespace chatterino { -void openLinkIncognito(const QString &link); -} -#endif +bool supportsIncognitoLinks(); +void openLinkIncognito(const QString &link); + +} // namespace chatterino diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index d4f9242b9..52264c511 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -16,6 +16,7 @@ #include "singletons/Theme.hpp" #include "singletons/WindowManager.hpp" #include "util/DistanceBetweenPoints.hpp" +#include "util/IncognitoBrowser.hpp" #include "widgets/Scrollbar.hpp" #include "widgets/TooltipWidget.hpp" #include "widgets/dialogs/UserInfoPopup.hpp" @@ -1222,6 +1223,10 @@ void ChannelView::addContextMenuItems( menu->addAction("Open link", [url] { QDesktopServices::openUrl(QUrl(url)); }); + if (supportsIncognitoLinks()) { + menu->addAction("Open link incognito", + [url] { openLinkIncognito(url); }); + } menu->addAction("Copy link", [url] { QApplication::clipboard()->setText(url); }); @@ -1256,15 +1261,8 @@ void ChannelView::addContextMenuItems( R"(^(?:https?:\/\/)?(?:www\.|go\.)?twitch\.tv\/(?[a-z0-9_]{3,}))", QRegularExpression::CaseInsensitiveOption); static QSet ignoredUsernames{ - "videos", - "settings", - "directory", - "jobs", - "friends", - "inventory", - "payments", - "subscriptions", - "messages", + "videos", "settings", "directory", "jobs", "friends", + "inventory", "payments", "subscriptions", "messages", }; auto twitchMatch = diff --git a/src/widgets/splits/SplitContainer.cpp b/src/widgets/splits/SplitContainer.cpp index 61d465c2b..147a03a54 100644 --- a/src/widgets/splits/SplitContainer.cpp +++ b/src/widgets/splits/SplitContainer.cpp @@ -469,10 +469,7 @@ void SplitContainer::paintEvent(QPaintEvent *) if (notebook != nullptr) { if (notebook->getPageCount() > 1) { - text += - "\n\nTip: After adding a split you can hold to " - "move it or split it " - "further."; + text += "\n\nAfter adding hold to move or split it."; } }