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.

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