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