7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-20 00:21:31 +00:00

API: Add a preferred plugin settings path

Fixes https://gitlab.com/kicad/code/kicad/-/issues/9054
This commit is contained in:
Jon Evans 2024-12-28 16:20:49 -05:00
parent 6e437fe3d6
commit 2c94684f9f
5 changed files with 71 additions and 4 deletions

View File

@ -71,3 +71,20 @@ message GetTextAsShapesResponse
{
repeated TextWithShapes text_with_shapes = 1;
}
// Return a writeable path that a plugin can use for storing persistent data such as configuration
// files, etc. This path may not yet exist; actual creation of the directory for a given plugin is
// up to the plugin itself. Files in this path will not be modified if the plugin is uninstalled or
// upgraded.
//
// Returns StringResponse
message GetPluginSettingsPath
{
// The identifier of the plugin
string identifier = 1;
}
message StringResponse
{
string response = 1;
}

View File

@ -26,7 +26,9 @@
#include <eda_text.h>
#include <geometry/shape_compound.h>
#include <google/protobuf/empty.pb.h>
#include <paths.h>
#include <pgm_base.h>
#include <api/api_plugin.h>
#include <api/api_utils.h>
#include <project/net_settings.h>
#include <project/project_file.h>
@ -49,6 +51,9 @@ API_HANDLER_COMMON::API_HANDLER_COMMON() :
&API_HANDLER_COMMON::handleGetTextAsShapes );
registerHandler<ExpandTextVariables, ExpandTextVariablesResponse>(
&API_HANDLER_COMMON::handleExpandTextVariables );
registerHandler<GetPluginSettingsPath, StringResponse>(
&API_HANDLER_COMMON::handleGetPluginSettingsPath );
}
@ -223,3 +228,38 @@ HANDLER_RESULT<ExpandTextVariablesResponse> API_HANDLER_COMMON::handleExpandText
return reply;
}
HANDLER_RESULT<StringResponse> API_HANDLER_COMMON::handleGetPluginSettingsPath(
const HANDLER_CONTEXT<GetPluginSettingsPath>& aCtx )
{
wxString identifier = wxString::FromUTF8( aCtx.Request.identifier() );
if( identifier.IsEmpty() )
{
ApiResponseStatus e;
e.set_status( ApiStatusCode::AS_BAD_REQUEST );
e.set_error_message( "plugin identifier is missing" );
return tl::unexpected( e );
}
if( API_PLUGIN::IsValidIdentifier( identifier ) )
{
ApiResponseStatus e;
e.set_status( ApiStatusCode::AS_BAD_REQUEST );
e.set_error_message( "plugin identifier is invalid" );
return tl::unexpected( e );
}
wxFileName path( PATHS::GetUserSettingsPath(), wxEmptyString );
path.AppendDir( "plugins" );
// Create the base plugins path if needed, but leave the specific plugin to create its own path
PATHS::EnsurePathExists( path.GetPath() );
path.AppendDir( identifier );
StringResponse reply;
reply.set_response( path.GetPath() );
return reply;
}

View File

@ -115,10 +115,7 @@ API_PLUGIN_CONFIG::API_PLUGIN_CONFIG( API_PLUGIN& aParent, const wxFileName& aCo
return;
}
// At minimum, we need a reverse-DNS style identifier with two dots and a 2+ character TLD
wxRegEx identifierRegex( wxS( "[\\w\\d]{2,}\\.[\\w\\d]+\\.[\\w\\d]+" ) );
if( !identifierRegex.Matches( identifier ) )
if( !API_PLUGIN::IsValidIdentifier( identifier ) )
{
wxLogTrace( traceApi, wxString::Format( "Plugin: identifier %s does not meet requirements",
identifier ) );
@ -172,6 +169,14 @@ bool API_PLUGIN::IsOk() const
}
bool API_PLUGIN::IsValidIdentifier( const wxString& aIdentifier )
{
// At minimum, we need a reverse-DNS style identifier with two dots and a 2+ character TLD
wxRegEx identifierRegex( wxS( "[\\w\\d]{2,}\\.[\\w\\d]+\\.[\\w\\d]+" ) );
return identifierRegex.Matches( aIdentifier );
}
const wxString& API_PLUGIN::Identifier() const
{
return m_config->identifier;

View File

@ -54,6 +54,9 @@ private:
HANDLER_RESULT<commands::ExpandTextVariablesResponse> handleExpandTextVariables(
const HANDLER_CONTEXT<commands::ExpandTextVariables>& aCtx );
HANDLER_RESULT<commands::StringResponse> handleGetPluginSettingsPath(
const HANDLER_CONTEXT<commands::GetPluginSettingsPath>& aCtx );
};
#endif //KICAD_API_HANDLER_COMMON_H

View File

@ -110,6 +110,8 @@ public:
bool IsOk() const;
static bool IsValidIdentifier( const wxString& aIdentifier );
const wxString& Identifier() const;
const wxString& Name() const;
const wxString& Description() const;