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-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 <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 
45  m_filePicker_new = new wxFilePickerCtrl( this, wxID_ANY, wxEmptyString, _( "Select a file" ),
46  wxFileSelectorDefaultWildcardStr, wxDefaultPosition, wxDefaultSize,
47  wxFLP_DEFAULT_STYLE | wxFLP_FILE_MUST_EXIST | wxFLP_OPEN );
48  m_filePicker_new->SetFileName( wxFileName( fileName ) );
49  m_filePicker_new->Enable( false );
50  m_filePicker_new->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_GLOBAL_SYM_LIB_TABLE_CONFIG::onUpdateFilePicker ), NULL, this );
51 
52  bSizer2->Replace( m_filePicker1, m_filePicker_new, true );
53  bSizer2->Layout();
54 
55  if( !m_defaultFileFound )
56  m_customRb->SetValue( true );
57 
58  wxButton* okButton = (wxButton *) FindWindowById( wxID_OK );
59 
60  if( okButton )
61  okButton->SetDefault();
62 
64 }
65 
66 
68 {
69  m_filePicker_new->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_GLOBAL_SYM_LIB_TABLE_CONFIG::onUpdateFilePicker ), NULL, this );
70 }
71 
72 
74 {
75  aEvent.Enable( m_customRb->GetValue() );
76 }
77 
78 
80 {
81  aEvent.Enable( m_defaultFileFound );
82 }
83 
84 
86 {
87  // Create an empty table if requested by the user.
88  if( m_emptyRb->GetValue() )
89  {
90  SYMBOL_LIB_TABLE emptyTable;
91 
92  try
93  {
95  }
96  catch( const IO_ERROR& ioe )
97  {
98  DisplayError( this,
99  wxString::Format( _( "Error occurred writing empty symbol library table "
100  "file.\n\n%s" ),
102  ioe.What() ) );
103  return false;
104  }
105 
106  return true;
107  }
108 
109  wxString fileName = m_filePicker_new->GetPath();
110 
111  if( fileName.IsEmpty() )
112  {
113  DisplayError( this, _( "Please select a symbol library table file." ) );
114  return false;
115  }
116 
117  wxFileName fn = fileName;
118 
119  // Make sure the symbol library table to copy actually exists.
120  if( !fn.FileExists() )
121  {
122  DisplayError( this,
123  wxString::Format( _( "File \"%s\" not found." ), fn.GetFullPath() ) );
124  return false;
125  }
126 
127  // Make sure the symbol library table to copy is a valid symbol library table file.
128  SYMBOL_LIB_TABLE tmpTable;
129 
130  try
131  {
132  tmpTable.Load( fn.GetFullPath() );
133  }
134  catch( const IO_ERROR& ioe )
135  {
136  DisplayError( this,
137  wxString::Format( _( "File \"%s\" is not a valid symbol library table "
138  "file.\n\n%s" ), fn.GetFullPath(), ioe.What() ) );
139  return false;
140  }
141 
142  // Create the config path if it doesn't already exist.
143  wxFileName symTableFileName = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
144 
145  if( !symTableFileName.DirExists() && !symTableFileName.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
146  {
147  DisplayError( this,
148  wxString::Format( _( "Cannot create global library table path \"%s\"." ),
149  symTableFileName.GetPath() ) );
150  return false;
151  }
152 
153  // Copy the global symbol library table file to the user config.
154  if( !::wxCopyFile( fn.GetFullPath(), symTableFileName.GetFullPath() ) )
155  {
156  DisplayError( this,
157  wxString::Format( _( "Cannot copy global symbol library table "
158  "file:\n\n \"%s\"\n\n:to:\n\n\"%s\"." ),
159  fn.GetFullPath(), symTableFileName.GetFullPath() ) );
160  return false;
161  }
162 
163  // Load the successfully copied symbol library table file. This should not fail
164  // since the file was tested above. Check for failure anyway to keep the compiler
165  // from complaining.
166  try
167  {
169  return false;
170  }
171  catch( const IO_ERROR& ioe )
172  {
173  DisplayError( this,
174  wxString::Format( _( "Error occurred loading global symbol library table:"
175  "\n\n%s" ), ioe.What() ) );
176  return false;
177  }
178 
179  return true;
180 }
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.