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.

124 lines
4.1 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-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef SCH_IO_DATABASE_H_
  21. #define SCH_IO_DATABASE_H_
  22. #include <database/database_connection.h>
  23. #include <sch_io/sch_io.h>
  24. #include <sch_io/sch_io_mgr.h>
  25. #include <wildcards_and_files_ext.h>
  26. class DATABASE_LIB_SETTINGS;
  27. struct DATABASE_LIB_TABLE;
  28. /**
  29. * A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins
  30. * on both the symbol and footprint side of things that expose the database contents to the
  31. * schematic and board editors. The architecture of these is slightly different from the other
  32. * plugins because the backing file is just a configuration file rather than something that
  33. * contains symbol or footprint data.
  34. */
  35. class SCH_IO_DATABASE : public SCH_IO
  36. {
  37. public:
  38. SCH_IO_DATABASE();
  39. virtual ~SCH_IO_DATABASE();
  40. const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override
  41. {
  42. return IO_BASE::IO_FILE_DESC( _HKI( "KiCad database library files" ),
  43. { FILEEXT::DatabaseLibraryFileExtension } );
  44. }
  45. int GetModifyHash() const override { return 0; }
  46. void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
  47. const wxString& aLibraryPath,
  48. const STRING_UTF8_MAP* aProperties = nullptr ) override;
  49. void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
  50. const wxString& aLibraryPath,
  51. const STRING_UTF8_MAP* aProperties = nullptr ) override;
  52. LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
  53. const STRING_UTF8_MAP* aProperties = nullptr ) override;
  54. bool SupportsSubLibraries() const override { return true; }
  55. void GetSubLibraryNames( std::vector<wxString>& aNames ) override;
  56. void GetAvailableSymbolFields( std::vector<wxString>& aNames ) override;
  57. void GetDefaultSymbolFields( std::vector<wxString>& aNames ) override;
  58. // Database libraries can never be written using the symbol editing API
  59. bool IsLibraryWritable( const wxString& aLibraryPath ) override { return false; }
  60. void SetLibTable( SYMBOL_LIB_TABLE* aTable ) override
  61. {
  62. m_libTable = aTable;
  63. }
  64. DATABASE_LIB_SETTINGS* Settings() const { return m_settings.get(); }
  65. bool TestConnection( wxString* aErrorMsg = nullptr );
  66. private:
  67. void cacheLib();
  68. void ensureSettings( const wxString& aSettingsPath );
  69. void ensureConnection();
  70. void connect();
  71. std::unique_ptr<LIB_SYMBOL> loadSymbolFromRow( const wxString& aSymbolName,
  72. const DATABASE_LIB_TABLE& aTable,
  73. const DATABASE_CONNECTION::ROW& aRow );
  74. static std::optional<bool> boolFromAny( const std::any& aVal );
  75. SYMBOL_LIB_TABLE* m_libTable;
  76. std::unique_ptr<DATABASE_LIB_SETTINGS> m_settings;
  77. /// Generally will be null if no valid connection is established
  78. std::unique_ptr<DATABASE_CONNECTION> m_conn;
  79. std::set<wxString> m_customFields;
  80. std::set<wxString> m_defaultShownFields;
  81. std::map<wxString, std::unique_ptr<LIB_SYMBOL>> m_nameToSymbolcache;
  82. long long m_cacheTimestamp;
  83. int m_cacheModifyHash;
  84. wxString m_lastError;
  85. };
  86. #endif //SCH_IO_DATABASE_H_