1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4
5 #include "win32preference.h"
6 #include "../../../../lib/platform/win32/win32support.h"
7 #include "../../../../lib/platform/win32/winstring.h"
8 #include "../../../include/iappdelegate.h"
9 #include "../../../include/iapplication.h"
10
11 //------------------------------------------------------------------------
12 namespace VSTGUI {
13 namespace Standalone {
14 namespace Platform {
15 namespace Win32 {
16
17 //------------------------------------------------------------------------
Win32Preference()18 Win32Preference::Win32Preference ()
19 {
20 auto& appInfo = IApplication::instance ().getDelegate ().getInfo ();
21 vstgui_assert (!appInfo.uri.empty (), "need uri for preferences");
22 UTF8String path ("SOFTWARE\\" + appInfo.uri.getString ());
23 auto winStr = dynamic_cast<WinString*> (path.getPlatformString ());
24 vstgui_assert (winStr);
25 DWORD dw;
26 RegCreateKeyEx (HKEY_CURRENT_USER, winStr->getWideString (), 0, REG_NONE,
27 REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_READ, NULL, &hKey, &dw);
28 }
29
30 //------------------------------------------------------------------------
~Win32Preference()31 Win32Preference::~Win32Preference ()
32 {
33 RegCloseKey (hKey);
34 }
35
36 //------------------------------------------------------------------------
set(const UTF8String & key,const UTF8String & value)37 bool Win32Preference::set (const UTF8String& key, const UTF8String& value)
38 {
39 auto keyStr = dynamic_cast<WinString*> (key.getPlatformString ());
40 auto valueStr = dynamic_cast<WinString*> (value.getPlatformString ());
41 vstgui_assert (keyStr);
42 auto res = RegSetValueEx (hKey, keyStr->getWideString (), NULL, REG_SZ,
43 reinterpret_cast<const BYTE*> (valueStr->getWideString ()),
44 static_cast<DWORD> (wcslen (valueStr->getWideString ()) * 2));
45
46 return SUCCEEDED (res);
47 }
48
49 //------------------------------------------------------------------------
get(const UTF8String & key)50 Optional<UTF8String> Win32Preference::get (const UTF8String& key)
51 {
52 auto keyStr = dynamic_cast<WinString*> (key.getPlatformString ());
53 vstgui_assert (keyStr);
54
55 DWORD dwType {};
56 DWORD dwCount {};
57 if (SUCCEEDED (
58 RegQueryValueEx (hKey, keyStr->getWideString (), NULL, &dwType, nullptr, &dwCount)) &&
59 dwType == REG_SZ && dwCount > 0)
60 {
61 auto buffer = std::make_unique<uint8_t[]> (dwCount + 1);
62 if (SUCCEEDED (RegQueryValueEx (hKey, keyStr->getWideString (), NULL, &dwType,
63 buffer.get (), &dwCount)))
64 {
65 UTF8StringHelper helper (reinterpret_cast<const WCHAR*> (buffer.get ()));
66 return Optional<UTF8String> (UTF8String (helper));
67 }
68 }
69
70 return {};
71 }
72
73 //------------------------------------------------------------------------
74 } // Win32
75 } // Platform
76 } // Standalone
77 } // VSTGUI
78