KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_lib_settings.cpp
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
8* modify it under the terms of the GNU General Public License
9* as published by the Free Software Foundation; either version 2
10* of the License, or (at your option) any later version.
11*
12* This program is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15* GNU 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, you may find one here:
19* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20* or you may search the http://www.gnu.org website for the version 2 license,
21* or you may write to the Free Software Foundation, Inc.,
22* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23*/
24
25#include <core/kicad_algo.h>
26#include <json_common.h>
27#include <algorithm>
28
30#include <settings/parameters.h>
32
33
34const int dblibSchemaVersion = 1;
35
36
37DATABASE_FIELD_MAPPING::DATABASE_FIELD_MAPPING( const std::string& aColumn, const std::string& aName,
38 bool aVisibleOnAdd, bool aVisibleInChooser,
39 bool aShowName, bool aInheritProperties ) :
40 column( aColumn ),
41 name( aName ),
42 name_wx( aName.c_str(), wxConvUTF8 ),
43 visible_on_add( aVisibleOnAdd ),
44 visible_in_chooser( aVisibleInChooser ),
45 show_name( aShowName ),
46 inherit_properties( aInheritProperties )
47{
48}
49
50
51DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) :
53{
54
55 m_params.emplace_back( new PARAM<std::string>( "source.dsn", &m_Source.dsn, "" ) );
56
57 m_params.emplace_back( new PARAM<std::string>( "source.username", &m_Source.username, "" ) );
58
59 m_params.emplace_back( new PARAM<std::string>( "source.password", &m_Source.password, "" ) );
60
61 m_params.emplace_back( new PARAM<std::string>( "source.connection_string",
63
64 m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
65
66 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
67 "libraries",
68 [&]() -> nlohmann::json
69 {
70 // TODO: implement this; libraries are read-only from KiCad at the moment
71 return {};
72 },
73 [&]( const nlohmann::json aObj )
74 {
75 m_Tables.clear();
76
77 if( !aObj.is_array() )
78 return;
79
80 for( const nlohmann::json& entry : aObj )
81 {
82 if( entry.empty() || !entry.is_object() )
83 continue;
84
85 DATABASE_LIB_TABLE table;
86
87 table.name = entry["name"].get<std::string>();
88 table.table = entry["table"].get<std::string>();
89 table.key_col = entry["key"].get<std::string>();
90 table.symbols_col = entry["symbols"].get<std::string>();
91 table.footprints_col = entry["footprints"].get<std::string>();
92
93 // Sanitize library display names; currently only `/` is removed because we
94 // use it as a separator and allow it in symbol names.
95 std::erase( table.name, '/' );
96
97 if( entry.contains( "properties" ) && entry["properties"].is_object() )
98 {
99 const nlohmann::json& propJson = entry["properties"];
100
101 table.properties.description =
102 fetchOrDefault<std::string>( propJson, "description" );
103
104 table.properties.footprint_filters =
105 fetchOrDefault<std::string>( propJson, "footprint_filters" );
106
107 table.properties.keywords =
108 fetchOrDefault<std::string>( propJson, "keywords" );
109
110 table.properties.exclude_from_bom =
111 fetchOrDefault<std::string>( propJson, "exclude_from_bom" );
112
113 table.properties.exclude_from_board =
114 fetchOrDefault<std::string>( propJson, "exclude_from_board" );
115
116 table.properties.exclude_from_sim =
117 fetchOrDefault<std::string>( propJson, "exclude_from_sim" );
118 }
119
120 if( entry.contains( "fields" ) && entry["fields"].is_array() )
121 {
122 for( const nlohmann::json& fieldJson : entry["fields"] )
123 {
124 if( fieldJson.empty() || !fieldJson.is_object() )
125 continue;
126
127 table.fields.emplace_back( DATABASE_FIELD_MAPPING(
128 fetchOrDefault<std::string>( fieldJson, "column" ),
129 fetchOrDefault<std::string>( fieldJson, "name" ),
130 fetchOrDefault<bool>( fieldJson, "visible_on_add" ),
131 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" ),
132 fetchOrDefault<bool>( fieldJson, "show_name" ),
133 fetchOrDefault<bool>( fieldJson, "inherit_properties" ) ) );
134 }
135 }
136
137 m_Tables.emplace_back( std::move( table ) );
138 }
139 },
140 {} ) );
141
142 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
143
144 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
145
146 registerMigration( 0, 1,
147 [&]() -> bool
148 {
149 /*
150 * Schema 0 -> 1
151 * Move internal symbol properties from fields with special names to
152 * a separate place in the schema.
153 */
154 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
155 return true;
156
157 for( nlohmann::json& library : At( "libraries" ) )
158 {
159 if( !library.contains( "fields" ) )
160 continue;
161
162 for( const nlohmann::json& field : library["fields"] )
163 {
164 if( !field.contains( "name" ) || !field.contains( "column" ) )
165 continue;
166
167 std::string name = field["name"].get<std::string>();
168 std::string col = field["column"].get<std::string>();
169
170 if( name == "ki_description" )
171 library["properties"]["description"] = col;
172 else if( name == "ki_fp_filters" )
173 library["properties"]["footprint_filters"] = col;
174 }
175 }
176
177 return true;
178 } );
179}
180
181
183{
185}
const char * name
Definition: DXF_plotter.cpp:62
DATABASE_LIB_SETTINGS(const std::string &aFilename)
wxString getFileExt() const override
std::vector< DATABASE_LIB_TABLE > m_Tables
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:295
const int dblibSchemaVersion
@ NONE
Definition: eda_shape.h:69
static const std::string DatabaseLibraryFileExtension
SETTINGS_LOC
Definition: json_settings.h:54
DATABASE_FIELD_MAPPING(const std::string &aColumn, const std::string &aName, bool aVisibleOnAdd, bool aVisibleInChooser, bool aShowName, bool aInheritProperties)
std::string connection_string
Definition of file extensions used in Kicad.