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.

141 lines
4.7 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 The 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 <kiway.h>
  24. #include "fp_lib_table.h"
  25. DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG( wxWindow* aParent ) :
  26. DIALOG_GLOBAL_LIB_TABLE_CONFIG( aParent, _( "footprint" ), KIWAY::FACE_PCB )
  27. {
  28. }
  29. DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::~DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG()
  30. {
  31. }
  32. wxFileName DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::GetGlobalTableFileName()
  33. {
  34. return FP_LIB_TABLE::GetGlobalTableFileName();
  35. }
  36. bool DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::TransferDataFromWindow()
  37. {
  38. // Create an empty table if requested by the user.
  39. if( m_emptyRb->GetValue() )
  40. {
  41. FP_LIB_TABLE emptyTable;
  42. try
  43. {
  44. emptyTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() );
  45. }
  46. catch( const IO_ERROR& ioe )
  47. {
  48. DisplayError( this, wxString::Format( _( "Error occurred writing empty footprint "
  49. "library table '%s'." ),
  50. FP_LIB_TABLE::GetGlobalTableFileName() )
  51. + wxS( "\n" ) + ioe.What() );
  52. return false;
  53. }
  54. }
  55. else
  56. {
  57. wxString fileName = m_filePicker1->GetPath();
  58. if( fileName.IsEmpty() )
  59. {
  60. DisplayError( this, _( "Please select a footprint library table file." ) );
  61. return false;
  62. }
  63. wxFileName fn = fileName;
  64. // Make sure the footprint library table to copy actually exists.
  65. if( !fn.FileExists() )
  66. {
  67. DisplayError( this, wxString::Format( _( "File '%s' not found." ), fn.GetFullPath() ) );
  68. return false;
  69. }
  70. // Make sure the footprint library table to copy is a valid footprint library table file.
  71. FP_LIB_TABLE tmpTable;
  72. try
  73. {
  74. tmpTable.Load( fn.GetFullPath() );
  75. }
  76. catch( const IO_ERROR& ioe )
  77. {
  78. DisplayError( this,
  79. wxString::Format( _( "'%s' is not a valid footprint library table." ),
  80. fn.GetFullPath() )
  81. + wxS( "\n" ) + ioe.What() );
  82. return false;
  83. }
  84. // Create the config path if it doesn't already exist.
  85. wxFileName fpTableFileName = FP_LIB_TABLE::GetGlobalTableFileName();
  86. if( !fpTableFileName.DirExists() && !fpTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
  87. {
  88. DisplayError( this, wxString::Format( _( "Cannot create library table path '%s'." ),
  89. fpTableFileName.GetPath() ) );
  90. return false;
  91. }
  92. // Copy the global footprint library table file to the user config.
  93. if( !::wxCopyFile( fn.GetFullPath(), fpTableFileName.GetFullPath() ) )
  94. {
  95. DisplayError( this,
  96. wxString::Format( _( "Cannot copy footprint library table from:\n"
  97. "%s\n"
  98. "to:\n"
  99. "%s." ),
  100. fn.GetFullPath(), fpTableFileName.GetFullPath() ) );
  101. return false;
  102. }
  103. }
  104. // Load the successfully copied footprint library table file. This should not fail
  105. // since the file was tested above. Check for failure anyway to keep the compiler
  106. // from complaining.
  107. try
  108. {
  109. if( !FP_LIB_TABLE::LoadGlobalTable( GFootprintTable ) )
  110. return false;
  111. }
  112. catch( const IO_ERROR& ioe )
  113. {
  114. DisplayError( this, _( "Error loading footprint library table." )
  115. + wxS( "\n" ) + ioe.What() );
  116. return false;
  117. }
  118. return true;
  119. }