From 5d0dff54982b174d382aca8da00393273bc7b3e5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 8 Sep 2020 13:21:50 +0300 Subject: DeleteDirectoryTreeW - new function that deletes a folder with all subfolders (recursively) --- src/mir_app/src/db_ini.cpp | 15 ++------------- src/mir_app/src/profilemanager.cpp | 24 +++++++----------------- src/mir_core/src/mir_core.def | 1 + src/mir_core/src/mir_core64.def | 1 + src/mir_core/src/path.cpp | 24 ++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/mir_app/src/db_ini.cpp b/src/mir_app/src/db_ini.cpp index 2580d8eb3d..f7122100e0 100644 --- a/src/mir_app/src/db_ini.cpp +++ b/src/mir_app/src/db_ini.cpp @@ -216,13 +216,7 @@ protected: void Recycle_OnClick(CCtrlBase*) { - ptrW szIniPath(m_iniPath.GetText()); - SHFILEOPSTRUCT shfo = {}; - shfo.wFunc = FO_DELETE; - shfo.pFrom = szIniPath; - szIniPath[mir_wstrlen(szIniPath) + 1] = '\0'; - shfo.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; - SHFileOperation(&shfo); + DeleteDirectoryTreeW(ptrW(m_iniPath.GetText()), true); Close(); } @@ -553,12 +547,7 @@ static void DoAutoExec(void) if (!mir_wstrcmpi(szOnCompletion, L"delete")) DeleteFile(szIniPath); else if (!mir_wstrcmpi(szOnCompletion, L"recycle")) { - SHFILEOPSTRUCT shfo = {}; - shfo.wFunc = FO_DELETE; - shfo.pFrom = szIniPath; - szIniPath[mir_wstrlen(szIniPath) + 1] = 0; - shfo.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; - SHFileOperation(&shfo); + DeleteDirectoryTreeW(szIniPath, true); } else if (!mir_wstrcmpi(szOnCompletion, L"rename")) { wchar_t szRenamePrefix[MAX_PATH], szNewPath[MAX_PATH]; diff --git a/src/mir_app/src/profilemanager.cpp b/src/mir_app/src/profilemanager.cpp index fc4912cf7b..96e802d6cf 100644 --- a/src/mir_app/src/profilemanager.cpp +++ b/src/mir_app/src/profilemanager.cpp @@ -84,14 +84,14 @@ class CCreateProfileDlg : public CDlgBase int CreateProfile(const wchar_t *profile, DATABASELINK *link) { - wchar_t buf[256]; - int err = 0; - // check if the file already exists const wchar_t *file = wcsrchr(profile, '\\'); if (file) file++; - + + int err = 0; + wchar_t buf[256]; + if (_waccess(profile, 0) == 0) { // file already exists! mir_snwprintf(buf, @@ -101,12 +101,7 @@ class CCreateProfileDlg : public CDlgBase return 0; // move the file - SHFILEOPSTRUCT sf = {}; - sf.wFunc = FO_DELETE; - sf.pFrom = buf; - sf.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; - mir_snwprintf(buf, L"%s\0", profile); - if (SHFileOperation(&sf) != 0) { + if (DeleteDirectoryTreeW(profile, true) != 0) { mir_snwprintf(buf, TranslateT("Couldn't move '%s' to the Recycle Bin. Please select another profile name."), file); MessageBox(m_hwnd, buf, TranslateT("Problem moving profile"), MB_ICONINFORMATION | MB_OK); return 0; @@ -329,13 +324,8 @@ class CChooseProfileDlg : public CDlgBase if (IDYES != MessageBoxW(nullptr, wszMessage, L"Miranda NG", MB_YESNO | MB_TASKMODAL | MB_ICONWARNING)) return; - wszMessage.Format(L"%s\\%s%c", m_pd->ptszProfileDir, item.pszText, 0); - - SHFILEOPSTRUCT sf = {}; - sf.wFunc = FO_DELETE; - sf.pFrom = wszMessage; - sf.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_ALLOWUNDO; - SHFileOperation(&sf); + wszMessage.Format(L"%s\\%s", m_pd->ptszProfileDir, item.pszText); + DeleteDirectoryTreeW(wszMessage, true); m_profileList.DeleteItem(item.iItem); } diff --git a/src/mir_core/src/mir_core.def b/src/mir_core/src/mir_core.def index 1636bc0239..d0e17162d9 100644 --- a/src/mir_core/src/mir_core.def +++ b/src/mir_core/src/mir_core.def @@ -2,6 +2,7 @@ LIBRARY mir_core.mir EXPORTS ?g_pCurrDb@@3PAVMDatabaseCommon@@A @1 NONAME +DeleteDirectoryTreeW @2 CallFunctionAsync @5 CallPluginEventHook @7 CallService @8 diff --git a/src/mir_core/src/mir_core64.def b/src/mir_core/src/mir_core64.def index bb77e99aac..797fb38dcd 100644 --- a/src/mir_core/src/mir_core64.def +++ b/src/mir_core/src/mir_core64.def @@ -2,6 +2,7 @@ LIBRARY mir_core.mir EXPORTS ?g_pCurrDb@@3PEAVMDatabaseCommon@@EA @1 NONAME +DeleteDirectoryTreeW @2 CallFunctionAsync @5 CallPluginEventHook @7 CallService @8 diff --git a/src/mir_core/src/path.cpp b/src/mir_core/src/path.cpp index 99d95d9757..596eea39c4 100644 --- a/src/mir_core/src/path.cpp +++ b/src/mir_core/src/path.cpp @@ -207,6 +207,30 @@ MIR_CORE_DLL(int) CreateDirectoryTreeW(const wchar_t *szDir) return (CreateDirectoryW(szTestDir, nullptr) == 0) ? GetLastError() : 0; } +MIR_CORE_DLL(int) DeleteDirectoryTreeW(const wchar_t *pwszDir, bool bAllowUndo) +{ + if (pwszDir == nullptr) + return ERROR_BAD_ARGUMENTS; + + CMStringW wszPath(pwszDir); + wszPath.AppendChar(0); + + SHFILEOPSTRUCTW file_op = { + nullptr, + FO_DELETE, + wszPath, + L"", + FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION, + false, + nullptr, + L"" }; + + if (bAllowUndo) + file_op.fFlags |= FOF_ALLOWUNDO; + + return SHFileOperationW(&file_op); +} + int InitPathUtils(void) { GetModuleFileNameA(nullptr, szMirandaPath, _countof(szMirandaPath)); -- cgit v1.2.3