Get rid of magic numbers
[TortoiseGit.git] / src / TortoiseShell / ShellCache.h
blob9bef60c901adf8b3d21377ed4724393b641ff02e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012-2017 - TortoiseGit
4 // Copyright (C) 2003-2011, 2017 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "registry.h"
22 #include "Globals.h"
24 #define ADMINDIRTIMEOUT 10000
25 #define DRIVETYPETIMEOUT 300000 // 5 min
27 #define DEFAULTMENUTOPENTRIES MENUSYNC|MENUCREATEREPOS|MENUCLONE|MENUCOMMIT
28 #define DEFAULTMENUEXTENTRIES MENUSVNIGNORE|MENUSTASHAPPLY|MENUSUBSYNC
30 typedef CComCritSecLock<CComCriticalSection> Locker;
32 typedef enum tristate_t
34 /** state known to be false (the constant does not evaulate to false) */
35 tristate_false = 2,
36 /** state known to be true */
37 tristate_true,
38 /** state could be true or false */
39 tristate_unknown
40 } tristate_t;
42 /**
43 * \ingroup TortoiseShell
44 * Helper class which caches access to the registry. Also provides helper methods
45 * for checks against the settings stored in the registry.
47 class ShellCache
49 public:
50 enum CacheType
52 none,
53 exe,
54 dll,
55 dllFull,// same as dll except it uses commandline git tool with all status modes supported
58 ShellCache();
59 ~ShellCache();
61 bool RefreshIfNeeded();
63 CacheType GetCacheType();
64 DWORD BlockStatus();
65 unsigned __int64 GetMenuLayout();
66 unsigned __int64 GetMenuExt();
67 unsigned __int64 GetMenuMask();
69 bool IsProcessElevated();
70 BOOL IsOnlyNonElevated();
72 BOOL IsRecursive();
73 BOOL IsFolderOverlay();
74 BOOL IsSimpleContext();
75 BOOL HasShellMenuAccelerators();
76 BOOL IsUnversionedAsModified();
77 BOOL IsRecurseSubmodules();
78 BOOL ShowUnversionedOverlay();
79 BOOL ShowIgnoredOverlay();
80 BOOL IsGetLockTop();
81 BOOL ShowExcludedAsNormal();
82 BOOL HideMenusForUnversionedItems();
84 BOOL IsRemote();
85 BOOL IsFixed();
86 BOOL IsCDRom();
87 BOOL IsRemovable();
88 BOOL IsRAM();
89 BOOL IsUnknown();
91 BOOL IsContextPathAllowed(LPCTSTR path);
92 BOOL IsPathAllowed(LPCTSTR path);
93 DWORD GetLangID();
94 BOOL HasGITAdminDir(LPCTSTR path, BOOL bIsDir, CString* ProjectTopDir = nullptr);
96 private:
97 void ExcludeContextValid();
99 class CPathFilter
101 public:
102 /// node in the lookup tree
103 struct SEntry
105 tstring path;
107 /// default (path spec did not end a '?').
108 /// if this is not set, the default for all
109 /// sub-paths is !included.
110 /// This is a temporary setting an be invalid
111 /// after @ref PostProcessData
112 bool recursive;
114 /// this is an "include" specification
115 tristate_t included;
117 /// if @ref recursive is not set, this is
118 /// the parent path status being passed down
119 /// combined with the information of other
120 /// entries for the same @ref path.
121 tristate_t subPathIncluded;
123 /// do entries for sub-paths exist?
124 bool hasSubFolderEntries;
126 /// STL support
127 /// For efficient folding, it is imperative that
128 /// "recursive" entries are first
129 bool operator<(const SEntry& rhs) const
131 int diff = _wcsicmp(path.c_str(), rhs.path.c_str());
132 return (diff < 0) || ((diff == 0) && recursive && !rhs.recursive);
135 friend bool operator<(const SEntry& rhs, const std::pair<LPCTSTR, size_t>& lhs);
136 friend bool operator<(const std::pair<LPCTSTR, size_t>& lhs, const SEntry& rhs);
139 private:
140 /// lookup by path (all entries sorted by path)
141 typedef std::vector<SEntry> TData;
142 TData data;
144 /// registry keys plus cached last content
145 CRegStdString excludelist;
146 tstring excludeliststr;
148 CRegStdString includelist;
149 tstring includeliststr;
151 /// construct \ref data content
152 void AddEntry(const tstring& s, bool include);
153 void AddEntries(const tstring& s, bool include);
155 /// for all paths, have at least one entry in data
156 void PostProcessData();
158 /// lookup. default result is "unknown".
159 /// We must look for *every* parent path because of situations like:
160 /// excluded: C:, C:\some\deep\path
161 /// include: C:\some
162 /// lookup for C:\some\deeper\path
163 tristate_t IsPathAllowed(LPCTSTR path, TData::const_iterator begin, TData::const_iterator end) const;
165 public:
166 /// construction
167 CPathFilter();
169 /// notify of (potential) registry settings
170 void Refresh();
172 /// data access
173 tristate_t IsPathAllowed(LPCTSTR path) const;
176 friend bool operator< (const CPathFilter::SEntry& rhs, const std::pair<LPCTSTR, size_t>& lhs);
177 friend bool operator< (const std::pair<LPCTSTR, size_t>& lhs, const CPathFilter::SEntry& rhs);
179 struct AdminDir_s
181 BOOL bHasAdminDir;
182 tstring sProjectRoot;
183 ULONGLONG timeout;
186 public:
187 CRegStdDWORD cachetype;
188 CRegStdDWORD blockstatus;
189 CRegStdDWORD langid;
190 CRegStdDWORD onlynonelevated;
191 CRegStdDWORD showrecursive;
192 CRegStdDWORD folderoverlay;
193 CRegStdDWORD getlocktop;
194 CRegStdDWORD driveremote;
195 CRegStdDWORD drivefixed;
196 CRegStdDWORD drivecdrom;
197 CRegStdDWORD driveremove;
198 CRegStdDWORD drivefloppy;
199 CRegStdDWORD driveram;
200 CRegStdDWORD driveunknown;
201 CRegStdDWORD menulayoutlow; /* Fist level mask */
202 CRegStdDWORD menulayouthigh;
203 CRegStdDWORD shellmenuaccelerators;
204 CRegStdDWORD menuextlow; /* ext menu mask */
205 CRegStdDWORD menuexthigh;
206 CRegStdDWORD simplecontext;
207 CRegStdDWORD menumasklow_lm;
208 CRegStdDWORD menumaskhigh_lm;
209 CRegStdDWORD menumasklow_cu;
210 CRegStdDWORD menumaskhigh_cu;
211 CRegStdDWORD unversionedasmodified;
212 CRegStdDWORD recursesubmodules;
213 CRegStdDWORD showunversionedoverlay;
214 CRegStdDWORD showignoredoverlay;
215 CRegStdDWORD excludedasnormal;
216 CRegStdDWORD hidemenusforunversioneditems;
218 CPathFilter pathFilter;
220 ULONGLONG drivetypeticker;
221 ULONGLONG menumaskticker;
222 UINT drivetypecache[27];
223 TCHAR drivetypepathcache[MAX_PATH]; // MAX_PATH ok.
224 TCHAR szDecSep[5];
225 TCHAR szThousandsSep[5];
226 std::map<tstring, AdminDir_s> admindircache;
227 CRegStdString nocontextpaths;
228 tstring excludecontextstr;
229 std::vector<tstring> excontextvector;
230 CComCriticalSection m_critSec;
231 HANDLE m_registryChangeEvent;
232 HKEY m_hNotifyRegKey;
233 bool isElevated;
236 inline bool operator<(const ShellCache::CPathFilter::SEntry& lhs, const std::pair<LPCTSTR, size_t>& rhs)
238 return _wcsnicmp(lhs.path.c_str(), rhs.first, rhs.second) < 0;
241 inline bool operator<(const std::pair<LPCTSTR, size_t>& lhs, const ShellCache::CPathFilter::SEntry& rhs)
243 return _wcsnicmp(lhs.first, rhs.path.c_str(), lhs.second) < 0;