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.

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