From 37024c991c2729d34756985ebb2d9426a58dcf48 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 23 Mar 2014 00:10:36 +0000 Subject: [PATCH] Fix wxFileSystemWatcher::RemoveAll() to actually work. We need to call DoRemove() on all watcher objects to really remove them, just removing our record of them was not enough and e.g. resulted in errors if we tried to re-add a previously watched path again. Closes #15531. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@76182 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/private/fswatcher.h | 10 +++++++++- samples/fswatcher/fswatcher.cpp | 26 +++++++++++++++++++++++++- src/common/fswatchercmn.cpp | 4 ++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 9cd86bbe5f..a6942dcc88 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -583,6 +583,7 @@ All: - Fix wxSocket::WaitForAccept() in non-main thread (Hajo Kirchhoff). - Fix memory overallocation in wxVector::reserve() (Nigel Paton). - Fix `wx-config --libs` in monolithic build. +- Fix wxFileSystemWatcher::RemoveAll() (Melroy Tellis). - Fix the build under HP-UX (tested under 11.31). - Add Aragonese translation. diff --git a/include/wx/private/fswatcher.h b/include/wx/private/fswatcher.h index cfffba4a89..e2bf55a89d 100644 --- a/include/wx/private/fswatcher.h +++ b/include/wx/private/fswatcher.h @@ -84,8 +84,16 @@ public: virtual bool RemoveAll() { + bool ret = true; + for ( wxFSWatchEntries::iterator it = m_watches.begin(); + it != m_watches.end(); + ++it ) + { + if ( !DoRemove(it->second) ) + ret = false; + } m_watches.clear(); - return true; + return ret; } // Check whether any filespec matches the file's ext (if present) diff --git a/samples/fswatcher/fswatcher.cpp b/samples/fswatcher/fswatcher.cpp index d8279a9057..727ce088e1 100644 --- a/samples/fswatcher/fswatcher.cpp +++ b/samples/fswatcher/fswatcher.cpp @@ -51,7 +51,9 @@ private: void OnAdd(wxCommandEvent& event); void OnAddTree(wxCommandEvent& event); void OnRemove(wxCommandEvent& event); + void OnRemoveAll(wxCommandEvent& WXUNUSED(event)); void OnRemoveUpdateUI(wxUpdateUIEvent& event); + void OnRemoveAllUpdateUI(wxUpdateUIEvent& event); void OnFileSystemEvent(wxFileSystemWatcherEvent& event); void LogEvent(const wxFileSystemWatcherEvent& event); @@ -151,7 +153,8 @@ MyFrame::MyFrame(const wxString& title) BTN_ID_ADD = 200, BTN_ID_ADD_TREE, - BTN_ID_REMOVE + BTN_ID_REMOVE, + BTN_ID_REMOVE_ALL }; // ================================================================ @@ -215,10 +218,12 @@ MyFrame::MyFrame(const wxString& title) wxButton* buttonAdd = new wxButton(panel, BTN_ID_ADD, "&Add"); wxButton* buttonAddTree = new wxButton(panel, BTN_ID_ADD_TREE, "Add &tree"); wxButton* buttonRemove = new wxButton(panel, BTN_ID_REMOVE, "&Remove"); + wxButton* buttonRemoveAll = new wxButton(panel, BTN_ID_REMOVE_ALL, "Remove a&ll"); wxSizer *btnSizer = new wxGridSizer(2); btnSizer->Add(buttonAdd, wxSizerFlags().Center().Border(wxALL)); btnSizer->Add(buttonAddTree, wxSizerFlags().Center().Border(wxALL)); btnSizer->Add(buttonRemove, wxSizerFlags().Center().Border(wxALL)); + btnSizer->Add(buttonRemoveAll, wxSizerFlags().Center().Border(wxALL)); // and put it all together leftSizer->Add(btnSizer, wxSizerFlags(0).Expand()); @@ -280,6 +285,10 @@ MyFrame::MyFrame(const wxString& title) wxCommandEventHandler(MyFrame::OnRemove)); Connect(BTN_ID_REMOVE, wxEVT_UPDATE_UI, wxUpdateUIEventHandler(MyFrame::OnRemoveUpdateUI)); + Connect(BTN_ID_REMOVE_ALL, wxEVT_BUTTON, + wxCommandEventHandler(MyFrame::OnRemoveAll)); + Connect(BTN_ID_REMOVE_ALL, wxEVT_UPDATE_UI, + wxUpdateUIEventHandler(MyFrame::OnRemoveAllUpdateUI)); // and show itself (the frames, unlike simple controls, are not shown when // created initially) @@ -453,11 +462,26 @@ void MyFrame::OnRemove(wxCommandEvent& WXUNUSED(event)) } } +void MyFrame::OnRemoveAll(wxCommandEvent& WXUNUSED(event)) +{ + if ( !m_watcher->RemoveAll() ) + { + wxLogError("Error removing all paths from watched paths"); + } + + m_filesList->DeleteAllItems(); +} + void MyFrame::OnRemoveUpdateUI(wxUpdateUIEvent& event) { event.Enable(m_filesList->GetFirstSelected() != wxNOT_FOUND); } +void MyFrame::OnRemoveAllUpdateUI(wxUpdateUIEvent& event) +{ + event.Enable( m_filesList->GetItemCount() != 0 ); +} + void MyFrame::OnFileSystemEvent(wxFileSystemWatcherEvent& event) { // TODO remove when code is rock-solid diff --git a/src/common/fswatchercmn.cpp b/src/common/fswatchercmn.cpp index 66fb869e9c..cb32a14100 100644 --- a/src/common/fswatchercmn.cpp +++ b/src/common/fswatchercmn.cpp @@ -305,9 +305,9 @@ bool wxFileSystemWatcherBase::RemoveTree(const wxFileName& path) bool wxFileSystemWatcherBase::RemoveAll() { - m_service->RemoveAll(); + const bool ret = m_service->RemoveAll(); m_watches.clear(); - return true; + return ret; } int wxFileSystemWatcherBase::GetWatchedPathsCount() const