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.

185 lines
6.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
  5. * Copyright (C) 2020 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. * @file filename_resolver.h
  26. */
  27. #ifndef FILENAME_RESOLVER_H
  28. #define FILENAME_RESOLVER_H
  29. #include <list>
  30. #include <map>
  31. #include <vector>
  32. #include <wx/string.h>
  33. class PGM_BASE;
  34. struct SEARCH_PATH
  35. {
  36. wxString m_Alias; // alias to the base path
  37. wxString m_Pathvar; // base path as stored in the config file
  38. wxString m_Pathexp; // expanded base path
  39. wxString m_Description; // description of the aliased path
  40. };
  41. /**
  42. * Provide an extensible class to resolve 3D model paths.
  43. *
  44. * Initially the legacy behavior will be implemented and an incomplete path would be checked
  45. * against the project directory or the KICAD6_3DMODEL_DIR environment variable. In the future a
  46. * configurable set of search paths may be specified.
  47. */
  48. class FILENAME_RESOLVER
  49. {
  50. public:
  51. FILENAME_RESOLVER();
  52. /**
  53. * Set the user's configuration directory for 3D models.
  54. *
  55. * @param aConfigDir
  56. * @return true if the call succeeds (directory exists).
  57. */
  58. bool Set3DConfigDir( const wxString& aConfigDir );
  59. /**
  60. * Set the current KiCad project directory as the first entry in the model path list.
  61. *
  62. * @param[in] aProjDir current project directory
  63. * @param[out] flgChanged optional, set to true if directory was changed
  64. * @retval true success
  65. * @retval false failure
  66. */
  67. bool SetProject( PROJECT* aProject, bool* flgChanged = nullptr );
  68. wxString GetProjectDir() const;
  69. /**
  70. * Set a pointer to the application's #PGM_BASE instance used to extract the local env vars.
  71. */
  72. void SetProgramBase( PGM_BASE* aBase );
  73. /**
  74. * Clear the current path list and substitutes the given path list and update the path
  75. * configuration file on success.
  76. */
  77. bool UpdatePathList( const std::vector<SEARCH_PATH>& aPathList );
  78. /**
  79. * Write the current path list to a config file.
  80. * @param aResolvePaths indicates whether env vars should also be written out or not
  81. */
  82. bool WritePathList( const wxString& aDir, const wxString& aFilename, bool aResolvePaths );
  83. /**
  84. * Determines the full path of the given file name.
  85. *
  86. * In the future remote files may be supported, in which case it is best to require a full
  87. * URI in which case ResolvePath should check that the URI conforms to RFC-2396 and related
  88. * documents and copies \a aFileName into aResolvedName if the URI is valid.
  89. */
  90. wxString ResolvePath( const wxString& aFileName );
  91. /**
  92. * Produce a relative path based on the existing search directories or returns the same path
  93. * if the path is not a superset of an existing search path.
  94. *
  95. * @param aFullPathName is an absolute path to shorten.
  96. * @return the shortened path or \a aFullPathName.
  97. */
  98. wxString ShortenPath( const wxString& aFullPathName );
  99. /**
  100. * Return a pointer to the internal path list; the items in:load.
  101. *
  102. * The list can be used to set up the list of search paths available to a 3D file browser.
  103. *
  104. * @return pointer to the internal path list.
  105. */
  106. const std::list<SEARCH_PATH>* GetPaths() const;
  107. /**
  108. * Return true if the given name contains an alias and populates the string \a anAlias
  109. * with the alias and \a aRelPath with the relative path.
  110. */
  111. bool SplitAlias( const wxString& aFileName, wxString& anAlias, wxString& aRelPath ) const;
  112. /**
  113. * Returns true if the given path is a valid aliased relative path.
  114. *
  115. * If the path contains an alias then hasAlias is set true.
  116. */
  117. bool ValidateFileName( const wxString& aFileName, bool& hasAlias ) const;
  118. /**
  119. * Return a list of path environment variables local to KiCad.
  120. *
  121. * This list always includes KICAD6_3DMODEL_DIR even if it is not defined locally.
  122. */
  123. bool GetKicadPaths( std::list< wxString >& paths ) const;
  124. private:
  125. /**
  126. * Build the path list using available information such as KICAD6_3DMODEL_DIR and the 3d_path_list
  127. * configuration file.
  128. *
  129. * @warning Invalid paths are silently discarded and removed from the configuration file.
  130. *
  131. * @return true if at least one valid path was found.
  132. */
  133. bool createPathList( void );
  134. /**
  135. * Check that a path is valid and adds it to the search list.
  136. *
  137. * @param aPath is the alias set to be checked and added.
  138. * @return true if aPath is valid.
  139. */
  140. bool addPath( const SEARCH_PATH& aPath );
  141. /**
  142. * Read a list of path names from a configuration file.
  143. *
  144. * @return true if a file was found and contained at least one valid path.
  145. */
  146. bool readPathList( void );
  147. /**
  148. * Check the ${ENV_VAR} component of a path and adds it to the resolver's path list if
  149. * it is not yet in the list.
  150. */
  151. void checkEnvVarPath( const wxString& aPath );
  152. wxString m_configDir; // 3D configuration directory
  153. std::list<SEARCH_PATH> m_paths; // list of base paths to search from
  154. int m_errflags;
  155. PGM_BASE* m_pgm;
  156. PROJECT* m_project;
  157. wxString m_curProjDir;
  158. };
  159. #endif // FILENAME_RESOLVER_H