KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_lib_settings.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) 2022 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_DATABASE_LIB_SETTINGS_H
22#define KICAD_DATABASE_LIB_SETTINGS_H
23
24#include <json_common.h>
25#include <functional>
27#include <wx/string.h>
28
29#include <map>
30#include <memory>
31
32
40
41
43{
45 std::string dsn;
46 std::string username;
47 std::string password;
48 int timeout = 0;
49 std::string connection_string;
50 std::string path;
51 std::string type_name;
52
54 DATABASE_SOURCE Expanded( const wxString& aBaseDir ) const;
55
62 std::string Describe() const;
63};
64
65
67{
68 std::string column;
69 std::string name;
70 wxString name_wx;
73 bool show_name;
75 nlohmann::json source;
76
78
79 explicit DATABASE_FIELD_MAPPING( const std::string& aColumn, const std::string& aName,
80 bool aVisibleOnAdd, bool aVisibleInChooser, bool aShowName,
81 bool aInheritProperties );
82
83 void SetName( const std::string& aName );
84};
85
86
88{
89 std::string description;
90 std::string footprint_filters;
91 std::string keywords;
92 std::string exclude_from_sim;
93 std::string exclude_from_bom;
94 std::string exclude_from_board;
95
96 bool operator==( const MAPPABLE_SYMBOL_PROPERTIES& aOther ) const = default;
97};
98
99
114{
115 std::string name;
116 std::string table;
117 std::string key_col;
118 std::string symbols_col;
119 std::string footprints_col;
120 std::string pins_col;
121
123 std::vector<DATABASE_FIELD_MAPPING> fields;
124
125 nlohmann::json source;
126};
127
128
134
135
139using DATABASE_SECRET_LOOKUP = std::function<bool( const wxString& aService, const wxString& aKey, wxString& aSecret )>;
140
142{
143public:
144 DATABASE_LIB_SETTINGS( const std::string& aFilename );
145
147
149
150 std::vector<DATABASE_LIB_TABLE> m_Tables;
151
153
155
156 std::string m_Name;
157 std::string m_Description;
158
160 void Load() override;
161
166 bool Store() override;
167
172 void ResolvePassword( const DATABASE_SECRET_LOOKUP& aLookup = nullptr );
173
178 DATABASE_SOURCE ExpandedSource( const wxString& aBaseDir = wxEmptyString ) const;
179
181 wxString GetLoadPath() const;
182
183protected:
184 wxString getFileExt() const override;
185
186private:
187 static nlohmann::json tableToJson( const DATABASE_LIB_TABLE& aTable );
189 static DATABASE_LIB_TABLE tableFromJson( const nlohmann::json& aEntry );
190 static nlohmann::json fieldToJson( const DATABASE_FIELD_MAPPING& aField );
191 static DATABASE_FIELD_MAPPING fieldFromJson( const nlohmann::json& aEntry );
192
202};
203
204
212KICOMMON_API std::unique_ptr<DATABASE_LIB_SETTINGS> LoadDatabaseLibSettingsReadOnly( const wxString& aPath,
213 bool* aLoaded = nullptr );
214
219KICOMMON_API wxString DefaultDatabaseLibFileName( const wxString& aLibName );
220
225KICOMMON_API std::string DatabasePathForStorage( const wxString& aPath, const wxString& aBaseDir );
226
231KICOMMON_API wxString DatabasePathFromInput( const wxString& aInput, const wxString& aBaseDir );
232
234inline constexpr int DEFAULT_DATABASE_TIMEOUT_SECONDS = 2;
235
240inline int DatabaseFileWaitSeconds( int aTimeoutSeconds )
241{
242 return aTimeoutSeconds > 0 ? aTimeoutSeconds + 10 : 30;
243}
244
249
254
262KICOMMON_API bool GetDatabasePassword( const DATABASE_SOURCE& aSource, std::string& aPassword,
263 const DATABASE_SECRET_LOOKUP& aLookup = nullptr );
264
269 const DATABASE_SECRET_LOOKUP& aLookup = nullptr );
270
275KICOMMON_API bool StoreDatabasePassword( const DATABASE_SOURCE& aSource, const std::string& aPassword );
276
277#endif //KICAD_DATABASE_LIB_SETTINGS_H
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
Read a model file and creates a generic display structure.
static DATABASE_LIB_TABLE tableFromJson(const nlohmann::json &aEntry)
Missing or mistyped keys load as defaults so one incomplete entry cannot stop the others.
void ResolvePassword(const DATABASE_SECRET_LOOKUP &aLookup=nullptr)
Fills in the source password from the keyring when the file has none.
DATABASE_LIB_SETTINGS(const std::string &aFilename)
wxString getFileExt() const override
static DATABASE_FIELD_MAPPING fieldFromJson(const nlohmann::json &aEntry)
DATABASE_SOURCE ExpandedSource(const wxString &aBaseDir=wxEmptyString) const
bool Store() override
Stores parameters, then restores the raw file value of every scalar the caller left unchanged so that...
std::vector< DATABASE_LIB_TABLE > m_Tables
static nlohmann::json tableToJson(const DATABASE_LIB_TABLE &aTable)
DATABASE_CACHE_SETTINGS m_Cache
static nlohmann::json fieldToJson(const DATABASE_FIELD_MAPPING &aField)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
KICOMMON_API bool StoreDatabasePassword(const DATABASE_SOURCE &aSource, const std::string &aPassword)
Stores the password for the source's DSN login in the system keyring, or removes the entry when the p...
KICOMMON_API wxString DatabasePathFromInput(const wxString &aInput, const wxString &aBaseDir)
KICOMMON_API void ResolveDatabasePassword(DATABASE_SOURCE &aSource, const DATABASE_SECRET_LOOKUP &aLookup=nullptr)
Fills in the source password from the keyring, but only when the library file did not supply one.
KICOMMON_API wxString DefaultDatabaseLibFileName(const wxString &aLibName)
KICOMMON_API DATABASE_SECRET_LOOKUP DefaultDatabaseSecretLookup()
The lookup used when none is injected, which must never prompt to unlock the keyring.
DATABASE_SOURCE_TYPE
std::function< bool(const wxString &aService, const wxString &aKey, wxString &aSecret)> DATABASE_SECRET_LOOKUP
Keyring lookup signature, matching KIPLATFORM::SECRETS::GetSecretNoPrompt.
KICOMMON_API bool GetDatabasePassword(const DATABASE_SOURCE &aSource, std::string &aPassword, const DATABASE_SECRET_LOOKUP &aLookup=nullptr)
DSN passwords that are not saved in the (often shared) library file live in the system keyring,...
constexpr int DEFAULT_DATABASE_TIMEOUT_SECONDS
The source timeout a library file gets when it sets none.
KICOMMON_API bool IsNoPromptSecretLookup(const DATABASE_SECRET_LOOKUP &aLookup)
KICOMMON_API std::unique_ptr< DATABASE_LIB_SETTINGS > LoadDatabaseLibSettingsReadOnly(const wxString &aPath, bool *aLoaded=nullptr)
Loads a database library settings file without allowing the load itself (e.g.
int DatabaseFileWaitSeconds(int aTimeoutSeconds)
KICOMMON_API std::string DatabasePathForStorage(const wxString &aPath, const wxString &aBaseDir)
#define KICOMMON_API
Definition kicommon.h:27
int max_age
Max age of a cached table before it expires, in seconds.
int max_size
Maximum number of tables to cache, each holding the rows read from it.
bool visible_in_chooser
Whether the column is shown by default in the chooser.
std::string column
Database column name.
std::string name
KiCad field name.
void SetName(const std::string &aName)
bool inherit_properties
Whether or not to inherit properties from symbol field.
bool visible_on_add
Whether to show the field when placing the symbol.
bool show_name
Whether or not to show the field name as well as its value.
nlohmann::json source
Raw JSON as loaded; untouched keys are written back verbatim.
wxString name_wx
KiCad field name (converted)
Scalar settings as parsed at load, so Store() can tell which ones the caller changed.
A database library table will be mapped to a sub-library provided by the database library entry in th...
std::string key_col
Unique key column name (will form part of the LIB_ID)
std::string name
KiCad library nickname (will form part of the LIB_ID)
nlohmann::json source
Raw JSON as loaded; untouched keys are written back verbatim.
std::string symbols_col
Column name containing KiCad symbol refs.
std::string footprints_col
Column name containing KiCad footprint refs.
std::vector< DATABASE_FIELD_MAPPING > fields
std::string table
Database table to pull content from.
MAPPABLE_SYMBOL_PROPERTIES properties
std::string pins_col
Column name containing JSON pin assignments (optional)
std::string type_name
Raw source.type as written, kept for the error when the type is INVALID.
DATABASE_SOURCE Expanded(const wxString &aBaseDir) const
std::string Describe() const
std::string path
Database file of a SQLITE or ACCESS source.
std::string connection_string
DATABASE_SOURCE_TYPE type
bool operator==(const MAPPABLE_SYMBOL_PROPERTIES &aOther) const =default