OLD | NEW |
---|---|
(Empty) | |
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 #include "chromeos/dbus/ibus/ibus_lookup_table.h" | |
6 | |
7 #include <string> | |
8 #include "base/logging.h" | |
9 #include "dbus/message.h" | |
10 #include "chromeos/dbus/ibus/ibus_text.h" | |
11 #include "chromeos/dbus/ibus/ibus_object.h" | |
12 | |
13 namespace chromeos { | |
14 | |
15 void AppendIBusLookupTable(const IBusLookupTable& table, | |
16 dbus::MessageWriter* writer) { | |
17 IBusObjectWriter ibus_lookup_table_writer("IBusLookupTable", | |
18 "uubbiavav", | |
19 writer); | |
20 ibus_lookup_table_writer.AppendUint32(table.page_size()); | |
21 ibus_lookup_table_writer.AppendUint32(table.cursor_position()); | |
22 ibus_lookup_table_writer.AppendBool(table.is_cursor_visible()); | |
23 ibus_lookup_table_writer.AppendBool(false); // Not used in Chrome. | |
24 ibus_lookup_table_writer.AppendInt32(static_cast<int32>(table.orientation())); | |
25 | |
26 const std::vector<std::string>& candidates = table.candidates(); | |
27 dbus::MessageWriter text_writer(NULL); | |
28 ibus_lookup_table_writer.OpenArray("v", &text_writer); | |
29 for (size_t i = 0; i < candidates.size(); ++i) { | |
30 // Write candidate string as IBusText. | |
31 IBusText text; | |
32 text.set_text(candidates[i]); | |
33 AppendIBusText(text, &text_writer); | |
34 } | |
35 ibus_lookup_table_writer.CloseContainer(&text_writer); | |
36 | |
37 const std::vector<std::string>& labels = table.labels(); | |
38 dbus::MessageWriter label_writer(NULL); | |
39 ibus_lookup_table_writer.OpenArray("v", &label_writer); | |
40 for (size_t i = 0; i < labels.size(); ++i) { | |
41 // Write label string as IBusText. | |
42 IBusText text; | |
43 text.set_text(labels[i]); | |
44 AppendIBusText(text, &label_writer); | |
45 } | |
46 ibus_lookup_table_writer.CloseContainer(&label_writer); | |
47 | |
48 ibus_lookup_table_writer.CloseAll(); | |
49 } | |
50 | |
51 bool PopIBusLookupTable(dbus::MessageReader* reader, IBusLookupTable* table) { | |
52 IBusObjectReader ibus_object_reader("IBusLookupTable", reader); | |
53 if (!ibus_object_reader.Init()) | |
54 return false; | |
55 | |
56 uint32 page_size = 0; | |
57 if (!ibus_object_reader.PopUint32(&page_size)) { | |
58 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
59 << "1st argument should be uint32."; | |
60 return false; | |
61 } | |
62 table->set_page_size(page_size); | |
63 | |
64 uint32 cursor_position = 0; | |
65 if (!ibus_object_reader.PopUint32(&cursor_position)) { | |
66 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
67 << "2nd argument should be uint32."; | |
68 return false; | |
69 } | |
70 table->set_cursor_position(cursor_position); | |
71 | |
72 bool cursor_visible = true; | |
73 if (!ibus_object_reader.PopBool(&cursor_visible)) { | |
74 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
75 << "3rd arugment should be boolean."; | |
76 return false; | |
77 } | |
78 table->set_is_cursor_visible(cursor_visible); | |
79 | |
80 bool unused_round_value = true; | |
81 if (!ibus_object_reader.PopBool(&unused_round_value)) { | |
82 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
83 << "4th argument should be boolean."; | |
84 return false; | |
85 } | |
86 | |
87 int32 orientation = 0; | |
88 if (!ibus_object_reader.PopInt32(&orientation)) { | |
89 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
90 << "5th arguemnt should be int32."; | |
91 return false; | |
92 } | |
93 table->set_orientation( | |
94 static_cast<IBusLookupTable::Orientation>(orientation)); | |
95 | |
96 dbus::MessageReader text_array_reader(NULL); | |
97 if (!ibus_object_reader.PopArray(&text_array_reader)) { | |
98 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
99 << "6th argument should be array."; | |
100 return false; | |
101 } | |
102 | |
103 std::vector<std::string>* candidates = table->mutable_candidates(); | |
104 while (text_array_reader.HasMoreData()) { | |
105 IBusText text; | |
106 if (!PopIBusText(&text_array_reader, &text)) { | |
107 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
108 << "6th argument should be array of IBusText."; | |
109 return false; | |
110 } | |
111 // The attributes in IBusText are not used in Chrome. | |
112 candidates->push_back(text.text()); | |
113 } | |
114 | |
115 dbus::MessageReader label_array_reader(NULL); | |
116 if (!ibus_object_reader.PopArray(&label_array_reader)) { | |
117 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
118 << "7th argument should be array."; | |
119 return false; | |
120 } | |
121 | |
122 std::vector<std::string>* labels = table->mutable_labels(); | |
123 while (label_array_reader.HasMoreData()) { | |
124 IBusText text; | |
125 if (!PopIBusText(&label_array_reader, &text)) { | |
126 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " | |
127 << "7th argument should be array of IBusText."; | |
128 return false; | |
129 } | |
130 // The attributes in IBusText are not used in Chrome. | |
131 labels->push_back(text.text()); | |
132 } | |
133 return true; | |
134 } | |
135 | |
136 /////////////////////////////////////////////////////////////////////////////// | |
137 // IBusLookupTable | |
138 IBusLookupTable::IBusLookupTable() | |
139 : page_size_(0), | |
140 cursor_position_(0), | |
141 is_cursor_visible_(true), | |
142 orientation_(IBUS_LOOKUP_TABLE_ORIENTATION_HORIZONTAL) { | |
143 } | |
144 | |
145 IBusLookupTable::~IBusLookupTable() { | |
146 } | |
147 | |
148 uint32 IBusLookupTable::page_size() const { | |
149 return page_size_; | |
150 } | |
satorux1
2012/05/11 22:55:29
Please incline simpler getters/setters in .h file.
Seigo Nonaka
2012/05/18 17:57:46
Done.
| |
151 | |
152 void IBusLookupTable::set_page_size(uint32 page_size) { | |
153 page_size_ = page_size; | |
154 } | |
155 | |
156 uint32 IBusLookupTable::cursor_position() const { | |
157 return cursor_position_; | |
158 } | |
159 | |
160 void IBusLookupTable::set_cursor_position(uint32 cursor_position) { | |
161 cursor_position_ = cursor_position; | |
162 } | |
163 | |
164 bool IBusLookupTable::is_cursor_visible() const { | |
165 return is_cursor_visible_; | |
166 } | |
167 | |
168 void IBusLookupTable::set_is_cursor_visible(bool is_cursor_visible) { | |
169 is_cursor_visible_ = is_cursor_visible; | |
170 } | |
171 | |
172 IBusLookupTable::Orientation IBusLookupTable::orientation() const { | |
173 return orientation_; | |
174 } | |
175 | |
176 void IBusLookupTable::set_orientation(Orientation orientation) { | |
177 orientation_ = orientation; | |
178 } | |
179 | |
180 const std::vector<std::string>& IBusLookupTable::candidates() const { | |
181 return candidates_; | |
182 } | |
183 | |
184 std::vector<std::string>* IBusLookupTable::mutable_candidates() { | |
185 return &candidates_; | |
186 } | |
187 | |
188 const std::vector<std::string>& IBusLookupTable::labels() const { | |
189 return labels_; | |
190 } | |
191 | |
192 std::vector<std::string>* IBusLookupTable::mutable_labels() { | |
193 return &labels_; | |
194 } | |
195 | |
196 } // namespace chromeos | |
OLD | NEW |