diff --git a/chatterino.pro b/chatterino.pro index 131696679..3957bc470 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -261,7 +261,8 @@ SOURCES += \ src/messages/MessageContainer.cpp \ src/debug/Benchmark.cpp \ src/common/UsernameSet.cpp \ - src/widgets/settingspages/AdvancedPage.cpp + src/widgets/settingspages/AdvancedPage.cpp \ + src/util/IncognitoBrowser.cpp HEADERS += \ src/Application.hpp \ @@ -462,7 +463,8 @@ HEADERS += \ src/widgets/helper/Button.hpp \ src/messages/MessageContainer.hpp \ src/common/UsernameSet.hpp \ - src/widgets/settingspages/AdvancedPage.hpp + src/widgets/settingspages/AdvancedPage.hpp \ + src/util/IncognitoBrowser.hpp RESOURCES += \ resources/resources.qrc \ diff --git a/src/main.cpp b/src/main.cpp index 4bf6e7daf..0bff94e0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "RunGui.hpp" #include "singletons/Paths.hpp" #include "singletons/Settings.hpp" +#include "util/IncognitoBrowser.hpp" #include #include @@ -11,9 +12,6 @@ using namespace chatterino; int main(int argc, char **argv) { - auto shared = std::make_shared(); - log(std::atomic_is_lock_free(&shared)); - QApplication a(argc, argv); // convert char** to QStringList diff --git a/src/nullableptr.h b/src/nullableptr.h new file mode 100644 index 000000000..f38ecfa5d --- /dev/null +++ b/src/nullableptr.h @@ -0,0 +1,59 @@ +#pragma once + +namespace chatterino { + +template +class NullablePtr +{ +public: + NullablePtr() + : element_(nullptr) + { + } + + NullablePtr(T *element) + : element_(element) + { + } + + T *operator->() const + { + assert(this->hasElement()); + + return element_; + } + + T &operator*() const + { + assert(this->hasElement()); + + return *element_; + } + + T *get() const + { + assert(this->hasElement()); + + return this->element_; + } + + bool isNull() const + { + return this->element_ == nullptr; + } + + bool hasElement() const + { + return this->element_ != nullptr; + } + + operator bool() const + { + return this->hasElement(); + } + +private: + T *element_; +}; + +} // namespace chatterino diff --git a/src/util/IncognitoBrowser.cpp b/src/util/IncognitoBrowser.cpp new file mode 100644 index 000000000..341478c74 --- /dev/null +++ b/src/util/IncognitoBrowser.cpp @@ -0,0 +1,74 @@ +#include "incognitobrowser.hpp" + +#include +#include +#include + +#include "debug/Log.hpp" + +#ifdef Q_OS_WIN + +namespace chatterino { +namespace { + QString getAppPath(const QString &executableName) + { + 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 + }; + + for (const auto &path : paths) { + auto val = QSettings(path, QSettings::NativeFormat) + .value("Default") + .toString(); + if (!val.isNull()) { + return val; + } + } + + return QString(); + } +} // namespace + +void openLinkIncognito(const QString &link) +{ + auto browserChoice = QSettings("HKEY_CURRENT_" + "USER\\Software\\Microsoft\\Windows\\Shell\\" + "Associations\\UrlAssociatio" + "ns\\http\\UserChoice", + QSettings::NativeFormat) + .value("Progid") + .toString(); + + if (!browserChoice.isNull()) { + if (browserChoice == "FirefoxURL") { + // Firefox + auto path = getAppPath("firefox.exe"); + + QProcess::startDetached(path, {"-private-window", link}); + } else if (browserChoice == "ChromeHTML") { + // Chrome + auto path = getAppPath("chrome.exe"); + + 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}); + } + */ + } +} + +} // namespace chatterino + +#endif diff --git a/src/util/IncognitoBrowser.hpp b/src/util/IncognitoBrowser.hpp new file mode 100644 index 000000000..bfc489507 --- /dev/null +++ b/src/util/IncognitoBrowser.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +#ifdef Q_OS_WIN + +// only supported on windows right now +# define INCOGNITO_LINKS_SUPPORTED + +namespace chatterino { +void openLinkIncognito(const QString &link); +} + +#endif diff --git a/src/util/rangealgorithm.hpp b/src/util/rangealgorithm.hpp new file mode 100644 index 000000000..e5c0527b7 --- /dev/null +++ b/src/util/rangealgorithm.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +namespace chatterino { +namespace util { + +template +typename Container::iterator find_if(Container &container, UnaryPredicate pred) +{ + return std::find_if(container.begin(), container.end(), pred); +} + +template +bool any_of(Container &container, UnaryPredicate pred) +{ + return std::any_of(container.begin(), container.end(), pred); +} + +} // namespace util +} // namespace chatterino diff --git a/src/widgets/dialogs/LoginDialog.hpp b/src/widgets/dialogs/LoginDialog.hpp index d9363727d..c2efa926c 100644 --- a/src/widgets/dialogs/LoginDialog.hpp +++ b/src/widgets/dialogs/LoginDialog.hpp @@ -3,19 +3,19 @@ #include "widgets/BaseWidget.hpp" #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace chatterino {