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