Debian Codegraph @

CodeLite/clFileSystemWatcher.cpp

raw
1 #include "clFileSystemWatcher.h"
2 #include <algorithm>
3 #include <set>
4 #include "fileutils.h"
5
6 wxDEFINE_EVENT(wxEVT_FILE_MODIFIED, clFileSystemEvent);
7 wxDEFINE_EVENT(wxEVT_FILE_NOT_FOUND, clFileSystemEvent);
8
9 // In milliseconds
10 #define FILE_CHECK_INTERVAL 500
11
12 clFileSystemWatcher::clFileSystemWatcher()
13 : m_owner(NULL)
14 #if CL_FSW_USE_TIMER
15 , m_timer(NULL)
16 #endif
17 {
18 #if CL_FSW_USE_TIMER
19 Bind(wxEVT_TIMER, &clFileSystemWatcher::OnTimer, this);
20 #else
21 m_watcher.SetOwner(this);
22 Bind(wxEVT_FSWATCHER, &clFileSystemWatcher::OnFileModified, this);
23 #endif
24 }
25
26 clFileSystemWatcher::~clFileSystemWatcher()
27 {
28 #if CL_FSW_USE_TIMER
29 Stop();
30 Unbind(wxEVT_TIMER, &clFileSystemWatcher::OnTimer, this);
31 #else
32 m_watcher.RemoveAll();
33 Unbind(wxEVT_FSWATCHER, &clFileSystemWatcher::OnFileModified, this);
34 #endif
35 }
36
37 void clFileSystemWatcher::SetFile(const wxFileName& filename)
38 {
39 #if CL_FSW_USE_TIMER
40 if(filename.Exists()) {
41 m_files.clear();
42 File f;
43 f.filename = filename;
44 f.lastModified = FileUtils::GetFileModificationTime(filename);
45 f.file_size = FileUtils::GetFileSize(filename);
46 m_files.insert(std::make_pair(filename.GetFullPath(), f));
47 }
48 #else
49 m_watcher.RemoveAll();
50 wxFileName dironly = filename;
51 dironly.SetFullName(""); // must be directory only
52 m_watcher.Add(dironly);
53 m_watchedFile = filename; // but we keep the file name for later usage
54 #endif
55 }
56
57 void clFileSystemWatcher::Start()
58 {
59 #if CL_FSW_USE_TIMER
60 Stop();
61
62 m_timer = new wxTimer(this);
63 m_timer->Start(FILE_CHECK_INTERVAL, true);
64 #else
65 #endif
66 }
67
68 void clFileSystemWatcher::Stop()
69 {
70 #if CL_FSW_USE_TIMER
71 if(m_timer) {
72 m_timer->Stop();
73 }
74 wxDELETE(m_timer);
75 #else
76 m_watcher.RemoveAll();
77 #endif
78 }
79
80 void clFileSystemWatcher::Clear()
81 {
82 #if CL_FSW_USE_TIMER
83 Stop();
84 m_files.clear();
85 #else
86 m_watcher.RemoveAll();
87 #endif
88 }
89
90 #if CL_FSW_USE_TIMER
91 void clFileSystemWatcher::OnTimer(wxTimerEvent& event)
92 {
93 std::set<wxString> nonExistingFiles;
94 std::for_each(m_files.begin(), m_files.end(), [&](const std::pair<wxString, clFileSystemWatcher::File>& p) {
95 const File& f = p.second;
96 const wxFileName& fn = f.filename;
97 if(!fn.Exists()) {
98
99 // fire file not found event
100 if(GetOwner()) {
101 clFileSystemEvent evt(wxEVT_FILE_NOT_FOUND);
102 evt.SetPath(fn.GetFullPath());
103 GetOwner()->AddPendingEvent(evt);
104 }
105
106 // add the missing file to a set
107 nonExistingFiles.insert(fn.GetFullPath());
108 } else {
109
110 #ifdef __WXMSW__
111 size_t prev_value = f.file_size;
112 size_t curr_value = FileUtils::GetFileSize(fn);
113 #else
114 time_t prev_value = f.lastModified;
115 time_t curr_value = FileUtils::GetFileModificationTime(fn);
116 #endif
117
118 if(prev_value != curr_value) {
119 // Fire a modified event
120 if(GetOwner()) {
121 clFileSystemEvent evt(wxEVT_FILE_MODIFIED);
122 evt.SetPath(fn.GetFullPath());
123 GetOwner()->AddPendingEvent(evt);
124 }
125 }
126 #ifdef __WXMSW__
127 File updatdFile = f;
128 updatdFile.file_size = curr_value;
129 #else
130 // Always update the last modified timestamp
131 File updatdFile = f;
132 updatdFile.lastModified = curr_value;
133 #endif
134 m_files[fn.GetFullPath()] = updatdFile;
135 }
136 });
137
138 // Remove the non existing files
139 std::for_each(nonExistingFiles.begin(), nonExistingFiles.end(), [&](const wxString& fn) { m_files.erase(fn); });
140
141 if(m_timer) {
142 m_timer->Start(FILE_CHECK_INTERVAL, true);
143 }
144 }
145 #endif
146
147 #if !CL_FSW_USE_TIMER
148 void clFileSystemWatcher::OnFileModified(wxFileSystemWatcherEvent& event)
149 {
150 if(event.GetChangeType() == wxFSW_EVENT_MODIFY) {
151 const wxFileName& modpath = event.GetPath();
152 if(modpath == m_watchedFile) {
153 if(GetOwner()) {
154 clFileSystemEvent evt(wxEVT_FILE_MODIFIED);
155 evt.SetPath(modpath.GetFullPath());
156 GetOwner()->AddPendingEvent(evt);
157 }
158 }
159 }
160 }
161 #endif
162
163 void clFileSystemWatcher::RemoveFile(const wxFileName& filename)
164 {
165 #if CL_FSW_USE_TIMER
166 if(m_files.count(filename.GetFullPath())) {
167 m_files.erase(filename.GetFullPath());
168 }
169 #endif
170 }
171
172 bool clFileSystemWatcher::IsRunning() const
173 {
174 #if CL_FSW_USE_TIMER
175 return m_timer;
176 #else
177 return m_watcher.GetWatchedPathsCount();
178 #endif
179 }