KiCad PCB EDA Suite
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 if( entry.contains( "properties" ) && entry["properties"].is_object() )
78 {
79 const nlohmann::json& pj = entry["properties"];
80
81 table.properties.description = fetchOrDefault<std::string>( pj, "description" );
82
83 table.properties.footprint_filters =
84 fetchOrDefault<std::string>( pj, "footprint_filters" );
85
86 table.properties.keywords = fetchOrDefault<std::string>( pj, "keywords" );
87
88 table.properties.exclude_from_bom =
89 fetchOrDefault<std::string>( pj, "exclude_from_bom" );
90
91 table.properties.exclude_from_board =
92 fetchOrDefault<std::string>( pj, "exclude_from_board" );
93 }
94
95 if( entry.contains( "fields" ) && entry["fields"].is_array() )
96 {
97 for( const nlohmann::json& fieldJson : entry["fields"] )
98 {
99 if( fieldJson.empty() || !fieldJson.is_object() )
100 continue;
101
102 std::string column = fetchOrDefault<std::string>( fieldJson, "column" );
103 std::string name = fetchOrDefault<std::string>( fieldJson, "name" );
104 bool visible_on_add = fetchOrDefault<bool>( fieldJson, "visible_on_add" );
105 bool visible_in_chooser =
106 fetchOrDefault<bool>( fieldJson, "visible_in_chooser" );
107 bool show_name = fetchOrDefault<bool>( fieldJson, "show_name" );
108 bool inherit = fetchOrDefault<bool>( fieldJson, "inherit_properties" );
109
110 table.fields.emplace_back(
111 DATABASE_FIELD_MAPPING(
112 {
113 column, name, visible_on_add, visible_in_chooser, show_name,
114 inherit
115 } ) );
116 }
117 }
118
119 m_Tables.emplace_back( std::move( table ) );
120 }
121 },
122 {} ) );
123
124 m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
125
126 m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
127
128 registerMigration( 0, 1,
129 [&]() -> bool
130 {
131 /*
132 * Schema 0 -> 1
133 * Move internal symbol properties from fields with special names to
134 * a separate place in the schema.
135 */
136 if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
137 return true;
138
139 for( nlohmann::json& library : At( "libraries" ) )
140 {
141 if( !library.contains( "fields" ) )
142 continue;
143
144 for( const nlohmann::json& field : library["fields"] )
145 {
146 if( !field.contains( "name" ) || !field.contains( "column" ) )
147 continue;
148
149 std::string name = field["name"].get<std::string>();
150 std::string col = field["column"].get<std::string>();
151
152 if( name == "ki_description" )
153 library["properties"]["description"] = col;
154 else if( name == "ki_fp_filters" )
155 library["properties"]["footprint_filters"] = col;
156 }
157 }
158
159 return true;
160 } );
161}
162
163
165{
167}
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)
const int dblibSchemaVersion
nlohmann::json json
Definition: gerbview.cpp:44
const std::string DatabaseLibraryFileExtension
SETTINGS_LOC
Definition: json_settings.h:47
@ NONE
Definition: kibis.h:53
std::string connection_string
Definition of file extensions used in Kicad.