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_manager_frame.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() )
130 }
131 
132 
133 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
134 {
135  // List of file extensions to save.
136  static const wxChar* extensionList[] = {
137  wxT( "*.pro" ),
138  wxT( "*.sch" ), wxT( "*.lib" ), wxT( "*.dcm" ), // Schematic related files
139  wxT( "*.cmp" ),
140  wxT( "*.brd" ), wxT( "*.kicad_pcb" ), // Brd files
141  wxT( "*.mod" ), wxT( "*.kicad_mod" ), // fp files
142  wxT( "*.gb?" ), wxT( "*.gbrjob" ), // Gerber files
143  wxT( "*.gko" ), wxT( "*.gm1" ),
144  wxT( "*.gm2" ), wxT( "*.g?" ),
145  wxT( "*.gp1" ), wxT( "*.gp2" ),
146  wxT( "*.gpb" ), wxT( "*.gpt" ),
147  wxT( "*.gt?" ),
148  wxT( "*.pos" ), wxT( "*.drl" ), wxT( "*.nc" ), wxT( "*.xnc" ), // Fab files
149  wxT( "*.d356" ), wxT( "*.rpt" ),
150  wxT( "*.stp" ), wxT( "*.step" ), // 3d files
151  wxT( "*.wrl" ),
152  wxT( "*.net" ), wxT( "*.py" ),
153  wxT( "*.pdf" ), wxT( "*.txt" ), wxT( "*.kicad_wks" ),
154  wxT( "fp-lib-table" ), wxT( "sym-lib-table" )
155  };
156 
157  wxString msg;
158  wxFileName fileName = GetProjectFileName();
159  wxString oldCwd = wxGetCwd();
160 
161  fileName.SetExt( wxT( "zip" ) );
162 
163  wxFileDialog dlg( this, _( "Archive Project Files" ),
164  fileName.GetPath(), fileName.GetFullName(),
165  ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
166 
167  if( dlg.ShowModal() == wxID_CANCEL )
168  return;
169 
170  wxFileName zip = dlg.GetPath();
171 
172  wxString currdirname = fileName.GetPathWithSep();
173  wxDir dir( currdirname );
174 
175  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
176  return;
177 
178  wxSetWorkingDirectory( currdirname );
179 
180  // Prepare the zip file
181  wxString zipfilename = zip.GetFullPath();
182 
183  wxFFileOutputStream ostream( zipfilename );
184 
185  if( !ostream.IsOk() ) // issue to create the file. Perhaps not writable dir
186  {
187  wxMessageBox( wxString::Format( _( "Unable to create zip archive file \"%s\"" ),
188  zipfilename ) );
189  return;
190  }
191 
192  wxZipOutputStream zipstream( ostream );
193 
194  // Build list of filenames to put in zip archive
195  wxString currFilename;
196 
197  wxArrayString files;
198 
199  for( unsigned ii = 0; ii < arrayDim( extensionList ); ii++ )
200  wxDir::GetAllFiles( currdirname, &files, extensionList[ii] );
201 
202  files.Sort();
203 
204  int zipBytesCnt = 0;
205 
206  for( unsigned ii = 0; ii < files.GetCount(); ii++ )
207  {
208  wxFileSystem fsfile;
209 
210  wxFileName curr_fn( files[ii] );
211  curr_fn.MakeRelativeTo( currdirname );
212  currFilename = curr_fn.GetFullPath();
213 
214  msg.Printf( _( "Archive file \"%s\"" ), GetChars( currFilename ) );
215  PrintMsg( msg );
216 
217  // Read input file and add it to the zip file:
218  wxFSFile* infile = fsfile.OpenFile( currFilename );
219 
220  if( infile )
221  {
222  zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
223  infile->GetStream()->Read( zipstream );
224  zipstream.CloseEntry();
225  int zippedsize = zipstream.GetSize() - zipBytesCnt;
226  zipBytesCnt = zipstream.GetSize();
227  PrintMsg( wxT(" ") );
228  msg.Printf( _( "(%lu bytes, compressed %d bytes)\n" ),
229  (unsigned long)infile->GetStream()->GetSize(), zippedsize );
230  PrintMsg( msg );
231  delete infile;
232  }
233  else
234  PrintMsg( _( " >>Error\n" ) );
235  }
236 
237  zipBytesCnt = ostream.GetSize();
238 
239  if( zipstream.Close() )
240  {
241  msg.Printf( _( "\nZip archive \"%s\" created (%d bytes)" ),
242  GetChars( zipfilename ), zipBytesCnt );
243  PrintMsg( msg );
244  PrintMsg( wxT( "\n** end **\n" ) );
245  }
246  else
247  {
248  msg.Printf( wxT( "Unable to create archive \"%s\", abort\n" ),
249  GetChars( zipfilename ) );
250  PrintMsg( msg );
251  }
252 
253  wxSetWorkingDirectory( oldCwd );
254 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
static const std::vector< std::string > extensionList
FILE_HISTORY & GetFileHistory()
Definition: pgm_kicad.h:57
This file is part of the common library.
wxString ZipFileWildcard()
void OnFileHistory(wxCommandEvent &event)
#define NULL
void PrintMsg(const wxString &aText)
Displays aText in the text panel.
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:108
#define ZipFileExtension
const wxString GetProjectFileName()
wxString GetFileFromHistory(int cmdId, const wxString &type, FILE_HISTORY *aFileHistory=NULL)
Fetches the file name from the file history list.
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:101
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
#define _(s)
Definition: 3d_actions.cpp:31
void LoadProject(const wxFileName &aProjectFileName)
void OnUnarchiveFiles(wxCommandEvent &event)