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/dir.h>
33
34#include <pgm_base.h>
35#include <confirm.h>
36#include <core/arraydim.h>
37#include <gestfich.h>
38#include <string_utils.h>
39#include <launch_ext.h>
40#include "wx/tokenzr.h"
41
42#include <wx/wfstream.h>
43#include <wx/fs_zip.h>
44#include <wx/zipstrm.h>
45
46#include <filesystem>
47
48void QuoteString( wxString& string )
49{
50 if( !string.StartsWith( wxT( "\"" ) ) )
51 {
52 string.Prepend ( wxT( "\"" ) );
53 string.Append ( wxT( "\"" ) );
54 }
55}
56
57
58wxString FindKicadFile( const wxString& shortname )
59{
60 // Test the presence of the file in the directory shortname of
61 // the KiCad binary path.
62#ifndef __WXMAC__
63 wxString fullFileName = Pgm().GetExecutablePath() + shortname;
64#else
65 wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
66#endif
67 if( wxFileExists( fullFileName ) )
68 return fullFileName;
69
70 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
71 {
72 wxFileName buildDir( Pgm().GetExecutablePath(), shortname );
73 buildDir.RemoveLastDir();
74#ifndef __WXMSW__
75 buildDir.AppendDir( shortname );
76#else
77 buildDir.AppendDir( shortname.BeforeLast( '.' ) );
78#endif
79
80 if( buildDir.GetDirs().Last() == "pl_editor" )
81 {
82 buildDir.RemoveLastDir();
83 buildDir.AppendDir( "pagelayout_editor" );
84 }
85
86 if( wxFileExists( buildDir.GetFullPath() ) )
87 return buildDir.GetFullPath();
88 }
89
90 // Test the presence of the file in the directory shortname
91 // defined by the environment variable KiCad.
92 if( Pgm().IsKicadEnvVariableDefined() )
93 {
94 fullFileName = Pgm().GetKicadEnvVariable() + shortname;
95
96 if( wxFileExists( fullFileName ) )
97 return fullFileName;
98 }
99
100#if defined( __WINDOWS__ )
101 // kicad can be installed highly portably on Windows, anywhere and concurrently
102 // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
103 return shortname;
104#else
105
106 // Path list for KiCad binary files
107 const static wxChar* possibilities[] = {
108#if defined( __WXMAC__ )
109 // all internal paths are relative to main bundle kicad.app
110 wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
111 wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
112 wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
113 wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
114 wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
115 wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
116#else
117 wxT( "/usr/bin/" ),
118 wxT( "/usr/local/bin/" ),
119 wxT( "/usr/local/kicad/bin/" ),
120#endif
121 };
122
123 // find binary file from possibilities list:
124 for( unsigned i=0; i<arrayDim(possibilities); ++i )
125 {
126#ifndef __WXMAC__
127 fullFileName = possibilities[i] + shortname;
128#else
129 // make relative paths absolute
130 fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
131#endif
132
133 if( wxFileExists( fullFileName ) )
134 return fullFileName;
135 }
136
137 return shortname;
138
139#endif
140}
141
142
143int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess* aCallback,
144 bool aFileForKicad )
145{
146 wxString fullEditorName;
147 std::vector<wxString> params;
148
149#ifdef __UNIX__
150 wxString param;
151 bool inSingleQuotes = false;
152 bool inDoubleQuotes = false;
153
154 auto pushParam =
155 [&]()
156 {
157 if( !param.IsEmpty() )
158 {
159 params.push_back( param );
160 param.clear();
161 }
162 };
163
164 for( wxUniChar ch : aEditorName )
165 {
166 if( inSingleQuotes )
167 {
168 if( ch == '\'' )
169 {
170 pushParam();
171 inSingleQuotes = false;
172 continue;
173 }
174 else
175 {
176 param += ch;
177 }
178 }
179 else if( inDoubleQuotes )
180 {
181 if( ch == '"' )
182 {
183 pushParam();
184 inDoubleQuotes = false;
185 }
186 else
187 {
188 param += ch;
189 }
190 }
191 else if( ch == '\'' )
192 {
193 pushParam();
194 inSingleQuotes = true;
195 }
196 else if( ch == '"' )
197 {
198 pushParam();
199 inDoubleQuotes = true;
200 }
201 else if( ch == ' ' )
202 {
203 pushParam();
204 }
205 else
206 {
207 param += ch;
208 }
209 }
210
211 pushParam();
212
213 if( aFileForKicad )
214 fullEditorName = FindKicadFile( params[0] );
215 else
216 fullEditorName = params[0];
217
218 params.erase( params.begin() );
219#else
220
221 if( aFileForKicad )
222 fullEditorName = FindKicadFile( aEditorName );
223 else
224 fullEditorName = aEditorName;
225#endif
226
227 if( wxFileExists( fullEditorName ) )
228 {
229 std::vector<const wchar_t*> args;
230
231 args.emplace_back( fullEditorName.wc_str() );
232
233 if( !params.empty() )
234 {
235 for( const wxString& p : params )
236 args.emplace_back( p.wc_str() );
237 }
238
239 if( !aFileName.IsEmpty() )
240 args.emplace_back( aFileName.wc_str() );
241
242 args.emplace_back( nullptr );
243
244 return wxExecute( const_cast<wchar_t**>( args.data() ), wxEXEC_ASYNC, aCallback );
245 }
246
247 wxString msg;
248 msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
249 DisplayError( nullptr, msg, 20 );
250 return -1;
251}
252
253
254bool OpenPDF( const wxString& file )
255{
256 wxString msg;
257 wxString filename = file;
258
260
261 if( Pgm().UseSystemPdfBrowser() )
262 {
263 if( !LaunchExternal( filename ) )
264 {
265 msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
266 DisplayError( nullptr, msg );
267 return false;
268 }
269 }
270 else
271 {
272 const wchar_t* args[3];
273
274 args[0] = Pgm().GetPdfBrowserName().wc_str();
275 args[1] = filename.wc_str();
276 args[2] = nullptr;
277
278 if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
279 {
280 msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
281 DisplayError( nullptr, msg );
282 return false;
283 }
284 }
285
286 return true;
287}
288
289
290void OpenFile( const wxString& file )
291{
292 wxFileName fileName( file );
293 wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
294
295 if( !filetype )
296 return;
297
298 wxString command;
299 wxFileType::MessageParameters params( file );
300
301 filetype->GetOpenCommand( &command, params );
302 delete filetype;
303
304 if( !command.IsEmpty() )
305 wxExecute( command );
306}
307
308
309void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
310{
311 if( !wxCopyFile( aSrcPath, aDestPath ) )
312 {
313 wxString msg;
314
315 if( !aErrors.IsEmpty() )
316 aErrors += "\n";
317
318 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
319 aErrors += msg;
320 }
321}
322
323
324wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
325{
326 return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
327}
328
329
330bool RmDirRecursive( const wxString& aFileName, wxString* aErrors )
331{
332 namespace fs = std::filesystem;
333
334 std::string rmDir = aFileName.ToStdString();
335
336 if( rmDir.length() < 3 )
337 {
338 if( aErrors )
339 *aErrors = _( "Invalid directory name, cannot remove root" );
340
341 return false;
342 }
343
344 if( !fs::exists( rmDir ) )
345 {
346 if( aErrors )
347 *aErrors = wxString::Format( _( "Directory '%s' does not exist" ), aFileName );
348
349 return false;
350 }
351
352 fs::path path( rmDir );
353
354 if( !fs::is_directory( path ) )
355 {
356 if( aErrors )
357 *aErrors = wxString::Format( _( "'%s' is not a directory" ), aFileName );
358
359 return false;
360 }
361
362 try
363 {
364 fs::remove_all( path );
365 }
366 catch( const fs::filesystem_error& e )
367 {
368 if( aErrors )
369 *aErrors = wxString::Format( _( "Error removing directory '%s': %s" ), aFileName, e.what() );
370
371 return false;
372 }
373
374 return true;
375}
376
377bool CopyDirectory( const wxString& aSourceDir, const wxString& aDestDir, wxString& aErrors )
378{
379 wxDir dir( aSourceDir );
380 if( !dir.IsOpened() )
381 {
382 aErrors += wxString::Format( _( "Could not open source directory: %s" ), aSourceDir );
383 aErrors += wxT( "\n" );
384 return false;
385 }
386
387 if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
388 {
389 aErrors += wxString::Format( _( "Could not create destination directory: %s" ), aDestDir );
390 aErrors += wxT( "\n" );
391 return false;
392 }
393
394 wxString filename;
395 bool cont = dir.GetFirst( &filename );
396 while( cont )
397 {
398 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
399 wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
400
401 if( wxFileName::DirExists( sourcePath ) )
402 {
403 // Recursively copy subdirectories
404 if( !CopyDirectory( sourcePath, destPath, aErrors ) )
405 {
406 return false;
407 }
408 }
409 else
410 {
411 // Copy files
412 if( !wxCopyFile( sourcePath, destPath ) )
413 {
414 aErrors += wxString::Format( _( "Could not copy file: %s to %s" ), sourcePath, destPath );
415 return false;
416 }
417 }
418
419 cont = dir.GetNext( &filename );
420 }
421
422 return true;
423}
424
425
426bool AddDirectoryToZip( wxZipOutputStream& aZip, const wxString& aSourceDir, wxString& aErrors,
427 const wxString& aParentDir )
428{
429 wxDir dir( aSourceDir );
430 if( !dir.IsOpened() )
431 {
432 aErrors += wxString::Format( _( "Could not open source directory: %s" ), aSourceDir );
433 aErrors += "\n";
434 return false;
435 }
436
437 wxString filename;
438 bool cont = dir.GetFirst( &filename );
439 while( cont )
440 {
441 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
442 wxString zipPath = aParentDir + filename;
443
444 if( wxFileName::DirExists( sourcePath ) )
445 {
446 // Add directory entry to the ZIP file
447 aZip.PutNextDirEntry( zipPath + "/" );
448 // Recursively add subdirectories
449 if( !AddDirectoryToZip( aZip, sourcePath, aErrors, zipPath + "/" ) )
450 {
451 return false;
452 }
453 }
454 else
455 {
456 // Add file entry to the ZIP file
457 aZip.PutNextEntry( zipPath );
458 wxFFileInputStream fileStream( sourcePath );
459 if( !fileStream.IsOk() )
460 {
461 aErrors += wxString::Format( _( "Could not read file: %s" ), sourcePath );
462 return false;
463 }
464 aZip.Write( fileStream );
465 }
466
467 cont = dir.GetNext( &filename );
468 }
469
470 return true;
471}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
virtual const wxString & GetKicadEnvVariable() const
Definition: pgm_base.h:181
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition: pgm_base.cpp:1045
virtual const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:187
virtual const wxString & GetExecutablePath() const
Definition: pgm_base.cpp:1039
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
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:58
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:324
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:254
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:309
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:143
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Removes the directory aDirName and all its contents including subdirectories and their files.
Definition: gestfich.cpp:330
void OpenFile(const wxString &file)
Definition: gestfich.cpp:290
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:48
bool CopyDirectory(const wxString &aSourceDir, const wxString &aDestDir, wxString &aErrors)
Copy a directory and its contents to another directory.
Definition: gestfich.cpp:377
bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
Add a directory and its contents to a zip file.
Definition: gestfich.cpp:426
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE