1 /**
2  * File utilities to uninstall a D-Mod
3 
4  * Copyright (C) 2005, 2006  Dan Walma
5  * Copyright (C) 2008  Sylvain Beucler
6 
7  * This file is part of GNU FreeDink
8 
9  * GNU FreeDink is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13 
14  * GNU FreeDink is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18 
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see
21  * <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "RecursiveDelete.hpp"
25 #include <wx/dir.h>
26 #include <wx/wx.h>
27 
RecursiveDelete(bool aRemoveSaveGames)28 RecursiveDelete::RecursiveDelete(bool aRemoveSaveGames) :
29   mRemoveSaveGames(aRemoveSaveGames),
30   mError(false)
31 {
32 }
33 
34 
OnFile(const wxString & aFilename)35 wxDirTraverseResult RecursiveDelete::OnFile(const wxString& aFilename)
36 {
37     bool lRemoveFile( true );
38 
39     // See if it is a save game file
40     if (mRemoveSaveGames == false)
41     {
42       wxString lFilename = aFilename.Mid( aFilename.Find( '/', true ) + 1 );
43       if (lFilename.Mid(0, 4).MakeUpper() == _T("SAVE")
44 	  && lFilename.Mid(lFilename.Len() - 3).MakeUpper() == _T("DAT"))
45         {
46 	  lRemoveFile = false;
47         }
48     }
49 
50     // Remove the file
51     if (lRemoveFile == true)
52       {
53         if (::wxRemoveFile(aFilename) == false)
54         {
55 	  wxLogError(_("Could not remove %s"), aFilename.c_str());
56 	  mError = true;
57         }
58     }
59     return wxDIR_CONTINUE;
60 }
61 
OnDir(const wxString & aDirname)62 wxDirTraverseResult RecursiveDelete::OnDir( const wxString& aDirname )
63 {
64   RecursiveDelete lRecursiveDelete( false );
65   wxDir* lDir = new wxDir( aDirname );
66   lDir->Traverse( lRecursiveDelete );
67   delete lDir;
68 
69   if ( lRecursiveDelete.getError() == false )
70     {
71       if ( ::wxRmdir( aDirname ) == false )
72 	{
73 	  wxLogError(_("Could not remove %s"), aDirname.c_str());
74 	  mError = true;
75 	}
76     }
77   else if ( mError == false )
78     {
79       mError = true;
80     }
81 
82   return wxDIR_IGNORE;
83 }
84 
getError(void)85 bool RecursiveDelete::getError( void )
86 {
87   return mError;
88 }
89