Fcitx
option.cpp
1 /*
2  * SPDX-FileCopyrightText: 2015-2015 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "option.h"
9 #include <memory>
10 #include <regex>
11 #include <stdexcept>
12 #include <string>
13 #include <utility>
14 #include "configuration.h"
15 #include "rawconfig.h"
16 
17 namespace fcitx {
18 
19 OptionBase::OptionBase(Configuration *parent, std::string path,
20  std::string description)
21  : parent_(parent), path_(std::move(path)),
22  description_(std::move(description)) {
23 
24  // Force the rule of "/" not allowed in option, so our life of GUI would be
25  // easier.
26  if (path_.find('/') != std::string::npos) {
27  throw std::invalid_argument(
28  "/ is not allowed in option, option path is " + path_);
29  }
30  parent_->addOption(this);
31 }
32 
33 OptionBase::~OptionBase() {}
34 
35 bool OptionBase::isDefault() const { return false; }
36 
37 const std::string &OptionBase::path() const { return path_; }
38 
39 const std::string &OptionBase::description() const { return description_; }
40 
41 void OptionBase::dumpDescription(RawConfig &config) const {
42  config.setValueByPath("Type", typeString());
43  config.setValueByPath("Description", description_);
44 }
45 
46 OptionBaseV3::~OptionBaseV3() {}
47 
48 ExternalOption::ExternalOption(Configuration *parent, std::string path,
49  std::string description, std::string uri)
50  : OptionBase(parent, std::move(path), std::move(description)),
51  externalUri_(std::move(uri)) {}
52 
53 std::string ExternalOption::typeString() const { return "External"; }
54 
55 void ExternalOption::reset() {}
56 bool ExternalOption::isDefault() const { return false; }
57 
58 void ExternalOption::marshall(RawConfig & /*config*/) const {}
59 bool ExternalOption::unmarshall(const RawConfig & /*config*/,
60  bool /*partial*/) {
61  return true;
62 }
63 std::unique_ptr<Configuration> ExternalOption::subConfigSkeleton() const {
64  return nullptr;
65 }
66 
67 bool ExternalOption::equalTo(const OptionBase & /*other*/) const {
68  return true;
69 }
70 void ExternalOption::copyFrom(const OptionBase & /*other*/) {}
71 
72 bool ExternalOption::skipDescription() const { return false; }
73 bool ExternalOption::skipSave() const { return true; }
74 void ExternalOption::dumpDescription(RawConfig &config) const {
75  OptionBase::dumpDescription(config);
76  config.setValueByPath("External", externalUri_);
77  // This field is required by dbus.
78  config.setValueByPath("DefaultValue", "");
79 }
80 
81 void SubConfigOption::dumpDescription(RawConfig &config) const {
82  ExternalOption::dumpDescription(config);
83  config.setValueByPath("LaunchSubConfig", "True");
84 }
85 
86 bool RegexConstrain::check(const std::string &value) const {
87  try {
88  std::regex re(value);
89  } catch (const std::regex_error &) {
90  return false;
91  }
92  return true;
93 }
94 
95 void RegexConstrain::dumpDescription(RawConfig &config) const {
96  config.setValueByPath("IsRegex", "True");
97 }
98 } // namespace fcitx
Definition: action.cpp:17
Definition: matchrule.h:78