You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
9.3 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
19 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
19 years ago
18 years ago
18 years ago
18 years ago
18 years ago
19 years ago
19 years ago
19 years ago
  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 Wayne Stambaugh <stambaughw@gmail.com>
  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. /**
  26. * @file gestfich.cpp
  27. * @brief Functions for file management
  28. */
  29. #include <wx/mimetype.h>
  30. #include <wx/dir.h>
  31. #include <pgm_base.h>
  32. #include <confirm.h>
  33. #include <core/arraydim.h>
  34. #include <gestfich.h>
  35. #include <string_utils.h>
  36. #include <launch_ext.h>
  37. #include "wx/tokenzr.h"
  38. #include <filesystem>
  39. void QuoteString( wxString& string )
  40. {
  41. if( !string.StartsWith( wxT( "\"" ) ) )
  42. {
  43. string.Prepend ( wxT( "\"" ) );
  44. string.Append ( wxT( "\"" ) );
  45. }
  46. }
  47. wxString FindKicadFile( const wxString& shortname )
  48. {
  49. // Test the presence of the file in the directory shortname of
  50. // the KiCad binary path.
  51. #ifndef __WXMAC__
  52. wxString fullFileName = Pgm().GetExecutablePath() + shortname;
  53. #else
  54. wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
  55. #endif
  56. if( wxFileExists( fullFileName ) )
  57. return fullFileName;
  58. if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
  59. {
  60. wxFileName buildDir( Pgm().GetExecutablePath(), shortname );
  61. buildDir.RemoveLastDir();
  62. #ifndef __WXMSW__
  63. buildDir.AppendDir( shortname );
  64. #else
  65. buildDir.AppendDir( shortname.BeforeLast( '.' ) );
  66. #endif
  67. if( buildDir.GetDirs().Last() == "pl_editor" )
  68. {
  69. buildDir.RemoveLastDir();
  70. buildDir.AppendDir( "pagelayout_editor" );
  71. }
  72. if( wxFileExists( buildDir.GetFullPath() ) )
  73. return buildDir.GetFullPath();
  74. }
  75. // Test the presence of the file in the directory shortname
  76. // defined by the environment variable KiCad.
  77. if( Pgm().IsKicadEnvVariableDefined() )
  78. {
  79. fullFileName = Pgm().GetKicadEnvVariable() + shortname;
  80. if( wxFileExists( fullFileName ) )
  81. return fullFileName;
  82. }
  83. #if defined( __WINDOWS__ )
  84. // kicad can be installed highly portably on Windows, anywhere and concurrently
  85. // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
  86. return shortname;
  87. #else
  88. // Path list for KiCad binary files
  89. const static wxChar* possibilities[] = {
  90. #if defined( __WXMAC__ )
  91. // all internal paths are relative to main bundle kicad.app
  92. wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
  93. wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
  94. wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
  95. wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
  96. wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
  97. wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
  98. #else
  99. wxT( "/usr/bin/" ),
  100. wxT( "/usr/local/bin/" ),
  101. wxT( "/usr/local/kicad/bin/" ),
  102. #endif
  103. };
  104. // find binary file from possibilities list:
  105. for( unsigned i=0; i<arrayDim(possibilities); ++i )
  106. {
  107. #ifndef __WXMAC__
  108. fullFileName = possibilities[i] + shortname;
  109. #else
  110. // make relative paths absolute
  111. fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
  112. #endif
  113. if( wxFileExists( fullFileName ) )
  114. return fullFileName;
  115. }
  116. return shortname;
  117. #endif
  118. }
  119. int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess* aCallback,
  120. bool aFileForKicad )
  121. {
  122. wxString fullEditorName;
  123. std::vector<wxString> params;
  124. #ifdef __UNIX__
  125. wxString param;
  126. bool inSingleQuotes = false;
  127. bool inDoubleQuotes = false;
  128. auto pushParam =
  129. [&]()
  130. {
  131. if( !param.IsEmpty() )
  132. {
  133. params.push_back( param );
  134. param.clear();
  135. }
  136. };
  137. for( wxUniChar ch : aEditorName )
  138. {
  139. if( inSingleQuotes )
  140. {
  141. if( ch == '\'' )
  142. {
  143. pushParam();
  144. inSingleQuotes = false;
  145. continue;
  146. }
  147. else
  148. {
  149. param += ch;
  150. }
  151. }
  152. else if( inDoubleQuotes )
  153. {
  154. if( ch == '"' )
  155. {
  156. pushParam();
  157. inDoubleQuotes = false;
  158. }
  159. else
  160. {
  161. param += ch;
  162. }
  163. }
  164. else if( ch == '\'' )
  165. {
  166. pushParam();
  167. inSingleQuotes = true;
  168. }
  169. else if( ch == '"' )
  170. {
  171. pushParam();
  172. inDoubleQuotes = true;
  173. }
  174. else if( ch == ' ' )
  175. {
  176. pushParam();
  177. }
  178. else
  179. {
  180. param += ch;
  181. }
  182. }
  183. pushParam();
  184. if( aFileForKicad )
  185. fullEditorName = FindKicadFile( params[0] );
  186. else
  187. fullEditorName = params[0];
  188. params.erase( params.begin() );
  189. #else
  190. if( aFileForKicad )
  191. fullEditorName = FindKicadFile( aEditorName );
  192. else
  193. fullEditorName = aEditorName;
  194. #endif
  195. if( wxFileExists( fullEditorName ) )
  196. {
  197. std::vector<const wchar_t*> args;
  198. args.emplace_back( fullEditorName.wc_str() );
  199. if( !params.empty() )
  200. {
  201. for( const wxString& p : params )
  202. args.emplace_back( p.wc_str() );
  203. }
  204. if( !aFileName.IsEmpty() )
  205. args.emplace_back( aFileName.wc_str() );
  206. args.emplace_back( nullptr );
  207. return wxExecute( const_cast<wchar_t**>( args.data() ), wxEXEC_ASYNC, aCallback );
  208. }
  209. wxString msg;
  210. msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
  211. DisplayError( nullptr, msg, 20 );
  212. return -1;
  213. }
  214. bool OpenPDF( const wxString& file )
  215. {
  216. wxString msg;
  217. wxString filename = file;
  218. Pgm().ReadPdfBrowserInfos();
  219. if( Pgm().UseSystemPdfBrowser() )
  220. {
  221. if( !LaunchExternal( filename ) )
  222. {
  223. msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
  224. DisplayError( nullptr, msg );
  225. return false;
  226. }
  227. }
  228. else
  229. {
  230. const wchar_t* args[3];
  231. args[0] = Pgm().GetPdfBrowserName().wc_str();
  232. args[1] = filename.wc_str();
  233. args[2] = nullptr;
  234. if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
  235. {
  236. msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
  237. DisplayError( nullptr, msg );
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. void OpenFile( const wxString& file )
  244. {
  245. wxFileName fileName( file );
  246. wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
  247. if( !filetype )
  248. return;
  249. wxString command;
  250. wxFileType::MessageParameters params( file );
  251. filetype->GetOpenCommand( &command, params );
  252. delete filetype;
  253. if( !command.IsEmpty() )
  254. wxExecute( command );
  255. }
  256. void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
  257. {
  258. if( !wxCopyFile( aSrcPath, aDestPath ) )
  259. {
  260. wxString msg;
  261. if( !aErrors.IsEmpty() )
  262. aErrors += "\n";
  263. msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
  264. aErrors += msg;
  265. }
  266. }
  267. wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
  268. {
  269. return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
  270. }
  271. bool RmDirRecursive( const wxString& aFileName, wxString* aErrors )
  272. {
  273. namespace fs = std::filesystem;
  274. std::string rmDir = aFileName.ToStdString();
  275. if( rmDir.length() < 3 )
  276. {
  277. if( aErrors )
  278. *aErrors = _( "Invalid directory name, cannot remove root" );
  279. return false;
  280. }
  281. if( !fs::exists( rmDir ) )
  282. {
  283. if( aErrors )
  284. *aErrors = wxString::Format( _( "Directory '%s' does not exist" ), aFileName );
  285. return false;
  286. }
  287. fs::path path( rmDir );
  288. if( !fs::is_directory( path ) )
  289. {
  290. if( aErrors )
  291. *aErrors = wxString::Format( _( "'%s' is not a directory" ), aFileName );
  292. return false;
  293. }
  294. try
  295. {
  296. fs::remove_all( path );
  297. }
  298. catch( const fs::filesystem_error& e )
  299. {
  300. if( aErrors )
  301. *aErrors = wxString::Format( _( "Error removing directory '%s': %s" ), aFileName, e.what() );
  302. return false;
  303. }
  304. return true;
  305. }