KiCad PCB EDA Suite
dialog_global_sym_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) 2017-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 #include <macros.h>
26 
27 #include "symbol_lib_table.h"
28 
29 
31  DIALOG_GLOBAL_LIB_TABLE_CONFIG( aParent, _( "symbol" ) )
32 {
33 }
34 
35 
37 {
38 }
39 
40 
42 {
44 }
45 
46 
48 {
49  // Create an empty table if requested by the user.
50  if( m_emptyRb->GetValue() )
51  {
52  SYMBOL_LIB_TABLE emptyTable;
53 
54  try
55  {
57  }
58  catch( const IO_ERROR& ioe )
59  {
60  DisplayError( this, wxString::Format( _( "Error occurred writing empty symbol library "
61  "table.\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 symbol library table file." ) );
75  return false;
76  }
77 
78  wxFileName fn = fileName;
79 
80  // Make sure the symbol library table to copy actually exists.
81  if( !fn.FileExists() )
82  {
83  DisplayError( this, wxString::Format( _( "File '%s' not found." ), fn.GetFullPath() ) );
84  return false;
85  }
86 
87  // Make sure the symbol library table to copy is a valid symbol library table file.
88  SYMBOL_LIB_TABLE tmpTable;
89 
90  try
91  {
92  tmpTable.Load( fn.GetFullPath() );
93  }
94  catch( const IO_ERROR& ioe )
95  {
96  DisplayError( this, wxString::Format( _( "'%s' is not a valid symbol library table.\n\n%s" ),
97  fn.GetFullPath(),
98  ioe.What() ) );
99  return false;
100  }
101 
102  // Create the config path if it doesn't already exist.
103  wxFileName symTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
104 
105  if( !symTableFileName.DirExists() && !symTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
106  {
107  DisplayError( this, wxString::Format( _( "Cannot create global library table path '%s'." ),
108  symTableFileName.GetPath() ) );
109  return false;
110  }
111 
112  // Copy the global symbol library table file to the user config.
113  if( !::wxCopyFile( fn.GetFullPath(), symTableFileName.GetFullPath() ) )
114  {
115  DisplayError( this, wxString::Format( _( "Cannot copy global symbol library table file "
116  "'%s' to '%s'." ),
117  fn.GetFullPath(),
118  symTableFileName.GetFullPath() ) );
119  return false;
120  }
121 
122  // Load the successfully copied symbol library table file. This should not fail
123  // since the file was tested above. Check for failure anyway to keep the compiler
124  // from complaining.
125  try
126  {
128  return false;
129  }
130  catch( const IO_ERROR& ioe )
131  {
132  DisplayError( this, wxString::Format( _( "Error occurred loading global symbol library "
133  "table.\n\n%s" ),
134  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:252
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
This file is part of the common library.
This file contains miscellaneous commonly used macros and functions.
static bool LoadGlobalTable(SYMBOL_LIB_TABLE &aTable)
Load the global symbol library table into aTable.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
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()
Fetch the global symbol library table file name.