KiCad PCB EDA Suite
searchhelpfilefullpath.cpp
Go to the documentation of this file.
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 
26 #include <pgm_base.h>
27 #include <common.h>
28 #include <config.h> // to define DEFAULT_INSTALL_PATH
29 #include <macros.h>
30 #include <wx/log.h>
31 #include <trace_helpers.h>
32 #include <paths.h>
33 
34 
41 wxString FindFileInSearchPaths( const SEARCH_STACK& aStack,
42  const wxString& aFilename, const wxArrayString* aSubdirs )
43 {
44  wxPathList paths;
45 
46  for( unsigned i = 0; i < aStack.GetCount(); ++i )
47  {
48  wxFileName fn( aStack[i], wxEmptyString );
49 
50  if( aSubdirs )
51  {
52  for( unsigned j = 0; j < aSubdirs->GetCount(); j++ )
53  fn.AppendDir( (*aSubdirs)[j] );
54  }
55 
56  wxLogTrace( tracePathsAndFiles, " %s", fn.GetFullPath() );
57 
58  if( fn.DirExists() )
59  {
60  paths.Add( fn.GetPath() );
61  }
62  }
63 
64  return paths.FindValidPath( aFilename );
65 }
66 
67 
68 // See also FindKicadHelpPath.cpp.notused.
69 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSStack, const wxString& aBaseName )
70 {
71  wxArrayString subdirs;
72  wxArrayString altsubdirs;
73  SEARCH_STACK ss = aSStack;
74 
75  // It might already be in aSStack, but why depend on other code
76  // far away when it's so easy to add it again (to our copy) as the first place to look.
77 
78  // This is CMAKE_INSTALL_PREFIX unless DEFAULT_INSTALL_PATH was defined during
79  // build configuration:
80  ss.AddPaths( wxT( DEFAULT_INSTALL_PATH ), 0 );
81 
82 #if defined(__WXMAC__)
83  ss.AddPaths( PATHS::GetOSXKicadMachineDataDir() );
84  ss.AddPaths( Pgm().GetExecutablePath(), 0 );
85 
86  // OS X packages can have the help files in
87  // /Library/Application\ Support/kicad/help,
88  // and in Contents/SharedSupport/help inside the
89  // bundle.
90  // Below we account for an international subdirectory.
91  subdirs.Add( "help" );
92  altsubdirs.Add( "Contents" );
93  altsubdirs.Add( "SharedSupport" );
94  altsubdirs.Add( "help" );
95 #endif
96 
97 #if ! defined(__WXMAC__) // && defined(__linux__)
98  // This is the executable path minus the trailing bin directory used on Windows and Linux.
99  wxFileName tmp( Pgm().GetExecutablePath(), wxEmptyString );
100  wxArrayString binDirs = tmp.GetDirs();
101 
102  if( !binDirs.IsEmpty() && binDirs[ binDirs.GetCount() - 1 ].CmpNoCase( wxT( "bin" ) ) == 0 )
103  tmp.RemoveLastDir();
104 
105  ss.AddPaths( tmp.GetPath(), 0 );
106 
107  // Based on kicad-doc.bzr/CMakeLists.txt, line 20, the help files are
108  // installed into "<CMAKE_INSTALL_PREFIX>/share/doc/kicad/help" for linux.
109  // This is ${KICAD_HELP} var in that CMakeLists.txt file.
110  // Below we account for an international subdirectory.
111  subdirs.Add( "share" );
112  subdirs.Add( "doc" );
113  subdirs.Add( "kicad" );
114  subdirs.Add( "help" );
115 
116  // Based on kicad-doc.bzr/CMakeLists.txt, line 35, the help files are
117  // installed into "<CMAKE_INSTALL_PREFIX>/doc/help" for Windows.
118  // This is ${KICAD_HELP} var in that CMakeLists.txt file.
119  // Below we account for an international subdirectory.
120  altsubdirs.Add( "doc" );
121  altsubdirs.Add( "help" );
122 #endif
123 
124  // If there's a KICAD environment variable set, always use that guy's path first.
125  if( !Pgm().GetKicadEnvVariable().IsEmpty() )
126  ss.AddPaths( Pgm().GetKicadEnvVariable(), 0 );
127 
128  /* Search for a help file.
129  * we *must* find a help file.
130  * so help is searched in directories in this order:
131  * help/<canonical name> like help/en_GB
132  * help/<short name> like help/en
133  * help/en
134  */
135 
136  wxLocale* i18n = Pgm().GetLocale();
137 
138  // We try to find help file in help/<canonical name>
139  // If fails, try to find help file in help/<short canonical name>
140  // If fails, try to find help file in help/en
141  wxArrayString locale_name_dirs;
142  locale_name_dirs.Add( i18n->GetCanonicalName() ); // canonical name like fr_FR
143 
144  // wxLocale::GetName() does not return always the short name
145  locale_name_dirs.Add( i18n->GetName().BeforeLast( '_' ) ); // short canonical name like fr
146  locale_name_dirs.Add( "en" ); // default (en)
147 
148 #if defined(DEBUG) && 1
149  ss.Show( wxString( __func__ ) );
150  wxLogTrace( tracePathsAndFiles, "%s: m_help_file:'%s'", __func__, aBaseName );
151 #endif
152 
153  wxLogTrace( tracePathsAndFiles, "Checking SEARCH_STACK for file %s", aBaseName );
154 
155  // Help files can be html (.html ext) or pdf (.pdf ext) files.
156  // Therefore, <BaseName>.html file is searched and if not found,
157  // <BaseName>.pdf file is searched in the same paths
158  wxString fn;
159 
160  for( unsigned ii = 0; ii < locale_name_dirs.GetCount(); ii++ )
161  {
162  subdirs.Add( locale_name_dirs[ii] );
163  altsubdirs.Add( locale_name_dirs[ii] );
164 
165  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &altsubdirs );
166 
167  if( !fn.IsEmpty() )
168  {
169  // Prepend URI protocol since we will open in a browser
170  fn = wxT( "file://" ) + fn;
171  break;
172  }
173 
174  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &altsubdirs );
175 
176  if( !fn.IsEmpty() )
177  break;
178 
179  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &subdirs );
180 
181  if( !fn.IsEmpty() )
182  {
183  // Prepend URI protocol since we will open in a browser
184  fn = wxT( "file://" ) + fn;
185  break;
186  }
187 
188  fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &subdirs );
189 
190  if( !fn.IsEmpty() )
191  break;
192 
193  subdirs.RemoveAt( subdirs.GetCount() - 1 );
194  altsubdirs.RemoveAt( altsubdirs.GetCount() - 1 );
195  }
196 
197  return fn;
198 }
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
Look for files in a number of paths.
Definition: search_stack.h:41
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
This file contains miscellaneous commonly used macros and functions.
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSStack, const wxString &aBaseName)
Return the help file's full path.
wxLogTrace helper definitions.
wxString FindFileInSearchPaths(const SEARCH_STACK &aStack, const wxString &aFilename, const wxArrayString *aSubdirs)
Function FindFileInSearchPaths looks in "this" for aFilename, but first modifies every search path by...
see class PGM_BASE
The common library.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).