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.

167 lines
6.8 KiB

  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 (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. #include <nlohmann/json.hpp>
  25. #include <database/database_lib_settings.h>
  26. #include <settings/parameters.h>
  27. #include <wildcards_and_files_ext.h>
  28. const int dblibSchemaVersion = 1;
  29. DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) :
  30. JSON_SETTINGS( aFilename, SETTINGS_LOC::NONE, dblibSchemaVersion )
  31. {
  32. m_params.emplace_back( new PARAM<std::string>( "source.dsn", &m_Source.dsn, "" ) );
  33. m_params.emplace_back( new PARAM<std::string>( "source.username", &m_Source.username, "" ) );
  34. m_params.emplace_back( new PARAM<std::string>( "source.password", &m_Source.password, "" ) );
  35. m_params.emplace_back(
  36. new PARAM<std::string>( "source.connection_string", &m_Source.connection_string, "" ) );
  37. m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
  38. m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
  39. "libraries",
  40. [&]() -> nlohmann::json
  41. {
  42. // TODO: implement this; libraries are read-only from KiCad at the moment
  43. return {};
  44. },
  45. [&]( const nlohmann::json aObj )
  46. {
  47. m_Tables.clear();
  48. if( !aObj.is_array() )
  49. return;
  50. for( const nlohmann::json& entry : aObj )
  51. {
  52. if( entry.empty() || !entry.is_object() )
  53. continue;
  54. DATABASE_LIB_TABLE table;
  55. table.name = entry["name"].get<std::string>();
  56. table.table = entry["table"].get<std::string>();
  57. table.key_col = entry["key"].get<std::string>();
  58. table.symbols_col = entry["symbols"].get<std::string>();
  59. table.footprints_col = entry["footprints"].get<std::string>();
  60. if( entry.contains( "properties" ) && entry["properties"].is_object() )
  61. {
  62. const nlohmann::json& pj = entry["properties"];
  63. table.properties.description = fetchOrDefault<std::string>( pj, "description" );
  64. table.properties.footprint_filters =
  65. fetchOrDefault<std::string>( pj, "footprint_filters" );
  66. table.properties.keywords = fetchOrDefault<std::string>( pj, "keywords" );
  67. table.properties.exclude_from_bom =
  68. fetchOrDefault<std::string>( pj, "exclude_from_bom" );
  69. table.properties.exclude_from_board =
  70. fetchOrDefault<std::string>( pj, "exclude_from_board" );
  71. }
  72. if( entry.contains( "fields" ) && entry["fields"].is_array() )
  73. {
  74. for( const nlohmann::json& fieldJson : entry["fields"] )
  75. {
  76. if( fieldJson.empty() || !fieldJson.is_object() )
  77. continue;
  78. std::string column = fetchOrDefault<std::string>( fieldJson, "column" );
  79. std::string name = fetchOrDefault<std::string>( fieldJson, "name" );
  80. bool visible_on_add = fetchOrDefault<bool>( fieldJson, "visible_on_add" );
  81. bool visible_in_chooser =
  82. fetchOrDefault<bool>( fieldJson, "visible_in_chooser" );
  83. bool show_name = fetchOrDefault<bool>( fieldJson, "show_name" );
  84. bool inherit = fetchOrDefault<bool>( fieldJson, "inherit_properties" );
  85. table.fields.emplace_back(
  86. DATABASE_FIELD_MAPPING(
  87. {
  88. column, name, visible_on_add, visible_in_chooser, show_name,
  89. inherit
  90. } ) );
  91. }
  92. }
  93. m_Tables.emplace_back( std::move( table ) );
  94. }
  95. },
  96. {} ) );
  97. m_params.emplace_back( new PARAM<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
  98. m_params.emplace_back( new PARAM<int>( "cache.max_age", &m_Cache.max_age, 10 ) );
  99. registerMigration( 0, 1,
  100. [&]() -> bool
  101. {
  102. /*
  103. * Schema 0 -> 1
  104. * Move internal symbol properties from fields with special names to
  105. * a separate place in the schema.
  106. */
  107. if( !Contains( "libraries" ) || !At( "libraries" ).is_array() )
  108. return true;
  109. for( nlohmann::json& library : At( "libraries" ) )
  110. {
  111. if( !library.contains( "fields" ) )
  112. continue;
  113. for( const nlohmann::json& field : library["fields"] )
  114. {
  115. if( !field.contains( "name" ) || !field.contains( "column" ) )
  116. continue;
  117. std::string name = field["name"].get<std::string>();
  118. std::string col = field["column"].get<std::string>();
  119. if( name == "ki_description" )
  120. library["properties"]["description"] = col;
  121. else if( name == "ki_fp_filters" )
  122. library["properties"]["footprint_filters"] = col;
  123. }
  124. }
  125. return true;
  126. } );
  127. }
  128. wxString DATABASE_LIB_SETTINGS::getFileExt() const
  129. {
  130. return DatabaseLibraryFileExtension;
  131. }