Fcitx
addonmanager.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_ADDONMANAGER_H_
8 #define _FCITX_ADDONMANAGER_H_
9 
10 #include <memory>
11 #include <string>
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <vector>
15 #include <fcitx-utils/macros.h>
16 #include <fcitx-utils/semver.h>
17 #include <fcitx/addoninfo.h>
18 #include <fcitx/addoninstance.h>
19 #include <fcitx/addonloader.h>
20 #include <fcitx/fcitxcore_export.h>
21 
22 /// \addtogroup FcitxCore
23 /// \{
24 /// \file
25 /// \brief Addon Manager class
26 
27 namespace fcitx {
28 
29 class Instance;
30 class EventLoop;
31 class AddonManagerPrivate;
32 class FCITXCORE_EXPORT AddonManager {
33  friend class Instance;
34 
35 public:
36  /// Construct an addon manager.
37  AddonManager();
38 
39  /**
40  * Create addon manager with given addon config dir.
41  *
42  * By default, addonConfigDir is set to "addon".
43  * It can be a relative path to PkgData, or an absolute path.
44  * This function is only used by test.
45  *
46  * @param addonConfigDir directory name.
47  *
48  * @see StandardPath
49  */
50  AddonManager(const std::string &addonConfigDir);
51 
52  /**
53  * Destruct and unload all addons.
54  *
55  */
56  virtual ~AddonManager();
57 
58  /**
59  * Register addon loader, including static and shared library loader.
60  *
61  * This function usually need to be called before any other function call to
62  * addon manager.
63  *
64  * @param registry static addon registry that can be used to set a list of
65  * built-in addons.
66  */
67  void registerDefaultLoader(StaticAddonRegistry *registry);
68 
69  /**
70  * Register new addon loader.
71  *
72  * @param loader addon loader instance.
73  */
74  void registerLoader(std::unique_ptr<AddonLoader> loader);
75 
76  /**
77  * Unregister addon loader.
78  *
79  * @param name name of addon type.
80  */
81  void unregisterLoader(const std::string &name);
82 
83  /**
84  * Load addon based on given parameter.
85  *
86  * By default, addon is enable or disabled by config file, but
87  * enabled or disabled may be used to override it.
88  *
89  * Usually this function should only be called once.
90  * You can pass --enable=... --disable= in fcitx's flag to set it.
91  * "enabled" will override "disabled" if they have same addon name in it.
92  *
93  * A special name "all" can be used to enable or disable all addons.
94  *
95  * @param enabled set of additionally enabled addons.
96  * @param disabled set of disabled addons
97  */
98  void load(const std::unordered_set<std::string> &enabled = {},
99  const std::unordered_set<std::string> &disabled = {});
100 
101  /**
102  * Destruct all addon, all information is cleared to the initial state.
103  *
104  * But depending on the addon it loads, it may have some leftover data in
105  * the memory.
106  */
107  void unload();
108 
109  /**
110  * Save all addon configuration.
111  *
112  * @see fcitx::AddonInstance::save
113  */
114  void saveAll();
115 
116  /**
117  * Get the loaded addon instance.
118  *
119  * @param name name of addon.
120  * @param load to force load the addon if possible.
121  * @return instance of addon.
122  */
123  AddonInstance *addon(const std::string &name, bool load = false);
124 
125  /**
126  * Get the currently loaded addon instance.
127  *
128  * This is same as AddonManager::addon(name, false), but allow to be used
129  * with a constant AddonManager.
130  *
131  * @param name of addon.
132  * @return instance of addon, null if not found.
133  * @since 5.1.6
134  */
135  AddonInstance *lookupAddon(const std::string &name) const;
136 
137  /**
138  * Return the loaded addon name in the order of they were loaded.
139  *
140  * @return the name of loaded addons.
141  * @since 5.1.6
142  */
143  const std::vector<std::string> &loadedAddonNames() const;
144 
145  /**
146  * Get addon information for given addon.
147  *
148  * @param name name of addon.
149  * @return const fcitx::AddonInfo*
150  */
151  const AddonInfo *addonInfo(const std::string &name) const;
152  std::unordered_set<std::string> addonNames(AddonCategory category);
153 
154  /**
155  * Return the fcitx instance when it is created by Fcitx.
156  *
157  * @return fcitx instance.
158  */
159  Instance *instance();
160  /**
161  * Return the associated event loop.
162  *
163  * If AddonManager is created by Instance, it will return the event loop of
164  * associated instance.
165  *
166  * @return event loop.
167  */
168  EventLoop *eventLoop();
169 
170  /**
171  * Set event loop.
172  *
173  * It should be only used with stand alone AddonManager.
174  * E.g. write test or for some special purpose.
175  *
176  * @param eventLoop event loop.
177  * @see fcitx::AddonManager::eventLoop
178  */
179  void setEventLoop(EventLoop *eventLoop);
180 
181  /**
182  * Return the version number of Fcitx5Core library.
183  */
184  const SemanticVersion &version() const;
185 
186  /**
187  * Check directory for quick hint for whether update is required.
188  *
189  * @since 5.0.6
190  */
191  bool checkUpdate() const;
192 
193  /**
194  * Set addon parameters that may be used during addon construction.
195  *
196  * This is usually passed from command line flags --option or -o.
197  *
198  * @param options map from addon name to a set of string values
199  * @since 5.1.7
200  */
201  void setAddonOptions(
202  std::unordered_map<std::string, std::vector<std::string>> options);
203 
204  /**
205  * Query addon options that set with setAddonOptions for given addon.
206  *
207  * @param name addon name
208  * @return Options for given addon
209  * @since 5.1.7
210  */
211  std::vector<std::string> addonOptions(const std::string &name);
212 
213 private:
214  void setInstance(Instance *instance);
215  std::unique_ptr<AddonManagerPrivate> d_ptr;
216  FCITX_DECLARE_PRIVATE(AddonManager);
217 };
218 } // namespace fcitx
219 
220 #endif // _FCITX_ADDONMANAGER_H_
An instance represents a standalone Fcitx instance.
Definition: instance.h:88
Definition: action.cpp:17
Provide a Semantic version 2.0 implementation.
Definition: semver.h:46
Base class for any addon in fcitx.
Definition: addoninstance.h:74
Addon For fcitx.