From b0fa2561b69f2c0a9f655102a9c1d4b9e2b95959 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Thu, 13 Jul 2023 18:58:36 -0400 Subject: [PATCH] Add support for enum based policies --- common/dialogs/panel_data_collection.cpp | 6 +- common/pgm_base.cpp | 14 ++-- kicad/dialogs/panel_kicad_launcher.cpp | 4 +- kicad/kicad_manager_frame.cpp | 2 +- kicad/menubar.cpp | 4 +- kicad/tools/kicad_manager_control.cpp | 4 +- libs/kiplatform/gtk/policy.cpp | 11 +++- libs/kiplatform/include/kiplatform/policy.h | 13 +++- libs/kiplatform/msw/policy.cpp | 72 +++++++++++++++++---- libs/kiplatform/osx/policy.mm | 11 +++- 10 files changed, 107 insertions(+), 34 deletions(-) diff --git a/common/dialogs/panel_data_collection.cpp b/common/dialogs/panel_data_collection.cpp index 5ffe069abd..5e6ace5250 100644 --- a/common/dialogs/panel_data_collection.cpp +++ b/common/dialogs/panel_data_collection.cpp @@ -47,9 +47,9 @@ bool PANEL_DATA_COLLECTION::TransferDataToWindow() { applySettingsToPanel(); - KIPLATFORM::POLICY::STATE policyState = - KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_DATACOLLECTION ); - if( policyState != KIPLATFORM::POLICY::STATE::NOT_CONFIGURED ) + KIPLATFORM::POLICY::PBOOL policyState = + KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_DATACOLLECTION ); + if( policyState != KIPLATFORM::POLICY::PBOOL::NOT_CONFIGURED ) { Disable(); } diff --git a/common/pgm_base.cpp b/common/pgm_base.cpp index a0ab68321e..d533673c90 100644 --- a/common/pgm_base.cpp +++ b/common/pgm_base.cpp @@ -243,11 +243,11 @@ const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEdit #ifdef KICAD_USE_SENTRY bool PGM_BASE::IsSentryOptedIn() { - KIPLATFORM::POLICY::STATE policyState = - KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_DATACOLLECTION ); - if( policyState != KIPLATFORM::POLICY::STATE::NOT_CONFIGURED ) + KIPLATFORM::POLICY::PBOOL policyState = + KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_DATACOLLECTION ); + if( policyState != KIPLATFORM::POLICY::PBOOL::NOT_CONFIGURED ) { - return policyState == KIPLATFORM::POLICY::STATE::ENABLED; + return policyState == KIPLATFORM::POLICY::PBOOL::ENABLED; } return m_sentry_optin_fn.Exists(); @@ -354,10 +354,10 @@ void PGM_BASE::sentryPrompt() if( !IsGUI() ) return; - KIPLATFORM::POLICY::STATE policyState = - KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_DATACOLLECTION ); + KIPLATFORM::POLICY::PBOOL policyState = + KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_DATACOLLECTION ); - if( policyState == KIPLATFORM::POLICY::STATE::NOT_CONFIGURED + if( policyState == KIPLATFORM::POLICY::PBOOL::NOT_CONFIGURED && !m_settings_manager->GetCommonSettings()->m_DoNotShowAgain.data_collection_prompt ) { wxMessageDialog optIn = wxMessageDialog( diff --git a/kicad/dialogs/panel_kicad_launcher.cpp b/kicad/dialogs/panel_kicad_launcher.cpp index 3b09061f1c..980f99b5fa 100644 --- a/kicad/dialogs/panel_kicad_launcher.cpp +++ b/kicad/dialogs/panel_kicad_launcher.cpp @@ -162,8 +162,8 @@ void PANEL_KICAD_LAUNCHER::CreateLaunchers() addLauncher( KICAD_MANAGER_ACTIONS::showPluginManager, KiScaledBitmap( BITMAPS::icon_pcm, this, 48, true ), _( "Manage downloadable packages from KiCad and 3rd party repositories" ), - ( KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_PCM ) - != KIPLATFORM::POLICY::STATE::DISABLED ) ); + ( KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_PCM ) + != KIPLATFORM::POLICY::PBOOL::DISABLED ) ); m_frame->SetPcmButton( bb ); diff --git a/kicad/kicad_manager_frame.cpp b/kicad/kicad_manager_frame.cpp index 92684beb85..984b2d77ef 100644 --- a/kicad/kicad_manager_frame.cpp +++ b/kicad/kicad_manager_frame.cpp @@ -917,7 +917,7 @@ void KICAD_MANAGER_FRAME::OnIdle( wxIdleEvent& aEvent ) } } - if( KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_PCM ) != KIPLATFORM::POLICY::STATE::DISABLED + if( KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_PCM ) != KIPLATFORM::POLICY::PBOOL::DISABLED && settings->m_PcmUpdateCheck ) { m_pcm->RunBackgroundUpdate(); diff --git a/kicad/menubar.cpp b/kicad/menubar.cpp index 542f464eb7..f41bedf5a0 100644 --- a/kicad/menubar.cpp +++ b/kicad/menubar.cpp @@ -177,8 +177,8 @@ void KICAD_MANAGER_FRAME::doReCreateMenuBar() wxMenuItem* pcmMenuItem = toolsMenu->Add( KICAD_MANAGER_ACTIONS::showPluginManager ); - if( KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_PCM ) - == KIPLATFORM::POLICY::STATE::DISABLED ) + if( KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_PCM ) + == KIPLATFORM::POLICY::PBOOL::DISABLED ) { pcmMenuItem->Enable( false ); } diff --git a/kicad/tools/kicad_manager_control.cpp b/kicad/tools/kicad_manager_control.cpp index 526c945d86..9856330ad4 100644 --- a/kicad/tools/kicad_manager_control.cpp +++ b/kicad/tools/kicad_manager_control.cpp @@ -883,8 +883,8 @@ int KICAD_MANAGER_CONTROL::Execute( const TOOL_EVENT& aEvent ) int KICAD_MANAGER_CONTROL::ShowPluginManager( const TOOL_EVENT& aEvent ) { - if( KIPLATFORM::POLICY::GetPolicyState( POLICY_KEY_PCM ) - == KIPLATFORM::POLICY::STATE::DISABLED ) + if( KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_PCM ) + == KIPLATFORM::POLICY::PBOOL::DISABLED ) { // policy disables the plugin manager return 0; diff --git a/libs/kiplatform/gtk/policy.cpp b/libs/kiplatform/gtk/policy.cpp index 50f33b5769..47956e3998 100644 --- a/libs/kiplatform/gtk/policy.cpp +++ b/libs/kiplatform/gtk/policy.cpp @@ -22,7 +22,14 @@ #include -KIPLATFORM::POLICY::STATE KIPLATFORM::POLICY::GetPolicyState( const wxString& aKey ) + +KIPLATFORM::POLICY::PBOOL KIPLATFORM::POLICY::GetPolicyBool( const wxString& aKey ) { - return STATE::NOT_CONFIGURED; + return PBOOL::NOT_CONFIGURED; +} + + +std::uint32_t KIPLATFORM::POLICY::GetPolicyEnumUInt( const wxString& aKey ) +{ + return 0; } \ No newline at end of file diff --git a/libs/kiplatform/include/kiplatform/policy.h b/libs/kiplatform/include/kiplatform/policy.h index 9ee3abd774..2a4796f6a0 100644 --- a/libs/kiplatform/include/kiplatform/policy.h +++ b/libs/kiplatform/include/kiplatform/policy.h @@ -22,20 +22,29 @@ #ifndef KIPLATFORM_POLICY_H_ #define KIPLATFORM_POLICY_H_ +#include + class wxString; namespace KIPLATFORM { namespace POLICY { - enum class STATE + enum class PBOOL { ENABLED, DISABLED, NOT_CONFIGURED }; - STATE GetPolicyState( const wxString& aKey ); + PBOOL GetPolicyBool( const wxString& aKey ); + std::uint32_t GetPolicyEnumUInt( const wxString& aKey ); + + template + T GetPolicyEnum( const wxString& aKey ) + { + return static_cast( GetPolicyEnumUInt( aKey ) ); + } } } diff --git a/libs/kiplatform/msw/policy.cpp b/libs/kiplatform/msw/policy.cpp index 2f7cc7f7eb..5521c1856f 100644 --- a/libs/kiplatform/msw/policy.cpp +++ b/libs/kiplatform/msw/policy.cpp @@ -21,41 +21,91 @@ #include #include +#include #include +#include + #define POLICY_KEY_ROOT "Software\\Policies\\KiCad\\KiCad" -KIPLATFORM::POLICY::STATE KIPLATFORM::POLICY::GetPolicyState( const wxString& aKey ) +static wxRegKey* GetPolicyRegKey( wxString& aKey ) { + wxString key = aKey; wxRegKey* keyToUse = nullptr; - wxRegKey userKey( wxRegKey::HKCU, POLICY_KEY_ROOT ); - wxRegKey compKey( wxRegKey::HKLM, POLICY_KEY_ROOT ); + + wxString keyPath = POLICY_KEY_ROOT; + + wxStringTokenizer tokenizer( aKey, "\\" ); + while( tokenizer.HasMoreTokens() ) + { + wxString token = tokenizer.GetNextToken(); + + if( tokenizer.HasMoreTokens() ) + { + keyPath.Append( "\\" ); + keyPath.Append( token ); + } + else + key = token; + } + + std::unique_ptr userKey = std::make_unique( wxRegKey::HKCU, keyPath ); // we have user level policies take precedence over computer level policies - if( userKey.Exists() && userKey.HasValue( aKey ) ) + if( userKey->Exists() && userKey->HasValue( key ) ) { - keyToUse = &userKey; + keyToUse = userKey.release(); } else { - if( compKey.Exists() && compKey.HasValue( aKey ) ) + std::unique_ptr compKey = std::make_unique( wxRegKey::HKLM, keyPath ); + + if( compKey->Exists() && compKey->HasValue( key ) ) { - keyToUse = &compKey; + keyToUse = compKey.release(); } } + aKey = key; + return keyToUse; +} + + +KIPLATFORM::POLICY::PBOOL KIPLATFORM::POLICY::GetPolicyBool( const wxString& aKey ) +{ + wxString key = aKey; + std::unique_ptr keyToUse( GetPolicyRegKey( key ) ); + if( keyToUse != nullptr ) { long value; - if( keyToUse->QueryValue( aKey, &value ) ) + if( keyToUse->QueryValue( key, &value ) ) { if( value == 1 ) - return POLICY::STATE::ENABLED; + return POLICY::PBOOL::ENABLED; else - return POLICY::STATE::DISABLED; + return POLICY::PBOOL::DISABLED; } } - return STATE::NOT_CONFIGURED; + return PBOOL::NOT_CONFIGURED; +} + + +std::uint32_t KIPLATFORM::POLICY::GetPolicyEnumUInt( const wxString& aKey ) +{ + wxString key = aKey; + std::unique_ptr keyToUse( GetPolicyRegKey( key ) ); + + if( keyToUse != nullptr ) + { + long value; + if( keyToUse->QueryValue( key, &value ) ) + { + return value; + } + } + + return 0; } \ No newline at end of file diff --git a/libs/kiplatform/osx/policy.mm b/libs/kiplatform/osx/policy.mm index 50f33b5769..47956e3998 100644 --- a/libs/kiplatform/osx/policy.mm +++ b/libs/kiplatform/osx/policy.mm @@ -22,7 +22,14 @@ #include -KIPLATFORM::POLICY::STATE KIPLATFORM::POLICY::GetPolicyState( const wxString& aKey ) + +KIPLATFORM::POLICY::PBOOL KIPLATFORM::POLICY::GetPolicyBool( const wxString& aKey ) { - return STATE::NOT_CONFIGURED; + return PBOOL::NOT_CONFIGURED; +} + + +std::uint32_t KIPLATFORM::POLICY::GetPolicyEnumUInt( const wxString& aKey ) +{ + return 0; } \ No newline at end of file