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
18 * along with this program. If not, see <https://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 <vector>
28#include <nlohmann/json_fwd.hpp>
29#include <nlohmann/json-schema.hpp>
30#include <tl/expected.hpp>
31#include <wx/bmpbndl.h>
32#include <wx/filename.h>
33#include <wx/image.h>
34#include <wx/string.h>
35
37#include <kicommon.h>
38
39
41class API_PLUGIN;
43
44
46{
47 wxString package_name;
48 wxString version;
49};
50
51
58
59
61{
62 tl::expected<bool, wxString> FromJson( const nlohmann::json& aJson );
63
65 wxString min_version;
66 std::vector<PLUGIN_DEPENDENCY> dependencies;
67};
68
69
74{
75 PLUGIN_ACTION( const API_PLUGIN& aPlugin ) :
76 plugin( aPlugin )
77 {}
78
79 wxString identifier;
80 wxString name;
81 wxString description;
82 bool show_button = false;
83 wxString entrypoint;
84 std::set<PLUGIN_ACTION_SCOPE> scopes;
85 std::vector<wxString> args;
86 std::vector<wxImage> icon_light;
87 std::vector<wxImage> icon_dark;
88
90};
91
98{
99public:
100 API_PLUGIN( const wxFileName& aConfigFile, const JSON_SCHEMA_VALIDATOR& aValidator );
101
102 ~API_PLUGIN();
103
104 bool IsOk() const;
105
106 const wxString& ErrorMessage() const;
107
108 static bool IsValidIdentifier( const wxString& aIdentifier );
109
110 const wxString& Identifier() const;
111 const wxString& Name() const;
112 const wxString& Description() const;
113 const PLUGIN_RUNTIME& Runtime() const;
114 wxString BasePath() const;
115
116 const std::vector<PLUGIN_ACTION>& Actions() const;
117
118 wxString ActionSettingsKey( const PLUGIN_ACTION& aAction ) const;
119
120private:
121 friend struct API_PLUGIN_CONFIG;
122
123 std::optional<PLUGIN_ACTION> createActionFromJson( const nlohmann::json& aJson );
124
125 wxFileName m_configFile;
126
127 std::unique_ptr<API_PLUGIN_CONFIG> m_config;
128};
129
134{
135 bool operator()( const std::unique_ptr<API_PLUGIN>& item1,
136 const std::unique_ptr<API_PLUGIN>& item2 ) const
137 {
138 return item1->Identifier() < item2->Identifier();
139 }
140};
141
142
143class LOGGING_ERROR_HANDLER : public nlohmann::json_schema::error_handler
144{
145public:
147
148 bool HasError() const { return m_hasError; }
149
150 const wxString& ErrorMessage() const { return m_errorMessage; }
151
152 void error( const nlohmann::json::json_pointer& ptr, const nlohmann::json& instance,
153 const std::string& message ) override;
154
155private:
158};
159
160#endif //KICAD_API_PLUGIN_H
PLUGIN_RUNTIME_TYPE
Definition api_plugin.h:53
A plugin that is invoked by KiCad and runs as an external process; communicating with KiCad via the I...
Definition api_plugin.h:98
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:121
wxString ActionSettingsKey(const PLUGIN_ACTION &aAction) const
const wxString & Identifier() const
std::unique_ptr< API_PLUGIN_CONFIG > m_config
Definition api_plugin.h:127
wxFileName m_configFile
Definition api_plugin.h:125
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
Code outside of kicommon must validate through this class rather than instantiating nlohmann::json_sc...
bool HasError() const
Definition api_plugin.h:148
const wxString & ErrorMessage() const
Definition api_plugin.h:150
void error(const nlohmann::json::json_pointer &ptr, const nlohmann::json &instance, const std::string &message) override
std::string message
#define KICOMMON_API
Definition kicommon.h:27
Comparison functor for ensuring API_PLUGINs have unique identifiers.
Definition api_plugin.h:134
bool operator()(const std::unique_ptr< API_PLUGIN > &item1, const std::unique_ptr< API_PLUGIN > &item2) const
Definition api_plugin.h:135
An action performed by a plugin via the IPC API.
Definition api_plugin.h:74
const API_PLUGIN & plugin
Definition api_plugin.h:89
wxString name
Definition api_plugin.h:80
wxString description
Definition api_plugin.h:81
std::set< PLUGIN_ACTION_SCOPE > scopes
Definition api_plugin.h:84
wxString identifier
Definition api_plugin.h:79
std::vector< wxImage > icon_light
Definition api_plugin.h:86
wxString entrypoint
Definition api_plugin.h:83
std::vector< wxImage > icon_dark
Definition api_plugin.h:87
std::vector< wxString > args
Definition api_plugin.h:85
PLUGIN_ACTION(const API_PLUGIN &aPlugin)
Definition api_plugin.h:75
wxString package_name
Definition api_plugin.h:47
wxString min_version
Definition api_plugin.h:65
tl::expected< bool, wxString > FromJson(const nlohmann::json &aJson)
PLUGIN_RUNTIME_TYPE type
Definition api_plugin.h:64
std::vector< PLUGIN_DEPENDENCY > dependencies
Definition api_plugin.h:66