Fcitx
addoninfo.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 "addoninfo.h"
9 #include "fcitx-config/configuration.h"
10 namespace fcitx {
11 
12 FCITX_CONFIGURATION(
13  AddonConfigBase, Option<I18NString> name{this, "Name", "Addon Name"};
14  Option<I18NString> comment{this, "Comment", "Comment"};
15  Option<SemanticVersion> version{this, "Version", "Addon Version"};
16  Option<std::string> type{this, "Type", "Addon Type"};
17  Option<std::string> library{this, "Library", "Addon Library"};
18  Option<bool> configurable{this, "Configurable", "Configurable", false};
19  Option<bool> enabled{this, "Enabled", "Enabled", true};
20  Option<AddonCategory> category{this, "Category", "Category"};
21  Option<std::vector<std::string>> dependencies{this, "Dependencies",
22  "Dependencies"};
23  Option<std::vector<std::string>> optionalDependencies{
24  this, "OptionalDependencies", "Optional Dependencies"};
25  Option<bool> onDemand{this, "OnDemand", "Load only on request", false};
26  Option<int> uiPriority{this, "UIPriority", "User interface priority", 0};
27  Option<UIType> uiType{this, "UIType", "User interface type",
28  UIType::PhyscialKeyboard};)
29 
30 FCITX_CONFIGURATION(AddonConfig,
31  Option<AddonConfigBase> addon{this, "Addon", "Addon"};)
32 
33 namespace {
34 
35 void parseDependencies(const std::vector<std::string> &data,
36  std::vector<std::string> &dependencies,
37  std::vector<std::tuple<std::string, SemanticVersion>>
38  &dependenciesWithVersion) {
39  dependencies.clear();
40  dependenciesWithVersion.clear();
41  for (const auto &item : data) {
42  auto tokens = stringutils::split(item, ":");
43  if (tokens.size() == 1) {
44  dependencies.push_back(tokens[0]);
45  dependenciesWithVersion.emplace_back(tokens[0], SemanticVersion{});
46  } else if (tokens.size() == 2) {
47  auto version = SemanticVersion::parse(tokens[1]);
48  if (version) {
49  dependencies.push_back(tokens[0]);
50  dependenciesWithVersion.emplace_back(tokens[0],
51  version.value());
52  }
53  }
54  }
55 }
56 } // namespace
57 
58 class AddonInfoPrivate : public AddonConfig {
59 public:
60  AddonInfoPrivate(const std::string &name) : uniqueName_(name) {}
61 
62  bool valid_ = false;
63  std::string uniqueName_;
64  OverrideEnabled overrideEnabled_ = OverrideEnabled::NotSet;
65  std::vector<std::string> dependencies_;
66  std::vector<std::string> optionalDependencies_;
67  std::vector<std::tuple<std::string, SemanticVersion>>
68  dependenciesWithVersion_;
69  std::vector<std::tuple<std::string, SemanticVersion>>
70  optionalDependenciesWithVersion_;
71 };
72 
73 AddonInfo::AddonInfo(const std::string &name)
74  : d_ptr(std::make_unique<AddonInfoPrivate>(name)) {}
75 
76 AddonInfo::~AddonInfo() {}
77 
78 bool AddonInfo::isValid() const {
79  FCITX_D();
80  return d->valid_;
81 }
82 
83 const std::string &AddonInfo::uniqueName() const {
84  FCITX_D();
85  return d->uniqueName_;
86 }
87 
88 const I18NString &AddonInfo::name() const {
89  FCITX_D();
90  return d->addon->name.value();
91 }
92 
93 const I18NString &AddonInfo::comment() const {
94  FCITX_D();
95  return d->addon->comment.value();
96 }
97 
98 const std::string &AddonInfo::type() const {
99  FCITX_D();
100  return d->addon->type.value();
101 }
102 
103 AddonCategory AddonInfo::category() const {
104  FCITX_D();
105  return d->addon->category.value();
106 }
107 
108 const std::string &AddonInfo::library() const {
109  FCITX_D();
110  return d->addon->library.value();
111 }
112 
113 const std::vector<std::string> &AddonInfo::dependencies() const {
114  FCITX_D();
115  return d->dependencies_;
116 }
117 
118 const std::vector<std::string> &AddonInfo::optionalDependencies() const {
119  FCITX_D();
120  return d->optionalDependencies_;
121 }
122 
123 const std::vector<std::tuple<std::string, SemanticVersion>> &
124 AddonInfo::dependenciesWithVersion() const {
125  FCITX_D();
126  return d->dependenciesWithVersion_;
127 }
128 
129 const std::vector<std::tuple<std::string, SemanticVersion>> &
130 AddonInfo::optionalDependenciesWithVersion() const {
131  FCITX_D();
132  return d->optionalDependenciesWithVersion_;
133 }
134 
135 bool AddonInfo::onDemand() const {
136  FCITX_D();
137  return d->addon->onDemand.value();
138 }
139 
140 int AddonInfo::uiPriority() const {
141  FCITX_D();
142  return d->addon->uiPriority.value();
143 }
144 
145 UIType AddonInfo::uiType() const {
146  FCITX_D();
147  return d->addon->uiType.value();
148 }
149 
150 void AddonInfo::load(const RawConfig &config) {
151  FCITX_D();
152  d->load(config);
153 
154  parseDependencies(*d->addon->dependencies, d->dependencies_,
155  d->dependenciesWithVersion_);
156  parseDependencies(*d->addon->optionalDependencies, d->optionalDependencies_,
157  d->optionalDependenciesWithVersion_);
158 
159  // Validate more information
160  d->valid_ = !(d->uniqueName_.empty()) &&
161  !(d->addon->type.value().empty()) &&
162  !(d->addon->library.value().empty());
163 }
164 
165 bool AddonInfo::isEnabled() const {
166  FCITX_D();
167  if (d->overrideEnabled_ == OverrideEnabled::NotSet) {
168  return *d->addon->enabled;
169  }
170  return d->overrideEnabled_ == OverrideEnabled::Enabled;
171 }
172 
173 bool AddonInfo::isDefaultEnabled() const {
174  FCITX_D();
175  return *d->addon->enabled;
176 }
177 
178 bool AddonInfo::isConfigurable() const {
179  FCITX_D();
180  return *d->addon->configurable;
181 }
182 
183 const SemanticVersion &AddonInfo::version() const {
184  FCITX_D();
185  return *d->addon->version;
186 }
187 
188 void AddonInfo::setOverrideEnabled(OverrideEnabled overrideEnabled) {
189  FCITX_D();
190  d->overrideEnabled_ = overrideEnabled;
191 }
192 } // namespace fcitx
Definition: action.cpp:12
Provide a Semantic version 2.0 implementation.
Definition: semver.h:45
std::vector< std::string > split(std::string_view str, std::string_view delim, SplitBehavior behavior)
Split the string by delim.