1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * @file FileFilterHelper.h
5 * @brief Declaration file for FileFilterHelper
11 #include "UnicodeString.h"
19 * @brief File extension of file filter files.
21 const TCHAR FileFilterExt[] = _T(".flt");
24 * @brief Helper structure for UI and management of filters.
26 * This structure is mostly used as UI aid and to manage filters from UI.
27 * fileinfo contains time of filter file's last modification time. By
28 * comparing fileinfo to filter file in disk we can determine if file
29 * is changed since we last time loaded it.
33 String name; /**< Name of filter */
34 String description; /**< Description of filter (shown in UI) */
35 String fullpath; /**< Full path to filter file */
36 DirItem fileinfo; /**< For tracking if file has been modified */
39 /// Interface for testing files & directories for exclusion, as diff traverses file tree
43 virtual bool includeFile(const String& szFileName) const = 0;
44 virtual bool includeDir(const String& szDirName) const = 0;
45 bool includeFile(const String& szFileName1, const String& szFileName2) const
47 if (!szFileName1.empty())
48 return includeFile(szFileName1);
49 else if (!szFileName2.empty())
50 return includeFile(szFileName2);
54 bool includeFile(const String& szFileName1, const String& szFileName2, const String& szFileName3) const
56 if (!szFileName1.empty())
57 return includeFile(szFileName1);
58 else if (!szFileName2.empty())
59 return includeFile(szFileName2);
60 else if (!szFileName3.empty())
61 return includeFile(szFileName3);
65 bool includeDir(const String& szDirName1, const String& szDirName2) const
67 if (!szDirName1.empty())
68 return includeDir(szDirName1);
69 else if (!szDirName2.empty())
70 return includeDir(szDirName2);
74 bool includeDir(const String& szDirName1, const String& szDirName2, const String& szDirName3) const
76 if (!szDirName1.empty())
77 return includeDir(szDirName1);
78 else if (!szDirName2.empty())
79 return includeDir(szDirName2);
80 else if (!szDirName3.empty())
81 return includeDir(szDirName3);
88 * @brief Helper class for using filefilters.
90 * A FileFilterHelper object is the owner of any active mask, and of the file filter manager
91 * This is kind of a File Filter SuperManager, taking care of both inline filters from strings
92 * and loaded file filters (the latter handled by its internal file filter manager)
94 * This class is mainly for handling two ways to filter files in WinMerge:
95 * - File masks: *.ext lists (*.cpp *.h etc)
96 * - File filters: regular expression rules in separate files
98 * There can be only one filter or file mask active at a time. This class
99 * keeps track of selected filtering method and provides simple functions for
100 * clients for querying if file is included to compare. Clients don't need
101 * to care about compare methods etc details.
103 class FileFilterHelper : public IDiffFilter
109 String GetGlobalFilterPathWithCreate() const;
110 String GetUserFilterPathWithCreate() const;
112 FileFilterMgr * GetManager() const;
113 void SetFileFilterPath(const String& szFileFilterPath);
114 std::vector<FileFilterInfo> GetFileFilters(String & selected) const;
115 String GetFileFilterName(const String& filterPath) const;
116 String GetFileFilterPath(const String& filterName) const;
117 void SetUserFilterPath(const String & filterPath);
119 void ReloadUpdatedFilters();
120 void LoadAllFileFilters();
122 void LoadFileFilterDirPattern(const String& dir, const String& szPattern);
124 void UseMask(bool bUseMask);
125 void SetMask(const String& strMask);
127 bool IsUsingMask() const;
128 String GetFilterNameOrMask() const;
129 bool SetFilter(const String &filter);
131 bool includeFile(const String& szFileName) const override;
132 bool includeDir(const String& szDirName) const override;
135 String ParseExtensions(const String &extensions) const;
138 std::unique_ptr<FilterList> m_pMaskFilter; /*< Filter for filemasks (*.cpp) */
139 FileFilter * m_currentFilter; /*< Currently selected filefilter */
140 std::unique_ptr<FileFilterMgr> m_fileFilterMgr; /*< Associated FileFilterMgr */
141 String m_sFileFilterPath; /*< Path to current filter */
142 String m_sMask; /*< File mask (if defined) "*.cpp *.h" etc */
143 bool m_bUseMask; /*< If `true` file mask is used, filter otherwise */
144 String m_sGlobalFilterPath; /*< Path for shared filters */
145 String m_sUserSelFilterPath; /*< Path for user's private filters */
149 * @brief Return filtermanager used.
151 inline FileFilterMgr * FileFilterHelper::GetManager() const
153 return m_fileFilterMgr.get();
157 * @brief Returns true if active filter is a mask.
159 inline bool FileFilterHelper::IsUsingMask() const