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