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_i.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,
60  wxString::Format( _( "Error occurred writing empty footprint library "
61  "table file.\n\n%s" ),
63  ioe.What() ) );
64  return false;
65  }
66 
67  return true;
68  }
69 
70  wxString fileName = m_filePicker1->GetPath();
71 
72  if( fileName.IsEmpty() )
73  {
74  DisplayError( this, _( "Please select a footprint library table file." ) );
75  return false;
76  }
77 
78  wxFileName fn = fileName;
79 
80  // Make sure the footprint library table to copy actually exists.
81  if( !fn.FileExists() )
82  {
83  DisplayError( this,
84  wxString::Format( _( "File \"%s\" not found." ), fn.GetFullPath() ) );
85  return false;
86  }
87 
88  // Make sure the footprint library table to copy is a valid footprint library table file.
89  FP_LIB_TABLE tmpTable;
90 
91  try
92  {
93  tmpTable.Load( fn.GetFullPath() );
94  }
95  catch( const IO_ERROR& ioe )
96  {
97  DisplayError( this,
98  wxString::Format( _( "File \"%s\" is not a valid footprint library table "
99  "file.\n\n%s" ), fn.GetFullPath(), ioe.What() ) );
100  return false;
101  }
102 
103  // Create the config path if it doesn't already exist.
104  wxFileName fpTableFileName = FP_LIB_TABLE::GetGlobalTableFileName();
105 
106  if( !fpTableFileName.DirExists() && !fpTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
107  {
108  DisplayError( this,
109  wxString::Format( _( "Cannot create global library table path \"%s\"." ),
110  fpTableFileName.GetPath() ) );
111  return false;
112  }
113 
114  // Copy the global footprint library table file to the user config.
115  if( !::wxCopyFile( fn.GetFullPath(), fpTableFileName.GetFullPath() ) )
116  {
117  DisplayError( this,
118  wxString::Format( _( "Cannot copy global footprint library table "
119  "file:\n\n \"%s\"\n\n:to:\n\n\"%s\"." ),
120  fn.GetFullPath(), fpTableFileName.GetFullPath() ) );
121  return false;
122  }
123 
124  // Load the successfully copied footprint library table file. This should not fail
125  // since the file was tested above. Check for failure anyway to keep the compiler
126  // from complaining.
127  try
128  {
130  return false;
131  }
132  catch( const IO_ERROR& ioe )
133  {
134  DisplayError( this,
135  wxString::Format( _( "Error occurred loading global footprint library table:"
136  "\n\n%s" ), ioe.What() ) );
137  return false;
138  }
139 
140  return true;
141 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:253
FP_LIB_TABLE GFootprintTable
!!!!!!!!!!!!!! This code is obsolete because of the merge into pcbnew, don't bother with it.
Definition: cvpcb.cpp:120
This file is part of the common library.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
static bool LoadGlobalTable(FP_LIB_TABLE &aTable)
Load the global footprint library table into aTable.
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
#define _(s)
Definition: 3d_actions.cpp:33
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()