KiCad PCB EDA Suite
gestfich.cpp File Reference

Functions for file management. More...

#include <wx/mimetype.h>
#include <wx/filename.h>
#include <wx/dir.h>
#include <pgm_base.h>
#include <confirm.h>
#include <core/arraydim.h>
#include <gestfich.h>
#include <string_utils.h>
#include <launch_ext.h>

Go to the source code of this file.

Functions

void QuoteString (wxString &string)
 Add un " to the start and the end of string (if not already done). More...
 
wxString FindKicadFile (const wxString &shortname)
 Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin. More...
 
int ExecuteFile (const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback)
 Call the executable file aEditorName with the parameter aFileName. More...
 
bool OpenPDF (const wxString &file)
 Run the PDF viewer and display a PDF file. More...
 
void OpenFile (const wxString &file)
 
void KiCopyFile (const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
 
wxString QuoteFullPath (wxFileName &fn, wxPathFormat format)
 Quote return value of wxFileName::GetFullPath(). More...
 

Detailed Description

Functions for file management.

Definition in file gestfich.cpp.

Function Documentation

◆ ExecuteFile()

int ExecuteFile ( const wxString &  aEditorName,
const wxString &  aFileName,
wxProcess *  aCallback 
)

Call the executable file aEditorName with the parameter aFileName.

Definition at line 115 of file gestfich.cpp.

116{
117 wxString fullEditorName;
118 wxString param;
119
120#ifdef __UNIX__
121 int space = aEditorName.Find( ' ' );
122
123 if( space > 0 && !aEditorName.Contains( "\"" ) && !aEditorName.Contains( "\'" ) )
124 {
125 fullEditorName = FindKicadFile( aEditorName.Mid( 0, space ) );
126 param = aEditorName.Mid( space + 1 );
127 }
128 else
129#endif
130 {
131 fullEditorName = FindKicadFile( aEditorName );
132 }
133
134 if( wxFileExists( fullEditorName ) )
135 {
136 int i = 0;
137 const wchar_t* args[4];
138
139 args[i++] = fullEditorName.wc_str();
140
141 if( !param.IsEmpty() )
142 args[i++] = param.wc_str();
143
144 if( !aFileName.IsEmpty() )
145 args[i++] = aFileName.wc_str();
146
147 args[i] = nullptr;
148
149 return wxExecute( const_cast<wchar_t**>( args ), wxEXEC_ASYNC, aCallback );
150 }
151
152 wxString msg;
153 msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
154 DisplayError( nullptr, msg, 20 );
155 return -1;
156}
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
#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:52

References _, DisplayError(), and FindKicadFile().

Referenced by KICAD_MANAGER_CONTROL::Execute(), DIALOG_CONFIG_EQUFILES::OnEditEquFile(), DIALOG_BOM::OnEditGenerator(), SCH_EDIT_FRAME::OnOpenPcbnew(), PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor(), PCB_EDIT_FRAME::RunEeschema(), and GERBVIEW_INSPECTION_TOOL::ShowSource().

◆ FindKicadFile()

wxString FindKicadFile ( const wxString &  shortname)

Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin.

The binary path is found from:

  • binary path.
  • KICAD environment variable.
  • c:\kicad or /usr/local/kicad (the default).
  • default binary path.

Definition at line 52 of file gestfich.cpp.

53{
54 // Test the presence of the file in the directory shortname of
55 // the KiCad binary path.
56#ifndef __WXMAC__
57 wxString fullFileName = Pgm().GetExecutablePath() + shortname;
58#else
59 wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
60#endif
61 if( wxFileExists( fullFileName ) )
62 return fullFileName;
63
64 // Test the presence of the file in the directory shortname
65 // defined by the environment variable KiCad.
66 if( Pgm().IsKicadEnvVariableDefined() )
67 {
68 fullFileName = Pgm().GetKicadEnvVariable() + shortname;
69
70 if( wxFileExists( fullFileName ) )
71 return fullFileName;
72 }
73
74#if defined( __WINDOWS__ )
75 // kicad can be installed highly portably on Windows, anywhere and concurrently
76 // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
77 return shortname;
78#endif
79
80 // Path list for KiCad binary files
81 const static wxChar* possibilities[] = {
82#if defined( __WXMAC__ )
83 // all internal paths are relative to main bundle kicad.app
84 wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
85 wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
86 wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
87 wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
88 wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
89 wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
90#else
91 wxT( "/usr/bin/" ),
92 wxT( "/usr/local/bin/" ),
93 wxT( "/usr/local/kicad/bin/" ),
94#endif
95 };
96
97 // find binary file from possibilities list:
98 for( unsigned i=0; i<arrayDim(possibilities); ++i )
99 {
100#ifndef __WXMAC__
101 fullFileName = possibilities[i] + shortname;
102#else
103 // make relative paths absolute
104 fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
105#endif
106
107 if( wxFileExists( fullFileName ) )
108 return fullFileName;
109 }
110
111 return shortname;
112}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111

References arrayDim(), and Pgm().

Referenced by KICAD_MANAGER_FRAME::DoWithAcceptedFiles(), and ExecuteFile().

◆ KiCopyFile()

void KiCopyFile ( const wxString &  aSrcPath,
const wxString &  aDestPath,
wxString &  aErrors 
)
Parameters
aSrcPathis the full filename of the source.
aDestPathis the full filename of the target
aErrorsa wxString to append any errors to

Definition at line 214 of file gestfich.cpp.

215{
216 if( !wxCopyFile( aSrcPath, aDestPath ) )
217 {
218 wxString msg;
219
220 if( !aErrors.IsEmpty() )
221 aErrors += "\n";
222
223 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
224 aErrors += msg;
225 }
226}

References _.

Referenced by MIGRATION_TRAVERSER::OnFile(), SAVE_AS_TRAVERSER::OnFile(), SCH::IFACE::SaveFileAs(), GERBV::IFACE::SaveFileAs(), PGE::IFACE::SaveFileAs(), PCB::IFACE::SaveFileAs(), PCB_EDIT_FRAME::SavePcbCopy(), and PCB_EDIT_FRAME::SavePcbFile().

◆ OpenFile()

void OpenFile ( const wxString &  file)

Definition at line 195 of file gestfich.cpp.

196{
197 wxFileName fileName( file );
198 wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
199
200 if( !filetype )
201 return;
202
203 wxString command;
204 wxFileType::MessageParameters params( file );
205
206 filetype->GetOpenCommand( &command, params );
207 delete filetype;
208
209 if( !command.IsEmpty() )
210 wxExecute( command );
211}

Referenced by PROJECT_TREE_ITEM::Activate().

◆ OpenPDF()

bool OpenPDF ( const wxString &  file)

Run the PDF viewer and display a PDF file.

Parameters
filethe PDF file to open.
Returns
true is success or false if no PDF viewer found.

Definition at line 159 of file gestfich.cpp.

160{
161 wxString msg;
162 wxString filename = file;
163
164 Pgm().ReadPdfBrowserInfos();
165
166 if( Pgm().UseSystemPdfBrowser() )
167 {
168 if( !LaunchExternal( filename ) )
169 {
170 msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
171 DisplayError( nullptr, msg );
172 return false;
173 }
174 }
175 else
176 {
177 const wchar_t* args[3];
178
179 args[0] = Pgm().GetPdfBrowserName().wc_str();
180 args[1] = filename.wc_str();
181 args[2] = nullptr;
182
183 if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
184 {
185 msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
186 DisplayError( nullptr, msg );
187 return false;
188 }
189 }
190
191 return true;
192}
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25

References _, DisplayError(), LaunchExternal(), and Pgm().

Referenced by PROJECT_TREE_ITEM::Activate(), and GetAssociatedDocument().

◆ QuoteFullPath()

wxString QuoteFullPath ( wxFileName &  fn,
wxPathFormat  format = wxPATH_NATIVE 
)

Quote return value of wxFileName::GetFullPath().

This allows file name paths with spaces to be used as parameters to ProcessExecute function calls.

Parameters
fnis the filename to wrap.
formatif provided, can be used to transform the nature of the wrapped filename to another platform.

Definition at line 229 of file gestfich.cpp.

230{
231 return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
232}

◆ QuoteString()

void QuoteString ( wxString &  string)

Add un " to the start and the end of string (if not already done).

Parameters
stringstring to modify.

Definition at line 42 of file gestfich.cpp.

43{
44 if( !string.StartsWith( wxT( "\"" ) ) )
45 {
46 string.Prepend ( wxT( "\"" ) );
47 string.Append ( wxT( "\"" ) );
48 }
49}