KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_global_design_block_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#include <macros.h>
27
29
30#include <kiplatform/io.h>
31
32
38
39
43
44
49
50
52{
53 // Create an empty table if requested by the user.
54 if( m_emptyRb->GetValue() )
55 {
56 DESIGN_BLOCK_LIB_TABLE emptyTable;
57
58 try
59 {
61 }
62 catch( const IO_ERROR& ioe )
63 {
64 DisplayError( this,
65 wxString::Format( _( "Error creating design block library table '%s'.\n"
66 "%s" ),
68 ioe.What() ) );
69 return false;
70 }
71 }
72 else
73 {
74 wxString fileName = m_filePicker1->GetPath();
75
76 if( fileName.IsEmpty() )
77 {
78 DisplayError( this, _( "Please select a design block library table file." ) );
79 return false;
80 }
81
82 wxFileName fn = fileName;
83
84 // Make sure the design block library table to copy actually exists.
85 if( !fn.FileExists() )
86 {
87 DisplayError( this, wxString::Format( _( "File '%s' not found." ), fn.GetFullPath() ) );
88 return false;
89 }
90
91 // Make sure the design block library table to copy is a valid design block library
92 // table file.
94
95 try
96 {
97 tmpTable.Load( fn.GetFullPath() );
98 }
99 catch( const IO_ERROR& ioe )
100 {
101 DisplayError( this,
102 wxString::Format( _( "Error reading design block library table '%s'.\n"
103 "%s" ),
104 fn.GetFullPath(), ioe.What() ) );
105 return false;
106 }
107
108 // Create the config path if it doesn't already exist.
109 wxFileName designBlockTableFileName = DESIGN_BLOCK_LIB_TABLE::GetGlobalTableFileName();
110
111 if( !wxFileName::DirExists( designBlockTableFileName.GetPath() )
112 && !wxFileName::Mkdir( designBlockTableFileName.GetPath(), 0x777, wxPATH_MKDIR_FULL ) )
113 {
114 DisplayError( this, wxString::Format( _( "Cannot create global library table '%s'." ),
115 designBlockTableFileName.GetPath() ) );
116 return false;
117 }
118
119 // Copy the global design block library table file to the user config.
120 if( !::wxCopyFile( fn.GetFullPath(), designBlockTableFileName.GetFullPath() ) )
121 {
123 this,
124 wxString::Format( _( "Error copying global design block library table '%s' "
125 "to '%s'." ),
126 fn.GetFullPath(), designBlockTableFileName.GetFullPath() ) );
127 return false;
128 }
129
130 // Ensure the copied file is writable
131 KIPLATFORM::IO::MakeWriteable( designBlockTableFileName.GetFullPath() );
132 }
133
134 // Load the successfully copied design block library table file. This should not fail since the
135 // file was tested above. Check for failure anyway to keep the compiler from complaining.
136 try
137 {
140 return false;
141 }
142 catch( const IO_ERROR& )
143 {
144 return false;
145 }
146
147 return true;
148}
static wxString GetGlobalTableFileName()
static bool LoadGlobalTable(DESIGN_BLOCK_LIB_TABLE &aTable)
Load the global design block library table into aTable.
static DESIGN_BLOCK_LIB_TABLE & GetGlobalLibTable()
DIALOG_GLOBAL_LIB_TABLE_CONFIG(wxWindow *aParent, const wxString &aTableName, const KIWAY::FACE_T aFaceType)
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.
#define _(s)
This file contains miscellaneous commonly used macros and functions.
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:70