KiCad PCB EDA Suite
Loading...
Searching...
No Matches
windows/policy.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright (C) 2022 Mark Roszko <[email protected]>
5* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
6*
7* This program is free software: you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the
9* Free Software Foundation, either version 3 of the License, or (at your
10* option) any later version.
11*
12* This program is distributed in the hope that it will be useful, but
13* WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License along
18* with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <kiplatform/policy.h>
22
23#include <wx/string.h>
24#include <wx/tokenzr.h>
25#include <wx/msw/registry.h>
26
27#include <memory>
28
29#define POLICY_KEY_ROOT "Software\\Policies\\KiCad\\KiCad"
30
31
32static wxRegKey* GetPolicyRegKey( wxString& aKey )
33{
34 wxString key = aKey;
35 wxRegKey* keyToUse = nullptr;
36
37 wxString keyPath = POLICY_KEY_ROOT;
38
39 wxStringTokenizer tokenizer( aKey, "\\" );
40 while( tokenizer.HasMoreTokens() )
41 {
42 wxString token = tokenizer.GetNextToken();
43
44 if( tokenizer.HasMoreTokens() )
45 {
46 keyPath.Append( "\\" );
47 keyPath.Append( token );
48 }
49 else
50 key = token;
51 }
52
53 std::unique_ptr<wxRegKey> userKey = std::make_unique<wxRegKey>( wxRegKey::HKCU, keyPath );
54
55 // we have user level policies take precedence over computer level policies
56 if( userKey->Exists() && userKey->HasValue( key ) )
57 {
58 keyToUse = userKey.release();
59 }
60 else
61 {
62 std::unique_ptr<wxRegKey> compKey = std::make_unique<wxRegKey>( wxRegKey::HKLM, keyPath );
63
64 if( compKey->Exists() && compKey->HasValue( key ) )
65 {
66 keyToUse = compKey.release();
67 }
68 }
69
70 aKey = key;
71 return keyToUse;
72}
73
74
76{
77 wxString key = aKey;
78 std::unique_ptr<wxRegKey> keyToUse( GetPolicyRegKey( key ) );
79
80 if( keyToUse != nullptr )
81 {
82 long value;
83 if( keyToUse->QueryValue( key, &value ) )
84 {
85 if( value == 1 )
86 return POLICY::PBOOL::ENABLED;
87 else
88 return POLICY::PBOOL::DISABLED;
89 }
90 }
91
92 return PBOOL::NOT_CONFIGURED;
93}
94
95
96std::uint32_t KIPLATFORM::POLICY::GetPolicyEnumUInt( const wxString& aKey )
97{
98 wxString key = aKey;
99 std::unique_ptr<wxRegKey> keyToUse( GetPolicyRegKey( key ) );
100
101 if( keyToUse != nullptr )
102 {
103 long value;
104 if( keyToUse->QueryValue( key, &value ) )
105 {
106 return value;
107 }
108 }
109
110 return 0;
111}
std::uint32_t GetPolicyEnumUInt(const wxString &aKey)
Definition: unix/policy.cpp:32
PBOOL GetPolicyBool(const wxString &aKey)
Definition: unix/policy.cpp:26
static wxRegKey * GetPolicyRegKey(wxString &aKey)
#define POLICY_KEY_ROOT