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/wfstream.h>
33 #include <wx/zipstrm.h>
34 
35 #include <confirm.h>
36 #include <kiway.h>
37 
38 #include "pgm_kicad.h"
39 
40 #include "kicad.h"
41 
42 
43 #define ZipFileExtension wxT( "zip" )
44 #define ZipFileWildcard _( "Zip file (*.zip)|*.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  zipfilesys.ChangePathTo( zipfiledlg.GetPath() + wxT( "#zip:" ), true );
88 
89  wxFSFile* zipfile = NULL;
90  wxString localfilename = zipfilesys.FindFirst( wxT( "*.*" ) );
91 
92  while( !localfilename.IsEmpty() )
93  {
94  zipfile = zipfilesys.OpenFile( localfilename );
95  if( !zipfile )
96  {
97  DisplayError( this, wxT( "Zip file read error" ) );
98  break;
99  }
100 
101  wxFileName uzfn = localfilename.AfterLast( ':' );
102  uzfn.MakeAbsolute( unzipDir );
103  wxString unzipfilename = uzfn.GetFullPath();
104 
105  msg.Printf( _( "Extract file '%s'" ), GetChars( unzipfilename ) );
106  PrintMsg( msg );
107 
108  wxInputStream* stream = zipfile->GetStream();
109  wxFFileOutputStream* ofile = new wxFFileOutputStream( unzipfilename );
110 
111  if( ofile->Ok() )
112  {
113  ofile->Write( *stream );
114  PrintMsg( _( " OK\n" ) );
115  }
116  else
117  PrintMsg( _( " *ERROR*\n" ) );
118 
119  delete ofile;
120  delete zipfile;
121 
122  localfilename = zipfilesys.FindNext();
123  }
124 
125  PrintMsg( wxT( "** end **\n" ) );
126 }
127 
128 
129 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
130 {
131  // List of file extensions to save.
132  static const wxChar* extentionList[] = {
133  wxT( "*.sch" ), wxT( "*.lib" ), wxT( "*.mod" ), wxT( "*.cmp" ),
134  wxT( "*.brd" ), wxT( "*.kicad_pcb" ), wxT( "*.gbr" ), wxT( "*.pos" ),
135  wxT( "*.net" ), wxT( "*.pro" ), wxT( "*.drl" ), wxT( "*.py" ),
136  wxT( "*.pdf" ), wxT( "*.txt" ), wxT( "*.dcm" ), wxT( "*.kicad_wks" ),
137  };
138 
139  wxString msg;
140  wxFileName fileName = GetProjectFileName();
141  wxString oldCwd = wxGetCwd();
142 
143  fileName.SetExt( wxT( "zip" ) );
144 
145  wxFileDialog dlg( this, _( "Archive Project Files" ),
146  fileName.GetPath(), fileName.GetFullName(),
147  ZipFileWildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
148 
149  if( dlg.ShowModal() == wxID_CANCEL )
150  return;
151 
152  wxFileName zip = dlg.GetPath();
153 
154  wxString currdirname = fileName.GetPathWithSep();
155  wxDir dir( currdirname );
156 
157  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
158  return;
159 
160  wxSetWorkingDirectory( currdirname );
161 
162  // Prepare the zip file
163  wxString zipfilename = zip.GetFullPath();
164 
165  wxFFileOutputStream ostream( zipfilename );
166 
167  if( !ostream.IsOk() ) // issue to create the file. Perhaps not writable dir
168  {
169  wxMessageBox( wxString::Format( _( "Unable to create zip archive file '%s'" ),
170  zipfilename ) );
171  return;
172  }
173 
174  wxZipOutputStream zipstream( ostream );
175 
176  // Build list of filenames to put in zip archive
177  wxString currFilename;
178 
179  wxArrayString files;
180 
181  for( unsigned ii = 0; ii < DIM( extentionList ); ii++ )
182  wxDir::GetAllFiles( currdirname, &files, extentionList[ii] );
183 
184  files.Sort();
185 
186  int zipBytesCnt = 0;
187 
188  for( unsigned ii = 0; ii < files.GetCount(); ii++ )
189  {
190  wxFileSystem fsfile;
191 
192  wxFileName curr_fn( files[ii] );
193  curr_fn.MakeRelativeTo( currdirname );
194  currFilename = curr_fn.GetFullPath();
195 
196  msg.Printf( _( "Archive file <%s>" ), GetChars( currFilename ) );
197  PrintMsg( msg );
198 
199  // Read input file and add it to the zip file:
200  wxFSFile* infile = fsfile.OpenFile( currFilename );
201 
202  if( infile )
203  {
204  zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
205  infile->GetStream()->Read( zipstream );
206  zipstream.CloseEntry();
207  int zippedsize = zipstream.GetSize() - zipBytesCnt;
208  zipBytesCnt = zipstream.GetSize();
209  PrintMsg( wxT(" ") );
210  msg.Printf( _( "(%lu bytes, compressed %d bytes)\n" ),
211  (unsigned long)infile->GetStream()->GetSize(), zippedsize );
212  PrintMsg( msg );
213  delete infile;
214  }
215  else
216  PrintMsg( _( " >>Error\n" ) );
217  }
218 
219  zipBytesCnt = ostream.GetSize();
220 
221  if( zipstream.Close() )
222  {
223  msg.Printf( _( "\nZip archive <%s> created (%d bytes)" ),
224  GetChars( zipfilename ), zipBytesCnt );
225  PrintMsg( msg );
226  PrintMsg( wxT( "\n** end **\n" ) );
227  }
228  else
229  {
230  msg.Printf( wxT( "Unable to create archive <%s>, abort\n" ),
231  GetChars( zipfilename ) );
232  PrintMsg( msg );
233  }
234 
235  wxSetWorkingDirectory( oldCwd );
236 }
#define DIM(x)
of elements in an array
Definition: macros.h:98
This file is part of the common library.
void OnFileHistory(wxCommandEvent &event)
void PrintMsg(const wxString &aText)
Displays aText in the text panel.
Definition: mainframe.cpp:201
KICAD_MANAGER_FRAME is the KiCad main frame.
void OnArchiveFiles(wxCommandEvent &event)
PGM_KICAD & PgmTop()
Definition: kicad.cpp:71
#define ZipFileExtension
const wxString GetProjectFileName()
Definition: mainframe.cpp:147
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
wxString GetFileFromHistory(int cmdId, const wxString &type, wxFileHistory *aFileHistory=NULL)
Function GetFileFromHistory fetches the file name from the file history list.
Definition: basicframe.cpp:419
void LoadProject(const wxFileName &aProjectFileName)
Definition: prjconfig.cpp:61
void OnUnarchiveFiles(wxCommandEvent &event)
wxFileHistory & GetFileHistory()
Definition: pgm_kicad.h:57
#define ZipFileWildcard
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71