Fcitx
inputmethodmanager.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_INPUTMETHODMANAGER_H_
8 #define _FCITX_INPUTMETHODMANAGER_H_
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 #include <vector>
15 #include <fcitx-utils/macros.h>
16 #include <fcitx/fcitxcore_export.h>
17 #include <fcitx/inputmethodgroup.h>
18 
19 /// \addtogroup FcitxCore
20 /// \{
21 /// \file
22 /// \brief Input Method Manager For fcitx.
23 
24 namespace fcitx {
25 
26 class AddonManager;
27 class InputMethodManagerPrivate;
28 class Instance;
29 class InputMethodEntry;
30 
31 /**
32  * Class to manage all the input method relation information.
33  *
34  * It will list all the available input methods from configuration file and
35  * addon. The configuration file is located under $XDG_DATA/fcitx5/inputmethod.
36  *
37  * Additional runtime input method can be reported by input method addon by
38  * listInputMethods.
39  *
40  * @see InputMethodEngine::listInputMethods
41  */
42 class FCITXCORE_EXPORT InputMethodManager : public ConnectableObject {
43 public:
44  InputMethodManager(AddonManager *addonManager);
45  virtual ~InputMethodManager();
46 
47  /**
48  * Load the input method information from disk.
49  *
50  * If it does not exist, use the callback to create the default setup.
51  */
52  void load(const std::function<void(InputMethodManager &)>
53  &buildDefaultGroupCallback = {});
54 
55  /// Reset all the group information to initial state.
56  void reset(const std::function<void(InputMethodManager &)>
57  &buildDefaultGroupCallback = {});
58 
59  /**
60  * Load new input method configuration file from disk.
61  *
62  * It only load "new" input method configuration, and it would not update
63  * the loaded data. Should only be used after load is called.
64  */
65  void refresh();
66 
67  /**
68  * Save the input method information to disk.
69  *
70  * Commonly, the storage path will be ~/.config/fcitx5/profile.
71  */
72  void save();
73 
74  /// Return all the names of group by order.
75  std::vector<std::string> groups() const;
76 
77  /// Return the number of groups.
78  int groupCount() const;
79 
80  /// Set the name of current group, rest of the group order will be adjusted
81  /// accordingly.
82  void setCurrentGroup(const std::string &group);
83 
84  /// Return the current group.
85  const InputMethodGroup &currentGroup() const;
86 
87  /// Simply enumerate input method groups.
88  void enumerateGroup(bool forward);
89 
90  /**
91  * enumerate group to a certain group.
92  *
93  * For example, current order is A,B,C,D, enumerateGroupTo(C)
94  * will make order to be C,D,A,B.
95  * @arg groupName group name
96  */
97  void enumerateGroupTo(const std::string &groupName);
98 
99  /**
100  * Set default input method for current group.
101  *
102  * @see InputMethodGroup::setDefaultInputMethod
103  */
104  void setDefaultInputMethod(const std::string &name);
105 
106  /// Return the input methdo group of given name.
107  const InputMethodGroup *group(const std::string &name) const;
108 
109  /**
110  * Update the information of an existing group.
111  *
112  * The group info will be revalidated and filtered to the existing input
113  * methods.
114  */
115  void setGroup(InputMethodGroup newGroupInfo);
116 
117  /// Create a new empty group with given name.
118  void addEmptyGroup(const std::string &name);
119 
120  /// Remove an existing group by name.
121  void removeGroup(const std::string &name);
122 
123  /**
124  * Update the initial order of groups.
125  *
126  * This function should be only used in the buildDefaultGroupCallback.
127  * Otherwise the group order can be only modified via setCurrentGroup.
128  *
129  * @param groups the order of groups.
130  * @see InputMethodManager::load
131  */
132  void setGroupOrder(const std::vector<std::string> &groups);
133 
134  /// Return a given input method entry by name.
135  const InputMethodEntry *entry(const std::string &name) const;
136 
137  /**
138  * Enumerate all the input method entries.
139  *
140  * @return return true if the enumeration is done without interruption.
141  */
142  bool foreachEntries(
143  const std::function<bool(const InputMethodEntry &entry)> &callback);
144 
145  /**
146  * Check if there is new entries could be loaded.
147  *
148  * @return whether need to perform update.
149  * @see InputMethodManager::refresh
150  */
151  bool checkUpdate() const;
152 
153  /**
154  * Emit the signal when current group is about to change.
155  *
156  * @see InputMethodManager::setCurrentGroup
157  * @see InputMethodManager::removeGroup
158  * @see InputMethodManager::setGroup
159  * @see InputMethodManager::load
160  * @see InputMethodManager::reset
161  */
162  FCITX_DECLARE_SIGNAL(InputMethodManager, CurrentGroupAboutToChange,
163  void(const std::string &group));
164  FCITX_DECLARE_SIGNAL(InputMethodManager, CurrentGroupChanged,
165  void(const std::string &group));
166 
167  /**
168  * Emit the signal when a new group is added.
169  *
170  * This will not be emitted when building the group.
171  *
172  * @see InputMethodManager::addEmptyGroup
173  * @since 5.0.8
174  */
176  void(const std::string &group));
177 
178  /**
179  * Emit the signal when a group is removed.
180  *
181  * This will not be emitted when building the group.
182  *
183  * @see InputMethodManager::removeGroup
184  * @since 5.0.8
185  */
187  void(const std::string &group));
188 
189 private:
190  std::unique_ptr<InputMethodManagerPrivate> d_ptr;
191  FCITX_DECLARE_PRIVATE(InputMethodManager);
192 };
193 } // namespace fcitx
194 
195 #endif // _FCITX_INPUTMETHODMANAGER_H_
Base class for all object supports connection.
#define FCITX_DECLARE_SIGNAL(CLASS_NAME, NAME,...)
Declare signal by type.
Definition: action.cpp:17
Utilities to enable use object with signal.
Class to manage all the input method relation information.