diff --git a/common/gestfich.cpp b/common/gestfich.cpp index b616947678..35f1fee076 100644 --- a/common/gestfich.cpp +++ b/common/gestfich.cpp @@ -369,67 +369,3 @@ wxString QuoteFullPath( wxFileName& fn, wxPathFormat format ) { return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" ); } - - -bool DeleteDirectory( const wxString& aDirName, bool aRecurse, bool aIncludeHidden ) -{ - int hiddenFlag = ( aIncludeHidden ? wxDIR_HIDDEN : 0 ); - wxDir mainDir( aDirName ); - - if( !mainDir.IsOpened() ) - return false; - - if( mainDir.HasSubDirs() && !aRecurse ) - { - mainDir.Close(); - return false; - } - - // Get the name from wxWidgets so that we are sure all the separators are correct - wxString fullDirName = mainDir.GetNameWithSep(); - - wxString dir; - bool valid = mainDir.GetFirst( &dir, wxEmptyString, wxDIR_DIRS | hiddenFlag ); - - // Iterate over the subdirectories to recursively delete them and their contents - while( valid ) - { - dir.Prepend( fullDirName ); - - // This call will also delete the actual directory, so we don't have to - if( !DeleteDirectory( dir, true, aIncludeHidden ) ) - { - mainDir.Close(); - return false; - } - valid = mainDir.GetNext( &dir ); - } - - - wxString file; - valid = mainDir.GetFirst( &file, wxEmptyString, wxDIR_FILES | hiddenFlag ); - - // Iterate over the files to remove all of them from the directory - while( valid ) - { - file.Prepend( fullDirName ); - - if( !wxRemoveFile( file ) ) - { - mainDir.Close(); - return false; - } - valid = mainDir.GetNext( &file ); - } - - mainDir.Close(); - - // Now delete the actual directory - if( !wxRmdir( aDirName ) ) - { - mainDir.Close(); - return false; - } - - return true; -} diff --git a/include/gestfich.h b/include/gestfich.h index f5c8436442..900feaa7ce 100644 --- a/include/gestfich.h +++ b/include/gestfich.h @@ -152,21 +152,4 @@ wxString FindKicadFile( const wxString& shortname ); */ extern wxString QuoteFullPath( wxFileName& fn, wxPathFormat format = wxPATH_NATIVE ); -/** - * Delete a directory and all of its contents recursively. - * This function ensures that all contents of subdirectories are deleted before deleting - * the directory. If recursion is disabled, then the existence of subdirectories will - * cause the deletion to fail and the function to return false. - * - * Note that if hidden files/folders exist in the directory, and aIncludeHidden is false, - * then the directory may not be deleted. - * - * @param aRecurse specifies if subdirectories should also be deleted - * @param aIncludeHidden specifies if hidden files/directories should be deleted as well - * @return true if the directory could be deleted - * - */ -bool DeleteDirectory( const wxString& aDirName, bool aRecurse = true, bool aIncludeHidden = true ); - - #endif /* __INCLUDE__GESTFICH_H__ */ diff --git a/kicad/treeproject_item.cpp b/kicad/treeproject_item.cpp index a032a997d6..75c0e3c862 100644 --- a/kicad/treeproject_item.cpp +++ b/kicad/treeproject_item.cpp @@ -31,10 +31,13 @@ #include +#include #include +#include #include #include #include + #include "treeprojectfiles.h" #include "pgm_kicad.h" #include "tree_project_frame.h" @@ -126,16 +129,16 @@ bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check ) void TREEPROJECT_ITEM::Delete() { - bool isDirectory = wxDirExists( GetFileName() ); - bool success; + wxString errMsg; - if( !isDirectory ) - success = wxRemoveFile( GetFileName() ); - else - success = DeleteDirectory( GetFileName() ); + if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) ) + { + wxString dialogMsg = wxString::Format( _( "Failed to delete '%s'"), GetFileName() ); + DisplayErrorMessage( m_parent, dialogMsg, errMsg ); + return; + } - if( success ) - m_parent->Delete( GetId() ); + m_parent->Delete( GetId() ); } diff --git a/libs/kiplatform/CMakeLists.txt b/libs/kiplatform/CMakeLists.txt index aa0d3b2125..371357f0f7 100644 --- a/libs/kiplatform/CMakeLists.txt +++ b/libs/kiplatform/CMakeLists.txt @@ -1,8 +1,8 @@ - # Add the appropriate source files if( APPLE ) set( PLATFORM_SRCS osx/app.mm + osx/environment.mm osx/ui.mm ) @@ -15,13 +15,27 @@ if( APPLE ) elseif( WIN32 ) set( PLATFORM_SRCS msw/app.cpp + msw/environment.cpp msw/ui.cpp ) elseif( UNIX ) set( PLATFORM_SRCS gtk/app.cpp + gtk/environment.cpp gtk/ui.cpp ) + + find_package(PkgConfig REQUIRED) + pkg_check_modules(GTK3 REQUIRED gtk+-3.0) + + # Detect GTK3 and configure it + set( PLATFORM_LIBS + ${GTK3_LIBRARIES} + ) + + include_directories( SYSTEM ${GTK3_INCLUDE_DIRS} ) + link_directories( ${GTK3_LIBRARY_DIRS} ) + add_definitions( ${GTK3_CFLAGS_OTHER} ) endif() diff --git a/libs/kiplatform/gtk/environment.cpp b/libs/kiplatform/gtk/environment.cpp new file mode 100644 index 0000000000..e285706192 --- /dev/null +++ b/libs/kiplatform/gtk/environment.cpp @@ -0,0 +1,40 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 Ian McInerney + * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include + + +bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError ) +{ + GError* err = nullptr; + GFile* file = g_file_new_for_path( aPath.fn_str() ); + + bool retVal = g_file_trash( file, NULL, &err ); + + // Extract the error string if the operation failed + if( !retVal && err ) + aError = err->message; + + g_clear_error( &err ); + g_object_unref( file ); + + return retVal; +} diff --git a/libs/kiplatform/include/kiplatform/environment.h b/libs/kiplatform/include/kiplatform/environment.h new file mode 100644 index 0000000000..3887449d5a --- /dev/null +++ b/libs/kiplatform/include/kiplatform/environment.h @@ -0,0 +1,37 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 Ian McInerney + * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include + +namespace KIPLATFORM +{ + namespace ENV + { + /** + * Move the specified file/directory to the trash bin/recycle bin. + * + * @param aPath is the absolute path of the file/directory to move to the trash + * @param aError is the error message saying why the operation failed + * + * @return true if the operation succeeds, false if it fails (see the contents of aError) + */ + bool MoveToTrash( const wxString& aPath, wxString& aError ); + } +} diff --git a/libs/kiplatform/msw/environment.cpp b/libs/kiplatform/msw/environment.cpp new file mode 100644 index 0000000000..821ed67c64 --- /dev/null +++ b/libs/kiplatform/msw/environment.cpp @@ -0,0 +1,51 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 Ian McInerney + * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include +#include + +#include +#include + +bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError ) +{ + // The filename field must be a double-null terminated string + wxString temp = aPath + '\0'; + + SHFILEOPSTRUCT fileOp; + ::ZeroMemory( &fileOp, sizeof( fileOp ) ); + + fileOp.hwnd = NULL; // Set to null since there is no progress dialog + fileOp.wFunc = FO_DELETE; + fileOp.pFrom = temp.c_str(); + fileOp.pTo = NULL; // Set to to NULL since we aren't moving the file + fileOp.fFlags = FOF_ALLOWUNDO | FOF_NOERRORUI | FOF_NOCONFIRMATION | FOF_SILENT; + + int eVal = SHFileOperation( &fileOp ); + + if( eVal != 0 ) + { + aError = wxString::Format( _( "Error code: %d" ), eVal ); + return false; + } + + return true; +} diff --git a/libs/kiplatform/osx/environment.mm b/libs/kiplatform/osx/environment.mm new file mode 100644 index 0000000000..4e0913a53e --- /dev/null +++ b/libs/kiplatform/osx/environment.mm @@ -0,0 +1,46 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 Ian McInerney + * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include + +#import +#include + + +bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError ) +{ + wxString temp = "file:///" + aPath; + + NSURL* url = [NSURL URLWithString:wxCFStringRef( temp ).AsNSString()]; + NSError* err = NULL; + + BOOL result = [[NSFileManager defaultManager] trashItemAtURL:url resultingItemURL:nil error:&err]; + + // Extract the error string if the operation failed + if( result == NO ) + { + NSString* errmsg; + errmsg = [err.localizedFailureReason stringByAppendingFormat:@"\n\n%@", err.localizedRecoverySuggestion]; + aError = wxCFStringRef::AsString( (CFStringRef) errmsg ); + return false; + } + + return true; +}