KiCad PCB EDA Suite
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, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2008-2017 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2019 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 <fctsys.h>
32 #include <wx/mimetype.h>
33 #include <wx/filename.h>
34 #include <wx/dir.h>
35 #include <pgm_base.h>
36 #include <confirm.h>
37 #include <common.h>
38 #include <macros.h>
39 
40 #include <gestfich.h>
41 
42 void AddDelimiterString( wxString& string )
43 {
44  if( !string.StartsWith( wxT( "\"" ) ) )
45  {
46  string.Prepend ( wxT( "\"" ) );
47  string.Append ( wxT( "\"" ) );
48  }
49 }
50 
51 
52 wxString EDA_FILE_SELECTOR( const wxString& aTitle,
53  const wxString& aPath,
54  const wxString& aFileName,
55  const wxString& aExtension,
56  const wxString& aWildcard,
57  wxWindow* aParent,
58  int aStyle,
59  const bool aKeepWorkingDirectory,
60  const wxPoint& aPosition,
61  wxString* aMruPath )
62 {
63  wxString fullfilename;
64  wxString curr_cwd = wxGetCwd();
65  wxString defaultname = aFileName;
66  wxString defaultpath = aPath;
67  wxString dotted_Ext = wxT(".") + aExtension;
68 
69 #ifdef __WINDOWS__
70  defaultname.Replace( wxT( "/" ), wxT( "\\" ) );
71  defaultpath.Replace( wxT( "/" ), wxT( "\\" ) );
72 #endif
73 
74  if( defaultpath.IsEmpty() )
75  {
76  if( aMruPath == NULL )
77  defaultpath = wxGetCwd();
78  else
79  defaultpath = *aMruPath;
80  }
81 
82  wxSetWorkingDirectory( defaultpath );
83 
84 #if 0 && defined (DEBUG)
85  printf( "defaultpath=\"%s\" defaultname=\"%s\" Ext=\"%s\" Mask=\"%s\" flag=%d keep_working_directory=%d\n",
86  TO_UTF8( defaultpath ),
87  TO_UTF8( defaultname ),
88  TO_UTF8( aExtension ),
89  TO_UTF8( aWildcard ),
90  aStyle,
91  aKeepWorkingDirectory );
92 #endif
93 
94  fullfilename = wxFileSelector( aTitle, defaultpath, defaultname,
95  dotted_Ext, aWildcard,
96  aStyle, // open mode wxFD_OPEN, wxFD_SAVE ..
97  aParent, aPosition.x, aPosition.y );
98 
99  if( aKeepWorkingDirectory )
100  wxSetWorkingDirectory( curr_cwd );
101 
102  if( !fullfilename.IsEmpty() && aMruPath )
103  {
104  wxFileName fn = fullfilename;
105  *aMruPath = fn.GetPath();
106  }
107 
108  return fullfilename;
109 }
110 
111 
112 wxString FindKicadFile( const wxString& shortname )
113 {
114  // Test the presence of the file in the directory shortname of
115  // the KiCad binary path.
116 #ifndef __WXMAC__
117  wxString fullFileName = Pgm().GetExecutablePath() + shortname;
118 #else
119  wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
120 #endif
121  if( wxFileExists( fullFileName ) )
122  return fullFileName;
123 
124  // Test the presence of the file in the directory shortname
125  // defined by the environment variable KiCad.
126  if( Pgm().IsKicadEnvVariableDefined() )
127  {
128  fullFileName = Pgm().GetKicadEnvVariable() + shortname;
129 
130  if( wxFileExists( fullFileName ) )
131  return fullFileName;
132  }
133 
134  // Path list for KiCad binary files
135  const static wxChar* possibilities[] = {
136 #if defined( __WINDOWS__ )
137  wxT( "c:/kicad/bin/" ),
138  wxT( "d:/kicad/bin/" ),
139  wxT( "c:/Program Files/kicad/bin/" ),
140  wxT( "d:/Program Files/kicad/bin/" ),
141 #elif defined( __WXMAC__ )
142  // all internal paths are relative to main bundle kicad.app
143  wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
144  wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
145  wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
146  wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
147  wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
148  wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
149 #else
150  wxT( "/usr/bin/" ),
151  wxT( "/usr/local/bin/" ),
152  wxT( "/usr/local/kicad/bin/" ),
153 #endif
154  };
155 
156  // find binary file from possibilities list:
157  for( unsigned i=0; i<arrayDim(possibilities); ++i )
158  {
159 #ifndef __WXMAC__
160  fullFileName = possibilities[i] + shortname;
161 #else
162  // make relative paths absolute
163  fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
164 #endif
165 
166  if( wxFileExists( fullFileName ) )
167  return fullFileName;
168  }
169 
170  return shortname;
171 }
172 
173 
174 int ExecuteFile( wxWindow* frame, const wxString& ExecFile, const wxString& param,
175  wxProcess *callback )
176 {
177  wxString fullFileName = FindKicadFile( ExecFile );
178 
179  if( wxFileExists( fullFileName ) )
180  {
181  if( !param.IsEmpty() )
182  fullFileName += wxT( " " ) + param;
183 
184  return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
185  }
186 #ifdef __WXMAC__
187  else
188  {
189  AddDelimiterString( fullFileName );
190 
191  if( !param.IsEmpty() )
192  fullFileName += wxT( " " ) + param;
193 
194  return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
195  }
196 #endif
197 
198  wxString msg;
199  msg.Printf( _( "Command \"%s\" could not found" ), GetChars( fullFileName ) );
200  DisplayError( frame, msg, 20 );
201  return -1;
202 }
203 
204 
205 bool OpenPDF( const wxString& file )
206 {
207  wxString command;
208  const wxString& filename = file;
209 
210  Pgm().ReadPdfBrowserInfos();
211 
212  if( !Pgm().UseSystemPdfBrowser() ) // Run the preferred PDF Browser
213  {
214 #ifdef __WXMSW__
215  // Windows requires double quotes around the filename to handle spaces
216  command = Pgm().GetPdfBrowserName() + wxT( " \"" ) + filename + wxT( "\"" );
217 #else
218  command = Pgm().GetPdfBrowserName() + wxT( " '" ) + filename + wxT( "'" );
219 #endif
220  }
221  else
222  {
223  if( wxLaunchDefaultApplication( filename ) )
224  return true;
225 
226 #ifdef __WXMAC__
227  command = wxT( "/usr/bin/open -a '" ) + filename + wxT( "'" );
228 #endif
229  // If launching the system default PDF viewer fails, fall through with empty command
230  // string so the error message is displayed.
231  }
232 
233  if( !command.IsEmpty() )
234  {
235  if( ProcessExecute( command ) != -1 )
236  {
237  return true;
238  }
239  else
240  {
241  wxString msg;
242  msg.Printf( _( "Problem while running the PDF viewer\nCommand is \"%s\"" ), command );
243  DisplayError( NULL, msg );
244  }
245  }
246  else
247  {
248  wxString msg;
249  msg.Printf( _( "Unable to find a PDF viewer for \"%s\"" ), file );
250  DisplayError( NULL, msg );
251  }
252 
253  return false;
254 }
255 
256 
257 void OpenFile( const wxString& file )
258 {
259  wxFileName fileName( file );
260  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
261 
262  if( !filetype )
263  return;
264 
265  wxString command;
266  wxFileType::MessageParameters params( file );
267 
268  filetype->GetOpenCommand( &command, params );
269  delete filetype;
270 
271  if( !command.IsEmpty() )
272  ProcessExecute( command );
273 }
274 
275 
276 bool doPrintFile( const wxString& file, bool aDryRun )
277 {
278  wxFileName fileName( file );
279  wxString ext = fileName.GetExt();
280  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( ext );
281 
282  if( !filetype )
283  return false;
284 
285  wxString printCommand;
286  wxString openCommand;
287  wxString application;
288 
289  wxFileType::MessageParameters params( file );
290  filetype->GetPrintCommand( &printCommand, params );
291  filetype->GetOpenCommand( &openCommand, params );
292  delete filetype;
293 
294  if( !printCommand.IsEmpty() )
295  {
296  if( !aDryRun )
297  ProcessExecute( printCommand );
298 
299  return true;
300  }
301 
302 #ifdef __WXMAC__
303  if( ext == "ps" || ext == "pdf" )
304  application = "Preview";
305  else if( ext == "csv" )
306  application = "Numbers";
307  else if( ext == "txt" || ext == "rpt" || ext == "pos" || ext == "cmp" || ext == "net" )
308  application = "TextEdit";
309 
310  if( !application.IsEmpty() )
311  {
312  printCommand.Printf( "osascript -e 'tell application \"%s\"' "
313  "-e ' set srcFileRef to (open POSIX file \"%s\")' "
314  "-e ' activate' "
315  "-e ' print srcFileRef print dialog true' "
316  "-e 'end tell' ",
317  application,
318  file );
319 
320  if( !aDryRun )
321  system( printCommand.c_str() );
322 
323  return true;
324  }
325 #endif
326 
327 #ifdef __WXGTK__
328  if( ext == "ps" || ext == "pdf"
329  || ext == "csv"
330  || ext == "txt" || ext == "rpt" || ext == "pos" || ext == "cmp" || ext == "net" )
331  {
332  printCommand.Printf( "lp \"%s\"", file );
333 
334  if( !aDryRun )
335  ProcessExecute( printCommand );
336 
337  return true;
338  }
339 #endif
340 
341  if( !aDryRun )
342  {
343  DisplayError( nullptr, wxString::Format( _( "Cannot print '%s'.\n\nUnknown filetype." ),
344  file ) );
345  }
346 
347  return false;
348 }
349 
350 
351 void PrintFile( const wxString& file )
352 {
353  doPrintFile( file, false );
354 }
355 
356 
357 bool CanPrintFile( const wxString& file )
358 {
359  return doPrintFile( file, true );
360 }
361 
362 
363 void CopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
364 {
365  if( !wxCopyFile( aSrcPath, aDestPath ) )
366  {
367  wxString msg;
368 
369  if( !aErrors.IsEmpty() )
370  aErrors += "\n";
371 
372  msg.Printf( _( "Cannot copy file \"%s\"." ), aDestPath );
373  aErrors += msg;
374  }
375 }
376 
377 
378 wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
379 {
380  return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
381 }
382 
383 
384 bool DeleteDirectory( const wxString& aDirName, bool aRecurse, bool aIncludeHidden )
385 {
386  int hiddenFlag = ( aIncludeHidden ? wxDIR_HIDDEN : 0 );
387  wxDir mainDir( aDirName );
388 
389  if( !mainDir.IsOpened() )
390  return false;
391 
392  if( mainDir.HasSubDirs() && !aRecurse )
393  {
394  mainDir.Close();
395  return false;
396  }
397 
398  // Get the name from wxWidgets so that we are sure all the separators are correct
399  wxString fullDirName = mainDir.GetNameWithSep();
400 
401  wxString dir;
402  bool valid = mainDir.GetFirst( &dir, wxEmptyString, wxDIR_DIRS | hiddenFlag );
403 
404  // Iterate over the subdirectories to recursively delete them and their contents
405  while( valid )
406  {
407  dir.Prepend( fullDirName );
408 
409  // This call will also delete the actual directory, so we don't have to
410  if( !DeleteDirectory( dir, true, aIncludeHidden ) )
411  {
412  mainDir.Close();
413  return false;
414  }
415  valid = mainDir.GetNext( &dir );
416  }
417 
418 
419  wxString file;
420  valid = mainDir.GetFirst( &file, wxEmptyString, wxDIR_FILES | hiddenFlag );
421 
422  // Iterate over the files to remove all of them from the directory
423  while( valid )
424  {
425  file.Prepend( fullDirName );
426 
427  if( !wxRemoveFile( file ) )
428  {
429  mainDir.Close();
430  return false;
431  }
432  valid = mainDir.GetNext( &file );
433  }
434 
435  mainDir.Close();
436 
437  // Now delete the actual directory
438  if( !wxRmdir( aDirName ) )
439  {
440  mainDir.Close();
441  return false;
442  }
443 
444  return true;
445 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:359
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:276
bool DeleteDirectory(const wxString &aDirName, bool aRecurse, bool aIncludeHidden)
Delete a directory and all of its contents recursively.
Definition: gestfich.cpp:384
This file is part of the common library TODO brief description.
This file is part of the common library.
void CopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Function CopyFile.
Definition: gestfich.cpp:363
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
#define NULL
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:52
void AddDelimiterString(wxString &string)
Function AddDelimiterString Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:42
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:112
void PrintFile(const wxString &file)
Definition: gestfich.cpp:351
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
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:101
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:378
#define _(s)
Definition: 3d_actions.cpp:33
The common library.
void OpenFile(const wxString &file)
Definition: gestfich.cpp:257
int ExecuteFile(wxWindow *frame, const wxString &ExecFile, const wxString &param, wxProcess *callback)
Function ExecuteFile calls the executable file ExecFile with the command line parameters param.
Definition: gestfich.cpp:174
bool CanPrintFile(const wxString &file)
Definition: gestfich.cpp:357
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:205