KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gestfich.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) 2004 Jean-Pierre Charras, [email protected]
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
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
31#include <wx/mimetype.h>
32#include <wx/filename.h>
33#include <wx/dir.h>
34
35#include <pgm_base.h>
36#include <confirm.h>
37#include <core/arraydim.h>
38#include <gestfich.h>
39#include <string_utils.h>
40#include <launch_ext.h>
41#include "wx/tokenzr.h"
42
43#include <filesystem>
44
45void QuoteString( wxString& string )
46{
47 if( !string.StartsWith( wxT( "\"" ) ) )
48 {
49 string.Prepend ( wxT( "\"" ) );
50 string.Append ( wxT( "\"" ) );
51 }
52}
53
54
55wxString FindKicadFile( const wxString& shortname )
56{
57 // Test the presence of the file in the directory shortname of
58 // the KiCad binary path.
59#ifndef __WXMAC__
60 wxString fullFileName = Pgm().GetExecutablePath() + shortname;
61#else
62 wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
63#endif
64 if( wxFileExists( fullFileName ) )
65 return fullFileName;
66
67 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
68 {
69 wxFileName buildDir( Pgm().GetExecutablePath(), shortname );
70 buildDir.RemoveLastDir();
71#ifndef __WXMSW__
72 buildDir.AppendDir( shortname );
73#else
74 buildDir.AppendDir( shortname.BeforeLast( '.' ) );
75#endif
76
77 if( buildDir.GetDirs().Last() == "pl_editor" )
78 {
79 buildDir.RemoveLastDir();
80 buildDir.AppendDir( "pagelayout_editor" );
81 }
82
83 if( wxFileExists( buildDir.GetFullPath() ) )
84 return buildDir.GetFullPath();
85 }
86
87 // Test the presence of the file in the directory shortname
88 // defined by the environment variable KiCad.
89 if( Pgm().IsKicadEnvVariableDefined() )
90 {
91 fullFileName = Pgm().GetKicadEnvVariable() + shortname;
92
93 if( wxFileExists( fullFileName ) )
94 return fullFileName;
95 }
96
97#if defined( __WINDOWS__ )
98 // kicad can be installed highly portably on Windows, anywhere and concurrently
99 // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
100 return shortname;
101#else
102
103 // Path list for KiCad binary files
104 const static wxChar* possibilities[] = {
105#if defined( __WXMAC__ )
106 // all internal paths are relative to main bundle kicad.app
107 wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
108 wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
109 wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
110 wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
111 wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
112 wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
113#else
114 wxT( "/usr/bin/" ),
115 wxT( "/usr/local/bin/" ),
116 wxT( "/usr/local/kicad/bin/" ),
117#endif
118 };
119
120 // find binary file from possibilities list:
121 for( unsigned i=0; i<arrayDim(possibilities); ++i )
122 {
123#ifndef __WXMAC__
124 fullFileName = possibilities[i] + shortname;
125#else
126 // make relative paths absolute
127 fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
128#endif
129
130 if( wxFileExists( fullFileName ) )
131 return fullFileName;
132 }
133
134 return shortname;
135
136#endif
137}
138
139
140int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback )
141{
142 wxString fullEditorName;
143 std::vector<wxString> params;
144
145#ifdef __UNIX__
146 wxString param;
147 bool inSingleQuotes = false;
148 bool inDoubleQuotes = false;
149
150 auto pushParam =
151 [&]()
152 {
153 if( !param.IsEmpty() )
154 {
155 params.push_back( param );
156 param.clear();
157 }
158 };
159
160 for( wxUniChar ch : aEditorName )
161 {
162 if( inSingleQuotes )
163 {
164 if( ch == '\'' )
165 {
166 pushParam();
167 inSingleQuotes = false;
168 continue;
169 }
170 else
171 {
172 param += ch;
173 }
174 }
175 else if( inDoubleQuotes )
176 {
177 if( ch == '"' )
178 {
179 pushParam();
180 inDoubleQuotes = false;
181 }
182 else
183 {
184 param += ch;
185 }
186 }
187 else if( ch == '\'' )
188 {
189 pushParam();
190 inSingleQuotes = true;
191 }
192 else if( ch == '"' )
193 {
194 pushParam();
195 inDoubleQuotes = true;
196 }
197 else if( ch == ' ' )
198 {
199 pushParam();
200 }
201 else
202 {
203 param += ch;
204 }
205 }
206
207 pushParam();
208
209 fullEditorName = FindKicadFile( params[0] );
210 params.erase( params.begin() );
211#else
212 fullEditorName = FindKicadFile( aEditorName );
213#endif
214
215 if( wxFileExists( fullEditorName ) )
216 {
217 std::vector<const wchar_t*> args;
218
219 args.emplace_back( fullEditorName.wc_str() );
220
221 if( !params.empty() )
222 {
223 for( const wxString& p : params )
224 args.emplace_back( p.wc_str() );
225 }
226
227 if( !aFileName.IsEmpty() )
228 args.emplace_back( aFileName.wc_str() );
229
230 args.emplace_back( nullptr );
231
232 return wxExecute( const_cast<wchar_t**>( args.data() ), wxEXEC_ASYNC, aCallback );
233 }
234
235 wxString msg;
236 msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
237 DisplayError( nullptr, msg, 20 );
238 return -1;
239}
240
241
242bool OpenPDF( const wxString& file )
243{
244 wxString msg;
245 wxString filename = file;
246
247 Pgm().ReadPdfBrowserInfos();
248
249 if( Pgm().UseSystemPdfBrowser() )
250 {
251 if( !LaunchExternal( filename ) )
252 {
253 msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
254 DisplayError( nullptr, msg );
255 return false;
256 }
257 }
258 else
259 {
260 const wchar_t* args[3];
261
262 args[0] = Pgm().GetPdfBrowserName().wc_str();
263 args[1] = filename.wc_str();
264 args[2] = nullptr;
265
266 if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
267 {
268 msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
269 DisplayError( nullptr, msg );
270 return false;
271 }
272 }
273
274 return true;
275}
276
277
278void OpenFile( const wxString& file )
279{
280 wxFileName fileName( file );
281 wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
282
283 if( !filetype )
284 return;
285
286 wxString command;
287 wxFileType::MessageParameters params( file );
288
289 filetype->GetOpenCommand( &command, params );
290 delete filetype;
291
292 if( !command.IsEmpty() )
293 wxExecute( command );
294}
295
296
297void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
298{
299 if( !wxCopyFile( aSrcPath, aDestPath ) )
300 {
301 wxString msg;
302
303 if( !aErrors.IsEmpty() )
304 aErrors += "\n";
305
306 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
307 aErrors += msg;
308 }
309}
310
311
312wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
313{
314 return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
315}
316
317
318bool RmDirRecursive( const wxString& aFileName, wxString* aErrors )
319{
320 namespace fs = std::filesystem;
321
322 std::string rmDir = aFileName.ToStdString();
323
324 if( rmDir.length() < 3 )
325 {
326 if( aErrors )
327 *aErrors = _( "Invalid directory name, cannot remove root" );
328
329 return false;
330 }
331
332 if( !fs::exists( rmDir ) )
333 {
334 if( aErrors )
335 *aErrors = wxString::Format( _( "Directory '%s' does not exist" ), aFileName );
336
337 return false;
338 }
339
340 fs::path path( rmDir );
341
342 if( !fs::is_directory( path ) )
343 {
344 if( aErrors )
345 *aErrors = wxString::Format( _( "'%s' is not a directory" ), aFileName );
346
347 return false;
348 }
349
350 try
351 {
352 fs::remove_all( path );
353 }
354 catch( const fs::filesystem_error& e )
355 {
356 if( aErrors )
357 *aErrors = wxString::Format( _( "Error removing directory '%s': %s" ), aFileName, e.what() );
358
359 return false;
360 }
361
362 return true;
363}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
#define _(s)
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
Definition: gestfich.cpp:55
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:312
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:242
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:297
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Removes the directory aDirName and all its contents including subdirectories and their files.
Definition: gestfich.cpp:318
void OpenFile(const wxString &file)
Definition: gestfich.cpp:278
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:45
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:140
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119