#include "wx/fswatcher.h"
#include "wx/listctrl.h"
+#include "wx/cmdline.h"
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
MyFrame(const wxString& title);
virtual ~MyFrame();
+ // 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());
+
+ bool CreateWatcherIfNecessary();
+
private:
// file system watcher creation
- void OnEventLoopEnter();
void CreateWatcher();
// event handlers
void OnAbout(wxCommandEvent& event);
void OnAdd(wxCommandEvent& event);
+ void OnAddTree(wxCommandEvent& event);
void OnRemove(wxCommandEvent& event);
void OnFileSystemEvent(wxFileSystemWatcherEvent& event);
wxListView *m_filesList; // list of watched paths
wxFileSystemWatcher* m_watcher; // file system watcher
- friend class MyApp;
-
const static wxString LOG_FORMAT; // how to format events
};
// 'Main program' equivalent: the program execution "starts" here
virtual bool OnInit()
{
- // TODO remove then this code becomes rock-solid
- wxLog::AddTraceMask(wxTRACE_EVT_SOURCE);
+ if ( !wxApp::OnInit() )
+ return false;
+
+ wxLog::AddTraceMask("EventSource");
wxLog::AddTraceMask(wxTRACE_FSWATCHER);
// create the main application window
// create the file system watcher here, because it needs an active loop
virtual void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
{
- m_frame->OnEventLoopEnter();
+ if ( m_frame->CreateWatcherIfNecessary() )
+ {
+ if ( !m_dirToWatch.empty() )
+ m_frame->AddEntry(wxFSWPath_Dir, m_dirToWatch);
+ }
+ }
+
+ 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;
}
private:
MyFrame *m_frame;
+
+ // The directory to watch if specified on the command line.
+ wxString m_dirToWatch;
};
// Create a new application object: this macro will allow wxWidgets to create
MENU_ID_WATCH = 101,
BTN_ID_ADD = 200,
- BTN_ID_REMOVE = 201,
+ BTN_ID_ADD_TREE,
+ BTN_ID_REMOVE
};
// ================================================================
// buttons
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");
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));
// and put it all together
// buttons
Connect(BTN_ID_ADD, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(MyFrame::OnAdd));
+ Connect(BTN_ID_ADD_TREE, wxEVT_COMMAND_BUTTON_CLICKED,
+ wxCommandEventHandler(MyFrame::OnAddTree));
Connect(BTN_ID_REMOVE, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(MyFrame::OnRemove));
delete m_watcher;
}
-void MyFrame::OnEventLoopEnter()
+bool MyFrame::CreateWatcherIfNecessary()
{
if (m_watcher)
- return;
+ return false;
CreateWatcher();
Connect(wxEVT_FSWATCHER,
wxFileSystemWatcherEventHandler(MyFrame::OnFileSystemEvent));
+
+ return true;
}
void MyFrame::CreateWatcher()
{
wxCHECK_RET(m_watcher, "Watcher not initialized");
m_filesList->DeleteAllItems();
- delete m_watcher;
- m_watcher = NULL;
+ wxDELETE(m_watcher);
}
}
void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
{
- wxCHECK_RET(m_watcher, "Watcher not initialized");
+ AddEntry(wxFSWPath_Dir);
+}
- // TODO account for adding the files as well
- const wxString& dir = wxDirSelector("Choose a folder to watch", "",
- wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
- if ( dir.empty() )
- return;
+void MyFrame::OnAddTree(wxCommandEvent& WXUNUSED(event))
+{
+ AddEntry(wxFSWPath_Tree);
+}
+
+void MyFrame::AddEntry(wxFSWPathType type, wxString filename)
+{
+ 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;
+ }
- wxLogDebug("Adding directory: '%s'", dir);
+ wxCHECK_RET(m_watcher, "Watcher not initialized");
+
+ wxLogDebug("Adding %s: '%s'",
+ filename,
+ type == wxFSWPath_Dir ? "directory" : "directory tree");
- if (!m_watcher->Add(wxFileName::DirName(dir), wxFSW_EVENT_ALL))
+ bool ok = false;
+ switch ( type )
{
- wxLogError("Error adding '%s' to watched paths", dir);
+ case wxFSWPath_Dir:
+ ok = m_watcher->Add(wxFileName::DirName(filename));
+ break;
+
+ case wxFSWPath_Tree:
+ ok = m_watcher->AddTree(wxFileName::DirName(filename));
+ break;
+
+ case wxFSWPath_File:
+ case wxFSWPath_None:
+ wxFAIL_MSG( "Unexpected path type." );
}
- else
+
+ if (!ok)
{
- m_filesList->InsertItem(m_filesList->GetItemCount(), dir);
+ wxLogError("Error adding '%s' to watched paths", filename);
+ return;
}
+
+ m_filesList->InsertItem(m_filesList->GetItemCount(), filename);
}
void MyFrame::OnRemove(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnFileSystemEvent(wxFileSystemWatcherEvent& event)
{
// TODO remove when code is rock-solid
- wxLogDebug(wxTRACE_FSWATCHER, "*** %s ***", event.ToString());
+ wxLogTrace(wxTRACE_FSWATCHER, "*** %s ***", event.ToString());
LogEvent(event);
}