KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_plugin.h
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) 2024 Jon Evans <[email protected]>
5 * Copyright The 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#ifndef KICAD_API_PLUGIN_H
22#define KICAD_API_PLUGIN_H
23
24#include <memory>
25#include <optional>
26#include <set>
27#include <nlohmann/json_fwd.hpp>
28#include <nlohmann/json-schema.hpp>
29#include <tl/expected.hpp>
30#include <wx/bmpbndl.h>
31#include <wx/filename.h>
32#include <wx/string.h>
33
35#include <kicommon.h>
36
37
39class API_PLUGIN;
41
42
44{
45 wxString package_name;
46 wxString version;
47};
48
49
56
57
59{
60 tl::expected<bool, wxString> FromJson( const nlohmann::json& aJson );
61
63 wxString min_version;
64 std::vector<PLUGIN_DEPENDENCY> dependencies;
65};
66
67
72{
73 PLUGIN_ACTION( const API_PLUGIN& aPlugin ) :
74 plugin( aPlugin )
75 {}
76
77 wxString identifier;
78 wxString name;
79 wxString description;
80 bool show_button = false;
81 wxString entrypoint;
82 std::set<PLUGIN_ACTION_SCOPE> scopes;
83 std::vector<wxString> args;
84 wxBitmapBundle icon_light;
85 wxBitmapBundle icon_dark;
86
88};
89
96{
97public:
98 API_PLUGIN( const wxFileName& aConfigFile, const JSON_SCHEMA_VALIDATOR& aValidator );
99
100 ~API_PLUGIN();
101
102 bool IsOk() const;
103
104 const wxString& ErrorMessage() const;
105
106 static bool IsValidIdentifier( const wxString& aIdentifier );
107
108 const wxString& Identifier() const;
109 const wxString& Name() const;
110 const wxString& Description() const;
111 const PLUGIN_RUNTIME& Runtime() const;
112 wxString BasePath() const;
113
114 const std::vector<PLUGIN_ACTION>& Actions() const;
115
116 wxString ActionSettingsKey( const PLUGIN_ACTION& aAction ) const;
117
118private:
119 friend struct API_PLUGIN_CONFIG;
120
121 std::optional<PLUGIN_ACTION> createActionFromJson( const nlohmann::json& aJson );
122
123 wxFileName m_configFile;
124
125 std::unique_ptr<API_PLUGIN_CONFIG> m_config;
126};
127
132{
133 bool operator()( const std::unique_ptr<API_PLUGIN>& item1,
134 const std::unique_ptr<API_PLUGIN>& item2 ) const
135 {
136 return item1->Identifier() < item2->Identifier();
137 }
138};
139
140
141class LOGGING_ERROR_HANDLER : public nlohmann::json_schema::error_handler
142{
143public:
145
146 bool HasError() const { return m_hasError; }
147
148 const wxString& ErrorMessage() const { return m_errorMessage; }
149
150 void error( const nlohmann::json::json_pointer& ptr, const nlohmann::json& instance,
151 const std::string& message ) override;
152
153private:
156};
157
158#endif //KICAD_API_PLUGIN_H
PLUGIN_RUNTIME_TYPE
Definition api_plugin.h:51
A plugin that is invoked by KiCad and runs as an external process; communicating with KiCad via the I...
Definition api_plugin.h:96
const PLUGIN_RUNTIME & Runtime() const
const wxString & Name() const
const std::vector< PLUGIN_ACTION > & Actions() const
friend struct API_PLUGIN_CONFIG
Definition api_plugin.h:119
wxString ActionSettingsKey(const PLUGIN_ACTION &aAction) const
const wxString & Identifier() const
std::unique_ptr< API_PLUGIN_CONFIG > m_config
Definition api_plugin.h:125
wxFileName m_configFile
Definition api_plugin.h:123
bool IsOk() const
wxString BasePath() const
static bool IsValidIdentifier(const wxString &aIdentifier)
const wxString & Description() const
API_PLUGIN(const wxFileName &aConfigFile, const JSON_SCHEMA_VALIDATOR &aValidator)
std::optional< PLUGIN_ACTION > createActionFromJson(const nlohmann::json &aJson)
const wxString & ErrorMessage() const
bool HasError() const
Definition api_plugin.h:146
const wxString & ErrorMessage() const
Definition api_plugin.h:148
void error(const nlohmann::json::json_pointer &ptr, const nlohmann::json &instance, const std::string &message) override
#define KICOMMON_API
Definition kicommon.h:27
Comparison functor for ensuring API_PLUGINs have unique identifiers.
Definition api_plugin.h:132
bool operator()(const std::unique_ptr< API_PLUGIN > &item1, const std::unique_ptr< API_PLUGIN > &item2) const
Definition api_plugin.h:133
An action performed by a plugin via the IPC API.
Definition api_plugin.h:72
wxBitmapBundle icon_light
Definition api_plugin.h:84
const API_PLUGIN & plugin
Definition api_plugin.h:87
wxString name
Definition api_plugin.h:78
wxString description
Definition api_plugin.h:79
std::set< PLUGIN_ACTION_SCOPE > scopes
Definition api_plugin.h:82
wxString identifier
Definition api_plugin.h:77
wxString entrypoint
Definition api_plugin.h:81
wxBitmapBundle icon_dark
Definition api_plugin.h:85
std::vector< wxString > args
Definition api_plugin.h:83
PLUGIN_ACTION(const API_PLUGIN &aPlugin)
Definition api_plugin.h:73
wxString package_name
Definition api_plugin.h:45
wxString min_version
Definition api_plugin.h:63
tl::expected< bool, wxString > FromJson(const nlohmann::json &aJson)
PLUGIN_RUNTIME_TYPE type
Definition api_plugin.h:62
std::vector< PLUGIN_DEPENDENCY > dependencies
Definition api_plugin.h:64