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
111 if( entry.contains( "fields" ) && entry["fields"].is_array() )
112 {
113 for( const nlohmann::json& fieldJson : entry["fields"] )
114 {
115 if( fieldJson.empty() || !fieldJson.is_object() )
116 continue;
117
118 std::string column = fetchOrDefault<std::string>( fieldJson, "column" );
119 std::string name = fetchOrDefault<std::string>( fieldJson, "name" );
120 bool visible_on_add = fetchOrDefault<bool>( fieldJson, "visible_on_add" );
121 bool visible_in_chooser =
122 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" );
123 bool show_name = fetchOrDefault<bool>( fieldJson, "show_name" );
124 bool inherit = fetchOrDefault<bool>( fieldJson, "inherit_properties" );
125
126 table.fields.emplace_back(
127 DATABASE_FIELD_MAPPING( column, name, visible_on_add,
128 visible_in_chooser, show_name, inherit ) );
129 }
130 }
131
132 m_Tables.emplace_back( std::move( table ) );
133 }
134 },
135 {} ) );
136
137 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
138
139 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
140
141 registerMigration( 0, 1,
142 [&]() -> bool
143 {
144 /*
145 * Schema 0 -> 1
146 * Move internal symbol properties from fields with special names to
147 * a separate place in the schema.
148 */
149 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
150 return true;
151
152 for( nlohmann::json& library : At( "libraries" ) )
153 {
154 if( !library.contains( "fields" ) )
155 continue;
156
157 for( const nlohmann::json& field : library["fields"] )
158 {
159 if( !field.contains( "name" ) || !field.contains( "column" ) )
160 continue;
161
162 std::string name = field["name"].get<std::string>();
163 std::string col = field["column"].get<std::string>();
164
165 if( name == "ki_description" )
166 library["properties"]["description"] = col;
167 else if( name == "ki_fp_filters" )
168 library["properties"]["footprint_filters"] = col;
169 }
170 }
171
172 return true;
173 } );
174}
175
176
178{
180}
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:293
const int dblibSchemaVersion
static const std::string DatabaseLibraryFileExtension
SETTINGS_LOC
Definition: json_settings.h:54
@ NONE
Definition: kibis.h:54
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.