KiCad PCB EDA Suite
kicad/files-io.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) 2004-2015 Jean-Pierre Charras
5  * Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <wx/dir.h>
31 #include <wx/fs_zip.h>
32 #include <wx/uri.h>
33 #include <wx/wfstream.h>
34 #include <wx/zipstrm.h>
35 
36 #include <confirm.h>
37 #include <kiway.h>
38 #include "pgm_kicad.h"
40 
41 #include "kicad.h"
42 
43 
44 #define ZipFileExtension wxT( "zip" )
45 
46 
47 void KICAD_MANAGER_FRAME::OnFileHistory( wxCommandEvent& event )
48 {
49  wxFileName projFileName = GetFileFromHistory( event.GetId(), _( "KiCad project file" ),
50  &PgmTop().GetFileHistory() );
51  if( !projFileName.FileExists() )
52  return;
53 
54  LoadProject( projFileName );
55 }
56 
57 
58 void KICAD_MANAGER_FRAME::OnUnarchiveFiles( wxCommandEvent& event )
59 {
60  wxFileName fn = GetProjectFileName();
61 
62  fn.SetExt( ZipFileExtension );
63 
64  wxFileDialog zipfiledlg( this, _( "Unzip Project" ), fn.GetPath(),
65  fn.GetFullName(), ZipFileWildcard(),
66  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
67 
68  if( zipfiledlg.ShowModal() == wxID_CANCEL )
69  return;
70 
71  wxString msg = wxString::Format( _( "\nOpen \"%s\"\n" ), GetChars( zipfiledlg.GetPath() ) );
72  PrintMsg( msg );
73 
74  wxDirDialog dirDlg( this, _( "Target Directory" ), fn.GetPath(),
75  wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
76 
77  if( dirDlg.ShowModal() == wxID_CANCEL )
78  return;
79 
80  wxString unzipDir = dirDlg.GetPath() + wxT( "/" );
81  msg.Printf( _( "Unzipping project in \"%s\"\n" ), GetChars( unzipDir ) );
82  PrintMsg( msg );
83 
84  wxFileSystem zipfilesys;
85 
86  zipfilesys.AddHandler( new wxZipFSHandler );
87  auto path = wxURI( zipfiledlg.GetPath() + wxT( "#zip:" ) ).BuildURI();
88  zipfilesys.ChangePathTo( path, true );
89 
90  wxFSFile* zipfile = NULL;
91  wxString localfilename = zipfilesys.FindFirst( wxFileSelectorDefaultWildcardStr, wxFILE );
92 
93  while( !localfilename.IsEmpty() )
94  {
95  zipfile = zipfilesys.OpenFile( localfilename );
96  if( !zipfile )
97  {
98  DisplayError( this, wxT( "Zip file read error" ) );
99  break;
100  }
101 
102  wxFileName uzfn = localfilename.AfterLast( ':' );
103  uzfn.MakeAbsolute( unzipDir );
104  wxString unzipfilename = uzfn.GetFullPath();
105 
106  msg.Printf( _( "Extract file \"%s\"" ), GetChars( unzipfilename ) );
107  PrintMsg( msg );
108 
109  wxInputStream* stream = zipfile->GetStream();
110  wxFFileOutputStream* ofile = new wxFFileOutputStream( unzipfilename );
111 
112  if( ofile->Ok() )
113  {
114  ofile->Write( *stream );
115  PrintMsg( _( " OK\n" ) );
116  }
117  else
118  PrintMsg( _( " *ERROR*\n" ) );
119 
120  delete ofile;
121  delete zipfile;
122 
123  localfilename = zipfilesys.FindNext();
124  }
125 
126  PrintMsg( wxT( "** end **\n" ) );
127 
128  if( unzipDir == Prj().GetProjectPath() )
129  {
130  wxCommandEvent dummy;
131  OnRefresh( dummy );
132  }
133 }
134 
135 
136 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
137 {
138  // List of file extensions to save.
139  static const wxChar* extentionList[] = {
140  wxT( "*.pro" ),
141  wxT( "*.sch" ), wxT( "*.lib" ), wxT( "*.dcm" ), // Schematic related files
142  wxT( "*.cmp" ),
143  wxT( "*.brd" ), wxT( "*.kicad_pcb" ), // Brd files
144  wxT( "*.mod" ), wxT( "*.kicad_mod" ), // fp files
145  wxT( "*.gb?" ), wxT( "*.gbrjob" ), // Gerber files
146  wxT( "*.gko" ), wxT( "*.gm1" ),
147  wxT( "*.gm2" ), wxT( "*.g?" ),
148  wxT( "*.gp1" ), wxT( "*.gp2" ),
149  wxT( "*.gpb" ), wxT( "*.gpt" ),
150  wxT( "*.gt?" ),
151  wxT( "*.pos" ), wxT( "*.drl" ), // Fab files
152  wxT( "*.d356" ), wxT( "*.rpt" ),
153  wxT( "*.stp" ), wxT( "*.step" ), // 3d files
154  wxT( "*.wrl" ),
155  wxT( "*.net" ), wxT( "*.py" ),
156  wxT( "*.pdf" ), wxT( "*.txt" ), wxT( "*.kicad_wks" ),
157  wxT( "fp-lib-table" ), wxT( "sym-lib-table" )
158  };
159 
160  wxString msg;
161  wxFileName fileName = GetProjectFileName();
162  wxString oldCwd = wxGetCwd();
163 
164  fileName.SetExt( wxT( "zip" ) );
165 
166  wxFileDialog dlg( this, _( "Archive Project Files" ),
167  fileName.GetPath(), fileName.GetFullName(),
168  ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
169 
170  if( dlg.ShowModal() == wxID_CANCEL )
171  return;
172 
173  wxFileName zip = dlg.GetPath();
174 
175  wxString currdirname = fileName.GetPathWithSep();
176  wxDir dir( currdirname );
177 
178  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
179  return;
180 
181  wxSetWorkingDirectory( currdirname );
182 
183  // Prepare the zip file
184  wxString zipfilename = zip.GetFullPath();
185 
186  wxFFileOutputStream ostream( zipfilename );
187 
188  if( !ostream.IsOk() ) // issue to create the file. Perhaps not writable dir
189  {
190  wxMessageBox( wxString::Format( _( "Unable to create zip archive file \"%s\"" ),
191  zipfilename ) );
192  return;
193  }
194 
195  wxZipOutputStream zipstream( ostream );
196 
197  // Build list of filenames to put in zip archive
198  wxString currFilename;
199 
200  wxArrayString files;
201 
202  for( unsigned ii = 0; ii < arrayDim( extentionList ); ii++ )
203  wxDir::GetAllFiles( currdirname, &files, extentionList[ii] );
204 
205  files.Sort();
206 
207  int zipBytesCnt = 0;
208 
209  for( unsigned ii = 0; ii < files.GetCount(); ii++ )
210  {
211  wxFileSystem fsfile;
212 
213  wxFileName curr_fn( files[ii] );
214  curr_fn.MakeRelativeTo( currdirname );
215  currFilename = curr_fn.GetFullPath();
216 
217  msg.Printf( _( "Archive file \"%s\"" ), GetChars( currFilename ) );
218  PrintMsg( msg );
219 
220  // Read input file and add it to the zip file:
221  wxFSFile* infile = fsfile.OpenFile( currFilename );
222 
223  if( infile )
224  {
225  zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
226  infile->GetStream()->Read( zipstream );
227  zipstream.CloseEntry();
228  int zippedsize = zipstream.GetSize() - zipBytesCnt;
229  zipBytesCnt = zipstream.GetSize();
230  PrintMsg( wxT(" ") );
231  msg.Printf( _( "(%lu bytes, compressed %d bytes)\n" ),
232  (unsigned long)infile->GetStream()->GetSize(), zippedsize );
233  PrintMsg( msg );
234  delete infile;
235  }
236  else
237  PrintMsg( _( " >>Error\n" ) );
238  }
239 
240  zipBytesCnt = ostream.GetSize();
241 
242  if( zipstream.Close() )
243  {
244  msg.Printf( _( "\nZip archive \"%s\" created (%d bytes)" ),
245  GetChars( zipfilename ), zipBytesCnt );
246  PrintMsg( msg );
247  PrintMsg( wxT( "\n** end **\n" ) );
248  }
249  else
250  {
251  msg.Printf( wxT( "Unable to create archive \"%s\", abort\n" ),
252  GetChars( zipfilename ) );
253  PrintMsg( msg );
254  }
255 
256  wxSetWorkingDirectory( oldCwd );
257 }
FILE_HISTORY & GetFileHistory()
Definition: pgm_kicad.h:57
This file is part of the common library.
void OnRefresh(wxCommandEvent &event)
Definition: mainframe.cpp:512
wxString ZipFileWildcard()
void OnFileHistory(wxCommandEvent &event)
void PrintMsg(const wxString &aText)
Displays aText in the text panel.
Definition: mainframe.cpp:194
KICAD_MANAGER_FRAME is the KiCad main frame.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Definition of file extensions used in Kicad.
void OnArchiveFiles(wxCommandEvent &event)
PGM_KICAD & PgmTop()
Definition: kicad.cpp:72
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:99
#define ZipFileExtension
const wxString GetProjectFileName()
Definition: mainframe.cpp:140
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
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
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
wxString GetFileFromHistory(int cmdId, const wxString &type, wxFileHistory *aFileHistory=NULL)
Function GetFileFromHistory fetches the file name from the file history list.
void LoadProject(const wxFileName &aProjectFileName)
Definition: prjconfig.cpp:61
void OnUnarchiveFiles(wxCommandEvent &event)
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:243