- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_engine_ibus.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_IBUS_H_
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_IBUS_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
12 #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
13 #include "chromeos/dbus/ibus/ibus_engine_service.h"
14 #include "chromeos/ime/ibus_bridge.h"
15 #include "dbus/object_path.h"
16
17 namespace chromeos {
18
19 class IBusComponent;
20 class IBusText;
21
22 class IBusEngineService;
23 namespace input_method {
24 class CandidateWindow;
25 struct KeyEventHandle;
26 }  // namespace input_method
27
28 class InputMethodEngineIBus : public InputMethodEngine,
29                               public IBusEngineHandlerInterface {
30  public:
31   InputMethodEngineIBus();
32
33   virtual ~InputMethodEngineIBus();
34
35   void Initialize(
36       InputMethodEngine::Observer* observer,
37       const char* engine_name,
38       const char* extension_id,
39       const char* engine_id,
40       const char* description,
41       const std::vector<std::string>& languages,
42       const std::vector<std::string>& layouts,
43       const GURL& options_page,
44       std::string* error);
45
46   // InputMethodEngine overrides.
47   virtual void StartIme() OVERRIDE;
48   virtual bool SetComposition(int context_id,
49                               const char* text,
50                               int selection_start,
51                               int selection_end,
52                               int cursor,
53                               const std::vector<SegmentInfo>& segments,
54                               std::string* error) OVERRIDE;
55   virtual bool ClearComposition(int context_id, std::string* error) OVERRIDE;
56   virtual bool CommitText(int context_id, const char* text,
57                           std::string* error) OVERRIDE;
58   virtual const CandidateWindowProperty&
59     GetCandidateWindowProperty() const OVERRIDE;
60   virtual void SetCandidateWindowProperty(
61       const CandidateWindowProperty& property) OVERRIDE;
62   virtual bool SetCandidateWindowVisible(bool visible,
63                                          std::string* error) OVERRIDE;
64   virtual void SetCandidateWindowAuxText(const char* text) OVERRIDE;
65   virtual void SetCandidateWindowAuxTextVisible(bool visible) OVERRIDE;
66   virtual bool SetCandidates(int context_id,
67                              const std::vector<Candidate>& candidates,
68                              std::string* error) OVERRIDE;
69   virtual bool SetCursorPosition(int context_id, int candidate_id,
70                                  std::string* error) OVERRIDE;
71   virtual bool SetMenuItems(const std::vector<MenuItem>& items) OVERRIDE;
72   virtual bool UpdateMenuItems(const std::vector<MenuItem>& items) OVERRIDE;
73   virtual bool IsActive() const OVERRIDE;
74   virtual void KeyEventDone(input_method::KeyEventHandle* key_data,
75                             bool handled) OVERRIDE;
76   virtual bool DeleteSurroundingText(int context_id,
77                                      int offset,
78                                      size_t number_of_chars,
79                                      std::string* error) OVERRIDE;
80
81   // IBusEngineHandlerInterface overrides.
82   virtual void FocusIn(ibus::TextInputType text_input_type) OVERRIDE;
83   virtual void FocusOut() OVERRIDE;
84   virtual void Enable() OVERRIDE;
85   virtual void Disable() OVERRIDE;
86   virtual void PropertyActivate(const std::string& property_name) OVERRIDE;
87   virtual void PropertyShow(const std::string& property_name) OVERRIDE;
88   virtual void PropertyHide(const std::string& property_name) OVERRIDE;
89   virtual void SetCapability(IBusCapability capability) OVERRIDE;
90   virtual void Reset() OVERRIDE;
91   virtual void ProcessKeyEvent(uint32 keysym, uint32 keycode, uint32 state,
92                                const KeyEventDoneCallback& callback) OVERRIDE;
93   virtual void CandidateClicked(uint32 index, ibus::IBusMouseButton button,
94                                 uint32 state) OVERRIDE;
95   virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos,
96                                   uint32 anchor_pos) OVERRIDE;
97
98   // Called when the connection with ibus-daemon is connected.
99   void OnConnected();
100
101   // Called whtn the connection with ibus-daemon is disconnected.
102   void OnDisconnected();
103
104  private:
105   // Returns true if the connection to ibus-daemon is avaiable.
106   bool IsConnected();
107
108   // Converts MenuItem to InputMethodProperty.
109   void MenuItemToProperty(const MenuItem& item,
110                           input_method::InputMethodProperty* property);
111
112   // Registers the engine component.
113   void RegisterComponent();
114
115   // Called when the RegisterComponent is failed.
116   void OnComponentRegistrationFailed();
117
118   // Called when the RegisterComponent is succeeded.
119   void OnComponentRegistered();
120
121   // Called when the ibus-daemon sends CreateEngine message with corresponding
122   // engine id.
123   void CreateEngineHandler(
124       const IBusEngineFactoryService::CreateEngineResponseSender& sender);
125
126   // Returns current IBusEngineService, if there is no available service, this
127   // function returns NULL.
128   IBusEngineService* GetCurrentService();
129
130   // True if the current context has focus.
131   bool focused_;
132
133   // True if this engine is active.
134   bool active_;
135
136   // ID that is used for the current input context.  False if there is no focus.
137   int context_id_;
138
139   // Next id that will be assigned to a context.
140   int next_context_id_;
141
142   // This IME ID in Chrome Extension.
143   std::string engine_id_;
144
145   // This IME ID in ibus.
146   std::string ibus_id_;
147
148   // The current object path.
149   dbus::ObjectPath object_path_;
150
151   // The current auxialy text and it's visiblity.
152   scoped_ptr<IBusText> aux_text_;
153   bool aux_text_visible_;
154
155   // Pointer to the object recieving events for this IME.
156   InputMethodEngine::Observer* observer_;
157
158   // The current preedit text, and it's cursor position.
159   scoped_ptr<IBusText> preedit_text_;
160   int preedit_cursor_;
161
162   // The current engine component.
163   scoped_ptr<IBusComponent> component_;
164
165   // The current candidate window.
166   scoped_ptr<input_method::CandidateWindow> candidate_window_;
167
168   // The current candidate window property.
169   CandidateWindowProperty candidate_window_property_;
170
171   // Indicates whether the candidate window is visible.
172   bool window_visible_;
173
174   // Mapping of candidate index to candidate id.
175   std::vector<int> candidate_ids_;
176
177   // Mapping of candidate id to index.
178   std::map<int, int> candidate_indexes_;
179
180   // Used for making callbacks.
181   base::WeakPtrFactory<InputMethodEngineIBus> weak_ptr_factory_;
182 };
183
184 }  // namespace chromeos
185
186 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_IBUS_H_