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.

197 lines
6.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2015 CERN
  5. * Copyright (C) 2014-2018 KiCad Developers, see CHANGELOG.TXT for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <pgm_base.h>
  26. #include <common.h>
  27. #include <config.h> // to define DEFAULT_INSTALL_PATH
  28. #include <macros.h>
  29. #include <wx/log.h>
  30. #include <trace_helpers.h>
  31. /**
  32. * Function FindFileInSearchPaths
  33. * looks in "this" for \a aFilename, but first modifies every search
  34. * path by appending a list of path fragments from aSubdirs. That modification
  35. * is not relative.
  36. */
  37. wxString FindFileInSearchPaths( const SEARCH_STACK& aStack,
  38. const wxString& aFilename, const wxArrayString* aSubdirs )
  39. {
  40. wxPathList paths;
  41. for( unsigned i = 0; i < aStack.GetCount(); ++i )
  42. {
  43. wxFileName fn( aStack[i], wxEmptyString );
  44. if( aSubdirs )
  45. {
  46. for( unsigned j = 0; j < aSubdirs->GetCount(); j++ )
  47. fn.AppendDir( (*aSubdirs)[j] );
  48. }
  49. wxLogTrace( tracePathsAndFiles, " %s", fn.GetFullPath() );
  50. if( fn.DirExists() )
  51. {
  52. paths.Add( fn.GetPath() );
  53. }
  54. }
  55. return paths.FindValidPath( aFilename );
  56. }
  57. // See also FindKicadHelpPath.cpp.notused.
  58. wxString SearchHelpFileFullPath( const SEARCH_STACK& aSStack, const wxString& aBaseName )
  59. {
  60. wxArrayString subdirs;
  61. wxArrayString altsubdirs;
  62. SEARCH_STACK ss = aSStack;
  63. // It might already be in aSStack, but why depend on other code
  64. // far away when it's so easy to add it again (to our copy) as the first place to look.
  65. // This is CMAKE_INSTALL_PREFIX unless DEFAULT_INSTALL_PATH was defined during
  66. // build configuration:
  67. ss.AddPaths( wxT( DEFAULT_INSTALL_PATH ), 0 );
  68. #if defined(__WXMAC__)
  69. ss.AddPaths( GetOSXKicadMachineDataDir() );
  70. ss.AddPaths( Pgm().GetExecutablePath(), 0 );
  71. // OS X packages can have the help files in
  72. // /Library/Application\ Support/kicad/help,
  73. // and in Contents/SharedSupport/help inside the
  74. // bundle.
  75. // Below we account for an international subdirectory.
  76. subdirs.Add( "help" );
  77. altsubdirs.Add( "Contents" );
  78. altsubdirs.Add( "SharedSupport" );
  79. altsubdirs.Add( "help" );
  80. #endif
  81. #if ! defined(__WXMAC__) // && defined(__linux__)
  82. // This is the executable path minus the trailing bin directory used on Windows and Linux.
  83. wxFileName tmp( Pgm().GetExecutablePath(), wxEmptyString );
  84. wxArrayString binDirs = tmp.GetDirs();
  85. if( !binDirs.IsEmpty() && binDirs[ binDirs.GetCount() - 1 ].CmpNoCase( wxT( "bin" ) ) == 0 )
  86. tmp.RemoveLastDir();
  87. ss.AddPaths( tmp.GetPath(), 0 );
  88. // Based on kicad-doc.bzr/CMakeLists.txt, line 20, the help files are
  89. // installed into "<CMAKE_INSTALL_PREFIX>/share/doc/kicad/help" for linux.
  90. // This is ${KICAD_HELP} var in that CMakeLists.txt file.
  91. // Below we account for an international subdirectory.
  92. subdirs.Add( "share" );
  93. subdirs.Add( "doc" );
  94. subdirs.Add( "kicad" );
  95. subdirs.Add( "help" );
  96. // Based on kicad-doc.bzr/CMakeLists.txt, line 35, the help files are
  97. // installed into "<CMAKE_INSTALL_PREFIX>/doc/help" for Windows.
  98. // This is ${KICAD_HELP} var in that CMakeLists.txt file.
  99. // Below we account for an international subdirectory.
  100. altsubdirs.Add( "doc" );
  101. altsubdirs.Add( "help" );
  102. #endif
  103. // If there's a KICAD environment variable set, always use that guy's path first.
  104. if( !Pgm().GetKicadEnvVariable().IsEmpty() )
  105. ss.AddPaths( Pgm().GetKicadEnvVariable(), 0 );
  106. /* Search for a help file.
  107. * we *must* find a help file.
  108. * so help is searched in directories in this order:
  109. * help/<canonical name> like help/en_GB
  110. * help/<short name> like help/en
  111. * help/en
  112. */
  113. wxLocale* i18n = Pgm().GetLocale();
  114. // We try to find help file in help/<canonical name>
  115. // If fails, try to find help file in help/<short canonical name>
  116. // If fails, try to find help file in help/en
  117. wxArrayString locale_name_dirs;
  118. locale_name_dirs.Add( i18n->GetCanonicalName() ); // canonical name like fr_FR
  119. // wxLocale::GetName() does not return always the short name
  120. locale_name_dirs.Add( i18n->GetName().BeforeLast( '_' ) ); // short canonical name like fr
  121. locale_name_dirs.Add( "en" ); // default (en)
  122. #if defined(DEBUG) && 1
  123. ss.Show( wxString( __func__ ) );
  124. wxLogTrace( tracePathsAndFiles, "%s: m_help_file:'%s'", __func__, aBaseName );
  125. #endif
  126. wxLogTrace( tracePathsAndFiles, "Checking SEARCH_STACK for file %s", aBaseName );
  127. // Help files can be html (.html ext) or pdf (.pdf ext) files.
  128. // Therefore, <BaseName>.html file is searched and if not found,
  129. // <BaseName>.pdf file is searched in the same paths
  130. wxString fn;
  131. for( unsigned ii = 0; ii < locale_name_dirs.GetCount(); ii++ )
  132. {
  133. subdirs.Add( locale_name_dirs[ii] );
  134. altsubdirs.Add( locale_name_dirs[ii] );
  135. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &altsubdirs );
  136. if( !fn.IsEmpty() )
  137. {
  138. // Prepend URI protocol since we will open in a browser
  139. fn = wxT( "file://" ) + fn;
  140. break;
  141. }
  142. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &altsubdirs );
  143. if( !fn.IsEmpty() )
  144. break;
  145. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &subdirs );
  146. if( !fn.IsEmpty() )
  147. {
  148. // Prepend URI protocol since we will open in a browser
  149. fn = wxT( "file://" ) + fn;
  150. break;
  151. }
  152. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &subdirs );
  153. if( !fn.IsEmpty() )
  154. break;
  155. subdirs.RemoveAt( subdirs.GetCount() - 1 );
  156. altsubdirs.RemoveAt( altsubdirs.GetCount() - 1 );
  157. }
  158. return fn;
  159. }