Pick Ref: Browse Ref buttons added to dialogs derived from ChooseVersion.
[TortoiseGit.git] / src / Utils / PathWatcher.h
blob533718622b9126b028c5d43d07e02a0d8b5e2ee4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2007-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
20 #include "TGitPath.h"
22 #define READ_DIR_CHANGE_BUFFER_SIZE 4096
24 typedef CComCritSecLock<CComCriticalSection> AutoLocker;
26 /**
27 * \ingroup Utils
28 * Watches the file system for changes.
30 * When a CPathWatcher object is created, a new thread is started which
31 * waits for file system change notifications.
32 * To add folders to the list of watched folders, call \c AddPath().
34 * The folders are watched recursively. To prevent having too many folders watched,
35 * children of already watched folders are automatically removed from watching.
37 class CPathWatcher
39 public:
40 CPathWatcher(void);
41 ~CPathWatcher(void);
43 /**
44 * Adds a new path to be watched. The path \b must point to a directory.
45 * If the path is already watched because a parent of that path is already
46 * watched recursively, then the new path is just ignored and the method
47 * returns false.
49 bool AddPath(const CTGitPath& path);
50 /**
51 * Removes a path and all its children from the watched list.
53 bool RemovePathAndChildren(const CTGitPath& path);
55 /**
56 * Returns the number of recursively watched paths.
58 int GetNumberOfWatchedPaths() {return watchedPaths.GetCount();}
60 /**
61 * Stops the watching thread.
63 void Stop();
65 /**
66 * Returns the number of changed paths up to now.
68 int GetNumberOfChangedPaths() {return m_changedPaths.GetCount();}
70 /**
71 * Returns the list of paths which maybe got changed, i.e., for which
72 * a change notification was received.
74 CTGitPathList GetChangedPaths() {return m_changedPaths;}
76 /**
77 * Clears the list of changed paths
79 void ClearChangedPaths() {AutoLocker lock(m_critSec); m_changedPaths.Clear();}
81 private:
82 static unsigned int __stdcall ThreadEntry(void* pContext);
83 void WorkerThread();
85 void ClearInfoMap();
87 private:
88 CComAutoCriticalSection m_critSec;
89 HANDLE m_hThread;
90 HANDLE m_hCompPort;
91 volatile LONG m_bRunning;
93 CTGitPathList watchedPaths; ///< list of watched paths.
94 CTGitPathList m_changedPaths; ///< list of paths which got changed
96 /**
97 * Helper class: provides information about watched directories.
99 class CDirWatchInfo
101 private:
102 CDirWatchInfo(); // private & not implemented
103 CDirWatchInfo & operator=(const CDirWatchInfo & rhs);//so that they're aren't accidentally used. -- you'll get a linker error
104 public:
105 CDirWatchInfo(HANDLE hDir, const CTGitPath& DirectoryName);
106 ~CDirWatchInfo();
108 protected:
109 public:
110 bool CloseDirectoryHandle();
112 HANDLE m_hDir; ///< handle to the directory that we're watching
113 CTGitPath m_DirName; ///< the directory that we're watching
114 CHAR m_Buffer[READ_DIR_CHANGE_BUFFER_SIZE]; ///< buffer for ReadDirectoryChangesW
115 DWORD m_dwBufLength; ///< length or returned data from ReadDirectoryChangesW -- ignored?...
116 OVERLAPPED m_Overlapped;
117 CString m_DirPath; ///< the directory name we're watching with a backslash at the end
118 //HDEVNOTIFY m_hDevNotify; ///< Notification handle
121 std::map<HANDLE, CDirWatchInfo *> watchInfoMap;
123 HDEVNOTIFY m_hdev;