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 
127 
128 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
129 {
130  // List of file extensions to save.
131  static const wxChar* extentionList[] = {
132  wxT( "*.sch" ), wxT( "*.lib" ), wxT( "*.mod" ), wxT( "*.cmp" ),
133  wxT( "*.brd" ), wxT( "*.kicad_pcb" ), wxT( "*.gbr" ), wxT( "*.pos" ),
134  wxT( "*.net" ), wxT( "*.pro" ), wxT( "*.drl" ), wxT( "*.py" ),
135  wxT( "*.pdf" ), wxT( "*.txt" ), wxT( "*.dcm" ), wxT( "*.kicad_wks" ),
136  };
137 
138  wxString msg;
139  wxFileName fileName = GetProjectFileName();
140  wxString oldCwd = wxGetCwd();
141 
142  fileName.SetExt( wxT( "zip" ) );
143 
144  wxFileDialog dlg( this, _( "Archive Project Files" ),
145  fileName.GetPath(), fileName.GetFullName(),
146  ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
147 
148  if( dlg.ShowModal() == wxID_CANCEL )
149  return;
150 
151  wxFileName zip = dlg.GetPath();
152 
153  wxString currdirname = fileName.GetPathWithSep();
154  wxDir dir( currdirname );
155 
156  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
157  return;
158 
159  wxSetWorkingDirectory( currdirname );
160 
161  // Prepare the zip file
162  wxString zipfilename = zip.GetFullPath();
163 
164  wxFFileOutputStream ostream( zipfilename );
165 
166  if( !ostream.IsOk() ) // issue to create the file. Perhaps not writable dir
167  {
168  wxMessageBox( wxString::Format( _( "Unable to create zip archive file '%s'" ),
169  zipfilename ) );
170  return;
171  }
172 
173  wxZipOutputStream zipstream( ostream );
174 
175  // Build list of filenames to put in zip archive
176  wxString currFilename;
177 
178  wxArrayString files;
179 
180  for( unsigned ii = 0; ii < DIM( extentionList ); ii++ )
181  wxDir::GetAllFiles( currdirname, &files, extentionList[ii] );
182 
183  files.Sort();
184 
185  int zipBytesCnt = 0;
186 
187  for( unsigned ii = 0; ii < files.GetCount(); ii++ )
188  {
189  wxFileSystem fsfile;
190 
191  wxFileName curr_fn( files[ii] );
192  curr_fn.MakeRelativeTo( currdirname );
193  currFilename = curr_fn.GetFullPath();
194 
195  msg.Printf( _( "Archive file <%s>" ), GetChars( currFilename ) );
196  PrintMsg( msg );
197 
198  // Read input file and add it to the zip file:
199  wxFSFile* infile = fsfile.OpenFile( currFilename );
200 
201  if( infile )
202  {
203  zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
204  infile->GetStream()->Read( zipstream );
205  zipstream.CloseEntry();
206  int zippedsize = zipstream.GetSize() - zipBytesCnt;
207  zipBytesCnt = zipstream.GetSize();
208  PrintMsg( wxT(" ") );
209  msg.Printf( _( "(%lu bytes, compressed %d bytes)\n" ),
210  (unsigned long)infile->GetStream()->GetSize(), zippedsize );
211  PrintMsg( msg );
212  delete infile;
213  }
214  else
215  PrintMsg( _( " >>Error\n" ) );
216  }
217 
218  zipBytesCnt = ostream.GetSize();
219 
220  if( zipstream.Close() )
221  {
222  msg.Printf( _( "\nZip archive <%s> created (%d bytes)" ),
223  GetChars( zipfilename ), zipBytesCnt );
224  PrintMsg( msg );
225  PrintMsg( wxT( "\n** end **\n" ) );
226  }
227  else
228  {
229  msg.Printf( wxT( "Unable to create archive <%s>, abort\n" ),
230  GetChars( zipfilename ) );
231  PrintMsg( msg );
232  }
233 
234  wxSetWorkingDirectory( oldCwd );
235 }
#define DIM(x)
of elements in an array
Definition: macros.h:98
This file is part of the common library.
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
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
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73