KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
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
22
23#include <confirm.h>
24#include <kiface_base.h>
25#include <kiway.h>
26
27#include "fp_lib_table.h"
28
29#include <kiplatform/io.h>
30
31
33 DIALOG_GLOBAL_LIB_TABLE_CONFIG( aParent, _( "footprint" ), KIWAY::FACE_PCB )
34{
35}
36
37
41
42
47
48
50{
51 // Create an empty table if requested by the user.
52 if( m_emptyRb->GetValue() )
53 {
54 FP_LIB_TABLE emptyTable;
55
56 try
57 {
59 }
60 catch( const IO_ERROR& ioe )
61 {
62 DisplayError( this, wxString::Format( _( "Error occurred writing empty footprint "
63 "library table '%s'." ),
65 + wxS( "\n" ) + ioe.What() );
66 return false;
67 }
68 }
69 else
70 {
71 wxString fileName = m_filePicker1->GetPath();
72
73 if( fileName.IsEmpty() )
74 {
75 DisplayError( this, _( "Please select a footprint library table file." ) );
76 return false;
77 }
78
79 wxFileName fn = fileName;
80
81 // Make sure the footprint library table to copy actually exists.
82 if( !fn.FileExists() )
83 {
84 DisplayError( this, 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( _( "'%s' is not a valid footprint library table." ),
99 fn.GetFullPath() )
100 + wxS( "\n" ) + ioe.What() );
101 return false;
102 }
103
104 // Create the config path if it doesn't already exist.
105 wxFileName fpTableFileName = FP_LIB_TABLE::GetGlobalTableFileName();
106
107 if( !wxFileName::DirExists( fpTableFileName.GetPath() )
108 && !wxFileName::Mkdir( fpTableFileName.GetPath(), 0x777, wxPATH_MKDIR_FULL ) )
109 {
110 DisplayError( this, wxString::Format( _( "Cannot create library table path '%s'." ),
111 fpTableFileName.GetPath() ) );
112 return false;
113 }
114
115 // Copy the global footprint library table file to the user config.
116 if( !::wxCopyFile( fn.GetFullPath(), fpTableFileName.GetFullPath() ) )
117 {
118 DisplayError( this,
119 wxString::Format( _( "Cannot copy footprint library table from:\n"
120 "%s\n"
121 "to:\n"
122 "%s." ),
123 fn.GetFullPath(), fpTableFileName.GetFullPath() ) );
124 return false;
125 }
126
127 // Ensure the copied file is writable
128 KIPLATFORM::IO::MakeWriteable( fpTableFileName.GetFullPath() );
129 }
130
131 // Load the successfully copied footprint library table file. This should not fail
132 // since the file was tested above. Check for failure anyway to keep the compiler
133 // from complaining.
134 try
135 {
137 return false;
138 }
139 catch( const IO_ERROR& ioe )
140 {
141 DisplayError( this, _( "Error loading footprint library table." )
142 + wxS( "\n" ) + ioe.What() );
143 return false;
144 }
145
146 return true;
147}
DIALOG_GLOBAL_LIB_TABLE_CONFIG(wxWindow *aParent, const wxString &aTableName, const KIWAY::FACE_T aFaceType)
static bool LoadGlobalTable(FP_LIB_TABLE &aTable)
Load the global footprint library table into aTable.
static wxString GetGlobalTableFileName()
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:286
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:169
This file is part of the common library.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition cvpcb.cpp:150
#define _(s)
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:70