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.

139 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "dialog_global_fp_lib_table_config.h"
  21. #include <confirm.h>
  22. #include <kiface_base.h>
  23. #include "fp_lib_table.h"
  24. DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG( wxWindow* aParent ) :
  25. DIALOG_GLOBAL_LIB_TABLE_CONFIG( aParent, _( "footprint" ) )
  26. {
  27. }
  28. DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::~DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG()
  29. {
  30. }
  31. wxFileName DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::GetGlobalTableFileName()
  32. {
  33. return FP_LIB_TABLE::GetGlobalTableFileName();
  34. }
  35. bool DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::TransferDataFromWindow()
  36. {
  37. // Create an empty table if requested by the user.
  38. if( m_emptyRb->GetValue() )
  39. {
  40. FP_LIB_TABLE emptyTable;
  41. try
  42. {
  43. emptyTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() );
  44. }
  45. catch( const IO_ERROR& ioe )
  46. {
  47. DisplayError( this, wxString::Format( _( "Error occurred writing empty footprint "
  48. "library table '%s'." ),
  49. FP_LIB_TABLE::GetGlobalTableFileName() )
  50. + wxS( "\n" ) + ioe.What() );
  51. return false;
  52. }
  53. return true;
  54. }
  55. wxString fileName = m_filePicker1->GetPath();
  56. if( fileName.IsEmpty() )
  57. {
  58. DisplayError( this, _( "Please select a footprint library table file." ) );
  59. return false;
  60. }
  61. wxFileName fn = fileName;
  62. // Make sure the footprint library table to copy actually exists.
  63. if( !fn.FileExists() )
  64. {
  65. DisplayError( this, wxString::Format( _( "File '%s' not found." ), fn.GetFullPath() ) );
  66. return false;
  67. }
  68. // Make sure the footprint library table to copy is a valid footprint library table file.
  69. FP_LIB_TABLE tmpTable;
  70. try
  71. {
  72. tmpTable.Load( fn.GetFullPath() );
  73. }
  74. catch( const IO_ERROR& ioe )
  75. {
  76. DisplayError( this, wxString::Format( _( "'%s' is not a valid footprint library table." ),
  77. fn.GetFullPath() )
  78. + wxS( "\n" ) + ioe.What() );
  79. return false;
  80. }
  81. // Create the config path if it doesn't already exist.
  82. wxFileName fpTableFileName = FP_LIB_TABLE::GetGlobalTableFileName();
  83. if( !fpTableFileName.DirExists() && !fpTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
  84. {
  85. DisplayError( this, wxString::Format( _( "Cannot create library table path '%s'." ),
  86. fpTableFileName.GetPath() ) );
  87. return false;
  88. }
  89. // Copy the global footprint library table file to the user config.
  90. if( !::wxCopyFile( fn.GetFullPath(), fpTableFileName.GetFullPath() ) )
  91. {
  92. DisplayError( this, wxString::Format( _( "Cannot copy footprint library table from:\n"
  93. "%s\n"
  94. "to:\n"
  95. "%s." ),
  96. fn.GetFullPath(),
  97. fpTableFileName.GetFullPath() ) );
  98. return false;
  99. }
  100. // Load the successfully copied footprint library table file. This should not fail
  101. // since the file was tested above. Check for failure anyway to keep the compiler
  102. // from complaining.
  103. try
  104. {
  105. if( !FP_LIB_TABLE::LoadGlobalTable( GFootprintTable ) )
  106. return false;
  107. }
  108. catch( const IO_ERROR& ioe )
  109. {
  110. DisplayError( this, _( "Error loading footprint library table." )
  111. + wxS( "\n" ) + ioe.What() );
  112. return false;
  113. }
  114. return true;
  115. }