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) 2017 Wayne Stambaugh <stambaughw@gmail.com>
5  * Copyright (C) 2017 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 <grid_tricks.h>
25 #include <kiface_i.h>
26 #include <lib_table_grid.h>
27 #include <lib_table_lexer.h>
28 #include <macros.h>
29 
30 #include <wx/filename.h>
31 
32 #include "symbol_lib_table.h"
33 
34 
37 {
39 
40  // Attempt to find the default global file table from the KiCad template folder.
41  wxString fileName = Kiface().KifaceSearch().FindValidPath( fn.GetName() );
42 
43  m_defaultFileFound = wxFileName::FileExists( fileName );
44  m_filePicker1->SetFileName( wxFileName( fileName ) );
45 
46  if( !m_defaultFileFound )
47  m_customRb->SetValue( true );
48 
49  wxButton* okButton = (wxButton *) FindWindowById( wxID_OK );
50 
51  if( okButton )
52  okButton->SetDefault();
53 
55 }
56 
57 
59 {
60 }
61 
62 
64 {
65  aEvent.Enable( m_customRb->GetValue() );
66 }
67 
68 
70 {
71  aEvent.Enable( m_defaultFileFound );
72 }
73 
74 
76 {
77  // Create an empty table if requested by the user.
78  if( m_emptyRb->GetValue() )
79  {
80  SYMBOL_LIB_TABLE emptyTable;
81 
82  try
83  {
85  }
86  catch( const IO_ERROR& ioe )
87  {
88  DisplayError( this,
89  wxString::Format( _( "Error occurred writing empty symbol library table "
90  "file.\n\n%s" ),
92  ioe.What() ) );
93  return false;
94  }
95 
96  return true;
97  }
98 
99  wxString fileName = m_filePicker1->GetPath();
100 
101  if( fileName.IsEmpty() )
102  {
103  DisplayError( this, _( "Please select a symbol library table file." ) );
104  return false;
105  }
106 
107  wxFileName fn = fileName;
108 
109  // Make sure the symbol library table to copy actually exists.
110  if( !fn.FileExists() )
111  {
112  DisplayError( this,
113  wxString::Format( _( "File \"%s\" not found." ), fn.GetFullPath() ) );
114  return false;
115  }
116 
117  // Make sure the symbol library table to copy is a valid symbol library table file.
118  SYMBOL_LIB_TABLE tmpTable;
119 
120  try
121  {
122  tmpTable.Load( fn.GetFullPath() );
123  }
124  catch( const IO_ERROR& ioe )
125  {
126  DisplayError( this,
127  wxString::Format( _( "File \"%s\" is not a valid symbol library table "
128  "file.\n\n%s" ), fn.GetFullPath(), ioe.What() ) );
129  return false;
130  }
131 
132  // Create the config path if it doesn't already exist.
133  wxFileName symTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
134 
135  if( !symTableFileName.DirExists() && !symTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
136  {
137  DisplayError( this,
138  wxString::Format( _( "Cannot create global library table path \"%s\"." ),
139  symTableFileName.GetPath() ) );
140  return false;
141  }
142 
143  // Copy the global symbol library table file to the user config.
144  if( !::wxCopyFile( fn.GetFullPath(), symTableFileName.GetFullPath() ) )
145  {
146  DisplayError( this,
147  wxString::Format( _( "Cannot copy global symbol library table "
148  "file:\n\n \"%s\"\n\n:to:\n\n\"%s\"." ),
149  fn.GetFullPath(), symTableFileName.GetFullPath() ) );
150  return false;
151  }
152 
153  // Load the successfully copied symbol library table file. This should not fail
154  // since the file was tested above. Check for failure anyway to keep the compiler
155  // from complaining.
156  try
157  {
159  return false;
160  }
161  catch( const IO_ERROR& ioe )
162  {
163  DisplayError( this,
164  wxString::Format( _( "Error occurred loading global symbol library table:"
165  "\n\n%s" ), ioe.What() ) );
166  return false;
167  }
168 
169  return true;
170 }
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
virtual void onUpdateFilePicker(wxUpdateUIEvent &aEvent) override
This file is part of the common library.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
This file contains miscellaneous commonly used macros and functions.
SEARCH_STACK & KifaceSearch()
Only for DSO specific &#39;non-library&#39; files.
Definition: kiface_i.h:127
static bool LoadGlobalTable(SYMBOL_LIB_TABLE &aTable)
Load the global symbol library table into aTable.
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
virtual void onUpdateDefaultSelection(wxUpdateUIEvent &aEvent) override
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:73
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
static wxString GetGlobalTableFileName()
Fetch the global symbol library table file name.
Class DIALOG_GLOBAL_SYM_LIB_TABLE_CONFIG_BASE.