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