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