| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/*!******************************************************************** |
| 3 |
|
| 4 |
Audacity: A Digital Audio Editor |
| 5 |
|
| 6 |
SchemeRegistrar.cpp |
| 7 |
|
| 8 |
Dmitry Vedenko |
| 9 |
|
| 10 |
**********************************************************************/ |
| 11 |
|
| 12 |
#include <string_view> |
| 13 |
#include <functional> |
| 14 |
|
| 15 |
#include <windows.h> |
| 16 |
|
| 17 |
#include <wx/log.h> |
| 18 |
#include <exception> |
| 19 |
|
| 20 |
#include "CodeConversions.h" |
| 21 |
#include "URLSchemesRegistry.h" |
| 22 |
|
| 23 |
/* |
| 24 |
* |
| 25 |
* On Windows, the following registry structure is expected by the OS: |
| 26 |
* HKEY_CLASSES_ROOT |
| 27 |
%schema% |
| 28 |
(Default) = "URL:%schema% Protocol" |
| 29 |
URL Protocol = "" |
| 30 |
DefaultIcon |
| 31 |
(Default) = "%audacity.exe%,1" |
| 32 |
shell |
| 33 |
open |
| 34 |
command |
| 35 |
(Default) = "%audacity.exe" -u "%1" |
| 36 |
|
| 37 |
We will update the values every time to ensure popper operation |
| 38 |
for users with multiple Audacity installations |
| 39 |
*/ |
| 40 |
|
| 41 |
class RegistryKey final |
| 42 |
{ |
| 43 |
public: |
| 44 |
RegistryKey() |
| 45 |
{ |
| 46 |
auto result = RegOpenCurrentUser(KEY_ALL_ACCESS, &mKeyHandle); |
| 47 |
|
| 48 |
if (result != ERROR_SUCCESS) |
| 49 |
{ |
| 50 |
wxLogDebug( |
| 51 |
"Failed to create registry key: %s", wxSysErrorMsgStr(result)); |
| 52 |
throw std::exception("Failed to create registry key"); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
RegistryKey(HKEY parent, const std::wstring& name) |
| 57 |
{ |
| 58 |
auto result = |
| 59 |
RegOpenKeyExW(parent, name.c_str(), 0, KEY_ALL_ACCESS, &mKeyHandle); |
| 60 |
|
| 61 |
if (result == ERROR_FILE_NOT_FOUND) |
| 62 |
result = RegCreateKeyW(parent, name.c_str(), &mKeyHandle); |
| 63 |
|
| 64 |
if (result != ERROR_SUCCESS) |
| 65 |
{ |
| 66 |
wxLogDebug("Failed to open registry key: %s", wxSysErrorMsgStr(result)); |
| 67 |
throw std::exception("Failed to open registry key"); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
RegistryKey(const RegistryKey& parent, const std::wstring& name) |
| 72 |
: RegistryKey(parent.mKeyHandle, name) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
~RegistryKey() |
| 77 |
{ |
| 78 |
RegCloseKey(mKeyHandle); |
| 79 |
} |
| 80 |
|
| 81 |
RegistryKey(const RegistryKey&) = delete; |
| 82 |
RegistryKey& operator = (const RegistryKey&) = delete; |
| 83 |
|
| 84 |
void SetValue(const wchar_t* subKey, const std::wstring& value) |
| 85 |
{ |
| 86 |
auto result = RegSetValueExW( |
| 87 |
mKeyHandle, subKey, 0, REG_SZ, |
| 88 |
reinterpret_cast<const BYTE*>(value.c_str()), |
| 89 |
(value.size() + 1) * sizeof(wchar_t)); |
| 90 |
|
| 91 |
if (result != ERROR_SUCCESS) |
| 92 |
{ |
| 93 |
wxLogDebug( |
| 94 |
"Failed to set value to a registry key: %s", wxSysErrorMsgStr(result)); |
| 95 |
throw std::exception("Failed to set value to a registry key"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
HKEY GetHandle() const |
| 100 |
{ |
| 101 |
return mKeyHandle; |
| 102 |
} |
| 103 |
|
| 104 |
private: |
| 105 |
HKEY mKeyHandle {}; |
| 106 |
}; |
| 107 |
|
| 108 |
void SetSchemaRegistrar(std::function<bool(std::string_view)> registrar); |
| 109 |
|
| 110 |
auto registrar = ([]() { |
| 111 |
URLSchemesRegistry::Get().SetRegistrar([](std::string_view schemaView) { |
| 112 |
constexpr size_t fileNameBufferSize = MAX_PATH + 1; |
| 113 |
wchar_t filenameBuffer[fileNameBufferSize] = { 0 }; |
| 114 |
|
| 115 |
const size_t fileNameLength = |
| 116 |
GetModuleFileNameW(nullptr, filenameBuffer, fileNameBufferSize); |
| 117 |
|
| 118 |
// According to MSND - fileNameBufferSize is only returned on buffer overflow |
| 119 |
if (fileNameLength == fileNameBufferSize) |
| 120 |
return false; |
| 121 |
|
| 122 |
const std::wstring schema = audacity::ToWString(schemaView); |
| 123 |
|
| 124 |
try |
| 125 |
{ |
| 126 |
RegistryKey hkcu; |
| 127 |
|
| 128 |
//! Bug fix: Clean up conflicting registry entries from Audacity 4 alphas |
| 129 |
try |
| 130 |
{ |
| 131 |
RegistryKey capabilitiesKey(hkcu, L"Software\\Audacity 4 \\Capabilities"); |
| 132 |
RegDeleteKeyW(capabilitiesKey.GetHandle(), L"URLAssociations"); |
| 133 |
} |
| 134 |
catch (...) { } |
| 135 |
|
| 136 |
RegistryKey rootKey(hkcu, L"Software\\Classes"); |
| 137 |
|
| 138 |
RegistryKey schemaKey(rootKey, schema); |
| 139 |
schemaKey.SetValue(L"", L"URL:" + schema); |
| 140 |
schemaKey.SetValue(L"URL Protocol", L""); |
| 141 |
|
| 142 |
RegistryKey curVerKey(schemaKey, L"CurVer"); |
| 143 |
curVerKey.SetValue(L"", schema + L".Url.Audacity.3"); |
| 144 |
|
| 145 |
RegistryKey handlerKey(rootKey, schema + L".Url.Audacity.3"); |
| 146 |
|
| 147 |
const std::wstring filename(filenameBuffer); |
| 148 |
|
| 149 |
RegistryKey iconKey(handlerKey, L"DefaultIcon"); |
| 150 |
iconKey.SetValue(L"", filename + std::wstring(L",1")); |
| 151 |
|
| 152 |
RegistryKey shellKey(handlerKey, L"shell"); |
| 153 |
RegistryKey openKey(shellKey, L"open"); |
| 154 |
RegistryKey commandKey(openKey, L"command"); |
| 155 |
|
| 156 |
commandKey.SetValue(L"", L"\"" + filename + L"\" -u \"%1\""); |
| 157 |
} |
| 158 |
catch (...) |
| 159 |
{ |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
return true; |
| 164 |
}); |
| 165 |
|
| 166 |
return true; |
| 167 |
})(); |