Respect wxFileName::DontFollowLink() in wxFileSystemWatcher.
[wxWidgets.git] / samples / fswatcher / fswatcher.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/fswatcher/fswatcher.cpp
3 // Purpose: wxFileSystemWatcher sample
4 // Author: Bartosz Bekier
5 // Created: 2009-06-27
6 // RCS-ID: $Id$
7 // Copyright: (c) Bartosz Bekier
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16 #ifndef WX_PRECOMP
17 #include "wx/wx.h"
18 #endif
19
20 #ifndef wxHAS_IMAGES_IN_RESOURCES
21 #include "../sample.xpm"
22 #endif
23
24 #include "wx/fswatcher.h"
25 #include "wx/listctrl.h"
26 #include "wx/cmdline.h"
27
28 // Define a new frame type: this is going to be our main frame
29 class MyFrame : public wxFrame
30 {
31 public:
32 MyFrame(const wxString& title);
33 virtual ~MyFrame();
34
35 // Add an entry of the specified type asking the user for the filename if
36 // the one passed to this function is empty.
37 void AddEntry(wxFSWPathType type, wxString filename = wxString());
38
39 bool CreateWatcherIfNecessary();
40
41 private:
42 // file system watcher creation
43 void CreateWatcher();
44
45 // event handlers
46 void OnClear(wxCommandEvent& WXUNUSED(event)) { m_evtConsole->Clear(); }
47 void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
48 void OnWatch(wxCommandEvent& event);
49 void OnFollowLinks(wxCommandEvent& event);
50 void OnAbout(wxCommandEvent& event);
51
52 void OnAdd(wxCommandEvent& event);
53 void OnAddTree(wxCommandEvent& event);
54 void OnRemove(wxCommandEvent& event);
55 void OnRemoveUpdateUI(wxUpdateUIEvent& event);
56
57 void OnFileSystemEvent(wxFileSystemWatcherEvent& event);
58 void LogEvent(const wxFileSystemWatcherEvent& event);
59
60 wxTextCtrl *m_evtConsole; // events console
61 wxListView *m_filesList; // list of watched paths
62 wxFileSystemWatcher* m_watcher; // file system watcher
63 bool m_followLinks; // should symlinks be dereferenced
64
65 const static wxString LOG_FORMAT; // how to format events
66 };
67
68 const wxString MyFrame::LOG_FORMAT = " %-12s %-36s %-36s";
69
70 // Define a new application type, each program should derive a class from wxApp
71 class MyApp : public wxApp
72 {
73 public:
74 // 'Main program' equivalent: the program execution "starts" here
75 virtual bool OnInit()
76 {
77 if ( !wxApp::OnInit() )
78 return false;
79
80 wxLog::AddTraceMask("EventSource");
81 wxLog::AddTraceMask(wxTRACE_FSWATCHER);
82
83 // create the main application window
84 m_frame = new MyFrame("File System Watcher wxWidgets App");
85
86 // If we returned false here, the application would exit immediately.
87 return true;
88 }
89
90 // create the file system watcher here, because it needs an active loop
91 virtual void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
92 {
93 if ( m_frame->CreateWatcherIfNecessary() )
94 {
95 if ( !m_dirToWatch.empty() )
96 m_frame->AddEntry(wxFSWPath_Dir, m_dirToWatch);
97 }
98 }
99
100 virtual void OnInitCmdLine(wxCmdLineParser& parser)
101 {
102 wxApp::OnInitCmdLine(parser);
103 parser.AddParam("directory to watch",
104 wxCMD_LINE_VAL_STRING,
105 wxCMD_LINE_PARAM_OPTIONAL);
106 }
107
108 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
109 {
110 if ( !wxApp::OnCmdLineParsed(parser) )
111 return false;
112
113 if ( parser.GetParamCount() )
114 m_dirToWatch = parser.GetParam();
115
116 return true;
117 }
118
119 private:
120 MyFrame *m_frame;
121
122 // The directory to watch if specified on the command line.
123 wxString m_dirToWatch;
124 };
125
126 // Create a new application object: this macro will allow wxWidgets to create
127 // the application object during program execution (it's better than using a
128 // static object for many reasons) and also declares the accessor function
129 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
130 // not wxApp)
131 IMPLEMENT_APP(MyApp)
132
133
134 // ============================================================================
135 // implementation
136 // ============================================================================
137
138 // frame constructor
139 MyFrame::MyFrame(const wxString& title)
140 : wxFrame(NULL, wxID_ANY, title),
141 m_watcher(NULL), m_followLinks(false)
142 {
143 SetIcon(wxICON(sample));
144
145 // IDs for menu and buttons
146 enum
147 {
148 MENU_ID_QUIT = wxID_EXIT,
149 MENU_ID_CLEAR = wxID_CLEAR,
150 MENU_ID_WATCH = 101,
151 MENU_ID_DEREFERENCE,
152
153 BTN_ID_ADD = 200,
154 BTN_ID_ADD_TREE,
155 BTN_ID_REMOVE
156 };
157
158 // ================================================================
159 // menu
160
161 // create a menu bar
162 wxMenu *menuFile = new wxMenu;
163 menuFile->Append(MENU_ID_CLEAR, "&Clear log\tCtrl-L");
164 menuFile->AppendSeparator();
165 menuFile->Append(MENU_ID_QUIT, "E&xit\tAlt-X", "Quit this program");
166
167 // "Watch" menu
168 wxMenu *menuMon = new wxMenu;
169 wxMenuItem* it = menuMon->AppendCheckItem(MENU_ID_WATCH, "&Watch\tCtrl-W");
170 // started by default, because file system watcher is started by default
171 it->Check(true);
172
173 #if defined(__UNIX__)
174 // Let the user decide whether to dereference symlinks. If he makes the
175 // wrong choice, asserts will occur if the symlink target is also watched
176 it = menuMon->AppendCheckItem(MENU_ID_DEREFERENCE,
177 "&Follow symlinks\tCtrl-F",
178 _("If checked, dereference symlinks")
179 );
180 it->Check(false);
181 Connect(MENU_ID_DEREFERENCE, wxEVT_COMMAND_MENU_SELECTED,
182 wxCommandEventHandler(MyFrame::OnFollowLinks));
183 #endif // __UNIX__
184
185 // the "About" item should be in the help menu
186 wxMenu *menuHelp = new wxMenu;
187 menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
188
189 // now append the freshly created menu to the menu bar...
190 wxMenuBar *menuBar = new wxMenuBar();
191 menuBar->Append(menuFile, "&File");
192 menuBar->Append(menuMon, "&Watch");
193 menuBar->Append(menuHelp, "&Help");
194
195 // ... and attach this menu bar to the frame
196 SetMenuBar(menuBar);
197
198 // ================================================================
199 // upper panel
200
201 // panel
202 wxPanel *panel = new wxPanel(this);
203 wxSizer *panelSizer = new wxGridSizer(2);
204 wxBoxSizer *leftSizer = new wxBoxSizer(wxVERTICAL);
205
206 // label
207 wxStaticText* label = new wxStaticText(panel, wxID_ANY, "Watched paths");
208 leftSizer->Add(label, wxSizerFlags().Center().Border(wxALL));
209
210 // list of files
211 m_filesList = new wxListView(panel, wxID_ANY, wxPoint(-1,-1),
212 wxSize(300,200), wxLC_LIST | wxLC_SINGLE_SEL);
213 leftSizer->Add(m_filesList, wxSizerFlags(1).Expand());
214
215 // buttons
216 wxButton* buttonAdd = new wxButton(panel, BTN_ID_ADD, "&Add");
217 wxButton* buttonAddTree = new wxButton(panel, BTN_ID_ADD_TREE, "Add &tree");
218 wxButton* buttonRemove = new wxButton(panel, BTN_ID_REMOVE, "&Remove");
219 wxSizer *btnSizer = new wxGridSizer(2);
220 btnSizer->Add(buttonAdd, wxSizerFlags().Center().Border(wxALL));
221 btnSizer->Add(buttonAddTree, wxSizerFlags().Center().Border(wxALL));
222 btnSizer->Add(buttonRemove, wxSizerFlags().Center().Border(wxALL));
223
224 // and put it all together
225 leftSizer->Add(btnSizer, wxSizerFlags(0).Expand());
226 panelSizer->Add(leftSizer, wxSizerFlags(1).Expand());
227 panel->SetSizerAndFit(panelSizer);
228
229 // ================================================================
230 // lower panel
231
232 wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
233 wxDefaultPosition, wxDefaultSize,
234 wxTE_READONLY);
235 wxString h = wxString::Format(LOG_FORMAT, "event", "path", "new path");
236 headerText->SetValue(h);
237
238 // event console
239 m_evtConsole = new wxTextCtrl(this, wxID_ANY, "",
240 wxDefaultPosition, wxSize(200,200),
241 wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL);
242
243 // set monospace font to have output in nice columns
244 wxFont font(9, wxFONTFAMILY_TELETYPE,
245 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
246 headerText->SetFont(font);
247 m_evtConsole->SetFont(font);
248
249 // ================================================================
250 // laying out whole frame
251
252 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
253 sizer->Add(panel, wxSizerFlags(1).Expand());
254 sizer->Add(headerText, wxSizerFlags().Expand());
255 sizer->Add(m_evtConsole, wxSizerFlags(1).Expand());
256 SetSizerAndFit(sizer);
257
258 // set size and position on screen
259 SetSize(800, 600);
260 CentreOnScreen();
261
262 // ================================================================
263 // event handlers & show
264
265 // menu
266 Connect(MENU_ID_CLEAR, wxEVT_COMMAND_MENU_SELECTED,
267 wxCommandEventHandler(MyFrame::OnClear));
268 Connect(MENU_ID_QUIT, wxEVT_COMMAND_MENU_SELECTED,
269 wxCommandEventHandler(MyFrame::OnQuit));
270 Connect(MENU_ID_WATCH, wxEVT_COMMAND_MENU_SELECTED,
271 wxCommandEventHandler(MyFrame::OnWatch));
272 Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
273 wxCommandEventHandler(MyFrame::OnAbout));
274
275 // buttons
276 Connect(BTN_ID_ADD, wxEVT_COMMAND_BUTTON_CLICKED,
277 wxCommandEventHandler(MyFrame::OnAdd));
278 Connect(BTN_ID_ADD_TREE, wxEVT_COMMAND_BUTTON_CLICKED,
279 wxCommandEventHandler(MyFrame::OnAddTree));
280 Connect(BTN_ID_REMOVE, wxEVT_COMMAND_BUTTON_CLICKED,
281 wxCommandEventHandler(MyFrame::OnRemove));
282 Connect(BTN_ID_REMOVE, wxEVT_UPDATE_UI,
283 wxUpdateUIEventHandler(MyFrame::OnRemoveUpdateUI));
284
285 // and show itself (the frames, unlike simple controls, are not shown when
286 // created initially)
287 Show(true);
288 }
289
290 MyFrame::~MyFrame()
291 {
292 delete m_watcher;
293 }
294
295 bool MyFrame::CreateWatcherIfNecessary()
296 {
297 if (m_watcher)
298 return false;
299
300 CreateWatcher();
301 Connect(wxEVT_FSWATCHER,
302 wxFileSystemWatcherEventHandler(MyFrame::OnFileSystemEvent));
303
304 return true;
305 }
306
307 void MyFrame::CreateWatcher()
308 {
309 wxCHECK_RET(!m_watcher, "Watcher already initialized");
310 m_watcher = new wxFileSystemWatcher();
311 m_watcher->SetOwner(this);
312 }
313
314 // ============================================================================
315 // event handlers
316 // ============================================================================
317
318 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
319 {
320 wxMessageBox("Demonstrates the usage of file system watcher, "
321 "the wxWidgets monitoring system notifying you of "
322 "changes done to your files.\n"
323 "(c) 2009 Bartosz Bekier\n",
324 "About wxWidgets File System Watcher Sample",
325 wxOK | wxICON_INFORMATION, this);
326 }
327
328 void MyFrame::OnWatch(wxCommandEvent& event)
329 {
330 wxLogDebug("%s start=%d", __WXFUNCTION__, event.IsChecked());
331
332 if (event.IsChecked())
333 {
334 wxCHECK_RET(!m_watcher, "Watcher already initialized");
335 CreateWatcher();
336 }
337 else
338 {
339 wxCHECK_RET(m_watcher, "Watcher not initialized");
340 m_filesList->DeleteAllItems();
341 wxDELETE(m_watcher);
342 }
343 }
344
345 void MyFrame::OnFollowLinks(wxCommandEvent& event)
346 {
347 m_followLinks = event.IsChecked();
348 }
349
350 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
351 {
352 AddEntry(wxFSWPath_Dir);
353 }
354
355 void MyFrame::OnAddTree(wxCommandEvent& WXUNUSED(event))
356 {
357 AddEntry(wxFSWPath_Tree);
358 }
359
360 void MyFrame::AddEntry(wxFSWPathType type, wxString filename)
361 {
362 if ( filename.empty() )
363 {
364 // TODO account for adding the files as well
365 filename = wxDirSelector("Choose a folder to watch", "",
366 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
367 if ( filename.empty() )
368 return;
369 }
370
371 wxCHECK_RET(m_watcher, "Watcher not initialized");
372
373 wxLogDebug("Adding %s: '%s'",
374 filename,
375 type == wxFSWPath_Dir ? "directory" : "directory tree");
376
377 wxString prefix;
378 bool ok = false;
379
380 // This will tell wxFileSystemWatcher whether to dereference symlinks
381 wxFileName fn = wxFileName::DirName(filename);
382 if (!m_followLinks)
383 {
384 fn.DontFollowLink();
385 }
386
387 switch ( type )
388 {
389 case wxFSWPath_Dir:
390 ok = m_watcher->Add(fn);
391 prefix = "Dir: ";
392 break;
393
394 case wxFSWPath_Tree:
395 ok = m_watcher->AddTree(fn);
396 prefix = "Tree: ";
397 break;
398
399 case wxFSWPath_File:
400 case wxFSWPath_None:
401 wxFAIL_MSG( "Unexpected path type." );
402 }
403
404 if (!ok)
405 {
406 wxLogError("Error adding '%s' to watched paths", filename);
407 return;
408 }
409
410 // Prepend 'prefix' to the filepath, partly for display
411 // but mostly so that OnRemove() can work out the correct way to remove it
412 m_filesList->InsertItem(m_filesList->GetItemCount(),
413 prefix + wxFileName::DirName(filename).GetFullPath());
414 }
415
416 void MyFrame::OnRemove(wxCommandEvent& WXUNUSED(event))
417 {
418 wxCHECK_RET(m_watcher, "Watcher not initialized");
419 long idx = m_filesList->GetFirstSelected();
420 if (idx == -1)
421 return;
422
423 bool ret;
424 wxString path = m_filesList->GetItemText(idx).Mid(6);
425
426 // This will tell wxFileSystemWatcher whether to dereference symlinks
427 wxFileName fn = wxFileName::DirName(path);
428 if (!m_followLinks)
429 {
430 fn.DontFollowLink();
431 }
432
433 // TODO we know it is a dir, but it doesn't have to be
434 if (m_filesList->GetItemText(idx).StartsWith("Dir: "))
435 {
436 ret = m_watcher->Remove(fn);
437 }
438 else if (m_filesList->GetItemText(idx).StartsWith("Tree: "))
439 {
440 ret = m_watcher->RemoveTree(fn);
441 }
442 else
443 {
444 wxFAIL_MSG("Unexpected item in wxListView.");
445 }
446
447 if (!ret)
448 {
449 wxLogError("Error removing '%s' from watched paths", path);
450 }
451 else
452 {
453 m_filesList->DeleteItem(idx);
454 }
455 }
456
457 void MyFrame::OnRemoveUpdateUI(wxUpdateUIEvent& event)
458 {
459 event.Enable(m_filesList->GetFirstSelected() != wxNOT_FOUND);
460 }
461
462 void MyFrame::OnFileSystemEvent(wxFileSystemWatcherEvent& event)
463 {
464 // TODO remove when code is rock-solid
465 wxLogTrace(wxTRACE_FSWATCHER, "*** %s ***", event.ToString());
466 LogEvent(event);
467
468 int type = event.GetChangeType();
469 if ((type == wxFSW_EVENT_DELETE) || (type == wxFSW_EVENT_RENAME))
470 {
471 // If path is one of our watched dirs, we need to react to this
472 // otherwise there'll be asserts if later we try to remove it
473 wxString eventpath = event.GetPath().GetFullPath();
474 bool found(false);
475 for (size_t n = m_filesList->GetItemCount(); n > 0; --n)
476 {
477 wxString path, foo = m_filesList->GetItemText(n-1);
478 if ((!m_filesList->GetItemText(n-1).StartsWith("Dir: ", &path)) &&
479 (!m_filesList->GetItemText(n-1).StartsWith("Tree: ", &path)))
480 {
481 wxFAIL_MSG("Unexpected item in wxListView.");
482 }
483 if (path == eventpath)
484 {
485 if (type == wxFSW_EVENT_DELETE)
486 {
487 m_filesList->DeleteItem(n-1);
488 }
489 else
490 {
491 // At least in wxGTK, we'll never get here: renaming the top
492 // watched dir gives IN_MOVE_SELF and no new-name info.
493 // However I'll leave the code in case other platforms do
494 wxString newname = event.GetNewPath().GetFullPath();
495 if (newname.empty() ||
496 newname == event.GetPath().GetFullPath())
497 {
498 // Just in case either of these are possible...
499 wxLogTrace(wxTRACE_FSWATCHER,
500 "Invalid attempt to rename to %s", newname);
501 return;
502 }
503 wxString prefix =
504 m_filesList->GetItemText(n-1).StartsWith("Dir: ") ?
505 "Dir: " : "Tree: ";
506 m_filesList->SetItemText(n-1, prefix + newname);
507 }
508 found = true;
509 // Don't break: a filepath may have been added more than once
510 }
511 }
512
513 if (found)
514 {
515 wxString msg = wxString::Format(
516 "Your watched path %s has been deleted or renamed\n",
517 eventpath);
518 m_evtConsole->AppendText(msg);
519 }
520 }
521 }
522
523
524 static wxString GetFSWEventChangeTypeName(int changeType)
525 {
526 switch (changeType)
527 {
528 case wxFSW_EVENT_CREATE:
529 return "CREATE";
530 case wxFSW_EVENT_DELETE:
531 return "DELETE";
532 case wxFSW_EVENT_RENAME:
533 return "RENAME";
534 case wxFSW_EVENT_MODIFY:
535 return "MODIFY";
536 case wxFSW_EVENT_ACCESS:
537 return "ACCESS";
538 }
539
540 return "INVALID_TYPE";
541 }
542
543 void MyFrame::LogEvent(const wxFileSystemWatcherEvent& event)
544 {
545 wxString entry = wxString::Format(LOG_FORMAT + "\n",
546 GetFSWEventChangeTypeName(event.GetChangeType()),
547 event.GetPath().GetFullPath(),
548 event.GetNewPath().GetFullPath());
549 m_evtConsole->AppendText(entry);
550 }