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