KiCad PCB EDA Suite
dialog_global_fp_lib_table_config.cpp
Go to the documentation of this file.
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 
22 
23 #include <confirm.h>
24 #include <kiface_base.h>
25 
26 #include "fp_lib_table.h"
27 
28 
30  DIALOG_GLOBAL_LIB_TABLE_CONFIG( aParent, _( "footprint" ) )
31 {
32 }
33 
34 
36 {
37 }
38 
39 
41 {
43 }
44 
45 
47 {
48  // Create an empty table if requested by the user.
49  if( m_emptyRb->GetValue() )
50  {
51  FP_LIB_TABLE emptyTable;
52 
53  try
54  {
56  }
57  catch( const IO_ERROR& ioe )
58  {
59  DisplayError( this, wxString::Format( _( "Error occurred writing empty footprint "
60  "library table '%s'." ),
62  + wxS( "\n" ) + ioe.What() );
63  return false;
64  }
65 
66  return true;
67  }
68 
69  wxString fileName = m_filePicker1->GetPath();
70 
71  if( fileName.IsEmpty() )
72  {
73  DisplayError( this, _( "Please select a footprint library table file." ) );
74  return false;
75  }
76 
77  wxFileName fn = fileName;
78 
79  // Make sure the footprint library table to copy actually exists.
80  if( !fn.FileExists() )
81  {
82  DisplayError( this, wxString::Format( _( "File '%s' not found." ), fn.GetFullPath() ) );
83  return false;
84  }
85 
86  // Make sure the footprint library table to copy is a valid footprint library table file.
87  FP_LIB_TABLE tmpTable;
88 
89  try
90  {
91  tmpTable.Load( fn.GetFullPath() );
92  }
93  catch( const IO_ERROR& ioe )
94  {
95  DisplayError( this, wxString::Format( _( "'%s' is not a valid footprint library table." ),
96  fn.GetFullPath() )
97  + wxS( "\n" ) + ioe.What() );
98  return false;
99  }
100 
101  // Create the config path if it doesn't already exist.
102  wxFileName fpTableFileName = FP_LIB_TABLE::GetGlobalTableFileName();
103 
104  if( !fpTableFileName.DirExists() && !fpTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
105  {
106  DisplayError( this, wxString::Format( _( "Cannot create library table path '%s'." ),
107  fpTableFileName.GetPath() ) );
108  return false;
109  }
110 
111  // Copy the global footprint library table file to the user config.
112  if( !::wxCopyFile( fn.GetFullPath(), fpTableFileName.GetFullPath() ) )
113  {
114  DisplayError( this, wxString::Format( _( "Cannot copy footprint library table from:\n"
115  "%s\n"
116  "to:\n"
117  "%s." ),
118  fn.GetFullPath(),
119  fpTableFileName.GetFullPath() ) );
120  return false;
121  }
122 
123  // Load the successfully copied footprint library table file. This should not fail
124  // since the file was tested above. Check for failure anyway to keep the compiler
125  // from complaining.
126  try
127  {
129  return false;
130  }
131  catch( const IO_ERROR& ioe )
132  {
133  DisplayError( this, _( "Error loading footprint library table." )
134  + wxS( "\n" ) + ioe.What() );
135  return false;
136  }
137 
138  return true;
139 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: cvpcb.cpp:134
This file is part of the common library.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
static bool LoadGlobalTable(FP_LIB_TABLE &aTable)
Load the global footprint library table into aTable.
#define _(s)
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
static wxString GetGlobalTableFileName()