|
| 1 | +// SPDX-FileCopyrightText: Copyright (C) Kushview, LLC. |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +#include <juce_core/system/juce_TargetPlatform.h> |
| 5 | + |
| 6 | +#if JUCE_WINDOWS |
| 7 | + |
| 8 | +#include <element/application.hpp> |
| 9 | +#include "log.hpp" |
| 10 | + |
| 11 | +#ifndef WIN32_LEAN_AND_MEAN |
| 12 | + #define WIN32_LEAN_AND_MEAN |
| 13 | +#endif |
| 14 | +#ifndef NOMINMAX |
| 15 | + #define NOMINMAX |
| 16 | +#endif |
| 17 | +#include <windows.h> |
| 18 | + |
| 19 | +using namespace juce; |
| 20 | + |
| 21 | +namespace element { |
| 22 | + |
| 23 | +void Application::registerURLSchemeHandler() |
| 24 | +{ |
| 25 | + const auto exePath = File::getSpecialLocation (File::currentExecutableFile).getFullPathName(); |
| 26 | + const auto cmd = "\"" + exePath + "\" \"%1\""; |
| 27 | + |
| 28 | + HKEY hkey; |
| 29 | + DWORD disposition; |
| 30 | + const wchar_t* keyPath = L"Software\\Classes\\element"; |
| 31 | + |
| 32 | + // Create the element key |
| 33 | + if (::RegCreateKeyExW (HKEY_CURRENT_USER, keyPath, 0, nullptr, 0, KEY_WRITE, nullptr, &hkey, &disposition) == ERROR_SUCCESS) |
| 34 | + { |
| 35 | + // Set default value to "URL:Element Protocol" |
| 36 | + ::RegSetValueExW (hkey, nullptr, 0, REG_SZ, (const BYTE*)L"URL:Element Protocol", sizeof (wchar_t) * 20); |
| 37 | + |
| 38 | + // Set "URL Protocol" value to empty string |
| 39 | + ::RegSetValueExW (hkey, L"URL Protocol", 0, REG_SZ, (const BYTE*)L"", sizeof (wchar_t)); |
| 40 | + |
| 41 | + ::RegCloseKey (hkey); |
| 42 | + } |
| 43 | + |
| 44 | + // Create the shell\open\command subkey |
| 45 | + const std::wstring fullPath = std::wstring (keyPath) + L"\\shell\\open\\command"; |
| 46 | + if (::RegCreateKeyExW (HKEY_CURRENT_USER, fullPath.c_str(), 0, nullptr, 0, KEY_WRITE, nullptr, &hkey, &disposition) == ERROR_SUCCESS) |
| 47 | + { |
| 48 | + const std::wstring cmdW = cmd.toWideCharPointer(); |
| 49 | + ::RegSetValueExW (hkey, nullptr, 0, REG_SZ, (const BYTE*)cmdW.c_str(), (DWORD)((cmdW.length() + 1) * sizeof (wchar_t))); |
| 50 | + ::RegCloseKey (hkey); |
| 51 | + } |
| 52 | + |
| 53 | + Logger::writeToLog ("URL scheme handler (Windows Registry) registered"); |
| 54 | +} |
| 55 | + |
| 56 | +void Application::unregisterURLSchemeHandler() |
| 57 | +{ |
| 58 | + ::RegDeleteTreeW (HKEY_CURRENT_USER, L"Software\\Classes\\element"); |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace element |
| 62 | + |
| 63 | +#endif |
0 commit comments