2009-10-22 07:35:43 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: samples/fswatcher/fswatcher.cpp
|
|
|
|
// Purpose: wxFileSystemWatcher sample
|
|
|
|
// Author: Bartosz Bekier
|
|
|
|
// Created: 2009-06-27
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Bartosz Bekier
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2012-03-03 19:28:58 -05:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2009-10-22 07:35:43 -04:00
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/fswatcher.h"
|
|
|
|
#include "wx/listctrl.h"
|
2010-10-24 18:41:13 -04:00
|
|
|
#include "wx/cmdline.h"
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
// Define a new frame type: this is going to be our main frame
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame(const wxString& title);
|
|
|
|
virtual ~MyFrame();
|
|
|
|
|
2011-05-03 19:31:39 -04:00
|
|
|
// Add an entry of the specified type asking the user for the filename if
|
|
|
|
// the one passed to this function is empty.
|
|
|
|
void AddEntry(wxFSWPathType type, wxString filename = wxString());
|
2010-10-24 18:41:13 -04:00
|
|
|
|
|
|
|
bool CreateWatcherIfNecessary();
|
|
|
|
|
2009-10-22 07:35:43 -04:00
|
|
|
private:
|
|
|
|
// file system watcher creation
|
|
|
|
void CreateWatcher();
|
|
|
|
|
|
|
|
// event handlers
|
|
|
|
void OnClear(wxCommandEvent& WXUNUSED(event)) { m_evtConsole->Clear(); }
|
|
|
|
void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
|
|
|
|
void OnWatch(wxCommandEvent& event);
|
2012-10-24 14:21:31 -04:00
|
|
|
void OnFollowLinks(wxCommandEvent& event);
|
2009-10-22 07:35:43 -04:00
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
|
|
|
void OnAdd(wxCommandEvent& event);
|
2011-05-03 19:31:39 -04:00
|
|
|
void OnAddTree(wxCommandEvent& event);
|
2009-10-22 07:35:43 -04:00
|
|
|
void OnRemove(wxCommandEvent& event);
|
2012-10-24 14:21:31 -04:00
|
|
|
void OnRemoveUpdateUI(wxUpdateUIEvent& event);
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
void OnFileSystemEvent(wxFileSystemWatcherEvent& event);
|
|
|
|
void LogEvent(const wxFileSystemWatcherEvent& event);
|
|
|
|
|
|
|
|
wxTextCtrl *m_evtConsole; // events console
|
|
|
|
wxListView *m_filesList; // list of watched paths
|
|
|
|
wxFileSystemWatcher* m_watcher; // file system watcher
|
2012-10-24 14:21:31 -04:00
|
|
|
bool m_followLinks; // should symlinks be dereferenced
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
const static wxString LOG_FORMAT; // how to format events
|
|
|
|
};
|
|
|
|
|
|
|
|
const wxString MyFrame::LOG_FORMAT = " %-12s %-36s %-36s";
|
|
|
|
|
|
|
|
// Define a new application type, each program should derive a class from wxApp
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// 'Main program' equivalent: the program execution "starts" here
|
|
|
|
virtual bool OnInit()
|
|
|
|
{
|
2010-10-24 18:41:13 -04:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2009-10-22 07:36:35 -04:00
|
|
|
wxLog::AddTraceMask("EventSource");
|
2009-10-22 07:35:43 -04:00
|
|
|
wxLog::AddTraceMask(wxTRACE_FSWATCHER);
|
|
|
|
|
|
|
|
// create the main application window
|
|
|
|
m_frame = new MyFrame("File System Watcher wxWidgets App");
|
|
|
|
|
|
|
|
// If we returned false here, the application would exit immediately.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the file system watcher here, because it needs an active loop
|
|
|
|
virtual void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
|
|
|
|
{
|
2010-10-24 18:41:13 -04:00
|
|
|
if ( m_frame->CreateWatcherIfNecessary() )
|
|
|
|
{
|
|
|
|
if ( !m_dirToWatch.empty() )
|
2011-05-03 19:31:39 -04:00
|
|
|
m_frame->AddEntry(wxFSWPath_Dir, m_dirToWatch);
|
2010-10-24 18:41:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnInitCmdLine(wxCmdLineParser& parser)
|
|
|
|
{
|
|
|
|
wxApp::OnInitCmdLine(parser);
|
|
|
|
parser.AddParam("directory to watch",
|
|
|
|
wxCMD_LINE_VAL_STRING,
|
|
|
|
wxCMD_LINE_PARAM_OPTIONAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnCmdLineParsed(parser) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( parser.GetParamCount() )
|
|
|
|
m_dirToWatch = parser.GetParam();
|
|
|
|
|
|
|
|
return true;
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MyFrame *m_frame;
|
2010-10-24 18:41:13 -04:00
|
|
|
|
|
|
|
// The directory to watch if specified on the command line.
|
|
|
|
wxString m_dirToWatch;
|
2009-10-22 07:35:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new application object: this macro will allow wxWidgets to create
|
|
|
|
// the application object during program execution (it's better than using a
|
|
|
|
// static object for many reasons) and also declares the accessor function
|
|
|
|
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
|
|
|
|
// not wxApp)
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// frame constructor
|
|
|
|
MyFrame::MyFrame(const wxString& title)
|
|
|
|
: wxFrame(NULL, wxID_ANY, title),
|
2012-10-24 14:21:31 -04:00
|
|
|
m_watcher(NULL), m_followLinks(false)
|
2009-10-22 07:35:43 -04:00
|
|
|
{
|
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
// IDs for menu and buttons
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MENU_ID_QUIT = wxID_EXIT,
|
|
|
|
MENU_ID_CLEAR = wxID_CLEAR,
|
|
|
|
MENU_ID_WATCH = 101,
|
2012-10-24 14:21:31 -04:00
|
|
|
MENU_ID_DEREFERENCE,
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
BTN_ID_ADD = 200,
|
2011-05-03 19:31:39 -04:00
|
|
|
BTN_ID_ADD_TREE,
|
|
|
|
BTN_ID_REMOVE
|
2009-10-22 07:35:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// ================================================================
|
|
|
|
// menu
|
|
|
|
|
|
|
|
// create a menu bar
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
|
|
|
menuFile->Append(MENU_ID_CLEAR, "&Clear log\tCtrl-L");
|
|
|
|
menuFile->AppendSeparator();
|
|
|
|
menuFile->Append(MENU_ID_QUIT, "E&xit\tAlt-X", "Quit this program");
|
|
|
|
|
|
|
|
// "Watch" menu
|
|
|
|
wxMenu *menuMon = new wxMenu;
|
|
|
|
wxMenuItem* it = menuMon->AppendCheckItem(MENU_ID_WATCH, "&Watch\tCtrl-W");
|
|
|
|
// started by default, because file system watcher is started by default
|
|
|
|
it->Check(true);
|
|
|
|
|
2012-10-24 14:21:31 -04:00
|
|
|
#if defined(__UNIX__)
|
|
|
|
// Let the user decide whether to dereference symlinks. If he makes the
|
|
|
|
// wrong choice, asserts will occur if the symlink target is also watched
|
|
|
|
it = menuMon->AppendCheckItem(MENU_ID_DEREFERENCE,
|
|
|
|
"&Follow symlinks\tCtrl-F",
|
|
|
|
_("If checked, dereference symlinks")
|
|
|
|
);
|
|
|
|
it->Check(false);
|
|
|
|
Connect(MENU_ID_DEREFERENCE, wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnFollowLinks));
|
|
|
|
#endif // __UNIX__
|
|
|
|
|
2009-10-22 07:35:43 -04:00
|
|
|
// the "About" item should be in the help menu
|
|
|
|
wxMenu *menuHelp = new wxMenu;
|
2012-01-20 11:51:09 -05:00
|
|
|
menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
// now append the freshly created menu to the menu bar...
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar();
|
|
|
|
menuBar->Append(menuFile, "&File");
|
|
|
|
menuBar->Append(menuMon, "&Watch");
|
|
|
|
menuBar->Append(menuHelp, "&Help");
|
|
|
|
|
|
|
|
// ... and attach this menu bar to the frame
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
|
|
|
|
// ================================================================
|
|
|
|
// upper panel
|
|
|
|
|
|
|
|
// panel
|
|
|
|
wxPanel *panel = new wxPanel(this);
|
|
|
|
wxSizer *panelSizer = new wxGridSizer(2);
|
|
|
|
wxBoxSizer *leftSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
|
|
|
|
// label
|
|
|
|
wxStaticText* label = new wxStaticText(panel, wxID_ANY, "Watched paths");
|
|
|
|
leftSizer->Add(label, wxSizerFlags().Center().Border(wxALL));
|
|
|
|
|
|
|
|
// list of files
|
|
|
|
m_filesList = new wxListView(panel, wxID_ANY, wxPoint(-1,-1),
|
|
|
|
wxSize(300,200), wxLC_LIST | wxLC_SINGLE_SEL);
|
|
|
|
leftSizer->Add(m_filesList, wxSizerFlags(1).Expand());
|
|
|
|
|
|
|
|
// buttons
|
|
|
|
wxButton* buttonAdd = new wxButton(panel, BTN_ID_ADD, "&Add");
|
2011-05-03 19:31:39 -04:00
|
|
|
wxButton* buttonAddTree = new wxButton(panel, BTN_ID_ADD_TREE, "Add &tree");
|
2009-10-22 07:35:43 -04:00
|
|
|
wxButton* buttonRemove = new wxButton(panel, BTN_ID_REMOVE, "&Remove");
|
|
|
|
wxSizer *btnSizer = new wxGridSizer(2);
|
|
|
|
btnSizer->Add(buttonAdd, wxSizerFlags().Center().Border(wxALL));
|
2011-05-03 19:31:39 -04:00
|
|
|
btnSizer->Add(buttonAddTree, wxSizerFlags().Center().Border(wxALL));
|
2009-10-22 07:35:43 -04:00
|
|
|
btnSizer->Add(buttonRemove, wxSizerFlags().Center().Border(wxALL));
|
|
|
|
|
|
|
|
// and put it all together
|
|
|
|
leftSizer->Add(btnSizer, wxSizerFlags(0).Expand());
|
|
|
|
panelSizer->Add(leftSizer, wxSizerFlags(1).Expand());
|
|
|
|
panel->SetSizerAndFit(panelSizer);
|
|
|
|
|
|
|
|
// ================================================================
|
|
|
|
// lower panel
|
|
|
|
|
|
|
|
wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
|
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
wxTE_READONLY);
|
|
|
|
wxString h = wxString::Format(LOG_FORMAT, "event", "path", "new path");
|
|
|
|
headerText->SetValue(h);
|
|
|
|
|
|
|
|
// event console
|
|
|
|
m_evtConsole = new wxTextCtrl(this, wxID_ANY, "",
|
|
|
|
wxDefaultPosition, wxSize(200,200),
|
|
|
|
wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL);
|
|
|
|
|
|
|
|
// set monospace font to have output in nice columns
|
|
|
|
wxFont font(9, wxFONTFAMILY_TELETYPE,
|
|
|
|
wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
|
|
|
|
headerText->SetFont(font);
|
|
|
|
m_evtConsole->SetFont(font);
|
|
|
|
|
|
|
|
// ================================================================
|
|
|
|
// laying out whole frame
|
|
|
|
|
|
|
|
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
sizer->Add(panel, wxSizerFlags(1).Expand());
|
|
|
|
sizer->Add(headerText, wxSizerFlags().Expand());
|
|
|
|
sizer->Add(m_evtConsole, wxSizerFlags(1).Expand());
|
|
|
|
SetSizerAndFit(sizer);
|
|
|
|
|
|
|
|
// set size and position on screen
|
|
|
|
SetSize(800, 600);
|
|
|
|
CentreOnScreen();
|
|
|
|
|
|
|
|
// ================================================================
|
|
|
|
// event handlers & show
|
|
|
|
|
|
|
|
// menu
|
|
|
|
Connect(MENU_ID_CLEAR, wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnClear));
|
|
|
|
Connect(MENU_ID_QUIT, wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnQuit));
|
|
|
|
Connect(MENU_ID_WATCH, wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnWatch));
|
|
|
|
Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnAbout));
|
|
|
|
|
|
|
|
// buttons
|
|
|
|
Connect(BTN_ID_ADD, wxEVT_COMMAND_BUTTON_CLICKED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnAdd));
|
2011-05-03 19:31:39 -04:00
|
|
|
Connect(BTN_ID_ADD_TREE, wxEVT_COMMAND_BUTTON_CLICKED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnAddTree));
|
2009-10-22 07:35:43 -04:00
|
|
|
Connect(BTN_ID_REMOVE, wxEVT_COMMAND_BUTTON_CLICKED,
|
|
|
|
wxCommandEventHandler(MyFrame::OnRemove));
|
2012-10-24 14:21:31 -04:00
|
|
|
Connect(BTN_ID_REMOVE, wxEVT_UPDATE_UI,
|
|
|
|
wxUpdateUIEventHandler(MyFrame::OnRemoveUpdateUI));
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
// and show itself (the frames, unlike simple controls, are not shown when
|
|
|
|
// created initially)
|
|
|
|
Show(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
|
|
|
delete m_watcher;
|
|
|
|
}
|
|
|
|
|
2010-10-24 18:41:13 -04:00
|
|
|
bool MyFrame::CreateWatcherIfNecessary()
|
2009-10-22 07:35:43 -04:00
|
|
|
{
|
|
|
|
if (m_watcher)
|
2010-10-24 18:41:13 -04:00
|
|
|
return false;
|
2009-10-22 07:35:43 -04:00
|
|
|
|
|
|
|
CreateWatcher();
|
|
|
|
Connect(wxEVT_FSWATCHER,
|
|
|
|
wxFileSystemWatcherEventHandler(MyFrame::OnFileSystemEvent));
|
2010-10-24 18:41:13 -04:00
|
|
|
|
|
|
|
return true;
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::CreateWatcher()
|
|
|
|
{
|
|
|
|
wxCHECK_RET(!m_watcher, "Watcher already initialized");
|
|
|
|
m_watcher = new wxFileSystemWatcher();
|
|
|
|
m_watcher->SetOwner(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// event handlers
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxMessageBox("Demonstrates the usage of file system watcher, "
|
|
|
|
"the wxWidgets monitoring system notifying you of "
|
|
|
|
"changes done to your files.\n"
|
|
|
|
"(c) 2009 Bartosz Bekier\n",
|
|
|
|
"About wxWidgets File System Watcher Sample",
|
|
|
|
wxOK | wxICON_INFORMATION, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnWatch(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
wxLogDebug("%s start=%d", __WXFUNCTION__, event.IsChecked());
|
|
|
|
|
|
|
|
if (event.IsChecked())
|
|
|
|
{
|
|
|
|
wxCHECK_RET(!m_watcher, "Watcher already initialized");
|
|
|
|
CreateWatcher();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxCHECK_RET(m_watcher, "Watcher not initialized");
|
|
|
|
m_filesList->DeleteAllItems();
|
2010-06-20 14:18:23 -04:00
|
|
|
wxDELETE(m_watcher);
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 14:21:31 -04:00
|
|
|
void MyFrame::OnFollowLinks(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
m_followLinks = event.IsChecked();
|
|
|
|
}
|
|
|
|
|
2009-10-22 07:35:43 -04:00
|
|
|
void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2011-05-03 19:31:39 -04:00
|
|
|
AddEntry(wxFSWPath_Dir);
|
|
|
|
}
|
2009-10-22 07:35:43 -04:00
|
|
|
|
2011-05-03 19:31:39 -04:00
|
|
|
void MyFrame::OnAddTree(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
AddEntry(wxFSWPath_Tree);
|
2010-10-24 18:41:13 -04:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:31:39 -04:00
|
|
|
void MyFrame::AddEntry(wxFSWPathType type, wxString filename)
|
2010-10-24 18:41:13 -04:00
|
|
|
{
|
2011-05-03 19:31:39 -04:00
|
|
|
if ( filename.empty() )
|
|
|
|
{
|
|
|
|
// TODO account for adding the files as well
|
|
|
|
filename = wxDirSelector("Choose a folder to watch", "",
|
|
|
|
wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
|
|
|
|
if ( filename.empty() )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCHECK_RET(m_watcher, "Watcher not initialized");
|
|
|
|
|
|
|
|
wxLogDebug("Adding %s: '%s'",
|
|
|
|
filename,
|
|
|
|
type == wxFSWPath_Dir ? "directory" : "directory tree");
|
2009-10-22 07:35:43 -04:00
|
|
|
|
2012-07-13 07:22:10 -04:00
|
|
|
wxString prefix;
|
2011-05-03 19:31:39 -04:00
|
|
|
bool ok = false;
|
2012-10-24 14:21:31 -04:00
|
|
|
|
|
|
|
// This will tell wxFileSystemWatcher whether to dereference symlinks
|
|
|
|
wxFileName fn = wxFileName::DirName(filename);
|
|
|
|
if (!m_followLinks)
|
|
|
|
{
|
|
|
|
fn.DontFollowLink();
|
|
|
|
}
|
|
|
|
|
2011-05-03 19:31:39 -04:00
|
|
|
switch ( type )
|
2009-10-22 07:35:43 -04:00
|
|
|
{
|
2011-05-03 19:31:39 -04:00
|
|
|
case wxFSWPath_Dir:
|
2012-10-24 14:21:31 -04:00
|
|
|
ok = m_watcher->Add(fn);
|
2012-07-13 07:22:10 -04:00
|
|
|
prefix = "Dir: ";
|
2011-05-03 19:31:39 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wxFSWPath_Tree:
|
2012-10-24 14:21:31 -04:00
|
|
|
ok = m_watcher->AddTree(fn);
|
2012-07-13 07:22:10 -04:00
|
|
|
prefix = "Tree: ";
|
2011-05-03 19:31:39 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wxFSWPath_File:
|
|
|
|
case wxFSWPath_None:
|
|
|
|
wxFAIL_MSG( "Unexpected path type." );
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
2011-05-03 19:31:39 -04:00
|
|
|
|
|
|
|
if (!ok)
|
2009-10-22 07:35:43 -04:00
|
|
|
{
|
2011-05-03 19:31:39 -04:00
|
|
|
wxLogError("Error adding '%s' to watched paths", filename);
|
|
|
|
return;
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
2011-05-03 19:31:39 -04:00
|
|
|
|
2012-07-13 07:22:10 -04:00
|
|
|
// Prepend 'prefix' to the filepath, partly for display
|
|
|
|
// but mostly so that OnRemove() can work out the correct way to remove it
|
|
|
|
m_filesList->InsertItem(m_filesList->GetItemCount(),
|
|
|
|
prefix + wxFileName::DirName(filename).GetFullPath());
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnRemove(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxCHECK_RET(m_watcher, "Watcher not initialized");
|
|
|
|
long idx = m_filesList->GetFirstSelected();
|
|
|
|
if (idx == -1)
|
|
|
|
return;
|
|
|
|
|
2012-07-13 07:22:10 -04:00
|
|
|
bool ret;
|
2012-10-24 14:21:31 -04:00
|
|
|
wxString path = m_filesList->GetItemText(idx).Mid(6);
|
|
|
|
|
|
|
|
// This will tell wxFileSystemWatcher whether to dereference symlinks
|
|
|
|
wxFileName fn = wxFileName::DirName(path);
|
|
|
|
if (!m_followLinks)
|
|
|
|
{
|
|
|
|
fn.DontFollowLink();
|
|
|
|
}
|
|
|
|
|
2009-10-22 07:35:43 -04:00
|
|
|
// TODO we know it is a dir, but it doesn't have to be
|
2012-10-24 14:21:31 -04:00
|
|
|
if (m_filesList->GetItemText(idx).StartsWith("Dir: "))
|
2012-07-13 07:22:10 -04:00
|
|
|
{
|
2012-10-24 14:21:31 -04:00
|
|
|
ret = m_watcher->Remove(fn);
|
2012-07-13 07:22:10 -04:00
|
|
|
}
|
2012-10-24 14:21:31 -04:00
|
|
|
else if (m_filesList->GetItemText(idx).StartsWith("Tree: "))
|
2012-07-13 07:22:10 -04:00
|
|
|
{
|
2012-10-24 14:21:31 -04:00
|
|
|
ret = m_watcher->RemoveTree(fn);
|
2012-07-13 07:22:10 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxFAIL_MSG("Unexpected item in wxListView.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
2009-10-22 07:35:43 -04:00
|
|
|
{
|
|
|
|
wxLogError("Error removing '%s' from watched paths", path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_filesList->DeleteItem(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 14:21:31 -04:00
|
|
|
void MyFrame::OnRemoveUpdateUI(wxUpdateUIEvent& event)
|
|
|
|
{
|
|
|
|
event.Enable(m_filesList->GetFirstSelected() != wxNOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2009-10-22 07:35:43 -04:00
|
|
|
void MyFrame::OnFileSystemEvent(wxFileSystemWatcherEvent& event)
|
|
|
|
{
|
|
|
|
// TODO remove when code is rock-solid
|
2010-10-24 18:41:18 -04:00
|
|
|
wxLogTrace(wxTRACE_FSWATCHER, "*** %s ***", event.ToString());
|
2009-10-22 07:35:43 -04:00
|
|
|
LogEvent(event);
|
2012-10-14 21:10:12 -04:00
|
|
|
|
|
|
|
int type = event.GetChangeType();
|
|
|
|
if ((type == wxFSW_EVENT_DELETE) || (type == wxFSW_EVENT_RENAME))
|
|
|
|
{
|
|
|
|
// If path is one of our watched dirs, we need to react to this
|
|
|
|
// otherwise there'll be asserts if later we try to remove it
|
|
|
|
wxString eventpath = event.GetPath().GetFullPath();
|
|
|
|
bool found(false);
|
|
|
|
for (size_t n = m_filesList->GetItemCount(); n > 0; --n)
|
|
|
|
{
|
|
|
|
wxString path, foo = m_filesList->GetItemText(n-1);
|
|
|
|
if ((!m_filesList->GetItemText(n-1).StartsWith("Dir: ", &path)) &&
|
|
|
|
(!m_filesList->GetItemText(n-1).StartsWith("Tree: ", &path)))
|
|
|
|
{
|
|
|
|
wxFAIL_MSG("Unexpected item in wxListView.");
|
|
|
|
}
|
|
|
|
if (path == eventpath)
|
|
|
|
{
|
|
|
|
if (type == wxFSW_EVENT_DELETE)
|
|
|
|
{
|
|
|
|
m_filesList->DeleteItem(n-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// At least in wxGTK, we'll never get here: renaming the top
|
|
|
|
// watched dir gives IN_MOVE_SELF and no new-name info.
|
|
|
|
// However I'll leave the code in case other platforms do
|
|
|
|
wxString newname = event.GetNewPath().GetFullPath();
|
|
|
|
if (newname.empty() ||
|
|
|
|
newname == event.GetPath().GetFullPath())
|
|
|
|
{
|
|
|
|
// Just in case either of these are possible...
|
|
|
|
wxLogTrace(wxTRACE_FSWATCHER,
|
|
|
|
"Invalid attempt to rename to %s", newname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wxString prefix =
|
|
|
|
m_filesList->GetItemText(n-1).StartsWith("Dir: ") ?
|
|
|
|
"Dir: " : "Tree: ";
|
|
|
|
m_filesList->SetItemText(n-1, prefix + newname);
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
// Don't break: a filepath may have been added more than once
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
|
|
|
"Your watched path %s has been deleted or renamed\n",
|
|
|
|
eventpath);
|
|
|
|
m_evtConsole->AppendText(msg);
|
|
|
|
}
|
|
|
|
}
|
2009-10-22 07:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static wxString GetFSWEventChangeTypeName(int changeType)
|
|
|
|
{
|
|
|
|
switch (changeType)
|
|
|
|
{
|
|
|
|
case wxFSW_EVENT_CREATE:
|
|
|
|
return "CREATE";
|
|
|
|
case wxFSW_EVENT_DELETE:
|
|
|
|
return "DELETE";
|
|
|
|
case wxFSW_EVENT_RENAME:
|
|
|
|
return "RENAME";
|
|
|
|
case wxFSW_EVENT_MODIFY:
|
|
|
|
return "MODIFY";
|
|
|
|
case wxFSW_EVENT_ACCESS:
|
|
|
|
return "ACCESS";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "INVALID_TYPE";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::LogEvent(const wxFileSystemWatcherEvent& event)
|
|
|
|
{
|
|
|
|
wxString entry = wxString::Format(LOG_FORMAT + "\n",
|
|
|
|
GetFSWEventChangeTypeName(event.GetChangeType()),
|
|
|
|
event.GetPath().GetFullPath(),
|
|
|
|
event.GetNewPath().GetFullPath());
|
|
|
|
m_evtConsole->AppendText(entry);
|
|
|
|
}
|