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 (C) 2022 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 <json_common.h>
26
28#include <settings/parameters.h>
30
31
32const int dblibSchemaVersion = 1;
33
34
35DATABASE_FIELD_MAPPING::DATABASE_FIELD_MAPPING( std::string aColumn, std::string aName,
36 bool aVisibleOnAdd, bool aVisibleInChooser,
37 bool aShowName, bool aInheritProperties ) :
38 column( aColumn ),
39 name( aName ), name_wx( aName.c_str(), wxConvUTF8 ), visible_on_add( aVisibleOnAdd ),
40 visible_in_chooser( aVisibleInChooser ), show_name( aShowName ),
41 inherit_properties( aInheritProperties )
42{
43}
44
45
46DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) :
48{
49
50 m_params.emplace_back( new PARAM<std::string>( "source.dsn", &m_Source.dsn, "" ) );
51
52 m_params.emplace_back( new PARAM<std::string>( "source.username", &m_Source.username, "" ) );
53
54 m_params.emplace_back( new PARAM<std::string>( "source.password", &m_Source.password, "" ) );
55
56 m_params.emplace_back(
57 new PARAM<std::string>( "source.connection_string", &m_Source.connection_string, "" ) );
58
59 m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
60
61 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
62 "libraries",
63 [&]() -> nlohmann::json
64 {
65 // TODO: implement this; libraries are read-only from KiCad at the moment
66 return {};
67 },
68 [&]( const nlohmann::json aObj )
69 {
70 m_Tables.clear();
71
72 if( !aObj.is_array() )
73 return;
74
75 for( const nlohmann::json& entry : aObj )
76 {
77 if( entry.empty() || !entry.is_object() )
78 continue;
79
80 DATABASE_LIB_TABLE table;
81
82 table.name = entry["name"].get<std::string>();
83 table.table = entry["table"].get<std::string>();
84 table.key_col = entry["key"].get<std::string>();
85 table.symbols_col = entry["symbols"].get<std::string>();
86 table.footprints_col = entry["footprints"].get<std::string>();
87
88 // Sanitize library display names; currently only `/` is removed because we use it
89 // as a separator and allow it in symbol names.
90 table.name.erase( std::remove( table.name.begin(), table.name.end(), '/' ),
91 table.name.end() );
92
93 if( entry.contains( "properties" ) && entry["properties"].is_object() )
94 {
95 const nlohmann::json& pj = entry["properties"];
96
97 table.properties.description = fetchOrDefault<std::string>( pj, "description" );
98
99 table.properties.footprint_filters =
100 fetchOrDefault<std::string>( pj, "footprint_filters" );
101
102 table.properties.keywords = fetchOrDefault<std::string>( pj, "keywords" );
103
104 table.properties.exclude_from_bom =
105 fetchOrDefault<std::string>( pj, "exclude_from_bom" );
106
107 table.properties.exclude_from_board =
108 fetchOrDefault<std::string>( pj, "exclude_from_board" );
109
110 table.properties.exclude_from_sim =
111 fetchOrDefault<std::string>( pj, "exclude_from_sim" );
112 }
113
114 if( entry.contains( "fields" ) && entry["fields"].is_array() )
115 {
116 for( const nlohmann::json& fieldJson : entry["fields"] )
117 {
118 if( fieldJson.empty() || !fieldJson.is_object() )
119 continue;
120
121 std::string column = fetchOrDefault<std::string>( fieldJson, "column" );
122 std::string name = fetchOrDefault<std::string>( fieldJson, "name" );
123 bool visible_on_add = fetchOrDefault<bool>( fieldJson, "visible_on_add" );
124 bool visible_in_chooser =
125 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" );
126 bool show_name = fetchOrDefault<bool>( fieldJson, "show_name" );
127 bool inherit = fetchOrDefault<bool>( fieldJson, "inherit_properties" );
128
129 table.fields.emplace_back(
130 DATABASE_FIELD_MAPPING( column, name, visible_on_add,
131 visible_in_chooser, show_name, inherit ) );
132 }
133 }
134
135 m_Tables.emplace_back( std::move( table ) );
136 }
137 },
138 {} ) );
139
140 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
141
142 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
143
144 registerMigration( 0, 1,
145 [&]() -> bool
146 {
147 /*
148 * Schema 0 -> 1
149 * Move internal symbol properties from fields with special names to
150 * a separate place in the schema.
151 */
152 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
153 return true;
154
155 for( nlohmann::json& library : At( "libraries" ) )
156 {
157 if( !library.contains( "fields" ) )
158 continue;
159
160 for( const nlohmann::json& field : library["fields"] )
161 {
162 if( !field.contains( "name" ) || !field.contains( "column" ) )
163 continue;
164
165 std::string name = field["name"].get<std::string>();
166 std::string col = field["column"].get<std::string>();
167
168 if( name == "ki_description" )
169 library["properties"]["description"] = col;
170 else if( name == "ki_fp_filters" )
171 library["properties"]["footprint_filters"] = col;
172 }
173 }
174
175 return true;
176 } );
177}
178
179
181{
183}
const char * name
Definition: DXF_plotter.cpp:57
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
static const std::string DatabaseLibraryFileExtension
SETTINGS_LOC
Definition: json_settings.h:54
@ NONE
No connection to this item.
DATABASE_FIELD_MAPPING(std::string aColumn, std::string aName, bool aVisibleOnAdd, bool aVisibleInChooser, bool aShowName, bool aInheritProperties)
std::string connection_string
Definition of file extensions used in Kicad.