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