7 #ifndef _FCITX_UTILS_STANDARDPATH_H_ 8 #define _FCITX_UTILS_STANDARDPATH_H_ 27 #include <unordered_map> 29 #include <fcitx-utils/fcitxutils_export.h> 31 #include <fcitx-utils/macros.h> 40 template <
typename... Types>
46 bool operator()(
const std::string & ,
47 const std::string & ,
bool ) {
52 template <
typename First,
typename... Rest>
57 Chainer(First first, Rest... rest)
58 : super_class(std::move(rest)...), filter(std::move(first)) {}
60 bool operator()(
const std::string &path,
const std::string &dir,
62 if (!filter(path, dir, user)) {
65 return super_class::operator()(path, dir, user);
77 bool operator()(
const std::string &path,
const std::string &dir,
79 return !filter(path, dir, isUser);
92 struct FCITXUTILS_DEPRECATED_EXPORT
User {
93 bool operator()(
const std::string & ,
94 const std::string & ,
bool isUser) {
100 struct FCITXUTILS_DEPRECATED_EXPORT
Prefix {
101 Prefix(
const std::string &prefix_) : prefix(prefix_) {}
103 bool operator()(
const std::string &path,
const std::string & ,
105 return path.starts_with(prefix);
112 struct FCITXUTILS_DEPRECATED_EXPORT
Suffix {
113 Suffix(
const std::string &suffix_) : suffix(suffix_) {}
115 bool operator()(
const std::string &path,
const std::string & ,
117 return path.ends_with(suffix);
129 const std::string &tempPath = {})
130 : fd_(
UnixFD::own(fd)), path_(realFile), tempPath_(tempPath) {}
134 int fd()
const {
return fd_.fd(); }
135 bool isValid()
const {
return fd_.isValid(); }
137 const std::string &path()
const {
return path_; }
138 const std::string &tempPath()
const {
return tempPath_; }
147 std::string tempPath_;
161 int fd()
const {
return fd_.fd(); }
162 bool isValid()
const {
return fd_.isValid(); }
164 const std::string &path()
const {
return path_; }
175 using StandardPathFileMap = std::map<std::string, StandardPathFile>;
176 using StandardPathFilesMap =
177 std::map<std::string, std::vector<StandardPathFile>>;
210 const std::string &packageName,
211 const std::unordered_map<std::string, std::string> &builtInPath,
212 bool skipBuiltInPath,
bool skipUserPath);
214 explicit StandardPath(
bool skipFcitxPath,
bool skipUserPath);
229 static const char *fcitxPath(
const char *path);
233 static std::string fcitxPath(
const char *path,
const char *subPath);
238 void scanDirectories(
Type type,
239 const std::function<
bool(
const std::string &path,
240 bool user)> &scanner)
const;
246 void scanDirectories(
247 const std::string &userDir,
const std::vector<std::string> &directories,
248 const std::function<
bool(
const std::string &path,
bool user)> &scanner)
253 void scanFiles(
Type type,
const std::string &path,
254 const std::function<
bool(
const std::string &path,
255 const std::string &dir,
bool user)>
259 std::string userDirectory(
Type type)
const;
262 std::vector<std::string> directories(
Type type)
const;
265 std::string locate(
Type type,
const std::string &path)
const;
268 std::vector<std::string> locateAll(
Type type,
269 const std::string &path)
const;
292 const std::string &pathOrig)
const;
304 bool safeSave(
Type type,
const std::string &pathOrig,
305 const std::function<
bool(
int)> &callback)
const;
315 std::map<std::string, std::string>
316 locateWithFilter(
Type type,
const std::string &path,
317 std::function<
bool(
const std::string &path,
318 const std::string &dir,
bool user)>
330 template <
typename Arg1,
typename... Args>
331 std::map<std::string, std::string>
332 locate(
Type type,
const std::string &path, Arg1 arg1, Args... args)
const {
333 return locateWithFilter(type, path,
335 std::move(arg1), std::move(args)...));
339 std::vector<StandardPathFile> openAll(
Type type,
const std::string &path,
344 multiOpenFilter(
Type type,
const std::string &path,
int flags,
345 std::function<
bool(
const std::string &path,
346 const std::string &dir,
bool user)>
352 template <
typename... Args>
353 StandardPathFileMap
multiOpen(
Type type,
const std::string &path,
int flags,
354 Args... args)
const {
355 return multiOpenFilter(type, path, flags,
361 multiOpenAllFilter(
Type type,
const std::string &path,
int flags,
362 std::function<
bool(
const std::string &path,
363 const std::string &dir,
bool user)>
369 template <
typename... Args>
371 int flags, Args... args)
const {
372 return multiOpenAllFilter(type, path, flags,
376 int64_t timestamp(
Type type,
const std::string &path)
const;
386 static std::string findExecutable(
const std::string &name);
394 static bool hasExecutable(
const std::string &name);
403 void syncUmask()
const;
414 bool skipBuiltInPath()
const;
421 bool skipUserPath()
const;
424 std::unique_ptr<StandardPathPrivate> d_ptr;
430 builder <<
"StandardPathFile(fd=" << file.fd() <<
",path=" << file.path()
443 return StandardPathsType::Config;
445 return StandardPathsType::PkgConfig;
447 return StandardPathsType::Data;
449 return StandardPathsType::Cache;
451 return StandardPathsType::Runtime;
453 return StandardPathsType::Addon;
455 return StandardPathsType::PkgData;
457 return StandardPathsType::Config;
464 #endif // _FCITX_UTILS_STANDARDPATH_H_
Class wrap around the unix fd.
StandardPathsType
Enum for location type.
Filter class that filters based on user file.
Utility class that wraps around UnixFD.
Filter class to chain sub filters together.
Filter class that revert the sub filter result.
Filter class that filters file based on prefix.
Utility class to handle unix file descriptor.
New Utility classes to handle application specific path.
Filter class that filters file based on suffix.
static UnixFD own(int fd)
Create a UnixFD by owning the fd.
std::map< std::string, std::string > locate(Type type, const std::string &path, Arg1 arg1, Args... args) const
Locate all files match the filter under first [directory]/[path].
Utility class to open, locate, list files based on XDG standard.
StandardPathFilesMap multiOpenAll(Type type, const std::string &path, int flags, Args... args) const
Open all files match the filter under all [directory]/[path].
Type
Enum for location type.
StandardPathFileMap multiOpen(Type type, const std::string &path, int flags, Args... args) const
Open all files match the filter under first [directory]/[path].
addon shared library dir.
File descriptor wrapper that handles file descriptor and rename automatically.