You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 Jon Evans <jon@craftyjon.com>
  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. #include <core/kicad_algo.h>
  25. #include <json_common.h>
  26. #include <database/database_lib_settings.h>
  27. #include <settings/parameters.h>
  28. #include <wildcards_and_files_ext.h>
  29. const int dblibSchemaVersion = 1;
  30. DATABASE_FIELD_MAPPING::DATABASE_FIELD_MAPPING( std::string aColumn, std::string aName,
  31. bool aVisibleOnAdd, bool aVisibleInChooser,
  32. bool aShowName, bool aInheritProperties ) :
  33. column( aColumn ),
  34. name( aName ),
  35. name_wx( aName.c_str(), wxConvUTF8 ),
  36. visible_on_add( aVisibleOnAdd ),
  37. visible_in_chooser( aVisibleInChooser ),
  38. show_name( aShowName ),
  39. inherit_properties( aInheritProperties )
  40. {
  41. }
  42. DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) :
  43. JSON_SETTINGS( aFilename, SETTINGS_LOC::NONE, dblibSchemaVersion )
  44. {
  45. m_params.emplace_back( new PARAM<std::string>( "source.dsn", &m_Source.dsn, "" ) );
  46. m_params.emplace_back( new PARAM<std::string>( "source.username", &m_Source.username, "" ) );
  47. m_params.emplace_back( new PARAM<std::string>( "source.password", &m_Source.password, "" ) );
  48. m_params.emplace_back( new PARAM<std::string>( "source.connection_string",
  49. &m_Source.connection_string, "" ) );
  50. m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
  51. m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
  52. "libraries",
  53. [&]() -> nlohmann::json
  54. {
  55. // TODO: implement this; libraries are read-only from KiCad at the moment
  56. return {};
  57. },
  58. [&]( const nlohmann::json aObj )
  59. {
  60. m_Tables.clear();
  61. if( !aObj.is_array() )
  62. return;
  63. for( const nlohmann::json& entry : aObj )
  64. {
  65. if( entry.empty() || !entry.is_object() )
  66. continue;
  67. DATABASE_LIB_TABLE table;
  68. table.name = entry["name"].get<std::string>();
  69. table.table = entry["table"].get<std::string>();
  70. table.key_col = entry["key"].get<std::string>();
  71. table.symbols_col = entry["symbols"].get<std::string>();
  72. table.footprints_col = entry["footprints"].get<std::string>();
  73. // Sanitize library display names; currently only `/` is removed because we
  74. // use it as a separator and allow it in symbol names.
  75. alg::delete_matching( table.name, '/' );
  76. if( entry.contains( "properties" ) && entry["properties"].is_object() )
  77. {
  78. const nlohmann::json& propJson = entry["properties"];
  79. table.properties.description =
  80. fetchOrDefault<std::string>( propJson, "description" );
  81. table.properties.footprint_filters =
  82. fetchOrDefault<std::string>( propJson, "footprint_filters" );
  83. table.properties.keywords =
  84. fetchOrDefault<std::string>( propJson, "keywords" );
  85. table.properties.exclude_from_bom =
  86. fetchOrDefault<std::string>( propJson, "exclude_from_bom" );
  87. table.properties.exclude_from_board =
  88. fetchOrDefault<std::string>( propJson, "exclude_from_board" );
  89. table.properties.exclude_from_sim =
  90. fetchOrDefault<std::string>( propJson, "exclude_from_sim" );
  91. }
  92. if( entry.contains( "fields" ) && entry["fields"].is_array() )
  93. {
  94. for( const nlohmann::json& fieldJson : entry["fields"] )
  95. {
  96. if( fieldJson.empty() || !fieldJson.is_object() )
  97. continue;
  98. table.fields.emplace_back( DATABASE_FIELD_MAPPING(
  99. fetchOrDefault<std::string>( fieldJson, "column" ),
  100. fetchOrDefault<std::string>( fieldJson, "name" ),
  101. fetchOrDefault<bool>( fieldJson, "visible_on_add" ),
  102. fetchOrDefault<bool>( fieldJson, "visible_in_chooser" ),
  103. fetchOrDefault<bool>( fieldJson, "show_name" ),
  104. fetchOrDefault<bool>( fieldJson, "inherit_properties" ) ) );
  105. }
  106. }
  107. m_Tables.emplace_back( std::move( table ) );
  108. }
  109. },
  110. {} ) );
  111. m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
  112. m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
  113. registerMigration( 0, 1,
  114. [&]() -> bool
  115. {
  116. /*
  117. * Schema 0 -> 1
  118. * Move internal symbol properties from fields with special names to
  119. * a separate place in the schema.
  120. */
  121. if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
  122. return true;
  123. for( nlohmann::json& library : At( "libraries" ) )
  124. {
  125. if( !library.contains( "fields" ) )
  126. continue;
  127. for( const nlohmann::json& field : library["fields"] )
  128. {
  129. if( !field.contains( "name" ) || !field.contains( "column" ) )
  130. continue;
  131. std::string name = field["name"].get<std::string>();
  132. std::string col = field["column"].get<std::string>();
  133. if( name == "ki_description" )
  134. library["properties"]["description"] = col;
  135. else if( name == "ki_fp_filters" )
  136. library["properties"]["footprint_filters"] = col;
  137. }
  138. }
  139. return true;
  140. } );
  141. }
  142. wxString DATABASE_LIB_SETTINGS::getFileExt() const
  143. {
  144. return FILEEXT::DatabaseLibraryFileExtension;
  145. }