OSDN Git Service

MergeLang.vcxproj*: Add *.po files for ShellExtension
[winmerge-jp/winmerge-jp.git] / Src / FileFilterHelper.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** 
3  * @file  FileFilterHelper.h
4  *
5  * @brief Declaration file for FileFilterHelper
6  */
7 #pragma once
8
9 #include <vector>
10 #include <memory>
11 #include "UnicodeString.h"
12 #include "DirItem.h"
13
14 class FileFilterMgr;
15 class FilterList;
16 struct FileFilter;
17
18 /**
19  * @brief File extension of file filter files.
20  */
21 const TCHAR FileFilterExt[] = _T(".flt");
22
23 /**
24  * @brief Helper structure for UI and management of filters.
25  *
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.
30  */
31 struct FileFilterInfo
32 {
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 */
37 };
38
39 /// Interface for testing files & directories for exclusion, as diff traverses file tree
40 class IDiffFilter
41 {
42 public:
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
46         {
47                 if (!szFileName1.empty())
48                         return includeFile(szFileName1);
49                 else if (!szFileName2.empty())
50                         return includeFile(szFileName2);
51                 else
52                         return false;
53         }
54         bool includeFile(const String& szFileName1, const String& szFileName2, const String& szFileName3) const
55         {
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);
62                 else
63                         return false;
64         }
65         bool includeDir(const String& szDirName1, const String& szDirName2) const
66         {
67                 if (!szDirName1.empty())
68                         return includeDir(szDirName1);
69                 else if (!szDirName2.empty())
70                         return includeDir(szDirName2);
71                 else
72                         return false;
73         }
74         bool includeDir(const String& szDirName1, const String& szDirName2, const String& szDirName3) const
75         {
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);
82                 else
83                         return false;
84         }
85 };
86
87 /**
88  * @brief Helper class for using filefilters.
89  *
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)
93  *
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
97  *
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.
102  */
103 class FileFilterHelper : public IDiffFilter
104 {
105 public:
106         FileFilterHelper();
107         ~FileFilterHelper();
108
109         String GetGlobalFilterPathWithCreate() const;
110         String GetUserFilterPathWithCreate() const;
111
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);
118
119         void ReloadUpdatedFilters();
120         void LoadAllFileFilters();
121
122         void LoadFileFilterDirPattern(const String& dir, const String& szPattern);
123
124         void UseMask(bool bUseMask);
125         void SetMask(const String& strMask);
126
127         bool IsUsingMask() const;
128         String GetFilterNameOrMask() const;
129         bool SetFilter(const String &filter);
130
131         bool includeFile(const String& szFileName) const override;
132         bool includeDir(const String& szDirName) const override;
133
134 protected:
135         String ParseExtensions(const String &extensions) const;
136
137 private:
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 */
146 };
147
148 /**
149  * @brief Return filtermanager used.
150  */
151 inline FileFilterMgr * FileFilterHelper::GetManager() const
152 {
153         return m_fileFilterMgr.get();
154 }
155
156 /**
157  * @brief Returns true if active filter is a mask.
158  */
159 inline bool FileFilterHelper::IsUsingMask() const
160 {
161         return m_bUseMask;
162 }
163